Rendering Issues for Override Material with non-shadergraph-shader

I am trying to get a shader to work for rendering entities with an object render pass for object picking. It is supposed to render every entity in a fixed color, but it only considers some faces and the box on the left does not even have entities behind it.


Using a Shadergraph-Shader instead renders the correct parts of the screen (but has color inaccuracies), so my assumption was that something in this shader is missing. What am I doing wrong here?

Shader "ObjectPicking/TestDotsSimple"
{
    SubShader
    {
        Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 4.5
            #pragma multi_compile _ DOTS_INSTANCING_ON

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl"

            struct Attributes
            {
                float4 positionOS   : POSITION;                 
            };

            struct Varyings
            {
                float4 positionHCS  : SV_POSITION;
            };            

            Varyings vert(Attributes IN)
            {
                Varyings OUT;
                OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
                return OUT;
            }

            float4 frag() : SV_Target
            {
                return float4(0,0,1,0);
            }
            ENDHLSL
        }
    }
}

Got it working with the help of the example shader in Library/PackageCache/com.unity.render-pipelines.core/Tests/Editor/SimpleDots.shader. It used UNITY_SETUP_INSTANCE_ID which is not explained anywhere and seems to be some legacy functionality? I also can’t find it in the shadergraph-generated code. It seems required to make it work, however.

Working Shader:

Shader "ObjectPicking/ObjectPicking"
{
    SubShader
    {
        Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }

        Pass
        {
            Name "ObjectPickPass"
            HLSLPROGRAM
            #pragma exclude_renderers gles
            #pragma vertex vert
            #pragma fragment frag
            #include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DOTS.hlsl"

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            
            struct appdata
            {
                float4 vertex : POSITION;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            v2f vert (appdata v)
            {
                UNITY_SETUP_INSTANCE_ID(v);
                v2f o;
                UNITY_TRANSFER_INSTANCE_ID(v, o);
                o.vertex = mul(UNITY_MATRIX_VP, mul(UNITY_MATRIX_M, float4(v.vertex)));
                return o;
            }

            #ifdef UNITY_DOTS_INSTANCING_ENABLED
                UNITY_DOTS_INSTANCING_START(MaterialPropertyMetadata)
                    UNITY_DOTS_INSTANCED_PROP(int2, _EntityId)
                UNITY_DOTS_INSTANCING_END(MaterialPropertyMetadata)
            #endif

            float4 frag(v2f input) : SV_Target
            {
                UNITY_SETUP_INSTANCE_ID(input);
                #if defined(UNITY_DOTS_INSTANCING_ENABLED)
                int2 id = UNITY_ACCESS_DOTS_INSTANCED_PROP_WITH_CUSTOM_DEFAULT(int2, _EntityId, int2(-404, 404));
                return float4(id.xy, 0.0, 0.0);
                #else
                return float4(-404, 404, 0, 0);
                #endif
            }
            ENDHLSL
        }
    }
}