Search Unity

GPU instancing causing glitches

Discussion in 'General Graphics' started by bitinn, Apr 30, 2018.

  1. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    961
    Hi all,

    Has anyone seen this kind of rendering glitch in Unity 2017.4?

    - Macbook Pro + Intel GPU.
    - Using Deferred Shading.
    - Using Metal API on macOS.
    - Material has GPU instancing enabled.
    - Moving the mouse will cause glitch viewable in both Scene and Game view.



    If I disable GPU instancing on the material or disable Metal API support, this issue is gone.

    I have already submitted a bug report, just wondering anyone has seen this, or better, has a workaround that doesn't involve choosing between Metal API and GPU instancing...
     
    Last edited: Apr 30, 2018
  2. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    961
    Further troubleshooting suggest:

    The issue seems to take place when I use Amplify Shader Editor to lerp 2 colors using an alpha mask.

    Specifically:

    - using the alpha channel from a color node will trigger this.
    - using any channel from a texture sampler node will trigger this.

    (the annoying part is: a bare minimal shader graph won't trigger this bug, but I am unsure what other properties are needed, it seems to take place when I have a dozen of nodes...)
     
    Last edited: Apr 30, 2018
  3. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    961
    OK, looks like chaining multiple lerp together is triggering this bug in Unity.

    - I can chain about 4-5 lerp and output to albedo to trigger this bug.
    - Lerping normal map's R/G channel individually then unpack normal can also trigger this bug.

    There seems to be an invisible limit on how many "lerp" I can do in a shader graph when using GPU instancing and Metal together, which suggests this is a Unity problem...
     
  4. bricevdm

    bricevdm

    Joined:
    Nov 4, 2009
    Posts:
    34
    Hello,

    I ran into this exact issue today, and the fix was to use different Props struct for the instance properties: one for the vertex shader and another for fragment shader. It's fine for all other platforms, but somehow not on Metal.
     
  5. ViniciusGraciano

    ViniciusGraciano

    Joined:
    May 19, 2013
    Posts:
    13
    I don't think this is a bug, as I have seen it happening in shaders where instancing was not properly configured for use.

    If you are accessing instanced properties both in your vertex and fragment shaders, then you must make sure that you have:
    • In your vertex shader, UNITY_VERTEX_INPUT_INSTANCE_ID is declared in your input struct, UNITY_SETUP_INSTANCE_ID is used before accessing any instanced property, and UNITY_TRANSFER_INSTANCED_ID is called in order to feed the fragment shader input.
    • In your fragment shader, UNITY_VERTEX_INPUT_INSTANCE_ID is declared in your input struct, and UNITY_SETUP_INSTANCE_ID is used before accessing any instanced property.
    If any of these steps are missing, I usually get the flickering that you are seeing, in particular when feeding instanced data through a MaterialPropertyBlock. Probably, the shader cannot figure out the correct ID to access the constant buffer and your rendering result just go wild.
     
  6. bricevdm

    bricevdm

    Joined:
    Nov 4, 2009
    Posts:
    34
    This is a platform specific bug, if any of those were indeed missing it would consistently break on all platforms.

    As a rule of thumb, assuming people are doing something wrong when they most likely checked all obvious approaches is generaly not a good angle, online or at work ;)
     
  7. Coredumping

    Coredumping

    Joined:
    Dec 17, 2014
    Posts:
    51
    Ran into this today. Fix was to not mix different types of instance variables, e.g. instead of having a float, float and a float4, I changed it to two float4. Seems to have done the trick.