Aditya AGRAWAL
Aditya AGRAWAL

Reputation: 38

Accessing the index of an implicitly defined array in GLSL

I was trying to make a simple texture (fragment) shader that would loop through the uniform array atextures[] which is implicitly defined. The following code returns the following error

Code:

#version 330 core

out vec4 FragColor;
in vec2 TexCoord;

uniform sampler2D atextures[];
uniform int textureLength;

void main()
{
    for (int i=0; i<textureLength; i++){
        FragColor = texture(atextures[i], TexCoord);
    }
} 

Error:

Indirect index into implicitly-sized array

However when I change the index from i to 0, the following compiles fine. Did I setup the for loop wrong? Or did I initialise the array correctly?

Upvotes: 1

Views: 1352

Answers (1)

Rabbid76
Rabbid76

Reputation: 210908

You are getting the error because you have code that violates the specification. See the (most recent) OpenGL Shading Language 4.60 Specification - 4.1.9. Arrays:

It is legal to declare an array without a size (unsized) and then later redeclare the same name as an array of the same type and specify a size, or index it only with constant integral expressions (implicitly sized).


What you try to do is not the create an implicitly-sized array, but an dynamically-sized array.

It is not possible to create uniform array with a variable size. A variable size is just possible for the bottommost variable in a Shader Storage Block.
Anyway you should prefer to use an sampler2DArray instead of an array of sampler2D. With a sampler array, you must use a separate texture unit for each element, and the array index must be a Dynamically uniform expression

Upvotes: 2

Related Questions