// File: /workspace/github_cpp_makefile_star2_O1/YiweiMao[P]tessDX/src/tessellator.cpp
// Function: CHWTessellator::IsoLineProcessTessFactors(float, float, CHWTessellator::PROCESSED_TESS_FACTORS_ISOLINE&)
void CHWTessellator::IsoLineProcessTessFactors( float TessFactor_V_LineDensity, float TessFactor_U_LineDetail, 
                                                PROCESSED_TESS_FACTORS_ISOLINE& processedTessFactors )
{
    // Is the patch culled?
    if( !(TessFactor_V_LineDensity > 0) || // NaN will pass
        !(TessFactor_U_LineDetail > 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_V_LineDensity = fmin( D3D11_TESSELLATOR_MAX_ISOLINE_DENSITY_TESSELLATION_FACTOR, 
                                    fmax( D3D11_TESSELLATOR_MIN_ISOLINE_DENSITY_TESSELLATION_FACTOR, TessFactor_V_LineDensity ) );
    TessFactor_U_LineDetail = fmin( upperBound, fmax( lowerBound, TessFactor_U_LineDetail ) );

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

    // Process tessFactors
    if( HWIntegerPartitioning() )
    {
        TessFactor_U_LineDetail = ceil(TessFactor_U_LineDetail);
        processedTessFactors.lineDetailParity = isEven(TessFactor_U_LineDetail) ? TESSELLATOR_PARITY_EVEN : TESSELLATOR_PARITY_ODD;
    }
    else
    {
        processedTessFactors.lineDetailParity = m_originalParity;
    }

    FXP fxpTessFactor_U_LineDetail = floatToFixed(TessFactor_U_LineDetail);
    
    SetTessellationParity(processedTessFactors.lineDetailParity);

    ComputeTessFactorContext(fxpTessFactor_U_LineDetail, processedTessFactors.lineDetailTessFactorCtx);
    processedTessFactors.numPointsPerLine = NumPointsForTessFactor(fxpTessFactor_U_LineDetail);

    OverridePartitioning(D3D11_TESSELLATOR_PARTITIONING_INTEGER);

    TessFactor_V_LineDensity = ceil(TessFactor_V_LineDensity);
    processedTessFactors.lineDensityParity = isEven(TessFactor_V_LineDensity) ? TESSELLATOR_PARITY_EVEN : TESSELLATOR_PARITY_ODD;
    SetTessellationParity(processedTessFactors.lineDensityParity);
    FXP fxpTessFactor_V_LineDensity = floatToFixed(TessFactor_V_LineDensity);
    ComputeTessFactorContext(fxpTessFactor_V_LineDensity, processedTessFactors.lineDensityTessFactorCtx);

    processedTessFactors.numLines = NumPointsForTessFactor(fxpTessFactor_V_LineDensity) - 1; // don't draw last line at V == 1.

    RestorePartitioning();

    // Compute some initial data.

    // outside edge offsets
    m_NumPoints = processedTessFactors.numPointsPerLine * processedTessFactors.numLines;
    if( m_outputPrimitive == D3D11_TESSELLATOR_OUTPUT_POINT )
    {
        m_NumIndices = m_NumPoints;
    }
    else // line
    {
        m_NumIndices = processedTessFactors.numLines*(processedTessFactors.numPointsPerLine-1)*2;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rsi, %r14
movaps %xmm0, %xmm2
movss %xmm0, 0x4(%rsp)
movss %xmm1, (%rsp)
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm2
jbe 0x4042b0
ucomiss %xmm0, %xmm1
jbe 0x4042b0
movq %rdi, %rbx
movb $0x0, 0x30(%r14)
movl 0x8(%rdi), %eax
movss 0xc1db0(%rip), %xmm3    # 0x4c6044
cmpl $0x2, %eax
je 0x4042ba
movss 0xc1d9f(%rip), %xmm0    # 0x4c6040
cmpl $0x3, %eax
jne 0x4042c2
movss 0xc1d9a(%rip), %xmm3    # 0x4c6048
jmp 0x4042c2
movb $0x1, 0x30(%r14)
jmp 0x4043dc
movss 0xc1d8a(%rip), %xmm0    # 0x4c604c
cvtss2sd %xmm2, %xmm2
maxsd 0xc1dd2(%rip), %xmm2    # 0x4c60a0
minsd 0xc1dd2(%rip), %xmm2    # 0x4c60a8
cvtsd2ss %xmm2, %xmm2
movss %xmm2, 0x4(%rsp)
movaps %xmm3, %xmm2
cmpunordss %xmm3, %xmm2
movaps %xmm2, %xmm4
andps %xmm1, %xmm4
maxss %xmm3, %xmm1
andnps %xmm1, %xmm2
orps %xmm4, %xmm2
movaps %xmm2, %xmm1
minss %xmm0, %xmm1
cmpunordss %xmm0, %xmm0
movaps %xmm0, %xmm3
andps %xmm2, %xmm3
andnps %xmm1, %xmm0
orps %xmm3, %xmm0
movss %xmm0, (%rsp)
movq $0x0, 0x28(%rbx)
cmpl $0x1, 0xc(%rbx)
ja 0x404336
callq 0x4010c8
movss %xmm0, (%rsp)
cvttss2si %xmm0, %eax
andl $0x1, %eax
jmp 0x404338
movl (%rbx), %eax
movl %eax, 0x4(%r14)
movq %rsp, %rdi
callq 0x4057c1
movl %eax, %ebp
movl 0x4(%r14), %eax
movl %eax, 0x4(%rbx)
leaq 0x1c(%r14), %rdx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x403060
movq %rbx, %rdi
movl %ebp, %esi
callq 0x40318a
movl %eax, 0x34(%r14)
movl $0x0, 0xc(%rbx)
leaq 0x4(%rsp), %r15
movss (%r15), %xmm0
callq 0x4010c8
cvttss2si %xmm0, %eax
movss %xmm0, (%r15)
andl $0x1, %eax
movl %eax, (%r14)
movl %eax, 0x4(%rbx)
movq %r15, %rdi
callq 0x4057c1
movl %eax, %ebp
leaq 0x8(%r14), %rdx
movq %rbx, %rdi
movl %eax, %esi
callq 0x403060
movq %rbx, %rdi
movl %ebp, %esi
callq 0x40318a
decl %eax
movl %eax, 0x38(%r14)
movl 0x8(%rbx), %ecx
movl %ecx, 0xc(%rbx)
movl 0x34(%r14), %ecx
movl %ecx, %edx
imull %eax, %edx
movl %edx, 0x28(%rbx)
decl %ecx
imull %eax, %ecx
addl %ecx, %ecx
cmpl $0x0, 0x10(%rbx)
cmovel %edx, %ecx
movl %ecx, 0x2c(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nop