diff --git a/res/shaders/raytracing/ClosestHit.slang b/res/shaders/raytracing/ClosestHit.slang index d5e43c4..a34afab 100644 --- a/res/shaders/raytracing/ClosestHit.slang +++ b/res/shaders/raytracing/ClosestHit.slang @@ -53,7 +53,6 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu // gamma correction result = result / (result + float3(1.0)); result = pow(result, float3(1.0/2.2)); - hitValue.color = result; } \ No newline at end of file diff --git a/src/Engine/Graphics/RenderPass/RayTracingPass.cpp b/src/Engine/Graphics/RenderPass/RayTracingPass.cpp index 47486f4..a141b7d 100644 --- a/src/Engine/Graphics/RenderPass/RayTracingPass.cpp +++ b/src/Engine/Graphics/RenderPass/RayTracingPass.cpp @@ -75,6 +75,24 @@ void RayTracingPass::render() { } } } + 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); + } } pipeline = graphics->createRayTracingPipeline(Gfx::RayTracingPipelineCreateInfo{ .pipelineLayout = pipelineLayout, .rayGenGroup = {.shader = rayGen}, .hitGroups = callableGroups, .missGroups = {{.shader = miss}}, diff --git a/src/Engine/Graphics/VertexData.cpp b/src/Engine/Graphics/VertexData.cpp index 6d98f75..1f6a454 100644 --- a/src/Engine/Graphics/VertexData.cpp +++ b/src/Engine/Graphics/VertexData.cpp @@ -20,6 +20,7 @@ void VertexData::resetMeshData() { instanceData.clear(); instanceMeshData.clear(); rayTracingScene.clear(); + transparentData.clear(); for (auto& mat : materialData) { for (auto& inst : mat.instances) { inst.instanceData.clear(); @@ -47,6 +48,8 @@ void VertexData::updateMesh(entt::entity id, uint32 meshIndex, PMesh mesh, Compo .inverseTransformMatrix = glm::inverse(transformMatrix), }; + auto [instanceId, meshletOffset] = getCullingMapping(id, meshIndex, data.numMeshlets); + referencedInstance->updateDescriptor(); if (mat->hasTransparency()) { auto params = referencedInstance->getMaterialOffsets(); @@ -55,15 +58,16 @@ void VertexData::updateMesh(entt::entity id, uint32 meshIndex, PMesh mesh, Compo .vertexData = this, .offsets = { - .instanceOffset = static_cast(instanceData.size()), + .instanceOffset = 0, .textureOffset = params.textureOffset, .samplerOffset = params.samplerOffset, .floatOffset = params.floatOffset, }, .worldPosition = Vector(inst.transformMatrix[3]), + .instanceData = inst, + .meshData = data, + .rayTracingScene = mesh->blas, }); - instanceData.add(inst); - instanceMeshData.add(data); return; } if (materialData.size() <= mat->getId()) { @@ -76,9 +80,6 @@ void VertexData::updateMesh(entt::entity id, uint32 meshIndex, PMesh mesh, Compo } BatchedDrawCall& matInstanceData = matData.instances[referencedInstance->getId()]; matInstanceData.materialInstance = referencedInstance; - - auto [instanceId, meshletOffset] = getCullingMapping(id, meshIndex, data.numMeshlets); - matInstanceData.rayTracingData.add(mesh->blas); matInstanceData.instanceData.add(inst); matInstanceData.instanceMeshData.add(data); @@ -146,6 +147,13 @@ void VertexData::createDescriptors() { } } } + for (uint32 i = 0; i < transparentData.size(); ++i) + { + transparentData[i].offsets.instanceOffset = instanceData.size(); + instanceData.add(transparentData[i].instanceData); + instanceMeshData.add(transparentData[i].meshData); + rayTracingScene.add(transparentData[i].rayTracingScene); + } cullingOffsetBuffer->rotateBuffer(cullingOffsets.size() * sizeof(uint32)); cullingOffsetBuffer->updateContents(ShaderBufferCreateInfo{ .sourceData = diff --git a/src/Engine/Graphics/VertexData.h b/src/Engine/Graphics/VertexData.h index 71f6fc5..0b5bb66 100644 --- a/src/Engine/Graphics/VertexData.h +++ b/src/Engine/Graphics/VertexData.h @@ -49,6 +49,9 @@ class VertexData { VertexData* vertexData; DrawCallOffsets offsets; Vector worldPosition; + InstanceData instanceData; + MeshData meshData; + Gfx::PBottomLevelAS rayTracingScene; }; void resetMeshData(); void updateMesh(entt::entity id, uint32 meshIndex, PMesh mesh, Component::Transform& transform); diff --git a/src/Engine/Graphics/Vulkan/RayTracing.cpp b/src/Engine/Graphics/Vulkan/RayTracing.cpp index 2348284..3b593ca 100644 --- a/src/Engine/Graphics/Vulkan/RayTracing.cpp +++ b/src/Engine/Graphics/Vulkan/RayTracing.cpp @@ -151,7 +151,7 @@ TopLevelAS::TopLevelAS(PGraphics graphics, const Gfx::TopLevelASCreateInfo& crea VmaAllocationCreateInfo{ .usage = VMA_MEMORY_USAGE_AUTO, }, - Gfx::QueueType::GRAPHICS); + Gfx::QueueType::GRAPHICS, graphics->getAccelerationProperties().minAccelerationStructureScratchOffsetAlignment); VkAccelerationStructureBuildGeometryInfoKHR buildGeometry = { .sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR, diff --git a/src/Engine/Window/GameView.cpp b/src/Engine/Window/GameView.cpp index e40c96b..5a8a0a1 100644 --- a/src/Engine/Window/GameView.cpp +++ b/src/Engine/Window/GameView.cpp @@ -23,12 +23,12 @@ using namespace Seele; GameView::GameView(Gfx::PGraphics graphics, PWindow window, const ViewportCreateInfo& createInfo, std::string dllPath) : View(graphics, window, createInfo, "Game"), scene(new Scene(graphics)), gameInterface(dllPath) { reloadGame(); - renderGraph.addPass(new CachedDepthPass(graphics, scene)); - renderGraph.addPass(new DepthCullingPass(graphics, scene)); - renderGraph.addPass(new VisibilityPass(graphics, scene)); - renderGraph.addPass(new LightCullingPass(graphics, scene)); - renderGraph.addPass(new BasePass(graphics, scene)); - //renderGraph.addPass(new RayTracingPass(graphics, scene)); + //renderGraph.addPass(new CachedDepthPass(graphics, scene)); + //renderGraph.addPass(new DepthCullingPass(graphics, scene)); + //renderGraph.addPass(new VisibilityPass(graphics, scene)); + //renderGraph.addPass(new LightCullingPass(graphics, scene)); + //renderGraph.addPass(new BasePass(graphics, scene)); + renderGraph.addPass(new RayTracingPass(graphics, scene)); renderGraph.setViewport(viewport); renderGraph.createRenderPass(); }