Search Unity

Question how to Get accurate instanceID cmd.DrawMeshInstanced?

Discussion in 'Universal Render Pipeline' started by luyu2020, Sep 27, 2022.

  1. luyu2020

    luyu2020

    Joined:
    Sep 1, 2021
    Posts:
    5
    I'm working on a grass render feature using draw mesh instanced to optimized performance, and many of my computation is based on instance ID.

    Although cmd.DrawMeshInstanced() API can support up to 1023 instances, when I profile it in frame debugger, I can see the actual draw call sometimes break into 2 in editor, and even more draw calls on my phone:
    DrawInstancedOnPhone.png

    When I tried to access instance ID (in my case I need it to vary from 0~1023), this draw call split causes trouble. Instance ID in vertex input structure got reset every draw call. And it seems there is some variable called "unity_BaseInstancedID" which can offset instance ID every draw call. I searched forum and notice this variable is handled by unity automatically.
    I inspected this value on frame debugger, and it does not change, you can see it in following pic:
    BaseInstanceIDFail.png

    I came up with two solutions:
    1. using mpb, and inject an int[], passing the id for each instance.
    2. figure out the instancing limitation for the platform app is running on, and split the draw call myself, pass my own "my_BaseInstancedID".
    But these are not elegant as instancing should be. I wonder if you guys have better ideas.