Reputation: 77
In glsl 420 functionality was added to specify binding in the shader instead of having to call glUniform1i
. For example:
layout(binding = 0) uniform sampler2D u_Texture;
But how can that be done for arrays?
layout(binding ?) uniform sampler2D u_Textures[16];
I want the bindings to be 0,1,2,...,15. Is there no way for me to do this in glsl without any glUniform
calls?
Upvotes: 3
Views: 2019
Reputation: 210878
When used with OpenGL, if the binding qualifier is used with a uniform block or shader storage block instanced as an array, the first element of the array takes the specified block binding and each subsequent element takes the next consecutive binding point.
That means you just have to specify the binding point of the first sampler in the array:
layout(binding = 0) uniform sampler2D u_Textures[16];
But note, when samplers aggregated into arrays within a shader, these types can only be indexed with a dynamically uniform expression, or texture lookup will result in undefined values.
I recommend to use sampler2DArray
(see Sampler) rather than an array of sampler2D
.
See also:
Why are Uniform variables not working in GLSL?
In a fragment shader, why can't I use a flat input integer to index a uniform array of sampler2D?
Upvotes: 3