Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invariant and precise keywords in GLSL

I am trying to understand these two concepts. The manual I am reading is very brief on them and things like multipass algorithm are new to me. I would like to have some examples (not code) of where would I need to use invariant or precise variables, just to get a general idea so I can continue without leaving things behind.

When they say

GLSL does not guarantee that two identical computations in different shaders will result in exactly the same value

I don't see how and when would I need 2 identical computations in different shaders.

like image 291
ali Avatar asked Jan 11 '23 17:01

ali


1 Answers

These keywords are needed to avoid "cracking" and other visual artifacts that can arise from computations being done slightly differently in different shaders.

Rendered scenes are built up from a large number of "primitives" -- small pieces of image (generally triangles, sometimes more complex "patches") that are each rendered, with the accumulation making up the whole image. Where two primitives abut, it's crucial that both shaders involved calculate the border in exactly the same way. If not, then floating-point rounding may cause things to occasionally differ by a tiny amount, causing a "crack" where the background leaks through and becomes visible.

For simple (vertex+fragment) shader setups, the invariant keyword is enough for this -- you generally use it on the computations of vertex position in the vertex shader, and possibly for other varyings that have a similar sensitivity to rounding that can cause artifacts.

When you add in geometry and tesselation shaders, things get worse as the borders between the patches/primitives may be complex shapes rather than straight lines. It is often the case that one tesselation shader will calculate a border in one direction while the adjacent one calculates it in the other, so it is crucial to use a calculation that is fully symmetric and produces bit-identical results both ways. This is where the precise keyword comes into play, generally when you have computations like ab + cd. With precise that cannot be compiled as MUL + FMA, but must instead be MUL + MUL + ADD.

like image 180
Chris Dodd Avatar answered Feb 03 '23 14:02

Chris Dodd