Resize buffers

Hi all,
I have crashes at launch after resizing buffers. I have several buffers that I have to resize from time to time. For example, I have this buffer I have created as

optix::Buffer b = context->createBuffer(RT_BUFFER_OUTPUT, RT_FORMAT_USER, receivers, transmitters);
	b->setElementSize(sizeof(ReceptionInfo));
	context["receptionInfoBuffer"]->set(b);
receptionInfoBuffer=b;

I also have a buffer of buffers, created as

optix::Buffer  internalRaysBuffer = context->createBuffer(RT_BUFFER_INPUT, RT_FORMAT_BUFFER_ID, receivers);
	int* buffers = static_cast<int*>(internalRaysBuffer->map());
	for (size_t i = 0; i < receivers; i++)
	{
		optix::Buffer aux = context->createBuffer(RT_BUFFER_INPUT_OUTPUT | RT_BUFFER_GPU_LOCAL, RT_FORMAT_INT, raySphere.elevationSteps, raySphere.azimuthSteps, transmitters);
		
		buffers[i] = aux->getId();

	}
	internalRaysBuffer->unmap();
	context["internalRaysBuffer"]->set(internalRaysBuffer);

I can launch and everything goes well. Then I have to resize. According to the documentation, if I am not missing anything, I could resize the buffer simply as

receptionInfoBuffer->setSize(newReceivers, 1u);

for instance.
And for the buffer of buffers I do

internalRaysBuffer->setSize(newReceivers);
			int* buffers = static_cast<int*>(internalRaysBuffer->map());
			for (unsigned int i = oldReceivers; i < newReceivers; i++)
			{
				
				optix::Buffer aux = context->createBuffer(RT_BUFFER_INPUT_OUTPUT | RT_BUFFER_GPU_LOCAL, RT_FORMAT_INT, raySphere.elevationSteps, raySphere.azimuthSteps, 1u);
				
				buffers[i] = aux->getId();
			}
			internalRaysBuffer->unmap();

But then if I launch again I have a crash with message error code -1 and message Unknown error (Details: Function “_rtContextLaunch3D” caught exception: Encountered a CUDA error: cudaDriver().CuMemcpyDtoHAsync( dstHost, srcDevice, byteCount, hStream.get() ) returned (719): Launch failed)

I have tried setting again the element size, setting the buffer in the context again and all of this result in the same crashes.

However, if I just destroy() the buffers and create them again, everything works well.

How should I resize the buffers? It is more efficient to resize them or can I just destroy and recreate them?
Thanks a lot.

Changing the buffer size with setSize() like that should work. That basically happens with all viewport related buffers when reshaping a window in any example.

But that is not a “resize” operation. You’re assuming the data is still defined after the setSize() call, which is not the case. It can be a completely new allocation. None of my applications assumes that the data is persistent and they work.

Does it work if you fill the buffer of buffer IDs with all aux buffer IDs again instead of only appending the new ones?

Mind that it’s your responsibility to manage all OptiX objects you create to be able to destroy them again or get their ID. Means your for-loop creating the aux buffers should actually track all created buffers, like in a std::vector. That way you could also get their IDs again to fill the newly sized buffer of IDs.

The first questions which come to mind are:
Why do you need to grow that buffer at all?
How often does that happen?
Do you know what the maximum size is going to be?
Then you could just create it at that capacity and track the actually valid cells in a separate variable which counts the number of aux buffer IDs you use.

“Does it work if you fill the buffer of buffer IDs with all aux buffer IDs again instead of only appending the new ones?”
Yes, that was the problem. Now it works. Thank you, Detlef.

“But that is not a “resize” operation” May I suggest that you clarify it in the docs, it explicitly says “The width of the resized buffer”

So, related to the efficiency, if it is a new allocation, do we get any gains by resizing instead of recreating?
And
“Why do you need to grow that buffer at all?”
I have to add and remove receivers/transmitters and I use those buffers to hold the results. The data the buffer holds is not persistent between launches, but the size of the buffer changes.
“How often does that happen?”
It is not frequent, but it may occur in bursts, like adding 10 new receivers and then some minutes later removing a few, then after some minutes, adding another burst. I can add all of them together or one after the other.
“Do you know what the maximum size is going to be?”
Yes, or at least I can estimate it.

Yes, I’ve let our technical writer know about the confusing wording in the OptiX API Reference and we’ll clarify that.