Search code examples
c++openglshadertextures

Counting texels using a fragment shader


I have two textures generated using a fragment shader. I want to be able to count the number of texels in each texture that are above some colour intensity.

How can this be done?

My initial thought is to count these texels using the fragment shader before generating the texture. However, this would require some sort of global counter. I can't use occlusion queries because the textures are created from other textures. I'm using OpenGL 2.1.


Solution

  • Thanks for the ideas,
    I found a simple way I think is the most efficient. I initially thought occlusion queries could only be used with geometry but they can also be used with textures.

    1. Turn on occlusion queries
    2. Render the image using a fragment shader and discard texels below required color intensity
    3. Retrieve number of texels passing drawing test from query