Search Unity

Question InstanceID resetting on 455th instance

Discussion in 'Shaders' started by Epineurien, Mar 29, 2023.

  1. Epineurien

    Epineurien

    Joined:
    Mar 9, 2013
    Posts:
    45
    When using Graphics.DrawMeshInstanced, the InstanceID wrap back to 0 at the 455th instance.
    Is this expected behavior, a bug, or some known issue?
    Because the documentation about it seems to imply that InstanceID should continue as expected, even when tasked with more than 1023 instances.

    It's a significant problem because I'm using shared textures to individualize each instance, and they can't fetch their own data if InstanceID is not behaving as expected.
    This behavior happen both with Unity's cube or a 18k vertices model, so I don't think it's a GPU limitation.
    It happen both with my full shader and a minimalist one that directly use the InstanceID for color (tested both in ShaderGraph and AmplifyShader).

    I could just change my code to make batch of 455 mesh before calling DrawMeshInstanced, but I need to know if it's stable behavior, or if that number will change depending on platform/GPU/etc.
    Or if I'm doing something terribly wrong.

    Happen with Unity 2022.1.10 with URP & ECS, in case it matter.
     
  2. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    The GPU will indeed wrap the
    SV_InstanceID
    , but Unity passes an offset in
    cbuffer UnityDrawCallInfo { int unity_BaseInstanceID; }


    Are you using FETCH_INSTANCED_PROP and all the other stuff from UnityInstancing.hlsl? That *should* handle the offsetting directly.
     
    Invertex likes this.
  3. Epineurien

    Epineurien

    Joined:
    Mar 9, 2013
    Posts:
    45
    ...I will be honest, I only recognized some of those words. And looking up "FETCH_INSTANCED_PROP" for documentation only return this very thread.

    Joke aside, even when including UnityInstancing, my custom node still get an undeclared identifier error for UNITY_GET_INSTANCE_ID - which from what I could find on the net is what I'm supposed to use. All infos I could find about this were about "manual" shader, is this something that just doesn't work in ShaderGraph? Because I haven't worked with those in ages and am completely rusted.
     
  4. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    I'm sorry; the property is
    UNITY_ACCESS_INSTANCED_PROP
    .

    You may want to ask about it here: https://forum.unity.com/forums/shader-graph.346/ .