added very basic multithreading to renderer
This commit is contained in:
parent
41ae90b1b5
commit
f0a7cffc39
4 changed files with 26 additions and 17 deletions
|
@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.5)
|
|||
project(CG1_Tracer LANGUAGES CXX)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_FLAGS "-O3 -pthread")
|
||||
|
||||
if(NOT WIN32)
|
||||
find_package(Threads REQUIRED)
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
#include "ray.h"
|
||||
|
||||
int Ray::rayCount = 0;
|
||||
std::atomic_int Ray::rayCount = 0;
|
||||
|
|
|
@ -42,7 +42,7 @@ private:
|
|||
int remainingBounces = ICG_RAY_BOUNCES;
|
||||
#endif
|
||||
|
||||
static int rayCount;
|
||||
static std::atomic_int rayCount;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <thread>
|
||||
|
||||
Texture SimpleRenderer::renderImage(Scene const &scene, Camera const &camera, int width, int height) {
|
||||
Texture image(width, height);
|
||||
|
@ -17,9 +18,9 @@ Texture SimpleRenderer::renderImage(Scene const &scene, Camera const &camera, in
|
|||
// Super-hacky progress bar!
|
||||
std::cout << "(SimpleRenderer): Begin rendering..." << std::endl;
|
||||
std::cout << "| 0%";
|
||||
int const barSize = 50;
|
||||
int const stepSize = (width * height) / barSize;
|
||||
for (int i = 0; i < barSize - 3 - 5; ++i)
|
||||
int const barSizeGoal = 100;
|
||||
int const stepSize = width / barSizeGoal;
|
||||
for (int i = 0; i < width / stepSize - 3 - 5; ++i)
|
||||
std::cout << " ";
|
||||
std::cout << "100% |" << std::endl << "|";
|
||||
int k = 0;
|
||||
|
@ -27,18 +28,25 @@ Texture SimpleRenderer::renderImage(Scene const &scene, Camera const &camera, in
|
|||
// Start timer
|
||||
start = std::chrono::steady_clock::now();
|
||||
|
||||
float const aspectRatio = static_cast<float>(height) / width;
|
||||
float const aspectRatio = static_cast<float>(height) / static_cast<float>(width);
|
||||
std::vector<std::thread> threads;
|
||||
threads.reserve(width);
|
||||
for (int x = 0; x < image.width(); ++x) {
|
||||
threads.emplace_back([&image, aspectRatio](auto camera, auto scene, int x) {
|
||||
for (int y = 0; y < image.height(); ++y) {
|
||||
Ray ray = camera.createRay((static_cast<float>(x) / width * 2 - 1), -(static_cast<float>(y) / height * 2 - 1) * aspectRatio);
|
||||
image.setPixelAt(x, y, clamped(scene.traceRay(ray)));
|
||||
|
||||
Ray ray = camera->createRay((static_cast<float>(x) / image.width() * 2.0f - 1),
|
||||
-(static_cast<float>(y) / image.height() * 2 - 1) * aspectRatio);
|
||||
image.setPixelAt(x, y, clamped(scene->traceRay(ray)));
|
||||
}
|
||||
}, &camera, &scene, x);
|
||||
}
|
||||
for (auto &thread: threads) {
|
||||
thread.join();
|
||||
// Super hacky progress bar!
|
||||
if (++k % stepSize == 0) {
|
||||
std::cout << "=" << std::flush;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Stop timer
|
||||
stop = std::chrono::steady_clock::now();
|
||||
|
|
Loading…
Reference in a new issue