简体   繁体   中英

Do global constants still have to be initialized with constant expressions?

Since GLSL 4.20 const -qualified variables no longer have to be initialized by constant expressions. But when I actually try to define a global const -qualified variable initialized by a non-constant-expression, Mesa emits an error. Here's the example code:

#version 420
uniform vec2 v;
const float x=v.x;

out vec4 color;
void main()
{
    color=vec4(x,v.y,0,1);
}

Here's how I test-compile (to avoid any OpenGL code):

$ glsl_compiler --version 420 test.frag 
Info log for test.frag:
0:3(15): error: initializer of const variable `x' must be a constant expression

If I move const float x=vx; line into the main function body, compilation ends successfully.

OTOH, nvidia driver, being (as usual) much more permissive, accepts the original code without warnings.

So, does GLSL 4.20+ actually prohibit non-constant-expression initializers of const -qualified variables in the global scope, or is this error a Mesa bug?

All versions of GLSL prohibit initializing a const qualified global variable with anything other than a constant expression (or for SPIR-V-bound GLSL, an expression involving constant expressions and specialization constants).

The only thing that changed in 4.20 regarding this was that, previously, any non-parameter variable declared const was restricted to initialization by constant expressions. 4.20 allowed function-local variables to be const while being initialized by non-constant expressions. But that's all.

Essentially, global const variables in GLSL are like C++11 constexpr variables. Local const variables are like C++ const variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM