From the OpenGL ES spec section 2.10.4 (Shader Variables: Varying Variables):
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_VECTORSworth of varying variables may fail to link
In Chrome on my machine, gl.getParameter(gl.MAX_VARYING_VECTORS) returns 15, which means I can use 15 vec4 varyings in a shader.
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".
But how many varyings of type vec3, vec2 or float can be used?
The OpenGL ES spec seems to hint at this, without being explicit:
any varying variable ... will count against this limit.
A program whose shaders access more than
MAX_VARYING_VECTORSworth of varying variables may fail to link
I'm making two guesses:
floats is given by:MAX_VARYING_VECTORS * 4floats per vec4 vector)MAX_VARYING_VECTORS is 8, then each of the following can safely be used without causing any linking errors:
vec4 varyingsvec3 varyingsvec2 varyingsfloat varyingsvec4, 3 vec3, 3 vec2 and 5 float varyingsvec4 varying array of length 8
vec3 varying array of length 10
vec2 varying array of length 16
float varying array of length 32
vec4 / vec3 / vec2 / float variables or arrays, which uses a maximum of 32 floatsSo with my MAX_VARYING_VECTORS value of 15, I guess I can use a maximum of 60 floats.
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.
So my questions are:
MAX_VARYING_VECTORS is 8, then is it safe to use 16 vec2 varyings? Is this guaranteed to always work?varying – used for interpolated data between a vertex shader and a fragment shader. Available for writing in the vertex shader, and read-only in a fragment shader.
varying variables contain data shared from a vertex shader to a fragment shader. The variable must be written in the vertex shader and the read-only value in the fragment shader is then interpolated from the vertices which make up the fragment.
So, if you want to render two objects with two different shaders, you should bind one object (it's buffers actually), use first material ( gl. useProgram(shaderProgramA) ) and dispatch draw. Then bind second object, set second material ( gl. useProgram(shaderProgramB) ) and dispatch another call.
GLSL also allows user defined varying variables. These must be declared in both the vertex and fragment shaders, for instance: varying float intensity; A varying variable must be written on a vertex shader, where we compute the value of the variable for each vertex.
From the WebGL spec
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.
So yes, if you read the algorithm if MAX_VARYING_VECTORS is 8 you can use 16 vec2s. You can not however use 10 vec3s. You could only use 8 vec3s
There are also array restrictions. For example you couldn't have an array of floats larger than 8 nor an array of vec2 larger than 8 if MAX_VARYING_VECTORS is 8.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With