Files
Seele/src/Engine/Graphics/RenderPass/RayTracingPass.cpp
T

138 lines
6.9 KiB
C++
Raw Normal View History

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;
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,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
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-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,
});
}
void RayTracingPass::beginFrame(const Component::Camera& cam) { RenderPass::beginFrame(cam); }
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-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();
desc->updateAccelerationStructure(0, tlas);
desc->updateTexture(1, Gfx::PTexture2D(texture));
desc->updateBuffer(2, StaticMeshVertexData::getInstance()->getIndexBuffer());
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-07-10 21:07:10 +02:00
command->traceRays(texture->getWidth(), texture->getHeight(), 1);
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() {
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 = {
.name = "RT",
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() {}