2024-07-10 21:07:10 +02:00
|
|
|
#include "RayTracingPass.h"
|
2024-07-15 08:32:50 +02:00
|
|
|
#include "Asset/AssetRegistry.h"
|
2025-04-08 22:01:42 +02:00
|
|
|
#include "Asset/EnvironmentMapAsset.h"
|
2024-07-10 21:07:10 +02:00
|
|
|
#include "Graphics/RayTracing.h"
|
|
|
|
|
#include "Graphics/Shader.h"
|
|
|
|
|
#include "Graphics/StaticMeshVertexData.h"
|
2026-03-14 13:53:04 +01:00
|
|
|
#include "Material/Material.h"
|
|
|
|
|
#include "Material/MaterialInstance.h"
|
2026-04-12 20:49:02 +02:00
|
|
|
#include "RenderPass.h"
|
|
|
|
|
#include <iostream>
|
2024-07-10 21:07:10 +02:00
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
|
2024-12-25 14:59:08 +01:00
|
|
|
struct SampleParams {
|
|
|
|
|
uint32 pass;
|
|
|
|
|
uint32 samplesPerPixel;
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-08 19:15:12 +01:00
|
|
|
RayTracingPass::RayTracingPass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics), scene(scene) {
|
2024-07-10 21:07:10 +02:00
|
|
|
paramsLayout = graphics->createDescriptorLayout("pRayTracingParams");
|
2025-03-07 21:48:27 +01:00
|
|
|
paramsLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
|
|
|
|
.name = TLAS_NAME,
|
2024-07-10 21:07:10 +02:00
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR,
|
|
|
|
|
});
|
|
|
|
|
paramsLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
2025-03-07 21:48:27 +01:00
|
|
|
.name = ACCUMULATOR_NAME,
|
2024-07-10 21:07:10 +02:00
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE,
|
|
|
|
|
});
|
|
|
|
|
paramsLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
2025-03-07 21:48:27 +01:00
|
|
|
.name = TEXTURE_NAME,
|
2024-12-25 14:59:08 +01:00
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE,
|
|
|
|
|
});
|
|
|
|
|
paramsLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
2025-03-07 21:48:27 +01:00
|
|
|
.name = INDEXBUFFER_NAME,
|
2024-07-10 21:07:10 +02:00
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
|
|
|
|
});
|
2024-12-25 14:59:08 +01:00
|
|
|
paramsLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
2025-03-07 21:48:27 +01:00
|
|
|
.name = SKYBOX_NAME,
|
2024-12-25 14:59:08 +01:00
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
|
|
|
|
|
});
|
|
|
|
|
paramsLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
2025-03-07 21:48:27 +01:00
|
|
|
.name = SKYSAMPLER_NAME,
|
2024-12-25 14:59:08 +01:00
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER,
|
2025-03-07 21:48:27 +01:00
|
|
|
});
|
2024-07-10 21:07:10 +02:00
|
|
|
paramsLayout->create();
|
|
|
|
|
pipelineLayout = graphics->createPipelineLayout("RayTracing");
|
|
|
|
|
pipelineLayout->addDescriptorLayout(viewParamsLayout);
|
|
|
|
|
pipelineLayout->addDescriptorLayout(Material::getDescriptorLayout());
|
|
|
|
|
pipelineLayout->addDescriptorLayout(paramsLayout);
|
|
|
|
|
pipelineLayout->addDescriptorLayout(scene->getLightEnvironment()->getDescriptorLayout());
|
2024-07-12 13:33:52 +02:00
|
|
|
pipelineLayout->addDescriptorLayout(StaticMeshVertexData::getInstance()->getVertexDataLayout());
|
|
|
|
|
pipelineLayout->addDescriptorLayout(StaticMeshVertexData::getInstance()->getInstanceDataLayout());
|
2024-12-25 14:59:08 +01:00
|
|
|
pipelineLayout->addPushConstants(Gfx::SePushConstantRange{
|
|
|
|
|
.stageFlags = Gfx::SE_SHADER_STAGE_RAYGEN_BIT_KHR,
|
|
|
|
|
.offset = 0,
|
|
|
|
|
.size = sizeof(SampleParams),
|
2026-03-16 16:52:43 +01:00
|
|
|
.name = "pSamps",
|
2024-12-25 14:59:08 +01:00
|
|
|
});
|
2026-04-12 20:49:02 +02:00
|
|
|
|
|
|
|
|
graphics->beginShaderCompilation(ShaderCompilationInfo{
|
|
|
|
|
.name = "RayGenMiss",
|
|
|
|
|
.modules = {"RayGen", "AnyHit", "Miss"},
|
|
|
|
|
.entryPoints = {{"raygen", "RayGen"}, {"anyhit", "AnyHit"}, {"miss", "Miss"}},
|
|
|
|
|
.defines = {{"RAY_TRACING", "1"}},
|
|
|
|
|
.rootSignature = pipelineLayout,
|
|
|
|
|
});
|
|
|
|
|
rayGen = graphics->createRayGenShader({0});
|
|
|
|
|
anyhit = graphics->createAnyHitShader({1});
|
|
|
|
|
miss = graphics->createMissShader({2});
|
|
|
|
|
pipelineLayout->create();
|
2024-07-10 21:07:10 +02:00
|
|
|
graphics->getShaderCompiler()->registerRenderPass("RayTracing", Gfx::PassConfig{
|
|
|
|
|
.baseLayout = pipelineLayout,
|
2024-07-15 17:55:22 +02:00
|
|
|
.mainFile = "ClosestHit",
|
2024-07-10 21:07:10 +02:00
|
|
|
.useMaterial = true,
|
|
|
|
|
.rayTracing = true,
|
2026-04-12 20:49:02 +02:00
|
|
|
.dumpIntermediates = true,
|
2024-07-10 21:07:10 +02:00
|
|
|
});
|
2025-07-09 23:25:03 +02:00
|
|
|
skyBox = AssetRegistry::findEnvironmentMap("", "newport_loft")->getSkybox();
|
2024-12-25 14:59:08 +01:00
|
|
|
skyBoxSampler = graphics->createSampler({});
|
2024-07-10 21:07:10 +02:00
|
|
|
}
|
|
|
|
|
|
2025-01-12 11:26:52 +01:00
|
|
|
static uint32 pass = 0;
|
2025-04-11 23:13:19 +02:00
|
|
|
static Component::Transform lastCam;
|
|
|
|
|
void RayTracingPass::beginFrame(const Component::Camera& cam, const Component::Transform& transform) {
|
2025-07-14 21:10:07 +02:00
|
|
|
updateViewParameters(cam, transform);
|
2025-08-10 19:16:55 +02:00
|
|
|
viewParamsSet = createViewParamsSet();
|
|
|
|
|
if (lastCam.getPosition() != transform.getPosition() || lastCam.getForward() != transform.getForward()) {
|
2025-04-11 23:13:19 +02:00
|
|
|
lastCam = transform;
|
2025-01-12 11:26:52 +01:00
|
|
|
pass = 0;
|
2024-12-25 14:59:08 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-07-10 21:07:10 +02:00
|
|
|
|
|
|
|
|
void RayTracingPass::render() {
|
2025-05-06 19:36:43 +02:00
|
|
|
graphics->beginDebugRegion("RayTracingPass");
|
2025-03-26 13:38:48 +01:00
|
|
|
texture->changeLayout(Gfx::SE_IMAGE_LAYOUT_GENERAL, Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR);
|
2024-07-15 17:55:22 +02:00
|
|
|
Array<Gfx::RayTracingHitGroup> callableGroups;
|
2024-07-10 21:07:10 +02:00
|
|
|
Array<Gfx::PBottomLevelAS> accelerationStructures;
|
|
|
|
|
Array<InstanceData> instanceData;
|
|
|
|
|
|
|
|
|
|
for (VertexData* vertexData : VertexData::getList()) {
|
|
|
|
|
auto& materialData = vertexData->getMaterialData();
|
|
|
|
|
|
|
|
|
|
for (auto& matData : materialData) {
|
|
|
|
|
if (matData.instances.size() == 0)
|
|
|
|
|
continue;
|
|
|
|
|
PMaterial mat = matData.material;
|
|
|
|
|
|
|
|
|
|
Gfx::ShaderPermutation permutation = graphics->getShaderCompiler()->getTemplate("RayTracing");
|
2025-01-30 23:25:41 +01:00
|
|
|
permutation.setMaterial(mat->getName(), mat->getProfile());
|
2024-07-10 21:07:10 +02:00
|
|
|
permutation.setVertexData(vertexData->getTypeName());
|
|
|
|
|
|
|
|
|
|
const Gfx::ShaderCollection* collection = graphics->getShaderCompiler()->findShaders(Gfx::PermutationId(permutation));
|
|
|
|
|
assert(collection != nullptr);
|
|
|
|
|
|
|
|
|
|
for (auto& inst : matData.instances) {
|
|
|
|
|
for (uint32 i = 0; i < inst.instanceData.size(); ++i) {
|
2024-07-15 17:55:22 +02:00
|
|
|
Gfx::RayTracingHitGroup callableGroup = {
|
|
|
|
|
.closestHitShader = collection->callableShader,
|
2025-01-30 23:25:41 +01:00
|
|
|
.anyHitShader = anyhit,
|
2024-07-10 21:07:10 +02:00
|
|
|
};
|
2024-07-12 13:33:52 +02:00
|
|
|
callableGroup.parameters.resize(sizeof(VertexData::DrawCallOffsets));
|
|
|
|
|
std::memcpy(callableGroup.parameters.data(), &inst.offsets, sizeof(VertexData::DrawCallOffsets));
|
|
|
|
|
callableGroups.add(callableGroup);
|
2024-07-10 21:07:10 +02:00
|
|
|
|
|
|
|
|
instanceData.add(inst.instanceData[i]);
|
|
|
|
|
accelerationStructures.add(inst.rayTracingData[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-17 10:40:40 +02:00
|
|
|
for (const auto& transparentData : vertexData->getTransparentData()) {
|
|
|
|
|
PMaterial mat = transparentData.matInst->getBaseMaterial();
|
|
|
|
|
|
|
|
|
|
Gfx::ShaderPermutation permutation = graphics->getShaderCompiler()->getTemplate("RayTracing");
|
2025-01-30 23:25:41 +01:00
|
|
|
permutation.setMaterial(mat->getName(), mat->getProfile());
|
2024-07-17 10:40:40 +02:00
|
|
|
permutation.setVertexData(vertexData->getTypeName());
|
|
|
|
|
const Gfx::ShaderCollection* collection = graphics->getShaderCompiler()->findShaders(Gfx::PermutationId(permutation));
|
|
|
|
|
assert(collection != nullptr);
|
|
|
|
|
Gfx::RayTracingHitGroup callableGroup = {
|
|
|
|
|
.closestHitShader = collection->callableShader,
|
2025-01-30 23:25:41 +01:00
|
|
|
.anyHitShader = anyhit,
|
2024-07-17 10:40:40 +02:00
|
|
|
};
|
|
|
|
|
callableGroup.parameters.resize(sizeof(VertexData::DrawCallOffsets));
|
|
|
|
|
std::memcpy(callableGroup.parameters.data(), &transparentData.offsets, sizeof(VertexData::DrawCallOffsets));
|
|
|
|
|
callableGroups.add(callableGroup);
|
|
|
|
|
|
|
|
|
|
instanceData.add(transparentData.instanceData);
|
|
|
|
|
accelerationStructures.add(transparentData.rayTracingScene);
|
|
|
|
|
}
|
2024-07-10 21:07:10 +02:00
|
|
|
}
|
2024-07-15 17:55:22 +02:00
|
|
|
pipeline = graphics->createRayTracingPipeline(Gfx::RayTracingPipelineCreateInfo{
|
2025-03-30 22:17:29 +02:00
|
|
|
.pipelineLayout = pipelineLayout,
|
|
|
|
|
.rayGenGroup =
|
|
|
|
|
{
|
|
|
|
|
.shader = rayGen,
|
|
|
|
|
},
|
|
|
|
|
.hitGroups = callableGroups,
|
|
|
|
|
.missGroups =
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
.shader = miss,
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-07-15 17:55:22 +02:00
|
|
|
//.callableGroups = callableGroups,
|
|
|
|
|
});
|
2024-07-15 08:32:50 +02:00
|
|
|
tlas = graphics->createTopLevelAccelerationStructure(Gfx::TopLevelASCreateInfo{
|
2024-07-10 21:07:10 +02:00
|
|
|
.instances = instanceData,
|
|
|
|
|
.bottomLevelStructures = accelerationStructures,
|
|
|
|
|
});
|
2025-08-10 19:16:55 +02:00
|
|
|
Gfx::ODescriptorSet desc = paramsLayout->allocateDescriptorSet();
|
2025-03-07 21:48:27 +01:00
|
|
|
desc->updateAccelerationStructure(TLAS_NAME, 0, tlas);
|
2025-07-04 17:36:40 +02:00
|
|
|
desc->updateTexture(ACCUMULATOR_NAME, 0, radianceAccumulator->getDefaultView());
|
|
|
|
|
desc->updateTexture(TEXTURE_NAME, 0, texture->getDefaultView());
|
2025-03-07 21:48:27 +01:00
|
|
|
desc->updateBuffer(INDEXBUFFER_NAME, 0, StaticMeshVertexData::getInstance()->getIndexBuffer());
|
2025-07-04 17:36:40 +02:00
|
|
|
desc->updateTexture(SKYBOX_NAME, 0, skyBox->getDefaultView());
|
2025-03-07 21:48:27 +01:00
|
|
|
desc->updateSampler(SKYSAMPLER_NAME, 0, skyBoxSampler);
|
2024-07-10 21:07:10 +02:00
|
|
|
desc->writeChanges();
|
|
|
|
|
|
2024-07-15 17:55:22 +02:00
|
|
|
Gfx::ORenderCommand command = graphics->createRenderCommand("RayTracing");
|
2024-07-10 21:07:10 +02:00
|
|
|
command->bindPipeline(pipeline);
|
2024-07-12 13:33:52 +02:00
|
|
|
StaticMeshVertexData::getInstance()->getInstanceDataSet()->writeChanges();
|
|
|
|
|
StaticMeshVertexData::getInstance()->getVertexDataSet()->writeChanges();
|
2024-07-10 21:07:10 +02:00
|
|
|
command->bindDescriptor({viewParamsSet, StaticMeshVertexData::getInstance()->getInstanceDataSet(),
|
|
|
|
|
StaticMeshVertexData::getInstance()->getVertexDataSet(), Material::getDescriptorSet(),
|
2024-07-12 13:33:52 +02:00
|
|
|
scene->getLightEnvironment()->getDescriptorSet(), desc});
|
2024-12-25 14:59:08 +01:00
|
|
|
SampleParams sampleParams = {
|
|
|
|
|
.pass = 0,
|
|
|
|
|
.samplesPerPixel = 10000,
|
|
|
|
|
};
|
|
|
|
|
// for (uint32 i = 0; i < sampleParams.samplesPerPixel; ++i)
|
|
|
|
|
{
|
2025-01-12 11:26:52 +01:00
|
|
|
sampleParams.pass = pass;
|
2024-12-25 14:59:08 +01:00
|
|
|
command->pushConstants(Gfx::SE_SHADER_STAGE_RAYGEN_BIT_KHR, 0, sizeof(SampleParams), &sampleParams);
|
|
|
|
|
command->traceRays(texture->getWidth(), texture->getHeight(), 1);
|
|
|
|
|
radianceAccumulator->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR,
|
|
|
|
|
Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR);
|
|
|
|
|
texture->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR,
|
|
|
|
|
Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR);
|
|
|
|
|
}
|
2025-01-12 11:26:52 +01:00
|
|
|
pass++;
|
|
|
|
|
std::cout << pass << std::endl;
|
2024-07-10 21:07:10 +02:00
|
|
|
Array<Gfx::ORenderCommand> commands;
|
|
|
|
|
commands.add(std::move(command));
|
|
|
|
|
graphics->executeCommands(std::move(commands));
|
2025-05-06 19:36:43 +02:00
|
|
|
graphics->endDebugRegion();
|
2024-07-10 21:07:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RayTracingPass::endFrame() {}
|
|
|
|
|
|
|
|
|
|
void RayTracingPass::publishOutputs() {
|
2024-12-25 14:59:08 +01:00
|
|
|
radianceAccumulator = graphics->createTexture2D(TextureCreateInfo{
|
|
|
|
|
.format = Gfx::SE_FORMAT_R32G32B32A32_SFLOAT,
|
|
|
|
|
.width = viewport->getOwner()->getFramebufferWidth(),
|
|
|
|
|
.height = viewport->getOwner()->getFramebufferHeight(),
|
|
|
|
|
.usage = Gfx::SE_IMAGE_USAGE_STORAGE_BIT,
|
|
|
|
|
});
|
|
|
|
|
radianceAccumulator->changeLayout(Gfx::SE_IMAGE_LAYOUT_GENERAL, Gfx::SE_ACCESS_NONE, Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_SHADER_WRITE_BIT,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT | Gfx::SE_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR);
|
2024-07-10 21:07:10 +02:00
|
|
|
texture = graphics->createTexture2D(TextureCreateInfo{
|
|
|
|
|
.format = Gfx::SE_FORMAT_R32G32B32A32_SFLOAT,
|
|
|
|
|
.width = viewport->getOwner()->getFramebufferWidth(),
|
|
|
|
|
.height = viewport->getOwner()->getFramebufferHeight(),
|
|
|
|
|
.usage = Gfx::SE_IMAGE_USAGE_STORAGE_BIT,
|
|
|
|
|
});
|
2024-07-12 13:33:52 +02:00
|
|
|
texture->changeLayout(Gfx::SE_IMAGE_LAYOUT_GENERAL, Gfx::SE_ACCESS_NONE, Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
|
2024-07-16 15:32:51 +02:00
|
|
|
Gfx::SE_ACCESS_SHADER_WRITE_BIT,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT | Gfx::SE_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR);
|
2026-04-12 20:49:02 +02:00
|
|
|
resources->registerRenderPassOutput("BASEPASS_COLOR",
|
|
|
|
|
Gfx::RenderTargetAttachment(texture->getDefaultView(), Gfx::SE_IMAGE_LAYOUT_UNDEFINED,
|
|
|
|
|
Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL));
|
2024-07-10 21:07:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RayTracingPass::createRenderPass() {}
|