Suppose I have a fragment shader starting with #version 120
and a vertex shader with #version 150 compatibility
. This combination appears to work AFAICT, but does it go against the OpenGL or GLSL specifications? I couldn't find any information which discusses this issue. If there's none indeed, should I take it as "not forbidden" or as "not approved"?
No, there is no requirement that the shader versions match. There is a requirement that profiles match, but that actually only relates to mixing desktop and embedded GLSL. This is discussed briefly in the GLSL specification, but the implications of mixing different shader versions of compatible profiles are not fully discussed.
Shaders for the core or compatibility profiles that declare different versions can be linked together. However, es profile shaders cannot be linked with non-es profile shaders or with es profile shaders of a different version, or a link-time error will result. When linking shaders of versions allowed by these rules, remaining link-time errors will be given as per the linking rules in the GLSL version corresponding to the version of the context the shaders are linked under. Shader compile-time errors must still be given strictly based on the version declared (or defaulted to) within each shader.
You will run into syntax differences that could become an unwieldy mess between 1.20 and 1.50 however. For example, 150 compatibility
understands the use of in
and out
declared variables for shader stage I/O where as 120
must declare these as either attribute
or varying
. A varying
in a 1.20 vertex shader can pass data to an in
variable of the same name in a 1.50 fragment shader despite the different syntax.
There may also be minor changes to behavior of certain functions when you change shader version. GLSL 1.30, for example, stops respecting GL_DEPTH_TEXTURE_MODE
and always returns rrrr
... if you sample a depth texture from your 1.20 vertex shader and then again from the 1.50 fragment shader, you may very well get different results for the gba
part of the result. This sort of thing is not that common, but it's something to keep in mind.
Mixing #version 120
and #version 150 compatibility
in the same program is allowed. Let me cite the GLSL 4.50 specification, section 3.3 "Preprocessor" (emphasis mine):
Shaders for the core or compatibility profiles that declare different versions can be linked together. However, es profile shaders cannot be linked with non-es profile shaders or with es profile shaders of a different version, or a link-time error will result. When linking shaders of versions allowed by these rules, remaining link-time errors will be given as per the linking rules in the GLSL version corresponding to the version of the context the shaders are linked under. Shader compile-time errors must still be given strictly based on the version declared (or defaulted to) within each shader.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With