RT Shading works

This commit is contained in:
Dynamitos
2024-07-16 15:32:51 +02:00
parent f943407b26
commit 60e74f87b8
14 changed files with 60 additions and 51 deletions
@@ -77,10 +77,7 @@ void RayTracingPass::render() {
}
}
pipeline = graphics->createRayTracingPipeline(Gfx::RayTracingPipelineCreateInfo{
.pipelineLayout = pipelineLayout,
.rayGenGroup = {.shader = rayGen},
.hitGroups = callableGroups,
.missGroups = {{.shader = miss}},
.pipelineLayout = pipelineLayout, .rayGenGroup = {.shader = rayGen}, .hitGroups = callableGroups, .missGroups = {{.shader = miss}},
//.callableGroups = callableGroups,
});
tlas = graphics->createTopLevelAccelerationStructure(Gfx::TopLevelASCreateInfo{
@@ -122,7 +119,8 @@ void RayTracingPass::publishOutputs() {
.usage = Gfx::SE_IMAGE_USAGE_STORAGE_BIT,
});
texture->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_ACCESS_SHADER_WRITE_BIT,
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT | Gfx::SE_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR);
ShaderCompilationInfo compileInfo = {
.name = "RT",
.modules = {"RayGen", "Miss"},
@@ -39,9 +39,10 @@ void RenderPass::beginFrame(const Component::Camera& cam) {
viewParamsBuffer->rotateBuffer(sizeof(ViewParameter));
viewParamsBuffer->updateContents(uniformUpdate);
viewParamsBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
Gfx::SE_ACCESS_UNIFORM_READ_BIT,
Gfx::SE_ACCESS_UNIFORM_READ_BIT | Gfx::SE_ACCESS_TRANSFER_WRITE_BIT,
Gfx::SE_PIPELINE_STAGE_TASK_SHADER_BIT_EXT | Gfx::SE_PIPELINE_STAGE_MESH_SHADER_BIT_EXT |
Gfx::SE_PIPELINE_STAGE_VERTEX_SHADER_BIT | Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
Gfx::SE_PIPELINE_STAGE_VERTEX_SHADER_BIT | Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT |
Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT);
viewParamsLayout->reset();
viewParamsSet = viewParamsLayout->allocateDescriptorSet();
viewParamsSet->updateBuffer(0, viewParamsBuffer);
+2
View File
@@ -117,6 +117,8 @@ void BufferAllocation::updateContents(uint64 regionOffset, uint64 regionSize, vo
.size = regionSize,
};
vkCmdCopyBuffer(cmd->getHandle(), staging->buffer, buffer, 1, &copy);
pipelineBarrier(VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT);
cmd->bindResource(PBufferAllocation(this));
cmd->bindResource(PBufferAllocation(staging));
+3
View File
@@ -235,6 +235,9 @@ void RenderCommand::bindPipeline(Gfx::PRayTracingPipeline gfxPipeline) {
assert(threadId == std::this_thread::get_id());
rtPipeline = gfxPipeline.cast<RayTracingPipeline>();
rtPipeline->bind(handle);
boundResources.add(PBufferAllocation(rtPipeline->rayGen));
boundResources.add(PBufferAllocation(rtPipeline->hit));
boundResources.add(PBufferAllocation(rtPipeline->miss));
}
void RenderCommand::bindDescriptor(Gfx::PDescriptorSet descriptorSet, Array<uint32> dynamicOffsets) {
+4 -2
View File
@@ -654,6 +654,8 @@ void Graphics::initInstance(GraphicsInitializer initInfo) {
#ifdef __APPLE__
extensions.add("VK_KHR_portability_enumeration");
#endif
Array<const char*> layers = initInfo.layers;
layers.add("VK_LAYER_KHRONOS_validation");
VkInstanceCreateInfo info = {
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
.pNext = nullptr,
@@ -661,8 +663,8 @@ void Graphics::initInstance(GraphicsInitializer initInfo) {
.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR,
#endif
.pApplicationInfo = &appInfo,
.enabledLayerCount = (uint32)initInfo.layers.size(),
.ppEnabledLayerNames = initInfo.layers.data(),
.enabledLayerCount = (uint32)layers.size(),
.ppEnabledLayerNames = layers.data(),
.enabledExtensionCount = (uint32)extensions.size(),
.ppEnabledExtensionNames = extensions.data(),
};
@@ -478,6 +478,7 @@ PRayTracingPipeline PipelineCache::createPipeline(Gfx::RayTracingPipelineCreateI
.stage = VK_SHADER_STAGE_RAYGEN_BIT_KHR,
.module = rayGen->getModuleHandle(),
.pName = rayGen->getEntryPointName(),
.pSpecializationInfo = nullptr,
});
shaderGroups.add(VkRayTracingShaderGroupCreateInfoKHR{
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
@@ -487,6 +488,7 @@ PRayTracingPipeline PipelineCache::createPipeline(Gfx::RayTracingPipelineCreateI
.closestHitShader = VK_SHADER_UNUSED_KHR,
.anyHitShader = VK_SHADER_UNUSED_KHR,
.intersectionShader = VK_SHADER_UNUSED_KHR,
.pShaderGroupCaptureReplayHandle = nullptr,
});
}
{
@@ -499,6 +501,7 @@ PRayTracingPipeline PipelineCache::createPipeline(Gfx::RayTracingPipelineCreateI
.stage = VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR,
.module = hit->getModuleHandle(),
.pName = hit->getEntryPointName(),
.pSpecializationInfo = nullptr,
});
uint32 hitIndex = static_cast<uint32>(shaderStages.size() - 1);
uint32 anyHitIndex = VK_SHADER_UNUSED_KHR;
@@ -512,6 +515,7 @@ PRayTracingPipeline PipelineCache::createPipeline(Gfx::RayTracingPipelineCreateI
.stage = VK_SHADER_STAGE_ANY_HIT_BIT_KHR,
.module = anyHit->getModuleHandle(),
.pName = anyHit->getEntryPointName(),
.pSpecializationInfo = nullptr,
});
}
if (hitgroup.intersectionShader != nullptr) {
@@ -523,6 +527,7 @@ PRayTracingPipeline PipelineCache::createPipeline(Gfx::RayTracingPipelineCreateI
.stage = VK_SHADER_STAGE_INTERSECTION_BIT_KHR,
.module = intersect->getModuleHandle(),
.pName = intersect->getEntryPointName(),
.pSpecializationInfo = nullptr,
});
}
shaderGroups.add(VkRayTracingShaderGroupCreateInfoKHR{
@@ -533,6 +538,7 @@ PRayTracingPipeline PipelineCache::createPipeline(Gfx::RayTracingPipelineCreateI
.closestHitShader = hitIndex,
.anyHitShader = anyHitIndex,
.intersectionShader = intersectionIndex,
.pShaderGroupCaptureReplayHandle = nullptr,
});
}
}
@@ -546,6 +552,7 @@ PRayTracingPipeline PipelineCache::createPipeline(Gfx::RayTracingPipelineCreateI
.stage = VK_SHADER_STAGE_MISS_BIT_KHR,
.module = miss->getModuleHandle(),
.pName = miss->getEntryPointName(),
.pSpecializationInfo = nullptr,
});
shaderGroups.add(VkRayTracingShaderGroupCreateInfoKHR{
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
@@ -555,6 +562,7 @@ PRayTracingPipeline PipelineCache::createPipeline(Gfx::RayTracingPipelineCreateI
.closestHitShader = VK_SHADER_UNUSED_KHR,
.anyHitShader = VK_SHADER_UNUSED_KHR,
.intersectionShader = VK_SHADER_UNUSED_KHR,
.pShaderGroupCaptureReplayHandle = nullptr,
});
}
}
@@ -568,6 +576,7 @@ PRayTracingPipeline PipelineCache::createPipeline(Gfx::RayTracingPipelineCreateI
.stage = VK_SHADER_STAGE_CALLABLE_BIT_KHR,
.module = call->getModuleHandle(),
.pName = call->getEntryPointName(),
.pSpecializationInfo = nullptr,
});
shaderGroups.add(VkRayTracingShaderGroupCreateInfoKHR{
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
@@ -577,11 +586,15 @@ PRayTracingPipeline PipelineCache::createPipeline(Gfx::RayTracingPipelineCreateI
.closestHitShader = VK_SHADER_UNUSED_KHR,
.anyHitShader = VK_SHADER_UNUSED_KHR,
.intersectionShader = VK_SHADER_UNUSED_KHR,
.pShaderGroupCaptureReplayHandle = nullptr,
});
}
}
uint32 hash = CRC::Calculate(shaderStages.data(), sizeof(VkPipelineShaderStageCreateInfo) * shaderStages.size(), CRC::CRC_32());
hash = CRC::Calculate(shaderGroups.data(), sizeof(VkRayTracingShaderGroupCreateInfoKHR) * shaderGroups.size(), CRC::CRC_32(), hash);
if (rayTracingPipelines.contains(hash)) {
return rayTracingPipelines[hash];
}
VkRayTracingPipelineCreateInfoKHR pipelineInfo = {
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR,
.pNext = nullptr,
@@ -593,7 +606,11 @@ PRayTracingPipeline PipelineCache::createPipeline(Gfx::RayTracingPipelineCreateI
.layout = createInfo.pipelineLayout.cast<PipelineLayout>()->getHandle(),
};
VkPipeline pipelineHandle;
auto beginTime = std::chrono::high_resolution_clock::now();
VK_CHECK(vkCreateRayTracingPipelinesKHR(graphics->getDevice(), VK_NULL_HANDLE, cache, 1, &pipelineInfo, nullptr, &pipelineHandle));
auto endTime = std::chrono::high_resolution_clock::now();
int64 delta = std::chrono::duration_cast<std::chrono::microseconds>(endTime - beginTime).count();
std::cout << "RT creation time: " << delta << std::endl;
const uint32_t handleSize = graphics->getRayTracingProperties().shaderGroupHandleSize;
const uint32_t handleSizeAligned =
+6 -1
View File
@@ -220,4 +220,9 @@ RayTracingPipeline::~RayTracingPipeline() {
graphics->getDestructionManager()->queueResourceForDestruction(std::move(callable));
}
void RayTracingPipeline::bind(VkCommandBuffer handle) { vkCmdBindPipeline(handle, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, pipeline); }
void RayTracingPipeline::bind(VkCommandBuffer handle) {
vkCmdBindPipeline(handle, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, pipeline);
rayGen->bind();
hit->bind();
miss->bind();
}
+1
View File
@@ -101,6 +101,7 @@ class RayTracingPipeline : public Gfx::RayTracingPipeline {
uint64 missStride;
OBufferAllocation callable;
uint64 callableStride;
friend class RenderCommand;
};
DEFINE_REF(RayTracingPipeline)
} // namespace Vulkan