Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

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:
    4
    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.