In a fragment shader, why can’t I use a flat input integer to index a uniform array of sampler2D?

[…] but can’t use it to index an array of samplers as expected because compiler sees it as “non-constant” […]

In GLSL up to version 3.30 respectively GLSL ES up to version 3.00, the index of an array of texture samplers has to be a constant expression:

GLSL 3.30 Specification – 4.1.7 Samplers (page 21)
GLSL ES 3.00 Specification – 4.1.7.1 Samplers (page 29):

Samplers aggregated into arrays within a shader (using square brackets [ ]) can only be indexed with integral constant expressions […]


In later version, the index to an array of samplers has to be “dynamically uniform”. This means the index has to be the “same” for all fragments (e.g. a constant or a uniform variable).

GLSL 4.60 Specification – 4.1.11. Opaque Types (page 31)
GLSL ES 3.20 Specification – 4.1.11. Opaque Types (page 32)

When aggregated into arrays within a shader, opaque types can only be indexed with a dynamically uniform integral expression. […]

[…] Sampler types (e.g. sampler2D) are opaque types […]

GLSL 4.60 Specification – 3.8.2. Dynamically Uniform Expressions (page 20)
GLSL ES 3.20 Specification – 3.9.3. Dynamically Uniform Expressions (page 22)

A fragment-shader expression is dynamically uniform if all fragments evaluating it get the same resulting value.


A flat fragment shader input is invariant for a single primitive, but not for the entire mesh, not for all the primitives which are processed by a single “draw call” respectively it is not the same for an invocation group. A (flat) fragment shader input is not Dynamically uniform.

Leave a Comment