Using current DepthBuffer in FBO

Hello,

i would like to know, how to use a current depth buffer in a FBO. The rendering passes are done in the following way:


  1. depth-pass only: creates depth mask
    glEnable( GL_DEPTH_TEST );
    glDepthMask( true );
    glColorMask( false, false, false, false );
    renderGeometry( … )

  2. render 2 texture pass: creates texture
    create fbo
    render to fbo

  3. render FBO texture


In step 2 (fbo) i would like to use the depth buffer of step 1. How can i do that ?

  • Mika

I assume you mean you want to use the depth buffer from step 1 as input to step 2?

In step 1, create a GL_DEPTH_COMPONENT texture, attach it as a DEPTH_ATTACHMENT to the step 1 FBO, render with it. Then for step 2, bind that texture to a sampler uniform used in the shader for step.

Thank you for your quick reply. I’ve tought of your solution. But is it somehow possible without rendering the depth to a FBO (step 1) ?

I don’t get your question. What are you going to use in step 2 if not rendering the depth buffer in step 1?

Well yeah, you could render step 1 to the system framebuffer (with depth buffer) and then after step 1 copy the depth buffer to a texture via glCopyTexSubImage2D…

However, to feed the depth buffer into a subsequent pass in a shader, AFAICR you gotta get it in a texture somehow.

Now if you just want to depth test against it, say to run occlusion tests on or something, then you can just use the existing system depth buffer for that without needing to copy it to a texture.

Thats exactly what i want to do:
I would like to run z-tests using the existing “system depth buffer” i’ve created in step 1. I don’t need to access a depth texture in step 2. I just want to cull all geometric elements that I render to a FBO ( in step 2 ) using the system depth buffer from step 1.

How can I bind the existing system depth-buffer (from step 1) so it will be used for culling when rendering to a FBO (step 2) ?

How can I bind the existing system depth-buffer (from step 1) so it will be used for culling when rendering to a FBO (step 2) ?

You don’t. The buffers used for the default framebuffer cannot be used with FBOs. So you will have to render to an FBO in step 1, and then use that depth buffer in step 2.