diff --git a/src/ThreadPool.cpp b/src/ThreadPool.cpp index df0f0b3..391a887 100644 --- a/src/ThreadPool.cpp +++ b/src/ThreadPool.cpp @@ -27,7 +27,6 @@ void ThreadPool::cancel() { std::unique_lock l(queueLock); numRemaining = numRunning; - taskQueue.clear(); } while (true) { @@ -79,7 +78,7 @@ void ThreadPool::work() if (numRemaining == 0) { taskQueue.pop_front(); - completedCV.notify_one(); + completedCV.notify_all(); } } } diff --git a/src/main.cpp b/src/main.cpp index 329b399..25f1ea9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,9 +22,18 @@ int main() while (true) { window.beginFrame(); - if (ImGui::Button("Test")) + if (ImGui::Button("Render")) { - std::cout << "Test" << std::endl; + scene.startRender( + Camera{ + .position = glm::vec3(0, 10, 10), + .direction = glm::vec3(0, -1, -1), + }, + RenderParameter{ + .width = 1920, + .height = 1080, + .numSamples = 10000, + }); } window.update(scene.getImage()); } diff --git a/src/scene/Scene.cpp b/src/scene/Scene.cpp index a3d426e..c30ce97 100644 --- a/src/scene/Scene.cpp +++ b/src/scene/Scene.cpp @@ -18,6 +18,10 @@ static bool firstTime = true; void Scene::startRender(Camera cam, RenderParameter params) { threadPool.cancel(); + pendingCancel = true; + if (worker.joinable()) + worker.join(); + pendingCancel = false; image.clear(); accumulator.clear(); image.resize(params.width * params.height); @@ -36,6 +40,8 @@ void Scene::render(Camera camera, RenderParameter params) { for (int samp = 0; samp < params.numSamples; ++samp) { + if (pendingCancel) + return; Batch batch; for (int w = 0; w < params.width; ++w) { diff --git a/src/scene/Scene.h b/src/scene/Scene.h index ddd1c8f..d71f9ef 100644 --- a/src/scene/Scene.h +++ b/src/scene/Scene.h @@ -35,6 +35,7 @@ class Scene virtual void render(Camera cam, RenderParameter params); ThreadPool threadPool; std::thread worker; + std::atomic_bool pendingCancel = false; // the thing being displayed std::vector image; // radiance accumulator