// File: /workspace/github_cpp_makefile_star2_O2/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 %r14
pushq %rbx
subq $0x10, %rsp
movq %rsi, %r14
xorps %xmm2, %xmm2
ucomiss %xmm2, %xmm0
jbe 0x403f12
ucomiss %xmm2, %xmm1
jbe 0x403f12
movq %rdi, %rbx
movb $0x0, 0x30(%r14)
movl 0x8(%rdi), %eax
movss 0xc1153(%rip), %xmm2    # 0x4c5044
cmpl $0x2, %eax
je 0x403f1c
movss 0xc1142(%rip), %xmm3    # 0x4c5040
movss %xmm3, (%rsp)
cmpl $0x3, %eax
jne 0x403f29
movss 0xc1138(%rip), %xmm2    # 0x4c5048
jmp 0x403f29
movb $0x1, 0x30(%r14)
jmp 0x404023
movss 0xc1128(%rip), %xmm3    # 0x4c504c
movss %xmm3, (%rsp)
cvtss2sd %xmm0, %xmm0
maxsd 0xc11fb(%rip), %xmm0    # 0x4c5130
minsd 0xc11fb(%rip), %xmm0    # 0x4c5138
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
movaps %xmm2, %xmm0
callq 0x410d00
movaps %xmm0, %xmm1
movss (%rsp), %xmm0
callq 0x410d40
movss %xmm0, 0x4(%rsp)
andq $0x0, 0x28(%rbx)
cmpl $0x1, 0xc(%rbx)
ja 0x403f81
callq 0x4010c8
movss %xmm0, 0x4(%rsp)
cvttss2si %xmm0, %eax
andl $0x1, %eax
jmp 0x403f83
movl (%rbx), %eax
movl %eax, 0x4(%r14)
leaq 0x4(%rsp), %rdi
callq 0x404f49
movl %eax, %ebp
movl 0x4(%r14), %eax
movl %eax, 0x4(%rbx)
leaq 0x1c(%r14), %rdx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x402dda
movq %rbx, %rdi
movl %ebp, %esi
callq 0x402ebc
movl %eax, 0x34(%r14)
andl $0x0, 0xc(%rbx)
movss 0x8(%rsp), %xmm0
callq 0x4010c8
leaq 0xc(%rsp), %rdi
cvttss2si %xmm0, %eax
movss %xmm0, (%rdi)
andl $0x1, %eax
movl %eax, (%r14)
movl %eax, 0x4(%rbx)
callq 0x404f49
movl %eax, %ebp
leaq 0x8(%r14), %rdx
movq %rbx, %rdi
movl %eax, %esi
callq 0x402dda
movq %rbx, %rdi
movl %ebp, %esi
callq 0x402ebc
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 $0x10, %rsp
popq %rbx
popq %r14
popq %rbp
retq