// File: /workspace/github_cpp_makefile_star2_O2/YiweiMao[P]tessDX/src/tessellator.cpp
// Function: CHWTessellator::StitchTransition(int, int, int, CHWTessellator::TESSELLATOR_PARITY, int, int, CHWTessellator::TESSELLATOR_PARITY)
void CHWTessellator::StitchTransition(int baseIndexOffset,
                                    int insideEdgePointBaseOffset, int insideNumHalfTessFactorPoints, 
                                    TESSELLATOR_PARITY insideEdgeTessFactorParity,
                                    int outsideEdgePointBaseOffset, int outsideNumHalfTessFactorPoints,
                                    TESSELLATOR_PARITY outsideTessFactorParity
)
{

#ifdef ALLOW_XBOX_360_COMPARISON
    // Tables to assist in the stitching of 2 rows of points having arbitrary TessFactors.
    // The stitching order is governed by Ruler Function vertex split ordering (see external documentation).
    //
    // The contents of the finalPointPositionTable are where vertex i [0..32] ends up on the half-edge
    // at the max tessellation amount given ruler-function split order.  
    // Recall the other half of an edge is mirrored, so we only need to deal with one half.
    // This table is used to decide when to advance a point on the interior or exterior.
    // It supports odd TessFactor up to 65 and even TessFactor up to 64.
    static const int _finalPointPositionTable[33] = 
            { 0, 32, 16, 8, 17, 4, 18, 9, 19, 2, 20, 10, 21, 5, 22, 11, 23,
              1, 24, 12, 25, 6, 26, 13, 27, 3, 28, 14, 29, 7, 30, 15, 31 };
    // The loopStart and loopEnd tables below just provide optimal loop bounds for the
    // stitching algorithm further below, for any given halfTssFactor.  
    // There is probably a better way to encode this...

    // loopStart[halfTessFactor] encodes the FIRST entry other that [0] in finalPointPositionTable[] above which is
    // less than halfTessFactor.  Exceptions are entry 0 and 1, which are set up to skip the loop.
    static const int _loopStart[33] = 
            {1,1,17,9,9,5,5,5,5,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};
    // loopStart[halfTessFactor] encodes the LAST entry in finalPointPositionTable[] above which is
    // less than halfTessFactor.  Exceptions are entry 0 and 1, which are set up to skip the loop.
    static const int _loopEnd[33] = 
            {0,0,17,17,25,25,25,25,29,29,29,29,29,29,29,29,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,32};
    const int* finalPointPositionTable;
    const int* loopStart;
    const int* loopEnd;
    if( m_bXBox360Mode )
    {
        // The XBox360 vertex introduction order is always from the center of the edge.
        // So the final positions of points on the half-edge are this trivial table.
        static const int XBOXfinalPointPositionTable[33] = 
                { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
                  18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 };
        // loopStart and loopEnd (meaning described above) also become trivial for XBox360 splitting.
        static const int XBOXloopStart[33] = 
                {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
        static const int XBOXloopEnd[33] = 
                {0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};

        finalPointPositionTable = XBOXfinalPointPositionTable;
        loopStart = XBOXloopStart;
        loopEnd = XBOXloopEnd;
    }
    else
    {
        finalPointPositionTable = _finalPointPositionTable;
        loopStart = _loopStart;
        loopEnd =_loopEnd;
    }
#else
    // Tables to assist in the stitching of 2 rows of points having arbitrary TessFactors.
    // The stitching order is governed by Ruler Function vertex split ordering (see external documentation).
    //
    // The contents of the finalPointPositionTable are where vertex i [0..33] ends up on the half-edge
    // at the max tessellation amount given ruler-function split order.  
    // Recall the other half of an edge is mirrored, so we only need to deal with one half.
    // This table is used to decide when to advance a point on the interior or exterior.
    // It supports odd TessFactor up to 65 and even TessFactor up to 64.
    static const int finalPointPositionTable[33] = 
            { 0, 32, 16, 8, 17, 4, 18, 9, 19, 2, 20, 10, 21, 5, 22, 11, 23,
              1, 24, 12, 25, 6, 26, 13, 27, 3, 28, 14, 29, 7, 30, 15, 31 };

    // The loopStart and loopEnd tables below just provide optimal loop bounds for the
    // stitching algorithm further below, for any given halfTssFactor.  
    // There is probably a better way to encode this...

    // loopStart[halfTessFactor] encodes the FIRST entry in finalPointPositionTable[] above which is
    // less than halfTessFactor.  Exceptions are entry 0 and 1, which are set up to skip the loop.
    static const int loopStart[33] = 
            {1,1,17,9,9,5,5,5,5,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};
    // loopStart[halfTessFactor] encodes the LAST entry in finalPointPositionTable[] above which is
    // less than halfTessFactor.  Exceptions are entry 0 and 1, which are set up to skip the loop.
    static const int loopEnd[33] = 
            {0,0,17,17,25,25,25,25,29,29,29,29,29,29,29,29,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,32};
#endif
    if( TESSELLATOR_PARITY_ODD == insideEdgeTessFactorParity )
    {
        insideNumHalfTessFactorPoints -= 1;
    }
    if( TESSELLATOR_PARITY_ODD == outsideTessFactorParity )
    {
        outsideNumHalfTessFactorPoints -= 1;
    }
    // Walk first half
    int outsidePoint = outsideEdgePointBaseOffset; 
    int insidePoint = insideEdgePointBaseOffset; 

    // iStart,iEnd are a small optimization so the loop below doesn't have to go from 0 up to 31
    int iStart = min(loopStart[insideNumHalfTessFactorPoints],loopStart[outsideNumHalfTessFactorPoints]);
    int iEnd = max(loopEnd[insideNumHalfTessFactorPoints],loopEnd[outsideNumHalfTessFactorPoints]);

    if( finalPointPositionTable[0] < outsideNumHalfTessFactorPoints ) // since we dont' start the loop at 0 below, we need a special case.
    {
        // Advance outside
        DefineClockwiseTriangle(outsidePoint,outsidePoint+1,insidePoint,baseIndexOffset);
        baseIndexOffset += 3; outsidePoint++;
    }

    for(int i = iStart; i <= iEnd; i++)
    {
        if( /*(i>0) && <-- not needed since iStart is never 0*/(finalPointPositionTable[i] < insideNumHalfTessFactorPoints))
        {
            // Advance inside
            DefineClockwiseTriangle(insidePoint,outsidePoint,insidePoint+1,baseIndexOffset);
            baseIndexOffset += 3; insidePoint++;
        }
        if((finalPointPositionTable[i] < outsideNumHalfTessFactorPoints))
        {
            // Advance outside
            DefineClockwiseTriangle(outsidePoint,outsidePoint+1,insidePoint,baseIndexOffset);
            baseIndexOffset += 3; outsidePoint++;
        }
    }

    if( (insideEdgeTessFactorParity != outsideTessFactorParity) || (insideEdgeTessFactorParity == TESSELLATOR_PARITY_ODD))
    {
        if( insideEdgeTessFactorParity == outsideTessFactorParity )
        {
            // Quad in the middle
            DefineClockwiseTriangle(insidePoint,outsidePoint,insidePoint+1,baseIndexOffset);
            baseIndexOffset += 3;
            DefineClockwiseTriangle(insidePoint+1,outsidePoint,outsidePoint+1,baseIndexOffset);
            baseIndexOffset += 3;
            insidePoint++;
            outsidePoint++;
        }
        else if( TESSELLATOR_PARITY_EVEN == insideEdgeTessFactorParity )
        {
            // Triangle pointing inside
            DefineClockwiseTriangle(insidePoint,outsidePoint,outsidePoint+1,baseIndexOffset);
            baseIndexOffset += 3;
            outsidePoint++;
        }
        else
        {
            // Triangle pointing outside
            DefineClockwiseTriangle(insidePoint,outsidePoint,insidePoint+1,baseIndexOffset);
            baseIndexOffset += 3;
            insidePoint++;
        }
    }

    // Walk second half.  
    for(int i = iEnd; i >= iStart; i--)
    {
        if((finalPointPositionTable[i] < outsideNumHalfTessFactorPoints))
        {
            // Advance outside
            DefineClockwiseTriangle(outsidePoint,outsidePoint+1,insidePoint,baseIndexOffset);
            baseIndexOffset += 3; outsidePoint++;
        }
        if( /*(i>0) && <-- not needed since iStart is never 0*/ (finalPointPositionTable[i] < insideNumHalfTessFactorPoints))
        {
            // Advance inside
            DefineClockwiseTriangle(insidePoint,outsidePoint,insidePoint+1,baseIndexOffset);
            baseIndexOffset += 3; insidePoint++;
        }
    }
    // Below case is not needed if we didn't optimize loop above and made it run from 31 down to 0.
    if((finalPointPositionTable[0] < outsideNumHalfTessFactorPoints))
    {
        DefineClockwiseTriangle(outsidePoint,outsidePoint+1,insidePoint,baseIndexOffset);
        baseIndexOffset += 3; outsidePoint++;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r9d, %r11d
movl %edx, %r15d
movl 0x68(%rsp), %ebx
movl 0x60(%rsp), %ebp
leaq 0xc2473(%rip), %rax     # 0x4c5410
leaq 0xc22bc(%rip), %r10     # 0x4c5260
cmpb $0x0, 0x30(%rdi)
cmovneq %rax, %r10
leaq 0xc24ed(%rip), %rax     # 0x4c54a0
leaq 0xc2336(%rip), %r9      # 0x4c52f0
cmovneq %rax, %r9
leaq 0xc256b(%rip), %rax     # 0x4c5530
leaq 0xc23b4(%rip), %rdx     # 0x4c5380
cmovneq %rax, %rdx
xorl %eax, %eax
movl %r8d, 0x14(%rsp)
cmpl $0x1, %r8d
sete %al
subl %eax, %ecx
xorl %eax, %eax
movl %ebx, 0x10(%rsp)
cmpl $0x1, %ebx
sete %al
subl %eax, %ebp
movl %ecx, 0xc(%rsp)
movslq %ecx, %rax
movl (%r9,%rax,4), %ebx
movl %ebp, 0x8(%rsp)
movslq %ebp, %r8
movl (%r9,%r8,4), %ebp
cmpl %ebp, %ebx
movl %ebp, %ecx
cmovll %ebx, %ecx
movl %ecx, 0x18(%rsp)
movl %esi, %r12d
movl (%rdx,%rax,4), %eax
movl (%rdx,%r8,4), %r13d
cmpl %r13d, %eax
cmovgl %eax, %r13d
movq %r10, 0x20(%rsp)
movl (%r10), %eax
movl %eax, 0x1c(%rsp)
cmpl %r8d, %eax
movq %rdi, (%rsp)
jge 0x403051
leal 0x1(%r11), %r14d
movl %r11d, %esi
movl %r14d, %edx
movl %r15d, %ecx
movl %r12d, %r8d
callq 0x402592
addl $0x3, %r12d
movl %r14d, %r11d
cmpl %ebx, %ebp
cmovll %ebp, %ebx
movslq %ebx, %rbx
movslq %r13d, %rbp
cmpq %rbp, %rbx
jg 0x4030d3
movq 0x20(%rsp), %rax
movl (%rax,%rbx,4), %r14d
cmpl 0xc(%rsp), %r14d
jge 0x403096
leal 0x1(%r15), %r13d
movq (%rsp), %rdi
movl %r15d, %esi
movq %r11, %r15
movl %r11d, %edx
movl %r13d, %ecx
movl %r12d, %r8d
callq 0x402592
addl $0x3, %r12d
movl %r13d, %eax
jmp 0x40309c
movq %r15, %rax
movq %r11, %r15
cmpl 0x8(%rsp), %r14d
jge 0x4030c8
leal 0x1(%r15), %r13d
movq (%rsp), %rdi
movl %r15d, %esi
movl %r13d, %edx
movq %rax, %r15
movl %r15d, %ecx
movl %r12d, %r8d
callq 0x402592
addl $0x3, %r12d
movl %r13d, %r11d
jmp 0x4030ce
movq %r15, %r11
movq %rax, %r15
incq %rbx
jmp 0x40305c
movl 0x14(%rsp), %edx
cmpl $0x1, %edx
sete %al
movl 0x10(%rsp), %esi
cmpl %esi, %edx
setne %cl
orb %al, %cl
je 0x403135
cmpl %esi, %edx
jne 0x4031e1
leal 0x1(%r15), %r13d
movq (%rsp), %rbx
movq %rbx, %rdi
movl %r15d, %esi
movl %r11d, %edx
movl %r13d, %ecx
movl %r12d, %r8d
movq %r11, %r14
callq 0x402592
leal 0x3(%r12), %r8d
leal 0x1(%r14), %r15d
movq %rbx, %rdi
movl %r13d, %esi
movl %r14d, %edx
movl %r15d, %ecx
callq 0x402592
addl $0x6, %r12d
movl %r15d, %r11d
movl %r13d, %r15d
movl %r12d, %r13d
movslq 0x18(%rsp), %rbx
cmpq %rbx, %rbp
jl 0x4031a4
movq 0x20(%rsp), %rax
movl (%rax,%rbp,4), %r14d
cmpl 0x8(%rsp), %r14d
jge 0x403172
leal 0x1(%r11), %r12d
movq (%rsp), %rdi
movl %r11d, %esi
movl %r12d, %edx
movl %r15d, %ecx
movl %r13d, %r8d
callq 0x402592
addl $0x3, %r13d
movl %r12d, %r11d
cmpl 0xc(%rsp), %r14d
jge 0x40319f
leal 0x1(%r15), %r12d
movq (%rsp), %rdi
movl %r15d, %esi
movl %r11d, %edx
movl %r12d, %ecx
movl %r13d, %r8d
movq %r11, %r14
callq 0x402592
movq %r14, %r11
addl $0x3, %r13d
movl %r12d, %r15d
decq %rbp
jmp 0x40313d
movl 0x1c(%rsp), %eax
cmpl 0x8(%rsp), %eax
jge 0x4031d2
leal 0x1(%r11), %edx
movq (%rsp), %rdi
movl %r11d, %esi
movl %r15d, %ecx
movl %r13d, %r8d
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x402592
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leal 0x3(%r12), %r13d
testl %edx, %edx
movq (%rsp), %rdi
je 0x403211
leal 0x1(%r15), %r14d
movl %r15d, %esi
movl %r11d, %edx
movl %r14d, %ecx
movl %r12d, %r8d
movq %r11, %rbx
callq 0x402592
movq %rbx, %r11
movl %r14d, %r15d
jmp 0x403138
leal 0x1(%r11), %ebx
movl %r15d, %esi
movl %r11d, %edx
movl %ebx, %ecx
movl %r12d, %r8d
callq 0x402592
movl %ebx, %r11d
jmp 0x403138
nop