Jump to content
  • Advertisement
Sign in to follow this  
mv348

OpenGL Adding up pixels in a texture

This topic is 1999 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Let's say I have a 32x32 texture containing ints, or floats. I am trying to come up with a very fast method of adding up all the values in the texture using OpenGL.

 

I was thinking I could possibly set the texture minification filter to GL_LINEAR and render it into a texture half the size (16x16). According to the documentation:

GL_LINEAR:

Returns the weighted average of the four texture elements
                                        that are closest to the specified texture coordinates.

 

If the texture rendered into is half the size I believe the weighted average will be the same as the average. Moreover, the average of a new pixel will be (sum of 4 old pixels)/4.

 

So I'm thinking if I render from a 32x32 texture into a 16x16, and then into 8x8, 4x4, 2x2 and 1x1, and then use glReadPixel( ... ) I will get the (sum of all pixels )/4^5.

 

4^5 = 1024 so I can either pre-multiply the texture values by 1024 or divide the result by 1024. I think the former would be more appropriate for ints to avoid roundoff error. I could maybe even use bit-shifting to multiply by 1024 for added speed.

 

What do you think of this idea? Do you have a better one?

Edited by mv348

Share this post


Link to post
Share on other sites
Advertisement

I actually encountered the exact same problem in the context of a toy image compression algorithm. I tried exactly that, except I made a mipmap and then read back the one texel to the client side and multiplied by the texture's area.

 

It turned out that this did not have quite enough accuracy for my needs, since you lose accuracy with each average operation (and there are log2(max(n,m)) such operations)--though I strongly suspect it might have been acceptable had floating point textures been available.

 

Certainly, this will be the fastest way. The GPU is great as these sort of things, and texture accesses will generally be more cache coherent on the GPU than the CPU.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!