// File: /workspace/github_cpp_makefile_star2_O1/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 $0x50, %rsp
movq %rsi, %rbx
movss %xmm3, 0x8(%rsp)
xorps %xmm4, %xmm4
ucomiss %xmm4, %xmm0
jbe 0x40391b
ucomiss %xmm4, %xmm1
jbe 0x40391b
ucomiss %xmm4, %xmm2
jbe 0x40391b
movq %rdi, %r14
movb $0x0, 0x71(%rbx)
movl 0x8(%rdi), %ebp
movss 0xc2745(%rip), %xmm7    # 0x4c6044
cmpl $0x2, %ebp
je 0x403924
movss 0xc2734(%rip), %xmm6    # 0x4c6040
cmpl $0x3, %ebp
jne 0x40392c
movss 0xc272f(%rip), %xmm7    # 0x4c6048
jmp 0x40392c
movb $0x1, 0x71(%rbx)
jmp 0x403c30
movss 0xc2720(%rip), %xmm6    # 0x4c604c
movaps %xmm7, %xmm4
cmpunordss %xmm7, %xmm4
movaps %xmm4, %xmm5
andps %xmm0, %xmm5
maxss %xmm7, %xmm0
andnps %xmm0, %xmm4
orps %xmm5, %xmm4
movaps %xmm6, %xmm8
cmpunordss %xmm6, %xmm8
movaps %xmm4, %xmm0
minss %xmm6, %xmm0
ucomiss %xmm7, %xmm7
jp 0x40395e
maxss %xmm7, %xmm1
movaps %xmm8, %xmm5
andps %xmm4, %xmm5
movaps %xmm8, %xmm4
andnps %xmm0, %xmm4
jp 0x403972
maxss %xmm7, %xmm2
orps %xmm5, %xmm4
ucomiss %xmm6, %xmm6
jp 0x40397e
minss %xmm6, %xmm1
jp 0x403984
minss %xmm6, %xmm2
movss %xmm1, (%rsp)
movl 0xc(%r14), %r15d
cmpl $0x1, %r15d
ja 0x4039f6
movaps %xmm4, %xmm0
movss %xmm2, 0x4(%rsp)
movaps %xmm3, 0x40(%rsp)
movss %xmm6, 0x10(%rsp)
movss %xmm7, 0xc(%rsp)
movaps %xmm8, 0x20(%rsp)
callq 0x4010c8
movaps %xmm0, 0x30(%rsp)
movss (%rsp), %xmm0
callq 0x4010c8
movss %xmm0, (%rsp)
movss 0x4(%rsp), %xmm0
callq 0x4010c8
movaps 0x30(%rsp), %xmm4
movaps 0x20(%rsp), %xmm8
movss 0xc(%rsp), %xmm7
movss 0x10(%rsp), %xmm6
movaps 0x40(%rsp), %xmm3
movaps %xmm0, %xmm2
cmpl $0x2, %ebp
movss %xmm2, 0x4(%rsp)
jne 0x403a29
ucomiss 0xc2648(%rip), %xmm2    # 0x4c6050
ja 0x403a21
movss (%rsp), %xmm0
ucomiss 0xc263a(%rip), %xmm0    # 0x4c6050
ja 0x403a21
ucomiss 0xc2631(%rip), %xmm4    # 0x4c6050
jbe 0x403a29
movss 0xc262b(%rip), %xmm7    # 0x4c6054
movaps %xmm4, 0x30(%rsp)
movaps %xmm7, %xmm0
cmpunordss %xmm7, %xmm0
movaps %xmm0, %xmm1
andps %xmm3, %xmm1
maxss %xmm7, %xmm3
andnps %xmm3, %xmm0
orps %xmm1, %xmm0
movaps %xmm8, %xmm1
andps %xmm0, %xmm1
minss %xmm6, %xmm0
andnps %xmm0, %xmm8
orps %xmm1, %xmm8
movaps %xmm8, 0x20(%rsp)
movaps %xmm8, %xmm0
callq 0x4010c8
cmpl $0x2, %r15d
jb 0x403a73
movaps 0x20(%rsp), %xmm0
movss %xmm0, 0x8(%rsp)
movq $0x0, 0x28(%r14)
movaps 0x30(%rsp), %xmm0
movss %xmm0, 0x14(%rsp)
movss (%rsp), %xmm0
movss %xmm0, 0x18(%rsp)
movss 0x4(%rsp), %xmm0
movss %xmm0, 0x1c(%rsp)
jae 0x403add
xorl %eax, %eax
cvttss2si 0x14(%rsp,%rax,4), %ecx
andl $0x1, %ecx
movl %ecx, 0x10(%rbx,%rax,4)
incq %rax
cmpq $0x3, %rax
jne 0x403aa7
movss 0x8(%rsp), %xmm0
cvttss2si %xmm0, %eax
ucomiss 0xc2576(%rip), %xmm0    # 0x4c6044
setp %cl
setne %dl
orb %cl, %dl
andb %al, %dl
movzbl %dl, %eax
jmp 0x403af2
xorl %eax, %eax
movl (%r14), %ecx
movl %ecx, 0x10(%rbx,%rax,4)
incq %rax
cmpq $0x3, %rax
jne 0x403adf
movl (%r14), %eax
movl %eax, 0x1c(%rbx)
xorl %r15d, %r15d
leaq (%rsp,%r15), %rdi
addq $0x14, %rdi
callq 0x4057c1
movl %eax, (%rbx,%r15)
addq $0x4, %r15
cmpq $0xc, %r15
jne 0x403af8
leaq 0x8(%rsp), %rdi
callq 0x4057c1
movl %eax, 0xc(%rbx)
cmpl $0x2, 0xc(%r14)
jae 0x403b30
cmpl $0x10000, %eax          # imm = 0x10000
je 0x403b3e
jmp 0x403b61
cmpl $0x10000, %eax          # imm = 0x10000
jne 0x403b61
cmpl $0x1, 0x4(%r14)
jne 0x403b61
cmpl $0x10000, (%rbx)        # imm = 0x10000
jne 0x403b61
cmpl $0x10000, 0x4(%rbx)     # imm = 0x10000
jne 0x403b61
cmpl $0x10000, 0x8(%rbx)     # imm = 0x10000
jne 0x403b61
movb $0x1, 0x70(%rbx)
jmp 0x403c30
movb $0x0, 0x70(%rbx)
leaq 0x20(%rbx), %r15
xorl %r12d, %r12d
movl 0x10(%rbx,%r12,4), %eax
movl %eax, 0x4(%r14)
movl (%rbx,%r12,4), %esi
movq %r14, %rdi
movq %r15, %rdx
callq 0x403060
incq %r12
addq $0x14, %r15
cmpq $0x3, %r12
jne 0x403b6c
movl 0x1c(%rbx), %eax
movl %eax, 0x4(%r14)
movl 0xc(%rbx), %esi
leaq 0x5c(%rbx), %rdx
movq %r14, %rdi
callq 0x403060
xorl %r15d, %r15d
movl 0x10(%rbx,%r15,4), %eax
movl %eax, 0x4(%r14)
movl (%rbx,%r15,4), %esi
movq %r14, %rdi
callq 0x40318a
movl %eax, 0x74(%rbx,%r15,4)
addl %eax, 0x28(%r14)
incq %r15
cmpq $0x3, %r15
jne 0x403baa
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 0x40318a
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 $0x50, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop