precise qualifier not working as expected


Consider this line of code:

gl_Position = (M1 * M2) * V;

M1 and M2 being two mat4, V a vec4.

While compiling, this line will be optimized by the driver to the equivalent of M1 * (M2 * V), replacing the matrix multiply by a cheaper vertex by matrix multiplication. Unfortunately this leads to an unacceptable loss of precision for our use case.

So I did this:

precise vec4 Vp = (M1 * M2) * V;
gl_Position = Vp;

The specs state that the first line should not be optimized and compiled as written in source, ie. performing the actual matrix multiply, followed by a vertex-matrix multiply. Unfortunately that’s not what is happening. The precise qualifier is ignored and the same optimization applied as in the first example.

If I write:

mat4 M0 = M1 * M2;
gl_Position = M0 * V;

then I get the expected result, even without the use of precise.

However this looks very unreliable, as nothing would technically prevent the driver from optimizing the above code.

Any suggestions on how to make this work reliably in production code ?