From d573f9e9073e155e368a12e25d62a9215e891223 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Mon, 1 Nov 2021 02:20:57 +0100 Subject: [PATCH] Create the pipeline --- src/lightmap/gpuraytracer.cpp | 43 +++++++++++++++++++++++++++++++++-- src/lightmap/gpuraytracer.h | 8 +++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/lightmap/gpuraytracer.cpp b/src/lightmap/gpuraytracer.cpp index e13c802..df93ca0 100644 --- a/src/lightmap/gpuraytracer.cpp +++ b/src/lightmap/gpuraytracer.cpp @@ -84,6 +84,9 @@ void GPURaytracer::Raytrace(LevelMesh* level) printf("Creating shaders\n"); CreateShaders(); + printf("Creating pipeline\n"); + CreatePipeline(); + cmdbuffer->end(); #if 0 @@ -325,12 +328,12 @@ void GPURaytracer::CreateVertexAndIndexBuffers() size_t indexoffset = vertexoffset + vertexbuffersize; BufferBuilder vbuilder; - vbuilder.setUsage(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR); + vbuilder.setUsage(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT); vbuilder.setSize(vertexbuffersize); vertexBuffer = vbuilder.create(device.get()); BufferBuilder ibuilder; - ibuilder.setUsage(VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR); + ibuilder.setUsage(VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT); ibuilder.setSize(indexbuffersize); indexBuffer = ibuilder.create(device.get()); @@ -611,3 +614,39 @@ void GPURaytracer::CreateShaders() shaderClosestHit = builder.create(device.get()); } } + +void GPURaytracer::CreatePipeline() +{ + DescriptorSetLayoutBuilder setbuilder; + setbuilder.addBinding(0, VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, 1, VK_SHADER_STAGE_RAYGEN_BIT_KHR); + setbuilder.addBinding(1, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_RAYGEN_BIT_KHR); + setbuilder.addBinding(2, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_RAYGEN_BIT_KHR); + descriptorSetLayout = setbuilder.create(device.get()); + + PipelineLayoutBuilder layoutbuilder; + layoutbuilder.addSetLayout(descriptorSetLayout.get()); + pipelineLayout = layoutbuilder.create(device.get()); + + RayTracingPipelineBuilder builder; + builder.setLayout(pipelineLayout.get()); + builder.setMaxPipelineRayRecursionDepth(1); + builder.addShader(VK_SHADER_STAGE_RAYGEN_BIT_KHR, shaderRayGen.get()); + builder.addShader(VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR, shaderClosestHit.get()); + builder.addShader(VK_SHADER_STAGE_MISS_BIT_KHR, shaderMiss.get()); + pipeline = builder.create(device.get()); + + BufferBuilder bufbuilder; + bufbuilder.setUsage(VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR | VK_BUFFER_USAGE_TRANSFER_DST_BIT); + bufbuilder.setSize(pipeline->shaderGroupHandles.size()); + shaderBindingTable = bufbuilder.create(device.get()); + + BufferBuilder tbuilder; + tbuilder.setUsage(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_ONLY); + tbuilder.setSize(pipeline->shaderGroupHandles.size()); + sbtTransferBuffer = tbuilder.create(device.get()); + auto data = sbtTransferBuffer->Map(0, pipeline->shaderGroupHandles.size()); + memcpy(data, pipeline->shaderGroupHandles.data(), pipeline->shaderGroupHandles.size()); + sbtTransferBuffer->Unmap(); + + cmdbuffer->copyBuffer(sbtTransferBuffer.get(), shaderBindingTable.get()); +} diff --git a/src/lightmap/gpuraytracer.h b/src/lightmap/gpuraytracer.h index dad952c..a6b3f4a 100644 --- a/src/lightmap/gpuraytracer.h +++ b/src/lightmap/gpuraytracer.h @@ -19,6 +19,7 @@ private: void CreateBottomLevelAccelerationStructure(); void CreateTopLevelAccelerationStructure(); void CreateShaders(); + void CreatePipeline(); void RaytraceProbeSample(LightProbeSample* probe); void RaytraceSurfaceSample(Surface* surface, int x, int y); @@ -55,6 +56,13 @@ private: std::unique_ptr shaderMiss; std::unique_ptr shaderClosestHit; + std::unique_ptr descriptorSetLayout; + + std::unique_ptr pipelineLayout; + std::unique_ptr pipeline; + std::unique_ptr shaderBindingTable; + std::unique_ptr sbtTransferBuffer; + std::unique_ptr cmdpool; std::unique_ptr cmdbuffer; };