LaunchParameters containing an array of dynamic size (lights data)

Hey there, I am trying to pass an array of LightData (LightData*) to my Launch Parameters. My host code initializes an std::vector<LightData> lightData with the corresponding information for each light, and then I pass it to my launch parameters by assigning launchParams.lights = lightData.data(). However, my device code keeps crashing because of an illegal memory access within this LightData array. Before I make the call to OptiXLaunch, I make sure to update the space allocated for the launch parameters on my device by freeing the device_ptr, then reallocating space of size sizeof(launchParams). Could this be because my pipeline’s module is set up by passing the pipelineLaunchParamsVariableName, and at that point it just sees a LightData* in my launch parameter struct, not knowing how many elements the array will contain? What is the correct way to do this?

Kind regards,
Chuppa

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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.