Basic scene loop
This commit is contained in:
+10
-9
@@ -22,15 +22,13 @@ ThreadPool::~ThreadPool()
|
||||
}
|
||||
}
|
||||
|
||||
void ThreadPool::addJob(std::function<void()>&& job)
|
||||
void ThreadPool::runBatch(Batch&& batch)
|
||||
{
|
||||
{
|
||||
std::unique_lock l(queueLock);
|
||||
taskQueue.push_back(job);
|
||||
taskQueue.push_back(batch);
|
||||
queueCV.notify_one();
|
||||
}
|
||||
|
||||
void ThreadPool::waitIdle()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
std::unique_lock l(queueLock);
|
||||
@@ -47,19 +45,22 @@ void ThreadPool::work()
|
||||
std::function<void()> job;
|
||||
{
|
||||
std::unique_lock l(queueLock);
|
||||
if(taskQueue.empty())
|
||||
if (taskQueue.front().jobs.empty())
|
||||
{
|
||||
queueCV.wait(l);
|
||||
continue;
|
||||
}
|
||||
job = taskQueue.front();
|
||||
taskQueue.pop_front();
|
||||
job = taskQueue.front().jobs.front();
|
||||
taskQueue.front().jobs.pop_front();
|
||||
}
|
||||
job();
|
||||
{
|
||||
std::unique_lock l(queueLock);
|
||||
if (taskQueue.front().jobs.empty())
|
||||
{
|
||||
taskQueue.pop_front();
|
||||
completedCV.notify_one();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+7
-3
@@ -4,19 +4,23 @@
|
||||
#include <list>
|
||||
#include <thread>
|
||||
|
||||
struct Batch
|
||||
{
|
||||
std::list<std::function<void()>> jobs;
|
||||
};
|
||||
|
||||
class ThreadPool
|
||||
{
|
||||
public:
|
||||
ThreadPool(uint32_t numWorkers = std::thread::hardware_concurrency());
|
||||
~ThreadPool();
|
||||
void addJob(std::function<void()>&& job);
|
||||
void waitIdle();
|
||||
void runBatch(Batch&& batch);
|
||||
private:
|
||||
std::atomic_bool running = true;
|
||||
void work();
|
||||
std::mutex queueLock;
|
||||
std::condition_variable queueCV;
|
||||
std::condition_variable completedCV;
|
||||
std::list<std::function<void()>> taskQueue;
|
||||
std::list<Batch> taskQueue;
|
||||
std::vector<std::thread> workers;
|
||||
};
|
||||
+12
-6
@@ -1,18 +1,24 @@
|
||||
#include "scene/BVH.h"
|
||||
#include "scene/Scene.h"
|
||||
#include "util/ModelLoader.h"
|
||||
#include "window/Window.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
Scene scene;
|
||||
Window window;
|
||||
Window window(1920, 1080);
|
||||
scene.render(
|
||||
Camera{
|
||||
.position = glm::vec3(10, 0, 0),
|
||||
.direction = glm::vec3(-1, 0, 0),
|
||||
},
|
||||
RenderParameter{
|
||||
.width = 1920,
|
||||
.height = 1080,
|
||||
.numSamples = 10000,
|
||||
});
|
||||
while (true)
|
||||
{
|
||||
// IMGUI....
|
||||
if (Imgui.Button())
|
||||
{
|
||||
scene.render();
|
||||
}
|
||||
window.update(scene.getImage());
|
||||
}
|
||||
return 0;
|
||||
|
||||
+22
-16
@@ -1,34 +1,40 @@
|
||||
#include "Scene.h"
|
||||
|
||||
Scene::Scene(glm::vec3 cameraPos, glm::vec3 cameraDirection) : cameraPos(cameraPos), cameraDirection(cameraDirection) {}
|
||||
Scene::Scene() {}
|
||||
|
||||
Scene::~Scene() {}
|
||||
|
||||
void Scene::render(int width, int height, int numSamples)
|
||||
void Scene::render(Camera cam, RenderParameter params)
|
||||
{
|
||||
pendingCancel = true;
|
||||
worker.join();
|
||||
pendingCancel = false;
|
||||
worker = std::thread(
|
||||
[&]()
|
||||
{
|
||||
image.clear();
|
||||
accumulator.clear();
|
||||
image.resize(width * height * 3);
|
||||
for (int samp = 0; samp < numSamples; ++samp)
|
||||
image.resize(params.width * params.height * 3);
|
||||
accumulator.resize(params.width * params.height * 3);
|
||||
for (int samp = 0; samp < params.numSamples; ++samp)
|
||||
{
|
||||
std::function<void()> job = [&]()
|
||||
{
|
||||
std::vector<unsigned char> localAccumulator(width * height * 3);
|
||||
for (int w = 0; w < width; ++w)
|
||||
{
|
||||
for (int h = 0; h < height; ++h)
|
||||
{
|
||||
if (cancel)
|
||||
if (pendingCancel)
|
||||
return;
|
||||
bvh.traceRay();
|
||||
Batch batch;
|
||||
for (int w = 0; w < params.width; ++w)
|
||||
{
|
||||
for (int h = 0; h < params.height; ++h)
|
||||
{
|
||||
batch.jobs.push_back(
|
||||
[&]()
|
||||
{
|
||||
Ray r = Ray();
|
||||
bvh.traceRay(r);
|
||||
});
|
||||
}
|
||||
}
|
||||
// lock image
|
||||
// add result to image
|
||||
};
|
||||
threadPool.runBatch(std::move(batch));
|
||||
std::memcpy(image.data(), accumulator.data(), accumulator.size());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+12
-12
@@ -2,26 +2,26 @@
|
||||
#include "BVH.h"
|
||||
#include "window/Window.h"
|
||||
#include "util/Camera.h"
|
||||
#include "ThreadPool.h"
|
||||
|
||||
struct RenderParameter
|
||||
{
|
||||
int width;
|
||||
int height;
|
||||
int numSamples;
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Ray1
|
||||
/// Ray2
|
||||
/// Ray3
|
||||
/// Ray4
|
||||
/// Ray5
|
||||
/// GammaResolve
|
||||
/// Ray1
|
||||
/// Ray2
|
||||
/// Ray3
|
||||
/// </summary>
|
||||
class Scene
|
||||
{
|
||||
public:
|
||||
Scene();
|
||||
~Scene();
|
||||
void render(Camera cam, int width, int height, int numSamples);
|
||||
void render(Camera cam, RenderParameter params);
|
||||
constexpr const std::vector<unsigned char>& getImage() const { return image; }
|
||||
private:
|
||||
std::atomic_bool pendingCancel = false;
|
||||
ThreadPool threadPool;
|
||||
std::thread worker;
|
||||
// the thing being displayed
|
||||
std::vector<unsigned char> image;
|
||||
// radiance accumulator
|
||||
|
||||
Reference in New Issue
Block a user