来自OpenGL ES spec第 2.10.4 节(着色器变量:可变变量):
The number of interpolators available for processing varying variables is given by the implementation-dependent constant
MAX_VARYING_VECTORS
.This value represents the number of four-element floating-point vectors that can be interpolated; varying variables declared as matrices or arrays will consume multiple interpolators.
When a program is linked, any varying variable written by a vertex shader, or read by a fragment shader, will count against this limit.
A program whose shaders access more than
MAX_VARYING_VECTORS
worth of varying variables may fail to link
在我机器上的 Chrome 中,gl.getParameter(gl.MAX_VARYING_VECTORS)
返回 15
,这意味着我可以使用 15 个 vec4
变量着色器。
我已经通过一些测试验证了这一点。 15 vec4
变量工作正常,但是当尝试使用 16 时,程序无法链接,并且 gl.getProgramInfoLog()
返回“变量超过最大寄存器限制”
.
但是可以使用多少种 vec3
、vec2
或 float
类型的变量?
OpenGL ES 规范似乎暗示了这一点,但没有明确说明:
any varying variable ... will count against this limit.
A program whose shaders access more than
MAX_VARYING_VECTORS
worth of varying variables may fail to link
我有两个猜测:
- 不同
float
的最大数量由以下公式给出:
MAX_VARYING_VECTORS * 4
(每个vec4
向量 4float
) - 如果(例如)
MAX_VARYING_VECTORS
为8
,则可以安全地使用以下各项,而不会导致任何链接错误:- 8
vec4
变化 - 10
vec3
变化 - 16
vec2
变化 - 32
float
变化 - 3
vec4
、3vec3
、3vec2
和 5float
变化 - 1
vec4
长度可变的数组8
- 1
vec3
长度可变的数组10
- 1
vec2
长度可变的数组16
- 1
float
长度不同的数组32
vec4
/vec3
/vec2
/float
变量或数组的任何其他组合,其使用最大值32 个float
- 8
因此,如果我的 MAX_VARYING_VECTORS
值为 15
,我想我最多可以使用 60 个 float
。
我的测试似乎证实了这一点。
例如,30 个vec2
变量在我的机器上工作正常,但 31 个会导致“变量超出最大寄存器限制”
链接错误。
所以我的问题是:
- 我的两个猜测正确吗?
- 如果
MAX_VARYING_VECTORS
为8
,那么使用 16 个vec2
变量是否安全?这能保证始终有效吗?
最佳答案
来自 WebGL 规范
6.24 Packing Restrictions for Uniforms and Varyings
The OpenGL ES Shading Language, Version 1.00 [GLES20GLSL], Appendix A, Section 7 "Counting of Varyings and Uniforms" defines a conservative algorithm for computing the storage required for all of the uniform and varying variables in a shader. The GLSL ES specification requires that if the packing algorithm defined in Appendix A succeeds, then the shader must succeed compilation on the target platform. The WebGL API further requires that if the packing algorithm fails either for the uniform variables of a shader or for the varying variables of a program, compilation or linking must fail.
所以,是的,如果您阅读了算法,如果 MAX_VARYING_VECTORS
为 8,则可以使用 16 个 vec2
。但是您不能使用 10 个 vec3。您只能使用 8 个 vec3
还有数组限制。例如,如果 MAX_VARYING_VECTORS
为 8,则 float
数组不能大于 8,vec2
数组也不能大于 8。
关于opengl-es - WebGL 着色器 : maximum number of varying variables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26682631/