Confusion over the OpenGL invariant classifier

So, I went through the orange book (3rd edition), and I came across a paragraph in chapter 9 about the invariant qualifier. And he says:

The defining invariant is instructed by the compiler and is associated with ignoring expressions and functions that are not directly related to calculating the output.

This snippet comes after two similar code snippets:

uniform mat4 MVPmatrix; // ... in vec4 MCVertex; // ... a(); // does not modify gl_Position, MVP or MCVertex // ... // Transform vertex to clip space gl_Position = MVP * MCVertex; 

and

 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 book further states:

The first case may or may not calculate the converted positions in the same way, regardless of which unrelated function or expression is associated with the shader. This can cause rendering problems if the multi-pass algorithm is used to render the same geometry more than once.

What bothers me. If a() no way affects the variables involved in the calculation of the transformed position, then how will the calculation vary? (And how exactly does adding invariant help with this?). And, citing the first quote, what exactly do they mean by "ignoring unrelated functions"? Are they just not running?

+9
opengl
source share
2 answers

The goal of invariant is to make sure that you do the calculation always, no matter what the shader optimizer does with the shader (especially in several shader compilations).

I find that the phrase of the orange book is bad (and, as you have noticed, be misleading). The GLSL specification (language 1.2) of section 4.6 is much clearer:

In this section, variance refers to the ability to obtain different values โ€‹โ€‹from the same expression in different programs. For example, let's say two vertex shaders, in different programs, each set of gl_Position with the same expression in both shaders, and the input values โ€‹โ€‹in this expression are the same when you run both shaders. this is possible due to the independent compilation of the two shaders, that the values โ€‹โ€‹assigned to gl_Position are not exactly the same when the two shaders work. In this example, this can cause geometry alignment problems in a multi-pass algorithm. In general, such a dispersion between shaders. When such a variance does not exist for a particular output variable, this variable is called invariant.

and it is further explained that the invariant qualifier provides you with guarantees to avoid this problem.

+10
source share

invariant keyword (in short and unlike the more detailed Bahbar answer) is more about the very subtle computational differences that may appear, as you already mentioned, about several passes of the geometry.

Here's an example: you draw an arbitrary, strange (to make it harder) triangle on the screen. The rasterizer gets the normalized vertices and calculates all the fragments that it occupies, and then runs the fragment shader on it. Now imagine that you would like to draw another triangle exactly on top of it, but after 3 hours, while your computer is immersed in water, the temperature drops, and you eat lunch at this time. Then you recompile the shaders, and bam ...

Everyone can potentially affect the rasterizer. Shader optimization can lead to minor changes in output. While still technically correct, the result does not have to be exactly the same as the first triangle, and the โ€œproblemsโ€ will represent some pixel from the first remaining.

invariant provides a possible slower approach. I am not a driver architect, mind you, but in general this can mean a few additional reset, cleanup, or sometimes full-time push / pops. Only then will you be left with a "clean" computing state, and while your equipment is in order, the result should be exactly the same.

+3
source share

All Articles