﻿# Embree ray queries: use closest-hit data and occlusion results for different questions

Choose Embree closest-hit or occlusion queries by the required result, initialize ray state explicitly and avoid treating a visibility test as a list of surface intersections.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-embree-closest-hit-occlusion-query-result-contract/
Language: en

## Choose the question before the query

A renderer may need the nearest surface for shading or only a decision about whether a light is blocked. These are different questions. A nearest-hit implementation can return geometry information that a visibility-only test does not need, while a visibility query can terminate without discovering the nearest accepted surface. Specify the required result before optimizing traversal so later shading code does not interpret an early-exit visibility outcome as a complete geometric intersection.

## Read the closest-hit result contract

The [closest-hit reference](https://github.com/RenderKit/embree/blob/423865d4a0b7ab6cf9bcf8e5df9cab31bcbfc1c6/doc/src/api/rtcIntersect1.md) describes `rtcIntersect1`, initialized ray state and the invalid geometry-ID sentinel. On a hit, the query updates hit fields and the ray's far parameter. On a miss, it leaves the provided data unchanged. Initialize the result sentinel before each intended query rather than allowing a previous hit to survive accidentally and appear to describe a new ray that actually missed.

## Interpret occlusion through its own signal

The [occlusion reference](https://github.com/RenderKit/embree/blob/423865d4a0b7ab6cf9bcf8e5df9cab31bcbfc1c6/doc/src/api/rtcOccluded1.md) documents `rtcOccluded1` and its negative-infinity far-value signal for a committed blocker. That outcome is not a physical hit distance. Preserve the original shadow-segment bounds separately if later logic needs them. Check initialization of direction, range, mask, time and flags, because an incorrect ray segment or visibility mask can produce a plausible unblocked result without testing the intended path to a light.

## Keep accepted blockers distinct from candidates

The [occlusion-filter documentation](https://github.com/RenderKit/embree/blob/423865d4a0b7ab6cf9bcf8e5df9cab31bcbfc1c6/doc/src/api/rtcSetGeometryOccludedFilterFunction.md) explains acceptance and rejection of candidate hits. Once a hit is committed, occlusion traversal terminates; it is not an interface for gathering successive intersections. A transparent cutout can reject a candidate, but collecting layered surfaces requires a method with the appropriate intersection contract. Do not reuse the occlusion result as though it were a sorted list of object boundaries.

## Validate both hit and miss paths

Use a scene with a nearby surface, a farther surface and a masked blocker. Test a fresh miss after a successful hit to reveal stale output state. The guides to [GPU BVH traversal](/journal/learn-gpu-bvh-traversal/) and [ray coherence](/journal/learn-ray-coherence-gpu-queues/) provide neighboring context. Correct query selection preserves the distinction between nearest geometry and accepted visibility while allowing the renderer to choose an appropriate execution backend and measure its actual performance.
