View Full Version : CUDA CANNOT perceive changing VBO with glMapBuffer

01-23-2011, 01:21 PM

From my code I have just found a problem with vbo and cuda,

1) I change the data in VBO with opengl commands:

glBindBuffer( GL_ARRAY_BUFFER, _vbo_id );
GLfloat* unitdata = static_cast<GLfloat*>( glMapBuffer( GL_ARRAY_BUFFER, GL_READ_WRITE ) );
xxxx; /* change this VBO with pointer unitdata at host side */
glUnmapBuffer( GL_ARRAY_BUFFER );
glBindBuffer( GL_ARRAY_BUFFER, 0 );

2) Then I wanna read this VBO from cuda:

cudaGraphicsMapResources( 1, &amp;cgr, 0 ); /* cgr has already been initialized( registered with the corresponding VBO ID ) at the initialization phase */
cudaGraphicsResourceGetMappedPointer( &amp;pdata, &amp;datasizeinbyte, cgr );
xxxx; /* using this vbo's data in cuda(kernel) throught this mapped pointer */
cudaGraphicsUnmapResources( 1, &amp;cgr, 0 );

But by my program, every time after I have changed the vbo with opengl, the data read from cuda remains unchanged. I am really confused about that, I have printed the data (coppied first to host memory side) every time I use the mapped pointer in kernel.

Please help me, thanks very much!

01-23-2011, 11:23 PM
Try add GL.Finish before cudaGraphicsMapResources.
May be OGL command queue not executed buffer transferring at moment when CUDA map buffer.
Or use glMapBufferRange, glFlushMappedBufferRange with suitable set of flags.

01-24-2011, 02:25 AM
Thanks for your quick reply

I tried glFinish(), but it doesn't work

What I am very confused is that the shown geometry, which is bound to this VBO, changes after the call glMapBuffer(), this means the data in the VBO should have been changed( or not? or it means only the client temporary data, which resides at the client, and VERTEX_ARRAY indicates, is changed, but the data in graphics memory, remain unchanged? )

btw, from opengl api reference, I cannot find glMapBufferRange/glFlushMappedBufferRange() functions, any more hints please, thanks very much

01-24-2011, 02:39 AM
btw :)

If I register( not only map it ) this VBO to CUDA everytime after call of glMapBuffer, the cuda can notice the modification of VBO.

The question is, why must man register it again? after glMapBuffer, will the location of device memory of this VBO be changed?

01-24-2011, 05:05 AM
I cannot find glMapBufferRange/glFlushMappedBufferRange() functions, any more hints please, thanks very much

There is another variant: to copy device memory (CUDA buffer map pointer) to host memory by cuMemcpyDtoH.