Hi, I am creating a simple shader for mobile that uses cutout, since almost all objects in scene uses this shader it needs to be really efficient.
Here is my fragment code:
fixed4 frag (v2f i) : SV_Target{
fixed4 col = tex2D(_MainTex, i.uv);
if (col.a < 0.7)discard;
return col*i.light;
}
But when i look at the compiled shader, the fragment function for gles is transformed into this:
void main(){
u_xlat10_0 = texture(_MainTex, vs_TEXCOORD0.xy);
#ifdef UNITY_ADRENO_ES3
u_xlatb1 = !!(u_xlat10_0.w<0.699999988);
#else
u_xlatb1 = u_xlat10_0.w<0.699999988;
#endif
if((int(u_xlatb1) * int(0xffffffffu))!=0){discard;}
u_xlat0 = u_xlat10_0 * vec4(vs_TEXCOORD1);
SV_Target0 = u_xlat0;
return;
}
Which uses way more operations than needed!
I debuged the project using an Android with Adreno Profiler, that allows me to change the shaders in realtime on the device, and to confirm that the generated code is inefficient I changed the function to this:
void main(){
u_xlat10_0 = texture(_MainTex, vs_TEXCOORD0.xy);
if(u_xlat10_0.w > 0.7){discard;}
u_xlat0 = u_xlat10_0 * vec4(vs_TEXCOORD1);
SV_Target0 = u_xlat0;
return;
}
With this change, my fps improved by 20%, and there is no visual difference, but I don’t know how to make those changes directly on the generated shader in unity.
Is there a way to do this? Or there is another solution to improve my cutout shader?