Fixing SBT alignment

This commit is contained in:
mklefrancois 2020-05-27 16:50:02 +02:00
parent ccdc90f35c
commit 41d6bbeea4
2 changed files with 48 additions and 29 deletions

View file

@ -218,9 +218,10 @@ The size of each group can be described as follows:
~~~~ C++
// Sizes
uint32_t rayGenSize = groupHandleSize;
uint32_t missSize = groupHandleSize;
uint32_t hitSize = groupHandleSize + sizeof(HitRecordBuffer);
uint32_t rayGenSize = baseAlignment;
uint32_t missSize = baseAlignment;
uint32_t hitSize =
ROUND_UP(groupHandleSize + static_cast<int>(sizeof(HitRecordBuffer)), baseAlignment);
uint32_t newSbtSize = rayGenSize + 2 * missSize + 2 * hitSize;
~~~~
@ -238,14 +239,16 @@ Then write the new SBT like this, where only Hit 1 has extra data.
memcpy(pBuffer, handles[2], groupHandleSize); // Miss 1
pBuffer += missSize;
memcpy(pBuffer, handles[3], groupHandleSize); // Hit 0
pBuffer += groupHandleSize;
pBuffer += sizeof(HitRecordBuffer); // No data
uint8_t* pHitBuffer = pBuffer;
memcpy(pHitBuffer, handles[3], groupHandleSize); // Hit 0
// No data
pBuffer += hitSize;
memcpy(pBuffer, handles[4], groupHandleSize); // Hit 1
pBuffer += groupHandleSize;
memcpy(pBuffer, &m_hitShaderRecord[0], sizeof(HitRecordBuffer)); // Hit 1 data
pBuffer += sizeof(HitRecordBuffer);
pHitBuffer = pBuffer;
memcpy(pHitBuffer, handles[4], groupHandleSize); // Hit 1
pHitBuffer += groupHandleSize;
memcpy(pHitBuffer, &m_hitShaderRecord[0], sizeof(HitRecordBuffer)); // Hit 1 data
pBuffer += hitSize;
}
~~~~
@ -255,12 +258,21 @@ Then change the call to `m_alloc.createBuffer` to create the SBT buffer from `sb
m_rtSBTBuffer = m_alloc.createBuffer(cmdBuf, sbtBuffer, vk::BufferUsageFlagBits::eRayTracingKHR);
~~~~
Note: we are using this `define` for rounding up to the correct alignment
~~~~ C++
#ifndef ROUND_UP
#define ROUND_UP(v, powerOf2Alignment) (((v) + (powerOf2Alignment)-1) & ~((powerOf2Alignment)-1))
#endif
~~~~
## `raytrace`
Finally, since the size of the hit group is now larger than just the handle, we need to set the new value of the hit group stride in `HelloVulkan::raytrace`.
~~~~ C++
vk::DeviceSize hitGroupStride = progSize + sizeof(HitRecordBuffer);
vk::DeviceSize hitGroupStride =
ROUND_UP(m_rtProperties.shaderGroupHandleSize + sizeof(HitRecordBuffer), progOffset);
~~~~
!!! Note:
@ -316,10 +328,11 @@ The size of the SBT will now account for its 3 hit groups:
Finally, we need to add the new entry as well at the end of the buffer, reusing the handle of the second Hit Group and setting a different color.
~~~~ C++
memcpy(pBuffer, handles[4], groupHandleSize); // Hit 2
pBuffer += groupHandleSize;
memcpy(pBuffer, &m_hitShaderRecord[1], sizeof(HitRecordBuffer)); // Hit 2 data
pBuffer += sizeof(HitRecordBuffer);
pHitBuffer = pBuffer;
memcpy(pHitBuffer, handles[4], groupHandleSize); // Hit 2
pHitBuffer += groupHandleSize;
memcpy(pHitBuffer, &m_hitShaderRecord[1], sizeof(HitRecordBuffer)); // Hit 2 data
pBuffer += hitSize;
~~~~
!!! Warning