Arrays GLSL数组元素算子在二维数组上的奇异行为
我目前正在编写一个简单的着色器,遇到了一个奇怪的问题 我基本上是使用取样器2D数组渲染一组纹理,从中选择所需的纹理Arrays GLSL数组元素算子在二维数组上的奇异行为,arrays,opengl,glsl,Arrays,Opengl,Glsl,我目前正在编写一个简单的着色器,遇到了一个奇怪的问题 我基本上是使用取样器2D数组渲染一组纹理,从中选择所需的纹理 #version 330 core layout (location = 0) out vec4 color; in DATA { vec4 position; vec4 color; vec2 texCoord; flat int texID; } fs_in; uniform sampler2D textures[16]; void ma
#version 330 core
layout (location = 0) out vec4 color;
in DATA
{
vec4 position;
vec4 color;
vec2 texCoord;
flat int texID;
} fs_in;
uniform sampler2D textures[16];
void main()
{
vec4 texColor = fs_in.color;
if (fs_in.texID > 0)
{
texColor = fs_in.color * texture(textures[fs_in.texID - 1], fs_in.texCoord);
}
color = texColor;
}
但是,这段代码在纹理上给了我奇怪的瑕疵:
现在是奇怪的部分:
替换
texColor = fs_in.color * texture(textures[fs_in.texID - 1], fs_in.texCoord);
有以下几点
for (int i = 0; i < 16; i++)
if (fs_in.texID - 1 == i)
texColor = fs_in.color * texture(textures[i], fs_in.texCoord);
for (int i = 0; i < 16; i++)
if (fs_in.texID - 1 == i)
texColor = fs_in.color * texture(textures[i], fs_in.texCoord);
for(int i=0;i<16;i++)
if(fs_in.texID-1==i)
texColor=fs_in.color*纹理(纹理[i],fs_in.texCoord);
修复了以下问题:
此外,此问题仅出现在使用OpenGL 4.5.13469 Compatibility Profile Context 21.19.525.258的基于AMD的计算机上
在我的Surface Pro上运行原始程序效果非常好
也许我遗漏了一些明显的东西,但我不知道为什么会出现这个问题,或者为什么更改会修复它。感谢您的解释
~z~斯通
在GLSL 3.30中,取样器阵列只能由用户访问。您的着色器不应编译
但让我们假设您的硬件支持GLSL 3.30以上。在这种情况下:
在GLSL 4.00+中,取样器阵列只能由用户访问。除非您只渲染一个三角形,或者每个三角形都有相同的fs\u in.texID
值,否则该表达式在动态上是不一致的。我猜不是,因为你不只是穿制服
如果使用非动态统一的表达式索引不透明类型数组,则会得到未定义的行为
有以下几点
for (int i = 0; i < 16; i++)
if (fs_in.texID - 1 == i)
texColor = fs_in.color * texture(textures[i], fs_in.texCoord);
for (int i = 0; i < 16; i++)
if (fs_in.texID - 1 == i)
texColor = fs_in.color * texture(textures[i], fs_in.texCoord);
或者,只需删除该条件,使纹理的第0层仅包含一个,并始终从纹理中提取(因为有条件地这样做):
好的,我需要一个常数索引,我知道了。我做了一些挖掘,根据附录第5节:“以下是常量索引表达式:[…]•第4节[…]中定义的循环索引”。这难道不意味着迭代纹理数组是有效的吗?(我完全理解使用sampler2DArray更好,只是好奇而已)@LevyEhrstein:OpenGLES2.0;它有不同的规则。
uniform sampler2DArray textures;
void main()
{
vec4 texColor = fs_in.color;
if (fs_in.texID > 0)
{
texColor = fs_in.color * texture(textures, vec3(fs_in.texCoord, fs_in.texID - 1));
}
color = texColor;
}
uniform sampler2DArray textures;
void main()
{
color = fs_in.color * texture(textures, vec3(fs_in.texCoord, fs_in.texID));
}