You’re using a number of CUDA functions I’m unfamiliar with, but I can show you how I to OpenGL interop. Creat the context:
cudaSetDevice( cutGetMaxGflopsDeviceId() );
cudaGLSetGLDevice( cutGetMaxGflopsDeviceId() );
Create a buffer in OpenGL and register it:
// Create a pixel unpack buffer to store the perspective texture
glGenBuffers ( 1, &glPerspectiveBufferPos );
glBindBuffer ( GL_PIXEL_UNPACK_BUFFER, glPerspectiveBufferPos );
glBufferData( GL_PIXEL_UNPACK_BUFFER, GL_IMAGE_SIZE, 0, GL_DYNAMIC_COPY );
glBindBuffer ( GL_PIXEL_UNPACK_BUFFER, 0);
// set pixel alignments
glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 );
glPixelStorei ( GL_PACK_ALIGNMENT, 1 );
// register buffer object with CUDA
cutilSafeCall ( cudaGLRegisterBufferObject( glPerspectiveBufferPos ));
glut Idle func I do some operation on the buffer:
unsigned char *perspectiveCudaGlBuf;
cutilSafeCall( cudaGLMapBufferObject( (void**)&perspectiveCudaGlBuf, glPerspectiveBufferPos ) );
// some stuff with perspectiveCudaGlBuf as a device memory pointer
// unmap buffer object
cutilSafeCall( cudaGLUnmapBufferObject( glPerspectiveBufferPos ) );
And glut display just use the OpenGL texture buffer as normal:
// bind the perspective texture to the active GL texture
glBindBuffer ( GL_PIXEL_UNPACK_BUFFER, glPerspectiveBufferPos );
glTexImage2D ( GL_TEXTURE_2D, 0, 4 /* RGBA */, GL_IMAGE_WIDTH, GL_IMAGE_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0 );
glBindBuffer ( GL_PIXEL_UNPACK_BUFFER, 0 );
// first draw the perspective texture
glBegin ( GL_QUADS );
glTexCoord2f ( 1, 1 ); glVertex2f ( GL_IMAGE_WIDTH, GL_IMAGE_HEIGHT );
glTexCoord2f ( 1, 0 ); glVertex2f ( GL_IMAGE_WIDTH, 0.0 );
glTexCoord2f ( 0, 0 ); glVertex2f ( 0.0, 0.0 );
glTexCoord2f ( 0, 1 ); glVertex2f ( 0.0, GL_IMAGE_HEIGHT );
glEnd();