// File: /workspace/github_cpp_makefile_star2_O2/YiweiMao[P]tessDX/src/tessellator.cpp
// Function: CHWTessellator::TriProcessTessFactors(float, float, float, float, CHWTessellator::PROCESSED_TESS_FACTORS_TRI&)
void CHWTessellator::TriProcessTessFactors( float tessFactor_Ueq0, float tessFactor_Veq0, float tessFactor_Weq0, 
                                            float insideTessFactor, PROCESSED_TESS_FACTORS_TRI& processedTessFactors )
{
    // Is the patch culled?
    if( !(tessFactor_Ueq0 > 0) || // NaN will pass
        !(tessFactor_Veq0 > 0) ||
        !(tessFactor_Weq0 > 0) )
    {
        processedTessFactors.bPatchCulled = true;
        return;
    }
    else
    {
        processedTessFactors.bPatchCulled = false;
    }

    // Clamp edge TessFactors
    float lowerBound, upperBound;
    switch(m_originalPartitioning)
    {
        case D3D11_TESSELLATOR_PARTITIONING_INTEGER:
        case D3D11_TESSELLATOR_PARTITIONING_POW2: // don�t care about pow2 distinction for validation, just treat as integer
            lowerBound = D3D11_TESSELLATOR_MIN_ODD_TESSELLATION_FACTOR;
            upperBound = D3D11_TESSELLATOR_MAX_EVEN_TESSELLATION_FACTOR;
            break;
         
        case D3D11_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
            lowerBound = D3D11_TESSELLATOR_MIN_EVEN_TESSELLATION_FACTOR;
            upperBound = D3D11_TESSELLATOR_MAX_EVEN_TESSELLATION_FACTOR;
            break;

        case D3D11_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
            lowerBound = D3D11_TESSELLATOR_MIN_ODD_TESSELLATION_FACTOR;
            upperBound = D3D11_TESSELLATOR_MAX_ODD_TESSELLATION_FACTOR;
            break;
    }

    tessFactor_Ueq0 = fmin( upperBound, fmax( lowerBound, tessFactor_Ueq0 ) );
    tessFactor_Veq0 = fmin( upperBound, fmax( lowerBound, tessFactor_Veq0 ) );
    tessFactor_Weq0 = fmin( upperBound, fmax( lowerBound, tessFactor_Weq0 ) );

    if( HWIntegerPartitioning()) // pow2 or integer, round to next int (hw doesn't care about pow2 distinction)
    {
        tessFactor_Ueq0 = ceil(tessFactor_Ueq0);
        tessFactor_Veq0 = ceil(tessFactor_Veq0);
        tessFactor_Weq0 = ceil(tessFactor_Weq0);
    }

    // Clamp inside TessFactors
    if(D3D11_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD == m_originalPartitioning)
    {
        if( (tessFactor_Ueq0 > MIN_ODD_TESSFACTOR_PLUS_HALF_EPSILON) ||
            (tessFactor_Veq0 > MIN_ODD_TESSFACTOR_PLUS_HALF_EPSILON) ||
            (tessFactor_Weq0 > MIN_ODD_TESSFACTOR_PLUS_HALF_EPSILON)) 
            // Don't need the same check for insideTessFactor for tri patches, 
            // since there is only one insideTessFactor, as opposed to quad 
            // patches which have 2 insideTessFactors.
        {
            // Force picture frame
            lowerBound = D3D11_TESSELLATOR_MIN_ODD_TESSELLATION_FACTOR + EPSILON;
        }
    }

    insideTessFactor = fmin( upperBound, fmax( lowerBound, insideTessFactor ) );
    // Note the above clamps map NaN to lowerBound

    if( HWIntegerPartitioning()) // pow2 or integer, round to next int (hw doesn't care about pow2 distinction)
    {
        insideTessFactor = ceil(insideTessFactor);
    }

    // Reset our vertex and index buffers.  We have enough storage for the max tessFactor.
    m_NumPoints = 0;
    m_NumIndices = 0;

    // Process tessFactors
    float outsideTessFactor[TRI_EDGES] = {tessFactor_Ueq0, tessFactor_Veq0, tessFactor_Weq0};
    int edge;
    if( HWIntegerPartitioning() )
    {
        for( edge = 0; edge < TRI_EDGES; edge++ )
        {
            int edgeEven = isEven(outsideTessFactor[edge]);
            processedTessFactors.outsideTessFactorParity[edge] = edgeEven ? TESSELLATOR_PARITY_EVEN : TESSELLATOR_PARITY_ODD;
        }
        processedTessFactors.insideTessFactorParity = (isEven(insideTessFactor) || (FLOAT_ONE == insideTessFactor))
                                        ? TESSELLATOR_PARITY_EVEN : TESSELLATOR_PARITY_ODD;
    }
    else
    {
        for( edge = 0; edge < TRI_EDGES; edge++ )
        {
            processedTessFactors.outsideTessFactorParity[edge] = m_originalParity;
        }
        processedTessFactors.insideTessFactorParity = m_originalParity;
    }
    
    // Save fixed point TessFactors 
    for( edge = 0; edge < TRI_EDGES; edge++ )
    {
        processedTessFactors.outsideTessFactor[edge] = floatToFixed(outsideTessFactor[edge]);
    }
    processedTessFactors.insideTessFactor = floatToFixed(insideTessFactor);

    if( HWIntegerPartitioning() || Odd() )
    {
        // Special case if all TessFactors are 1 
        if( (FXP_ONE == processedTessFactors.insideTessFactor) &&
            (FXP_ONE == processedTessFactors.outsideTessFactor[Ueq0]) &&
            (FXP_ONE == processedTessFactors.outsideTessFactor[Veq0]) &&
            (FXP_ONE == processedTessFactors.outsideTessFactor[Weq0]) )
        {
            processedTessFactors.bJustDoMinimumTessFactor = true;
            return;
        }
    }
    processedTessFactors.bJustDoMinimumTessFactor = false;

    // Compute per-TessFactor metadata
    for(edge = 0; edge < TRI_EDGES; edge++ )
    {
        SetTessellationParity(processedTessFactors.outsideTessFactorParity[edge]);
        ComputeTessFactorContext(processedTessFactors.outsideTessFactor[edge], processedTessFactors.outsideTessFactorCtx[edge]);
    }
    SetTessellationParity(processedTessFactors.insideTessFactorParity);
    ComputeTessFactorContext(processedTessFactors.insideTessFactor, processedTessFactors.insideTessFactorCtx);

    // Compute some initial data.

    // outside edge offsets and storage
    for(edge = 0; edge < TRI_EDGES; edge++ )
    {
        SetTessellationParity(processedTessFactors.outsideTessFactorParity[edge]);
        processedTessFactors.numPointsForOutsideEdge[edge] = NumPointsForTessFactor(processedTessFactors.outsideTessFactor[edge]);
        m_NumPoints += processedTessFactors.numPointsForOutsideEdge[edge];
    }
    m_NumPoints -= 3;

    // inside edge offsets
    SetTessellationParity(processedTessFactors.insideTessFactorParity);
    processedTessFactors.numPointsForInsideTessFactor = NumPointsForTessFactor(processedTessFactors.insideTessFactor);
    {
        int pointCountMin = Odd() ? 4 : 3;
        // max() allows degenerate transition regions when inside TessFactor == 1
        processedTessFactors.numPointsForInsideTessFactor = max(pointCountMin,processedTessFactors.numPointsForInsideTessFactor);
    }

    processedTessFactors.insideEdgePointBaseOffset = m_NumPoints;

    // inside storage, including interior edges above
    {
        int numInteriorRings = (processedTessFactors.numPointsForInsideTessFactor >> 1) - 1; 
        int numInteriorPoints;
        if( Odd() )
        {
            numInteriorPoints = TRI_EDGES*(numInteriorRings*(numInteriorRings+1) - numInteriorRings);
        }
        else
        {
            numInteriorPoints = TRI_EDGES*(numInteriorRings*(numInteriorRings+1)) + 1;
        }
        m_NumPoints += numInteriorPoints;
    }

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x60, %rsp
movq %rsi, %rbx
xorps %xmm4, %xmm4
ucomiss %xmm4, %xmm0
jbe 0x4036c8
ucomiss %xmm4, %xmm1
jbe 0x4036c8
ucomiss %xmm4, %xmm2
jbe 0x4036c8
movq %rdi, %r14
movb $0x0, 0x71(%rbx)
movl 0x8(%rdi), %ebp
movss 0xc19a3(%rip), %xmm5    # 0x4c5044
cmpl $0x2, %ebp
movss %xmm3, 0x8(%rsp)
je 0x4036d1
movss 0xc198c(%rip), %xmm4    # 0x4c5040
movaps %xmm4, 0x10(%rsp)
cmpl $0x3, %ebp
jne 0x4036de
movss 0xc1982(%rip), %xmm5    # 0x4c5048
jmp 0x4036de
movb $0x1, 0x71(%rbx)
jmp 0x4039b3
movss 0xc1973(%rip), %xmm3    # 0x4c504c
movaps %xmm3, 0x10(%rsp)
movaps %xmm5, %xmm3
shufps $0x0, %xmm5, %xmm3      # xmm3 = xmm3[0,0],xmm5[0,0]
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movaps %xmm0, %xmm1
maxps %xmm3, %xmm1
cmpunordps %xmm3, %xmm3
andps %xmm3, %xmm0
andnps %xmm1, %xmm3
orps %xmm0, %xmm3
movaps 0x10(%rsp), %xmm0
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movaps %xmm3, %xmm1
minps %xmm0, %xmm3
cmpunordps %xmm0, %xmm0
andps %xmm0, %xmm1
andnps %xmm3, %xmm0
orps %xmm1, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm5, 0x50(%rsp)
movaps %xmm5, %xmm0
movaps %xmm2, %xmm1
callq 0x410d00
movaps %xmm0, %xmm1
movaps 0x10(%rsp), %xmm0
callq 0x410d40
movaps %xmm0, %xmm2
movl 0xc(%r14), %r15d
cmpl $0x1, %r15d
ja 0x403791
movaps 0x20(%rsp), %xmm0
movss %xmm2, 0x4(%rsp)
callq 0x4010c8
movaps %xmm0, 0x40(%rsp)
movaps 0x20(%rsp), %xmm0
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
callq 0x4010c8
movaps 0x40(%rsp), %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movaps %xmm1, 0x40(%rsp)
movss 0x4(%rsp), %xmm0
callq 0x4010c8
movaps %xmm0, %xmm2
movaps 0x40(%rsp), %xmm3
movss 0x8(%rsp), %xmm1
jmp 0x40379c
movss 0x8(%rsp), %xmm1
movaps 0x20(%rsp), %xmm3
cmpl $0x2, %ebp
movaps 0x50(%rsp), %xmm0
movss %xmm2, 0x4(%rsp)
jne 0x4037d6
ucomiss 0xc189d(%rip), %xmm2    # 0x4c5050
ja 0x4037ce
movaps %xmm3, %xmm2
shufps $0x55, %xmm3, %xmm2     # xmm2 = xmm2[1,1],xmm3[1,1]
ucomiss 0xc188d(%rip), %xmm2    # 0x4c5050
ja 0x4037ce
ucomiss 0xc1884(%rip), %xmm3    # 0x4c5050
jbe 0x4037d6
movss 0xc187e(%rip), %xmm0    # 0x4c5054
movaps %xmm3, 0x20(%rsp)
callq 0x410d00
movaps %xmm0, %xmm1
movaps 0x10(%rsp), %xmm0
callq 0x410d40
movss %xmm0, 0x10(%rsp)
callq 0x4010c8
cmpl $0x2, %r15d
jb 0x403804
movss 0x10(%rsp), %xmm0
movss %xmm0, 0xc(%rsp)
andq $0x0, 0x28(%r14)
movaps 0x20(%rsp), %xmm1
movlps %xmm1, 0x30(%rsp)
movss 0x4(%rsp), %xmm1
movss %xmm1, 0x38(%rsp)
xorl %eax, %eax
cmpl $0x2, %r15d
jae 0x403845
cmpq $0x3, %rax
je 0x403857
cvttss2si 0x30(%rsp,%rax,4), %ecx
andl $0x1, %ecx
movl %ecx, 0x10(%rbx,%rax,4)
incq %rax
jmp 0x40382d
movl (%r14), %ecx
cmpq $0x3, %rax
je 0x40386f
movl %ecx, 0x10(%rbx,%rax,4)
incq %rax
jmp 0x403845
cvttss2si %xmm0, %eax
ucomiss 0xc17e2(%rip), %xmm0    # 0x4c5044
setp %cl
setne %dl
orb %cl, %dl
andb %al, %dl
movzbl %dl, %ecx
movl %ecx, 0x1c(%rbx)
xorl %r15d, %r15d
cmpq $0xc, %r15
je 0x403892
leaq (%rsp,%r15), %rdi
addq $0x30, %rdi
callq 0x404f49
movl %eax, (%rbx,%r15)
addq $0x4, %r15
jmp 0x403875
leaq 0xc(%rsp), %rdi
callq 0x404f49
movl %eax, 0xc(%rbx)
cmpl $0x2, 0xc(%r14)
jae 0x4038af
cmpl $0x10000, %eax          # imm = 0x10000
je 0x4038bd
jmp 0x4038e0
cmpl $0x10000, %eax          # imm = 0x10000
jne 0x4038e0
cmpl $0x1, 0x4(%r14)
jne 0x4038e0
cmpl $0x10000, (%rbx)        # imm = 0x10000
jne 0x4038e0
cmpl $0x10000, 0x4(%rbx)     # imm = 0x10000
jne 0x4038e0
cmpl $0x10000, 0x8(%rbx)     # imm = 0x10000
jne 0x4038e0
movb $0x1, 0x70(%rbx)
jmp 0x4039b3
movb $0x0, 0x70(%rbx)
leaq 0x20(%rbx), %r15
xorl %r12d, %r12d
cmpq $0x3, %r12
je 0x403912
movl 0x10(%rbx,%r12,4), %eax
movl %eax, 0x4(%r14)
movl (%rbx,%r12,4), %esi
movq %r14, %rdi
movq %r15, %rdx
callq 0x402dda
incq %r12
addq $0x14, %r15
jmp 0x4038eb
movl 0x1c(%rbx), %eax
movl %eax, 0x4(%r14)
movl 0xc(%rbx), %esi
leaq 0x5c(%rbx), %rdx
movq %r14, %rdi
callq 0x402dda
xorl %r15d, %r15d
cmpq $0x3, %r15
je 0x403954
movl 0x10(%rbx,%r15,4), %eax
movl %eax, 0x4(%r14)
movl (%rbx,%r15,4), %esi
movq %r14, %rdi
callq 0x402ebc
movl %eax, 0x74(%rbx,%r15,4)
addl %eax, 0x28(%r14)
incq %r15
jmp 0x40392b
movl 0x28(%r14), %r15d
leal -0x3(%r15), %ebp
movl %ebp, 0x28(%r14)
movl 0x1c(%rbx), %r12d
movl %r12d, 0x4(%r14)
movl 0xc(%rbx), %esi
movq %r14, %rdi
callq 0x402ebc
xorl %ecx, %ecx
cmpl $0x1, %r12d
sete %cl
addl $0x3, %ecx
cmpl %eax, %ecx
cmovbel %eax, %ecx
movl %ecx, 0x80(%rbx)
movl %ecx, %eax
shrl %eax
leal -0x1(%rax), %ecx
leal (%rcx,%rcx,2), %edx
imull %edx, %ecx
imull %edx, %eax
incl %eax
cmpl $0x1, %r12d
movl %ebp, 0x84(%rbx)
cmovel %ecx, %eax
addl %r15d, %eax
addl $-0x3, %eax
movl %eax, 0x28(%r14)
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq