marking it much faster
This commit is contained in:
@@ -21,6 +21,7 @@ find_package(imgui CONFIG REQUIRED)
|
|||||||
|
|
||||||
add_executable(RayTracer "")
|
add_executable(RayTracer "")
|
||||||
target_include_directories(RayTracer PUBLIC src/)
|
target_include_directories(RayTracer PUBLIC src/)
|
||||||
|
target_include_directories(RayTracer PUBLIC ${VCPKG_INSTALLED_DIR}/x64-windows/include)
|
||||||
target_link_libraries(RayTracer PUBLIC Vulkan::Vulkan)
|
target_link_libraries(RayTracer PUBLIC Vulkan::Vulkan)
|
||||||
target_link_libraries(RayTracer PUBLIC Vulkan::Headers)
|
target_link_libraries(RayTracer PUBLIC Vulkan::Headers)
|
||||||
target_link_libraries(RayTracer PUBLIC assimp::assimp)
|
target_link_libraries(RayTracer PUBLIC assimp::assimp)
|
||||||
@@ -29,5 +30,6 @@ target_link_libraries(RayTracer PUBLIC imgui::imgui)
|
|||||||
target_link_libraries(RayTracer PUBLIC GLEW::GLEW)
|
target_link_libraries(RayTracer PUBLIC GLEW::GLEW)
|
||||||
target_link_libraries(RayTracer PUBLIC glm::glm)
|
target_link_libraries(RayTracer PUBLIC glm::glm)
|
||||||
target_link_libraries(RayTracer PUBLIC KTX::ktx)
|
target_link_libraries(RayTracer PUBLIC KTX::ktx)
|
||||||
|
target_link_libraries(RayTracer PUBLIC ${VCPKG_INSTALLED_DIR}/x64-windows/lib/slang.lib)
|
||||||
|
|
||||||
add_subdirectory(src/)
|
add_subdirectory(src/)
|
||||||
@@ -22,6 +22,22 @@ ThreadPool::~ThreadPool()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ThreadPool::cancel()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::unique_lock l(queueLock);
|
||||||
|
numRemaining = numRunning;
|
||||||
|
taskQueue.clear();
|
||||||
|
}
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
std::unique_lock l(queueLock);
|
||||||
|
if (taskQueue.empty())
|
||||||
|
return;
|
||||||
|
completedCV.wait(l);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void ThreadPool::runBatch(Batch&& batch)
|
void ThreadPool::runBatch(Batch&& batch)
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
@@ -53,11 +69,13 @@ void ThreadPool::work()
|
|||||||
}
|
}
|
||||||
job = taskQueue.front().jobs.front();
|
job = taskQueue.front().jobs.front();
|
||||||
taskQueue.front().jobs.pop_front();
|
taskQueue.front().jobs.pop_front();
|
||||||
|
numRunning++;
|
||||||
}
|
}
|
||||||
job.handle();
|
job.handle();
|
||||||
{
|
{
|
||||||
std::unique_lock l(queueLock);
|
std::unique_lock l(queueLock);
|
||||||
numRemaining--;
|
numRemaining--;
|
||||||
|
numRunning--;
|
||||||
if (numRemaining == 0)
|
if (numRemaining == 0)
|
||||||
{
|
{
|
||||||
taskQueue.pop_front();
|
taskQueue.pop_front();
|
||||||
|
|||||||
@@ -15,6 +15,9 @@ class ThreadPool
|
|||||||
public:
|
public:
|
||||||
ThreadPool(uint32_t numWorkers = std::thread::hardware_concurrency());
|
ThreadPool(uint32_t numWorkers = std::thread::hardware_concurrency());
|
||||||
~ThreadPool();
|
~ThreadPool();
|
||||||
|
|
||||||
|
// cancel running jobs
|
||||||
|
void cancel();
|
||||||
void runBatch(Batch&& batch);
|
void runBatch(Batch&& batch);
|
||||||
private:
|
private:
|
||||||
std::atomic_bool running = true;
|
std::atomic_bool running = true;
|
||||||
@@ -23,6 +26,7 @@ private:
|
|||||||
std::condition_variable queueCV;
|
std::condition_variable queueCV;
|
||||||
std::condition_variable completedCV;
|
std::condition_variable completedCV;
|
||||||
uint32_t numRemaining;
|
uint32_t numRemaining;
|
||||||
|
uint32_t numRunning;
|
||||||
std::list<Batch> taskQueue;
|
std::list<Batch> taskQueue;
|
||||||
std::vector<std::thread> workers;
|
std::vector<std::thread> workers;
|
||||||
};
|
};
|
||||||
+43
-3
@@ -1,4 +1,6 @@
|
|||||||
#include "Renderer.h"
|
#include "Renderer.h"
|
||||||
|
#include <slang-com-ptr.h>
|
||||||
|
#include <slang.h>
|
||||||
|
|
||||||
Renderer::Renderer()
|
Renderer::Renderer()
|
||||||
: instance(nullptr), physicalDevice(nullptr), device(nullptr), queue(nullptr), cmdPool(nullptr), cmdBuffers(nullptr),
|
: instance(nullptr), physicalDevice(nullptr), device(nullptr), queue(nullptr), cmdPool(nullptr), cmdBuffers(nullptr),
|
||||||
@@ -10,7 +12,8 @@ Renderer::Renderer()
|
|||||||
|
|
||||||
Renderer::~Renderer() {}
|
Renderer::~Renderer() {}
|
||||||
|
|
||||||
void Renderer::createDevice() {
|
void Renderer::createDevice()
|
||||||
|
{
|
||||||
vk::ApplicationInfo appInfo("RayTracer", 1, "RayTracer", 1, VK_API_VERSION_1_3);
|
vk::ApplicationInfo appInfo("RayTracer", 1, "RayTracer", 1, VK_API_VERSION_1_3);
|
||||||
vk::InstanceCreateInfo instanceCreateInfo({}, &appInfo);
|
vk::InstanceCreateInfo instanceCreateInfo({}, &appInfo);
|
||||||
instance = Instance(context, instanceCreateInfo);
|
instance = Instance(context, instanceCreateInfo);
|
||||||
@@ -52,7 +55,8 @@ void Renderer::createCommands()
|
|||||||
cmdBuffers = vk::raii::CommandBuffers(device, commandBufferAllocateInfo);
|
cmdBuffers = vk::raii::CommandBuffers(device, commandBufferAllocateInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::createDescriptors() {
|
void Renderer::createDescriptors()
|
||||||
|
{
|
||||||
vk::DescriptorSetLayoutBinding descriptorSetLayoutBinding(0, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex);
|
vk::DescriptorSetLayoutBinding descriptorSetLayoutBinding(0, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex);
|
||||||
vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo({}, descriptorSetLayoutBinding);
|
vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo({}, descriptorSetLayoutBinding);
|
||||||
descriptorLayout = DescriptorSetLayout(device, descriptorSetLayoutCreateInfo);
|
descriptorLayout = DescriptorSetLayout(device, descriptorSetLayoutCreateInfo);
|
||||||
@@ -62,8 +66,44 @@ void Renderer::createDescriptors() {
|
|||||||
pipelineLayout = PipelineLayout(device, pipelineLayoutCreateInfo);
|
pipelineLayout = PipelineLayout(device, pipelineLayoutCreateInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::createShaders() {
|
using namespace slang;
|
||||||
|
|
||||||
|
void Renderer::createShaders()
|
||||||
|
{
|
||||||
|
Slang::ComPtr<IGlobalSession> globalSession;
|
||||||
|
SlangGlobalSessionDesc desc = {};
|
||||||
|
createGlobalSession(&desc, globalSession.writeRef());
|
||||||
|
SessionDesc sessionDesc;
|
||||||
|
TargetDesc targetDesc;
|
||||||
|
targetDesc.format = SLANG_SPIRV;
|
||||||
|
targetDesc.profile = globalSession->findProfile("glsl_450");
|
||||||
|
sessionDesc.targets = &targetDesc;
|
||||||
|
sessionDesc.targetCount = 1;
|
||||||
|
const char* searchPaths[] = {"res/shaders/"};
|
||||||
|
sessionDesc.searchPaths = searchPaths;
|
||||||
|
sessionDesc.searchPathCount = 1;
|
||||||
|
/* ... fill in `sessionDesc` ... */
|
||||||
|
Slang::ComPtr<ISession> session;
|
||||||
|
globalSession->createSession(sessionDesc, session.writeRef());
|
||||||
|
|
||||||
|
Slang::ComPtr<IBlob> diagnostics;
|
||||||
|
IModule* module = session->loadModule("MyShaders", diagnostics.writeRef());
|
||||||
|
if (diagnostics)
|
||||||
|
{
|
||||||
|
std::cout << (const char*)diagnostics->getBufferPointer() << std::endl;
|
||||||
|
}
|
||||||
|
Slang::ComPtr<IEntryPoint> computeEntryPoint;
|
||||||
|
module->findEntryPointByName("myComputeMain", computeEntryPoint.writeRef());
|
||||||
|
IComponentType* components[] = {module, computeEntryPoint};
|
||||||
|
Slang::ComPtr<IComponentType> program;
|
||||||
|
session->createCompositeComponentType(components, 2, program.writeRef());
|
||||||
|
Slang::ComPtr<IComponentType> linkedProgram;
|
||||||
|
Slang::ComPtr<ISlangBlob> diagnosticBlob;
|
||||||
|
program->link(linkedProgram.writeRef(), diagnosticBlob.writeRef());
|
||||||
|
int entryPointIndex = 0; // only one entry point
|
||||||
|
int targetIndex = 0; // only one target
|
||||||
|
Slang::ComPtr<IBlob> kernelBlob;
|
||||||
|
linkedProgram->getEntryPointCode(entryPointIndex, targetIndex, kernelBlob.writeRef(), diagnostics.writeRef());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::render(Camera cam, RenderParameter param) {}
|
void Renderer::render(Camera cam, RenderParameter param) {}
|
||||||
+1
-1
@@ -44,7 +44,7 @@ struct AABB
|
|||||||
max = glm::vec3(std::max(max.x, transformed.x), std::max(max.y, transformed.y), std::max(max.z, transformed.z));
|
max = glm::vec3(std::max(max.x, transformed.x), std::max(max.y, transformed.y), std::max(max.z, transformed.z));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bool intersects(Ray ray, float tmin, float tmax)
|
bool intersects(Ray ray, float tmin, float tmax) const
|
||||||
{
|
{
|
||||||
glm::vec3 invD = 1.0f / ray.direction;
|
glm::vec3 invD = 1.0f / ray.direction;
|
||||||
glm::vec3 t0s = glm::vec3(min - ray.origin) * invD;
|
glm::vec3 t0s = glm::vec3(min - ray.origin) * invD;
|
||||||
|
|||||||
+79
-87
@@ -4,110 +4,102 @@
|
|||||||
|
|
||||||
void BVH::addModel(PModel model, glm::mat4 transform)
|
void BVH::addModel(PModel model, glm::mat4 transform)
|
||||||
{
|
{
|
||||||
model->boundingBox.transform(transform);
|
model->transform(transform);
|
||||||
for (auto& point : model->positions)
|
models.push_back(std::move(model));
|
||||||
{
|
|
||||||
point = glm::vec3(transform * glm::vec4(point, 1));
|
|
||||||
}
|
|
||||||
models.push_back(std::move(model));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BVH::addModels(std::vector<PModel> _models, glm::mat4 transform)
|
void BVH::addModels(std::vector<PModel> _models, glm::mat4 transform)
|
||||||
{
|
{
|
||||||
for (auto & _model : _models)
|
for (auto& _model : _models)
|
||||||
{
|
{
|
||||||
_model->boundingBox.transform(transform);
|
_model->transform(transform);
|
||||||
for (auto& point : _model->positions)
|
models.push_back(std::move(_model));
|
||||||
{
|
}
|
||||||
point = glm::vec3(transform * glm::vec4(point, 1));
|
|
||||||
}
|
|
||||||
models.push_back(std::move(_model));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BVH::generate()
|
void BVH::generate()
|
||||||
{
|
{
|
||||||
std::vector<PNode> pendingNodes;
|
std::vector<PNode> pendingNodes;
|
||||||
while (!models.empty())
|
while (!models.empty())
|
||||||
|
{
|
||||||
|
pendingNodes.push_back(std::make_unique<Node>(std::move(models.back())));
|
||||||
|
models.pop_back();
|
||||||
|
}
|
||||||
|
while (pendingNodes.size() > 1)
|
||||||
|
{
|
||||||
|
int lhs = pendingNodes.size();
|
||||||
|
int rhs = pendingNodes.size();
|
||||||
|
float minSurface = std::numeric_limits<float>::max();
|
||||||
|
for (int i = 0; i < pendingNodes.size(); ++i)
|
||||||
{
|
{
|
||||||
pendingNodes.push_back(std::make_unique<Node>(std::move(models.back())));
|
for (int j = 0; j < pendingNodes.size(); ++j)
|
||||||
models.pop_back();
|
{
|
||||||
}
|
if (i == j)
|
||||||
while (pendingNodes.size() > 1)
|
continue;
|
||||||
{
|
AABB combined = AABB::combine(pendingNodes[i]->aabb, pendingNodes[j]->aabb);
|
||||||
int lhs = pendingNodes.size();
|
float surface = combined.surfaceArea();
|
||||||
int rhs = pendingNodes.size();
|
if (minSurface > surface)
|
||||||
float minSurface = std::numeric_limits<float>::max();
|
|
||||||
for (int i = 0; i < pendingNodes.size(); ++i)
|
|
||||||
{
|
{
|
||||||
for (int j = 0; j < pendingNodes.size(); ++j)
|
lhs = i;
|
||||||
{
|
rhs = j;
|
||||||
if (i == j)
|
minSurface = surface;
|
||||||
continue;
|
|
||||||
AABB combined = AABB::combine(pendingNodes[i]->aabb, pendingNodes[j]->aabb);
|
|
||||||
float surface = combined.surfaceArea();
|
|
||||||
if (minSurface > surface)
|
|
||||||
{
|
|
||||||
lhs = i;
|
|
||||||
rhs = j;
|
|
||||||
minSurface = surface;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
PNode newNode = std::make_unique<Node>(AABB::combine(pendingNodes[lhs]->aabb, pendingNodes[rhs]->aabb));
|
}
|
||||||
newNode->left = std::move(pendingNodes[lhs]);
|
|
||||||
newNode->right = std::move(pendingNodes[rhs]);
|
|
||||||
pendingNodes.erase(pendingNodes.begin() + lhs);
|
|
||||||
pendingNodes.erase(pendingNodes.begin() + rhs);
|
|
||||||
pendingNodes.push_back(std::move(newNode));
|
|
||||||
}
|
}
|
||||||
hierarchy = std::move(pendingNodes[0]);
|
PNode newNode = std::make_unique<Node>(AABB::combine(pendingNodes[lhs]->aabb, pendingNodes[rhs]->aabb));
|
||||||
|
newNode->left = std::move(pendingNodes[lhs]);
|
||||||
|
newNode->right = std::move(pendingNodes[rhs]);
|
||||||
|
pendingNodes.erase(pendingNodes.begin() + lhs);
|
||||||
|
pendingNodes.erase(pendingNodes.begin() + rhs);
|
||||||
|
pendingNodes.push_back(std::move(newNode));
|
||||||
|
}
|
||||||
|
hierarchy = std::move(pendingNodes[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<IntersectionInfo> BVH::traceRay(Ray ray)
|
std::optional<IntersectionInfo> BVH::traceRay(Ray ray) const
|
||||||
{
|
{
|
||||||
auto results = generateIntersections(hierarchy, ray);
|
auto results = generateIntersections(hierarchy, ray);
|
||||||
float closestT = std::numeric_limits<float>::max();
|
float closestT = std::numeric_limits<float>::max();
|
||||||
IntersectionInfo info;
|
IntersectionInfo info;
|
||||||
for (uint32_t i = 0; i < results.size(); ++i)
|
for (uint32_t i = 0; i < results.size(); ++i)
|
||||||
|
{
|
||||||
|
if (results[i].t < closestT)
|
||||||
{
|
{
|
||||||
if (results[i].t < closestT)
|
closestT = results[i].t;
|
||||||
{
|
info = results[i];
|
||||||
closestT = results[i].t;
|
|
||||||
info = results[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (closestT < std::numeric_limits<float>::max())
|
|
||||||
{
|
|
||||||
return info;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (closestT < std::numeric_limits<float>::max())
|
||||||
|
{
|
||||||
|
return info;
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<IntersectionInfo> BVH::generateIntersections(const PNode& currentNode, Ray ray) const
|
||||||
|
{
|
||||||
|
if (!currentNode->aabb.intersects(ray, 0, std::numeric_limits<float>::max()))
|
||||||
|
{
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
if (currentNode->model != nullptr)
|
||||||
|
{
|
||||||
|
auto result = currentNode->model->intersect(ray);
|
||||||
|
if (result.has_value())
|
||||||
|
{
|
||||||
|
return {*result};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
auto leftResults = generateIntersections(currentNode->left, ray);
|
||||||
|
auto rightResults = generateIntersections(currentNode->right, ray);
|
||||||
|
|
||||||
std::vector<IntersectionInfo> BVH::generateIntersections(PNode& currentNode, Ray ray)
|
for (auto& it : rightResults)
|
||||||
{
|
{
|
||||||
if (!currentNode->aabb.intersects(ray, 0, std::numeric_limits<float>::max()))
|
leftResults.push_back(std::move(it));
|
||||||
{
|
}
|
||||||
return {};
|
return leftResults;
|
||||||
}
|
|
||||||
if (currentNode->model != nullptr)
|
|
||||||
{
|
|
||||||
auto result = currentNode->model->intersect(ray);
|
|
||||||
if (result.has_value())
|
|
||||||
{
|
|
||||||
return {*result};
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
auto leftResults = generateIntersections(currentNode->left, ray);
|
|
||||||
auto rightResults = generateIntersections(currentNode->right, ray);
|
|
||||||
|
|
||||||
for (auto& it : rightResults)
|
|
||||||
{
|
|
||||||
leftResults.push_back(std::move(it));
|
|
||||||
}
|
|
||||||
return leftResults;
|
|
||||||
}
|
}
|
||||||
+21
-22
@@ -1,34 +1,33 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "AABB.h"
|
#include "AABB.h"
|
||||||
#include "util/Model.h"
|
#include "util/Model.h"
|
||||||
#include <vector>
|
|
||||||
#include <glm/glm.hpp>
|
|
||||||
#include "util/Ray.h"
|
#include "util/Ray.h"
|
||||||
|
#include <glm/glm.hpp>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class BVH
|
class BVH
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void addModel(PModel model, glm::mat4 transform);
|
void addModel(PModel model, glm::mat4 transform);
|
||||||
void addModels(std::vector<PModel> models, glm::mat4 transform);
|
void addModels(std::vector<PModel> models, glm::mat4 transform);
|
||||||
void generate();
|
void generate();
|
||||||
|
|
||||||
std::optional<IntersectionInfo> traceRay(Ray ray);
|
std::optional<IntersectionInfo> traceRay(Ray ray) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
DECLARE_REF(Node)
|
DECLARE_REF(Node)
|
||||||
struct Node
|
struct Node
|
||||||
{
|
{
|
||||||
PNode left;
|
PNode left;
|
||||||
PNode right;
|
PNode right;
|
||||||
AABB aabb;
|
AABB aabb;
|
||||||
PModel model;
|
PModel model;
|
||||||
Node(AABB aabb) : aabb(aabb) {}
|
Node(AABB aabb) : aabb(aabb) {}
|
||||||
Node(PModel model) : aabb(model->boundingBox), model(std::move(model)) {}
|
Node(PModel model) : aabb(model->boundingBox), model(std::move(model)) {}
|
||||||
};
|
};
|
||||||
PNode hierarchy;
|
PNode hierarchy;
|
||||||
std::vector<PModel> models;
|
std::vector<PModel> models;
|
||||||
|
|
||||||
std::vector<IntersectionInfo> generateIntersections(PNode& currentNode, Ray ray);
|
std::vector<IntersectionInfo> generateIntersections(const PNode& currentNode, Ray ray) const;
|
||||||
};
|
};
|
||||||
+13
-17
@@ -17,13 +17,7 @@ Scene::~Scene() {}
|
|||||||
static bool firstTime = true;
|
static bool firstTime = true;
|
||||||
void Scene::startRender(Camera cam, RenderParameter params)
|
void Scene::startRender(Camera cam, RenderParameter params)
|
||||||
{
|
{
|
||||||
if (!firstTime)
|
threadPool.cancel();
|
||||||
{
|
|
||||||
pendingCancel = true;
|
|
||||||
worker.join();
|
|
||||||
firstTime = false;
|
|
||||||
}
|
|
||||||
pendingCancel = false;
|
|
||||||
image.clear();
|
image.clear();
|
||||||
accumulator.clear();
|
accumulator.clear();
|
||||||
image.resize(params.width * params.height);
|
image.resize(params.width * params.height);
|
||||||
@@ -31,22 +25,23 @@ void Scene::startRender(Camera cam, RenderParameter params)
|
|||||||
worker = std::thread(&Scene::render, this, cam, params);
|
worker = std::thread(&Scene::render, this, cam, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glm::vec3 rand01(glm::uvec3 x)
|
||||||
|
{ // pseudo-random number generator
|
||||||
|
for (int i = 3; i-- > 0;)
|
||||||
|
x = ((x >> 8U) ^ glm::uvec3(x.y, x.z, x.x)) * 1103515245U;
|
||||||
|
return glm::vec3(x) * (1.0f / float(0xffffffffU));
|
||||||
|
}
|
||||||
|
|
||||||
void Scene::render(Camera camera, RenderParameter params)
|
void Scene::render(Camera camera, RenderParameter params)
|
||||||
{
|
{
|
||||||
std::random_device rd;
|
|
||||||
for (int samp = 0; samp < params.numSamples; ++samp)
|
for (int samp = 0; samp < params.numSamples; ++samp)
|
||||||
{
|
{
|
||||||
if (pendingCancel)
|
|
||||||
return;
|
|
||||||
Batch batch;
|
Batch batch;
|
||||||
for (int w = 0; w < params.width; ++w)
|
for (int w = 0; w < params.width; ++w)
|
||||||
{
|
{
|
||||||
batch.jobs.push_back(
|
batch.jobs.push_back(
|
||||||
[&](int w) -> Task
|
[&](int w, int samp) -> Task
|
||||||
{
|
{
|
||||||
std::mt19937 gen(rd());
|
|
||||||
std::uniform_real_distribution<float> rnd01(0.0, 1.0);
|
|
||||||
std::uniform_real_distribution<float> rnd02(0.0, 2.0);
|
|
||||||
for (int h = 0; h < params.height; ++h)
|
for (int h = 0; h < params.height; ++h)
|
||||||
{
|
{
|
||||||
Ray cam = Ray(camera.position, glm::normalize(camera.direction));
|
Ray cam = Ray(camera.position, glm::normalize(camera.direction));
|
||||||
@@ -59,7 +54,8 @@ void Scene::render(Camera camera, RenderParameter params)
|
|||||||
|
|
||||||
//-- sample sensor
|
//-- sample sensor
|
||||||
glm::uvec2 pix = glm::uvec2(w, h);
|
glm::uvec2 pix = glm::uvec2(w, h);
|
||||||
glm::vec2 rnd2 = glm::vec2(rnd02(gen), rnd02(gen)); // vvv tent filter sample
|
glm::vec3 rnd1 = rand01(glm::uvec3(pix, samp));
|
||||||
|
glm::vec2 rnd2 = 2.0f * glm::vec2(rnd1); // vvv tent filter sample
|
||||||
glm::vec2 tent =
|
glm::vec2 tent =
|
||||||
glm::vec2(rnd2.x < 1 ? sqrt(rnd2.x) - 1 : 1 - sqrt(2 - rnd2.x), rnd2.y < 1 ? sqrt(rnd2.y) - 1 : 1 - sqrt(2 - rnd2.y));
|
glm::vec2(rnd2.x < 1 ? sqrt(rnd2.x) - 1 : 1 - sqrt(2 - rnd2.x), rnd2.y < 1 ? sqrt(rnd2.y) - 1 : 1 - sqrt(2 - rnd2.y));
|
||||||
glm::vec2 s =
|
glm::vec2 s =
|
||||||
@@ -76,7 +72,7 @@ void Scene::render(Camera camera, RenderParameter params)
|
|||||||
glm::vec3 lensX = glm::cross(lensN, glm::vec3(0, 1, 0)); // the exact vector doesnt matter
|
glm::vec3 lensX = glm::cross(lensN, glm::vec3(0, 1, 0)); // the exact vector doesnt matter
|
||||||
glm::vec3 lensY = glm::cross(lensN, lensX);
|
glm::vec3 lensY = glm::cross(lensN, lensX);
|
||||||
|
|
||||||
glm::vec3 lensSample = lensP + rnd01(gen) * camera.A * lensX + rnd01(gen) * camera.A * lensY;
|
glm::vec3 lensSample = lensP + rnd1.x * camera.A * lensX + rnd1.y * camera.A * lensY;
|
||||||
|
|
||||||
glm::vec3 focalPoint = cam.origin + (camera.S_O + S_I) * cam.direction;
|
glm::vec3 focalPoint = cam.origin + (camera.S_O + S_I) * cam.direction;
|
||||||
float t = glm::dot(focalPoint - r.origin, lensN) / glm::dot(r.direction, lensN);
|
float t = glm::dot(focalPoint - r.origin, lensN) / glm::dot(r.direction, lensN);
|
||||||
@@ -91,7 +87,7 @@ void Scene::render(Camera camera, RenderParameter params)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
co_return;
|
co_return;
|
||||||
}(w));
|
}(w, samp));
|
||||||
}
|
}
|
||||||
auto start = std::chrono::high_resolution_clock::now();
|
auto start = std::chrono::high_resolution_clock::now();
|
||||||
threadPool.runBatch(std::move(batch));
|
threadPool.runBatch(std::move(batch));
|
||||||
|
|||||||
+15
-1
@@ -11,6 +11,19 @@ struct RenderParameter
|
|||||||
int numSamples;
|
int numSamples;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct PointLight
|
||||||
|
{
|
||||||
|
glm::vec3 position;
|
||||||
|
glm::vec3 color;
|
||||||
|
float attenuation;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct DirectionalLight
|
||||||
|
{
|
||||||
|
glm::vec3 direction;
|
||||||
|
glm::vec3 color;
|
||||||
|
};
|
||||||
|
|
||||||
class Scene
|
class Scene
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -20,12 +33,13 @@ class Scene
|
|||||||
constexpr const std::vector<glm::vec3>& getImage() const { return image; }
|
constexpr const std::vector<glm::vec3>& getImage() const { return image; }
|
||||||
private:
|
private:
|
||||||
virtual void render(Camera cam, RenderParameter params);
|
virtual void render(Camera cam, RenderParameter params);
|
||||||
std::atomic_bool pendingCancel = false;
|
|
||||||
ThreadPool threadPool;
|
ThreadPool threadPool;
|
||||||
std::thread worker;
|
std::thread worker;
|
||||||
// the thing being displayed
|
// the thing being displayed
|
||||||
std::vector<glm::vec3> image;
|
std::vector<glm::vec3> image;
|
||||||
// radiance accumulator
|
// radiance accumulator
|
||||||
std::vector<glm::vec3> accumulator;
|
std::vector<glm::vec3> accumulator;
|
||||||
|
std::vector<PointLight> pointLights;
|
||||||
|
std::vector<DirectionalLight> directionalLights;
|
||||||
BVH bvh;
|
BVH bvh;
|
||||||
};
|
};
|
||||||
@@ -6,4 +6,8 @@ target_sources(RayTracer
|
|||||||
ModelLoader.h
|
ModelLoader.h
|
||||||
ModelLoader.cpp
|
ModelLoader.cpp
|
||||||
Ray.h
|
Ray.h
|
||||||
|
Texture.h
|
||||||
|
Texture.cpp
|
||||||
|
TextureLoader.h
|
||||||
|
TextureLoader.cpp
|
||||||
)
|
)
|
||||||
+22
-1
@@ -1,6 +1,26 @@
|
|||||||
#include "Model.h"
|
#include "Model.h"
|
||||||
|
|
||||||
std::optional<IntersectionInfo> Model::intersect(const Ray ray)
|
void Model::transform(glm::mat4 matrix)
|
||||||
|
{
|
||||||
|
for (auto& pos : positions)
|
||||||
|
{
|
||||||
|
pos = glm::vec3(matrix * glm::vec4(pos, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
boundingBox.transform(matrix);
|
||||||
|
|
||||||
|
for (int i = 0; i < indices.size(); i+=3)
|
||||||
|
{
|
||||||
|
auto e0 = positions[indices[i + 1]] - positions[indices[i + 0]];
|
||||||
|
auto e1 = positions[indices[i + 2]] - positions[indices[i + 0]];
|
||||||
|
es.push_back(e0);
|
||||||
|
es.push_back(e1);
|
||||||
|
|
||||||
|
faceNormals.push_back(glm::cross(e0, e1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<IntersectionInfo> Model::intersect(const Ray ray) const
|
||||||
{
|
{
|
||||||
std::optional<IntersectionInfo> intersection = {};
|
std::optional<IntersectionInfo> intersection = {};
|
||||||
float distance = 0;
|
float distance = 0;
|
||||||
@@ -51,3 +71,4 @@ std::optional<IntersectionInfo> Model::intersect(const Ray ray)
|
|||||||
|
|
||||||
return intersection;
|
return intersection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+8
-7
@@ -1,8 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Minimal.h"
|
#include "Minimal.h"
|
||||||
#include "scene/AABB.h"
|
#include "scene/AABB.h"
|
||||||
#include <vector>
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
// material infos
|
// material infos
|
||||||
// shading parameter
|
// shading parameter
|
||||||
@@ -19,11 +19,12 @@ struct IntersectionInfo
|
|||||||
class Model
|
class Model
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
AABB boundingBox;
|
AABB boundingBox;
|
||||||
std::vector<glm::vec3> positions;
|
std::vector<glm::vec3> positions;
|
||||||
std::vector<uint32_t> indices;
|
std::vector<uint32_t> indices;
|
||||||
std::vector<glm::vec3> es;
|
std::vector<glm::vec3> es;
|
||||||
std::vector<glm::vec3> faceNormals;
|
std::vector<glm::vec3> faceNormals;
|
||||||
std::optional<IntersectionInfo> intersect(Ray ray);
|
void transform(glm::mat4 matrix);
|
||||||
|
std::optional<IntersectionInfo> intersect(Ray ray) const;
|
||||||
};
|
};
|
||||||
DECLARE_REF(Model)
|
DECLARE_REF(Model)
|
||||||
@@ -28,12 +28,6 @@ std::vector<PModel> ModelLoader::loadModel(std::string_view filename)
|
|||||||
model->indices.push_back(face.mIndices[1]);
|
model->indices.push_back(face.mIndices[1]);
|
||||||
model->indices.push_back(face.mIndices[2]);
|
model->indices.push_back(face.mIndices[2]);
|
||||||
|
|
||||||
auto e0 = model->positions[face.mIndices[1]] - model->positions[face.mIndices[0]];
|
|
||||||
auto e1 = model->positions[face.mIndices[2]] - model->positions[face.mIndices[0]];
|
|
||||||
model->es.push_back(e0);
|
|
||||||
model->es.push_back(e1);
|
|
||||||
|
|
||||||
model->faceNormals.push_back(glm::cross(e0, e1));
|
|
||||||
}
|
}
|
||||||
model->boundingBox = aabb;
|
model->boundingBox = aabb;
|
||||||
result.push_back(std::move(model));
|
result.push_back(std::move(model));
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#include "Texture.h"
|
||||||
|
|
||||||
|
glm::vec3 Texture::sample(glm::vec2 texCoords) const
|
||||||
|
{
|
||||||
|
uint32_t x = texCoords.x * width;
|
||||||
|
uint32_t y = texCoords.y * height;
|
||||||
|
return textureData[y * width + x];
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "Minimal.h"
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <ktx.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class Texture
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
glm::vec3 sample(glm::vec2 texCoords) const;
|
||||||
|
std::vector<glm::vec3> textureData;
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
};
|
||||||
|
DECLARE_REF(Texture)
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#include "TextureLoader.h"
|
||||||
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
#include <stb_image.h>
|
||||||
|
|
||||||
|
PTexture TextureLoader::loadTexture(std::string_view filename)
|
||||||
|
{
|
||||||
|
int x, y, n;
|
||||||
|
auto* data = stbi_load(filename.data(), &x, &y, &n, 3);
|
||||||
|
std::vector<glm::vec3> texData(x * y);
|
||||||
|
for (uint32_t i = 0; i < texData.size(); ++i)
|
||||||
|
{
|
||||||
|
texData[i] = glm::vec3(data[i * 3 + 0] / 256.f, data[i * 3 + 1] / 256.f, data[i * 3 + 2] / 256.f);
|
||||||
|
}
|
||||||
|
auto result = std::make_unique<Texture>();
|
||||||
|
result->textureData = std::move(texData);
|
||||||
|
result->width = x;
|
||||||
|
result->height = y;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <string_view>
|
||||||
|
#include "Texture.h"
|
||||||
|
|
||||||
|
class TextureLoader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static PTexture loadTexture(std::string_view filename);
|
||||||
|
|
||||||
|
private:
|
||||||
|
};
|
||||||
+2
-1
@@ -10,6 +10,7 @@
|
|||||||
"glfw3",
|
"glfw3",
|
||||||
"glew",
|
"glew",
|
||||||
"glm",
|
"glm",
|
||||||
"fmt"
|
"fmt",
|
||||||
|
"shader-slang"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user