// File: /workspace/github_cpp_makefile_star2_O2/mlugo2[P]SunshineGPU/src/gpu.cpp
// Function: gpu::rasteration(__u128*, unsigned char (*) [800][3])
void gpu::rasteration(u128 VOB[], u8 FB[][SCREEN_WIDTH][3])
{
	// Start rendering (≧д≦ヾ)
	// initialize 
	int c1, c2, c3, z;
	int xMin, xMax, yMin, yMax;

	// Get min and max from the triangle
	xMin = getXMin(VOB);
	xMax = getXMax(VOB);
	yMin = getYMin(VOB);
	yMax = getYMax(VOB);

	// Going through every pixel of the frame buffer..
	for ( int j = yMin; j < yMax; j++ )
	{
		for ( int k = xMin; k < xMax; k++)
		{
			// Calculate somethings
			c1 = perpDotProduct(VOB[0], VOB[1], k, j);
			c2 = perpDotProduct(VOB[1], VOB[2], k, j);
			c3 = perpDotProduct(VOB[2], VOB[0], k, j);

			// If true then pixel is in the triangle
			if ( (c1 > 0 && c2 > 0 && c3 > 0) || (c1 < 0 && c2 < 0 && c3 < 0) )
			{
				// Get z
				z = getZ(VOB, k, j);

				// Write to buffer or not
				if ( z < ZB[j][k])
				{
					// Update Z-buffer
					ZB[j][k] = (u8)z;

					// Render 
					FB[j][k][0] = VOB[3].x;	// R
					FB[j][k][1] = VOB[3].y; // G
					FB[j][k][2] = VOB[3].z; // B
				}
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, (%rsp)
callq 0x702a
movl %eax, %ebp
movq %rbx, %rsi
callq 0x7052
movl %eax, %r14d
movq %rbx, %rsi
callq 0x707a
movl %eax, %r13d
movq %rbx, %rsi
callq 0x70a2
movslq %ebp, %rcx
movslq %r14d, %r12
movslq %r13d, %r14
cltq
movq %rax, 0x20(%rsp)
imulq $0x960, %r14, %rax      # imm = 0x960
movq %rcx, 0x18(%rsp)
leaq (%rcx,%rcx,2), %rcx
addq %rax, %rcx
leaq (%r15,%rcx), %rbp
addq $0x2, %rbp
imulq $0x320, %r14, %r13      # imm = 0x320
addq (%rsp), %r13
cmpq 0x20(%rsp), %r14
jge 0x701b
cvtsi2ss %r14d, %xmm0
movaps %xmm0, (%rsp)
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movaps %xmm0, 0x30(%rsp)
movq %rbp, 0x28(%rsp)
movq 0x18(%rsp), %r15
cmpq %r12, %r15
jge 0x7000
cvtsi2ss %r15d, %xmm3
movsd 0x8(%rbx), %xmm2
movsd 0x18(%rbx), %xmm1
movsd 0x28(%rbx), %xmm0
movaps %xmm0, %xmm4
shufps $0x55, %xmm0, %xmm4     # xmm4 = xmm4[1,1],xmm0[1,1]
movaps %xmm3, %xmm5
subss %xmm4, %xmm5
movaps %xmm2, %xmm4
subps %xmm0, %xmm4
movaps (%rsp), %xmm6
subss %xmm0, %xmm6
mulss %xmm4, %xmm5
shufps $0x55, %xmm4, %xmm4     # xmm4 = xmm4[1,1,1,1]
mulss %xmm6, %xmm4
subss %xmm4, %xmm5
cvttss2si %xmm5, %eax
shufps $0x0, %xmm3, %xmm3      # xmm3 = xmm3[0,0,0,0]
movaps %xmm1, %xmm4
shufps $0x11, %xmm2, %xmm4     # xmm4 = xmm4[1,0],xmm2[1,0]
shufps $0xe2, %xmm2, %xmm4     # xmm4 = xmm4[2,0],xmm2[2,3]
subps %xmm4, %xmm3
movaps %xmm1, %xmm5
unpcklps %xmm0, %xmm5            # xmm5 = xmm5[0],xmm0[0],xmm5[1],xmm0[1]
unpcklps %xmm1, %xmm2            # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
subps %xmm2, %xmm5
mulps %xmm3, %xmm5
movaps 0x30(%rsp), %xmm3
subps %xmm2, %xmm3
shufps $0x11, %xmm1, %xmm0     # xmm0 = xmm0[1,0],xmm1[1,0]
shufps $0xe2, %xmm1, %xmm0     # xmm0 = xmm0[2,0],xmm1[2,3]
subps %xmm4, %xmm0
mulps %xmm3, %xmm0
subps %xmm0, %xmm5
cvttps2dq %xmm5, %xmm0
pshufd $0x50, %xmm0, %xmm1     # xmm1 = xmm0[0,0,1,1]
pcmpgtd 0x1344(%rip), %xmm1     # 0x82e0
movmskpd %xmm1, %ecx
cmpl $0x3, %ecx
sete %cl
testl %eax, %eax
jle 0x6fae
testb %cl, %cl
jne 0x6fbf
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
pand %xmm0, %xmm1
movd %xmm1, %ecx
testl %eax, %ecx
jns 0x6ff4
movq %rbx, %rsi
movl %r15d, %edx
movl %r14d, %ecx
callq 0x70f6
movzbl (%r13,%r15), %ecx
cmpl %ecx, %eax
jge 0x6ff4
movb %al, (%r13,%r15)
cvttss2si 0x3c(%rbx), %eax
movb %al, -0x2(%rbp)
cvttss2si 0x38(%rbx), %eax
movb %al, -0x1(%rbp)
cvttss2si 0x34(%rbx), %eax
movb %al, (%rbp)
incq %r15
addq $0x3, %rbp
jmp 0x6f04
incq %r14
movq 0x28(%rsp), %rbp
addq $0x960, %rbp            # imm = 0x960
addq $0x320, %r13            # imm = 0x320
jmp 0x6edd
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq