Search Unity

Bug Instance ID Node always giving 0?

Discussion in 'Shader Graph' started by PraetorBlue, Nov 6, 2021.

  1. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Software versions:

    Unity 2021.2.0f1
    Universal RP 12.1.0
    Shader Graph 12.1.0

    EDIT: This seems to specifically be a problem with DrawMeshInstancedIndirect.

    Has anyone tried the new Instance ID node yet? I'm trying it and it seems to be returning 0 always. I'm drawing meshes with
    Graphics.DrawMeshInstancedIndirect(itemType.Mesh, 0, itemType.Material, bounds, drawArgsBuffer);
    . The material I'm using is using a shader graph and has GPU instancing enabled.

    I'm using this dumb thing just to test Instance ID right now. I expect that each instance would be offset by ever increasing amounts with this, but all the instances are being drawn at (0, 0, 0)
    upload_2021-11-5_21-42-26.png

    Am I doing something wrong? Or is there something wrong with this node?
     
    Last edited: Nov 6, 2021
  2. Warwlock

    Warwlock

    Joined:
    Aug 17, 2018
    Posts:
    40
    It is not a bug, you should disable SRP batcher when you want to use gpu instancing. They never work at the same time.

    To disable srp batcher in unity 2021.2.0f1, go to your urp asset (generally in the assets>settings folder). Under rendering, press 3 dots on the right. Select "Show Additinoal Properties". Then disable SRP batcher.
     
    PraetorBlue likes this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Thank you I will give that shot!
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Tried with SRP batcher disabled, no dice. Instance Id node is still giving me 0 for all instances.
    upload_2021-11-6_10-48-37.png
     
  5. Warwlock

    Warwlock

    Joined:
    Aug 17, 2018
    Posts:
    40
    I don't know how did you setup your project but this is the easiest way to Instancing is working or not (at least I think it easiest):

    Untitled-1.png

    Because of those 3 or 4 settings, I struggled to enable instancing before. I hope you it will work for you.

    And also try to duplicate your object, if there is only one object with that material it returns 0.
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    GPU instancing on the material is enabled
    SRP batcher is disabled on the render pipeline asset

    I am not using MeshRenderers. I am using
    Graphics.DrawMeshInstancedIndirect
    and I am using the Instance Id node in the vertex stage of the shader, not the fragment stage. Maybe it is broken only for the vertex stage?
     
  7. Warwlock

    Warwlock

    Joined:
    Aug 17, 2018
    Posts:
    40
    I haven't tried to draw any mesh via script before, but if you tried in another versions of unity and it was working, but not in this one, then it can be a bug.
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    This is the first version of Unity I've used that has had the Instance ID node in Shader Graph. I do believe it's a bug and it's quite frustrating! I'll try to make a simple example project and submit it.
     
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    While trying to create a sample project for this bug I ran into the following data:

    Instance ID works when using Mesh Renderers.
    Instance ID works when using DrawMeshInstanced.
    Instance ID is not working when using DrawMeshInstancedIndirect.

    My current working theory is that I need to somehow enable the PROCEDURAL_INSTANCING_ON pragma/flag in my shader graph. Is this possible?

    Oh wait - am I simply trying to use a currently unsupported feature? https://portal.productboard.com/uni...ral?utm_medium=social&utm_source=portal_share
     
    Last edited: Nov 6, 2021
    sthekken and Qriva like this.
  10. funkyCoty

    funkyCoty

    Joined:
    May 22, 2018
    Posts:
    727
    Is this seriously not supported still? Been debugging for awhile this morning trying to figure out why in the world my ShaderGraph is only drawing the first object in my buffer - searched online and found this thread. Why even give us an instanceID node if it doesn't actually work?
     
  11. sthekken

    sthekken

    Joined:
    Nov 17, 2018
    Posts:
    8
    It works for DrawMeshInstanced, as the post above mentioned. I agree though, it's a shame it isn't supported. I really like Shader Graph's ease of use and live feedback, very useful for creation, and not having access to the InstanceID for indirect draw calls means that you still have the CPU needing to bottleneck mesh draws by having to pass matrix data for each one. No cool grass for me :'(
     
  12. funkyCoty

    funkyCoty

    Joined:
    May 22, 2018
    Posts:
    727
    Yeah I realized this soon after I posted, should have edited with an update.

    Works for DrawMeshInstanced
    Does NOT work for DrawMeshInstancedIndirect or DrawMeshInstancedIndirectProcedural - indirect was what I needed though. So for supporting ShaderGraph users on the asset store, I simply have two rendering modes in my plugin. One for performance handicapped ShaderGraph users, and one for users writing their own shaders who can use DrawMeshInstancedIndirect.
     
  13. AG_in_person

    AG_in_person

    Joined:
    Jul 3, 2018
    Posts:
    5
    Hello.
    Similar situation here, stuck with:
    Graphics.DrawProceduralIndirect(...)

    If we cannot use the instance ID in this case, what's the workaround?
    Expanding my question: how can I write a shader that does what one expects from the above mentioned? I mean one that's lit, has shadows and uses the metallic workflow?
    Regards.

    EDIT: Using Unity 2022.2.8f1, URP 14.0.6, what else do we need to know?
     
    Last edited: May 20, 2023
  14. A12Acorns

    A12Acorns

    Joined:
    Jan 31, 2022
    Posts:
    5
    Bump, this should already be a supported feature for shadergraph.