146 lines
4.3 KiB
HTML
146 lines
4.3 KiB
HTML
<meta charset="utf-8" lang="en">
|
|
**NVIDIA Vulkan Ray Tracing Tutorial**
|
|
**Ray Query**
|
|
|
|
|
|

|
|
|
|
This is an extension of the Vulkan ray tracing [tutorial](vkrt_tutorial.md.htm).
|
|
|
|
(insert setup.md.html here)
|
|
|
|
# Ray Query
|
|
|
|
This extension is allowing to execute ray intersection queries in any shader stages. In this example, we will add
|
|
ray queries to the fragment shader to cast shadow rays.
|
|
|
|
In the contrary to all other examples, with this one, we are removing code. There are no need to have a SBT and a raytracing pipeline, the only thing that
|
|
will matter, is the creation of the acceleration structure.
|
|
|
|
Starting from the end of the tutorial, [ray_tracing__simple](https://github.com/nvpro-samples/vk_raytracing_tutorial/tree/master/ray_tracing__simple) we will remove
|
|
all functions that were dedicated to ray tracing and keep only the construction of the BLAS and TLAS.
|
|
|
|
# Cleanup
|
|
|
|
First, let's remove all extra code
|
|
|
|
## hello_vulkan (header)
|
|
|
|
Remove most functions and members to keep only what is need to create the acceleration structure:
|
|
|
|
~~~~ C++
|
|
// #VKRay
|
|
void initRayTracing();
|
|
nvvkpp::RaytracingBuilderKHR::Blas objectToVkGeometryKHR(const ObjModel& model);
|
|
void createBottomLevelAS();
|
|
void createTopLevelAS();
|
|
|
|
vk::PhysicalDeviceRayTracingPropertiesKHR m_rtProperties;
|
|
nvvkpp::RaytracingBuilderKHR m_rtBuilder;
|
|
~~~~
|
|
|
|
## hello_vulkan (source)
|
|
|
|
From the source code, remove the code for all functions that was previously removed.
|
|
|
|
## Shaders
|
|
|
|
You can safely remove all raytrace.* shaders
|
|
|
|
|
|
# Support for Fragment shader
|
|
|
|
In `HelloVulkan::createDescriptorSetLayout`, add the acceleration structure to the description layout.
|
|
|
|
~~~~ C++
|
|
// The top level acceleration structure
|
|
m_descSetLayoutBind.emplace_back( //
|
|
vkDS(7, vkDT::eAccelerationStructureKHR, 1, vkSS::eFragment));
|
|
~~~~
|
|
|
|
In `HelloVulkan::updateDescriptorSet`, write the value to the descriptor set.
|
|
|
|
~~~~ C++
|
|
vk::WriteDescriptorSetAccelerationStructureKHR descASInfo;
|
|
descASInfo.setAccelerationStructureCount(1);
|
|
descASInfo.setPAccelerationStructures(&m_rtBuilder.getAccelerationStructure());
|
|
writes.emplace_back(nvvkpp::util::createWrite(m_descSet, m_descSetLayoutBind[7], &descASInfo));
|
|
~~~~
|
|
|
|
|
|
## Shader
|
|
|
|
The last modification is in the fragment shader, where we will add the ray intersection query to trace shadow rays.
|
|
|
|
First, the version has bumpped to 460
|
|
|
|
~~~~ C++
|
|
#version 460
|
|
~~~~
|
|
|
|
Then we need to add new extensions
|
|
|
|
~~~~ C++
|
|
#extension GL_EXT_ray_tracing : enable
|
|
#extension GL_EXT_ray_query : enable
|
|
~~~~
|
|
|
|
We have to add the layout to access the top level acceleration structure.
|
|
|
|
~~~~ C++
|
|
layout(binding = 7, set = 0) uniform accelerationStructureEXT topLevelAS;
|
|
~~~~
|
|
|
|
|
|
Ad the end of the shader, add the following code to initiate the ray query. As we are only interested to know if the ray
|
|
has hit something, we can keep the minimal.
|
|
|
|
~~~~ C++
|
|
// Ray Query for shadow
|
|
vec3 origin = worldPos;
|
|
vec3 direction = L; // vector to light
|
|
float tMin = 0.01f;
|
|
float tMax = lightDistance;
|
|
|
|
// Initializes a ray query object but does not start traversal
|
|
rayQueryEXT rayQuery;
|
|
rayQueryInitializeEXT(rayQuery, topLevelAS, gl_RayFlagsTerminateOnFirstHitEXT, 0xFF, origin, tMin,
|
|
direction, tMax);
|
|
|
|
// Start traversal: return false if traversal is complete
|
|
while(rayQueryProceedEXT(rayQuery))
|
|
{
|
|
}
|
|
|
|
// Returns type of committed (true) intersection
|
|
if(rayQueryGetIntersectionTypeEXT(rayQuery, true) != gl_RayQueryCommittedIntersectionNoneEXT)
|
|
{
|
|
// Got an intersection == Shadow
|
|
outColor *= 0.1;
|
|
}
|
|
~~~~
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
!!! Info Ray Query
|
|
Information about [Ray Query](https://github.com/KhronosGroup/GLSL/blob/master/extensions/ext/GLSL_EXT_ray_query.txt) extension.
|
|
|
|
# Final Code
|
|
|
|
You can find the final code in the folder [ray_tracing_reflections](https://github.com/nvpro-samples/vk_raytracing_tutorial/tree/master/ray_tracing_reflections)
|
|
|
|
|
|
<!-- Markdeep: -->
|
|
<link rel="stylesheet" href="vkrt_tutorial.css?">
|
|
<script> window.markdeepOptions = { tocStyle: "medium" };</script>
|
|
<script src="markdeep.min.js" charset="utf-8"></script>
|
|
<script src="https://developer.nvidia.com/sites/default/files/akamai/gameworks/whitepapers/markdeep.min.js" charset="utf-8"></script>
|
|
<script>
|
|
window.alreadyProcessedMarkdeep || (document.body.style.visibility = "visible")
|
|
</script>
|