Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 10 of 11

Thread: Framebuffer depth test with depth and stencil buffer

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    5

    Framebuffer depth test with depth and stencil buffer

    I'm attempting to implement deferred shading. I've got a number of problems with it, but first and foremost, depth testing is not working right.

    In my deferred shading, I create a depth buffer, naturally. Depth testing works fine with this. But if I create a depth and stencil buffer, depth testing just doesn't work. It's peculiar.

    Buffer creation, depth component only, depth testing works:

    Code :
    glBindTexture(GL_TEXTURE_2D, depth_map);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_map, 0);

    Buffer creation, depth and stencil components, depth testing doesn't work:

    Code :
    glBindTexture(GL_TEXTURE_2D, depth_map);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH32F_STENCIL8, width, height, 0, GL_DEPTH_STENCIL, GL_FLOAT, NULL);
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depth_map, 0);

    This is literally the only change to my code, and depth testing works for the first one, and fails for the second one. Stencil test is disabled, depth test enabled for both. Any suggestions to fix this problem? I have read several threads regarding similar issues, suggesting this was an AMD driver problem, and indeed I have an AMD card. Hopefully I'm just doing something wrong.

    Thanks.

  2. #2
    Member Regular Contributor Nowhere-01's Avatar
    Join Date
    Feb 2011
    Location
    Novosibirsk
    Posts
    251
    try GL_DEPTH24_STENCIL8.

  3. #3
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    5
    Thank you, but that didn't work. Exactly the same result. I also tried with GL_DEPTH_COMPONENT instead of GL_DEPTH_STENCIL, but to no avail.

    Any other suggestions?

    Thanks

  4. #4
    Junior Member Regular Contributor
    Join Date
    Mar 2012
    Posts
    129
    Try:

    Code :
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, m_width, m_height, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL);

  5. #5
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    5
    Interesting.

    That seems to have worked very well, but now I'm having buffer clearing issues. I'm definitely clearing the buffers for my four framebuffer attachments before I draw, and I'm clearing the default framebuffer as well. There's also some flickering issues, which I assume could be to do with depth or stencil buffers not being cleared? Also, how on earth can changing the format for the depth and stencil texture make something fail to clear??

    Thanks for that suggestion though, that definitely got me on the right track. Are there any problems associated with using integers rather than floats for the depth buffer?

  6. #6
    Junior Member Regular Contributor tksuoran's Avatar
    Join Date
    Mar 2008
    Location
    Cambridge, UK
    Posts
    224
    Have you tried using TexStorage instead of TexImage? And ClearBuffer* for clearing?

  7. #7
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    498
    Quote Originally Posted by moldyviolinist View Post
    Thanks for that suggestion though, that definitely got me on the right track. Are there any problems associated with using integers rather than floats for the depth buffer?
    Internally, the depth buffer contains fixed-point values in the range 0..1, so there's no particular reason to use floats.

    However, as well as GL_UNSIGNED_INT_24_8, you can also use GL_FLOAT_32_UNSIGNED_INT_24_8_REV (a 32-bit float containing the depth value followed by a 32-bit unsigned integer containing the stencil value in the 8 least-significant bits).

    What you can't do is use a format of GL_DEPTH_STENCIL with a type which doesn't allow for the stencil value (8.4.4):

    An INVALID_ENUM error is generated if format is DEPTH_STENCIL and type
    is not UNSIGNED_INT_24_8 or FLOAT_32_UNSIGNED_INT_24_8_REV.
    .
    GL_UNSIGNED_INT_24_8 has the advantage that it can probably be copied directly to video memory, without the need for conversion.

    It should also be valid to use an internalformat of GL_DEPTH_STENCIL (or a sized version thereof) with a format of GL_DEPTH_COMPONENT and any scalar type, in which case the stencil component will be undefined (not a problem if you're just going to clear it).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •