GLSL : find global min. and max in texture

Started by
9 comments, last by dj3hut1 14 years, 2 months ago
Quote:Original post by MJP
Quote:Original post by Tree Penguin
Quote:Original post by taby
I suppose you could scan through the entire texture using a sampler2D from within the shader, but that would be a ridiculously huge waste of GPU time.


For postprocessing (doing hdr adjustments etc) that's a good way to do it. The shader would be heavy, but you only run it for one pixel, so it's not that bad.


The "run once per pixel" part is precisely what makes that method such a bad idea. A modern high-end GPU can potentially execute hundreds pix pixel shaders simultaneously, and will have a several dozen texture units available for sampling. Having a single shader that performs all of the texture sampling means that you don't make use of all of that hardware available to you.


What I think he was referring to was setting up an offscreen buffer of size 1x1, so even though it's run once per pixel, there's only one pixel being rendered in the first place (hopefully we're on the same page). Now, samoth's idea is a little better since he does the same thing, but sets up an FBO of size 1xn, where n is the number of lines in the texture to be enumerated, thus taking advantage of the parallelism that you speak of.

Either way, both methods involve amortized costs. We're not talking about calculations done per-frame, but literally only once. I am uncertain that we are all on the same page here. In fact, I think his comment about HDR is because we're not all on the same page, so the confusion has only been compounded. :)
Advertisement
Hello,

i have an idea for another algorithm, which takes benefit of the parallel gpu processing.
If your texture is a POT ( power of two )-texture and has a size of n x n the algorithm requires log_2(n) passes.

You can write a shader which takes as input the texture of the previous pass and renders to a texture ( via FBO ) with size n/2 x n/2.
In the (pixel) shader you do nothing more than calculating the min-max values for the current 4 neighbour texels and write them to the r and g-color channel for instance. The most difficult thing would be to calculate the correct texture coordinates.

This is repeated until your target texture has a size of 1x1 ;)

dj3hut1

P.S. : Maybe you need 2 shaders : one for the first pass ( a greyscale image as input ) and one for the remaining passes ( a 2 component image as input )

This topic is closed to new replies.

Advertisement