SpiecsEngine
 
Loading...
Searching...
No Matches

◆ CreateRTShaderBindingTable()

void Spices::VulkanRayTracing::CreateRTShaderBindingTable ( uint32_t rgenCount,
uint32_t missCount,
VkPipeline pipeline )

Create Shader Binding Table.

Parameters
[in]rgenCountRay Generation shader count in raytracing material.
[in]missCountRay Missing shader count in raytracing material.
[in]pipelineRayTracing Pipeline.

The SBT(buffer) need to have starting groups to be aligned and handles in the group to be aligned.

Get the shader group handles.

Allocate a buffer for storing the SBT.

Helper to retrieve the handle data.

Map the SBT buffer and write in the handles.

Ray Generation.

Miss.

Closest Hit.

The SBT(buffer) need to have starting groups to be aligned and handles in the group to be aligned.

Get the shader group handles.

Allocate a buffer for storing the SBT.

Helper to retrieve the handle data.

Map the SBT buffer and write in the handles.

Ray Generation.

Miss.

Closest Hit.

Definition at line 360 of file VulkanRayTracing.cpp.

361 {
363
364 const uint32_t hitCount = static_cast<uint32_t>(m_HitGroups->size());
365
366 const auto handleCount = rgenCount + missCount + hitCount;
367 const uint32_t handleSize = VulkanDevice::GetRTPipelineProperties().shaderGroupHandleSize;
368
372 const uint32_t handleSizeAligned = MemoryLibrary::align_up(handleSize, VulkanDevice::GetRTPipelineProperties().shaderGroupHandleAlignment);
373
374 m_RgenRegion.stride = MemoryLibrary::align_up(handleSizeAligned, VulkanDevice::GetRTPipelineProperties().shaderGroupBaseAlignment);
375 m_RgenRegion.size = m_RgenRegion.stride; // The size member of pRayGenShaderBindingTable must be equal to its stride member
376
377 m_MissRegion.stride = handleSizeAligned;
378 m_MissRegion.size = MemoryLibrary::align_up(missCount * handleSizeAligned, VulkanDevice::GetRTPipelineProperties().shaderGroupBaseAlignment);
379
380 m_HitRegion.stride = handleSizeAligned;
381 m_HitRegion.size = MemoryLibrary::align_up(hitCount * handleSizeAligned, VulkanDevice::GetRTPipelineProperties().shaderGroupBaseAlignment);
382
386 const uint32_t dataSize = handleCount * handleSize;
387 std::vector<uint8_t> handles(dataSize);
389 .m_VkFunc
390 .vkGetRayTracingShaderGroupHandlesKHR(
392 pipeline ,
393 0 ,
394 handleCount ,
395 dataSize ,
396 handles.data()
397 ))
398
399
402 VkDeviceSize sbtSize = m_RgenRegion.size + m_MissRegion.size + m_HitRegion.size + m_CallRegion.size;
403
404 m_RTSBTBuffer = std::make_unique<VulkanBuffer>(
406 "SBTBuffer" ,
407 sbtSize ,
408 VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
409 VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT |
410 VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR ,
411 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
412 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
413 );
414
415 DEBUGUTILS_SETOBJECTNAME(VK_OBJECT_TYPE_BUFFER, (uint64_t)m_RTSBTBuffer->Get(), m_VulkanState.m_Device, "SBT Buffer")
416
417 m_RgenRegion.deviceAddress = m_RTSBTBuffer->GetAddress();
418 m_MissRegion.deviceAddress = m_RTSBTBuffer->GetAddress() + m_RgenRegion.size;
419 m_HitRegion.deviceAddress = m_RTSBTBuffer->GetAddress() + m_RgenRegion.size + m_MissRegion.size;
420
424 auto getHandle = [&](int i) { return handles.data() + i * handleSize; };
425
429 uint64_t offset = 0;
430 uint32_t handleIdx = 0 ;
431
435 for (uint32_t c = 0; c < rgenCount; c++)
436 {
437 m_RTSBTBuffer->WriteToBuffer(getHandle(handleIdx++), handleSize, offset);
438 m_RgenRegion.stride;
439 }
440
444 offset = m_RgenRegion.size;
445 for (uint32_t c = 0; c < missCount; c++)
446 {
447 m_RTSBTBuffer->WriteToBuffer(getHandle(handleIdx++), handleSize, offset);
448 offset += m_MissRegion.stride;
449 }
450
454 offset = m_RgenRegion.size + m_MissRegion.size;
455 for (uint32_t c = 0; c < hitCount; c++)
456 {
457 m_RTSBTBuffer->WriteToBuffer(getHandle(handleIdx++), handleSize, offset);
458 offset += m_HitRegion.stride;
459 }
460 }
#define SPICES_PROFILE_ZONE
#define VK_CHECK(expr)
Vulkan Check macro. Verify Vulkan API Effectiveness.
Definition VulkanUtils.h:68
static constexpr integral align_up(integral x, size_t a) noexcept
Align up a memory size aligned with specific value.
static VkPhysicalDeviceRayTracingPipelinePropertiesKHR & GetRTPipelineProperties()
Get RayTracingPipelineProperties.
VulkanState & m_VulkanState
The global VulkanState Referenced from VulkanRenderBackend.
VkStridedDeviceAddressRegionKHR m_MissRegion
Ray Missing Region.
std::unique_ptr< VulkanBuffer > m_RTSBTBuffer
Shader Binding Table Buffer.
std::shared_ptr< std::unordered_map< std::string, uint32_t > > m_HitGroups
Scene ray hit shader groups.
VkStridedDeviceAddressRegionKHR m_HitRegion
Ray Hit Region.
VkStridedDeviceAddressRegionKHR m_CallRegion
Ray Callable Region.
VkStridedDeviceAddressRegionKHR m_RgenRegion
Ray Generation Region.
STL namespace.