Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLSL, Array of textures of differing size

Tags:

opengl

glsl

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?

like image 720
grivescorbett Avatar asked Aug 19 '12 23:08

grivescorbett


2 Answers

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.

like image 109
Nicol Bolas Avatar answered Nov 19 '22 08:11

Nicol Bolas


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.

like image 35
datenwolf Avatar answered Nov 19 '22 07:11

datenwolf