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. Dismiss Notice

Question Issues with DrawProcedural / RenderPrimitives in 2022.3

Discussion in 'Shaders' started by paulatwarp, Sep 7, 2023.

  1. paulatwarp

    paulatwarp

    Joined:
    May 17, 2018
    Posts:
    131
    I'm trying to upgrade a project from 2021.3.22 to 2022.3.8. This project has a procedural instancing surface shader called with DrawProcedural. But it doesn't appear to work in 2022.3.8. It is either drawing only 1 instance - or always returning 0 as the instanceID in the shader.

    I thought it might be because DrawProcedural is obsolete in 2022.3.8 so I converted the code to RenderPrimitives, but it suffers the same issue.

    To diagnose, I took the example code from RenderPrimitives and converted the shader to a surface shader. This has the same problem in that it works in version 2021.3 but doesn't work in 2022.3. (One note is that I have to manually add the PROCEDURAL_INSTANCING_ON keyword to the material to get it to do anything on either version - is this an indication that I'm missing something from the shader?)

    Example procedural instancing surface shader below. Can you let me know if you think it should work - or should I report it as a bug?

    Code (ShaderLab):
    1. Shader "ProceduralSurface"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    8.         _Metallic ("Metallic", Range(0,1)) = 0.0
    9.     }
    10.     SubShader
    11.     {
    12.         Tags { "RenderType"="Opaque" }
    13.         LOD 200
    14.  
    15.         CGPROGRAM
    16.         #include "UnityInstancing.cginc"
    17.         // Physically based Standard lighting model, and enable shadows on all light types
    18.         #pragma surface surf Standard fullforwardshadows vertex:vert finalcolor:mycolor
    19.         #pragma instancing_options procedural:setup
    20.  
    21.         // Use shader model 3.0 target, to get nicer looking lighting
    22.         #pragma target 3.0
    23.  
    24.         sampler2D _MainTex;
    25.  
    26.         struct appdata
    27.         {
    28.             float4 vertex : POSITION;
    29.             float3 normal : NORMAL;
    30.             float4 texcoord : TEXCOORD0;
    31.             float4 texcoord1 : TEXCOORD1;
    32.             float4 texcoord2 : TEXCOORD2;
    33.             float4 tangent : TANGENT;
    34.             fixed4 color : COLOR;
    35.             uint vertexID : SV_VertexID;
    36.             UNITY_VERTEX_INPUT_INSTANCE_ID
    37.         };
    38.        
    39.         struct Input
    40.         {
    41.             float2 uv_MainTex;
    42.             fixed4 color;
    43.         };
    44.  
    45.         half _Glossiness;
    46.         half _Metallic;
    47.         fixed4 _Color;
    48. #if defined (SHADER_API_D3D11)
    49.         StructuredBuffer<int> _Triangles;
    50.         StructuredBuffer<float3> _Positions;
    51. #endif
    52.         uniform uint _StartIndex;
    53.         uniform uint _BaseVertexIndex;
    54.         uniform float4x4 _ObjectToWorld;
    55.         uniform float _NumInstances;
    56.  
    57.         //// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    58.         //// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    59.         //// #pragma instancing_options assumeuniformscaling
    60.         UNITY_INSTANCING_BUFFER_START(Props)
    61.             // put more per-instance properties here
    62.         UNITY_INSTANCING_BUFFER_END(Props)
    63.  
    64.         void setup() {}
    65.  
    66.         void vert(inout appdata v, out Input d)
    67.         {
    68.             UNITY_INITIALIZE_OUTPUT(Input, d);
    69.             UNITY_SETUP_INSTANCE_ID(v);
    70. #if defined(UNITY_PROCEDURAL_INSTANCING_ENABLED)
    71.             uint vertexID = v.vertexID;
    72.             uint instanceID = UNITY_GET_INSTANCE_ID(v);
    73.             float3 pos = _Positions[_Triangles[vertexID + _StartIndex] + _BaseVertexIndex];
    74.             float4 wpos = mul(_ObjectToWorld, float4(pos + float3(instanceID, 0, 0), 1.0f));
    75.             v.vertex = wpos;
    76.             d.color = fixed4(instanceID / _NumInstances, 0, 0, 1);
    77. #endif
    78.         }
    79.  
    80.         void mycolor(Input IN, SurfaceOutputStandard o, inout fixed4 color)
    81.         {
    82.             color.rgb = IN.color;
    83.         }
    84.  
    85.         void surf (Input IN, inout SurfaceOutputStandard o)
    86.         {
    87.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    88.             o.Albedo = c.rgb;
    89.             // Metallic and smoothness come from slider variables
    90.             o.Metallic = _Metallic;
    91.             o.Smoothness = _Glossiness;
    92.             o.Alpha = c.a;
    93.         }
    94.         ENDCG
    95.     }
    96.     FallBack "Diffuse"
    97. }
     
  2. paulatwarp

    paulatwarp

    Joined:
    May 17, 2018
    Posts:
    131
    I've also noticed that I'm getting different results with DrawProcedural and RenderPrimitives, are the instanceCount limits different for these calls?
     
  3. paulatwarp

    paulatwarp

    Joined:
    May 17, 2018
    Posts:
    131
    We've tracked this down to a specific Unity revision. It's working in 2021.3.29 and broken in 2021.3.30.
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,835
    Hi!
    Please submit a bug report.
    Thanks!
     
    paulatwarp likes this.
  5. paulatwarp

    paulatwarp

    Joined:
    May 17, 2018
    Posts:
    131
    IN-54270

    Thanks.
     
    aleksandrk likes this.
  6. paulatwarp

    paulatwarp

    Joined:
    May 17, 2018
    Posts:
    131
    It turns out the reason I was seeing a difference was because of the culling mode.
     
    aleksandrk likes this.