Find the minimun depth value

Hi all.

I used FBO to render the depth buffer to a texture. Now I need to find the min value stored in that texture. My first approach was to render the texture in the frame buffer and then call glReadPixels() to store the value on an array and then find the min value in the array. That’s so slow because of passing from gpu to cpu with glReadPixels().

Does anybody nows how could I did faster?


how about block-reducing the depth texture in a shader by finding the minimum z in say a 2x2 block - a bit like mip mapping only you store the min depth at each step.

see the AMD paper “March of the Froblins” for some implementation details and caveats. (if you don’t love those froblins there’s something wrong with you.)

You could render a textured quad with a shader that checks the current pixel and compares it to the one at 0,0. If the one at 0,0 is smaller keep it, else use the current value. Then continue until you’ve gone through all the pixels on the quad.

Then simply readback the value at that location (0,0)

that should work

if u dont want to use shaders to find the min/max . u can use glGetMinmax :
//data is the pixel data vector

glMinmax(GL_MINMAX,format,GL_TRUE);//format is the internal format of ur data vector
glDrawPixels(w,h,format,data);//data is the pixel data vector
GLubyte minmax[SIZE];//depend of data format
//format and type are same as glDrawPixels

I think my best solution will be block-reduce algorithm. Seams fast and my code is structured fine for this algorithm.

(You were rigth. I love that froblins :slight_smile: )

Thanks everyone for your quick replies.

You cannot simply write to arbitrary pixel locations, means you would have to render this one pixel only and iterate over the whole texture which is most likely failing when exceeding the allowed number of executed instructions.
That’s like a one step block-reducing algorithm, there are reasons for not doing this in a single step.
You could do it with bigger steps than 2x2 though.