// File: /workspace/github_cpp_makefile_star2_O3/soybin[P]idyll/src/renderer.cpp
// Function: renderer::render(double, double)
math::vec3 renderer::render(double y, double x) {

	// get ray direction relative to the pixel being rendered
	// coordinates and rotate it
	math::vec3 dir = calculateRayDirection(x, y);
	math::vec3 color(0.0);

	//
	// render same pixel multiple times
	//
	for (int i = 0; i < SAMPLES; ++i) {

		//
		// data per sample
		//
		math::ray r(cameraPosition, dir);
		math::vec3 colorLeft(1.0);
		math::vec3 colorAccumulated(0.0);

		//
		// path tracing
		//
		double fdist = 0.0;
		for (int i = 0; i < BOUNCES; ++i) {

			//
			// get distance from fractal marching the ray's direction
			//
			double distance = march(r);
			if (distance == -1.0) {
				if (i == 0) {
					colorAccumulated = renderSky(y, x);
				}
				break;
			}

			//
			// record initial distance
			//
			if (i == 0) {
				fdist = distance;
			}

			//
			// get current position and normal
			//
			math::vec3 point = r.origin + r.direction * distance;
			math::vec3 normal = f->calculateNormal(point);	

			//
			// get fractal surface color
			//
			math::vec3 colorAtPoint = f->calculateColor(point);

			math::vec3 colorLighting(0.0);
			//
			// directional light
			//
			double dl = std::max(0.0, math::dot(lightDirection, normal));
			double dlShadow = 1.0;
			if (dl > 0.0) {
				dlShadow = f->calculateShadow({r.origin + r.direction * MIN_DIST, lightDirection});
			}
			colorLighting += lightColor * dl * dlShadow;

			//
			// sky light
			//
			double skyShadow = f->calculateShadow({r.origin + r.direction * MIN_DIST, brdf(r.direction, normal)});
			colorLighting += skyColor * skyShadow;

			//
			// add bounce color to sample color
			//
			colorLeft *= colorAtPoint;
			colorAccumulated += colorLeft * colorLighting;

			//
			// bounce ray
			//
			r.origin = point;
			r.direction = brdf(r.direction, normal);
		}

		//
		// scaling / grading
		//
		double ff = std::exp(-0.01 * fdist * fdist);
		colorAccumulated *= ff;
		colorAccumulated += math::vec3(0.9, 1.0, 1.0) * (1.0 - ff) *0.05;

		//
		// add clamped sample color to average color
		//
		color += math::clamp(colorAccumulated, 0.0, 1.0);
	}
	//get average of paths colors
	color /= (double)SAMPLES;

	// apply gamma correction
	color.x = pow(color.x, 0.45);
	color.y = pow(color.y, 0.45);
	color.z = pow(color.z, 0.45);

	return color * 255.0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movapd %xmm0, %xmm2
movq %rsi, %r12
movq %rdi, -0x80(%rbp)
leaq -0x208(%rbp), %rdi
movapd %xmm1, %xmm0
movsd %xmm2, -0x88(%rbp)
movapd %xmm2, %xmm1
callq 0x6bfe
leaq -0x1f0(%rbp), %rdi
xorpd %xmm0, %xmm0
callq 0x61e0
movl 0xc(%r12), %eax
testl %eax, %eax
jle 0x7f9e
leaq 0x30(%r12), %rax
movq %rax, -0x90(%rbp)
leaq -0xc8(%rbp), %rdx
leaq 0x48(%r12), %rax
movq %rax, -0x48(%rbp)
leaq 0x60(%r12), %rax
movq %rax, -0xa0(%rbp)
leaq 0x78(%r12), %rax
movq %rax, -0x98(%rbp)
xorl %ecx, %ecx
movsd 0x6bee(%rip), %xmm0     # 0xe678
leaq -0xf8(%rbp), %rbx
movl %ecx, -0x3c(%rbp)
movq -0x90(%rbp), %rcx
movq 0x10(%rcx), %rax
movq %rax, -0xd0(%rbp)
movups (%rcx), %xmm1
movaps %xmm1, -0xe0(%rbp)
movq -0x1f8(%rbp), %rax
movq %rax, 0x10(%rdx)
movupd -0x208(%rbp), %xmm1
movupd %xmm1, (%rdx)
leaq -0x238(%rbp), %rdi
callq 0x61e0
xorpd %xmm0, %xmm0
movq %rbx, %rdi
callq 0x61e0
xorpd %xmm1, %xmm1
cmpl $0x0, 0x10(%r12)
jle 0x7ec1
xorl %r14d, %r14d
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movapd -0xe0(%rbp), %xmm0
movaps -0xd0(%rbp), %xmm1
movapd -0xc0(%rbp), %xmm2
movupd %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
movq %r12, %rdi
callq 0x7704
ucomisd 0x6d06(%rip), %xmm0     # 0xe838
jne 0x7b3a
jnp 0x7e95
movapd %xmm0, %xmm1
testl %r14d, %r14d
leaq -0xc8(%rbp), %r15
je 0x7b4f
movsd -0x30(%rbp), %xmm1
movsd %xmm1, -0xa8(%rbp)
movl %r14d, -0x30(%rbp)
leaq -0x60(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x62a4
leaq -0x78(%rbp), %rdi
leaq -0xe0(%rbp), %r13
movq %r13, %rsi
movq %rbx, %rdx
callq 0x6208
movq 0xc0(%r12), %rsi
movq -0x68(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0x78(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x8db4
movq 0xc0(%r12), %rsi
movq -0x68(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0x78(%rbp), %xmm0
movups %xmm0, (%rsp)
leaq -0x1d8(%rbp), %rdi
callq 0x8d3c
xorps %xmm0, %xmm0
leaq -0x1c0(%rbp), %rdi
callq 0x61e0
movq -0x50(%rbp), %rax
movq %rax, 0x28(%rsp)
movups -0x60(%rbp), %xmm0
movups %xmm0, 0x18(%rsp)
movq -0x48(%rbp), %rcx
movq 0x10(%rcx), %rax
movq %rax, 0x10(%rsp)
movupd (%rcx), %xmm0
movupd %xmm0, (%rsp)
callq 0x6510
movapd %xmm0, %xmm1
ucomisd 0x6cba(%rip), %xmm0     # 0xe8c8
movsd 0x6a62(%rip), %xmm0     # 0xe678
jbe 0x7cbc
movq 0xc0(%r12), %rbx
leaq -0x220(%rbp), %r14
movq %r14, %rdi
movq %r15, %rsi
movsd 0x6c67(%rip), %xmm0     # 0xe8a0
movsd %xmm1, -0x38(%rbp)
callq 0x62a4
leaq -0x1a8(%rbp), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x6208
movq -0x48(%rbp), %rcx
movq 0x10(%rcx), %rax
leaq -0x148(%rbp), %rdx
movq %rax, 0x10(%rdx)
movups (%rcx), %xmm0
movups %xmm0, (%rdx)
movq -0x198(%rbp), %rax
movq %rax, -0x150(%rbp)
movups -0x1a8(%rbp), %xmm0
movaps %xmm0, -0x160(%rbp)
movaps -0x160(%rbp), %xmm0
movaps -0x150(%rbp), %xmm1
movapd -0x140(%rbp), %xmm2
movupd %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x8bc6
movsd -0x38(%rbp), %xmm1
movsd %xmm0, -0x38(%rbp)
maxsd 0x6bff(%rip), %xmm1     # 0xe8c8
leaq -0x250(%rbp), %rbx
movq %rbx, %rdi
movq -0xa0(%rbp), %rsi
movapd %xmm1, %xmm0
callq 0x62a4
leaq -0x220(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movsd -0x38(%rbp), %xmm0
callq 0x62a4
movq %r13, %rbx
movq %r12, %r13
leaq -0x1c0(%rbp), %rdi
movq %r14, %rsi
callq 0x63c8
movq 0xc0(%r12), %r12
movq %r14, %rdi
movq %r15, %rsi
movsd 0x6b7b(%rip), %xmm0     # 0xe8a0
callq 0x62a4
leaq -0x190(%rbp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x6208
movq -0x50(%rbp), %rax
movq %rax, 0x28(%rsp)
movups -0x60(%rbp), %xmm0
movups %xmm0, 0x18(%rsp)
movq 0x10(%r15), %rax
movq %rax, 0x10(%rsp)
movups (%r15), %xmm0
movups %xmm0, (%rsp)
leaq -0x178(%rbp), %rdi
movq %r13, %rsi
callq 0x77b8
movq -0x168(%rbp), %rax
leaq -0x118(%rbp), %rcx
movq %rax, 0x10(%rcx)
movups -0x178(%rbp), %xmm0
movups %xmm0, (%rcx)
movups -0x190(%rbp), %xmm0
movaps %xmm0, -0x130(%rbp)
movq -0x180(%rbp), %rax
movq %rax, -0x120(%rbp)
movaps -0x130(%rbp), %xmm0
movaps -0x120(%rbp), %xmm1
movapd -0x110(%rbp), %xmm2
movupd %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r12, %rdi
callq 0x8bc6
movq %r14, %rdi
movq -0x98(%rbp), %rsi
callq 0x62a4
leaq -0x1c0(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x63c8
leaq -0x238(%rbp), %rbx
movq %rbx, %rdi
leaq -0x1d8(%rbp), %rsi
callq 0x6428
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r13, %r12
callq 0x6270
leaq -0xf8(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x63c8
leaq -0xe0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x6338
movq -0x50(%rbp), %rax
movq %rax, 0x28(%rsp)
movups -0x60(%rbp), %xmm0
movups %xmm0, 0x18(%rsp)
movq 0x10(%r15), %rax
movq %rax, 0x10(%rsp)
movups (%r15), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x77b8
movq %r15, %rdi
movq %r14, %rsi
callq 0x6338
movl -0x30(%rbp), %r14d
incl %r14d
movsd -0xa8(%rbp), %xmm1
movapd %xmm1, %xmm0
cmpl 0x10(%r13), %r14d
jl 0x7af6
jmp 0x7ec1
testl %r14d, %r14d
jne 0x7ebc
leaq -0x78(%rbp), %r15
movq %r15, %rdi
movq %r12, %rsi
movsd -0x88(%rbp), %xmm0
callq 0x6dce
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6338
movsd -0x30(%rbp), %xmm1
movapd %xmm1, %xmm0
mulsd 0x6a03(%rip), %xmm0     # 0xe8d0
mulsd %xmm1, %xmm0
callq 0x34a0
movsd %xmm0, -0x30(%rbp)
movq %rbx, %rdi
callq 0x6458
leaq -0x1d8(%rbp), %r13
movq %r13, %rdi
movsd 0x69e3(%rip), %xmm0     # 0xe8d8
movsd 0x677b(%rip), %xmm1     # 0xe678
movapd %xmm1, %xmm2
callq 0x61f4
movsd 0x676a(%rip), %xmm0     # 0xe678
subsd -0x30(%rbp), %xmm0
leaq -0x60(%rbp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x62a4
leaq -0x78(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
movsd 0x69ac(%rip), %xmm0     # 0xe8e0
callq 0x62a4
movq %rbx, %rdi
movq %r13, %rsi
callq 0x63c8
movq -0xe8(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0xf8(%rbp), %xmm0
movups %xmm0, (%rsp)
xorps %xmm0, %xmm0
movq %r13, %rdi
movsd 0x670f(%rip), %xmm1     # 0xe678
callq 0x66c7
leaq -0x1f0(%rbp), %rdi
movq %r13, %rsi
callq 0x63c8
movsd 0x66f3(%rip), %xmm0     # 0xe678
movl -0x3c(%rbp), %ecx
incl %ecx
movl 0xc(%r12), %eax
cmpl %eax, %ecx
leaq -0xc8(%rbp), %rdx
jl 0x7a91
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
leaq -0x1f0(%rbp), %rbx
movq %rbx, %rdi
callq 0x64b2
movsd (%rbx), %xmm0
movsd 0x6928(%rip), %xmm1     # 0xe8e8
callq 0x3480
movsd %xmm0, (%rbx)
movsd 0x8(%rbx), %xmm0
movsd 0x6912(%rip), %xmm1     # 0xe8e8
callq 0x3480
movsd %xmm0, 0x8(%rbx)
movsd 0x10(%rbx), %xmm0
movsd 0x68fb(%rip), %xmm1     # 0xe8e8
callq 0x3480
movsd %xmm0, 0x10(%rbx)
movsd 0x68f1(%rip), %xmm0     # 0xe8f0
movq -0x80(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x62a4
movq %r14, %rax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop