From 39b5de65763075852b618642cfbdd9a7444b32e8 Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Fri, 24 Jan 2025 22:23:33 +0100 Subject: [PATCH 1/2] very basic working version --- src/ThreadPool.cpp | 2 +- src/scene/Scene.cpp | 40 +++++++++++++++++++++++++--------------- src/scene/Scene.h | 6 +++--- src/window/Window.cpp | 6 +++--- src/window/Window.h | 3 ++- 5 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/ThreadPool.cpp b/src/ThreadPool.cpp index cd1fe4e..b8e9266 100644 --- a/src/ThreadPool.cpp +++ b/src/ThreadPool.cpp @@ -45,7 +45,7 @@ void ThreadPool::work() std::function job; { std::unique_lock l(queueLock); - if (taskQueue.front().jobs.empty()) + if (taskQueue.empty() || taskQueue.front().jobs.empty()) { queueCV.wait(l); continue; diff --git a/src/scene/Scene.cpp b/src/scene/Scene.cpp index 64a58d2..4dc4c92 100644 --- a/src/scene/Scene.cpp +++ b/src/scene/Scene.cpp @@ -1,21 +1,28 @@ #include "Scene.h" +#include +#include Scene::Scene() {} Scene::~Scene() {} +static bool firstTime = true; void Scene::render(Camera cam, RenderParameter params) { - pendingCancel = true; - worker.join(); + if (!firstTime) + { + pendingCancel = true; + worker.join(); + firstTime = false; + } pendingCancel = false; + image.clear(); + accumulator.clear(); + image.resize(params.width * params.height); + accumulator.resize(params.width * params.height); worker = std::thread( [&]() { - image.clear(); - accumulator.clear(); - image.resize(params.width * params.height * 3); - accumulator.resize(params.width * params.height * 3); for (int samp = 0; samp < params.numSamples; ++samp) { if (pendingCancel) @@ -23,18 +30,21 @@ void Scene::render(Camera cam, RenderParameter params) Batch batch; for (int w = 0; w < params.width; ++w) { - for (int h = 0; h < params.height; ++h) - { - batch.jobs.push_back( - [&]() + batch.jobs.push_back( + [&, w]() + { + for (int h = 0; h < params.height; ++h) { - Ray r = Ray(); - bvh.traceRay(r); - }); - } + // Ray r = Ray(); + // bvh.traceRay(r); + accumulator[w + h * params.width] += + glm::vec3(w / float(params.width * params.numSamples), h / float(params.height * params.numSamples), 0); + } + }); } threadPool.runBatch(std::move(batch)); - std::memcpy(image.data(), accumulator.data(), accumulator.size()); + std::memcpy(image.data(), accumulator.data(), accumulator.size() * sizeof(glm::vec3)); + std::cout << samp << std::endl; } }); } diff --git a/src/scene/Scene.h b/src/scene/Scene.h index 24dde72..8f860e9 100644 --- a/src/scene/Scene.h +++ b/src/scene/Scene.h @@ -17,14 +17,14 @@ class Scene Scene(); ~Scene(); void render(Camera cam, RenderParameter params); - constexpr const std::vector& getImage() const { return image; } + constexpr const std::vector& getImage() const { return image; } private: std::atomic_bool pendingCancel = false; ThreadPool threadPool; std::thread worker; // the thing being displayed - std::vector image; + std::vector image; // radiance accumulator - std::vector accumulator; + std::vector accumulator; BVH bvh; }; \ No newline at end of file diff --git a/src/window/Window.cpp b/src/window/Window.cpp index 189166d..eb05c44 100644 --- a/src/window/Window.cpp +++ b/src/window/Window.cpp @@ -18,7 +18,7 @@ Window::Window(int width, int height) : width(width), height(height) glBindVertexArray(vao); glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_FLOAT, nullptr); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); program = glCreateProgram(); vertShader = glCreateShader(GL_VERTEX_SHADER); @@ -76,11 +76,11 @@ Window::Window(int width, int height) : width(width), height(height) Window::~Window() {} -void Window::update(const std::vector& textureData) +void Window::update(const std::vector& textureData) { glClear(GL_COLOR_BUFFER_BIT); glBindTexture(GL_TEXTURE_2D, texture); - glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, textureData.data()); + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_FLOAT, textureData.data()); glUseProgram(program); glDrawArrays(GL_TRIANGLES, 0, 3); glfwSwapBuffers(window); diff --git a/src/window/Window.h b/src/window/Window.h index 4eed59d..c5bc3fa 100644 --- a/src/window/Window.h +++ b/src/window/Window.h @@ -2,13 +2,14 @@ #include #include #include +#include class Window { public: Window(int width, int height); ~Window(); - void update(const std::vector& textureData); + void update(const std::vector& textureData); private: int width; From 21ceebbe4b0ed9ee9a5ac5e46b2641d338f46d3c Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Fri, 24 Jan 2025 22:34:23 +0100 Subject: [PATCH 2/2] adding basic timing --- CMakeSettings.json | 13 +++++++++++++ src/scene/Scene.cpp | 4 +++- src/window/Window.cpp | 5 ++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/CMakeSettings.json b/CMakeSettings.json index 7dc129b..7a02fa3 100644 --- a/CMakeSettings.json +++ b/CMakeSettings.json @@ -11,6 +11,19 @@ "buildCommandArgs": "", "ctestCommandArgs": "", "cmakeExecutable": "C:/Program Files/CMake/bin/cmake.exe" + }, + { + "name": "Release", + "generator": "Ninja", + "configurationType": "RelWithDebInfo", + "buildRoot": "${projectDir}\\bin\\${name}", + "installRoot": "${projectDir}\\out\\install\\${name}", + "cmakeExecutable": "C:/Program Files/CMake/bin/cmake.exe", + "cmakeCommandArgs": "", + "buildCommandArgs": "", + "ctestCommandArgs": "", + "inheritEnvironments": [ "msvc_x64_x64" ], + "variables": [] } ] } \ No newline at end of file diff --git a/src/scene/Scene.cpp b/src/scene/Scene.cpp index 4dc4c92..93d0efc 100644 --- a/src/scene/Scene.cpp +++ b/src/scene/Scene.cpp @@ -42,9 +42,11 @@ void Scene::render(Camera cam, RenderParameter params) } }); } + auto start = std::chrono::high_resolution_clock::now(); threadPool.runBatch(std::move(batch)); + auto end = std::chrono::high_resolution_clock::now(); + std::cout << std::chrono::duration_cast(end - start).count() << std::endl; std::memcpy(image.data(), accumulator.data(), accumulator.size() * sizeof(glm::vec3)); - std::cout << samp << std::endl; } }); } diff --git a/src/window/Window.cpp b/src/window/Window.cpp index eb05c44..96a2877 100644 --- a/src/window/Window.cpp +++ b/src/window/Window.cpp @@ -1,5 +1,4 @@ #include "Window.h" -#include #include #define GLSL(...) "#version 400\n" #__VA_ARGS__ @@ -7,13 +6,13 @@ Window::Window(int width, int height) : width(width), height(height) { glewExperimental = true; - assert(glfwInit()); + glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // We don't want the old OpenGL window = glfwCreateWindow(width, height, "RayTracer", nullptr, nullptr); glfwMakeContextCurrent(window); - assert(!glewInit()); + glewInit(); glGenVertexArrays(1, &vao); glBindVertexArray(vao); glGenTextures(1, &texture);