updating sbt record data example to also cover miss sbt data

This commit is contained in:
n8vm 2022-09-26 20:28:08 -06:00
parent e8270a7b38
commit 3a4ea2b697
11 changed files with 90 additions and 29 deletions

View file

@ -3,7 +3,15 @@
layout(location = 0) rayPayloadInEXT vec3 hitValue;
layout(shaderRecordEXT, std430) buffer SBT {
float r;
float g;
float b;
};
void main()
{
// for now, we do nothing in the miss program
// Update the hit value to the hit record SBT data associated with this
// miss record
hitValue = vec3(r, g, b);
}

View file

@ -30,10 +30,23 @@ void main()
float tmin = 0.001;
float tmax = 10000.0;
// Initialize the hit value to the raygen SBT data
hitValue = vec3(r, g, b);
// use border to demonstrate raygen record data
if (all(greaterThan(gl_LaunchIDEXT.xy, ivec2(16, 16))) && all(lessThan(gl_LaunchIDEXT.xy, gl_LaunchSizeEXT.xy - ivec2(16, 16))))
{
// Generate a checker board pattern to trace out rays or use hit record data
ivec2 pos = ivec2(gl_LaunchIDEXT / 16);
if (((pos.x + pos.y % 2) % 2) == 0) {
// This will set hit value to either hit or miss SBT record color
traceRayEXT(topLevelAS, gl_RayFlagsOpaqueEXT, 0xff, 0, 0, 0, origin.xyz, tmin, direction.xyz, tmax, 0);
}
else {
// Set the hit value to the raygen SBT data
hitValue = vec3(r, g, b);
}
}
else {
// Set hit value to black
hitValue = vec3(0.0, 0.0, 0.0);
}
imageStore(image, ivec2(gl_LaunchIDEXT.xy), vec4(hitValue, 0.0));
}

View file

@ -5,8 +5,18 @@ struct Payload
[[vk::location(0)]] float3 hitValue;
};
struct SBT {
float r;
float g;
float b;
};
[[vk::shader_record_ext]]
ConstantBuffer<SBT> sbt;
[shader("miss")]
void main(inout Payload p)
{
// for now, we do nothing in the miss program
// Update the hit value to the hit record SBT data associated with this
// miss record
p.hitValue = float3(sbt.r, sbt.g, sbt.g);
}

View file

@ -40,10 +40,26 @@ void main()
rayDesc.TMin = 0.001;
rayDesc.TMax = 10000.0;
// Initialize the hit value to the raygen SBT data
Payload payload;
payload.hitValue = float3(sbt.r, sbt.g, sbt.b);
// use border to demonstrate raygen record data
if (all(LaunchID.xy > int2(16, 16)) && all(LaunchID.xy < LaunchSize.xy - int2(16, 16)))
{
// Generate a checker board pattern to trace out rays or use hit record data
int2 pos = int2(LaunchID.xy / 16);
if (((pos.x + pos.y % 2) % 2) == 0) {
// This will set hit value to either hit or miss SBT record color
TraceRay(rs, RAY_FLAG_FORCE_OPAQUE, 0xff, 0, 0, 0, rayDesc, payload);
}
else {
// Set the hit value to the raygen SBT data
payload.hitValue = float3(sbt.r, sbt.g, sbt.b);
}
}
else {
// Set hit value to black
payload.hitValue = float3(0.0, 0.0, 0.0);
}
image[int2(LaunchID.xy)] = float4(payload.hitValue, 0.0);
}

View file

@ -3,6 +3,8 @@
*
* Uses the data section of each shader binding table record to color the background and geometry
*
* Example by Nate Morrical (https://github.com/natevm)
*
* Copyright (C) 2019-2020 by Sascha Willems - www.saschawillems.de
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
@ -503,16 +505,23 @@ public:
/*
Create the Shader Binding Tables that binds the programs and top-level acceleration structure
In this example, we embed data in each record that can be read by the device during ray tracing
SBT Layout used in this sample:
/-----------\
| raygen |
|-----------|
| miss |
|-----------|
| hit |
\-----------/
/----------------\
| raygen handle |
| - - - - - - - |
| raygen data |
|----------------|
| miss handle |
| - - - - - - - |
| miss data |
|----------------|
| hit handle |
| - - - - - - - |
| hit data |
\----------------/
*/
void createShaderBindingTable() {
@ -526,8 +535,11 @@ public:
const VkBufferUsageFlags bufferUsageFlags = VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
const VkMemoryPropertyFlags memoryUsageFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
// We allocate space for the handle (which is like lambda function pointers to call in the ray tracing pipeline)
// as well as the data to pass to those functions (which act as the variables being "captured" by those lambda functions)
VK_CHECK_RESULT(vulkanDevice->createBuffer(bufferUsageFlags, memoryUsageFlags, &raygenShaderBindingTable, handleSize + sizeof(float) * 3));
VK_CHECK_RESULT(vulkanDevice->createBuffer(bufferUsageFlags, memoryUsageFlags, &missShaderBindingTable, handleSize));
VK_CHECK_RESULT(vulkanDevice->createBuffer(bufferUsageFlags, memoryUsageFlags, &missShaderBindingTable, handleSize + sizeof(float) * 3));
VK_CHECK_RESULT(vulkanDevice->createBuffer(bufferUsageFlags, memoryUsageFlags, &hitShaderBindingTable, handleSize + sizeof(float) * 3));
// Copy handles
@ -538,17 +550,17 @@ public:
memcpy(missShaderBindingTable.mapped, shaderHandleStorage.data() + handleSizeAligned, handleSize);
memcpy(hitShaderBindingTable.mapped, shaderHandleStorage.data() + handleSizeAligned * 2, handleSize);
// set raygen sbt colors
{
glm::vec3 color(1.f, 0.f, 0.f);
memcpy(((uint8_t*)(raygenShaderBindingTable.mapped)) + handleSize, &color, sizeof(glm::vec3));
}
// Copy over raygen record data
glm::vec3 color1(0.5f, 0.5f, 0.5f);
memcpy(((uint8_t*)(raygenShaderBindingTable.mapped)) + handleSize, &color1, sizeof(glm::vec3));
// set chit sbt colors
{
glm::vec3 color(0.f, 1.f, 0.f);
memcpy(((uint8_t*)(hitShaderBindingTable.mapped)) + handleSize, &color, sizeof(glm::vec3));
}
// Copy over miss record data
glm::vec3 color2(1.f, 1.f, 1.f);
memcpy(((uint8_t*)(missShaderBindingTable.mapped)) + handleSize, &color2, sizeof(glm::vec3));
// Copy over hit group record data
glm::vec3 color3(1.f, 0.f, 0.f);
memcpy(((uint8_t*)(hitShaderBindingTable.mapped)) + handleSize, &color3, sizeof(glm::vec3));
}
/*
@ -752,6 +764,8 @@ public:
const uint32_t handleSizeAligned = vks::tools::alignedSize(rayTracingPipelineProperties.shaderGroupHandleSize, rayTracingPipelineProperties.shaderGroupHandleAlignment);
// Note, we add 3 * sizeof(float) to each SBT entry size to account for the data sections of these records
// that we use to store our color data
VkStridedDeviceAddressRegionKHR raygenShaderSbtEntry{};
raygenShaderSbtEntry.deviceAddress = getBufferDeviceAddress(raygenShaderBindingTable.buffer);
raygenShaderSbtEntry.stride = handleSizeAligned;
@ -760,7 +774,7 @@ public:
VkStridedDeviceAddressRegionKHR missShaderSbtEntry{};
missShaderSbtEntry.deviceAddress = getBufferDeviceAddress(missShaderBindingTable.buffer);
missShaderSbtEntry.stride = handleSizeAligned;
missShaderSbtEntry.size = handleSizeAligned;
missShaderSbtEntry.size = vks::tools::alignedSize(handleSizeAligned + 3 * sizeof(float), rayTracingPipelineProperties.shaderGroupBaseAlignment);
VkStridedDeviceAddressRegionKHR hitShaderSbtEntry{};
hitShaderSbtEntry.deviceAddress = getBufferDeviceAddress(hitShaderBindingTable.buffer);