Search Unity

GPU instancing not working with 'Legacy Shaders/VertexLit' shader.

Discussion in 'General Graphics' started by franpujolar, Dec 7, 2021.

  1. franpujolar

    franpujolar

    Joined:
    Dec 18, 2020
    Posts:
    9
    I have various meshes with the same material, which uses the 'Legacy Shaders/VertexLit' shader, this material has GPU instancing enabled, but for some reason it doesn't batch the meshes in the Frame Debugger, every mesh is drawn separately, so I have a lot of draw calls.

    What's weird, is that when I set this same material to use the 'Standard' shader, it starts working, and batches all the meshes correctly in a single draw call.

    Does anyone know why this happens? And how to make it work with the 'Legacy Shaders/VertexLit' shader?
     
  2. jubaerjams8548

    jubaerjams8548

    Joined:
    Jun 8, 2020
    Posts:
    41
    have you solved it?
     
  3. POOKSHANK

    POOKSHANK

    Joined:
    Feb 8, 2022
    Posts:
    307
    as far as i can tell, this shader doesn't support gpu instancing~ denoted in shaders by
    #pragma multi_compile_instancing

    i've added the functionality to my own shaders and it's quite simple.

    the vertex lit shader
    Unity-Built-in-Shaders/DefaultResourcesExtra/Normal-VertexLit.shader at master · TwoTailsGames/Unity-Built-in-Shaders · GitHub

    the standard shader (which has it)
    Unity-Built-in-Shaders/DefaultResourcesExtra/Standard.shader at master · TwoTailsGames/Unity-Built-in-Shaders · GitHub

    how to add
    Rendering 19 (catlikecoding.com)
     
  4. jubaerjams8548

    jubaerjams8548

    Joined:
    Jun 8, 2020
    Posts:
    41
    if you dont mind, could you please give me a specific example on how gpu instancing is implemented on that shader? Im in very need of it.
     
  5. POOKSHANK

    POOKSHANK

    Joined:
    Feb 8, 2022
    Posts:
    307
    the last link i sent has a very detailed explanation.

    i think it's about 5 lines in the shader.
     
    jubaerjams8548 likes this.
  6. jubaerjams8548

    jubaerjams8548

    Joined:
    Jun 8, 2020
    Posts:
    41
    Is there something im missing here?

    Code (CSharp):
    1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    2.  
    3. // Unlit shader. Simplest possible textured shader.
    4. // - SUPPORTS lightmap
    5. // - no lighting
    6. // - no per-material color
    7.  
    8. Shader "Heya" {
    9.  
    10. Properties {
    11.     _MainTex ("Base (RGB)", 2D) = "white" {}
    12. }
    13.  
    14. SubShader {
    15.     Tags { "RenderType"="Opaque" }
    16.     LOD 100
    17.  
    18.     // Non-lightmapped
    19.     Pass {
    20.         Tags { "LightMode" = "Vertex" }
    21.         Lighting Off
    22.         SetTexture [_MainTex] {
    23.             constantColor (1,1,1,1)
    24.             combine texture, constant // UNITY_OPAQUE_ALPHA_FFP
    25.         }
    26.     }
    27.  
    28.     // Lightmapped
    29.     Pass
    30.     {
    31.         Tags{ "LIGHTMODE" = "VertexLM" "RenderType" = "Opaque" }
    32.  
    33.         CGPROGRAM
    34.  
    35.         #pragma vertex vert
    36.         #pragma fragment frag
    37.         #pragma target 2.0
    38.         #include "UnityCG.cginc"
    39.         #pragma multi_compile_fog
    40.         #pragma multi_compile_instancing
    41.         #define USING_FOG (defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2))
    42.  
    43.         // uniforms
    44.         float4 _MainTex_ST;
    45.  
    46.         // vertex shader input data
    47.         struct appdata
    48.         {
    49.             float3 pos : POSITION;
    50.             float3 uv1 : TEXCOORD1;
    51.             float3 uv0 : TEXCOORD0;
    52.             UNITY_VERTEX_INPUT_INSTANCE_ID
    53.         };
    54.  
    55.         // vertex-to-fragment interpolators
    56.         struct v2f
    57.         {
    58.             float2 uv0 : TEXCOORD0;
    59.             float2 uv1 : TEXCOORD1;
    60. #if USING_FOG
    61.             fixed fog : TEXCOORD2;
    62. #endif
    63.             float4 pos : SV_POSITION;
    64.             UNITY_VERTEX_OUTPUT_STEREO
    65.         };
    66.  
    67.         // vertex shader
    68.         v2f vert(appdata IN)
    69.         {
    70.             v2f o;
    71.             UNITY_SETUP_INSTANCE_ID(IN);
    72.             UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    73.  
    74.             // compute texture coordinates
    75.             o.uv0 = IN.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    76.             o.uv1 = IN.uv0.xy * _MainTex_ST.xy + _MainTex_ST.zw;
    77.  
    78.             // fog
    79. #if USING_FOG
    80.             float3 eyePos = UnityObjectToViewPos(float4(IN.pos, 1));
    81.             float fogCoord = length(eyePos.xyz);  // radial fog distance
    82.             UNITY_CALC_FOG_FACTOR_RAW(fogCoord);
    83.             o.fog = saturate(unityFogFactor);
    84. #endif
    85.  
    86.             // transform position
    87.             o.pos = UnityObjectToClipPos(IN.pos);
    88.             return o;
    89.         }
    90.  
    91.         // textures
    92.         sampler2D _MainTex;
    93.  
    94.         // fragment shader
    95.         fixed4 frag(v2f IN) : SV_Target
    96.         {
    97.             fixed4 col, tex;
    98.  
    99.             // Fetch lightmap
    100.             half4 bakedColorTex = UNITY_SAMPLE_TEX2D(unity_Lightmap, IN.uv0.xy);
    101.             col.rgb = DecodeLightmap(bakedColorTex);
    102.  
    103.             // Fetch color texture
    104.             tex = tex2D(_MainTex, IN.uv1.xy);
    105.             col.rgb = tex.rgb * col.rgb;
    106.             col.a = 1;
    107.  
    108.             // fog
    109.     #if USING_FOG
    110.             col.rgb = lerp(unity_FogColor.rgb, col.rgb, IN.fog);
    111.     #endif
    112.             return col;
    113.         }
    114.  
    115.         ENDCG
    116.     }
    117. }
    118. }
     
  7. POOKSHANK

    POOKSHANK

    Joined:
    Feb 8, 2022
    Posts:
    307
    looks correct to me! just test it by duplicating an object and enabling instancing :)
     
    jubaerjams8548 likes this.
  8. jubaerjams8548

    jubaerjams8548

    Joined:
    Jun 8, 2020
    Posts:
    41
    it does not work, I took around 60 objects, no dynamic and static batching, and just gpu instancing enabled, Clicked on play button, and there are 65 drawcalls and saved 0. Im completely disappointed for two days, where switching to Mobile/diffuse shader at the same time shows me Gpu instancing working i mean 1 drawcall and saved 63. Idk what's wrong in my shader
     
  9. POOKSHANK

    POOKSHANK

    Joined:
    Feb 8, 2022
    Posts:
    307
    i didn't even notice earlier, but you have two passes in the shader! that would be why.

    add the instancing to the first pass as well and i believe that should fix your problem
     
  10. jubaerjams8548

    jubaerjams8548

    Joined:
    Jun 8, 2020
    Posts:
    41
    I removed the first pass after having the objects lightmapped, gpu instancing doesnt work somehow. Whatever, Thank you a lot, I will not use gpu instancing
     
    POOKSHANK likes this.