2024-07-10 21:07:10 +02:00
|
|
|
#include "RayTracingPass.h"
|
2024-07-15 08:32:50 +02:00
|
|
|
#include "Asset/AssetRegistry.h"
|
|
|
|
|
#include "Asset/MeshAsset.h"
|
|
|
|
|
#include "Graphics/Mesh.h"
|
2024-07-10 21:07:10 +02:00
|
|
|
#include "Graphics/RayTracing.h"
|
|
|
|
|
#include "Graphics/Shader.h"
|
|
|
|
|
#include "Graphics/StaticMeshVertexData.h"
|
|
|
|
|
#include "RenderPass.h"
|
|
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
|
2024-12-25 14:59:08 +01:00
|
|
|
struct SampleParams {
|
|
|
|
|
uint32 pass;
|
|
|
|
|
uint32 samplesPerPixel;
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-10 21:07:10 +02:00
|
|
|
RayTracingPass::RayTracingPass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics, scene) {
|
|
|
|
|
paramsLayout = graphics->createDescriptorLayout("pRayTracingParams");
|
|
|
|
|
paramsLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
|
|
|
|
.binding = 0,
|
|
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR,
|
|
|
|
|
});
|
|
|
|
|
paramsLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
|
|
|
|
.binding = 1,
|
|
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE,
|
|
|
|
|
});
|
|
|
|
|
paramsLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
|
|
|
|
.binding = 2,
|
2024-12-25 14:59:08 +01:00
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE,
|
|
|
|
|
});
|
|
|
|
|
paramsLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
|
|
|
|
.binding = 3,
|
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{
|
|
|
|
|
.binding = 4,
|
|
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
|
|
|
|
|
});
|
|
|
|
|
paramsLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
|
|
|
|
.binding = 5,
|
|
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER,
|
|
|
|
|
});
|
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),
|
|
|
|
|
});
|
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,
|
|
|
|
|
});
|
2024-12-25 14:59:08 +01:00
|
|
|
skyBox = AssetRegistry::findTexture("", "skybox")->getTexture().cast<Gfx::TextureCube>();
|
|
|
|
|
skyBoxSampler = graphics->createSampler({});
|
2024-07-10 21:07:10 +02:00
|
|
|
}
|
|
|
|
|
|
2024-12-25 14:59:08 +01:00
|
|
|
static uint32 i = 0;
|
|
|
|
|
static Component::Camera lastCam;
|
|
|
|
|
void RayTracingPass::beginFrame(const Component::Camera& cam) {
|
|
|
|
|
RenderPass::beginFrame(cam);
|
|
|
|
|
if (lastCam.getCameraPosition() != cam.getCameraPosition() || lastCam.getCameraForward() != cam.getCameraForward()) {
|
|
|
|
|
lastCam = cam;
|
|
|
|
|
i = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-10 21:07:10 +02:00
|
|
|
|
|
|
|
|
void RayTracingPass::render() {
|
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");
|
|
|
|
|
permutation.setMaterial(mat->getName());
|
|
|
|
|
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,
|
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");
|
|
|
|
|
permutation.setMaterial(mat->getName());
|
|
|
|
|
permutation.setVertexData(vertexData->getTypeName());
|
|
|
|
|
const Gfx::ShaderCollection* collection = graphics->getShaderCompiler()->findShaders(Gfx::PermutationId(permutation));
|
|
|
|
|
assert(collection != nullptr);
|
|
|
|
|
Gfx::RayTracingHitGroup callableGroup = {
|
|
|
|
|
.closestHitShader = collection->callableShader,
|
|
|
|
|
};
|
|
|
|
|
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{
|
2024-07-16 15:32:51 +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,
|
|
|
|
|
});
|
|
|
|
|
Gfx::PDescriptorSet desc = paramsLayout->allocateDescriptorSet();
|
2024-09-27 15:57:37 +02:00
|
|
|
desc->updateAccelerationStructure(0, 0, tlas);
|
2024-12-25 14:59:08 +01:00
|
|
|
desc->updateTexture(1, 0, radianceAccumulator);
|
|
|
|
|
desc->updateTexture(2, 0, texture);
|
|
|
|
|
desc->updateBuffer(3, 0, StaticMeshVertexData::getInstance()->getIndexBuffer());
|
|
|
|
|
desc->updateTexture(4, 0, skyBox);
|
|
|
|
|
desc->updateSampler(5, 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)
|
|
|
|
|
{
|
|
|
|
|
sampleParams.pass = i;
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
std::cout << i << std::endl;
|
2024-07-13 09:25:42 +02:00
|
|
|
texture->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR,
|
|
|
|
|
Gfx::SE_ACCESS_TRANSFER_READ_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT);
|
2024-07-10 21:07:10 +02:00
|
|
|
Array<Gfx::ORenderCommand> commands;
|
|
|
|
|
commands.add(std::move(command));
|
|
|
|
|
graphics->executeCommands(std::move(commands));
|
2024-07-12 13:33:52 +02:00
|
|
|
viewport->getOwner()->getBackBuffer()->changeLayout(Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, Gfx::SE_ACCESS_NONE,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, Gfx::SE_ACCESS_TRANSFER_WRITE_BIT,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT);
|
|
|
|
|
graphics->copyTexture(Gfx::PTexture2D(texture), viewport->getOwner()->getBackBuffer());
|
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);
|
2024-07-12 13:33:52 +02:00
|
|
|
ShaderCompilationInfo compileInfo = {
|
2024-12-25 14:59:08 +01:00
|
|
|
.name = "RayGenMiss",
|
2024-07-15 17:55:22 +02:00
|
|
|
.modules = {"RayGen", "Miss"},
|
|
|
|
|
.entryPoints = {{"raygen", "RayGen"}, {"miss", "Miss"}},
|
2024-07-12 13:33:52 +02:00
|
|
|
.defines = {{"RAY_TRACING", "1"}},
|
|
|
|
|
.rootSignature = pipelineLayout,
|
|
|
|
|
};
|
|
|
|
|
graphics->beginShaderCompilation(compileInfo);
|
|
|
|
|
rayGen = graphics->createRayGenShader({0});
|
2024-07-15 17:55:22 +02:00
|
|
|
miss = graphics->createMissShader({1});
|
2024-07-12 13:33:52 +02:00
|
|
|
pipelineLayout->create();
|
2024-07-10 21:07:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RayTracingPass::createRenderPass() {}
|