bluenoise-raytracer/ray_tracing_rayquery
2020-11-24 16:26:50 +01:00
..
images New documentation 2020-08-31 16:57:10 +02:00
shaders Fixing image leak and computeDiffuse in Wavefront shading 2020-07-09 11:30:10 +02:00
CMakeLists.txt Adding more project #defines 2020-11-24 16:26:50 +01:00
hello_vulkan.cpp Using final KHR ray tracing extension: VK_KHR_acceleration_structure, VK_KHR_ray_tracing_pipeline and VK_KHR_ray_query 2020-11-23 11:33:51 +01:00
hello_vulkan.h Using final KHR ray tracing extension: VK_KHR_acceleration_structure, VK_KHR_ray_tracing_pipeline and VK_KHR_ray_query 2020-11-23 11:33:51 +01:00
main.cpp Using final KHR ray tracing extension: VK_KHR_acceleration_structure, VK_KHR_ray_tracing_pipeline and VK_KHR_ray_query 2020-11-23 11:33:51 +01:00
README.md Using final KHR ray tracing extension: VK_KHR_acceleration_structure, VK_KHR_ray_tracing_pipeline and VK_KHR_ray_query 2020-11-23 11:33:51 +01:00

Ray Query - Tutorial

Tutorial (Setup)

This is an extension of the Vulkan ray tracing tutorial.

This extension is allowing to execute ray intersection queries in any shader stages. In this example, we will add ray queries (GLSL_EXT_ray_query) 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 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:

// #VKRay
void                             initRayTracing();
nvvk::RaytracingBuilderKHR::Blas objectToVkGeometryKHR(const ObjModel& model);
void                             createBottomLevelAS();
void                             createTopLevelAS();

vk::PhysicalDeviceRayTracingPropertiesKHR m_rtProperties;
nvvk::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 to have access to the acceleration structure directly in the fragment shader.

// 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.

  vk::AccelerationStructureKHR                   tlas = m_rtBuilder.getAccelerationStructure();
  vk::WriteDescriptorSetAccelerationStructureKHR descASInfo;
  descASInfo.setAccelerationStructureCount(1);
  descASInfo.setPAccelerationStructures(&tlas);
  writes.emplace_back(m_descSetLayoutBind.makeWrite(m_descSet, 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

#version 460

Then we need to add new extensions

#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.

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.

// 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;
}