Access variables between cu files

Hello together,

I’m pretty sure this question has been answered before but I think I miss the keyword to find the answer.

I have a ray generation program in a file raygen.cu and inside a variable seed:

RT_PROGRAM void function() {
// ...
unsigned int seed = ...;
//...
}

Now, all I want is to use this seed in another file.cu in a closest hit / any hit progam (read only):

RT_PROGRAM void closest_hit() {
// ...
// use seed from raygen.cu
// ...
}

How do I do that? Thank you in advance!

The random number seed should be per ray, which means it needs to be part of your per ray payload structure.
(That seed is not read only because it changes every time you draw a new random number.)

Example code for that OptiX API here:
https://github.com/nvpro-samples/optix_advanced_samples/blob/master/src/optixIntroduction/optixIntro_07/shaders/per_ray_data.h#L97
Initialization in the raygeneration program:
https://github.com/nvpro-samples/optix_advanced_samples/blob/master/src/optixIntroduction/optixIntro_07/shaders/raygeneration.cu#L157
Access inside the closest hit program (e.g. for explicit light sampling):
https://github.com/nvpro-samples/optix_advanced_samples/blob/master/src/optixIntroduction/optixIntro_07/shaders/closesthit.cu#L122

Very similar for the OptiX 7.x API, shown in these ported examples: https://github.com/NVIDIA/OptiX_Apps

(BTW, if you need global read-only variables, with the old OptiX API these would normally be stored in variables at the Context scope which is accessible in all program domains. In the OptiX 7.x API that would be inside the constant launch parameter structure.)

That solved all problems, thank you!

1 Like