Simple way to get texture dimensions in GLSL

Just a simple productivity enhancement. It would be nice if we could get the size of a sampler directly in GLSL rather than always passing in a uniform. Often times you need these parameters for doing things like texture filtering or handling boundary conditions.

The way I’ve been doing things so far (set 1 uniform per texture) is ugly, brittle and tedious. Every time I need to pass a texture to a shader it seems like I am always passing these values with it, and repeating the same code ad nauseum. A much better way would be to simply add built-in GLSL functions to pull the bounds out of the texture, ie:

uniform sampler2D my_tex;
uniform sampler2D my_other_tex;

ivec2 my_bounds = getBounds2D(my_tex);
ivec2 my_other_bounds = getBounds2D(my_other_tex);

I don’t think that such a feature would be very difficult to implement (though I could be wrong) and it would save an enormous amount of headaches.

This can already be done with SM4.0 (mentioned here).

textureSize2D(), textureSize3D(), etc.

Ah, thank you very much! I guess I need to read the specifications for SM4.0 a bit more carefully.

No problem. There certainly is a lot of new information to take in. :slight_smile: