OpenGL-Cuda integration

Hi all!

I need to put some cuda works in an OpenGL Project.
I didn’t find how to bind a Shader Buffer Storage Object with Cuda to fill it with cuda computing and use the result with GLSL fragment shader.

Any usefull link?

CUDA-OpenGL interop:
[url]http://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__OPENGL.html#group__CUDART__OPENGL[/url]

Sample codes:
[url]CUDA Samples :: CUDA Toolkit Documentation

introductory presentation:
[url]Page Not Found | NVIDIA

I try to modifie datas in a vbo like this :

at the end of open GL initialisation : cudaGLSetGLDevice(0);

during rendering :

cudaGLRegisterBufferObject(i);//i is my VBO adress 
float3 *dptr=NULL;
cudaGLMapBufferObject((void**)dptr, i);
dim3 dimBlock( 1, 1 );//just launch 1 thread
dim3 dimGrid( 1, 1 );
morph<<<dimGrid, dimBlock>>>(dptr);
cudaGLUnmapBufferObject(i); 
cudaGLUnregisterBufferObject(i);

And my dummy kernel :
global
void morph(float3*dptr)
{
dptr[threadIdx.x].z=0.0;
}

It does bullshit and cuda error says me : CUDA error: an illegal memory access was encountered.
cudaError_t error = cudaGetLastError();

Dont know why, but the cudaGLMapBufferObject dosent work when cudaGraphicsMapResources + cudaGraphicsResourceGetMappedPointer work.

I’m having trouble with this API as well, I get illegal memory accesses when checking errors for:

  • cudaGraphicsMapResources
  • cudaGraphicsResourceGetMappedPointer
  • cuda kernel using mapped pointer (makes sense if cudaGraphicsMapResources failed)
  • cudaGraphicsUnmapResources

Earlier in the code, I call cudaSetDevice(0) (after creating and setting active my openGL context), and after calling glBufferData on a PBO, I call cudaGlGraphicsRegisterBuffer on the PBO, which returns no error.

Am I using this incorrectly? Can anyone point me in the right direction? The sample codes look like what I have and the introductory presentation uses deprecated interop functions.

EDIT: I had spend a long time looking for my error already, but I found it after posting. I was casting a uint8_t* devByteArray for getting a pointer to mapped memory, but I should have been casting the address of the uint8_t*. Here’s what my own fixed code looks like for the line in question:

uint8_t* devByteArray;

BEFORE:
cudaGraphicsResourceGetMappedPointer((void**)devByteArray, &size, resource);

AFTER:
cudaGraphicsResourceGetMappedPointer((void**)&devByteArray, &size, resource);

I was using the API correctly in general, but improperly managing pointers and addresses. When CUDA encounters an illegal memory access, the current cuda context is destroyed, meaning any subsequent memory accesses would also be illegal.