gpt4 book ai didi

opengl - 关于 OpenGL 不变限定符的困惑

转载 作者:行者123 更新时间:2023-12-02 13:34:44 24 4
gpt4 key购买 nike

所以我正在阅读橙皮书(第三版),我在第 9 章中发现了一段关于不变限定符的段落。它说:

The invariant qualifier instructs the compiler and linked to ignore expressions and functions that are not directly related to the computation of the output.

这段话出现在两个类似的代码片段之后:

uniform mat4 MVPmatrix;
// ...

in vec4 MCVertex;
// ...

a(); // does not modify gl_Position, MVP or MCVertex

// ...
// Transform vertex to clip space
gl_Position = MVP * MCVertex;

uniform mat4 MVPmatrix;
// ...

invariant gl_Position;
in vec4 MCVertex;
// ...

a(); // does not modify gl_Position, MVP or MCVertex

// ...
// Transform vertex to clip space
gl_Position = MVP * MCVertex;

这本书接着指出:

The first case may or may not compute the transformed positions in exactly the same way no matter what unrelated function or expression is linked to the shader. This can cause problems in rendering if a multipass algorithm is used to render the same geometry more than once.

这让我很困惑。如果 a() 绝对不会影响计算变换位置所涉及的变量,那么计算会如何变化? (添加invariant到底有何帮助?)。引用第一句话,“忽略不相关的功能”到底是什么意思?他们只是没有被处决吗?

最佳答案

不变的目的是确保您所做的计算始终会产生相同的结果,无论着色器优化器对着色器执行什么操作(尤其是跨多个着色器编译) .

我发现橙皮书的措辞很差(并且具有误导性,正如您所指出的)。 GLSL 规范(语言 1.2)第 4.6 节更加清晰:

In this section, variance refers to the possibility of getting different values from the same expression in different programs. For example, say two vertex shaders, in different programs, each set gl_Position with the same expression in both shaders, and the input values into that expression are the same when both shaders run. It is possible, due to independent compilation of the two shaders, that the values assigned to gl_Position are not exactly the same when the two shaders run. In this example, this can cause problems with alignment of geometry in a multi-pass algorithm. In general, such variance between shaders is allowed. When such variance does not exist for a particular output variable, that variable is said to be invariant.

它接着解释了 invariant 限定符可以保证避免这个问题。

关于opengl - 关于 OpenGL 不变限定符的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18994069/

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