gpt4 book ai didi

opengl-es - 在 OpenGL 着色器中在 main 之外进行计算是否合理?

转载 作者:行者123 更新时间:2023-12-04 23:53:06 25 4
gpt4 key购买 nike

我有一些类似于下面的顶点着色器代码(这是一个稍微简化的示例):

attribute vec2 aPosition;
attribute vec4 aColor;

varying lowp vec4 vColor;

uniform vec4 uViewport;
mat4 viewportScale = mat4(2.0 / uViewport.z, 0, 0, 0, 0, -2.0 / uViewport.w, 0,0, 0, 0,1,0, -1,+1,0,1);

void main() {
vec2 pos = aPosition;
gl_Position = viewportScale * vec4(pos, 0, 1);
vColor = vec4(aColor.rgb*aColor.a, aColor.a);
}

特别是,viewportScale 矩阵是根据uViewport 统一主函数之外 计算的。从浏览器 (WebGL) 使用它,它似乎在我测试过的每台机器上都能正常工作……特别是,当我更改 uViewport< 时,viewportScale 矩阵会正确更新变量。这样做和在 main 函数内部进行相同的计算有什么区别吗?我找不到与此相关的任何示例或讨论。

我遇到了一个 related problem这让我对这个问题有点偏执——至少,我想了解发生了什么。

最佳答案

这不是 GLSL ES 1.00 中的合法着色器,它是与 ES 2.0 一起使用的 GLSL 版本。 WebGL 共享相同的 GLSL 定义,但 WebGL 规范中指定了一些异常(exception)情况。我在 WebGL 规范中找不到这个异常(exception),所以我相信着色器在 ES 2.0 和 WebGL 中都是非法的。

摘自 GLSL ES 1.00 规范,第 29 页的“4.3 存储限定符”部分(添加了强调):

Declarations of globals without a storage qualifier, or with just the const qualifier, may include initializers, in which case they will be initialized before the first line of main() is executed. Such initializers must be a constant expression.

第 49 页的“5.10 常量表达式”一节定义了常量表达式是什么。它包括:

The following may not be used in constant expressions:

  • Uniforms, attributes and varyings.

您案例中的表达式包含一个制服,这使其成为一个非常量表达式。因此它不能用作全局变量的初始值设定项。

关于opengl-es - 在 OpenGL 着色器中在 main 之外进行计算是否合理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28076568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com