openglcudadepth-bufferzbuffer

Opengl depth buffer to cuda


I'm a new programmer to Opengl, my aim is to retrieve the depth buffer into a FBO to be able to transfer to cuda without using glReadpixels.

Here is what I've already done:

void make_Fbo()
{

    glGenFramebuffers(1, &fbo);
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER,
                               GL_DEPTH_ATTACHMENT,
                           GL_RENDERBUFFER,
                                           fbo);
    check_gl_error("make_fbo");
}


void make_render_buffer()
{
    glGenRenderbuffers(1, &rb);
    glBindRenderbuffer(GL_RENDERBUFFER, rb);
    glRenderbufferStorage(GL_RENDERBUFFER,
                          GL_DEPTH_COMPONENT,
                               win.width,
                               win.height);
    check_gl_error("make render_buffer");
}

This code create my FBO with correct depth values.

A new problem appear now, according to the article "fast triangle rasterization using irregular z-buffer on cuda" It's not possible to acces to depth buffer attached to the FBO from Cuda.

Here is is the quote from the article:

Textures or render buffers can be attached onto the depth attachment point of FBOs to accommodate the depth values. However, as far as we have tested, they cannot be accessed by CUDA kernels. [...] we managed to use the color attachment points on the FBO. Apparently in this case we have to write a simple shader program to dump the depth values onto the color channels of the frame buffer. According to the GLSL specification [KBR06], the special variable gl_FragCoord

Are the statements still true? What do you advise me to dump the depth buffer to the color channels? to a texture ?


Solution

  • Well yes and no. The problem is that you can't access resources in CUDA while they are bound to the FBO.

    As I understand it, with cudaGraphicsGLRegisterImage() you enable cuda access to any type of image data. So if you use a depth buffer that is a rendertarget and is NOT bound to the FBO, you can use it.

    Here's the cuda API information:

    https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__OPENGL.html#group__CUDART__OPENGL_1g80d12187ae7590807c7676697d9fe03d

    And in this article they explain that you should round-robin or double-buffer the depth-buffer, or copy the data before using it in CUDA (but then you more or less void the whole idea of interop).

    http://codekea.com/xLj7d1ya5gD6/modifying-opengl-fbo-texture-attachment-in-cuda.html