Merge pull request #626 from VZout/master

Cleaned up the createShaderBindingTable for the raytracing examples
This commit is contained in:
Sascha Willems 2020-04-23 07:45:26 +02:00 committed by GitHub
commit cb50a95547
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 9 deletions

View file

@ -44,6 +44,8 @@ struct GeometryInstance {
#define INDEX_MISS 1 #define INDEX_MISS 1
#define INDEX_CLOSEST_HIT 2 #define INDEX_CLOSEST_HIT 2
#define NUM_SHADER_GROUPS 3
class VulkanExample : public VulkanExampleBase class VulkanExample : public VulkanExampleBase
{ {
public: public:
@ -414,7 +416,6 @@ public:
VkDeviceSize copyShaderIdentifier(uint8_t* data, const uint8_t* shaderHandleStorage, uint32_t groupIndex) { VkDeviceSize copyShaderIdentifier(uint8_t* data, const uint8_t* shaderHandleStorage, uint32_t groupIndex) {
const uint32_t shaderGroupHandleSize = rayTracingProperties.shaderGroupHandleSize; const uint32_t shaderGroupHandleSize = rayTracingProperties.shaderGroupHandleSize;
memcpy(data, shaderHandleStorage + groupIndex * shaderGroupHandleSize, shaderGroupHandleSize); memcpy(data, shaderHandleStorage + groupIndex * shaderGroupHandleSize, shaderGroupHandleSize);
data += shaderGroupHandleSize;
return shaderGroupHandleSize; return shaderGroupHandleSize;
} }
@ -423,7 +424,7 @@ public:
*/ */
void createShaderBindingTable() { void createShaderBindingTable() {
// Create buffer for the shader binding table // Create buffer for the shader binding table
const uint32_t sbtSize = rayTracingProperties.shaderGroupHandleSize * 3; const uint32_t sbtSize = rayTracingProperties.shaderGroupHandleSize * NUM_SHADER_GROUPS;
VK_CHECK_RESULT(vulkanDevice->createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_RAY_TRACING_BIT_NV, VK_BUFFER_USAGE_RAY_TRACING_BIT_NV,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
@ -433,10 +434,9 @@ public:
auto shaderHandleStorage = new uint8_t[sbtSize]; auto shaderHandleStorage = new uint8_t[sbtSize];
// Get shader identifiers // Get shader identifiers
VK_CHECK_RESULT(vkGetRayTracingShaderGroupHandlesNV(device, pipeline, 0, 3, sbtSize, shaderHandleStorage)); VK_CHECK_RESULT(vkGetRayTracingShaderGroupHandlesNV(device, pipeline, 0, NUM_SHADER_GROUPS, sbtSize, shaderHandleStorage));
auto* data = static_cast<uint8_t*>(shaderBindingTable.mapped); auto* data = static_cast<uint8_t*>(shaderBindingTable.mapped);
// Copy the shader identifiers to the shader binding table // Copy the shader identifiers to the shader binding table
VkDeviceSize offset = 0;
data += copyShaderIdentifier(data, shaderHandleStorage, INDEX_RAYGEN); data += copyShaderIdentifier(data, shaderHandleStorage, INDEX_RAYGEN);
data += copyShaderIdentifier(data, shaderHandleStorage, INDEX_MISS); data += copyShaderIdentifier(data, shaderHandleStorage, INDEX_MISS);
data += copyShaderIdentifier(data, shaderHandleStorage, INDEX_CLOSEST_HIT); data += copyShaderIdentifier(data, shaderHandleStorage, INDEX_CLOSEST_HIT);
@ -542,7 +542,7 @@ public:
/* /*
Setup ray tracing shader groups Setup ray tracing shader groups
*/ */
std::array<VkRayTracingShaderGroupCreateInfoNV, 3> groups{}; std::array<VkRayTracingShaderGroupCreateInfoNV, NUM_SHADER_GROUPS> groups{};
for (auto& group : groups) { for (auto& group : groups) {
// Init all groups with some default values // Init all groups with some default values
group.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_NV; group.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_NV;

View file

@ -403,7 +403,6 @@ public:
VkDeviceSize copyShaderIdentifier(uint8_t* data, const uint8_t* shaderHandleStorage, uint32_t groupIndex) { VkDeviceSize copyShaderIdentifier(uint8_t* data, const uint8_t* shaderHandleStorage, uint32_t groupIndex) {
const uint32_t shaderGroupHandleSize = rayTracingProperties.shaderGroupHandleSize; const uint32_t shaderGroupHandleSize = rayTracingProperties.shaderGroupHandleSize;
memcpy(data, shaderHandleStorage + groupIndex * shaderGroupHandleSize, shaderGroupHandleSize); memcpy(data, shaderHandleStorage + groupIndex * shaderGroupHandleSize, shaderGroupHandleSize);
data += shaderGroupHandleSize;
return shaderGroupHandleSize; return shaderGroupHandleSize;
} }
@ -425,7 +424,6 @@ public:
VK_CHECK_RESULT(vkGetRayTracingShaderGroupHandlesNV(device, pipeline, 0, NUM_SHADER_GROUPS, sbtSize, shaderHandleStorage)); VK_CHECK_RESULT(vkGetRayTracingShaderGroupHandlesNV(device, pipeline, 0, NUM_SHADER_GROUPS, sbtSize, shaderHandleStorage));
auto* data = static_cast<uint8_t*>(shaderBindingTable.mapped); auto* data = static_cast<uint8_t*>(shaderBindingTable.mapped);
// Copy the shader identifiers to the shader binding table // Copy the shader identifiers to the shader binding table
VkDeviceSize offset = 0;
data += copyShaderIdentifier(data, shaderHandleStorage, INDEX_RAYGEN); data += copyShaderIdentifier(data, shaderHandleStorage, INDEX_RAYGEN);
data += copyShaderIdentifier(data, shaderHandleStorage, INDEX_MISS); data += copyShaderIdentifier(data, shaderHandleStorage, INDEX_MISS);
data += copyShaderIdentifier(data, shaderHandleStorage, INDEX_CLOSEST_HIT); data += copyShaderIdentifier(data, shaderHandleStorage, INDEX_CLOSEST_HIT);

View file

@ -412,7 +412,6 @@ public:
VkDeviceSize copyShaderIdentifier(uint8_t* data, const uint8_t* shaderHandleStorage, uint32_t groupIndex) { VkDeviceSize copyShaderIdentifier(uint8_t* data, const uint8_t* shaderHandleStorage, uint32_t groupIndex) {
const uint32_t shaderGroupHandleSize = rayTracingProperties.shaderGroupHandleSize; const uint32_t shaderGroupHandleSize = rayTracingProperties.shaderGroupHandleSize;
memcpy(data, shaderHandleStorage + groupIndex * shaderGroupHandleSize, shaderGroupHandleSize); memcpy(data, shaderHandleStorage + groupIndex * shaderGroupHandleSize, shaderGroupHandleSize);
data += shaderGroupHandleSize;
return shaderGroupHandleSize; return shaderGroupHandleSize;
} }
@ -434,7 +433,6 @@ public:
VK_CHECK_RESULT(vkGetRayTracingShaderGroupHandlesNV(device, pipeline, 0, NUM_SHADER_GROUPS, sbtSize, shaderHandleStorage)); VK_CHECK_RESULT(vkGetRayTracingShaderGroupHandlesNV(device, pipeline, 0, NUM_SHADER_GROUPS, sbtSize, shaderHandleStorage));
auto* data = static_cast<uint8_t*>(shaderBindingTable.mapped); auto* data = static_cast<uint8_t*>(shaderBindingTable.mapped);
// Copy the shader identifiers to the shader binding table // Copy the shader identifiers to the shader binding table
VkDeviceSize offset = 0;
data += copyShaderIdentifier(data, shaderHandleStorage, INDEX_RAYGEN); data += copyShaderIdentifier(data, shaderHandleStorage, INDEX_RAYGEN);
data += copyShaderIdentifier(data, shaderHandleStorage, INDEX_MISS); data += copyShaderIdentifier(data, shaderHandleStorage, INDEX_MISS);
data += copyShaderIdentifier(data, shaderHandleStorage, INDEX_SHADOW_MISS); data += copyShaderIdentifier(data, shaderHandleStorage, INDEX_SHADOW_MISS);