// File: /workspace/github_c_makefile_star2_O2/Nealsbo[P]C-Software-Renderer/src/raster.c
// Function: Raster_DrawTriangle
void Raster_DrawTriangle( vec4 *v, SDL_Surface *Surface, obj_model_t *model, shader_t *shader ) {
	float w0, w1, w2, z, area;
	int index, x ,y, srcX, srcY;
	
	mat4 sst;
	color_t color;
	
	mat4_screen( sst, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2 );

	//vec3 p;
	vec3 overW = { 1.0f / v[0].w, 1.0f / v[1].w, 1.0f / v[2].w };
	vec3 pts[3] = {vec4_toVec3( vec4_pdiv( vec4_byMat4(v[0], sst) ) ),
				   vec4_toVec3( vec4_pdiv( vec4_byMat4(v[1], sst) ) ),
				   vec4_toVec3( vec4_pdiv( vec4_byMat4(v[2], sst) ) )};
				   
	vec2i bboxmin = { 0, 0 };
	vec2i bboxmax = { WINDOW_WIDTH - 1, WINDOW_HEIGHT - 1 };
	vec2i clamp   = { WINDOW_WIDTH - 1, WINDOW_HEIGHT - 1 };
	
	area = EdgeFunc( &pts[0], &pts[1], &pts[2] );
	
	bboxmax.x = MIN( MAX( pts[0].x, MAX( pts[1].x, pts[2].x) ), clamp.x );
	bboxmin.x = MAX( MIN( pts[0].x, MIN( pts[1].x, pts[2].x) ), 0 );
	bboxmax.y = MIN( MAX( pts[0].y, MAX( pts[1].y, pts[2].y) ), clamp.y );
	bboxmin.y = MAX( MIN( pts[0].y, MIN( pts[1].y, pts[2].y) ), 0 );

	for( x = bboxmin.x; x <= bboxmax.x; x++ ) {
		for( y = bboxmin.y; y <= bboxmax.y; y++ ) {
			int index = y * WINDOW_WIDTH + x;
			vec3 p = { x, y, 0.0f };
			w0 = EdgeFunc( &pts[1], &pts[2], &p );
			w1 = EdgeFunc( &pts[2], &pts[0], &p );
			w2 = EdgeFunc( &pts[0], &pts[1], &p );
			
			if( w0 >= 0.0f && w1 >= 0.0f && w2 >= 0.0f ) {
				w0 /= area;
				w1 /= area;
				w2 /= area;
				z = 1.0f / ( pts[0].z * w0 + pts[1].z * w1 + pts[2].z * w2 );
				
				if( z < renderer_->z_Buffer[index] ) {
					renderer_->z_Buffer[index] = z;
					
					vec3 d = { w0 * overW.x, w1 * overW.y, w2 * overW.z };
					float f = 1.0f/ (d.x + d.y + d.z);
					d = vec3_mlt(d, f);
					
					switch( renderer_->flagState ) {
						case RENDER_STATE_Z_BUFFER:
							color = Color_Gray( (unsigned char)( 255.0f * renderer_->z_Buffer[index] ) );
							break;
						case RENDER_STATE_LIT:
							color = Shader_Fragment( shader, model, d );
							break;
						case RENDER_STATE_UNLIT:
							color = Shader_Fragment( shader, model, d );
							break;
					}
					renderer_->PutPixel( Surface, p.x, p.y, Color_ToUInt32( color ) );
				}
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %rcx, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdi, %r13
leaq 0x140(%rsp), %r12
movss 0x26b2(%rip), %xmm0     # 0x7ca0
movss 0x26ae(%rip), %xmm1     # 0x7ca4
movq %r12, %rdi
callq 0x3e9b
movss 0x1c(%r13), %xmm0
movss 0xc(%r13), %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movaps 0x269c(%rip), %xmm0     # 0x7cb0
divps %xmm1, %xmm0
movaps %xmm0, 0x100(%rsp)
movss 0x19ed(%rip), %xmm0     # 0x7014
divss 0x2c(%r13), %xmm0
movss %xmm0, 0xc(%rsp)
movsd (%r13), %xmm0
movsd 0x8(%r13), %xmm1
movq %r12, %rdi
callq 0x2d1d
callq 0x2b22
callq 0x2b21
movaps %xmm0, 0x70(%rsp)
movss %xmm1, 0x1c(%rsp)
movsd 0x10(%r13), %xmm0
movsd 0x18(%r13), %xmm1
movq %r12, %rdi
callq 0x2d1d
callq 0x2b22
callq 0x2b21
movaps %xmm0, 0x30(%rsp)
movss %xmm1, 0x18(%rsp)
movsd 0x20(%r13), %xmm0
movsd 0x28(%r13), %xmm1
movq %r12, %rdi
callq 0x2d1d
callq 0x2b22
callq 0x2b21
movaps %xmm1, %xmm6
movaps 0x30(%rsp), %xmm12
movaps %xmm12, %xmm7
movaps %xmm12, %xmm1
maxps %xmm0, %xmm1
movaps %xmm12, %xmm2
minps %xmm0, %xmm2
movaps %xmm0, %xmm8
movaps 0x70(%rsp), %xmm4
movaps %xmm4, %xmm3
shufps $0x11, %xmm0, %xmm3     # xmm3 = xmm3[1,0],xmm0[1,0]
shufps $0xe2, %xmm0, %xmm3     # xmm3 = xmm3[2,0],xmm0[2,3]
movaps %xmm0, %xmm9
shufps $0x11, %xmm12, %xmm9    # xmm9 = xmm9[1,0],xmm12[1,0]
shufps $0xe2, %xmm12, %xmm9    # xmm9 = xmm9[2,0],xmm12[2,3]
unpcklps %xmm0, %xmm12           # xmm12 = xmm12[0],xmm0[0],xmm12[1],xmm0[1]
subps %xmm4, %xmm0
movaps %xmm4, %xmm10
subps %xmm4, %xmm7
movaps %xmm4, %xmm5
movaps %xmm7, %xmm4
shufps $0x55, %xmm7, %xmm4     # xmm4 = xmm4[1,1],xmm7[1,1]
movaps %xmm4, 0x80(%rsp)
movaps %xmm4, %xmm11
mulss %xmm0, %xmm11
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
mulss %xmm7, %xmm0
subss %xmm0, %xmm11
movaps %xmm5, %xmm0
maxps %xmm1, %xmm0
movaps %xmm0, %xmm1
cmpltps 0x2599(%rip), %xmm1     # 0x7cc0
shufps $0x55, %xmm10, %xmm10   # xmm10 = xmm10[1,1,1,1]
unpcklps %xmm1, %xmm1            # xmm1 = xmm1[0,0,1,1]
cvttss2si %xmm0, %ecx
movmskpd %xmm1, %eax
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
cvttss2si %xmm0, %edx
movslq %ecx, %rcx
movslq %edx, %rdx
movaps %xmm5, %xmm0
minps %xmm2, %xmm0
xorps %xmm1, %xmm1
cmpltps %xmm0, %xmm1
unpcklps %xmm1, %xmm1            # xmm1 = xmm1[0,0,1,1]
movmskpd %xmm1, %esi
cvttss2si %xmm0, %edi
movslq %edi, %r8
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
cvttss2si %xmm0, %edi
movslq %edi, %r9
unpcklps %xmm5, %xmm8            # xmm8 = xmm8[0],xmm5[0],xmm8[1],xmm5[1]
xorl %edi, %edi
testb $0x2, %sil
cmoveq %rdi, %r9
testb $0x2, %al
movl $0x2cf, %r14d           # imm = 0x2CF
cmovneq %rdx, %r14
subps %xmm9, %xmm3
movaps %xmm3, 0x90(%rsp)
testb $0x1, %sil
cmoveq %rdi, %r8
testb $0x1, %al
movl $0x4ff, %eax            # imm = 0x4FF
cmovneq %rcx, %rax
movq %rax, 0x50(%rsp)
movaps %xmm12, 0x30(%rsp)
subps %xmm12, %xmm8
movaps %xmm11, %xmm12
shufps $0x0, %xmm11, %xmm12    # xmm12 = xmm12[0,0],xmm11[0,0]
movq %r9, 0x48(%rsp)
imulq $0x1400, %r9, %rax      # imm = 0x1400
leaq (%rax,%r8,4), %r15
xorps %xmm13, %xmm13
leaq 0x69d9(%rip), %r12      # 0xc1b0
movss %xmm6, 0x8(%rsp)
movaps %xmm7, 0xf0(%rsp)
movaps %xmm8, 0xe0(%rsp)
movaps %xmm9, 0xd0(%rsp)
movaps %xmm10, 0xc0(%rsp)
movaps %xmm11, 0xb0(%rsp)
movaps %xmm12, 0xa0(%rsp)
cmpq 0x50(%rsp), %r8
jg 0x5a51
movq %r8, 0x60(%rsp)
cvtsi2ss %r8d, %xmm14
movaps %xmm14, %xmm15
shufps $0x0, %xmm14, %xmm15    # xmm15 = xmm15[0,0],xmm14[0,0]
subps 0x30(%rsp), %xmm15
mulps 0x90(%rsp), %xmm15
cvttss2si %xmm14, %eax
movl %eax, 0x10(%rsp)
subss 0x70(%rsp), %xmm14
mulss 0x80(%rsp), %xmm14
movq %r15, 0x58(%rsp)
movq 0x48(%rsp), %rbp
movss 0x17a9(%rip), %xmm5     # 0x7014
movaps %xmm14, 0x120(%rsp)
movaps %xmm15, 0x110(%rsp)
cmpq %r14, %rbp
jg 0x5a3b
cvtsi2ss %ebp, %xmm3
movaps %xmm3, %xmm1
shufps $0x0, %xmm3, %xmm1      # xmm1 = xmm1[0,0],xmm3[0,0]
subps %xmm9, %xmm1
mulps %xmm8, %xmm1
movaps %xmm15, %xmm0
subps %xmm1, %xmm0
ucomiss %xmm13, %xmm0
jb 0x5a2c
movaps %xmm0, %xmm1
shufps $0x55, %xmm0, %xmm1     # xmm1 = xmm1[1,1],xmm0[1,1]
ucomiss %xmm13, %xmm1
jb 0x5a2c
movaps %xmm3, %xmm2
subss %xmm10, %xmm2
mulss %xmm7, %xmm2
movaps %xmm14, %xmm1
subss %xmm2, %xmm1
ucomiss %xmm13, %xmm1
jb 0x5a2c
movaps %xmm3, 0x130(%rsp)
divps %xmm12, %xmm0
movaps %xmm0, %xmm2
shufps $0x55, %xmm0, %xmm2     # xmm2 = xmm2[1,1],xmm0[1,1]
divss %xmm11, %xmm1
mulss 0x18(%rsp), %xmm2
movss 0x1c(%rsp), %xmm3
mulss %xmm0, %xmm3
addss %xmm2, %xmm3
movaps %xmm6, %xmm4
mulss %xmm1, %xmm4
addss %xmm3, %xmm4
movaps %xmm5, %xmm2
divss %xmm4, %xmm2
movq (%r12), %rax
movq 0x18(%rax), %rax
movss (%rax,%r15), %xmm3
ucomiss %xmm2, %xmm3
jbe 0x5a2c
movss %xmm2, (%rax,%r15)
mulps 0x100(%rsp), %xmm0
mulss 0xc(%rsp), %xmm1
movaps %xmm0, %xmm3
shufps $0x55, %xmm0, %xmm3     # xmm3 = xmm3[1,1],xmm0[1,1]
addss %xmm0, %xmm3
addss %xmm1, %xmm3
movaps %xmm5, %xmm2
divss %xmm3, %xmm2
callq 0x2929
movq (%r12), %rdx
movl 0x8(%rdx), %ecx
cmpl $0x1, %ecx
je 0x5997
cmpl $0x2, %ecx
je 0x5997
cmpl $0x3, %ecx
movl 0x14(%rsp), %eax
jne 0x59a6
movq 0x18(%rdx), %rax
movss (%rax,%r15), %xmm0
mulss 0x1883(%rip), %xmm0     # 0x720c
cvttss2si %xmm0, %eax
movzbl %al, %edi
callq 0x1e67
jmp 0x59a6
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x62cf
movq (%r12), %rcx
movq 0x28(%rcx), %rbx
cvttss2si 0x130(%rsp), %r13d
movl %eax, 0x14(%rsp)
movl %eax, %edi
callq 0x20d8
movq 0x68(%rsp), %rdi
movl 0x10(%rsp), %esi
movl %r13d, %edx
movl %eax, %ecx
callq *%rbx
movss 0x1639(%rip), %xmm5     # 0x7014
movss 0x8(%rsp), %xmm6
movaps 0xf0(%rsp), %xmm7
movaps 0xe0(%rsp), %xmm8
movaps 0xd0(%rsp), %xmm9
movaps 0xc0(%rsp), %xmm10
movaps 0xb0(%rsp), %xmm11
movaps 0xa0(%rsp), %xmm12
xorps %xmm13, %xmm13
movaps 0x120(%rsp), %xmm14
movaps 0x110(%rsp), %xmm15
incq %rbp
addq $0x1400, %r15           # imm = 0x1400
jmp 0x587d
movq 0x60(%rsp), %r8
incq %r8
movq 0x58(%rsp), %r15
addq $0x4, %r15
jmp 0x5812
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop