Skip to content

Commit

Permalink
Beta set ( Single thread optimized | Multithreading in progress / Mul…
Browse files Browse the repository at this point in the history
…tithreading unoptimized)
  • Loading branch information
V-KMilev committed Nov 22, 2021
1 parent b3ac3d1 commit a6e2f0c
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 78 deletions.
34 changes: 31 additions & 3 deletions src/GLFW_window_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ int window_setup() {

MyGlfw myGlfw(window);

ThreadPool pool(MAX_NUMBER_OF_THREADS);
ThreadPool pool_multithread(MAX_NUMBER_OF_THREADS);
ThreadPool pool_onethread(1);

std::vector<Bucket> my_buckets = bucket_segmentation(image_width, image_height);

/* Loop until the user closes the window */
Expand All @@ -144,9 +146,35 @@ int window_setup() {

myImGui.newFrame();

if(change_position || change_view || change_bg || change_default) {
if(change_multithreading) {
pool_multithread.clear();
}
else {
pool_onethread.clear();
}

reset_counter();

samples_per_pixel = default_samples_per_pixel;

pixels = empty_pixels;
samples_in_pixels = empty_samples_in_pixels;
}

/* RENDER: */
for (Bucket &my_bucket : my_buckets) {
pool.enter_queue(my_bucket);
if(change_multithreading) {
pool_onethread.clear();

for (Bucket &my_bucket : my_buckets) {
pool_multithread.enter_queue(my_bucket);
}
} else {
pool_multithread.clear();

for (Bucket &my_bucket : my_buckets) {
pool_onethread.enter_queue(my_bucket);
}
}

#ifdef DEBUG
Expand Down
16 changes: 13 additions & 3 deletions src/backend/Render.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
/* WORLD: */
const Hittable_list world = get_scene();

void reset_counter() {
counter = 0;
}

Color ray_color(const Ray &ray, const Color &background, const Hittable &world, int depth) {

hit_record record;
Expand All @@ -21,6 +25,7 @@ Color ray_color(const Ray &ray, const Color &background, const Hittable &world,

Ray scattered;
Color attenuation;

Color emitted = record.material_ptr->emitted(record.u, record.v, record.point);

if (!record.material_ptr->scatter(ray, record, attenuation, scattered)) {
Expand All @@ -34,13 +39,15 @@ void render(const Bucket &my_bucket) {
/* CAMERA: */
Camera camera(lookfrom, lookat, view_up, 53.7, aspect_ratio, aperture, dist_to_focus, 0.0, 1.0);

const float div = 1.0f / samples_per_pixel;

std::cerr << "\n\rStart Bucket: " << std::this_thread::get_id() << " -> + " << my_bucket.bucket_id;

for (int y = my_bucket.start_y; y < my_bucket.end_y; y++) {
for (int x = my_bucket.start_x; x < my_bucket.end_x; x++) {

const int idx = image_width * y + x;
const int total = samples_per_pixel + samples_in_pixels[idx];
const int total = samples_in_pixels[idx] + samples_per_pixel;

Color pixel_in_set(0, 0, 0);
Color &current_pixel = pixels[idx];
Expand All @@ -54,14 +61,17 @@ void render(const Bucket &my_bucket) {

pixel_in_set += ray_color(ray, background, world, max_depth);
}
pixel_in_set *= div;

current_pixel = current_pixel * (samples_in_pixels[idx] / (float) total) + pixel_in_set * (samples_per_pixel / (float) total);

samples_in_pixels[idx] = total;
}
}

if(my_bucket.bucket_id == total_buckets) {
samples_per_pixel += 1;
if(++counter == total_buckets) {
samples_per_pixel += 2;
reset_counter();
}

std::cerr << "\n\rEnd Bucket: " << std::this_thread::get_id() << " -> - " << my_bucket.bucket_id;
Expand Down
8 changes: 7 additions & 1 deletion src/backend/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "Color.h"
#include "Camera.h"

#include <atomic>

#define RES_DEFAULT 400

#define RES_TEST 800
Expand All @@ -16,7 +18,7 @@
/* Basic parameters | Default settings */
float aspect_ratio = {16.0 / 9.0}; // Image: Aspect ratio: resolution

int image_width = {RES_FULL_HD}; // Image: Width
int image_width = {RES_TEST}; // Image: Width
int image_height = {static_cast<int>(image_width / aspect_ratio)}; // Image: Height

int samples_per_pixel = {1}; // Rays per pixel
Expand All @@ -42,12 +44,16 @@ Vec view_up(0.0, 1.0, 0.0);
float dist_to_focus = 4.5;
float aperture = 0.1;

/* Render parameters */
std::atomic<int> counter = {0};

/* Change event bools */
bool change_position = false;
bool change_view = false;
bool change_bg = false;
bool change_default = false;
bool change_stop = true;
bool change_multithreading = false;

/* Default settings */
int default_samples_per_pixel = {1};
Expand Down
23 changes: 14 additions & 9 deletions src/backend/Thread_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ const int MAX_NUMBER_OF_THREADS = std::thread::hardware_concurrency();

class ThreadPool {
public:
explicit ThreadPool(std::size_t num_Threads) { // Explicit: size_t to size_t only
explicit ThreadPool(std::size_t num_threads) { // Explicit: size_t to size_t only

std::cerr << "\rStart Thread Pool: " << num_Threads << "\n";
start(num_Threads);
std::cerr << "\rStart Thread Pool: " << num_threads << "\n";

start(num_threads);
}

~ThreadPool() {
Expand All @@ -51,10 +52,15 @@ class ThreadPool {
my_Done = false;
}

private:
void start(std::size_t num_Threads) {
void clear() {
while(!my_Tasks.empty()) {
my_Tasks.pop();
}
}

for (int i = 0; i < num_Threads; i++) {
private:
void start(std::size_t num_threads) {
for (int i = 0; i < num_threads; i++) {

/* Add new thread with a task at the end of the vector */
my_Threads.emplace_back(
Expand All @@ -73,9 +79,8 @@ class ThreadPool {
}

render(task); // Execute current Task
int value = counter.fetch_sub(1) - 1; // Downgrade Task index

if (value == 0) {
if (--counter == 0) {
my_Done = true;
my_Release_Master.notify_one();
}
Expand All @@ -95,7 +100,7 @@ class ThreadPool {
my_thread.join(); // Join all threads
}
}

public:
// using Task = std::function<Bucket()>; // Task is used as void funtion(Bucket)

Expand Down
28 changes: 13 additions & 15 deletions src/backend/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,24 @@ Hittable_list get_scene() {

shared_ptr<Material> material_sphere_c = make_shared<Lambertian>(checker);
shared_ptr<Material> material_sphere_n = make_shared<Lambertian>(noise);
shared_ptr<Material> material_white_c = make_shared<Lambertian>(checker);

shared_ptr<Material> material_white = make_shared<Lambertian>(Color(1,1,1));
shared_ptr<Material> material_gray = make_shared<Lambertian>(Color(0.5,0.5,0.5));
shared_ptr<Material> material_red = make_shared<Lambertian>(Color(1,0,0));
shared_ptr<Material> material_green = make_shared<Lambertian>(Color(0,1,0));
shared_ptr<Material> material_blue = make_shared<Lambertian>(Color(0,0,1));

shared_ptr<Material> difflight = make_shared<Diffuse_light>(Color(11,11,6));
shared_ptr<Material> difflight_red = make_shared<Diffuse_light>(Color(3,0,0));
shared_ptr<Material> difflight_blue = make_shared<Diffuse_light>(Color(0,0,3));
shared_ptr<Material> difflight_green = make_shared<Diffuse_light>(Color(0,3,0));
shared_ptr<Material> difflight_white = make_shared<Diffuse_light>(Color(3,3,3));
shared_ptr<Material> difflight_purple = make_shared<Diffuse_light>(Color(3,0,3));
shared_ptr<Material> difflight_magenta = make_shared<Diffuse_light>(Color(3,0,3));

shared_ptr<Material> difflight_w = make_shared<Diffuse_light>(Color(7,7,7));
shared_ptr<Material> difflight_purple_low = make_shared<Diffuse_light>(Color(0.1,0.0,0.1));
shared_ptr<Material> difflight_white_low = make_shared<Diffuse_light>(Color(1.0,1.0,1.0));
shared_ptr<Material> difflight_w = make_shared<Diffuse_light>(Color(37,37,37));

/* Objects */
world.add(make_shared<xz_rect>(-1000, 1000, -1000, 1000, 0, material_sphere_n));
// world.add(make_shared<xz_rect>(-250, 250, -250, 250, 2000, difflight_w));
world.add(make_shared<Sphere>(Point(0, 2000, 200), 100, difflight_w));

// l f r b from lookform(-x,+y,0)
Expand All @@ -75,13 +72,15 @@ Hittable_list get_scene() {
world.add(make_shared<xy_rect>(-14, 2, 0, 10, 14, material_gray)); // r-w3
world.add(make_shared<yz_rect>(0, 10, -2, 14, -14, material_gray)); // f-w4

world.add(make_shared<xz_rect>(-14, 2, -2, 2, 5, material_white)); // l-f1
world.add(make_shared<xz_rect>(-14, -10, -2, 14, 5, material_white)); // f-f2
world.add(make_shared<xz_rect>(-14, 2, 10, 14, 5, material_white)); // r-f3
world.add(make_shared<xz_rect>(-14, 2, -2, 2, 5, material_white_c)); // l-f1
world.add(make_shared<xz_rect>(-14, -10, -2, 14, 5, material_white_c)); // f-f2
world.add(make_shared<xz_rect>(-14, 2, 10, 14, 5, material_white_c)); // r-f3

world.add(make_shared<xy_rect>(-10, 2, 0, 5, 2, material_gray)); // l-in-w1
world.add(make_shared<yz_rect>(0, 5, -2, 10, -10, material_gray)); // f-in-w2
world.add(make_shared<xy_rect>(-10, 2, 0, 5, 10, material_gray)); // r-in-w3
world.add(make_shared<xz_rect>(-4, -2, -2, 14, 5, material_white_c)); // f-p-f1

world.add(make_shared<xy_rect>(-10, 2, 0, 5, 2, material_white_c)); // l-in-w1
world.add(make_shared<yz_rect>(0, 5, -2, 10, -10, material_white_c)); // f-in-w2
world.add(make_shared<xy_rect>(-10, 2, 0, 5, 10, material_white_c)); // r-in-w3

world.add(make_shared<xz_rect>(-14, 2, -2, 2, 10, material_white)); // l-r1
world.add(make_shared<xz_rect>(-14, -10, -2, 14, 10, material_white)); // f-r2
Expand All @@ -91,17 +90,16 @@ Hittable_list get_scene() {
world.add(make_shared<xz_rect>(-4, -2, -2, 14, 10, material_white)); // p-r2
world.add(make_shared<xz_rect>(0, 2, -2, 14, 10, material_white)); // p-r3

world.add(make_shared<xz_rect>(-4, -2, -2, 14, 5, material_white)); // f-p-f1
// box 2x2
world.add(make_shared<xy_rect>(-3.5, -2.5, 5, 6.5, 5.5, material_red)); // p-l-w1
world.add(make_shared<yz_rect>(5, 6.5, 5.5, 6.5, -2.5, material_green)); // p-b-w2
world.add(make_shared<xy_rect>(-3.5, -2.5, 5, 6.5, 6.5, material_blue)); // p-r-w3
world.add(make_shared<yz_rect>(5, 6.5, 5.5, 6.5, -3.5, material_gray)); // p-f-w4
world.add(make_shared<xz_rect>(-3.5, -2.5, 5.5, 6.5, 6.5, material_gray)); // p-r1

world.add(make_shared<Sphere>(Point(-3, 7, 6), 0.5, difflight_white_low)); // light sphere
world.add(make_shared<Sphere>(Point(-3, 7, 6), 0.5, difflight_w)); // light sphere

world.add(make_shared<xz_rect>(-10, 2, 2, 10, 0, difflight_purple_low)); // light-pool
world.add(make_shared<xz_rect>(-10, 2, 2, 10, 0, difflight_magenta)); // light-pool

return world;
}
Expand Down
29 changes: 6 additions & 23 deletions src/myGlfw/Glfw_controls.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ class MyGlfw {
int state = glfwGetKey(window, GLFW_KEY_W);

if (state == GLFW_PRESS) {
samples_per_pixel = default_samples_per_pixel;

pixels = empty_pixels;
samples_in_pixels = empty_samples_in_pixels;
change_position = true;

lookfrom[0] += precision;
glfwWaitEventsTimeout(0.1);
Expand All @@ -49,10 +46,7 @@ class MyGlfw {
int state = glfwGetKey(window, GLFW_KEY_S);

if (state == GLFW_PRESS) {
samples_per_pixel = default_samples_per_pixel;

pixels = empty_pixels;
samples_in_pixels = empty_samples_in_pixels;
change_position = true;

lookfrom[0] += -precision;
glfwWaitEventsTimeout(0.1);
Expand All @@ -63,10 +57,7 @@ class MyGlfw {
int state = glfwGetKey(window, GLFW_KEY_Q);

if (state == GLFW_PRESS) {
samples_per_pixel = default_samples_per_pixel;

pixels = empty_pixels;
samples_in_pixels = empty_samples_in_pixels;
change_position = true;

lookfrom[1] += precision;
glfwWaitEventsTimeout(0.1);
Expand All @@ -77,10 +68,8 @@ class MyGlfw {
int state = glfwGetKey(window, GLFW_KEY_E);

if (state == GLFW_PRESS) {
samples_per_pixel = default_samples_per_pixel;
change_position = true;

pixels = empty_pixels;
samples_in_pixels = empty_samples_in_pixels;
lookfrom[1] += -precision;
glfwWaitEventsTimeout(0.1);
}
Expand All @@ -90,10 +79,7 @@ class MyGlfw {
int state = glfwGetKey(window, GLFW_KEY_A);

if (state == GLFW_PRESS) {
samples_per_pixel = default_samples_per_pixel;

pixels = empty_pixels;
samples_in_pixels = empty_samples_in_pixels;
change_position = true;

lookfrom[2] += -precision;
glfwWaitEventsTimeout(0.1);
Expand All @@ -104,10 +90,7 @@ class MyGlfw {
int state = glfwGetKey(window, GLFW_KEY_D);

if (state == GLFW_PRESS) {
samples_per_pixel = default_samples_per_pixel;

pixels = empty_pixels;
samples_in_pixels = empty_samples_in_pixels;
change_position = true;

lookfrom[2] += precision;
glfwWaitEventsTimeout(0.1);
Expand Down
Loading

0 comments on commit a6e2f0c

Please sign in to comment.