In a ray-tracing pipeline, what do the ray-generation, closest-hit, any-hit and miss programs each do, and why does alpha-tested foliage need any-hit?
answer
- one program per ray event
- candidate hit versus accepted hit
- transparent texels on a leaf card
- mark everything else opaque
- binding table picks the program
basics
~20 sRay generation launches rays per pixel and writes results; closest-hit shades the nearest accepted hit; miss runs when nothing is hit; any-hit runs on candidate hits and can reject them. Foliage needs any-hit to ignore hits where its alpha texture is transparent.
solid answer
~40 sThe pipeline splits a ray's life into small programs. **Ray generation** runs once per launch element, usually per pixel: it builds rays, calls trace with a payload and writes the result. During traversal, **any-hit** runs on each candidate hit on non-opaque geometry and may ignore it; an **intersection** program tests custom primitives. When traversal ends, **closest-hit** shades the nearest accepted hit and may trace more rays, or **miss** returns the sky or marks a shadow ray unblocked. A leaf card's triangle covers transparent texels, so any-hit samples alpha and ignores the hit below the cutoff. Any-hit is costly and sees candidates in no fixed order, so other geometry is flagged opaque. The `shader binding table` maps each hit, by instance offset, geometry index and ray type, to its programs and parameters.
go deeper
Recall the four program names and when each runs: per pixel, on the nearest hit, on candidate hits, and when nothing is hit.
Explain how alpha-tested foliage uses any-hit to ignore transparent texels, why its order is unspecified, and why other geometry should be flagged opaque.
Show where pipeline cost hides: any-hit on large foliage areas, divergent hit shading across materials, and when inline queries are the cheaper choice.
Set standards for the team: which materials may use any-hit, how many hit programs the binding table carries, and where inline queries replace the full pipeline.
## The program kinds A ray-tracing pipeline splits the work of one ray into small programs, and the GPU runs the right one at each event during traversal. The model below is the one the common ray-tracing APIs share; their names for the pieces differ slightly. | Program | Runs when | Typical job | |---|---|---| | Ray generation | once per launch-grid element, usually one per pixel | builds rays, calls trace, writes the result | | Intersection | a ray enters the box of a custom primitive | tests a non-triangle shape such as a sphere or curve; triangles use a built-in test | | Any-hit | a candidate hit is found on non-opaque geometry | ignores the hit, accepts it, or accepts it and ends the search | | Closest-hit | traversal ends with an accepted hit | shades the nearest hit, may trace more rays | | Miss | traversal ends with no accepted hit | returns sky or environment light, or marks a shadow ray unblocked | Programs communicate through a small **payload**: a block of values the caller owns and each invoked program may read and write, such as a colour, a hit distance or a blocked flag. ## One ray's life 1. The ray-generation program computes a ray for its pixel and calls trace with a payload, a ray-type index and a mask. 2. Traversal walks the acceleration structures. For each candidate triangle on geometry marked non-opaque, the any-hit program runs and may ignore the hit. 3. When traversal finishes, the closest-hit program runs for the nearest accepted hit, or the miss program runs if nothing was accepted. 4. Closest-hit may trace further rays, such as a shadow ray towards a light, up to a recursion depth the application declares in advance. 5. Control returns to ray generation, which reads the payload and writes the pixel. ## Any-hit and alpha-tested foliage Leaves, grass and fences are often modelled as a few quads with an alpha texture: the triangle covers the whole card, but only some texels are leaf. The built-in triangle test hits the whole card, so without help a shadow ray is blocked by transparent texels and every leaf casts a rectangular shadow. - The any-hit program fetches the alpha texture at the hit's barycentric coordinates and **ignores the hit** when alpha is below the cutoff, so traversal continues to whatever lies behind. - Candidate hits reach any-hit in **no guaranteed order**, not nearest first, and traversal may skip candidates beyond the closest hit already accepted. - The same triangle may reach any-hit more than once unless the geometry asks the API to prevent it, so any-hit must not count or accumulate. - Any-hit is expensive: it interrupts fixed-function traversal to run programmable code, often with a texture fetch. Mark every geometry without cutouts as **opaque** so that any-hit is skipped for it. - **Shadow rays** can ask to end the search at the first accepted hit and skip closest-hit, because any blocker is enough. ## The shader binding table as a concept A scene has many materials, and each ray type, such as a camera ray or a shadow ray, needs different hit behaviour. The **shader binding table** is the lookup that maps a hit to the programs and parameters that handle it. - It is an array of **records**. A hit record names the closest-hit, any-hit and intersection programs for one kind of geometry and carries their parameters, such as a material's constants or resource references. - In the common model, the hit record is chosen by adding the instance's offset, the geometry's index within its bottom-level structure scaled by a stride, and a ray-type offset passed to trace. - Separate regions hold the ray-generation record and the miss records, usually one miss record per ray type. An offset that is wrong but still inside the table runs another material's programs, so it shows up as wrong shading on exactly the instances whose offsets are off. ## Inline ray queries Many APIs also let any shader stage trace a ray **inline**: the program steps through candidate hits itself and gets the closest one back. There is no binding table and there are no separate hit programs, which suits simple queries with one behaviour, such as shadows or ambient occlusion from a compute pass. The full pipeline suits rays that may hit any material, because the scheduler can group invocations that run the same hit program.
- What happens to performance if every mesh in the scene is left non-opaque?Every candidate hit on every triangle then stops fixed-function traversal to run the any-hit program, usually with a texture fetch, even on solid walls where the answer is always accept. Traversal becomes much slower and less coherent. Flag geometry and instances without cutouts as opaque so that any-hit is skipped, and keep alpha-tested materials to the meshes that need them.
- When would you trace with inline ray queries instead of the full pipeline?Inline queries suit simple, uniform questions such as a shadow or ambient-occlusion ray from a compute or fragment program, where every hit is handled the same way and no material shading runs. They avoid the binding table. The full pipeline suits rays that can hit any material, since the scheduler can regroup hit-program invocations instead of making one program branch over every material.
- Any-hit runs once, on the closest hit, after traversal has finished.
- Closest-hit runs for every triangle the ray passes through.
- Marking geometry opaque only affects blending, not tracing cost.
- The shader binding table stores the acceleration structure's nodes.
- Any-hit sees candidate hits strictly nearest first, so it can count layers.