// File: 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:
subq $0xb8, %rsp
movq %rdi, 0xb0(%rsp)
movq %rsi, 0xa8(%rsp)
movq %rdx, 0xa0(%rsp)
movq 0xb0(%rsp), %rdi
movq %rdi, 0x8(%rsp)
movq 0xa8(%rsp), %rsi
callq 0x137f0
movq 0x8(%rsp), %rdi
movl %eax, 0x8c(%rsp)
movq 0xa8(%rsp), %rsi
callq 0x13870
movq 0x8(%rsp), %rdi
movl %eax, 0x88(%rsp)
movq 0xa8(%rsp), %rsi
callq 0x138f0
movq 0x8(%rsp), %rdi
movl %eax, 0x84(%rsp)
movq 0xa8(%rsp), %rsi
callq 0x13970
movl %eax, 0x80(%rsp)
movl 0x84(%rsp), %eax
movl %eax, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
cmpl 0x80(%rsp), %eax
jge 0x137e2
movl 0x8c(%rsp), %eax
movl %eax, 0x78(%rsp)
movl 0x78(%rsp), %eax
cmpl 0x88(%rsp), %eax
jge 0x137d0
movq 0x8(%rsp), %rdi
movq 0xa8(%rsp), %rax
movups (%rax), %xmm0
movaps %xmm0, 0x60(%rsp)
movq 0xa8(%rsp), %rax
movups 0x10(%rax), %xmm0
movaps %xmm0, 0x50(%rsp)
movl 0x78(%rsp), %esi
movl 0x7c(%rsp), %edx
movsd 0x60(%rsp), %xmm0
movsd 0x68(%rsp), %xmm1
movsd 0x50(%rsp), %xmm2
movsd 0x58(%rsp), %xmm3
callq 0x139f0
movq 0x8(%rsp), %rdi
movl %eax, 0x9c(%rsp)
movq 0xa8(%rsp), %rax
movups 0x10(%rax), %xmm0
movaps %xmm0, 0x40(%rsp)
movq 0xa8(%rsp), %rax
movups 0x20(%rax), %xmm0
movaps %xmm0, 0x30(%rsp)
movl 0x78(%rsp), %esi
movl 0x7c(%rsp), %edx
movsd 0x40(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
movsd 0x38(%rsp), %xmm3
callq 0x139f0
movq 0x8(%rsp), %rdi
movl %eax, 0x98(%rsp)
movq 0xa8(%rsp), %rax
movups 0x20(%rax), %xmm0
movaps %xmm0, 0x20(%rsp)
movq 0xa8(%rsp), %rax
movups (%rax), %xmm0
movaps %xmm0, 0x10(%rsp)
movl 0x78(%rsp), %esi
movl 0x7c(%rsp), %edx
movsd 0x20(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
movsd 0x18(%rsp), %xmm3
callq 0x139f0
movl %eax, 0x94(%rsp)
cmpl $0x0, 0x9c(%rsp)
jle 0x13688
cmpl $0x0, 0x98(%rsp)
jle 0x13688
cmpl $0x0, 0x94(%rsp)
jg 0x136b2
cmpl $0x0, 0x9c(%rsp)
jge 0x137be
cmpl $0x0, 0x98(%rsp)
jge 0x137be
cmpl $0x0, 0x94(%rsp)
jge 0x137be
movq 0x8(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movl 0x78(%rsp), %edx
movl 0x7c(%rsp), %ecx
callq 0x13a70
movq 0x8(%rsp), %rcx
movl %eax, 0x90(%rsp)
movl 0x90(%rsp), %eax
movslq 0x7c(%rsp), %rdx
imulq $0x320, %rdx, %rdx      # imm = 0x320
addq %rdx, %rcx
movslq 0x78(%rsp), %rdx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jge 0x137bc
movq 0x8(%rsp), %rcx
movb 0x90(%rsp), %dl
movslq 0x7c(%rsp), %rax
imulq $0x320, %rax, %rax      # imm = 0x320
addq %rax, %rcx
movslq 0x78(%rsp), %rax
movb %dl, (%rax,%rcx)
movq 0xa8(%rsp), %rax
cvttss2si 0x3c(%rax), %eax
movb %al, %dl
movq 0xa0(%rsp), %rax
movslq 0x7c(%rsp), %rcx
imulq $0x960, %rcx, %rcx      # imm = 0x960
addq %rcx, %rax
movslq 0x78(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
movb %dl, (%rax,%rcx)
movq 0xa8(%rsp), %rax
cvttss2si 0x38(%rax), %eax
movb %al, %dl
movq 0xa0(%rsp), %rax
movslq 0x7c(%rsp), %rcx
imulq $0x960, %rcx, %rcx      # imm = 0x960
addq %rcx, %rax
movslq 0x78(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
movb %dl, 0x1(%rax,%rcx)
movq 0xa8(%rsp), %rax
cvttss2si 0x34(%rax), %eax
movb %al, %cl
movq 0xa0(%rsp), %rax
movslq 0x7c(%rsp), %rdx
imulq $0x960, %rdx, %rdx      # imm = 0x960
addq %rdx, %rax
movslq 0x78(%rsp), %rdx
imulq $0x3, %rdx, %rdx
addq %rdx, %rax
movb %cl, 0x2(%rax)
jmp 0x137be
jmp 0x137c0
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x13562
jmp 0x137d2
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x13546
addq $0xb8, %rsp
retq
nopw (%rax,%rax)