FBO: Depth Buffer or Depth Texture?

Hi folks,

Sorry for the confusing and technically propably wrong title, but I could not come up with anything better :).
I am currently thinking what would be necessary for a simple deferred shading FBO and I came to the conclusion that it would be the best to only save the depth, to recover the position later on (to have the possibility to add more shading features later on).- Even though since I have never really wrote the Depth to a texture I dont really know how the FBO for that should look like.- For now I always just added a depth buffer like this:


	glGenRenderbuffersEXT(1, &depthBuffer);
	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthBuffer);
	glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthBuffer);

But I saw that you can also bind a texture to an FBO and define its internal format as the GL_DEPTH_COMPONENT, so I was wondering what would make more sense in my case, and if I would use the texture as a depth attachment, would I also add the “normal” depthbuffer?

it would be ideal to just use one texture for high precision depth, and the three other ones for material information and normals. the depth in a GL_DEPTH_COMPONENT texture is written automatically right? are there any good examples on how to use them and with which texture parameters?

Thank you!

No, you nailed it. The only thing more precise would have been to say “Depth Renderbuffer or Depth Texture?”.

…save the depth, to recover the position later on (to have the possibility to add more shading features later on).- Even though since I have never really wrote the Depth to a texture…I saw that you can also bind a texture to an FBO and define its internal format as the GL_DEPTH_COMPONENT, so I was wondering what would make more sense in my case, and if I would use the texture as a depth attachment, would I also add the “normal” depthbuffer? it would be ideal to just use one texture for high precision depth

Use a texture if you need to feed the depth buffer to a subsequent pass as a readable texture resource in a shader.
Otherwise just use a renderbuffer.

And you can’t steal the system (window) framebuffer for use in an FBO (AFAIK). However, you can share the same one between multiple FBOs if you want.

the depth in a GL_DEPTH_COMPONENT texture is written automatically right?

Yep, assuming you have depth writes enabled, and the texture bound as the depth attachment of an FBO.

are there any good examples on how to use them and with which texture parameters?

Try this: EXT_framebuffer_object - Color texture & Depth texture (OpenGL Wiki)
Scan the rest of the page for other potentially useful snippets such as depth+stencil rendering.

Thank you very much, that helped me alot, I got the basics working allready and also directional lights :slight_smile:

Anyways I am experiencing a driver bug (or simply the drivers for mac dont support it) when it comes to mipmapping (I am on a first generation Mac Book Pro with a ATI Radeon x1600).-

Everytime I try to use any setting related to mipmapping when setting up GL_TEXTURE_MIN_FILTER or GL_TEXTURE_MAX_FILTER with for example GL_LINEAR_MIPMAP_LINEAR it breaks the FBO.- glGenerateMipmapEXT itself does not break it, only the parts with the texture filtering:


	glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, lightAccumFbo);
	
    glBindTexture( GL_TEXTURE_2D, result);
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_FLOAT, NULL);
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glGenerateMipmapEXT( GL_TEXTURE_2D);

I also tried it without floating point values and it still breaks.- Any ideas?

ATI Radeon X1600 does not support filtering of floating-point textures. Therefore, the FBO should be incomplete because the texture is incomplete.

Try to put glGenerateMipmapEXT before glTexImage2D.

Ah okay, but shouldnt GL_NEAREST_MIPMAP_NEAREST work then? if I put the glGenerateMipmapEXT before glTexImage2D it does not break anything but does the command still work that way? Is there any simple test to check if mipmapping works on that texture? Thanks!

Edit: And do the mipmaps automatically get deleted, when I delete the texture?

As far as I know, ATI X1000 cannot mipmap non-power-of-two textures. I am not sure about floating-point ones though. Maybe you could google the specification of your GPU…

Well, test it yourself with a simpler example.

Yes, they do, but this one should go in the beginners section.