Viewing the depth buffer with max efficiency

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Viewing the depth buffer with max efficiency

Post by robmar »

Has anyone managed to render a texture or FBO with the scene´s depth buffer by temporarily switching off the texture compare mode?

I´m trying to find the most efficient way of viewing the depth buffer in an intensity z-modified blue, without moving pixels, and processed by the GPU.

Adding a shader to a texture is easy, but is it possible to add a shader or something to the FBO renderer, so that it draws pixels not based on color but depth?
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Viewing the depth buffer with max efficiency

Post by hendu »

You can do that in GL (attaching the depth as a texture), but not in irr currently. Other ways include saving the depth when rendering it.
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Viewing the depth buffer with max efficiency

Post by robmar »

Do you know which function calls that would be? I could have a go at implementing both methods to see the speed difference.
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Viewing the depth buffer with max efficiency

Post by robmar »

I found this code snippet, wondered if it could be adapted for zbuffer rendering:-

Code: Select all

 
glBindTexture(GL_TEXTURE_2D, mTexIdShadowDepthRender);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);   // Turn off compare mode to draw.
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2d(0,1);
çglVertex2d(x, y);
glTexCoord2d(0,0);
glVertex2d(x,y+size);
glTexCoord2d(1,1);
glVertex2d(x+size, y);
glTexCoord2d(1,0);
glVertex2d(x+size, y+size);
glEnd();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);       // Reinstate compare mode.
 
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Viewing the depth buffer with max efficiency

Post by hendu »

To save the depth, just write gl_FragCoord.z to another buffer. You'll need either a float buffer, or to use some clever packing with a 8-bit one.
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Viewing the depth buffer with max efficiency

Post by robmar »

Okay, thanks, this is going to be fun! Any good references on GLSL programming would be welcome!

The aim is to reformat the FBO to show the rendered scene image on the left half of the screen, and show the depth buffer as a grey scale (white nearest to viewer) on the right side. I guess its going to be a good exercise in GLSL... I can use the split window examples + shader to achive this, I hope!
Last edited by robmar on Fri Apr 06, 2012 10:02 pm, edited 1 time in total.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Viewing the depth buffer with max efficiency

Post by hendu »

Don't forget to normalize the depth for visualization, showing it as-is would show just one color ;)
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Viewing the depth buffer with max efficiency

Post by robmar »

I thought the depth buffer was stored as floats from 0.0 to 1.0?
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Viewing the depth buffer with max efficiency

Post by hendu »

Yes, but in a typical scene, the depth difference of one object and another is small enough to be within 1/256 of each other.
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Viewing the depth buffer with max efficiency

Post by robmar »

But if I calculate the intensity based on the direct z-buffer value, then it will just scale, i.e the pixels will be half intensity when z == 0.5.
Am I missing the point?
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Viewing the depth buffer with max efficiency

Post by hendu »

Yes, because all of your pixels will be 0.5 (for example) unless you normalize ;)
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Viewing the depth buffer with max efficiency

Post by robmar »

and if I adjust the near and far distance to scale across the required view depth rather than to the far distance?
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Viewing the depth buffer with max efficiency

Post by hendu »

It may or may not work, I don't know.
Post Reply