Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug 2022.1 shader graph - GPU instancing broken? Regression from 2021 LTS

Discussion in 'Shader Graph' started by rzubek, Jun 29, 2022.

  1. rzubek

    rzubek

    Joined:
    Aug 21, 2014
    Posts:
    72
    (Moving from the HDRP forum because this looks like a Shader Graph bug)

    I've been experiencing a big perf regression after upgrade to 2022, related to GPU instancing no longer working correctly in shaders compiled by Shader Graph.

    To repro, I have a test project started in 2021.3.2, which I upgraded to 2022.1.5, and now GPU instancing with instanced properties is broken.

    The test project spawns 20k objects, which have a Shader Graph shader that reads an instance variable called _Emissive_Multiplier, which gets passed in via MaterialPropertyBlock. In the shader, this property is defined as "shader declaration = hybrid per instance", and the material is set to enable GPU instancing.

    This worked correctly in 2021.3, all the instances got GPU instanced, they received their instance properties, and everything was drawn in a few dozen passes, like so:

    Screenshot A 1.png Screenshot A 2.png

    However, after upgrade to 2022.1, this is broken, and each individual building gets drawn in its own draw call:

    Screenshot B 1.png Screenshot B 2.png

    The frame debugger message "Non-instanced properties set for instanced shader" is confusing, because the instance property is set in the shader graph editor as "hybrid per instance", not a material property.

    Looking at compiled shader, it looks like the main difference between 2021 and 2022 is that in the old version, the instance property got correctly declared using UNITY_DEFINE_INSTANCED_PROP:

    Code (CSharp):
    1.         // Injected Instanced Properties (must be included before UnityInstancing.hlsl)
    2.         #if defined(UNITY_HYBRID_V1_INSTANCING_ENABLED)
    3.         #define HYBRID_V1_CUSTOM_ADDITIONAL_MATERIAL_VARS \
    4.         UNITY_DEFINE_INSTANCED_PROP(float, _Emission_Multiplier)
    5.         #define UNITY_ACCESS_HYBRID_INSTANCED_PROP(var, type) UNITY_ACCESS_INSTANCED_PROP(unity_Builtins0, var)
    6.         #else
    7.         #define UNITY_ACCESS_HYBRID_INSTANCED_PROP(var, type) var
    8.         #endif
    9.  
    10.         // -- Graph Properties
    11.         CBUFFER_START(UnityPerMaterial)
    12.     ...
    13.         #ifdef UNITY_HYBRID_V1_INSTANCING_ENABLED
    14.         float _Emission_Multiplier_dummy;
    15.         #else
    16.         float _Emission_Multiplier;
    17.         #endif
    18.         CBUFFER_END
    19.  

    Meanwhile in 2022 it gets added to CBUFFER instead, which is clearly incorrect, if it's to be an instanced property:

    Code (CSharp):
    1.         // -- Graph Properties
    2.         CBUFFER_START(UnityPerMaterial)
    3.     ...
    4.         // Hybrid instanced properties
    5.         float _Emission_Multiplier;
    6.         CBUFFER_END
    7.         #if defined(UNITY_DOTS_INSTANCING_ENABLED)
    8.         // DOTS instancing definitions
    9.         UNITY_DOTS_INSTANCING_START(MaterialPropertyMetadata)
    10.             UNITY_DOTS_INSTANCED_PROP(float, _Emission_Multiplier)
    11.         UNITY_DOTS_INSTANCING_END(MaterialPropertyMetadata)
    TLDR: looks like shader graph shaders compiled in 2022.1 break GPU instancing. Is there any fix for this?

    Because otherwise, that's a severe performance regression.
     
  2. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,296
    If you think this is bug and you have repro steps then report it properly, otherwise it's going to be ignored.
    I am not even sure if any SG dev reads this forum.
     
  3. rzubek

    rzubek

    Joined:
    Aug 21, 2014
    Posts:
    72
    Yeah, bug report submitted with a repro project - but I'm hoping someone in the community has run into this and maybe has a workaround?
     
  4. rzubek

    rzubek

    Joined:
    Aug 21, 2014
    Posts:
    72