在 WebGL 中一次渲染多个纹理是否会更快,例如:
varying float materialIndex;
varying vec2 textureCoord;
uniform sampler2D textureSampler1;
uniform sampler2D textureSampler2;
uniform sampler2D textureSampler3;
uniform sampler2D textureSampler4;
vec4 getMaterial(float materialId, textureCoord) {
vec4 color;
if (materialId == 1.0)
{
color = texture2D( textureSampler1, textureCoord );
}
else if (materialId == 2.0)
{
color = texture2D( textureSampler2, textureCoord );
}
else if (materialId == 3.0)
{
color = texture2D( textureSampler3, textureCoord );
}
else
{
color = texture2D( textureSampler4, textureCoord );
}
return color;
}
void main()
{
vec4 color = getMaterial(materialIndex, textureCoord);
gl_FragColor = color;
}
因为这将节省 CPU 必须发送到 GPU 的指令的四分之一,除了顶点着色器必须传递的额外信息之外,在大多数情况下,它不会比 4 倍的程序调用更快吗?我已经读到,即使在 opengl 中,迄今为止最大的性能损失也是 cpu,我敢打赌 Webgl 更是如此。或者这可能会更快?
uniform float materialIndex;
varying vec2 textureCoord;
uniform sampler2D textureSampler1;
uniform sampler2D textureSampler2;
uniform sampler2D textureSampler3;
uniform sampler2D textureSampler4;
void main()
{
vec4 color = getMaterial(materialIndex, textureCoord);
gl_FragColor = color;
}
或者就 CPU 必须执行的调用次数而言,更改功能统一与加载新着色器一样糟糕?