I’m currently working on using the PTX backend to inline MDL code into OptiX shaders, and I am encountering some issues when trying to use one link unit for multiple materials and could use some advice.
To provide some context, I’m setting the “visible_functions” option to the specific function I intend to call in the OptiX renderer. I’m able to retrieve the argument block data from the target code correctly, and when using direct callables (instead of inlining MDL calls) with multiple materials in a single link unit, the code works as expected. Also, I get no error when inlining the mdl calls and use one link unit per material.
However, when I attempt to include multiple materials into a single link unit, the target code only seems to produce a single function (given the specified visible function name), irrespective of the number of materials in the link unit. This doesn’t result in crashes, but the rendered output seems off, indicating what looks like a mismatch between what the inlined target code except as argument data in the shader vs. what I am passing to the function given the output of the link unit translation, for example, most materials appear to have black textures.
Is this behavior expected, or does it suggest an issue with my implementation?
Thank you for your assistance.
as shown in the OptiX 7 example, it is supposed to work like this:
You have (for example) a closest hit shader defined as
extern "C" __global__ void __closesthit__radiance()
where you call functions declared like
extern "C" __device__ mi::neuraylib::Bsdf_init_function mdlcode_init;
extern "C" __device__ mi::neuraylib::Bsdf_sample_function mdlcode_sample;
extern "C" __device__ mi::neuraylib::Bsdf_evaluate_function mdlcode_evaluate;
In the link unit, you requested code for a BSDF to be generated with the base name “mdlcode” for one material.
If you then set the “llvm_renderer_module” option to the LLVM bitcode of the closest hit shader module and the “visible_functions” option to “__closesthit__radiance”, the MDL code generator marks all functions not mentioned in the visible_functions list as internal, thus the PTX should in our case only export the “__closesthit__radiance” function.
If you additionally enabled the “inline_aggressively” option, the generated functions will have been inlined into your closesthit shader code.
This way you get one specialized closesthit shader per material. If you want to use multiple materials, here you need to use multiple link units as shown in the OptiX 7 example.
If you really want to do this with multiple materials in the same link unit, you would have to add calls to different generated functions in your closesthit shader (like to mdlcode_2_init, mdlcode_3_init, if you chose these names when you added the other materials to the link unit) or duplicate the closesthit shader and let each copy call different generated functions as well. But this sounds much more complicated than just using one link unit per material.
Is there a special reason, you want to put multiple materials in the same link unit?