[Optix 7] Sun and sky model integration


What’s the recommended way to integrate the MDL sun and sky model into an Optix path tracer?

Here is the MDL function source: MDL-SDK/base.mdl at 7565abebff3f591b8c4b7aec586a9020b5f915c1 · NVIDIA/MDL-SDK · GitHub

It returns an “texture_return” type.


Lets start with the easy part: The texture_return type is defined in the ::base module and is used by all ::base texture functions as the return value type of a texture lookup.
Basically, it looks like this:

struct texture_return {
color tint;
float mono;

Functions from ::base return the “full color” result in the tint field, and a “monochrome” result in the mono field. To use ::base::sun_and_sky() in the OptiX example, you must compile it as an environment function. mi::neuraylib::ILink_unit contains the method add_environment() for this purpose.

The first argument of add_environment() is a mi::neuraylib::IFunction_call. So, you must retrieve the mi::neuraylib::IFunction_definition of the function that should be used as an environment and create a function call from it by binding its parameters to any expressions. Note that add_environment() accepts functions returning a color or a ::base::texture_result value (which is then automatically converted to a color by selecting the tint field).

Once you added your environment function to the link unit, the translate_link_unit() will create an entry point in the generated PTX file which basically encapsulates your function call into a function of signature color (*)(mi::neuraylib::Shading_state_environment *state). Note that the available state inside environment functions is NOT mi::neuraylib::Shading_state_material.

Now you can call your compiled environment function from the OptiX miss shader. Set up the Shading_state_environment (by setting the direction and the read-only environment just like for the closest hit) and return the color from the call.

Hope that helps