LaunchParameters containing an array of dynamic size (lights data)

However, my device code keeps crashing because of an illegal memory access within this LightData array.

The launchParams.lights must be a pointer to GPU device memory. You cannot just assign the host side array pointer to that. That is not visible by the GPU.

Instead you need two more steps: Allocate the device memory with a CUDA runtime or driver API call to get a CUdeviceptr to a sufficiently large memory block.
Then you need to copy your lightData array data from host to device with a CUDA memcpy call.

Here is example code doing exactly that:
CUDA Runtime API: https://github.com/NVIDIA/OptiX_Apps/blob/master/apps/intro_runtime/src/Application.cpp#L2145
CUDA Driver API: https://github.com/NVIDIA/OptiX_Apps/blob/master/apps/intro_driver/src/Application.cpp#L2169

There is no “size()” call on these pointers, so you must also store the number of your light elements in another field in your launchParams.
That happens here in my examples:
https://github.com/NVIDIA/OptiX_Apps/blob/master/apps/intro_driver/shaders/system_parameter.h#L65
https://github.com/NVIDIA/OptiX_Apps/blob/master/apps/intro_driver/src/Application.cpp#L2185

A few lines further down that code, my host side SystemParameter structure is allocated on the device and then copied there:
https://github.com/NVIDIA/OptiX_Apps/blob/master/apps/intro_driver/src/Application.cpp#L2194
That copy is done everytime anything is changing inside the structure.

Then you should also be aware of the CUDA alignment requirements for vectors and arrays.
Follow the links in this post: https://forums.developer.nvidia.com/t/rtbuffer-indexing/167440/13

1 Like