I've been working with this piece of code to get the mip map level that I should sample for a texture that I got off a forum somewhere. I noticed that they use a const float. Now from what I understand this would just immediately get discarded after the function was done, so if there was any performance gain here it would be hyper-small.

But I know that sometimes there is a lot of compiler magic that goes on in graphics programming languages, so I was wondering if the const declaration actually did matter (like, does it only calculate the value once and reuse that value for every fragment on the screen?)

        float MipLevel( float2 uv, half mipcount, half texturedim)
            uv *= texturedim;

            float2 dx = ddx( uv );
            float2 dy = ddy( uv );
            float d = max( dot( dx, dx ), dot( dy, dy ) );

            // Clamp the value to the max mip level counts
            const float rangeClamp = pow(2, (mipcount - 1) * 2);
            d = clamp(d, 1.0, rangeClamp);

            float mipLevel = 0.5 * log2(d);

            return mipLevel;
  • \$\begingroup\$ Global and local variables, as well as input function parameters, can be declared with the const qualifier. This means that the variable's value cannot be changed after it is initialized. khronos.org/opengl/wiki/Type_Qualifier_(GLSL)#Constant qualifier \$\endgroup\$
    – Sidar
    Commented Mar 1, 2018 at 16:48
  • \$\begingroup\$ Although I do admit it's a bit weird considering that mipcount could change? \$\endgroup\$
    – Sidar
    Commented Mar 1, 2018 at 16:49
  • \$\begingroup\$ @Sidar that looks like an upvote-worthy answer to me. \$\endgroup\$
    – DMGregory
    Commented Mar 1, 2018 at 18:06
  • \$\begingroup\$ @DMGregory I haven't made it an answer as it doesn't clarify how it works for sittuations like this where mipcount can differ. Unless it's meant not to be mutated after it's set every time it's created at that point in the function. It's peculiar considering the const is redundant unless the programmer wants to be extremely safe? \$\endgroup\$
    – Sidar
    Commented Mar 1, 2018 at 18:12

1 Answer 1


A const value is never changed after it is initialized. By declaring a const the compiler can do certain optimizations, like calculating the const value at compile time instead of at runtime and replacing references to the variable with the actual value so that there is no extra overhead.

As well as being a hint to the compiler, it is also a hint to the developer who is reading the code. AKA self-documented. If the variable is declared as a const, even if there is no optimization advantage, the reader can be sure the value that is assigned at the time of initialization is the value that will be used when the const is referenced later.

In the case of the example in the question, it's possible and likely that the value passed to the mipcount argument is also a const. When compiling, the compiler will analyze the code, following the execution paths and if it finds that the value passed to mipcount is a const, it can then optimize rangeClamp to a literal value instead of a subtract, multiply and pow operation.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .