在片段着色器中,为什么不能使用平面输入整数来索引sampler2D的统一数组? [英] In a fragment shader, why can't I use a flat input integer to index a uniform array of sampler2D?
问题描述
这里是着色器的新功能,有点困惑.
new to shaders here, and a bit puzzled.
我想指定渲染精灵数组时要使用的纹理.因此,我在其顶点数据中放置了一个纹理索引,并将其作为一个平面值从顶点着色器传递到片段着色器,但是不能像预期的那样使用它为采样器数组建立索引,因为编译器将其视为非常数" ".相反,我不得不诉诸下面令人讨厌的代码.谁能解释这是怎么回事?
I want to specify textures to be used when I render an array of sprites. So I put a texture index in their vertex data, and pass it as a flat value from my vertex shader to my fragment shader, but can't use it to index an array of samplers as expected because compiler sees it as "non-constant". Instead I have to resort to the disgusting code below. Can anyone explain what is going on here?
const int numTextures = 2;
uniform sampler2D textures[numTextures];
in vec2 uv;
flat in int tex;
out vec4 colour;
void main(void)
{
// this caused the compiler error
/// "sampler arrays indexed with non-constant expressions"
// colour = texture( textures[ tex ], uv );
// hence this (ugh) ...
switch ( tex )
{
case 0:
colour = texture( textures[0], uv );
break;
case 1:
colour = texture( textures[1], uv );
break;
default:
colour = vec4( 0.3f, 0.3f, 0.3f, 1.0f );
break;
};
}
推荐答案
[...]但不能使用它为预期的采样器数组建立索引,因为编译器将其视为非恒定" [...]
[...] but can't use it to index an array of samplers as expected because compiler sees it as "non-constant" [...]
在版本3.30之前的GLSL和版本3.00以下的GLSL ES中,纹理采样器数组的索引必须是一个常量表达式:
In GLSL up to version 3.30 respectively GLSL ES up to version 3.00, the index of an array of texture samplers has to be a constant expression:
GLSL 3.30规范-4.1.7采样器(第21页) )
GLSL ES 3.00规范-4.1.7.1采样器(第29页) ):
在着色器中聚合成阵列的放大器(使用方括号[])只能用整数常量表达式[...]进行索引
Samplers aggregated into arrays within a shader (using square brackets [ ]) can only be indexed with integral constant expressions [...]
在更高版本中,采样器数组的索引必须动态统一".这意味着索引必须对所有片段都是相同的"(例如常量或统一变量).
In later version, the index to an array of samplers has to be "dynamically uniform". This means the index has to be the "same" for all fragments (e.g. a constant or a uniform variable).
GLSL 4.60规范-4.1.11.不透明类型(第31页)
GLSL ES 3.20规范-4.1.11.不透明类型(第32页)
在着色器中聚合到数组中时,不透明类型只能使用动态统一的整数表达式进行索引. [...]
When aggregated into arrays within a shader, opaque types can only be indexed with a dynamically uniform integral expression. [...]
[...]采样器类型(例如 sampler2D )是不透明的类型[...]
[...] Sampler types (e.g. sampler2D) are opaque types [...]
GLSL 4.60规范-3.8.2.动态统一表达式(第20页)
GLSL ES 3.20规范-3.9.3.动态统一表达式(第22页)
如果所有片段着色器表达式的求值都相同,则该片段着色器表达式是动态统一的.
A fragment-shader expression is dynamically uniform if all fragments evaluating it get the same resulting value.
对于单个图元,flat
片段着色器输入是不变的,但对于整个网格而言,则不是不变的,不是对于所有由单个绘制调用"处理的图元分别而言,对于调用组而言都是不相同的. (flat
)片段着色器输入不是动态统一
A flat
fragment shader input is invariant for a single primitive, but not for the entire mesh, not for all the primitives wich are processed by a single "draw call" respectively it is not the same for an invocation group. A (flat
) fragment shader input is not Dynamically uniform.
这篇关于在片段着色器中,为什么不能使用平面输入整数来索引sampler2D的统一数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!