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. Dismiss Notice

Question DrawMeshInstanced no more limit count?

Discussion in 'General Graphics' started by pan4ezzz, May 14, 2023.

  1. pan4ezzz

    pan4ezzz

    Joined:
    Jul 9, 2017
    Posts:
    19
    Hi Unity!
    I use 2022.2.12, and just try Graphics.DrawMeshInstanced() w/ million instances count and it work!

    I see some guard inside:
    Code (CSharp):
    1.  
    2. if (count < 0 || count > Mathf.Min(kMaxDrawMeshInstanceCount, matrices.Length))
    3. {
    4.     throw new ArgumentOutOfRangeException("count", $"Count must be in the range of 0 to {Mathf.Min(kMaxDrawMeshInstanceCount, matrices.Length)}.");
    5. }
    6.  
    But kMaxDrawMeshInstanceCount definitely not 1023, what is this miracle?
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,835
    Hi!
    We changed some internals in 2022, so it now uses 20 bits for array size.
    You are still limited by the constant buffer size (65536 bytes) if you're using them for instanced data, though.
     
    pan4ezzz likes this.
  3. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    817
    It wouldve been so nice if kMaxDrawMeshInstanceCount was public. That way i could just fill batches until that, achieving max performance for all platforms.

    Luckily with some InternalsVisibleTo we can get to it. In my Windows editor the max seems to be 1048575.
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,835
    As I said, it now uses 20 bits for the array size. Which gives 2^20 - 1 = 1048575.
     
    TJHeuvel-net likes this.