Question

Are there ways a shader can query the sum/average of a block of pixels in a texture read, rather than looking up a single value? I'm working in Cg but any examples and search terms are welcome.

I specifically mean built-in ways, NOT looping manually in my shader.

Was it helpful?

Solution

Not really.

The best you can do is the ARB_texture_gather extension (core in GL 4.0+)/D3D10.1's gather function.

This allows you to fetch from a 2x2 block of texels, but you only get one component of that texture at a time. Further, the pure extension/D3D10.1 version can only fetch the Red component of the texture. The GL 4.0/D3D11.0 version can fetch any of the four components, but each call will only fetch one.

Of course, you have to do the averaging of the 2x2 block yourself.

The only really viable alternative is to do what Lyth said: generate mipmaps for your texture and sample from them. The texture accessing functions can fetch from a specific mipmap, which represents an average of a particular range of pixels. And of course, those are limited as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top