Any hit shader result

Hi,
I want to implement PIP algorithms with OptiX.
I set the query point list to ray origin and triangle polygon to traversable.

When I use any_hit shader and set the ray direction to (1,0,0), sometimes noise exists.

It is just a simple shader.

Any_hit program:

payload.hitCount++;
optixIgnoreIntersection();

result:
(payload.hitCount%2 == 1) ? 1:0

In my thought, the result is the same regardless of ray direction.
However, the real result is different.

Why does this problem?

image
image

The anyhit programs are called as often as there are successful intersection tests with a geometric primitive.
The acceleration structures in OptiX are built with a splitting BVH algorithm by default which means that the same geometric primitive can appear in multiple BVH nodes and can be intersected multiple times along a single ray!

Algorithms which do counting inside anyhit programs are not going to work with that default.
To prevent that, the acceleration structure must be built with the OptixGeometryFlags OPTIX_GEOMETRY_FLAG_REQUIRE_SINGLE_ANYHIT_CALL

Explained here: https://raytracing-docs.nvidia.com/optix8/guide/index.html#acceleration_structures#6101

Note that intersection program and therefore anyhit program invocations are not happening in ray direction order but in BVH traversal order (which is view and implementation dependent), but in your case of ignoring all hits, all primitives along the tested ray interval should reach the anyhit program and then would finally call into the miss program or return to the raygen program when there is no miss program assigned to that ray type.

(There is no need for that ternary operator and the modulo in this line: int result = (payload.hitCount % 2 == 1) ? 1 : 0;
That’s identical to int result = payload.hitCount & 1; which is faster.)

1 Like

When using the input flag options,

triangleInputFlags = OPTIX_GEOMETRY_FLAG_REQUIRE_SINGLE_ANYHIT_CALL;
triangleInput.triangleArray.flags = &triangleInputFlags;

The results are as intended. Thanks! @droettger

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.