- Home /
What is more optimized: use manually the shader variables or declare it and then use
I'm trying to find out the best way to use the shader variables.
We have some kind of parameters which can be used in the shaders. For example some light parameters: _WorldSpaceLightPos0 - Directional lights: (world space direction, 0). Other lights: (world space position, 1).
So I have two ways: 1)
half3 lightPos = _WorldSpaceLightPos0.xyz;
o.diff = v.color * max(0, dot(o.worldNormal, lightPos));
o.lightDir = dot(o.worldNormal, normalize(lightPos + o.worldViewDir));
2)
o.diff = v.color * max(0, dot(o.worldNormal, _WorldSpaceLightPos0.xyz));
o.lightDir = dot(o.worldNormal, normalize(_WorldSpaceLightPos0.xyz + o.worldViewDir));
Could you please clarify which one will be more optimized?
Answer by RShields · Aug 15, 2018 at 05:06 AM
They should both compile to the exact same thing. You can check this with Show Generated Code in the Inspector.
This is because the compiler is pretty clever. While some are even better than others, all compilers are expected to handle cases like this by using two memory reads, rather than a read-write, read, read.
Probably the biggest optimizations available in shaders are to avoid branching and to use $$anonymous$$AD operations rather than ADD, $$anonymous$$UL. Some compilers are smart enough to figure out the latter, but the former is often just not feasible to optimize on the compiler side.
Thank you so much! Could you please clarify what is the $$anonymous$$AD operations? Just tried to google it but looks Like I'm doing something wrong.
$$anonymous$$ost graphics cards have the ability to perform a*x + b
in one step rather than doing a*x
then +b
. However, compilers have a hard time optimizing code. Here's an example:
float y = (x + 2.0) / 2.0;
will likely be converted to anADD
then aDIV
float y = (x + 2.0) * 0.5;
will likely be converted to anADD
then a$$anonymous$$UL
float y = (x / 2.0) + 1.0;
will likely be converted to aDIV
then anADD
float y = (x * 0.5) + 1.0;
will likely be converted to a$$anonymous$$AD
Therefore, the bottom code runs twice as fast as the top ones.