// File: Lut99[P]RayTracer/src/lib/Renderer.cpp
// Function: RayTracer::Renderer::render(RayTracer::RenderWorld*, RayTracer::Camera*)
Image Renderer::render(RenderWorld* world, Camera* cam) {
    Image out(cam->width, cam->height);
    ProgressBar prgrs(0, cam->width * cam->height - 1);

    // Render it (how depends on if the program is threaded or not)
    #ifndef THREADED

    for (int y = cam->height-1; y >= 0; y--) {
        for (int x = 0; x < cam->width; x++) {
            // Render the pixel
            out[y][x] = world->render_pixel(x, y, *cam);

            if (this->show_progressbar) {
                prgrs.update();
            }
        }
    }

    #else

    // Create a thread pool
    ThreadPool pool(this->n_threads, this->batch_size);

    // Use the thread pool to render it
    unsigned long batch_index = 0;
    unsigned long to_do = cam->width * cam->height;
    while (batch_index < to_do) {
        // Update the progressbar in any case for smoothness
        if (this->show_progressbar) {
            prgrs.set(batch_index);
        }

        // If a the queue is full, wait for another thread to notify us it has taken an element
        if (pool.batch_queue_full()) {
            pool.wait();
        }

        // Create a new batch and append it
        PixelBatch batch = pool.get_batch(cam->width, cam->height, batch_index);
        batch.camera = cam;
        batch.world = world;
        batch.out = &out;
        pool.add_batch(batch);
    }

    // Wait until all threads have been reaped
    pool.complete();
    pool.stop();

    // Update the bar one last time
    if (this->show_progressbar) {
        prgrs.set(batch_index);
    }

    #endif

    return out;
}

// Assembly:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x18(%rsp)
movq %rdi, %rax
movq %rax, 0x20(%rsp)
movq %rdi, 0x130(%rsp)
movq %rsi, 0x128(%rsp)
movq %rdx, 0x120(%rsp)
movq %rcx, 0x118(%rsp)
movq 0x128(%rsp), %rax
movq %rax, 0x28(%rsp)
movb $0x0, 0x117(%rsp)
movq 0x118(%rsp), %rax
movl 0x8(%rax), %esi
movl 0xc(%rax), %edx
callq 0x13b350
movq 0x118(%rsp), %rcx
movl 0x8(%rcx), %eax
movl 0xc(%rcx), %ecx
imull %ecx, %eax
decl %eax
movslq %eax, %rdx
xorl %eax, %eax
movl %eax, %esi
leaq 0x90(%rsp), %rdi
callq 0x1601b0
jmp 0x139385
movq 0x118(%rsp), %rax
movl 0xc(%rax), %eax
subl $0x1, %eax
movl %eax, 0x80(%rsp)
cmpl $0x0, 0x80(%rsp)
jl 0x1394e1
movl $0x0, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
movq 0x118(%rsp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x1394c9
movq 0x120(%rsp), %rsi
movl 0x7c(%rsp), %edx
movl 0x80(%rsp), %ecx
movq 0x118(%rsp), %r8
leaq 0x58(%rsp), %rdi
callq 0x162e30
jmp 0x1393ec
movq 0x18(%rsp), %rdi
movl 0x80(%rsp), %esi
callq 0x13b950
movl %edx, 0xc(%rsp)
movq %rax, 0x10(%rsp)
jmp 0x139408
movl 0xc(%rsp), %eax
movq 0x10(%rsp), %rcx
movq %rcx, 0x30(%rsp)
movl %eax, 0x38(%rsp)
movl 0x38(%rsp), %eax
movl %eax, 0x48(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x40(%rsp)
movl 0x7c(%rsp), %esi
leaq 0x40(%rsp), %rdi
callq 0x13b160
movq %rax, (%rsp)
jmp 0x139440
movq (%rsp), %rax
movq %rax, 0x50(%rsp)
leaq 0x50(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x13b0e0
jmp 0x13945a
movq 0x28(%rsp), %rax
testb $0x1, 0x8(%rax)
je 0x1394b7
leaq 0x90(%rsp), %rdi
movl $0x1, %esi
callq 0x160a50
jmp 0x139479
jmp 0x1394b7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
jmp 0x139517
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x139a40
jmp 0x139517
jmp 0x1394b9
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x1393b0
jmp 0x1394cb
movl 0x80(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0x13939a
movb $0x1, 0x117(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x139a40
testb $0x1, 0x117(%rsp)
jne 0x13950a
movq 0x18(%rsp), %rdi
callq 0x13b4a0
movq 0x20(%rsp), %rax
addq $0x138, %rsp            # imm = 0x138
retq
movq 0x18(%rsp), %rdi
callq 0x13b4a0
movq 0x88(%rsp), %rdi
callq 0xd0a10
nop