glsl-optimizer
glsl-optimizer copied to clipboard
Broken version check for support of sampler arrays in GLSL ES leads to incorrect error
bug.glsl
#version 100
#define SAMPLER_COUNT 2
uniform sampler2D samplers[SAMPLER_COUNT];
void main() {
vec4 sum = vec4(0.0);
for(int i = 0; i < SAMPLER_COUNT; i++) {
sum += samplers[i];
}
gl_FragColor = sum;
}
$ glslopt -2 -f bug.glsl
Failed to compile bug.glsl:
(7,9): error: sampler arrays indexed with non-constant expressions is forbidden in GLSL 1.30 and later
(7,2): error: operands to arithmetic operators must be numeric
However, according to the GLSL ES 2.0 spec Page 109 (115 in PDF): https://www.khronos.org/registry/OpenGL/specs/es/2.0/GLSL_ES_Specification_1.00.pdf:
5 Indexing of Arrays, Vectors and Matrices
[...] The following are constant-index-expressions:
- Constant expressions
- Loop indices as defined in section 4
- [...]
Samplers
GLSL ES 1.00 supports both arrays of samplers and arrays of structures which contain samplers. In both these cases, for ES 2.0, support for indexing with a constant-index-expression is mandated but support for indexing with other values is not mandated.
So this is a bug, because I'm using a constant-index-expressions, for which support is mandated by the GLSL spec.
I did look into it, and the broken check is here:
https://github.com/aras-p/glsl-optimizer/blob/d78c3d2f249aa870368ad320905bc954c47704f6/src/glsl/ast_array_index.cpp#L230-L250
This bug does not exist in a rebased fork with a more recent mesa: https://github.com/jamienicol/glsl-optimizer/ The fixed check is here.