Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: OpenGL: Use depth buffer from Gbuffer in default framebuffer  (Read 3720 times)

0 Members and 1 Guest are viewing this topic.

Peteck

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
OpenGL: Use depth buffer from Gbuffer in default framebuffer
« on: August 07, 2014, 03:13:20 pm »
So I am making a deferred rendering application with OpenGL. After doing the geometry and light passes I want to render some particles. But I need some depth testing on those. So what I want to do is to use the depth buffer binded to my GBuffer to do some depth testing when drawing to the default OpenGL framebuffer.

I make and bind the depth buffer/texture to the framebuffer/GBuffer like this
glGenTextures(1, &_depthTextureID);
glBindTexture(GL_TEXTURE_2D, _depthTextureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, _width, _height, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, _depthTextureID, 0);

My question then is: How do I bind the Gbuffer depth buffer to the default framebuffer for depth testing on the geometry pass?

lolz123

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
Re: OpenGL: Use depth buffer from Gbuffer in default framebuffer
« Reply #1 on: August 07, 2014, 05:20:04 pm »
Short answer: You can't. Render to a special "composition" texture that is part of the GBuffer, and then blit to the main framebuffer.

Long answer: Depth testing only works when rendering to the framebuffer to which it is bound. Since you cannot bind a custom depth buffer to the main framebuffer, you cannot simply switch the depth buffer over to the main framebuffer as an attachment.
You could do manual depth testing in a shader if you want to, but this is slow. So the best solution is the render to a "composition" attachment part of the GBuffer, and when you are done with all rendering, blit that to the main framebuffer.
Have you heard about the new Cray super computer?  It’s so fast, it executes an infinite loop in 6 seconds.

Peteck

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: OpenGL: Use depth buffer from Gbuffer in default framebuffer
« Reply #2 on: August 07, 2014, 06:36:44 pm »
I did some searching when I saw that I couldn't bind the depth buffer to another framebuffer.

glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_DEPTH_BUFFER_BIT, GL_NEAREST);

For me it looks like I just can blit the depth bits into the default frammebuffer. Don't know if it will work, haven't tested it yet.

 

anything