When we should use the program variants

hi guys

Can anyone help to explain what does program variant do? When we must add it into the code and when it’s not necessary .

Thanks in advance

 Pass{
            Tags{"LightMode"="ForwardBase"}

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            #include "Lighting.cginc"
            #include "AutoLight.cginc"

            #pragma multi_compile_fwdbase

            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed4 _Color;
            samplerCUBE _CubeMap;
            fixed _ReflectAmount;
            fixed4 _ReflectColor;

            struct a2v{
                float4 vertex:POSITION;
                fixed3 normal:NORMAL;
                float4 texcoord:TEXCOORD0;

            };
            struct v2f{
                float4 pos:SV_POSITION;
                float3 worldPos:TEXCOORD5;
                float2 uv:TEXCOORD0;
                fixed3 worldNormal:TEXCOORD1;
                fixed3 worldViewDir:TEXCOORD2;
                fixed3 reflDir:TEXCOORD3;
                fixed3 worldLightDir:TEXCOORD6;
                SHADOW_COORDS(4)
            };

            v2f vert(a2v v){
                v2f o;
                o.pos=UnityObjectToClipPos(v.vertex);
                o.worldPos=mul(unity_ObjectToWorld,v.vertex).xyz;
                o.uv=v.vertex.xy*_MainTex_ST.xy+_MainTex_ST.zw;
                o.worldNormal=mul(v.normal,(float3x3)unity_WorldToObject);
                o.worldViewDir=_WorldSpaceCameraPos-v.vertex.xyz;
                o.reflDir=reflect(-o.worldViewDir,o.worldNormal);
                o.worldLightDir=_WorldSpaceLightPos0.xyz;

                TRANSFER_SHADOW(o);
                return o;

            }

            fixed4 frag(v2f i):SV_Target{
                fixed3 worldNormal=normalize(i.worldNormal);
                fixed3 worldLightDir=normalize(i.worldLightDir);
                fixed3 albedo=tex2D(_MainTex,i.uv).rgb*_Color.rgb;
                fixed3 reflColor=texCUBE(_CubeMap,i.reflDir).rgb*_ReflectColor.rgb;
                fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;
                UNITY_LIGHT_ATTENUATION(atten,i,i.worldPos);
                fixed3 diffuse=_LightColor0.rgb*albedo*max(0,dot(worldNormal,worldLightDir));
                return fixed4(ambient+lerp(diffuse,reflColor,_ReflectAmount),1.0)*atten;
            }
            ENDCG

Hi!

In the v2f struct you have worldLightDir that uses TEXCOORD4 and SHADOW_COORD that also uses TEXCOORD4.
Change either one to 6 (an unused one) and you should be good.

thanks for reminding, I’ve changed the number.
and I want to ask when i must add the #pragma multi_compile_fwdbase and when it’s not so necessary. cause I found the code can be run well without it.