Skip to content

Commit

Permalink
Added: multi-threading
Browse files Browse the repository at this point in the history
  • Loading branch information
ChuzaWick420 committed Nov 24, 2024
1 parent 644dbb2 commit c090a52
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 19 deletions.
Binary file modified rendered_img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 41 additions & 18 deletions src/headers/camera/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,63 @@ void camera::show_img() {
void camera::render(const hittable& world) {
initialize();

pixel_grid.resize(img_height * img_width);
int total_pixels = img_height * img_width;

int index = 1;
pixel_grid.resize(total_pixels);

int total_pixels = img_height * img_width;
int thread_load = total_pixels / threads;

for (auto& pixel : pixel_grid) {
for (int j = 1; j <= threads; j++) {
worker_threads.push_back(std::thread([&, j] {

std::clog << "\rPercentage: " << int((index * 100) / total_pixels) << "% " << std::flush;
int thread_id = j;

color pixel_color(0, 0, 0);
for (int i = 0; i < thread_load; i++) {

int x = (index - 1) % img_width;
int y = (index - 1) / img_width;
int offset = (thread_id - 1) * thread_load;

for (int sample = 0; sample < samples_per_pixel; sample++) {
ray r = get_ray(x, y);
pixel_color += ray_color(r, max_depth, world);
}
color pixel_color(0, 0, 0);

int x = i % img_width;
int y = (i + offset) / img_width;

for (int sample = 0; sample < samples_per_pixel; sample++) {
ray r = get_ray(x, y);
pixel_color += ray_color(r, max_depth, world);
}

write_color(&pixel, pixel_color * pixel_samples_scale);
write_color(&pixel_grid[y * img_width + x], pixel_color * pixel_samples_scale);

this->i_image.setPixel(x, y, pixel);
this->i_image.setPixel(x, y, pixel_grid[y * img_width + x]);

index++;
//debug
/*printf(*/
/* "Thread_id: %d,\nThread_load: %d,\nOffset: %d,\nPosition: (%d, %d),\nColor: (%d, %d, %d),\n",*/
/* thread_id,*/
/* thread_load,*/
/* offset,*/
/* x, y,*/
/* pixel_grid[i + offset].r, pixel_grid[i + offset].g, pixel_grid[i + offset].b*/
/*);*/
}

}));
}

//waiting for threads
std::for_each(
worker_threads.begin(),
worker_threads.end(),
[](std::thread &t) {
t.join();
}
);

// optionally save the image
if (this->img_gen == true)
i_image.saveToFile("rendered_img.png");

std::clog << "done\n";

this->show_img();
this->show_img();
}

void camera::initialize() {
Expand Down
6 changes: 6 additions & 0 deletions src/headers/camera/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
#include "SFML/Graphics.hpp"

#include "../main_header.hpp"

#include <vector>
#include <thread>
#include <algorithm>

class camera {
public:
Expand All @@ -22,6 +25,8 @@ class camera {
point3 lookat = point3(0, 0, -1);
vec3 vup = vec3(0, 1, 0);

int threads = 1;

double defocus_angle = 0;
double focus_dist = 10;

Expand All @@ -32,6 +37,7 @@ class camera {
private:

std::vector<sf::Color> pixel_grid;
std::vector<std::thread> worker_threads;
int window_width = 1024;
int img_height; // Rendered image height
point3 center; // Camera center
Expand Down
4 changes: 3 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ int main () {

cam.aspect_ratio = 16.0 / 9.0;
cam.img_width = 1024;
cam.samples_per_pixel = 200;
cam.samples_per_pixel = 2;
cam.max_depth = 50;

cam.threads = 4;
cam.img_gen = true;

cam.vfov = 20;
Expand Down

0 comments on commit c090a52

Please sign in to comment.