From 1c9be00cecd26f38aba70d586c330e9a8e156b5a Mon Sep 17 00:00:00 2001 From: mklefrancois Date: Mon, 23 Aug 2021 13:45:20 +0200 Subject: [PATCH] Adapting to buildTlas changes, fix validation warnings, members initializations --- ray_tracing__advance/hello_vulkan.cpp | 3 +- ray_tracing__advance/obj.hpp | 8 ++--- ray_tracing__advance/raytrace.cpp | 28 ++++++++------- ray_tracing__simple/hello_vulkan.cpp | 15 ++++---- ray_tracing__simple/hello_vulkan.h | 8 ++--- .../hello_vulkan.cpp | 15 ++++---- ray_tracing_animation/hello_vulkan.cpp | 20 +++++------ ray_tracing_animation/hello_vulkan.h | 2 +- ray_tracing_anyhit/hello_vulkan.cpp | 15 ++++---- ray_tracing_anyhit/hello_vulkan.h | 8 ++--- ray_tracing_ao/hello_vulkan.cpp | 15 ++++---- ray_tracing_callable/hello_vulkan.cpp | 15 ++++---- ray_tracing_callable/hello_vulkan.h | 8 ++--- ray_tracing_gltf/hello_vulkan.cpp | 15 ++++---- ray_tracing_gltf/main.cpp | 10 +++--- ray_tracing_indirect_scissor/hello_vulkan.cpp | 34 ++++++++++--------- ray_tracing_indirect_scissor/hello_vulkan.h | 16 ++++----- ray_tracing_instances/hello_vulkan.cpp | 15 ++++---- ray_tracing_instances/hello_vulkan.h | 8 ++--- ray_tracing_intersection/hello_vulkan.cpp | 32 +++++++++-------- ray_tracing_intersection/hello_vulkan.h | 10 +++--- ray_tracing_jitter_cam/hello_vulkan.cpp | 15 ++++---- ray_tracing_manyhits/hello_vulkan.cpp | 13 +++---- ray_tracing_rayquery/hello_vulkan.cpp | 15 ++++---- ray_tracing_reflections/hello_vulkan.cpp | 15 ++++---- ray_tracing_specialization/hello_vulkan.cpp | 15 ++++---- 26 files changed, 197 insertions(+), 176 deletions(-) diff --git a/ray_tracing__advance/hello_vulkan.cpp b/ray_tracing__advance/hello_vulkan.cpp index c796103..7bd4320 100644 --- a/ray_tracing__advance/hello_vulkan.cpp +++ b/ray_tracing__advance/hello_vulkan.cpp @@ -551,7 +551,8 @@ void HelloVulkan::createImplictBuffers() auto cmdBuf = cmdGen.createCommandBuffer(); m_implObjects.implBuf = m_alloc.createBuffer(cmdBuf, m_implObjects.objImpl, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR - | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT); + | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT + | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR); m_implObjects.implMatBuf = m_alloc.createBuffer(cmdBuf, m_implObjects.implMat, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT); cmdGen.submitAndWait(cmdBuf); diff --git a/ray_tracing__advance/obj.hpp b/ray_tracing__advance/obj.hpp index a89ee3d..9cd4e5b 100644 --- a/ray_tracing__advance/obj.hpp +++ b/ray_tracing__advance/obj.hpp @@ -38,10 +38,10 @@ struct ObjInstance nvmath::mat4f transformIT{1}; // Inverse transpose uint32_t objIndex{0}; // Reference to the `m_objModel` uint32_t txtOffset{0}; // Offset in `m_textures` - VkDeviceAddress vertices; - VkDeviceAddress indices; - VkDeviceAddress materials; - VkDeviceAddress materialIndices; + VkDeviceAddress vertices{0}; + VkDeviceAddress indices{0}; + VkDeviceAddress materials{0}; + VkDeviceAddress materialIndices{0}; }; // Information pushed at each draw call diff --git a/ray_tracing__advance/raytrace.cpp b/ray_tracing__advance/raytrace.cpp index d7d7cc1..e0200b7 100644 --- a/ray_tracing__advance/raytrace.cpp +++ b/ray_tracing__advance/raytrace.cpp @@ -156,31 +156,33 @@ void Raytracer::createBottomLevelAS(std::vector& models, ImplInst& imp void Raytracer::createTopLevelAS(std::vector& instances, ImplInst& implicitObj) { - std::vector tlas; + std::vector tlas; auto nbObj = static_cast(instances.size()) - 1; // minus the implicit (for material) tlas.reserve(instances.size()); for(uint32_t i = 0; i < nbObj; i++) { - nvvk::RaytracingBuilderKHR::Instance rayInst; - rayInst.transform = instances[i].transform; // Position of the instance - rayInst.instanceCustomId = instances[i].objIndex; // gl_InstanceCustomIndexEXT - rayInst.blasId = instances[i].objIndex; - rayInst.hitGroupId = 0; // We will use the same hit group for all objects - rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + VkAccelerationStructureInstanceKHR rayInst; + rayInst.transform = nvvk::toTransformMatrixKHR(instances[i].transform); // Position of the instance + rayInst.instanceCustomIndex = instances[i].objIndex; // gl_InstanceCustomIndexEXT + rayInst.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress(instances[i].objIndex); + rayInst.instanceShaderBindingTableRecordOffset = 0; // We will use the same hit group for all objects + rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + rayInst.mask = 0xFF; tlas.emplace_back(rayInst); } // Add the blas containing all implicit if(!implicitObj.objImpl.empty()) { - nvvk::RaytracingBuilderKHR::Instance rayInst; - rayInst.transform = implicitObj.transform; // Position of the instance - rayInst.instanceCustomId = nbObj; // Same for material index - rayInst.blasId = static_cast(implicitObj.blasId); - rayInst.hitGroupId = 1; // We will use the same hit group for all objects (the second one) - rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + VkAccelerationStructureInstanceKHR rayInst; + rayInst.transform = nvvk::toTransformMatrixKHR(implicitObj.transform); // Position of the instance + rayInst.instanceCustomIndex = nbObj; // Same for material index + rayInst.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress(static_cast(implicitObj.blasId)); + rayInst.instanceShaderBindingTableRecordOffset = 1; // We will use the same hit group for all objects (the second one) + rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + rayInst.mask = 0xFF; tlas.emplace_back(rayInst); } diff --git a/ray_tracing__simple/hello_vulkan.cpp b/ray_tracing__simple/hello_vulkan.cpp index deb2dbb..58783ea 100644 --- a/ray_tracing__simple/hello_vulkan.cpp +++ b/ray_tracing__simple/hello_vulkan.cpp @@ -663,16 +663,17 @@ void HelloVulkan::createBottomLevelAS() void HelloVulkan::createTopLevelAS() { - std::vector tlas; + std::vector tlas; tlas.reserve(m_objInstance.size()); for(uint32_t i = 0; i < static_cast(m_objInstance.size()); i++) { - nvvk::RaytracingBuilderKHR::Instance rayInst; - rayInst.transform = m_objInstance[i].transform; // Position of the instance - rayInst.instanceCustomId = i; // gl_InstanceCustomIndexEXT - rayInst.blasId = m_objInstance[i].objIndex; - rayInst.hitGroupId = 0; // We will use the same hit group for all objects - rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + VkAccelerationStructureInstanceKHR rayInst; + rayInst.transform = nvvk::toTransformMatrixKHR(m_objInstance[i].transform); // Position of the instance + rayInst.instanceCustomIndex = i; // gl_InstanceCustomIndexEXT + rayInst.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress(m_objInstance[i].objIndex); + rayInst.instanceShaderBindingTableRecordOffset = 0; // We will use the same hit group for all objects + rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + rayInst.mask = 0xFF; tlas.emplace_back(rayInst); } m_rtBuilder.buildTlas(tlas, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR); diff --git a/ray_tracing__simple/hello_vulkan.h b/ray_tracing__simple/hello_vulkan.h index 4379cd6..6bb3a41 100644 --- a/ray_tracing__simple/hello_vulkan.h +++ b/ray_tracing__simple/hello_vulkan.h @@ -69,10 +69,10 @@ public: nvmath::mat4f transformIT{1}; // Inverse transpose uint32_t objIndex{0}; // Reference to the `m_objModel` uint32_t txtOffset{0}; // Offset in `m_textures` - VkDeviceAddress vertices; - VkDeviceAddress indices; - VkDeviceAddress materials; - VkDeviceAddress materialIndices; + VkDeviceAddress vertices{0}; + VkDeviceAddress indices{0}; + VkDeviceAddress materials{0}; + VkDeviceAddress materialIndices{0}; }; // Information pushed at each draw call diff --git a/ray_tracing_advanced_compilation/hello_vulkan.cpp b/ray_tracing_advanced_compilation/hello_vulkan.cpp index b553d6f..8fa3a1e 100644 --- a/ray_tracing_advanced_compilation/hello_vulkan.cpp +++ b/ray_tracing_advanced_compilation/hello_vulkan.cpp @@ -669,16 +669,17 @@ void HelloVulkan::createBottomLevelAS() void HelloVulkan::createTopLevelAS() { - std::vector tlas; + std::vector tlas; tlas.reserve(m_objInstance.size()); for(uint32_t i = 0; i < static_cast(m_objInstance.size()); i++) { - nvvk::RaytracingBuilderKHR::Instance rayInst; - rayInst.transform = m_objInstance[i].transform; // Position of the instance - rayInst.instanceCustomId = i; // gl_InstanceCustomIndexEXT - rayInst.blasId = m_objInstance[i].objIndex; - rayInst.hitGroupId = 0; // We will use the same hit group for all objects - rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + VkAccelerationStructureInstanceKHR rayInst; + rayInst.transform = nvvk::toTransformMatrixKHR(m_objInstance[i].transform); // Position of the instance + rayInst.instanceCustomIndex = i; // gl_InstanceCustomIndexEXT + rayInst.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress(m_objInstance[i].objIndex); + rayInst.instanceShaderBindingTableRecordOffset = 0; // We will use the same hit group for all objects + rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + rayInst.mask = 0xFF; tlas.emplace_back(rayInst); } m_rtBuilder.buildTlas(tlas, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR); diff --git a/ray_tracing_animation/hello_vulkan.cpp b/ray_tracing_animation/hello_vulkan.cpp index 31478dd..b2d52c4 100644 --- a/ray_tracing_animation/hello_vulkan.cpp +++ b/ray_tracing_animation/hello_vulkan.cpp @@ -671,15 +671,15 @@ void HelloVulkan::createBottomLevelAS() void HelloVulkan::createTopLevelAS() { - m_tlas.reserve(m_objInstance.size()); for(uint32_t i = 0; i < static_cast(m_objInstance.size()); i++) { - nvvk::RaytracingBuilderKHR::Instance rayInst; - rayInst.transform = m_objInstance[i].transform; // Position of the instance - rayInst.instanceCustomId = i; // gl_InstanceCustomIndexEXT - rayInst.blasId = m_objInstance[i].objIndex; - rayInst.hitGroupId = 0; - rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + VkAccelerationStructureInstanceKHR rayInst; + rayInst.transform = nvvk::toTransformMatrixKHR(m_objInstance[i].transform); // Position of the instance + rayInst.instanceCustomIndex = i; // gl_InstanceCustomIndexEXT + rayInst.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress(m_objInstance[i].objIndex); + rayInst.instanceShaderBindingTableRecordOffset = 0; // We will use the same hit group for all objects + rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + rayInst.mask = 0xFF; m_tlas.emplace_back(rayInst); } @@ -888,8 +888,8 @@ void HelloVulkan::animationInstances(float time) inst.transform = nvmath::rotation_mat4_y(i * deltaAngle + offset) * nvmath::translation_mat4(radius, 0.f, 0.f); inst.transformIT = nvmath::transpose(nvmath::invert(inst.transform)); - nvvk::RaytracingBuilderKHR::Instance& tinst = m_tlas[wusonIdx]; - tinst.transform = inst.transform; + VkAccelerationStructureInstanceKHR& tinst = m_tlas[wusonIdx]; + tinst.transform = nvvk::toTransformMatrixKHR(inst.transform); } // Update the buffer @@ -929,7 +929,7 @@ void HelloVulkan::animationObject(float time) vkCmdDispatch(cmdBuf, model.nbVertices, 1, 1); genCmdBuf.submitAndWait(cmdBuf); - m_rtBuilder.updateBlas(2); + m_rtBuilder.updateBlas(2, m_blas[2], VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR | VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_BUILD_BIT_KHR); } ////////////////////////////////////////////////////////////////////////// diff --git a/ray_tracing_animation/hello_vulkan.h b/ray_tracing_animation/hello_vulkan.h index 8524f22..5bf4a7b 100644 --- a/ray_tracing_animation/hello_vulkan.h +++ b/ray_tracing_animation/hello_vulkan.h @@ -150,7 +150,7 @@ public: VkPipeline m_rtPipeline; nvvk::SBTWrapper m_sbtWrapper; - std::vector m_tlas; + std::vector m_tlas; std::vector m_blas; struct RtPushConstant diff --git a/ray_tracing_anyhit/hello_vulkan.cpp b/ray_tracing_anyhit/hello_vulkan.cpp index 3929a4d..b3de3fc 100644 --- a/ray_tracing_anyhit/hello_vulkan.cpp +++ b/ray_tracing_anyhit/hello_vulkan.cpp @@ -665,16 +665,17 @@ void HelloVulkan::createBottomLevelAS() void HelloVulkan::createTopLevelAS() { - std::vector tlas; + std::vector tlas; tlas.reserve(m_objInstance.size()); for(uint32_t i = 0; i < static_cast(m_objInstance.size()); i++) { - nvvk::RaytracingBuilderKHR::Instance rayInst; - rayInst.transform = m_objInstance[i].transform; // Position of the instance - rayInst.instanceCustomId = i; // gl_InstanceCustomIndexEXT - rayInst.blasId = m_objInstance[i].objIndex; - rayInst.hitGroupId = 0; // We will use the same hit group for all objects - rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + VkAccelerationStructureInstanceKHR rayInst; + rayInst.transform = nvvk::toTransformMatrixKHR(m_objInstance[i].transform); // Position of the instance + rayInst.instanceCustomIndex = i; // gl_InstanceCustomIndexEXT + rayInst.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress(m_objInstance[i].objIndex); + rayInst.instanceShaderBindingTableRecordOffset = 0; // We will use the same hit group for all objects + rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + rayInst.mask = 0xFF; tlas.emplace_back(rayInst); } m_rtBuilder.buildTlas(tlas, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR); diff --git a/ray_tracing_anyhit/hello_vulkan.h b/ray_tracing_anyhit/hello_vulkan.h index 1b79818..b4758fe 100644 --- a/ray_tracing_anyhit/hello_vulkan.h +++ b/ray_tracing_anyhit/hello_vulkan.h @@ -69,10 +69,10 @@ public: nvmath::mat4f transformIT{1}; // Inverse transpose uint32_t objIndex{0}; // Reference to the `m_objModel` uint32_t txtOffset{0}; // Offset in `m_textures` - VkDeviceAddress vertices; - VkDeviceAddress indices; - VkDeviceAddress materials; - VkDeviceAddress materialIndices; + VkDeviceAddress vertices{0}; + VkDeviceAddress indices{0}; + VkDeviceAddress materials{0}; + VkDeviceAddress materialIndices{0}; }; // Information pushed at each draw call diff --git a/ray_tracing_ao/hello_vulkan.cpp b/ray_tracing_ao/hello_vulkan.cpp index e59c173..92a6b73 100644 --- a/ray_tracing_ao/hello_vulkan.cpp +++ b/ray_tracing_ao/hello_vulkan.cpp @@ -700,16 +700,17 @@ void HelloVulkan::createBottomLevelAS() void HelloVulkan::createTopLevelAS() { - std::vector tlas; + std::vector tlas; tlas.reserve(m_objInstance.size()); for(uint32_t i = 0; i < static_cast(m_objInstance.size()); i++) { - nvvk::RaytracingBuilderKHR::Instance rayInst; - rayInst.transform = m_objInstance[i].transform; // Position of the instance - rayInst.instanceCustomId = i; // gl_InstanceCustomIndexEXT - rayInst.blasId = m_objInstance[i].objIndex; - rayInst.hitGroupId = 0; // We will use the same hit group for all objects - rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + VkAccelerationStructureInstanceKHR rayInst; + rayInst.transform = nvvk::toTransformMatrixKHR(m_objInstance[i].transform); // Position of the instance + rayInst.instanceCustomIndex = i; // gl_InstanceCustomIndexEXT + rayInst.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress(m_objInstance[i].objIndex); + rayInst.instanceShaderBindingTableRecordOffset = 0; // We will use the same hit group for all objects + rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + rayInst.mask = 0xFF; tlas.emplace_back(rayInst); } m_rtBuilder.buildTlas(tlas, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR); diff --git a/ray_tracing_callable/hello_vulkan.cpp b/ray_tracing_callable/hello_vulkan.cpp index de0d4cb..03a5c17 100644 --- a/ray_tracing_callable/hello_vulkan.cpp +++ b/ray_tracing_callable/hello_vulkan.cpp @@ -662,16 +662,17 @@ void HelloVulkan::createBottomLevelAS() void HelloVulkan::createTopLevelAS() { - std::vector tlas; + std::vector tlas; tlas.reserve(m_objInstance.size()); for(uint32_t i = 0; i < static_cast(m_objInstance.size()); i++) { - nvvk::RaytracingBuilderKHR::Instance rayInst; - rayInst.transform = m_objInstance[i].transform; // Position of the instance - rayInst.instanceCustomId = i; // gl_InstanceCustomIndexEXT - rayInst.blasId = m_objInstance[i].objIndex; - rayInst.hitGroupId = 0; // We will use the same hit group for all objects - rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + VkAccelerationStructureInstanceKHR rayInst; + rayInst.transform = nvvk::toTransformMatrixKHR(m_objInstance[i].transform); // Position of the instance + rayInst.instanceCustomIndex = i; // gl_InstanceCustomIndexEXT + rayInst.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress(m_objInstance[i].objIndex); + rayInst.instanceShaderBindingTableRecordOffset = 0; // We will use the same hit group for all objects + rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + rayInst.mask = 0xFF; tlas.emplace_back(rayInst); } m_rtBuilder.buildTlas(tlas, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR); diff --git a/ray_tracing_callable/hello_vulkan.h b/ray_tracing_callable/hello_vulkan.h index 67c101d..8fdc61c 100644 --- a/ray_tracing_callable/hello_vulkan.h +++ b/ray_tracing_callable/hello_vulkan.h @@ -70,10 +70,10 @@ public: nvmath::mat4f transformIT{1}; // Inverse transpose uint32_t objIndex{0}; // Reference to the `m_objModel` uint32_t txtOffset{0}; // Offset in `m_textures` - VkDeviceAddress vertices; - VkDeviceAddress indices; - VkDeviceAddress materials; - VkDeviceAddress materialIndices; + VkDeviceAddress vertices{0}; + VkDeviceAddress indices{0}; + VkDeviceAddress materials{0}; + VkDeviceAddress materialIndices{0}; }; // Information pushed at each draw call diff --git a/ray_tracing_gltf/hello_vulkan.cpp b/ray_tracing_gltf/hello_vulkan.cpp index 5efaa2e..bb1ce69 100644 --- a/ray_tracing_gltf/hello_vulkan.cpp +++ b/ray_tracing_gltf/hello_vulkan.cpp @@ -668,16 +668,17 @@ void HelloVulkan::createBottomLevelAS() void HelloVulkan::createTopLevelAS() { - std::vector tlas; + std::vector tlas; tlas.reserve(m_gltfScene.m_nodes.size()); for(auto& node : m_gltfScene.m_nodes) { - nvvk::RaytracingBuilderKHR::Instance rayInst; - rayInst.transform = node.worldMatrix; - rayInst.instanceCustomId = node.primMesh; // gl_InstanceCustomIndexEXT: to find which primitive - rayInst.blasId = node.primMesh; - rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; - rayInst.hitGroupId = 0; // We will use the same hit group for all objects + VkAccelerationStructureInstanceKHR rayInst; + rayInst.transform = nvvk::toTransformMatrixKHR(node.worldMatrix); + rayInst.instanceCustomIndex = node.primMesh; // gl_InstanceCustomIndexEXT: to find which primitive + rayInst.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress(node.primMesh); + rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + rayInst.instanceShaderBindingTableRecordOffset = 0; // We will use the same hit group for all objects + rayInst.mask = 0xFF; tlas.emplace_back(rayInst); } m_rtBuilder.buildTlas(tlas, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR); diff --git a/ray_tracing_gltf/main.cpp b/ray_tracing_gltf/main.cpp index cbe81db..b2f3d44 100644 --- a/ray_tracing_gltf/main.cpp +++ b/ray_tracing_gltf/main.cpp @@ -51,10 +51,10 @@ static void onErrorCallback(int error, const char* description) } // Extra UI -void renderUI(HelloVulkan& helloVk) +void renderUI(HelloVulkan& helloVk, bool useRaytracer) { ImGuiH::CameraWidget(); - if(ImGui::CollapsingHeader("Light")) + if( !useRaytracer && ImGui::CollapsingHeader("Light")) { ImGui::RadioButton("Point", &helloVk.m_pushConstant.lightType, 0); ImGui::SameLine(); @@ -204,9 +204,9 @@ int main(int argc, char** argv) { ImGuiH::Panel::Begin(); ImGui::ColorEdit3("Clear color", reinterpret_cast(&clearColor)); - ImGui::Checkbox("Ray Tracer mode", &useRaytracer); // Switch between raster and ray tracing - - renderUI(helloVk); + if (ImGui::Checkbox("Ray Tracer mode", &useRaytracer)) // Switch between raster and ray tracing + helloVk.resetFrame(); + renderUI(helloVk, useRaytracer); ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); ImGuiH::Control::Info("", "", "(F10) Toggle Pane", ImGuiH::Control::Flags::Disabled); ImGuiH::Panel::End(); diff --git a/ray_tracing_indirect_scissor/hello_vulkan.cpp b/ray_tracing_indirect_scissor/hello_vulkan.cpp index bff76eb..660dd91 100644 --- a/ray_tracing_indirect_scissor/hello_vulkan.cpp +++ b/ray_tracing_indirect_scissor/hello_vulkan.cpp @@ -689,7 +689,7 @@ void HelloVulkan::fillLanternVerts(std::vector& vertices, std::ve for(int recursions = 0; recursions < 3; ++recursions) { std::vector new_triangles; - for(Triangle t : triangles) + for(const Triangle& t : triangles) { // Split each of three edges in half, then fixup the // length of the midpoint to match m_lanternModelRadius. @@ -712,8 +712,8 @@ void HelloVulkan::fillLanternVerts(std::vector& vertices, std::ve indices.reserve(triangles.size() * 3); // Write out the vertices to the vertices vector, and - // connect the tesselated triangles with indices in the indices vector. - for(Triangle t : triangles) + // connect the tessellated triangles with indices in the indices vector. + for(const Triangle& t : triangles) { vertices[t.vert0.index] = t.vert0.vertex; vertices[t.vert1.index] = t.vert1.vertex; @@ -829,30 +829,32 @@ void HelloVulkan::createTopLevelAS() assert(m_lanternCount == 0); m_lanternCount = m_lanterns.size(); - std::vector tlas; + std::vector tlas; tlas.reserve(m_objInstance.size() + m_lanternCount); // Add the OBJ instances. for(uint32_t i = 0; i < static_cast(m_objInstance.size()); i++) { - nvvk::RaytracingBuilderKHR::Instance rayInst; - rayInst.transform = m_objInstance[i].transform; // Position of the instance - rayInst.instanceCustomId = i; // gl_InstanceCustomIndexEXT - rayInst.blasId = m_objInstance[i].objIndex; - rayInst.hitGroupId = 0; // We will use the same hit group for all OBJ - rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + VkAccelerationStructureInstanceKHR rayInst; + rayInst.transform = nvvk::toTransformMatrixKHR(m_objInstance[i].transform); // Position of the instance + rayInst.instanceCustomIndex = i; // gl_InstanceCustomIndexEXT + rayInst.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress(m_objInstance[i].objIndex); + rayInst.instanceShaderBindingTableRecordOffset = 0; // We will use the same hit group for all objects + rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + rayInst.mask = 0xFF; tlas.emplace_back(rayInst); } // Add lantern instances. for(int i = 0; i < static_cast(m_lanterns.size()); ++i) { - nvvk::RaytracingBuilderKHR::Instance lanternInstance; - lanternInstance.transform = nvmath::translation_mat4(m_lanterns[i].position); - lanternInstance.instanceCustomId = i; - lanternInstance.blasId = uint32_t(m_lanternBlasId); - lanternInstance.hitGroupId = 1; // Next hit group is for lanterns. - lanternInstance.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + VkAccelerationStructureInstanceKHR lanternInstance; + lanternInstance.transform = nvvk::toTransformMatrixKHR(nvmath::translation_mat4(m_lanterns[i].position)); + lanternInstance.instanceCustomIndex = i; + lanternInstance.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress(uint32_t(m_lanternBlasId)); + lanternInstance.instanceShaderBindingTableRecordOffset = 1; // Next hit group is for lanterns. + lanternInstance.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + lanternInstance.mask = 0xFF; tlas.emplace_back(lanternInstance); } diff --git a/ray_tracing_indirect_scissor/hello_vulkan.h b/ray_tracing_indirect_scissor/hello_vulkan.h index 15ad451..9e233d0 100644 --- a/ray_tracing_indirect_scissor/hello_vulkan.h +++ b/ray_tracing_indirect_scissor/hello_vulkan.h @@ -80,10 +80,10 @@ public: nvmath::mat4f transformIT{1}; // Inverse transpose uint32_t objIndex{0}; // Reference to the `m_objModel` uint32_t txtOffset{0}; // Offset in `m_textures` - VkDeviceAddress vertices; - VkDeviceAddress indices; - VkDeviceAddress materials; - VkDeviceAddress materialIndices; + VkDeviceAddress vertices{0}; + VkDeviceAddress indices{0}; + VkDeviceAddress materials{0}; + VkDeviceAddress materialIndices{0}; }; // Information pushed at each draw call @@ -101,8 +101,8 @@ public: { nvmath::vec3f position; nvmath::vec3f color; - float brightness; - float radius; // Max world-space distance that light illuminates. + float brightness{0}; + float radius{0}; // Max world-space distance that light illuminates. }; // Information on each colored lantern, plus the info needed for dispatching the @@ -114,8 +114,8 @@ public: // Filled in by the device using a compute shader. // NOTE: I rely on indirectCommand being the first member. VkTraceRaysIndirectCommandKHR indirectCommand; - int32_t offsetX; - int32_t offsetY; + int32_t offsetX{0}; + int32_t offsetY{0}; // Filled in by the host. Lantern lantern; diff --git a/ray_tracing_instances/hello_vulkan.cpp b/ray_tracing_instances/hello_vulkan.cpp index b8d5041..b741ddd 100644 --- a/ray_tracing_instances/hello_vulkan.cpp +++ b/ray_tracing_instances/hello_vulkan.cpp @@ -666,16 +666,17 @@ void HelloVulkan::createBottomLevelAS() void HelloVulkan::createTopLevelAS() { - std::vector tlas; + std::vector tlas; tlas.reserve(m_objInstance.size()); for(uint32_t i = 0; i < static_cast(m_objInstance.size()); i++) { - nvvk::RaytracingBuilderKHR::Instance rayInst; - rayInst.transform = m_objInstance[i].transform; // Position of the instance - rayInst.instanceCustomId = i; // gl_InstanceCustomIndexEXT - rayInst.blasId = m_objInstance[i].objIndex; - rayInst.hitGroupId = 0; // We will use the same hit group for all objects - rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + VkAccelerationStructureInstanceKHR rayInst; + rayInst.transform = nvvk::toTransformMatrixKHR(m_objInstance[i].transform); // Position of the instance + rayInst.instanceCustomIndex = i; // gl_InstanceCustomIndexEXT + rayInst.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress(m_objInstance[i].objIndex); + rayInst.instanceShaderBindingTableRecordOffset = 0; // We will use the same hit group for all objects + rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + rayInst.mask = 0xFF; tlas.emplace_back(rayInst); } m_rtBuilder.buildTlas(tlas, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR); diff --git a/ray_tracing_instances/hello_vulkan.h b/ray_tracing_instances/hello_vulkan.h index ec89f98..0e1990d 100644 --- a/ray_tracing_instances/hello_vulkan.h +++ b/ray_tracing_instances/hello_vulkan.h @@ -87,10 +87,10 @@ public: nvmath::mat4f transformIT{1}; // Inverse transpose uint32_t objIndex{0}; // Reference to the `m_objModel` uint32_t txtOffset{0}; // Offset in `m_textures` - VkDeviceAddress vertices; - VkDeviceAddress indices; - VkDeviceAddress materials; - VkDeviceAddress materialIndices; + VkDeviceAddress vertices{0}; + VkDeviceAddress indices{0}; + VkDeviceAddress materials{0}; + VkDeviceAddress materialIndices{0}; }; // Information pushed at each draw call diff --git a/ray_tracing_intersection/hello_vulkan.cpp b/ray_tracing_intersection/hello_vulkan.cpp index 813fdc9..4c6a2e3 100644 --- a/ray_tracing_intersection/hello_vulkan.cpp +++ b/ray_tracing_intersection/hello_vulkan.cpp @@ -737,7 +737,9 @@ void HelloVulkan::createSpheres(uint32_t nbSpheres) nvvk::CommandPool genCmdBuf(m_device, m_graphicsQueueIndex); auto cmdBuf = genCmdBuf.createCommandBuffer(); m_spheresBuffer = m_alloc.createBuffer(cmdBuf, m_spheres, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT); - m_spheresAabbBuffer = m_alloc.createBuffer(cmdBuf, aabbs, VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT); + m_spheresAabbBuffer = m_alloc.createBuffer(cmdBuf, aabbs, + VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT + | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR); m_spheresMatIndexBuffer = m_alloc.createBuffer(cmdBuf, matIdx, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT); m_spheresMatColorBuffer = @@ -783,29 +785,31 @@ void HelloVulkan::createBottomLevelAS() void HelloVulkan::createTopLevelAS() { - std::vector tlas; + std::vector tlas; auto nbObj = static_cast(m_objInstance.size()) - 1; tlas.reserve(nbObj); for(uint32_t i = 0; i < nbObj; i++) { - nvvk::RaytracingBuilderKHR::Instance rayInst; - rayInst.transform = m_objInstance[i].transform; // Position of the instance - rayInst.instanceCustomId = i; // gl_InstanceCustomIndexEXT - rayInst.blasId = m_objInstance[i].objIndex; - rayInst.hitGroupId = 0; // We will use the same hit group for all objects - rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + VkAccelerationStructureInstanceKHR rayInst; + rayInst.transform = nvvk::toTransformMatrixKHR(m_objInstance[i].transform); // Position of the instance + rayInst.instanceCustomIndex = i; // gl_InstanceCustomIndexEXT + rayInst.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress(m_objInstance[i].objIndex); + rayInst.instanceShaderBindingTableRecordOffset = 0; // We will use the same hit group for all objects + rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + rayInst.mask = 0xFF; tlas.emplace_back(rayInst); } // Add the blas containing all spheres { - nvvk::RaytracingBuilderKHR::Instance rayInst; - rayInst.transform = m_objInstance[0].transform; // Position of the instance - rayInst.instanceCustomId = nbObj; // gl_InstanceCustomIndexEXT - rayInst.blasId = static_cast(m_objModel.size()); - rayInst.hitGroupId = 1; // We will use the same hit group for all objects - rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + VkAccelerationStructureInstanceKHR rayInst; + rayInst.transform = nvvk::toTransformMatrixKHR(m_objInstance[0].transform); // Position of the instance + rayInst.instanceCustomIndex = nbObj; // gl_InstanceCustomIndexEXT + rayInst.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress(static_cast(m_objModel.size())); + rayInst.instanceShaderBindingTableRecordOffset = 1; // We will use the same hit group for all objects + rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + rayInst.mask = 0xFF; tlas.emplace_back(rayInst); } diff --git a/ray_tracing_intersection/hello_vulkan.h b/ray_tracing_intersection/hello_vulkan.h index d1f1f95..8f06847 100644 --- a/ray_tracing_intersection/hello_vulkan.h +++ b/ray_tracing_intersection/hello_vulkan.h @@ -69,10 +69,10 @@ public: nvmath::mat4f transformIT{1}; // Inverse transpose uint32_t objIndex{0}; // Reference to the `m_objModel` uint32_t txtOffset{0}; // Offset in `m_textures` - VkDeviceAddress vertices; - VkDeviceAddress indices; - VkDeviceAddress materials; - VkDeviceAddress materialIndices; + VkDeviceAddress vertices{0}; + VkDeviceAddress indices{0}; + VkDeviceAddress materials{0}; + VkDeviceAddress materialIndices{0}; }; // Information pushed at each draw call @@ -162,7 +162,7 @@ public: struct Sphere { nvmath::vec3f center; - float radius; + float radius{0}; }; struct Aabb diff --git a/ray_tracing_jitter_cam/hello_vulkan.cpp b/ray_tracing_jitter_cam/hello_vulkan.cpp index ab1d1d2..61cdc34 100644 --- a/ray_tracing_jitter_cam/hello_vulkan.cpp +++ b/ray_tracing_jitter_cam/hello_vulkan.cpp @@ -664,16 +664,17 @@ void HelloVulkan::createBottomLevelAS() void HelloVulkan::createTopLevelAS() { - std::vector tlas; + std::vector tlas; tlas.reserve(m_objInstance.size()); for(uint32_t i = 0; i < static_cast(m_objInstance.size()); i++) { - nvvk::RaytracingBuilderKHR::Instance rayInst; - rayInst.transform = m_objInstance[i].transform; // Position of the instance - rayInst.instanceCustomId = i; // gl_InstanceCustomIndexEXT - rayInst.blasId = m_objInstance[i].objIndex; - rayInst.hitGroupId = 0; // We will use the same hit group for all objects - rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + VkAccelerationStructureInstanceKHR rayInst; + rayInst.transform = nvvk::toTransformMatrixKHR(m_objInstance[i].transform); // Position of the instance + rayInst.instanceCustomIndex = i; // gl_InstanceCustomIndexEXT + rayInst.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress(m_objInstance[i].objIndex); + rayInst.instanceShaderBindingTableRecordOffset = 0; // We will use the same hit group for all objects + rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + rayInst.mask = 0xFF; tlas.emplace_back(rayInst); } m_rtBuilder.buildTlas(tlas, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR); diff --git a/ray_tracing_manyhits/hello_vulkan.cpp b/ray_tracing_manyhits/hello_vulkan.cpp index 849432d..fedff2c 100644 --- a/ray_tracing_manyhits/hello_vulkan.cpp +++ b/ray_tracing_manyhits/hello_vulkan.cpp @@ -666,16 +666,17 @@ void HelloVulkan::createBottomLevelAS() void HelloVulkan::createTopLevelAS() { - std::vector tlas; + std::vector tlas; tlas.reserve(m_objInstance.size()); for(uint32_t i = 0; i < static_cast(m_objInstance.size()); i++) { - nvvk::RaytracingBuilderKHR::Instance rayInst; - rayInst.transform = m_objInstance[i].transform; // Position of the instance - rayInst.instanceCustomId = i; // gl_InstanceCustomIndexEXT - rayInst.blasId = m_objInstance[i].objIndex; - rayInst.hitGroupId = m_objInstance[i].hitgroup; // Using the hit group set in main + VkAccelerationStructureInstanceKHR rayInst; + rayInst.transform = nvvk::toTransformMatrixKHR(m_objInstance[i].transform); // Position of the instance + rayInst.instanceCustomIndex = i; // gl_InstanceCustomIndexEXT + rayInst.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress(m_objInstance[i].objIndex); + rayInst.instanceShaderBindingTableRecordOffset = m_objInstance[i].hitgroup; // Using the hit group set in main rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + rayInst.mask = 0xFF; tlas.emplace_back(rayInst); } m_rtBuilder.buildTlas(tlas, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR); diff --git a/ray_tracing_rayquery/hello_vulkan.cpp b/ray_tracing_rayquery/hello_vulkan.cpp index aa362e3..07adcd4 100644 --- a/ray_tracing_rayquery/hello_vulkan.cpp +++ b/ray_tracing_rayquery/hello_vulkan.cpp @@ -665,16 +665,17 @@ void HelloVulkan::createBottomLevelAS() void HelloVulkan::createTopLevelAS() { - std::vector tlas; + std::vector tlas; tlas.reserve(m_objInstance.size()); for(uint32_t i = 0; i < static_cast(m_objInstance.size()); i++) { - nvvk::RaytracingBuilderKHR::Instance rayInst; - rayInst.transform = m_objInstance[i].transform; // Position of the instance - rayInst.instanceCustomId = i; // gl_InstanceCustomIndexEXT - rayInst.blasId = m_objInstance[i].objIndex; - rayInst.hitGroupId = 0; // We will use the same hit group for all objects - rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + VkAccelerationStructureInstanceKHR rayInst; + rayInst.transform = nvvk::toTransformMatrixKHR(m_objInstance[i].transform); // Position of the instance + rayInst.instanceCustomIndex = i; // gl_InstanceCustomIndexEXT + rayInst.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress(m_objInstance[i].objIndex); + rayInst.instanceShaderBindingTableRecordOffset = 0; // We will use the same hit group for all objects + rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + rayInst.mask = 0xFF; tlas.emplace_back(rayInst); } m_rtBuilder.buildTlas(tlas, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR); diff --git a/ray_tracing_reflections/hello_vulkan.cpp b/ray_tracing_reflections/hello_vulkan.cpp index 9929561..b4317b5 100644 --- a/ray_tracing_reflections/hello_vulkan.cpp +++ b/ray_tracing_reflections/hello_vulkan.cpp @@ -663,16 +663,17 @@ void HelloVulkan::createBottomLevelAS() void HelloVulkan::createTopLevelAS() { - std::vector tlas; + std::vector tlas; tlas.reserve(m_objInstance.size()); for(uint32_t i = 0; i < static_cast(m_objInstance.size()); i++) { - nvvk::RaytracingBuilderKHR::Instance rayInst; - rayInst.transform = m_objInstance[i].transform; // Position of the instance - rayInst.instanceCustomId = i; // gl_InstanceCustomIndexEXT - rayInst.blasId = m_objInstance[i].objIndex; - rayInst.hitGroupId = 0; // We will use the same hit group for all objects - rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + VkAccelerationStructureInstanceKHR rayInst; + rayInst.transform = nvvk::toTransformMatrixKHR(m_objInstance[i].transform); // Position of the instance + rayInst.instanceCustomIndex = i; // gl_InstanceCustomIndexEXT + rayInst.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress(m_objInstance[i].objIndex); + rayInst.instanceShaderBindingTableRecordOffset = 0; // We will use the same hit group for all objects + rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + rayInst.mask = 0xFF; tlas.emplace_back(rayInst); } m_rtBuilder.buildTlas(tlas, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR); diff --git a/ray_tracing_specialization/hello_vulkan.cpp b/ray_tracing_specialization/hello_vulkan.cpp index 01a9cae..b7b307b 100644 --- a/ray_tracing_specialization/hello_vulkan.cpp +++ b/ray_tracing_specialization/hello_vulkan.cpp @@ -665,16 +665,17 @@ void HelloVulkan::createBottomLevelAS() void HelloVulkan::createTopLevelAS() { - std::vector tlas; + std::vector tlas; tlas.reserve(m_objInstance.size()); for(uint32_t i = 0; i < static_cast(m_objInstance.size()); i++) { - nvvk::RaytracingBuilderKHR::Instance rayInst; - rayInst.transform = m_objInstance[i].transform; // Position of the instance - rayInst.instanceCustomId = i; // gl_InstanceCustomIndexEXT - rayInst.blasId = m_objInstance[i].objIndex; - rayInst.hitGroupId = 0; // We will use the same hit group for all objects - rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + VkAccelerationStructureInstanceKHR rayInst; + rayInst.transform = nvvk::toTransformMatrixKHR(m_objInstance[i].transform); // Position of the instance + rayInst.instanceCustomIndex = i; // gl_InstanceCustomIndexEXT + rayInst.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress(m_objInstance[i].objIndex); + rayInst.instanceShaderBindingTableRecordOffset = 0; // We will use the same hit group for all objects + rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; + rayInst.mask = 0xFF; tlas.emplace_back(rayInst); } m_rtBuilder.buildTlas(tlas, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR);