opengl-es - WebGL 着色器 : maximum number of varying variables

标签 opengl-es opengl-es-2.0 webgl

来自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() 返回“变量超过最大寄存器限制”.

但是可以使用多少种 vec3vec2float 类型的变量?

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

我有两个猜测:

  1. 不同 float 的最大数量由以下公式给出:
    MAX_VARYING_VECTORS * 4
    (每个 vec4 向量 4 float)
  2. 如果(例如)MAX_VARYING_VECTORS8,则可以安全地使用以下各项,而不会导致任何链接错误:
    • 8 vec4 变化
    • 10 vec3 变化
    • 16 vec2 变化
    • 32 float 变化
    • 3 vec4、3 vec3、3 vec2 和 5 float 变化
    • 1 vec4 长度可变的数组 8
    • 1 vec3 长度可变的数组 10
    • 1 vec2 长度可变的数组 16
    • 1 float 长度不同的数组 32
    • vec4/vec3/vec2/float 变量或数组的任何其他组合,其使用最大值32 个 float

因此,如果我的 MAX_VARYING_VECTORS 值为 15,我想我最多可以使用 60 个 float。 我的测试似乎证实了这一点。 例如,30 个vec2 变量在我的机器上工作正常,但 31 个会导致“变量超出最大寄存器限制” 链接错误。

所以我的问题是:

  • 我的两个猜测正确吗?
  • 如果 MAX_VARYING_VECTORS8,那么使用 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/

相关文章:

android 动态壁纸 水波纹效果

iphone - 如何在2个OpenGL上下文之间共享纹理?

iphone - 在 OpenGL ES 1.1 中创建闪电

android - AndEngine 导入 GLES2 项目未运行

java - Android 中 OpenGL ES 2.0 形状的翻译

javascript - 如何在 Three.js 中比较像素

javascript - 将高度图应用于 three.js 中的 SphereGeometry

android - OpenGL ES 1.x vs. 2.x : OpenGL ES 1. x 还值得学习吗?

android - 一个着色器中的多个纹理,在 2D 中使用 ZBuffer 和 alpha 混合

javascript - "no VBO bound to enabled vertex attrib index 0"尝试在WebGL中绘制简单的纹理