Help with OpenGL buffers


In my application, I have a geometry with a variable number of polygons.

I display the polygons using 3 arrays: vertex, normal and color.
These are computed in Cuda.

When I change the number of polygons, all these arrays change size. In OpenGl,
a simple call to glBufferData(…) is enough to resize the buffers, but then
I need to “re-bind” them with Cuda.

For this is use (for the vertex):


// --- unregister the CUDA buffer ---
cudaGraphicsUnregisterResource( cuda_vbo_vertex ) ;

// --- resize the OpenGL buffer ---
glBindBuffer( GL_ARRAY_BUFFER, Cuda_OGL_Vertex ) ;
glBufferData( GL_ARRAY_BUFFER, size * sizeof(float), 0, GL_DYNAMIC_DRAW ) ;
glBindBuffer( GL_ARRAY_BUFFER, 0 ) ;

// --- register this buffer object with CUDA ---
cudaGraphicsGLRegisterBuffer( &cuda_vbo_vertex, Cuda_OGL_Vertex, cudaGraphicsMapFlagsWriteDiscard ) ;


(for the sake of clarity I removed all error checking here)

For some reason, this only work if I increase the size of the buffer.
When I start to decrease it’s size, eventually the next call to my Cuda function give me
“an illegal memory access was encountered”

So, my work around is to never decrease the size of the buffers, but I’d rather understand what I’m doing wrong.

I use Cuda 7.5

Any ideas?