WebGL着色器:可变变量的最大数量 [英] WebGL shaders: maximum number of varying variables
问题描述
从 OpenGL ES规范第2.10.4节(着色器变量:可变变量):
From the OpenGL ES spec section 2.10.4 (Shader Variables: Varying Variables):
可用于处理各种变量的插值器的数量由与实现相关的常量
MAX_VARYING_VECTORS
给出.
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.
着色器访问的变量值超过MAX_VARYING_VECTORS
的程序可能无法链接
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
变体.
In Chrome on my machine, gl.getParameter(gl.MAX_VARYING_VECTORS)
returns 15
, which means I can use 15 vec4
varyings in a shader.
我已经通过一些测试验证了这一点. 15 vec4
变体工作正常,但是尝试使用16时,程序无法链接,并且gl.getProgramInfoLog()
返回"Varyings over maximum register limit"
.
I've verified this with a few tests. 15 vec4
varyings work OK, but when attempting to use 16, the program fails to link and gl.getProgramInfoLog()
returns "Varyings over maximum register limit"
.
但是可以使用多少种类型的vec3
,vec2
或float
?
But how many varyings of type vec3
, vec2
or float
can be used?
OpenGL ES规范似乎暗示了这一点,而没有明确指出:
The OpenGL ES spec seems to hint at this, without being explicit:
任何变化的变量...都将计入此限制.
any varying variable ... will count against this limit.
其着色器访问的变量超过MAX_VARYING_VECTORS
worth 的程序可能无法链接
A program whose shaders access more than MAX_VARYING_VECTORS
worth of varying variables may fail to link
我正在做两个猜测:
- 变化的
float
的最大数量由下式给出:
MAX_VARYING_VECTORS * 4
(每个vec4
向量4个float
s) - 如果(例如)
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
s
- 8个
- The maximum number of varying
float
s is given by:
MAX_VARYING_VECTORS * 4
(4float
s pervec4
vector) - If (for example)
MAX_VARYING_VECTORS
is8
, then each of the following can safely be used without causing any linking errors:- 8
vec4
varyings - 10
vec3
varyings - 16
vec2
varyings - 32
float
varyings - 3
vec4
, 3vec3
, 3vec2
and 5float
varyings - 1
vec4
varying array of length8
- 1
vec3
varying array of length10
- 1
vec2
varying array of length16
- 1
float
varying array of length32
- Any other combination of
vec4
/vec3
/vec2
/float
variables or arrays, which uses a maximum of 32float
s
- 8
因此,根据我的MAX_VARYING_VECTORS
值15
,我猜我最多可以使用60个float
s.
我的测试似乎证实了这一点.
例如,在我的计算机上30个vec2
变体可以正常工作,但是31个变量会导致"Varyings over maximum register limit"
链接错误.
So with my MAX_VARYING_VECTORS
value of 15
, I guess I can use a maximum of 60 float
s.
My tests seem to confirm this.
For example, 30 vec2
varyings work OK on my machine, but 31 causes a "Varyings over maximum register limit"
linking error.
所以我的问题是:
- 我的两个猜测正确吗?
- 如果
MAX_VARYING_VECTORS
是8
,那么使用16个vec2
变体是否安全?这样可以保证始终有效吗?
- Are my two guesses correct?
- If
MAX_VARYING_VECTORS
is8
, then is it safe to use 16vec2
varyings? Is this guaranteed to always work?
推荐答案
来自WebGL规范
6.24制服和杂货的包装限制
6.24 Packing Restrictions for Uniforms and Varyings
OpenGL ES着色语言1.00版[GLES20GLSL],附录A,第7节可变和均匀计数"定义了一种保守算法,用于计算着色器中所有均匀变量和可变变量所需的存储量. GLSL ES规范要求,如果附录A中定义的打包算法成功,则着色器必须在目标平台上成功编译. WebGL API进一步要求,如果由于着色器的统一变量或程序的可变变量导致打包算法失败,则编译或链接必须失败.
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
s
So yes, if you read the algorithm if MAX_VARYING_VECTORS
is 8 you can use 16 vec2
s. You can not however use 10 vec3
s. You could only use 8 vec3
s
还有数组限制.例如,如果MAX_VARYING_VECTORS
为8,则不能有float
的数组大于8的数组,也不能有vec2
的数组大于8的数组.
There are also array restrictions. For example you couldn't have an array of float
s larger than 8 nor an array of vec2
larger than 8 if MAX_VARYING_VECTORS
is 8.
这篇关于WebGL着色器:可变变量的最大数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!