14

When doing multitexturing in GLSL, is there anyway to have an indexable array of samplers where each texture is a different size? This syntax isn't valid:

uniform sampler2D texArray[5];

Right now it seems like the only option is to individually create the samplers:

uniform sampler2D tex1;
uniform sampler2D tex2;
uniform sampler2D tex3;
uniform sampler2D tex4;
uniform sampler2D tex5;

But then I can't iterate through them, which is a real pain in the ass. Is there a solution?

2 Answers 2

27

This syntax isn't valid:

Says who? Arrays of samplers most certainly are valid (depending on the version). How you use them is a different matter.

GLSL 1.20 and below do not allow sampler arrays.

In GLSL 1.30 to 3.30, you can have sampler arrays, but with severe restrictions on the index. The index must be an integral constant expression. Thus, while you can declare a sampler array, you can't loop over it.

GLSL 4.00 and above allow the index to be a "dynamically uniform integral expression". That term basically means that all instantiations of the shader (within the same draw call) must get the same values.

So you can loop over a constant range in GLSL 4.00+, and index a sampler array with the loop counter. You can even get the index from a uniform variable. What you can't do is have the index depend on an input to the shader stage (unless that value is the same across all instances caused by the rendering command), or come from a value derived from a texture access (unless that value is the same across all instances caused by the rendering command), or something.

The only requirement on the textures placed in arrays of samplers is that they match the sampler type. So you have to use a GL_TEXTURE_2D on all the elements of a sampler2D array. Beyond that, the textures can have any number of differences, including size. The array exists to make coding easier; it doesn't change the semantics of what is there.

And remember: each individual element in the sampler array needs to be bound to its own texture image unit.

3
  • Can the textures in the various samplers in the array be differently sized? Also, where would one find this in the documentation? Aug 20, 2012 at 15:27
  • @grivescorbett: The GLSL specifications are actually surprisingly readable (even more surprising if you've ever tried reading the pre-4.3 OpenGL spec); I would suggest reading the appropriate version for your needs. It says this right there in the section titled "Samplers" whether you can have arrays and how they're indexed. And there's nothing in the spec that says that the textures in sampler arrays are connected in any way, so you can attach whatever sizes you want. As long as they're the right type (you can't use a 3D texture in a 2D sampler). Aug 20, 2012 at 15:39
  • @grivescorbett: Actually, I take some of that back slightly. The GLSL 1.20 spec doesn't seem to say that sampler arrays aren't possible (or if it does, then I didn't find where). The 1.30+ specs explicitly state that they are possible, as well as the conditions on their indicies. The reason I know 1.20 can't do it is because 1.30 explicitly states in its "changes from 1.20" section that texture arrays are now possible. Thus, they weren't possible before. Aug 20, 2012 at 15:47
2

is there anyway to have an indexable array of samplers where each texture is a different size?

Not yet. Maybe this gets added to a later OpenGL version down the road, but I doubt it.

But then I can't iterate through them, which is a real pain in the ass. Is there a solution?

As a workaround you could use Array Textures and use only subregions of each layer. Use a vec4 array to store the boudaries of each picture on each layer.

1
  • Thanks for the info, this is a sad deficiency of GLSL. Aug 20, 2012 at 0:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.