PDA

View Full Version : Find the minimun depth value



nachovall
03-18-2009, 02:45 AM
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?

Thanks.

Brolingstanz
03-18-2009, 09:33 AM
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.)

dukey
03-18-2009, 11:23 AM
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

Abdallah DIB
03-18-2009, 11:31 AM
if u dont want to use shaders to find the min/max . u can use glGetMinmax :
//data is the pixel data vector

//init
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glMinmax(GL_MINMAX,format,GL_TRUE);//format is the internal format of ur data vector
glEnable(GL_MINMAX);
//drawing
glRasterPos(..)
glDrawPixels(w,h,format,data);//data is the pixel data vector
GLubyte minmax[SIZE];//depend of data format
glGetMinmax(GL_MINMAX,GL_TRUE,internal_format,type ,minmax);
//format and type are same as glDrawPixels

nachovall
03-19-2009, 03:01 AM
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 :) )

Thanks everyone for your quick replies.

Relic
03-19-2009, 04:44 AM
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


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.