Portable way to create a histogram

I would like my shader to create a histogram at a mid-stage of the shader. ( The final gl_FragColor is not part of the histogram. ) The obvious way would be to have the histogram as a uniform array that the shader can update. However, at least on my nvidia hw, array indexing using computed values is not allowed. So…

int index;

index = (int)( clamp( color.r * ( histogram_length - 1 ), 0.0, histogram_length - 1 ) );

histogram_array[ index ] += 1;

This fails to link with the complaint that only texcoords can be indexed and then only with loop counters.

So I’m looking for ideas on how to proceed.

On a related note … why is there no integer version of “clamp” ?

“The obvious way would be to have the histogram as a uniform array that the shader can update.”

Shaders can not write into uniforms.

And you can also not write to arbitrary pixel locations inside a fragment shader, which means you can’t store the histogramm into a color buffer (e.g. 256 * 1 pixels FP32) in one pass.

One way to create a histogram is to use occlusion queries and do as many fullscreen passes as you have buckets, and simply discard fragments in the fragment shader that doesn’t fall into the current bucket.

Originally posted by Relic:
[b] “The obvious way would be to have the histogram as a uniform array that the shader can update.”

Shaders can not write into uniforms.

And you can also not write to arbitrary pixel locations inside a fragment shader, which means you can’t store the histogramm into a color buffer (e.g. 256 * 1 pixels FP32) in one pass. [/b]
Well there you go. Not just SOL but SOB (brains) too!

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.