Hi everyone,
I have been trying to share an RGB buffer and a depth buffer between CUDA and OPENGL for the last few days, and while I manage to share the RGB buffer I had no success in making CUDA happy about the depth buffer.
I tried two different approaches, in the first I create a texture like for the RGB using the following code that binds the buffer to a texture in OPENGL. At the end of this procedure I check if everything is fine and I never get an error from OPENGL.
glGenTextures(1, &depth_buffer_);
glBindTexture(GL_TEXTURE_2D, depth_buffer_);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, screen_width,
screen_height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
depth_buffer_, 0);
And these are the calls I have using CUDA:
gpuErrchk(cudaGraphicsGLRegisterImage(&depth_buffer_cuda_, depth_buffer_,
GL_TEXTURE_2D,
cudaGraphicsRegisterFlagsReadOnly));
gpuErrchk(cudaGraphicsMapResources(1, &depth_buffer_cuda_, 0));
gpuErrchk(cudaGraphicsSubResourceGetMappedArray(cuda_gl_depth_array_,
depth_buffer_cuda_, 0, 0));
Using this approach I get a CUDA invalid argument error from cudaGraphicsGLRegisterImage. I tried to create the texture with different parameters but there was no way to make CUDA happy.
In the second approach rather than creating a texture I created an FBO with the following lines:
glGenRenderbuffers(1, &depth_buffer_);
glBindRenderbuffer(GL_RENDERBUFFER, depth_buffer_);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32F, screen_width, screen_height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
And then the CUDA calls are the following:
gpuErrchk(cudaGraphicsGLRegisterBuffer(&depth_buffer_cuda_, depth_buffer_,
cudaGraphicsRegisterFlagsReadOnly));
gpuErrchk(cudaGraphicsMapResources(1, &depth_buffer_cuda_, 0));
gpuErrchk(cudaGraphicsSubResourceGetMappedArray(cuda_gl_depth_array_,
depth_buffer_cuda_, 0, 0));
In this case the first and second call of CUDA are fine but then I get an unknown cuda error from udaGraphicsSubResourceGetMappedArray.
I searched a lot online but I could not really find a working example of binding of the depth buffer between openGL and CUDA. Has someone encountered and solve this problem?
Thanks