Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
937 views
in Technique[技术] by (71.8m points)

opengl - Should I use the last GLSL version where it is possible?

My cross-platform graphics program uses shaders written in GLSL version 1.40. GLSL 1.40 features fully satisfy the needs of the application. In fact, very basic shaders are used for rendering textures:

vertex shader:

attribute vec2 position;
attribute vec2 textureCoordinates;

uniform mat3 transformationProjectionMatrix;

varying vec2 interpolatedTextureCoordinates;

void main() {
    interpolatedTextureCoordinates = textureCoordinates;
    gl_Position.xywz = vec4(transformationProjectionMatrix * vec3(position, 1.0), 0.0);
}

fragment shader:

precision mediump float;

uniform vec4 color;
uniform sampler2D textureData;

varying vec2 interpolatedTextureCoordinates;

void main() {
    gl_FragColor.rgba = color * texture(textureData, interpolatedTextureCoordinates).bgra;
}

Does it make sense to have several shaders in the program for different versions of GLSL that will be selected depending on the support on a specific hardware? Is it possible to get acceleration when using more modern versions of the shader language?

question from:https://stackoverflow.com/questions/65545736/should-i-use-the-last-glsl-version-where-it-is-possible

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Is it possible to get acceleration when using more modern versions of the shader language?

No, as long as your code doesn't use some functions available in newer GLSL version for optimization purposes, the performance should be practically the same.

Does it make sense to have several shaders in the program for different versions of GLSL that will be selected depending on the support on a specific hardware?

Normally, there should be no benefit in duplicating shader programs for different GLSL versions. OpenGL allows mixing GLSL version for different programs (but you shouldn't mix versions for different shaders of the same program), so that it makes sense using the lowest GLSL version supported by your graphics engine, and declare higher version only for specific optional GLSL programs requiring it.

Duplicating shaders for different GLSL versions is actually not needed, as the same source code can be compiled with different GLSL version header with some additional tricks / auxiliary macros, as GLSL keeps backward compatibility (the only major breakpoint was GLSL 150 / OpenGL 3.2 removing a bunch of functionality and changing GLSL syntax).

There are, however, bugs in OpenGL drivers that might change the view. For instance, I have experienced severe misbehavior of some GLSL programs compiled as #version 300 es on devices supporting OpenGL ES 3.0 (but not higher - e.g. these were the first drivers introducing OpenGL ES 3 support, and this support was quite buggy), while the same program compiled as #version 100 es worked as expected.

Another issue may strike, when you use some newer GLSL version feature without observing it. GLSL specification is quite a thing, and you may spend a lot of time as a bookworm just to know exactly which version added this particular feature or another - and some may surprise you. Beware that many drivers actually tolerate the usage of some new features without specifying an appropriate GLSL version (as long as hardware actually supports it). So that you may be confident that your GLSL program is #version 140 compliant, while actually it doesn't - this could be revealed suddenly by testing on another OpenGL driver.

In my experience, NVIDIA driver allows most severe deviations from GLSL specification (probably, because their GLSL compiler translates code to another language - Cg). AMD driver is more strict, but still allows some deviations. So far, less tolerant / most strict GLSL validator I've seen in macOS OpenGL drivers. In such conditions, specifying a higher GLSL version might be more safe / robust for compatibility with wider range of hardware, although this would just hide errors in your lowest "supported" GLSL version definition.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...