为了给我的着色器提供纹理,我使用了 Sampler2D 类型的制服,但是,由于我有很多顶点都使用不同的纹理和不同的着色器,所以当我通过每个顶点都是正确的纹理时,我会遇到瓶颈。所以我尝试使用一个使用纹理的全局数组并将其作为布局传递给我的着色器:
layout (location = 6) in sampler2D aTexture;\n
问题是,我的着色器不会编译引发错误:
0(24) : error C7514: OpenGL does not allow varying of type sampler2D
0(24) : error C7554: OpenGL requires sampler variables to be explicitly declared as uniform
既然我们不能使用布局为着色器提供纹理,那么你们如何使用纹理来避开这些瓶颈?我被迫使用制服?
最佳答案
简而言之:不,您不能使用纹理采样器作为顶点输入。它们必须是统一的。
因此,有两种可能的解决方案,每种解决方案都更适合特定场景:
为此,您必须确保每个着色器程序彼此之间非常不同(例如,一些是不透明的,而另一些是透明的或经过 alpha 测试)。
但是如果有些只有不同的贴图(比如有些有法线贴图,而另一些没有),那么我建议您在不使用分支的情况下回收代码,例如在没有法线贴图时使用 1x1 像素法线贴图
这是代码的示例着色器片段:
uniform sampler2DArray myArrayTexture;
in vec2 texCoord;
...
vec4 color1 = texture(myArrayTexture, vec3(texCoord, 0)); // sampling index 0
vec4 color2 = texture(myArrayTexture, vec3(texCoord, 1)); // sampling index 1
以下是将数组加载到 GPU 中的方法:
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, width, height, 1, format, type, dataAtIndex0);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 1, width, height, 1, format, type, dataAtIndex1);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 2, width, height, 1, format, type, dataAtIndex2);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 3, width, height, 1, format, type, dataAtIndex3);
...
关于c++ - 将纹理传递给 GLSL 着色器而不使用统一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60099956/