GPU-Instancing and shadowcaster pass

So I implemented a custom shadowpass into my surface shader, in order to get LOD Dithering working also on the shadows, when objects fade out. I noticed, that i have to add instancing modifications to the shadowpass too, or else it will break the gpu instancing. I added the #pragma multi_compile_instancing to the shadowpass, which solves this problem, but now, shadows on objects pop in kind of randomly-

        Pass
        {
            Tags {"LightMode"="ShadowCaster"}

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_shadowcaster
            #pragma multi_compile_instancing
            #pragma multi_compile _ LOD_FADE_CROSSFADE
            #include "UnityCG.cginc"

            struct v2f {
                V2F_SHADOW_CASTER;
            };

            v2f vert(appdata_full v)
            {
                v2f o;
                o.pos = ComputeScreenPos(UnityObjectToClipPos(v.vertex));
                TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
                return o;
            }

            float4 frag(v2f i) : SV_Target
            {
                #ifdef LOD_FADE_CROSSFADE
                    float2 vpos = i.pos.xy / i.pos.w * _ScreenParams.xy;
                    UnityApplyDitherCrossFade(vpos);
                #endif

                SHADOW_CASTER_FRAGMENT(i)
            }
            ENDCG
        }

Did I miss out something?

I know it’s almost a year later, but I just ran into the same problem (flickering LOD shadows with LOD cross fade in a custom surface shader).

What’s missing here is all the UNITY_VERTEX_INPUT_INSTANCE_ID and UNITY_TRANSFER_INSTANCE_ID(v, o) stuff from the manual page on GPU Instancing.
Add everything where it says “// necessary only if you want to access instanced properties in the fragment Shader.” to your shadow caster pass, and it should work.

3 Likes

Thanks! This actually saved me from a very big headache.