GLSL,不同大小的纹理数组 [英] GLSL, Array of textures of differing size

查看:109
本文介绍了GLSL,不同大小的纹理数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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?

推荐答案

此语法无效:

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及以下版本不允许采样器数组.

GLSL 1.20 and below do not allow sampler arrays.

在GLSL 1.30至3.30中,可以有采样器数组,但是对索引有严格的限制.索引必须是整数常量表达式.因此,尽管您可以声明一个采样器数组,但不能对其进行循环.

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及更高版本允许索引为"动态统一的整数表达式".该术语基本上意味着着色器的所有实例化(在同一绘制调用中)必须具有相同的值.

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.

因此,您可以在GLSL 4.00+中的恒定范围内循环,并使用循环计数器为采样器数组建立索引.您甚至可以从统一变量中获取索引.您不能做的是让索引取决于着色器阶段的输入(除非该值在所有由渲染命令引起的实例中相同),或来自于纹理访问(除非该值在所有实例中都由渲染命令引起)都是相同的.

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.

对放置在采样器阵列中的纹理的唯一要求是它们与采样器类型匹配.因此,您必须在sampler2D数组的所有元素上使用GL_TEXTURE_2D.除此之外,纹理可以具有任何数量的差异,包括大小.数组的存在使编码更容易.它不会改变其中所含内容的语义.

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.

这篇关于GLSL,不同大小的纹理数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆