ios/metal: z-pre pass doesn't work correctly with fast math enabled

We use z-pre pass in our application. It is well known that z-pre pass requires both shader passes to produce the exact same results for the position. In OpenGL this can be enforced using 'invariant gl_Position'. We have not found anything like that for Metal. We have observed that in iOS the z-pre pass does not work correctly when the shaders are compiled with fast math enabled (it works in iOS with fast math disabled and it works in OS X in both cases). Disabling fast math is too much of a performance penalty to be acceptable. Therefore we currently don't know how to solve this problem. Any suggestions and pointers are highly appreciated.

Replies

You can use the fma() built-in language function to perform fused multiply add operations instead of doing a * b + c. This works on scalars, vectors, and matrices.


There is some discussion about this here in the Advanced Metal Shader Optimization talk from WWDC16