// File: /workspace/github_cpp_makefile_star2_O1/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 %rsi, %rbx
cvttss2si 0xc(%rsi), %r9d
movl $0x1c, %eax
movl %r9d, %r10d
movl %r10d, %ecx
movss (%rbx,%rax), %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %r10d, %xmm1
ucomiss %xmm0, %xmm1
cvttss2si %xmm0, %r10d
cmovbel %ecx, %r10d
addq $0x10, %rax
cmpq $0x3c, %rax
jne 0x75a7
movl $0x1c, %eax
movl %r9d, %ecx
movss (%rbx,%rax), %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %r9d, %xmm1
ucomiss %xmm1, %xmm0
cvttss2si %xmm0, %r9d
cmovbel %ecx, %r9d
addq $0x10, %rax
cmpq $0x3c, %rax
jne 0x75d2
cvttss2si 0x8(%rbx), %eax
movl $0x18, %esi
movl %eax, %ecx
movl %ecx, %r8d
movss (%rbx,%rsi), %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
ucomiss %xmm0, %xmm1
cvttss2si %xmm0, %ecx
cmovbel %r8d, %ecx
addq $0x10, %rsi
cmpq $0x38, %rsi
jne 0x7604
movl $0x18, %esi
movl %eax, %r8d
movss (%rbx,%rsi), %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
ucomiss %xmm1, %xmm0
cvttss2si %xmm0, %eax
cmovbel %r8d, %eax
addq $0x10, %rsi
cmpq $0x38, %rsi
jne 0x762d
cmpl %eax, %ecx
jge 0x77f3
movslq %r10d, %rsi
movslq %ecx, %r14
cltq
movq %rax, 0x10(%rsp)
movslq %r9d, %rcx
leaq (%rsi,%rsi,2), %rax
addq %rax, %rdx
imulq $0x960, %r14, %rax      # imm = 0x960
addq %rdx, %rax
addq $0x2, %rax
movq %rax, 0x8(%rsp)
addq %rsi, %rdi
imulq $0x320, %r14, %r12      # imm = 0x320
addq %rdi, %r12
movl %esi, %ebp
subq %rsi, %rcx
movq %rcx, 0x18(%rsp)
movl %r9d, 0x4(%rsp)
movl %r10d, (%rsp)
cmpl %r9d, %r10d
jge 0x77cc
xorps %xmm0, %xmm0
cvtsi2ss %r14d, %xmm0
movaps %xmm0, 0x30(%rsp)
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movaps %xmm0, 0x20(%rsp)
xorl %r15d, %r15d
movq 0x8(%rsp), %r13
leal (%r15,%rbp), %eax
xorps %xmm3, %xmm3
cvtsi2ss %eax, %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 0x30(%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 0x20(%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 0x1b80(%rip), %xmm1     # 0x92e0
movmskpd %xmm1, %ecx
cmpl $0x3, %ecx
sete %cl
testl %eax, %eax
jle 0x7772
testb %cl, %cl
jne 0x7783
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
pand %xmm0, %xmm1
movd %xmm1, %ecx
testl %eax, %ecx
jns 0x77ba
leal (%r15,%rbp), %edx
movq %rbx, %rsi
movl %r14d, %ecx
callq 0x78de
movzbl (%r12,%r15), %ecx
cmpl %ecx, %eax
jge 0x77ba
movb %al, (%r12,%r15)
cvttss2si 0x3c(%rbx), %eax
movb %al, -0x2(%r13)
cvttss2si 0x38(%rbx), %eax
movb %al, -0x1(%r13)
cvttss2si 0x34(%rbx), %eax
movb %al, (%r13)
addq $0x3, %r13
incq %r15
cmpq %r15, 0x18(%rsp)
jne 0x76ca
incq %r14
addq $0x960, 0x8(%rsp)       # imm = 0x960
addq $0x320, %r12            # imm = 0x320
cmpq 0x10(%rsp), %r14
movl 0x4(%rsp), %r9d
movl (%rsp), %r10d
jne 0x76a3
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq