1
votes

I've got an OpenGL ES shader with some uniforms in it. I do some math on the uniforms in my fragment shader. Will the shader compiler generally optimize those expressions on the uniforms so they happen once instead of on every pixel? Or should I do the computations outside the shader and pass the results in?

Specifically, I've got three uniform coordinates I'm passing into the shader:

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

And then I compute some vectors among those points:

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

I'm curious if the shader compiler can optimize these such that they happen once per render, or if I should compute these outside the shader and pass them in as additional uniforms. Of course, for optimizations, I should really measure things to find the difference in my specific situation (since the GPU might be faster doing this on every pixel than my CPU), but I'm interested in how much scope the shader compilers have for optimizations like this in general.

Bonus points if you know that shader compilers on Android/iPhone devices might behave differently, or if different GLSL versions make a difference.

1

1 Answers

1
votes

Could a compiler optimize that? Yes, it could. Will it? Probably not, as this would require significant overhead when beginning to render an object (they have to pre-compute all the pre-computable stuff for shaders).