Can OpenGL shader compilers optimize expressions on uniforms?

可紊 提交于 2020-04-18 12:34:37

问题


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:


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).



来源:https://stackoverflow.com/questions/15688226/can-opengl-shader-compilers-optimize-expressions-on-uniforms

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!