Relationship of NVIDIA OptiX 7 programs

Can you please help me explain in detail the relationship between these eight programs in the ray tracing pipeline, starting from emitting a ray, how to go through these eight programs, and finally ending them.

For a start, please have a look at this thread:
https://forums.developer.nvidia.com/t/some-questions-about-ray/250279/2

That explains when raygeneration, intersection, anyhit, closesthit and miss programs are called and what can influence that with geometry flags during AS build, ray flags in optixTrace, and how anyhit programs can influence the traversal with optixTerminateRay and optixIgnoreIntersection.

It’s basically this:
The raygeneration program is the entry point of your pipeline and it can call optixTrace.
(Only some program domains are allowed to call optixTrace: raygen, closesthit, miss.)
Scene BVH traversal starts at the given traversable handle of the optixTrace call.
If there is any ray-AABB intersection, the intersection program is called. (The intersection program is implicit for built-in triangles.)
If there is a potential intersection reported, it calls into the optional anyhit program.
If the anyhit program calls optixIgnoreIntersection the ray.tmax value stays the same and scene traversal continues,
If the anyhit program calls optixTerminateRay, scene traversal stops and the closesthit program is called with that ray.tmax.
If there is no anyhit program, scene traversal continues until the closest intersection distance is found and set in ray.tmax and the closest hit program is called.
(In either anyhit or closesthit programs, the hit attribute registers of the reported intersection (e.g. two for barycentric coordinates on a triangle) provide the information allowing the calculation of the per-hit vertex attributes of your geometric primitive.)
If there is no closesthit program, nothing happens at that point.
If nothing is hit (or the anyhit program ignored all intersections) then the miss program is called.
If either the closesthit or miss program were reached (if provided or not), the ray traversal ends and returns to the code after the last called optixTrace.

If anything goes wrong with the optixTrace (illegal ray values, stack overflow, etc.), the optional or a default exception program is called. Control does not return to the location that triggered the exception, and execution of the launch index ends.

Callable programs are basically function tables in your pipeline which can be called by an index, which allows a flexible configuration of programs. Continuation callables can call optixTrace.

This table of device functions shows which functions may be called in what program domain.
If you check that table for the callables, that is probably what the above image wanted to express. Because continuation callable programs can call optixTrace, they are only allowed in raygen (where they don’t make much sense), closesthit and miss programs, while direct callable programs can be called in all program domains other than the exception program. That’s shown with the arrows in the above image.

感谢你的回答,那请帮我看一下,下面这种说法是否正确:
RayGen发射出一根光线后,Optix封装的底层就会开始依次枚举场景中的图元进行求交测试,每次枚举到图元后,如果它是三角形,就用内置的triangle intersection来求交,如果不是三角形(例如参数球体),就要用用户自定义的intersection shader来定义它的求交算法。如果求交失败,就枚举下一个图元;如果求交成功,对比一下这是不是最近的求交点,如果不是就抛弃,如果是就继续推进:判断图元是不是半透明的,如果是半透明的,那么就需要用户在any hit shader中定义它是否可以被认可为最近求交点,是否要抛弃;如果是不透明的(opaque),那么它就是我们认可的最近求交点,将他保留在hit committed维护(hit committed就是维护最近求交点的,如果发现更近的求交点就会更新)。接下来看一下是否有求交终止的命令,如果有的话就立刻结束一切求交任务,去closet shader计算最近求交点的着色以及弹射光线方向。

Hi @450422824,

The translation of your comment sounds approximately correct to me. Please Note that OptiX does not make any determination about what is opaque or translucent. That is entirely up to you, the user, to define & determine. The main distinction is that any-hit shaders are invoked for every intersection, while closest-hit shaders are invoked only for the nearest intersection point.

One useful way to think of the diagram you posted is that it’s a state machine. Each of the states is a little bit of code, and all the states together form the launch kernel. Some of the states are provided by OptiX (traversal & intersection), and some of the states are provided by you, the user (raygen, miss, closest-hit, etc.).


David.

Note that OptiX supports built-in triangle, sphere and different curve primitives already. You do not need to provide user-defined intersection programs for any of these built-in primitives. You only need to implement an intersection program for custom geometric primitives yourself.
There is no intersection program necessary for built-in triangles. That is automatically handled by OptiX and runs fully in hardware on RTX boards using the RT cores and an internal intersection program for all other boards.
For sphere and the different curve primitives you must get the built-in intersection program from OptiX with the optixBuiltinISModuleGet function and set it inside the hit record of the Shader Binding Table associated with the matching geometry acceleration structure containing these primitives.

OptiX will not enumerate the elements inside the scene. That sounds as if it checked all of them. Instead it will traverse through a bounding volume hierarchy (BVH) stored inside the acceleration structure you build with optixAccelBuild, starting at the traversableHandle in optixTrace to determine the intersected primitives as quickly as possible.

As David mentioned, OptiX doesn’t know anything about what happens inside your device program code.
You are responsible for implementing any desired behavior yourself, that means all material handling like translucency included.

Please read the relevant threads of these two forum searches for more information on anyhit programs and their common use cases like cutout opacity.
https://forums.developer.nvidia.com/search?q=anyhit%20%23visualization%3Aoptix
https://forums.developer.nvidia.com/search?q=cutout%20opacity%20%23visualization%3Aoptix

Generally, if you can avoid using anyhit programs, it’s a good idea to try if your raytracing algorithm is faster without them.