Skip to content

Broken version check for support of sampler arrays in GLSL ES leads to incorrect error #154

@ghost

Description

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:

if (array->type->element_type()->is_sampler()) {
if (!state->is_version(130, 100)) {
if (state->es_shader) {
_mesa_glsl_warning(&loc, state,
"sampler arrays indexed with non-constant "
"expressions is optional in %s",
state->get_version_string());
} else {
_mesa_glsl_warning(&loc, state,
"sampler arrays indexed with non-constant "
"expressions will be forbidden in GLSL 1.30 "
"and later");
}
} else if (!state->is_version(400, 0) && !state->ARB_gpu_shader5_enable) {
_mesa_glsl_error(&loc, state,
"sampler arrays indexed with non-constant "
"expressions is forbidden in GLSL 1.30 and "
"later");
}
}
}

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions