1e5c31af7Sopenharmony_ci// Copyright 2018-2024 The Khronos Group Inc. 2e5c31af7Sopenharmony_ci// 3e5c31af7Sopenharmony_ci// SPDX-License-Identifier: CC-BY-4.0 4e5c31af7Sopenharmony_ci 5e5c31af7Sopenharmony_ci[[ray-traversal]] 6e5c31af7Sopenharmony_ci= Ray Traversal 7e5c31af7Sopenharmony_ci 8e5c31af7Sopenharmony_ciThe ray traversal process identifies and handles intersections between a ray 9e5c31af7Sopenharmony_ciand geometries in an acceleration structure. 10e5c31af7Sopenharmony_ci 11e5c31af7Sopenharmony_ciRay traversal cannot be started by a Vulkan API command directly - a shader 12e5c31af7Sopenharmony_cimust execute 13e5c31af7Sopenharmony_ciifdef::VK_KHR_ray_query[code:OpRayQueryProceedKHR] 14e5c31af7Sopenharmony_ciifdef::VK_KHR_ray_query+VK_KHR_ray_tracing_pipeline[or] 15e5c31af7Sopenharmony_ciifdef::VK_KHR_ray_tracing_pipeline[a <<glossary-pipeline-trace-ray, pipeline trace ray>> instruction] 16e5c31af7Sopenharmony_ci. 17e5c31af7Sopenharmony_ciifdef::VK_KHR_ray_tracing_pipeline[] 18e5c31af7Sopenharmony_ciWhen the <<features-rayTracingPipeline, pname:rayTracingPipeline>> feature 19e5c31af7Sopenharmony_ciis enabled, code:OpTraceRayKHR can: be used for <<ray-tracing, ray tracing>> 20e5c31af7Sopenharmony_ciin a <<pipelines-ray-tracing, ray tracing pipeline>>. 21e5c31af7Sopenharmony_ciendif::VK_KHR_ray_tracing_pipeline[] 22e5c31af7Sopenharmony_ciifdef::VK_KHR_ray_query[] 23e5c31af7Sopenharmony_ciWhen the <<features-rayQuery, pname:rayQuery>> feature is enabled, 24e5c31af7Sopenharmony_cicode:OpRayQueryProceedKHR can: be used in any shader stage. 25e5c31af7Sopenharmony_ciendif::VK_KHR_ray_query[] 26e5c31af7Sopenharmony_ci 27e5c31af7Sopenharmony_ci 28e5c31af7Sopenharmony_ci[[ray-intersection-candidate-determination]] 29e5c31af7Sopenharmony_ci== Ray Intersection Candidate Determination 30e5c31af7Sopenharmony_ci 31e5c31af7Sopenharmony_ciOnce tracing begins, rays are first tested against instances in a top-level 32e5c31af7Sopenharmony_ciacceleration structure. 33e5c31af7Sopenharmony_ciA ray that intersects an instance will be transformed into the space of the 34e5c31af7Sopenharmony_ciinstance to continue traversal within that instance; therefore the transform 35e5c31af7Sopenharmony_cimatrix stored in the instance must be invertible. 36e5c31af7Sopenharmony_ci 37e5c31af7Sopenharmony_ciIn case multiple instances are intersected by a ray, the ray transformation 38e5c31af7Sopenharmony_ciinto the space of the instance is invariant under the order in which these 39e5c31af7Sopenharmony_ciinstances are encountered in the top-level acceleration structure. 40e5c31af7Sopenharmony_ci 41e5c31af7Sopenharmony_ci[NOTE] 42e5c31af7Sopenharmony_ci.Note 43e5c31af7Sopenharmony_ci==== 44e5c31af7Sopenharmony_ciApplying multiple forward and reverse transforms to a ray to transition from 45e5c31af7Sopenharmony_cione instance to another could result in accumulated errors. 46e5c31af7Sopenharmony_ciThus an implementation should behave as if the ray is transformed from the 47e5c31af7Sopenharmony_ciorigin for each instance independently. 48e5c31af7Sopenharmony_ci==== 49e5c31af7Sopenharmony_ci 50e5c31af7Sopenharmony_ciNext, rays are tested against geometries in an bottom-level acceleration 51e5c31af7Sopenharmony_cistructure to determine if a hit occurred between them, initially based only 52e5c31af7Sopenharmony_cion their geometric properties (i.e. their vertices). 53e5c31af7Sopenharmony_ciThe implementation performs similar operations to that of rasterization, but 54e5c31af7Sopenharmony_ciwith the effective viewport determined by the parameters of the ray, and the 55e5c31af7Sopenharmony_cigeometry transformed into a space determined by that viewport. 56e5c31af7Sopenharmony_ci 57e5c31af7Sopenharmony_ciThe vertices of each primitive are transformed from acceleration structure 58e5c31af7Sopenharmony_cispace #~as~# to ray space #~r~# according to the ray origin and direction as 59e5c31af7Sopenharmony_cifollows: 60e5c31af7Sopenharmony_ci 61e5c31af7Sopenharmony_ci[latexmath] 62e5c31af7Sopenharmony_ci++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 63e5c31af7Sopenharmony_ci\left( 64e5c31af7Sopenharmony_ci \begin{array}{c} 65e5c31af7Sopenharmony_ci x_{r} \\ 66e5c31af7Sopenharmony_ci y_{r}\\ 67e5c31af7Sopenharmony_ci z_{r} 68e5c31af7Sopenharmony_ci \end{array} 69e5c31af7Sopenharmony_ci\right) = 70e5c31af7Sopenharmony_ci\left( 71e5c31af7Sopenharmony_ci \begin{matrix} 72e5c31af7Sopenharmony_ci a_x^2(1-c) + c & a_xa_y(1-c) - sa_z & a_xa_z(1-c) + sa_y \\ 73e5c31af7Sopenharmony_ci a_xa_y(1-c) + sa_z & a_y^2(1-c) + c & a_ya_z(1-c) - sa_x \\ 74e5c31af7Sopenharmony_ci a_xa_z(1-c) - sa_y & a_ya_z(1-c) + sa_x & a_z^2(1-c) + c 75e5c31af7Sopenharmony_ci \end{matrix} 76e5c31af7Sopenharmony_ci\right) 77e5c31af7Sopenharmony_ci\left( 78e5c31af7Sopenharmony_ci \begin{array}{c} 79e5c31af7Sopenharmony_ci x_{as} - o_x \\ 80e5c31af7Sopenharmony_ci y_{as} - o_y \\ 81e5c31af7Sopenharmony_ci z_{as} - o_z 82e5c31af7Sopenharmony_ci \end{array} 83e5c31af7Sopenharmony_ci\right) 84e5c31af7Sopenharmony_ci++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 85e5c31af7Sopenharmony_ci 86e5c31af7Sopenharmony_cilatexmath:[\mathbf{a}] is the axis of rotation from the unnormalized ray 87e5c31af7Sopenharmony_cidirection vector latexmath:[\mathbf{d}] to the axis vector 88e5c31af7Sopenharmony_cilatexmath:[\mathbf{k}]: 89e5c31af7Sopenharmony_ci 90e5c31af7Sopenharmony_ci[latexmath] 91e5c31af7Sopenharmony_ci++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 92e5c31af7Sopenharmony_ci\mathbf{a} = \begin{cases} 93e5c31af7Sopenharmony_ci \frac{\mathbf{d} \times \mathbf{k}}{|| \mathbf{d} \times \mathbf{k} ||} & \mathrm{if}\; || \mathbf{d} \times \mathbf{k} || \ne 0 \\ 94e5c31af7Sopenharmony_ci \left(\begin{array}{c} 95e5c31af7Sopenharmony_ci 0 \\ 96e5c31af7Sopenharmony_ci 1 \\ 97e5c31af7Sopenharmony_ci 0 98e5c31af7Sopenharmony_ci \end{array} 99e5c31af7Sopenharmony_ci \right) & \mathrm{if}\; || \mathbf{d} \times \mathbf{k} || = 0 \\ 100e5c31af7Sopenharmony_ci \end{cases} 101e5c31af7Sopenharmony_ci++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 102e5c31af7Sopenharmony_ci 103e5c31af7Sopenharmony_cilatexmath:[\mathit{s}] and latexmath:[\mathit{c}] are the sine and cosine of 104e5c31af7Sopenharmony_cithe angle of rotation about latexmath:[\mathbf{a}] from 105e5c31af7Sopenharmony_cilatexmath:[\mathbf{d}] to latexmath:[\mathbf{k}]: 106e5c31af7Sopenharmony_ci 107e5c31af7Sopenharmony_ci[latexmath] 108e5c31af7Sopenharmony_ci++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 109e5c31af7Sopenharmony_ci\begin{aligned} 110e5c31af7Sopenharmony_cic &= {{\mathbf{d} \cdot \mathbf{k}}\over{||\mathbf{d}||}} \\ 111e5c31af7Sopenharmony_cis &= \sqrt{1 - c^2} 112e5c31af7Sopenharmony_ci\end{aligned} 113e5c31af7Sopenharmony_ci++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 114e5c31af7Sopenharmony_ci 115e5c31af7Sopenharmony_cilatexmath:[\mathbf{k}] is the unit vector: 116e5c31af7Sopenharmony_ci 117e5c31af7Sopenharmony_ci[latexmath] 118e5c31af7Sopenharmony_ci++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 119e5c31af7Sopenharmony_ci\mathbf{k} = \left( 120e5c31af7Sopenharmony_ci \begin{array}{c} 121e5c31af7Sopenharmony_ci 0 \\ 122e5c31af7Sopenharmony_ci 0 \\ 123e5c31af7Sopenharmony_ci -1 124e5c31af7Sopenharmony_ci \end{array} 125e5c31af7Sopenharmony_ci\right) 126e5c31af7Sopenharmony_ci++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 127e5c31af7Sopenharmony_ci 128e5c31af7Sopenharmony_cilatexmath:[\mathbf{o}] and latexmath:[\mathbf{d}] are the ray origin and 129e5c31af7Sopenharmony_ciunnormalized direction, respectively; the vector described by [eq]#x~as~#, 130e5c31af7Sopenharmony_ci[eq]#y~as~#, and [eq]#z~as~# is any position in acceleration structure 131e5c31af7Sopenharmony_cispace; and the vector described by [eq]#x~r~#, [eq]#y~r~#, and [eq]#z~r~# is 132e5c31af7Sopenharmony_cithe same position in ray space. 133e5c31af7Sopenharmony_ci 134e5c31af7Sopenharmony_ciAn _intersection candidate_ is a unique point of intersection between a ray 135e5c31af7Sopenharmony_ciand a geometric primitive. 136e5c31af7Sopenharmony_ciFor any primitive that has within its bounds a position 137e5c31af7Sopenharmony_cilatexmath:[\mathbf{xyz_{as}}] such that 138e5c31af7Sopenharmony_ci 139e5c31af7Sopenharmony_ci[latexmath] 140e5c31af7Sopenharmony_ci++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 141e5c31af7Sopenharmony_ci\begin{aligned} 142e5c31af7Sopenharmony_ci x_r &= 0 \\ 143e5c31af7Sopenharmony_ci y_r &= 0 \\ 144e5c31af7Sopenharmony_cit_\mathit{min} \lt {-{z_r}\over{||\mathbf{d}||}} &\lt t_\mathit{max} & \text{if the primitive is a triangle,} \\ 145e5c31af7Sopenharmony_cit_\mathit{min} \leq {-{z_r}\over{||\mathbf{d}||}} &\leq t_\mathit{max} & \text{otherwise} \\ 146e5c31af7Sopenharmony_ci\end{aligned} 147e5c31af7Sopenharmony_ci++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 148e5c31af7Sopenharmony_ci 149e5c31af7Sopenharmony_ci(where latexmath:[t = {-{z_r}\over{||\mathbf{d}||}}]), an intersection 150e5c31af7Sopenharmony_cicandidate exists. 151e5c31af7Sopenharmony_ci 152e5c31af7Sopenharmony_ciTriangle primitive bounds consist of all points on the plane formed by the 153e5c31af7Sopenharmony_cithree vertices and within the bounds of the edges between the vertices, 154e5c31af7Sopenharmony_cisubject to the watertightness constraints below. 155e5c31af7Sopenharmony_ciAABB primitive bounds consist of all points within an implementation-defined 156e5c31af7Sopenharmony_cibound which includes the specified box. 157e5c31af7Sopenharmony_ci 158e5c31af7Sopenharmony_ci[NOTE] 159e5c31af7Sopenharmony_ci.Note 160e5c31af7Sopenharmony_ci==== 161e5c31af7Sopenharmony_ciThe bounds of the AABB including all points internal to the bound implies 162e5c31af7Sopenharmony_cithat a ray started within the AABB will hit that AABB. 163e5c31af7Sopenharmony_ci==== 164e5c31af7Sopenharmony_ci 165e5c31af7Sopenharmony_ci[[raytraversal-ray-intersection-candidate-diagram]] 166e5c31af7Sopenharmony_ciimage::{images}/ray_intersection_candidate.svg[align="center",title="Ray intersection candidate",opts="{imageopts}"] 167e5c31af7Sopenharmony_ci 168e5c31af7Sopenharmony_ciThe determination of this condition is performed in an implementation 169e5c31af7Sopenharmony_cispecific manner, and may: be performed with floating point operations. 170e5c31af7Sopenharmony_ciDue to the complexity and number of operations involved, inaccuracies are 171e5c31af7Sopenharmony_ciexpected, particularly as the scale of values involved begins to diverge. 172e5c31af7Sopenharmony_ciImplementations should: take efforts to maintain as much precision as 173e5c31af7Sopenharmony_cipossible. 174e5c31af7Sopenharmony_ci 175e5c31af7Sopenharmony_ci[NOTE] 176e5c31af7Sopenharmony_ci.Note 177e5c31af7Sopenharmony_ci==== 178e5c31af7Sopenharmony_ciOne very common case is when geometries are close to each other at some 179e5c31af7Sopenharmony_cidistance from the origin in acceleration structure space, where an effect 180e5c31af7Sopenharmony_cisimilar to "`z-fighting`" is likely to be observed. 181e5c31af7Sopenharmony_ciApplications can mitigate this by ensuring their detailed geometries remain 182e5c31af7Sopenharmony_ciclose to the origin. 183e5c31af7Sopenharmony_ci 184e5c31af7Sopenharmony_ciAnother likely case is when the origin of a ray is set to a position on a 185e5c31af7Sopenharmony_cipreviously intersected surface, and its [eq]#t~min~# is zero or near zero; 186e5c31af7Sopenharmony_cian intersection may be detected on the emitting surface. 187e5c31af7Sopenharmony_ciThis case can usually be mitigated by offsetting [eq]#t~min~# slightly. 188e5c31af7Sopenharmony_ci==== 189e5c31af7Sopenharmony_ci 190e5c31af7Sopenharmony_ciifdef::VK_NV_ray_tracing_motion_blur[] 191e5c31af7Sopenharmony_ciFor a motion primitive or a motion instance, the positions for intersection 192e5c31af7Sopenharmony_ciare evaluated at the time specified in the code:time parameter to 193e5c31af7Sopenharmony_cicode:OpTraceRayMotionNV by interpolating between the two endpoints as 194e5c31af7Sopenharmony_cispecified for the given motion type. 195e5c31af7Sopenharmony_ciIf a motion acceleration structure is traced with code:OpTraceRayKHR, it 196e5c31af7Sopenharmony_cibehaves as a code:OpTraceRayMotionNV with code:time of 0.0. 197e5c31af7Sopenharmony_ciendif::VK_NV_ray_tracing_motion_blur[] 198e5c31af7Sopenharmony_ci 199e5c31af7Sopenharmony_ciIn the case of AABB geometries, implementations may: increase their size in 200e5c31af7Sopenharmony_cian acceleration structure in order to mitigate precision issues. 201e5c31af7Sopenharmony_ciThis may: result in false positive intersections being reported to the 202e5c31af7Sopenharmony_ciapplication. 203e5c31af7Sopenharmony_ci 204e5c31af7Sopenharmony_ciFor triangle intersection candidates, the [eq]#b# and [eq]#c# 205e5c31af7Sopenharmony_ci<<primsrast-polygon-barycentrics,barycentric coordinates>> on the triangle 206e5c31af7Sopenharmony_ciwhere the above condition is met are made available to future shading. 207e5c31af7Sopenharmony_ciifdef::VK_KHR_ray_tracing_pipeline[] 208e5c31af7Sopenharmony_ciIf the ray was traced with a <<glossary-pipeline-trace-ray, pipeline trace 209e5c31af7Sopenharmony_ciray>> instruction, these values are available as a vector of 2 32-bit 210e5c31af7Sopenharmony_cifloating point values in the code:HitAttributeKHR storage class. 211e5c31af7Sopenharmony_ciendif::VK_KHR_ray_tracing_pipeline[] 212e5c31af7Sopenharmony_ci 213e5c31af7Sopenharmony_ciOnce an intersection candidate is determined, it proceeds through the 214e5c31af7Sopenharmony_cifollowing operations, in order: 215e5c31af7Sopenharmony_ci 216e5c31af7Sopenharmony_ci . <<ray-intersection-culling>> 217e5c31af7Sopenharmony_ci . <<ray-intersection-confirmation>> 218e5c31af7Sopenharmony_ci . <<ray-closest-hit-determination>> 219e5c31af7Sopenharmony_ci . <<ray-result-determination>> 220e5c31af7Sopenharmony_ci 221e5c31af7Sopenharmony_ciThe sections below describe the exact details of these tests. 222e5c31af7Sopenharmony_ciThere is no ordering guarantee between operations performed on different 223e5c31af7Sopenharmony_ciintersection candidates. 224e5c31af7Sopenharmony_ci 225e5c31af7Sopenharmony_ci 226e5c31af7Sopenharmony_ci[[ray-traversal-watertight]] 227e5c31af7Sopenharmony_ci=== Watertightness 228e5c31af7Sopenharmony_ci 229e5c31af7Sopenharmony_ciFor a set of triangles with identical transforms, within a single instance: 230e5c31af7Sopenharmony_ci 231e5c31af7Sopenharmony_ci * Any set of two or more triangles where all triangles have one vertex 232e5c31af7Sopenharmony_ci with an identical position value, that vertex is a _shared vertex_. 233e5c31af7Sopenharmony_ci * Any set of two triangles with two shared vertices that were specified in 234e5c31af7Sopenharmony_ci the same <<drawing-triangle-lists, winding order>> in each triangle have 235e5c31af7Sopenharmony_ci a _shared edge_ defined by those vertices. 236e5c31af7Sopenharmony_ci 237e5c31af7Sopenharmony_ciA _closed fan_ is a set of three or more triangles where: 238e5c31af7Sopenharmony_ci 239e5c31af7Sopenharmony_ci * All triangles in the set have the same shared vertex as one of their 240e5c31af7Sopenharmony_ci vertices. 241e5c31af7Sopenharmony_ci * All edges that include the above vertex are shared edges. 242e5c31af7Sopenharmony_ci * All above shared edges are shared by exactly two triangles from the set. 243e5c31af7Sopenharmony_ci * No two triangles in the set intersect, except at shared edges. 244e5c31af7Sopenharmony_ci * Every triangle in the set is joined to every other triangle in the set 245e5c31af7Sopenharmony_ci by a series of the above shared edges. 246e5c31af7Sopenharmony_ci 247e5c31af7Sopenharmony_ciImplementations should: not double-hit or miss when a ray intersects a 248e5c31af7Sopenharmony_cishared edge, or a shared vertex of a closed fan. 249e5c31af7Sopenharmony_ci 250e5c31af7Sopenharmony_ci 251e5c31af7Sopenharmony_ci[[ray-intersection-culling]] 252e5c31af7Sopenharmony_ci== Ray Intersection Culling 253e5c31af7Sopenharmony_ci 254e5c31af7Sopenharmony_ciCandidate intersections go through several phases of culling before 255e5c31af7Sopenharmony_ciconfirmation as an actual hit. 256e5c31af7Sopenharmony_ciThere is no particular ordering dependency between the different culling 257e5c31af7Sopenharmony_cioperations. 258e5c31af7Sopenharmony_ci 259e5c31af7Sopenharmony_ci 260e5c31af7Sopenharmony_ci[[ray-traversal-culling-primitive]] 261e5c31af7Sopenharmony_ci=== Ray Primitive Culling 262e5c31af7Sopenharmony_ci 263e5c31af7Sopenharmony_ciIf the <<features-rayTraversalPrimitiveCulling, 264e5c31af7Sopenharmony_cipname:rayTraversalPrimitiveCulling>> or <<features-rayQuery, 265e5c31af7Sopenharmony_cipname:rayQuery>> features are enabled, the code:SkipTrianglesKHR and 266e5c31af7Sopenharmony_cicode:SkipAABBsKHR ray flags can: be specified when tracing a ray. 267e5c31af7Sopenharmony_cicode:SkipTrianglesKHR and code:SkipAABBsKHR are mutually exclusive. 268e5c31af7Sopenharmony_cicode:SkipTrianglesKHR is also mutually exclusive with 269e5c31af7Sopenharmony_cicode:CullBackFacingTrianglesKHR and code:CullFrontFacingTrianglesKHR. 270e5c31af7Sopenharmony_ci 271e5c31af7Sopenharmony_ciIf code:SkipTrianglesKHR was included in the `Ray Flags` operand of the ray 272e5c31af7Sopenharmony_citrace instruction, and the intersection is with a triangle primitive, the 273e5c31af7Sopenharmony_ciintersection is dropped, and no further processing of this intersection 274e5c31af7Sopenharmony_cioccurs. 275e5c31af7Sopenharmony_ciIf ename:VK_PIPELINE_CREATE_RAY_TRACING_SKIP_TRIANGLES_BIT_KHR was included 276e5c31af7Sopenharmony_ciin the pipeline, traversal with <<glossary-pipeline-trace-ray, pipeline 277e5c31af7Sopenharmony_citrace ray>> instructions will all behave as if code:SkipTrianglesKHR was 278e5c31af7Sopenharmony_ciincluded in their `Ray Flags` operand. 279e5c31af7Sopenharmony_ci 280e5c31af7Sopenharmony_ciIf code:SkipAABBsKHR was included in the `Ray Flags` operand of the ray 281e5c31af7Sopenharmony_citrace instruction, and the intersection is with an AABB primitive, the 282e5c31af7Sopenharmony_ciintersection is dropped, and no further processing of this intersection 283e5c31af7Sopenharmony_cioccurs. 284e5c31af7Sopenharmony_ciIf ename:VK_PIPELINE_CREATE_RAY_TRACING_SKIP_AABBS_BIT_KHR was included in 285e5c31af7Sopenharmony_cithe pipeline, traversal with <<glossary-pipeline-trace-ray, pipeline trace 286e5c31af7Sopenharmony_ciray>> instructions will all behave as if code:SkipAABBsKHR was included in 287e5c31af7Sopenharmony_citheir `Ray Flags` operand. 288e5c31af7Sopenharmony_ci 289e5c31af7Sopenharmony_ci 290e5c31af7Sopenharmony_ci=== Ray Mask Culling 291e5c31af7Sopenharmony_ci 292e5c31af7Sopenharmony_ciInstances can: be made invisible to particular rays based on the value of 293e5c31af7Sopenharmony_cislink:VkAccelerationStructureInstanceKHR::pname:mask used to add that 294e5c31af7Sopenharmony_ciinstance to a top-level acceleration structure, and the `Cull Mask` 295e5c31af7Sopenharmony_ciparameter used to trace the ray. 296e5c31af7Sopenharmony_ci 297e5c31af7Sopenharmony_ciFor the instance which is intersected, if [eq]#pname:mask & `Cull Mask` == 298e5c31af7Sopenharmony_ci0#, the intersection is dropped, and no further processing occurs. 299e5c31af7Sopenharmony_ci 300e5c31af7Sopenharmony_ci 301e5c31af7Sopenharmony_ci[[ray-traversal-culling-face]] 302e5c31af7Sopenharmony_ci=== Ray Face Culling 303e5c31af7Sopenharmony_ci 304e5c31af7Sopenharmony_ciAs in <<primsrast-polygons-basic,polygon rasterization>>, one of the stages 305e5c31af7Sopenharmony_ciof ray traversal is to determine if a triangle primitive is back- or 306e5c31af7Sopenharmony_cifront-facing, and primitives can: be culled based on that facing. 307e5c31af7Sopenharmony_ci 308e5c31af7Sopenharmony_ciIf the intersection candidate is with an AABB primitive, this operation is 309e5c31af7Sopenharmony_ciskipped. 310e5c31af7Sopenharmony_ci 311e5c31af7Sopenharmony_ci.Determination 312e5c31af7Sopenharmony_ci 313e5c31af7Sopenharmony_ciWhen a ray intersects a triangle primitive, the order that vertices are 314e5c31af7Sopenharmony_cispecified for the polygon affects whether the ray intersects the front or 315e5c31af7Sopenharmony_ciback face. 316e5c31af7Sopenharmony_ciFront or back facing is determined in the same way as they are for 317e5c31af7Sopenharmony_ci<<primsrast-polygons-basic,rasterization>>, based on the sign of the 318e5c31af7Sopenharmony_cipolygon's area but using the ray space coordinates instead of framebuffer 319e5c31af7Sopenharmony_cicoordinates. 320e5c31af7Sopenharmony_ciOne way to compute this area is: 321e5c31af7Sopenharmony_ci 322e5c31af7Sopenharmony_ci[latexmath] 323e5c31af7Sopenharmony_ci++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 324e5c31af7Sopenharmony_cia = -{1 \over 2}\sum_{i=0}^{n-1} 325e5c31af7Sopenharmony_ci x_r^i y_r^{i \oplus 1} - 326e5c31af7Sopenharmony_ci x_r^{i \oplus 1} y_r^i 327e5c31af7Sopenharmony_ci++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 328e5c31af7Sopenharmony_ci 329e5c31af7Sopenharmony_ciwhere latexmath:[x_r^i] and latexmath:[y_r^i] are the [eq]#x# and [eq]#y# 330e5c31af7Sopenharmony_ci<<ray-intersection-candidate-determination,ray space coordinates>> of the 331e5c31af7Sopenharmony_ci[eq]##i##th vertex of the [eq]#n#-vertex polygon (vertices are numbered 332e5c31af7Sopenharmony_cistarting at zero for the purposes of this computation) and [eq]#i {oplus} 1# 333e5c31af7Sopenharmony_ciis [eq]#(i {plus} 1) mod n#. 334e5c31af7Sopenharmony_ci 335e5c31af7Sopenharmony_ciBy default, if [eq]#a# is negative then the intersection is with the front 336e5c31af7Sopenharmony_ciface of the triangle, otherwise it is with the back face. 337e5c31af7Sopenharmony_ciIf ename:VK_GEOMETRY_INSTANCE_TRIANGLE_FLIP_FACING_BIT_KHR is included in 338e5c31af7Sopenharmony_cislink:VkAccelerationStructureInstanceKHR::pname:flags for the instance 339e5c31af7Sopenharmony_cicontaining the intersected triangle, this determination is reversed. 340e5c31af7Sopenharmony_ciAdditionally, if [eq]#a# is 0, the intersection candidate is treated as not 341e5c31af7Sopenharmony_ciintersecting with any face, irrespective of the sign. 342e5c31af7Sopenharmony_ci 343e5c31af7Sopenharmony_ci[NOTE] 344e5c31af7Sopenharmony_ci.Note 345e5c31af7Sopenharmony_ci==== 346e5c31af7Sopenharmony_ciIn a left-handed coordinate system, an intersection will be with the front 347e5c31af7Sopenharmony_ciface of a triangle if the vertices of the triangle, as defined in index 348e5c31af7Sopenharmony_ciorder, appear from the ray's perspective in a clockwise rotation order. 349e5c31af7Sopenharmony_ciename:VK_GEOMETRY_INSTANCE_TRIANGLE_FLIP_FACING_BIT_KHR was previously 350e5c31af7Sopenharmony_ciannotated as 351e5c31af7Sopenharmony_ciename:VK_GEOMETRY_INSTANCE_TRIANGLE_FRONT_COUNTERCLOCKWISE_BIT_KHR because 352e5c31af7Sopenharmony_ciof this. 353e5c31af7Sopenharmony_ci==== 354e5c31af7Sopenharmony_ci 355e5c31af7Sopenharmony_ciifdef::VK_KHR_ray_tracing_pipeline[] 356e5c31af7Sopenharmony_ciIf the ray was traced with a <<glossary-pipeline-trace-ray, pipeline trace 357e5c31af7Sopenharmony_ciray>> instruction, the code:HitKindKHR built-in is set to 358e5c31af7Sopenharmony_cicode:HitKindFrontFacingTriangleKHR if the intersection is with front-facing 359e5c31af7Sopenharmony_cigeometry, and code:HitKindBackFacingTriangleKHR if the intersection is with 360e5c31af7Sopenharmony_ciback-facing geometry, for shader stages considering this intersection. 361e5c31af7Sopenharmony_ciendif::VK_KHR_ray_tracing_pipeline[] 362e5c31af7Sopenharmony_ci 363e5c31af7Sopenharmony_ciifdef::VK_KHR_ray_query[] 364e5c31af7Sopenharmony_ciIf the ray was traced with code:OpRayQueryProceedKHR, 365e5c31af7Sopenharmony_cicode:OpRayQueryGetIntersectionFrontFaceKHR will return true for intersection 366e5c31af7Sopenharmony_cicandidates with front faces, or false for back faces. 367e5c31af7Sopenharmony_ciendif::VK_KHR_ray_query[] 368e5c31af7Sopenharmony_ci 369e5c31af7Sopenharmony_ci.Culling 370e5c31af7Sopenharmony_ci 371e5c31af7Sopenharmony_ciIf code:CullBackFacingTrianglesKHR was included in the `Ray Flags` parameter 372e5c31af7Sopenharmony_ciof the ray trace instruction, and the intersection is determined as with the 373e5c31af7Sopenharmony_ciback face of a triangle primitive, the intersection is dropped, and no 374e5c31af7Sopenharmony_cifurther processing of this intersection occurs. 375e5c31af7Sopenharmony_ci 376e5c31af7Sopenharmony_ciIf code:CullFrontFacingTrianglesKHR was included in the `Ray Flags` 377e5c31af7Sopenharmony_ciparameter of the ray trace instruction, and the intersection is determined 378e5c31af7Sopenharmony_cias with the front face of a triangle primitive, the intersection is dropped, 379e5c31af7Sopenharmony_ciand no further processing of this intersection occurs. 380e5c31af7Sopenharmony_ci 381e5c31af7Sopenharmony_ciThis culling is disabled if 382e5c31af7Sopenharmony_ciename:VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR was included 383e5c31af7Sopenharmony_ciin slink:VkAccelerationStructureInstanceKHR::pname:flags for the instance 384e5c31af7Sopenharmony_ciwhich the intersected geometry belongs to. 385e5c31af7Sopenharmony_ci 386e5c31af7Sopenharmony_ciIntersection candidates that have not intersected with any face ([eq]#a == 387e5c31af7Sopenharmony_ci0#) are unconditionally culled, irrespective of ray flags and geometry 388e5c31af7Sopenharmony_ciinstance flags. 389e5c31af7Sopenharmony_ci 390e5c31af7Sopenharmony_ciThe code:CullBackFacingTrianglesKHR and code:CullFrontFacingTrianglesKHR 391e5c31af7Sopenharmony_ci`Ray Flags` are mutually exclusive. 392e5c31af7Sopenharmony_ci 393e5c31af7Sopenharmony_ci 394e5c31af7Sopenharmony_ci=== Ray Opacity Culling 395e5c31af7Sopenharmony_ci 396e5c31af7Sopenharmony_ciEach geometry in the acceleration structure may: be considered either opaque 397e5c31af7Sopenharmony_cior not. 398e5c31af7Sopenharmony_ciOpaque geometries continue through traversal as normal, whereas non-opaque 399e5c31af7Sopenharmony_cigeometries need to be either confirmed or discarded by shader code. 400e5c31af7Sopenharmony_ciIntersection candidates can: also be culled based on their opacity. 401e5c31af7Sopenharmony_ci 402e5c31af7Sopenharmony_ci.Determination 403e5c31af7Sopenharmony_ci 404e5c31af7Sopenharmony_ciEach individual intersection candidate is initially determined as opaque if 405e5c31af7Sopenharmony_ciename:VK_GEOMETRY_OPAQUE_BIT_KHR was included in the 406e5c31af7Sopenharmony_cislink:VkAccelerationStructureGeometryKHR::pname:flags when the geometry it 407e5c31af7Sopenharmony_ciintersected with was built, otherwise it is considered non-opaque. 408e5c31af7Sopenharmony_ci 409e5c31af7Sopenharmony_ciifdef::VK_EXT_opacity_micromap[] 410e5c31af7Sopenharmony_ciIf the geometry includes an opacity micromap, the opacity of the 411e5c31af7Sopenharmony_ciintersection at this point is instead derived as described in 412e5c31af7Sopenharmony_ci<<ray-opacity-micromap,Ray Opacity Micromap>>. 413e5c31af7Sopenharmony_ciendif::VK_EXT_opacity_micromap[] 414e5c31af7Sopenharmony_ci 415e5c31af7Sopenharmony_ciifdef::VK_KHR_ray_tracing_pipeline[] 416e5c31af7Sopenharmony_ciIf the intersection candidate was generated by an <<shaders-intersection, 417e5c31af7Sopenharmony_ciintersection shader>>, the intersection is initially considered to have 418e5c31af7Sopenharmony_ciopacity matching the AABB candidate that it was generated from. 419e5c31af7Sopenharmony_ciendif::VK_KHR_ray_tracing_pipeline[] 420e5c31af7Sopenharmony_ci 421e5c31af7Sopenharmony_ciHowever, this opacity can be overridden when it is built into an instance. 422e5c31af7Sopenharmony_ciSetting ename:VK_GEOMETRY_INSTANCE_FORCE_OPAQUE_BIT_KHR in 423e5c31af7Sopenharmony_cislink:VkAccelerationStructureInstanceKHR::pname:flags will force all 424e5c31af7Sopenharmony_cigeometries in the instance to be considered opaque. 425e5c31af7Sopenharmony_ciSimilarly, setting ename:VK_GEOMETRY_INSTANCE_FORCE_NO_OPAQUE_BIT_KHR will 426e5c31af7Sopenharmony_ciforce all geometries in the instance to be considered non-opaque. 427e5c31af7Sopenharmony_ci 428e5c31af7Sopenharmony_ciThis can again be overridden by including code:OpaqueKHR or code:NoOpaqueKHR 429e5c31af7Sopenharmony_ciin the `Ray Flags` parameter when tracing a ray. 430e5c31af7Sopenharmony_cicode:OpaqueKHR forces all geometries to behave as if they are opaque, 431e5c31af7Sopenharmony_ciregardless of their build parameters. 432e5c31af7Sopenharmony_ciSimilarly, code:NoOpaqueKHR forces all geometries to behave as if they are 433e5c31af7Sopenharmony_cinon-opaque. 434e5c31af7Sopenharmony_ci 435e5c31af7Sopenharmony_ciifdef::VK_KHR_ray_query[] 436e5c31af7Sopenharmony_ciIf the ray was traced with code:OpRayQueryProceedKHR, to determine the 437e5c31af7Sopenharmony_ciopacity of AABB intersection candidates, 438e5c31af7Sopenharmony_cicode:OpRayQueryGetIntersectionCandidateAABBOpaqueKHR can: be used. 439e5c31af7Sopenharmony_ciThis instruction will return code:true for opaque intersection candidates, 440e5c31af7Sopenharmony_ciand code:false for non-opaque intersection candidates. 441e5c31af7Sopenharmony_ciendif::VK_KHR_ray_query[] 442e5c31af7Sopenharmony_ci 443e5c31af7Sopenharmony_ci.Culling 444e5c31af7Sopenharmony_ci 445e5c31af7Sopenharmony_ciIf code:CullOpaqueKHR is included in the `Ray Flags` parameter when tracing 446e5c31af7Sopenharmony_cia ray, an intersection with a geometry that is considered opaque is dropped, 447e5c31af7Sopenharmony_ciand no further processing occurs. 448e5c31af7Sopenharmony_ci 449e5c31af7Sopenharmony_ciIf code:CullNoOpaqueKHR is included in the `Ray Flags` parameter when 450e5c31af7Sopenharmony_citracing a ray, an intersection with a geometry that is considered non-opaque 451e5c31af7Sopenharmony_ciis dropped, and no further processing occurs. 452e5c31af7Sopenharmony_ci 453e5c31af7Sopenharmony_ciThe code:OpaqueKHR, code:NoOpaqueKHR, code:CullOpaqueKHR, and 454e5c31af7Sopenharmony_cicode:CullNoOpaqueKHR `Ray Flags` are mutually exclusive. 455e5c31af7Sopenharmony_ci 456e5c31af7Sopenharmony_ciifdef::VK_EXT_opacity_micromap[] 457e5c31af7Sopenharmony_ci[[ray-opacity-micromap]] 458e5c31af7Sopenharmony_ci=== Ray Opacity Micromap 459e5c31af7Sopenharmony_ci 460e5c31af7Sopenharmony_ciA ename:VK_GEOMETRY_TYPE_TRIANGLES_KHR geometry in the acceleration 461e5c31af7Sopenharmony_cistructure may: have an opacity micromap associated with it to give 462e5c31af7Sopenharmony_cifiner-grained opacity information. 463e5c31af7Sopenharmony_ci 464e5c31af7Sopenharmony_ciIf the intersection candidate is with a geometry with an associated opacity 465e5c31af7Sopenharmony_cimicromap and ename:VK_GEOMETRY_INSTANCE_DISABLE_OPACITY_MICROMAPS_EXT is not 466e5c31af7Sopenharmony_ciset in its instance then the micromap is used to determine geometry opacity 467e5c31af7Sopenharmony_ciinstead of the ename:VK_GEOMETRY_OPAQUE_BIT_KHR flag in the geometry. 468e5c31af7Sopenharmony_ci 469e5c31af7Sopenharmony_ciThe opacity information in the micromap object is accessed using the 470e5c31af7Sopenharmony_cicandidate intersection [eq]#u# and [eq]#v# coordinates. 471e5c31af7Sopenharmony_ciThe integer [eq]#u# and [eq]#v# are computed from [eq]#{lfloor}u{rfloor} 472e5c31af7Sopenharmony_ci{plus} {lfloor}v{rfloor}#, clamping [eq]#{lfloor}u{rfloor}# as needed to 473e5c31af7Sopenharmony_cikeep the sum less than or equal to [eq]#1 << subdivisionlevel#. 474e5c31af7Sopenharmony_ciThese values are mapped into a linear index with a space filling curve which 475e5c31af7Sopenharmony_ciis defined recursively by traversing into the sub-triangle nearest vertex 0, 476e5c31af7Sopenharmony_cithen the middle triangle with ordering flipped, then nearest vertex 1 then 477e5c31af7Sopenharmony_cinearest vertex 2. 478e5c31af7Sopenharmony_ci 479e5c31af7Sopenharmony_ciimage::{images}/micromap-subd.svg[align="center",title="Example ordering for micromap data",align="center",opts="{imageopts}"] 480e5c31af7Sopenharmony_ci 481e5c31af7Sopenharmony_ci[NOTE] 482e5c31af7Sopenharmony_ci.Note 483e5c31af7Sopenharmony_ci==== 484e5c31af7Sopenharmony_ciThis encoding is spatially coherent, purely hierarchical, and allows a 485e5c31af7Sopenharmony_cibit-parallel conversion between barycentric address and index values. 486e5c31af7Sopenharmony_ci 487e5c31af7Sopenharmony_ciSee the appendix for reference code implementing this mapping. 488e5c31af7Sopenharmony_ci==== 489e5c31af7Sopenharmony_ci 490e5c31af7Sopenharmony_ciThe result of the opacity micromap lookup and operations is to treat the 491e5c31af7Sopenharmony_ciintersection as opaque, non-opaque, or ignored. 492e5c31af7Sopenharmony_ciThe interpretation of the values depends on 493e5c31af7Sopenharmony_ciename:VK_GEOMETRY_INSTANCE_FORCE_OPACITY_MICROMAP_2_STATE_EXT in the 494e5c31af7Sopenharmony_ciinstance of the candidate intersection or 495e5c31af7Sopenharmony_ciename:ForceOpacityMicromap2StateEXT ray flags on the ray. 496e5c31af7Sopenharmony_ciIf either is set, the opacity micromap information is interpreted in 2 state 497e5c31af7Sopenharmony_cioverride mode. 498e5c31af7Sopenharmony_ciIf the result of the micromap lookup is to treat the intersection candidate 499e5c31af7Sopenharmony_cias ignored, no further processing of that candidate is done. 500e5c31af7Sopenharmony_ci 501e5c31af7Sopenharmony_ciIf the associated opacity micromap has format 502e5c31af7Sopenharmony_ciename:VK_OPACITY_MICROMAP_FORMAT_2_STATE_EXT, each element of the micromap 503e5c31af7Sopenharmony_ciis represented by a single bit at the index derived above. 504e5c31af7Sopenharmony_ci 505e5c31af7Sopenharmony_ciIf the associated opacity micromap has format 506e5c31af7Sopenharmony_ciename:VK_OPACITY_MICROMAP_FORMAT_4_STATE_EXT, each element is represented by 507e5c31af7Sopenharmony_cia two bit value at the index derived above. 508e5c31af7Sopenharmony_ci 509e5c31af7Sopenharmony_ci 510e5c31af7Sopenharmony_ci[options="header"] 511e5c31af7Sopenharmony_ci|==== 512e5c31af7Sopenharmony_ci| 4 State value | 2 State value | Special index value | 2 State override | Result 513e5c31af7Sopenharmony_ci| 0 | 0 | ename:VK_OPACITY_MICROMAP_SPECIAL_INDEX_FULLY_TRANSPARENT_EXT | Y | Ignored 514e5c31af7Sopenharmony_ci| 0 | 0 | ename:VK_OPACITY_MICROMAP_SPECIAL_INDEX_FULLY_TRANSPARENT_EXT | N | Ignored 515e5c31af7Sopenharmony_ci| 1 | 1 | ename:VK_OPACITY_MICROMAP_SPECIAL_INDEX_FULLY_OPAQUE_EXT | Y | Opaque 516e5c31af7Sopenharmony_ci| 1 | 1 | ename:VK_OPACITY_MICROMAP_SPECIAL_INDEX_FULLY_OPAQUE_EXT | N | Opaque 517e5c31af7Sopenharmony_ci| 2 | | ename:VK_OPACITY_MICROMAP_SPECIAL_INDEX_FULLY_UNKNOWN_TRANSPARENT_EXT | Y | Ignored 518e5c31af7Sopenharmony_ci| 2 | | ename:VK_OPACITY_MICROMAP_SPECIAL_INDEX_FULLY_UNKNOWN_TRANSPARENT_EXT | N | Non-opaque 519e5c31af7Sopenharmony_ci| 3 | | ename:VK_OPACITY_MICROMAP_SPECIAL_INDEX_FULLY_UNKNOWN_OPAQUE_EXT | Y | Opaque 520e5c31af7Sopenharmony_ci| 3 | | ename:VK_OPACITY_MICROMAP_SPECIAL_INDEX_FULLY_UNKNOWN_OPAQUE_EXT | N | Non-opaque 521e5c31af7Sopenharmony_ci|==== 522e5c31af7Sopenharmony_ci 523e5c31af7Sopenharmony_ci 524e5c31af7Sopenharmony_ciendif::VK_EXT_opacity_micromap[] 525e5c31af7Sopenharmony_ci 526e5c31af7Sopenharmony_ci 527e5c31af7Sopenharmony_ci[[ray-intersection-confirmation]] 528e5c31af7Sopenharmony_ci== Ray Intersection Confirmation 529e5c31af7Sopenharmony_ci 530e5c31af7Sopenharmony_ciDepending on the opacity of intersected geometry and whether it is a 531e5c31af7Sopenharmony_citriangle or an AABB, candidate intersections are further processed to 532e5c31af7Sopenharmony_cidetermine the eventual hit result. 533e5c31af7Sopenharmony_ciCandidates generated from AABB intersections run through the same 534e5c31af7Sopenharmony_ciconfirmation process as triangle hits. 535e5c31af7Sopenharmony_ci 536e5c31af7Sopenharmony_ci 537e5c31af7Sopenharmony_ci=== AABB Intersection Candidates 538e5c31af7Sopenharmony_ci 539e5c31af7Sopenharmony_ciFor an intersection candidate with an AABB geometry generated by 540e5c31af7Sopenharmony_ci<<ray-intersection-candidate-determination>>, shader code is executed to 541e5c31af7Sopenharmony_cidetermine whether any hits should be reported to the traversal 542e5c31af7Sopenharmony_ciinfrastructure; no further processing of this intersection candidate occurs. 543e5c31af7Sopenharmony_ciThe occurrence of an AABB intersection candidate does not guarantee the ray 544e5c31af7Sopenharmony_ciintersects the primitive bounds. 545e5c31af7Sopenharmony_ciTo avoid propagating false intersections the application should: verify the 546e5c31af7Sopenharmony_ciintersection candidate before reporting any hits. 547e5c31af7Sopenharmony_ci 548e5c31af7Sopenharmony_ciifdef::VK_KHR_ray_tracing_pipeline[] 549e5c31af7Sopenharmony_ciIf the ray was traced with a <<glossary-pipeline-trace-ray, pipeline trace 550e5c31af7Sopenharmony_ciray>> instruction, an <<shaders-intersection, intersection shader>> is 551e5c31af7Sopenharmony_ciinvoked from the <<shader-binding-table>> according to the 552e5c31af7Sopenharmony_ci<<shader-binding-table-indexing-rules, specified indexing>> for the 553e5c31af7Sopenharmony_ciintersected geometry. 554e5c31af7Sopenharmony_ciIf this shader calls code:OpReportIntersectionKHR, a new intersection 555e5c31af7Sopenharmony_cicandidate is generated as described 556e5c31af7Sopenharmony_ci<<aabb-intersection-candidate-generation, below>>. 557e5c31af7Sopenharmony_ciIf the intersection shader is ename:VK_SHADER_UNUSED_KHR (which is only 558e5c31af7Sopenharmony_ciallowed for a zero shader group) then no further processing of the 559e5c31af7Sopenharmony_ciintersection candidate occurs. 560e5c31af7Sopenharmony_ciendif::VK_KHR_ray_tracing_pipeline[] 561e5c31af7Sopenharmony_ci 562e5c31af7Sopenharmony_ci[[aabb-intersection-candidate-generation]] 563e5c31af7Sopenharmony_ciifdef::VK_KHR_ray_tracing_pipeline[] 564e5c31af7Sopenharmony_ciEach new candidate generated as a result of this processing is a generated 565e5c31af7Sopenharmony_ciintersection candidate that intersects the AABB geometry, with a [eq]#t# 566e5c31af7Sopenharmony_civalue equal to the `Hit` parameter of the code:OpReportIntersectionKHR 567e5c31af7Sopenharmony_ciinstruction. 568e5c31af7Sopenharmony_ciThe new generated candidate is then independently run through 569e5c31af7Sopenharmony_ci<<ray-intersection-confirmation>> as a 570e5c31af7Sopenharmony_ci<<ray-triangle-and-generated-intersection-candidates, generated 571e5c31af7Sopenharmony_ciintersection>>. 572e5c31af7Sopenharmony_ciendif::VK_KHR_ray_tracing_pipeline[] 573e5c31af7Sopenharmony_ci 574e5c31af7Sopenharmony_ciifdef::VK_KHR_ray_query[] 575e5c31af7Sopenharmony_ciIf the ray was traced with code:OpRayQueryProceedKHR, control is returned to 576e5c31af7Sopenharmony_cithe shader which executed code:OpRayQueryProceedKHR, returning code:true. 577e5c31af7Sopenharmony_ciThe resulting ray query has a candidate intersection type of 578e5c31af7Sopenharmony_cicode:RayQueryCandidateIntersectionAABBKHR. 579e5c31af7Sopenharmony_cicode:OpRayQueryGenerateIntersectionKHR can: be called to commit a new 580e5c31af7Sopenharmony_ciintersection candidate with committed intersection type of 581e5c31af7Sopenharmony_cicode:RayQueryCommittedIntersectionGeneratedKHR. 582e5c31af7Sopenharmony_ciFurther ray query processing can: be continued by executing 583e5c31af7Sopenharmony_cicode:OpRayQueryProceedKHR with the same ray query, or intersection can: be 584e5c31af7Sopenharmony_citerminated with code:OpRayQueryTerminateKHR. 585e5c31af7Sopenharmony_ciendif::VK_KHR_ray_query[] 586e5c31af7Sopenharmony_ciifdef::VK_KHR_ray_tracing_pipeline+VK_KHR_ray_query[] 587e5c31af7Sopenharmony_ciUnlike rays traced with a <<glossary-pipeline-trace-ray, pipeline trace 588e5c31af7Sopenharmony_ciray>> instruction, candidates generated in this way skip generated 589e5c31af7Sopenharmony_ciintersection candidate confirmation; applications should: make this 590e5c31af7Sopenharmony_cidetermination before generating the intersection. 591e5c31af7Sopenharmony_ciendif::VK_KHR_ray_tracing_pipeline+VK_KHR_ray_query[] 592e5c31af7Sopenharmony_ci 593e5c31af7Sopenharmony_ciThis operation may: be executed multiple times for the same intersection 594e5c31af7Sopenharmony_cicandidate. 595e5c31af7Sopenharmony_ci 596e5c31af7Sopenharmony_ci 597e5c31af7Sopenharmony_ci[[ray-triangle-and-generated-intersection-candidates]] 598e5c31af7Sopenharmony_ci=== Triangle and Generated Intersection Candidates 599e5c31af7Sopenharmony_ci 600e5c31af7Sopenharmony_ciFor triangle and <<aabb-intersection-candidate-generation, generated 601e5c31af7Sopenharmony_ciintersection candidates>>, additional shader code may: be executed based on 602e5c31af7Sopenharmony_cithe intersection's opacity. 603e5c31af7Sopenharmony_ci 604e5c31af7Sopenharmony_ciIf the intersection is opaque, the candidate is immediately confirmed as a 605e5c31af7Sopenharmony_civalid hit and passes to the next stage of processing. 606e5c31af7Sopenharmony_ci 607e5c31af7Sopenharmony_ciFor non-opaque intersection candidates, shader code is executed to determine 608e5c31af7Sopenharmony_ciwhether a hit occurred or not. 609e5c31af7Sopenharmony_ci 610e5c31af7Sopenharmony_ciifdef::VK_KHR_ray_tracing_pipeline[] 611e5c31af7Sopenharmony_ciIf the ray was traced with a <<glossary-pipeline-trace-ray, pipeline trace 612e5c31af7Sopenharmony_ciray>> instruction, an <<shaders-any-hit, any-hit shader>> is invoked from 613e5c31af7Sopenharmony_cithe <<shader-binding-table>> according to the specified indexing. 614e5c31af7Sopenharmony_ciIf this shader calls code:OpIgnoreIntersectionKHR, the candidate is dropped 615e5c31af7Sopenharmony_ciand no further processing of the candidate occurs. 616e5c31af7Sopenharmony_ciIf the <<shaders-any-hit, any-hit shader>> identified is 617e5c31af7Sopenharmony_ciename:VK_SHADER_UNUSED_KHR, the candidate is immediately confirmed as a 618e5c31af7Sopenharmony_civalid hit and passes to the next stage of processing. 619e5c31af7Sopenharmony_ciendif::VK_KHR_ray_tracing_pipeline[] 620e5c31af7Sopenharmony_ci 621e5c31af7Sopenharmony_ciifdef::VK_KHR_ray_query[] 622e5c31af7Sopenharmony_ciIf the ray was traced with code:OpRayQueryProceedKHR, control is returned to 623e5c31af7Sopenharmony_cithe shader which executed code:OpRayQueryProceedKHR, returning code:true. 624e5c31af7Sopenharmony_ciAs only triangle candidates participate in this operation with ray queries, 625e5c31af7Sopenharmony_cithe resulting candidate intersection type is always 626e5c31af7Sopenharmony_cicode:RayQueryCandidateIntersectionTriangleKHR. 627e5c31af7Sopenharmony_cicode:OpRayQueryConfirmIntersectionKHR can: be called on the ray query to 628e5c31af7Sopenharmony_ciconfirm the candidate as a hit with committed intersection type of 629e5c31af7Sopenharmony_cicode:RayQueryCommittedIntersectionTriangleKHR. 630e5c31af7Sopenharmony_ciFurther ray query processing can: be continued by executing 631e5c31af7Sopenharmony_cicode:OpRayQueryProceedKHR with the same ray query, or intersection can: be 632e5c31af7Sopenharmony_citerminated with code:OpRayQueryTerminateKHR. 633e5c31af7Sopenharmony_ciIf code:OpRayQueryConfirmIntersectionKHR has not been executed, the 634e5c31af7Sopenharmony_cicandidate is dropped and no further processing of the candidate occurs. 635e5c31af7Sopenharmony_ciendif::VK_KHR_ray_query[] 636e5c31af7Sopenharmony_ci 637e5c31af7Sopenharmony_ciThis operation may: be executed multiple times for the same intersection 638e5c31af7Sopenharmony_cicandidate unless ename:VK_GEOMETRY_NO_DUPLICATE_ANY_HIT_INVOCATION_BIT_KHR 639e5c31af7Sopenharmony_ciwas specified for the intersected geometry. 640e5c31af7Sopenharmony_ci 641e5c31af7Sopenharmony_ci 642e5c31af7Sopenharmony_ci[[ray-closest-hit-determination]] 643e5c31af7Sopenharmony_ci== Ray Closest Hit Determination 644e5c31af7Sopenharmony_ci 645e5c31af7Sopenharmony_ciUnless the ray was traced with the code:TerminateOnFirstHitKHR ray flag, the 646e5c31af7Sopenharmony_ciimplementation must: track the closest confirmed hit until all geometries 647e5c31af7Sopenharmony_cihave been tested and either confirmed or dropped. 648e5c31af7Sopenharmony_ci 649e5c31af7Sopenharmony_ciAfter an intersection candidate is confirmed, its [eq]#t# value is compared 650e5c31af7Sopenharmony_cito [eq]#t~max~# to determine which intersection is closer, where [eq]#t# is 651e5c31af7Sopenharmony_cithe parametric distance along the ray at which the intersection occurred. 652e5c31af7Sopenharmony_ci 653e5c31af7Sopenharmony_ci * If [eq]#t < t~max~#, [eq]#t~max~# is set to [eq]#t# and the candidate is 654e5c31af7Sopenharmony_ci set as the current closest hit. 655e5c31af7Sopenharmony_ci * If [eq]#t > t~max~#, the candidate is dropped and no further processing 656e5c31af7Sopenharmony_ci of that candidate occurs. 657e5c31af7Sopenharmony_ci * If [eq]#t = t~max~#, the candidate may: be set as the current closest 658e5c31af7Sopenharmony_ci hit or dropped. 659e5c31af7Sopenharmony_ci 660e5c31af7Sopenharmony_ciIf code:TerminateOnFirstHitKHR was included in the `Ray Flags` used to trace 661e5c31af7Sopenharmony_cithe ray, once the first hit is confirmed, the ray trace is terminated. 662e5c31af7Sopenharmony_ci 663e5c31af7Sopenharmony_ci 664e5c31af7Sopenharmony_ci[[ray-result-determination]] 665e5c31af7Sopenharmony_ci== Ray Result Determination 666e5c31af7Sopenharmony_ci 667e5c31af7Sopenharmony_ciOnce all candidates have finished processing the prior stages, or if the ray 668e5c31af7Sopenharmony_ciis forcibly terminated, the final result of the ray trace is determined. 669e5c31af7Sopenharmony_ci 670e5c31af7Sopenharmony_ciIf a closest hit result was identified by <<ray-closest-hit-determination>>, 671e5c31af7Sopenharmony_cia closest hit has occurred, otherwise the final result is a miss. 672e5c31af7Sopenharmony_ci 673e5c31af7Sopenharmony_ciifdef::VK_KHR_ray_tracing_pipeline[] 674e5c31af7Sopenharmony_ciFor rays traced with <<glossary-pipeline-trace-ray, pipeline trace ray>> 675e5c31af7Sopenharmony_ciinstructions which can: invoke a closest hit shader, if a closest hit result 676e5c31af7Sopenharmony_ciwas identified, a <<shaders-closest-hit, closest hit shader>> is invoked 677e5c31af7Sopenharmony_cifrom the <<shader-binding-table>> according to the 678e5c31af7Sopenharmony_ci<<shader-binding-table-indexing-rules, specified indexing>> for the 679e5c31af7Sopenharmony_ciintersected geometry. 680e5c31af7Sopenharmony_ciControl returns to the shader that executed the 681e5c31af7Sopenharmony_ci<<glossary-pipeline-trace-ray, pipeline trace ray>> instruction once this 682e5c31af7Sopenharmony_cishader returns. 683e5c31af7Sopenharmony_ciThis shader is skipped if either the ray flags included 684e5c31af7Sopenharmony_cicode:SkipClosestHitShaderKHR, or if the <<shaders-closest-hit, closest hit 685e5c31af7Sopenharmony_cishader>> identified is ename:VK_SHADER_UNUSED_KHR. 686e5c31af7Sopenharmony_ci 687e5c31af7Sopenharmony_ciFor rays traced with a <<glossary-pipeline-trace-ray, pipeline trace ray>> 688e5c31af7Sopenharmony_ciinstruction where no hit result was identified, the <<shaders-miss, miss 689e5c31af7Sopenharmony_cishader>> identified by the `Miss Index` parameter of the instruction is 690e5c31af7Sopenharmony_ciinvoked. 691e5c31af7Sopenharmony_ciControl returns to the shader that executed the pipeline trace ray 692e5c31af7Sopenharmony_ciinstruction once this shader returns. 693e5c31af7Sopenharmony_ciThis shader is skipped if the miss shader identified is 694e5c31af7Sopenharmony_ciename:VK_SHADER_UNUSED_KHR. 695e5c31af7Sopenharmony_ciendif::VK_KHR_ray_tracing_pipeline[] 696e5c31af7Sopenharmony_ci 697e5c31af7Sopenharmony_ciifdef::VK_KHR_ray_query[] 698e5c31af7Sopenharmony_ciIf the ray was traced with code:OpRayQueryProceedKHR, control is returned to 699e5c31af7Sopenharmony_cithe shader which executed code:OpRayQueryProceedKHR, returning code:false. 700e5c31af7Sopenharmony_ciIf a closest hit was identified by <<ray-closest-hit-determination>>, the 701e5c31af7Sopenharmony_ciray query will now have a committed intersection type of 702e5c31af7Sopenharmony_cicode:RayQueryCommittedIntersectionGeneratedKHR or 703e5c31af7Sopenharmony_cicode:RayQueryCommittedIntersectionTriangleKHR. 704e5c31af7Sopenharmony_ciIf no closest hit was identified, the committed intersection type will be 705e5c31af7Sopenharmony_cicode:RayQueryCommittedIntersectionNoneKHR. 706e5c31af7Sopenharmony_ci 707e5c31af7Sopenharmony_ciNo further processing of a ray query occurs after this result is determined. 708e5c31af7Sopenharmony_ciendif::VK_KHR_ray_query[] 709