// This section is included inside the Shaders chapter (shaders.txt) [[shaders-raytracing-shaders]] [[shaders-ray-generation]] == Ray Generation Shaders A ray generation shader is similar to a compute shader. Its main purpose is to execute ray tracing queries using code:OpTraceNV instructions and process the results. [[shaders-ray-generation-execution]] === Ray Generation Shader Execution One ray generation shader is executed per ray tracing dispatch. Its location in the shader binding table (see <> for details) is passed directly into fname:vkCmdTraceRaysNV using the pname:raygenShaderBindingTableBuffer and pname:raygenShaderBindingOffset parameters. [[shaders-intersection]] == Intersection Shaders Intersection shaders enable the implementation of arbitrary, application defined geometric primitives. An intersection shader for a primitive is executed whenever its axis-aligned bounding box is hit by a ray. A built-in intersection shader for triangle primitives that is used automatically whenever geometry of type ename:VK_GEOMETRY_TYPE_TRIANGLES_NV is specified. Like other ray tracing shader domains, an intersection shader operates on a single ray at a time. It also operates on a single primitive at a time. It is therefore the purpose of an intersection shader to compute the ray-primitive intersections and report them. To report an intersection, the shader calls the code:OpReportIntersectionNV instruction. An intersection shader communicates with any-hit and closest shaders by generating attribute values that they can: read. Intersection shaders cannot: read or modify the ray payload. [[shaders-intersection-execution]] === Intersection Shader Execution The order in which intersections are found along a ray, and therefore the order in which intersection shaders are executed, is unspecified. The intersection shader of the closest AABB which intersects the ray is guaranteed to be executed at some point during traversal, unless the ray is forcibly terminated. [[shaders-any-hit]] == Any-Hit Shaders The any-hit shader is executed after the intersection shader reports an intersection that lies within the current [eq]#[tmin,tmax]# of the ray. The main use of any-hit shaders is to programmatically decide whether or not an intersection will be accepted. The intersection will be accepted unless the shader calls the code:OpIgnoreIntersectionNV instruction. [[shaders-any-hit-execution]] === Any-Hit Shader Execution The order in which intersections are found along a ray, and therefore the order in which any-hit shaders are executed, is unspecified. The any-hit shader of the closest hit is guaranteed to be executed at some point during traversal, unless the ray is forcibly terminated. [[shaders-closest-hit]] == Closest Hit Shaders Closest hit shaders have read-only access to the attributes generated by the corresponding intersection shader, and can: read or modify the ray payload. They also have access to a number of system-generated values. Closest hit shaders can: call code:OpTraceNV to recursively trace rays. [[shaders-closest-hit-execution]] === Closest Hit Shader Execution Exactly one closest hit shader is executed when traversal is finished and an intersection has been found and accepted. [[shaders-miss]] == Miss Shaders Miss shaders can: access the ray payload and can: trace new rays through the code:OpTraceNV instruction, but cannot: access attributes since they are not associated with an intersection. [[shaders-miss-execution]] === Miss Shader Execution A miss shader is executed instead of a closest hit shader if no intersection was found during traversal. [[shaders-callable]] == Callable Shaders Callable shaders can: access a callable payload that works similarly to ray payloads to do subroutine work. [[shaders-callable-execution]] === Callable Shader Execution A callable shader is executed by calling code:OpExecuteCallableNV from an allowed shader stage.