As far as I know the shader compiler is able to optimize the code like strip out unnecessary codes or looking for predictable results etc, but to what extend?
Consider the following two vertex shaders:
v2f o;
o.color = mul(unity_ObjectToWorld, v.vertex);
o.vertex = UnityObjectToClipPos(v.vertex); //<--- Calculate from object space
return o;
v2f o;
o.color = mul(unity_ObjectToWorld, v.vertex);
o.vertex = UnityWorldToClipPos(o.color); //<--- Starts from world space calculated
return o;
The two shaders both calculate world space position, but the first one uses UnityObjectToClipPos() which has world space calculation in it, where as the second one directly uses UnityWorldToClipPos(). I would expect the compiler to detect the repeated pattern in the first shader and optimize it to make it look something like the second one. However, by looking at the compiled code the second one definitely has less instructions then the first one. So my question is to what extend can I rely on the compiler and is there a general rule of thumb in terms for writing optimized shader code?