在片段着色器中,为什么我不能使用平面输入整数来索引 sampler2D 的统一数组? [英] In a fragment shader, why can't I use a flat input integer to index a uniform array of sampler2D?
问题描述
我想指定渲染精灵数组时要使用的纹理.所以我在它们的顶点数据中放置了一个纹理索引,并将它作为一个平面值从我的顶点着色器传递到我的片段着色器,但不能像预期的那样使用它来索引采样器数组,因为编译器认为它是非-常数".相反,我不得不求助于下面令人作呕的代码.谁能解释一下这里发生了什么?
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 which 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屋!