WebGL 着色器:可变变量的最大数量 [英] WebGL shaders: maximum number of varying variables

查看:20
本文介绍了WebGL 着色器:可变变量的最大数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 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".

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

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 值得,则可能无法链接

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 个 floats)
  2. 如果(例如)MAX_VARYING_VECTORS8,则可以安全地使用以下各项,而不会导致任何链接错误:
    • 8 vec4 变化
    • 10 个 vec3 变化
    • 16 vec2 变化
    • 32 浮动变量
    • 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 floats
  1. The maximum number of varying floats is given by:
    MAX_VARYING_VECTORS * 4
    (4 floats per vec4 vector)
  2. If (for example) MAX_VARYING_VECTORS is 8, 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, 3 vec3, 3 vec2 and 5 float varyings
    • 1 vec4 varying array of length 8
    • 1 vec3 varying array of length 10
    • 1 vec2 varying array of length 16
    • 1 float varying array of length 32
    • Any other combination of vec4 / vec3 / vec2 / float variables or arrays, which uses a maximum of 32 floats

所以我的 MAX_VARYING_VECTORS 值为 15,我想我最多可以使用 60 个 float.我的测试似乎证实了这一点.例如,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 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.

所以我的问题是:

  • 我的两个猜测对吗?
  • 如果 MAX_VARYING_VECTORS8,那么使用 16 个 vec2 变量是否安全?这可以保证始终有效吗?
  • Are my two guesses correct?
  • If MAX_VARYING_VECTORS is 8, then is it safe to use 16 vec2 varyings? Is this guaranteed to always work?

推荐答案

来自 WebGL 规范

From the WebGL spec

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 个 vec3s.你只能使用 8 个 vec3s

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

还有数组限制.例如,如果 MAX_VARYING_VECTORS 为 8,则 float 数组或 vec2 数组均不能大于 8.

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.

这篇关于WebGL 着色器:可变变量的最大数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆