GLSL, Array of textures of differing size

让人想犯罪 __ 提交于 2019-12-17 22:42:45

问题


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?


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/12030711/glsl-array-of-textures-of-differing-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!