关于OpenGL不变限定词的困惑 [英] Confusion about the OpenGL invariant qualifier

查看:108
本文介绍了关于OpenGL不变限定词的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我浏览了橙皮书(第3版),并且在第9章中遇到了有关不变式限定词的一段内容.它说:

So I was going through the orange book (3rd edition) and I came across a passage in chapter 9 about the invariant qualifier. And it says:

不变限定符指示编译器和链接器忽略与输出的计算不直接相关的表达式和函数.

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

此段出现在两个类似的代码段之后:

This passage comes after two similar snippets of code:

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对此有何帮助?).并提到第一个引号,它们忽略无关功能"到底是什么意思?他们只是不被处决吗?

Which has me confused. If a() in no way affects the variables involved in calculating the transformed position, then how would the computation vary? (And how exactly does adding invariant help with that?). And referring to the first quote, what exactly do they mean by "ignoring the unrelated functions" ? Do they just not get executed?

推荐答案

invariant的目的是确保无论着色器优化器如何执行,所执行的计算始终会得到相同的结果.着色器(尤其是跨多个着色器编译).

The purpose of invariant is to make sure the computation you're doing will result in the same result always, no matter what the shader optimizer will do to the shader (notably across multiple shader compilations).

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

I find the phrasing of the orange book to be poor (and misleading, as you've noted). The GLSL specification (language 1.2) section 4.6 is much clearer:

在本节中,差异是指获得 不同程序中同一表达式的值不同.为了 例如,假设两个顶点着色器在不同的程序中各有一组 两个着色器和输入中具有相同表达式的gl_Position 当两个着色器都运行时,该表达式中的值相同.它是 由于两个着色器的独立编译,因此可能 当两者分配给gl_Position的值不完全相同时 着色器运行.在此示例中,这可能会导致对齐问题 多遍算法中的几何图形.一般来说,这种差异 允许在着色器之间进行切换.当这样的方差不存在时 特定的输出变量,该变量被称为不变的.

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限定词可以确保您避免此问题.

and it goes on to explain that the invariant qualifier gets you guarantees to avoid this issue.

这篇关于关于OpenGL不变限定词的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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