OptiX 6, clear output buffer

Hi all,

I use OptiX to make some computation, and I need to get back some data. For this I use an output buffer, like this:

optix::Buffer outBuffer;
outBuffer=context->createBuffer(RT_BUFFER_OUTPUT,
                                RT_FORMAT_USER,
                                N*N);
outBuffer->setElementSize(sizeof(outData));
context["outBuffer"]->set(outBuffer);

My problem is, between two Optix runs, this buffer is not cleared, this mean, data from the first run are still present after the second run.

I achieved to clear the buffer with the code below, but I had to set the buffer as “RT_BUFFER_INPUT_OUTPUT”, which is not really correct.

optix::Buffer outBuffer;
outData* pointerOutData;
outBuffer=context["outBuffer"]->getBuffer();
pointerOutData = (outData*)outBuffer->map(0,
                            RT_BUFFER_MAP_WRITE_DISCARD);
memset(pointerOutData, 0, N*N*sizeof(outData));
outBuffer->unmap();

Is there a way to clear an output buffer, without mapping it on the host and doing a “memset” on it?

Thanks,
Arnaud

Depending on your algorithm there are two simple ways to do that with the GPU.

If you’re not doing any scattered writes and the buffer size == launch dimension, you can simply clear resp. initialize with the first calculation.
Something like this in an iterative accumulation:
https://github.com/nvpro-samples/optix_advanced_samples/blob/master/src/optixIntroduction/optixIntro_04/shaders/raygeneration.cu#L138

If you’re programming some scatter algorithm and need all data to be initialized before starting the calculation, you can implement a specific ray generation program which is just filling the output buffer and launch that with the buffer size dimension explicitly by selecting it with the entry point argument in rtContextLaunch().
Note that the first time you invoke an entry point the device kernel gets compiled for that. Means the very first launch will take longer than the following invocations
Basically this:
https://github.com/nvpro-samples/optix_advanced_samples/blob/master/src/optixIntroduction/optixIntro_01/shaders/raygeneration.cu

(In OptiX 7 latter could simply be done with CUDA runtime API cudaMemset*() resp. CUDA driver API cuMemset*() functions on the device pointer.)

Thanks for your answer.

While the buffer size is different from the launch dimension, I tried the second method you proposed. And it works fine.