# Any hit shader result

**URL:** <https://forums.developer.nvidia.com/t/any-hit-shader-result/281836>\
**Category:** OptiX\
**Created:** [February 5, 2024, 2:17pm UTC](https://forums.developer.nvidia.com/t/any-hit-shader-result/281836 "2024-02-05T14:17:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aister91](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@aister91](https://forums.developer.nvidia.com/u/aister91)\
**Post date:** [February 5, 2024, 2:17pm UTC](https://forums.developer.nvidia.com/t/any-hit-shader-result/281836/1 "2024-02-05T14:17:53Z")

</div>

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](https://global.discourse-cdn.com/nvidia/original/4X/0/e/5/0e5b6db1035bfa7fbd179906ef1b0c372dcadce4.jpeg)  
 ![image](https://global.discourse-cdn.com/nvidia/original/4X/3/7/a/37a38859150dc9553ea41a3d25bcb4f068baa1f4.jpeg)

---

<div class="post-metadata">

**Author:** ![droettger](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/droettger/32/14043_2.png) [@droettger](https://forums.developer.nvidia.com/u/droettger)\
**Post date:** [February 5, 2024, 2:55pm UTC](https://forums.developer.nvidia.com/t/any-hit-shader-result/281836/2 "2024-02-05T14:55:35Z")

</div>

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](https://raytracing-docs.nvidia.com/optix8/api/group __optix__ types.html#ga1405d971960ccde46cc2c5771f34fc76)

Explained here: [https://raytracing-docs.nvidia.com/optix8/guide/index.html#acceleration\_structures#6101](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.)

---

<div class="post-metadata">

**Author:** ![aister91](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@aister91](https://forums.developer.nvidia.com/u/aister91)\
**Post date:** [February 5, 2024, 9:45pm UTC](https://forums.developer.nvidia.com/t/any-hit-shader-result/281836/3 "2024-02-05T21:45:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/system/32/68080_2.png) [@system](https://forums.developer.nvidia.com/u/system)\
**Post date:** [February 19, 2024, 9:46pm UTC](https://forums.developer.nvidia.com/t/any-hit-shader-result/281836/4 "2024-02-19T21:46:30Z")

</div>

Closed on February 19, 2024, 9:46pm UTC

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