Can OpenGL Shader Compilers Optimize Form Expressions?

I have an OpenGL ES shader with some uniforms. I do math in form in my fragmentator. Will the shader compiler even optimize these expressions in the form so that they happen once, and not on every pixel? Or should I do the calculations outside the shader and pass the results to?

In particular, I have three identical coordinates that I pass to the shader:

uniform vec2 u_a; uniform vec2 u_b; uniform vec2 u_c; 

And then I compute some vectors among these points:

 vec2 v0 = u_c - u_a; vec2 v1 = u_b - u_a; 

I am curious if the shader compiler can optimize them so that they are executed once for each rendering, or if I have to calculate them outside the shader and pass them as an extra uniform. Of course, for optimization, I have to really measure things to find the difference in my specific situation (since the GPU could do it faster on every pixel than my processor), but I wonder how much shader compilers have the volume of such optimizations in general.

Bonus points if you know that shader compilers on Android / iPhone devices may behave differently or different versions of GLSL matter.

0
source share
1 answer

Can the compiler optimize this? Yes it is possible. Will he? Probably not, since this will require significant overhead when starting rendering the object (they must first calculate all the precomputable things for shaders).

+1
source

Source: https://habr.com/ru/post/970724/


All Articles