1 - textureComputedLod
When doing some programmable texture access, textureQueryLod is very useful. However, textureQueryLod has a dependency on sampler object state (or texture object state).
The result of textureQueryLod is defined as follow according to the GLSL specification:
float ComputeAccessedLod(float computedLod)
{
// Clamp the computed LOD according to the texture LOD clamps.
if (computedLod < TEXTURE_MIN_LOD) computedLod = TEXTURE_MIN_LOD;
if (computedLod > TEXTURE_MAX_LOD) computedLod = TEXTURE_MAX_LOD;
// Clamp the computed LOD to the range of accessible levels.
if (computedLod < 0)
computedLod = 0.0;
if (computedLod > (float)
maxAccessibleLevel) computedLod = (float) maxAccessibleLevel;
// Return a value according to the min filter.
if (TEXTURE_MIN_FILTER is LINEAR or NEAREST) {
return 0.0;
} else if (TEXTURE_MIN_FILTER is NEAREST_MIPMAP_NEAREST
or LINEAR_MIPMAP_NEAREST) {
return ceil(computedLod + 0.5) - 1.0;
} else {
return computedLod;
{
In some cases, this dependency is quite annoying when going programmable as we have to manage the sampler state in the C++ code to ensure that the filtering is performed correctly.
There is a way to by-pass this code by calling:
glSamplerParameteri(Sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
glSamplerParameteri(Sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
That the parameter that makes me feel the most comfortable but still, if the idea is to program my texture access, why should I setup a sampler object?
My suggestion is to create a function (textureComputedLod?) that removes the dependency with the sampler object.
2 - textureLevels
Currently with GLSL it is possible to compute the number of mipmaps level of a texture like this:
ivec2 TextureSize = textureSize(Sampler, 0);
int Levels = int(log2(float(max(TextureSize.x, TextureSize.y)))) + 1;
Another, probably cheaper, alternative is to use an uniform to pass the level count.
I wonder if a GLSL function (textureLevels?) would not be appropriate for this duty.