Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Editing compiled shaders

Discussion in 'Shaders' started by z_master, Feb 1, 2018.

  1. z_master

    z_master

    Joined:
    Aug 12, 2015
    Posts:
    2
    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?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,229
    Not that I know of. You could see if you get better results if you write the shader in GLSL directly, or you could try using clip() instead of discard and see if the compiled shader produces something more efficient.