moving files

This commit is contained in:
Dynamitos
2025-01-25 13:04:12 +01:00
parent 9370e4125a
commit a47f921d08
14 changed files with 152 additions and 80 deletions
+43 -46
View File
@@ -1,65 +1,62 @@
#include "Scene.h"
#include "util/ModelLoader.h"
#include <iostream>
#include <chrono>
#include <iostream>
Scene::Scene()
{
bvh.addModels(ModelLoader::loadModel("../cube.fbx"),
glm::mat4(glm::vec4(1.0f, 0.0f, 0.0f, 0.0f),
glm::vec4(0.0f, 1.0f, 0.0f, 0.0f),
glm::vec4(0.0f, 0.0f, 1.0f, 0.0f),
glm::vec4(0.0f, 0.0f, 0.0f, 1.0f)));
bvh.addModels(ModelLoader::loadModel("../res/models/cube.fbx"), glm::mat4(glm::vec4(1.0f, 0.0f, 0.0f, 0.0f), glm::vec4(0.0f, 1.0f, 0.0f, 0.0f),
glm::vec4(0.0f, 0.0f, 1.0f, 0.0f), glm::vec4(0.0f, 0.0f, 0.0f, 1.0f)));
bvh.generate();
}
Scene::~Scene() {}
static bool firstTime = true;
void Scene::startRender(Camera cam, RenderParameter params)
{
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(&Scene::render, this, cam, params);
}
void Scene::render(Camera cam, RenderParameter params)
{
if (!firstTime)
for (int samp = 0; samp < params.numSamples; ++samp)
{
if (pendingCancel)
return;
Batch batch;
for (int w = 0; w < params.width; ++w)
{
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(
[&, cam, params]()
{
for (int samp = 0; samp < params.numSamples; ++samp)
batch.jobs.push_back(
[&, w]()
{
for (int h = 0; h < params.height; ++h)
{
if (pendingCancel)
return;
Batch batch;
for (int w = 0; w < params.width; ++w)
{
batch.jobs.push_back(
[&, w]()
{
for (int h = 0; h < params.height; ++h)
{
Ray r = Ray{
.origin = glm::vec3(0.0f),
.direction = glm::normalize(glm::vec3((float)std::rand() / RAND_MAX, (float)std::rand() / RAND_MAX, (float)std::rand() / RAND_MAX))
};
bvh.traceRay(r);
Ray r = Ray{.origin = glm::vec3(0.0f),
.direction = glm::normalize(
glm::vec3((float)std::rand() / RAND_MAX, (float)std::rand() / RAND_MAX, (float)std::rand() / RAND_MAX))};
bvh.traceRay(r);
accumulator[w + h * params.width] +=
glm::vec3(w / float(params.width * params.numSamples), h / float(params.height * params.numSamples), 0);
}
});
}
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<std::chrono::milliseconds>(end - start).count() << std::endl;
std::memcpy(image.data(), accumulator.data(), accumulator.size() * sizeof(glm::vec3));
accumulator[w + h * params.width] +=
glm::vec3(w / float(params.width * params.numSamples), h / float(params.height * params.numSamples), 0);
}
});
});
}
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<std::chrono::milliseconds>(end - start).count() << std::endl;
std::memcpy(image.data(), accumulator.data(), accumulator.size() * sizeof(glm::vec3));
}
}
+3 -2
View File
@@ -15,10 +15,11 @@ class Scene
{
public:
Scene();
~Scene();
void render(Camera cam, RenderParameter params);
virtual ~Scene();
void startRender(Camera cam, RenderParameter params);
constexpr const std::vector<glm::vec3>& getImage() const { return image; }
private:
virtual void render(Camera cam, RenderParameter params);
std::atomic_bool pendingCancel = false;
ThreadPool threadPool;
std::thread worker;