Search Unity

Resolved How to reduce render batches like Official Sample Boids

Discussion in 'Graphics for ECS' started by FishFly, Dec 7, 2022.

  1. FishFly

    FishFly

    Joined:
    Nov 15, 2013
    Posts:
    8
    Hi.
    I have a demo to test DOTs code, it goes well.
    But there are more render batches(more instance call in FrameDebuger) than official sample Boids's even Sample Boids has much more Tris and instance data .
    I have no idea what happened and need help. Thanks.
    Here is a picture:
     

    Attached Files:

    • Why.png
      Why.png
      File size:
      711.6 KB
      Views:
      146
  2. bnmguy

    bnmguy

    Joined:
    Oct 31, 2020
    Posts:
    137
    For starters, why is your number of vertices more than double? This seems odd.
    Despite this, your cpu and gpu time indicate your scene is actually more performant. You also have less SetPassCalls, which are the more taxing Draw Calls/Batches, so that is great!

    The overhead of draw calls has been improved over time by Unity. It seems you also get a nice performance boost from DOTS as well.

    I'd worry less about the batch numbers and more about specific performance bottlenecks, should you encounter them.
     
    Last edited: Dec 7, 2022
    JussiKnuuttila and DevDunk like this.
  3. FishFly

    FishFly

    Joined:
    Nov 15, 2013
    Posts:
    8
    Thanks for your replay.

    The mesh is Unity default Cube mesh, It has double vertices indeed:)
    In DOTs Hybrid batch can reduce SetPass call. for more information :
    https://forum.unity.com/threads/confused-about-performance-of-srp-batching-vs-gpu-instancing.949185/

    There are 50000 fish instances and huge amount Tris and Verts in the Boids Sample runtime, low batch number for Hybrid Batch function i knonw, but it's not same effect in my demo, it confuses me. In the further it could be a problem when lots lots of Units appear in project.

    DOTs is perfect. Later DOTs Animation,Physics code will be tested too .
     
  4. FishFly

    FishFly

    Joined:
    Nov 15, 2013
    Posts:
    8
    I got the reason about the difference of batch number between two samples.
    Official Sample Boids initialize all unit instances once a time:
    Code (CSharp):
    1. var world = World.Unmanaged;
    2.                 var boidEntities = CollectionHelper.CreateNativeArray<Entity, RewindableAllocator>(boidSchool.Count, ref world.UpdateAllocator);
    3.  
    4.                 Profiler.BeginSample("Instantiate");
    5.                 EntityManager.Instantiate(boidSchool.Prefab, boidEntities);
    6.                 Profiler.EndSample();
    My Sample initialize a unit instance per a given time:
    Code (CSharp):
    1. float time = spawn.ValueRO.Time + deltaTime;
    2.             if(time < spawn.ValueRO.InterTime)
    3.             {
    4.                 spawn.ValueRW.Time = time;
    5.                 continue;
    6.             }
    7.             state.EntityManager.Instantiate(spawn.ValueRO.Prefab);
    8.  
    9.             spawn.ValueRW.Time = time - spawn.ValueRO.InterTime;
    Initialize entities frequently cause structure change propblem,
    it seems no problem to the unit archetypes chunk number, Dose it make some trouble to Hybird Batch Strategy?
     

    Attached Files:

    • why2.png
      why2.png
      File size:
      311.2 KB
      Views:
      99
  5. JussiKnuuttila

    JussiKnuuttila

    Unity Technologies

    Joined:
    Jun 7, 2019
    Posts:
    351
    Entities Graphics adds Entities to batches when they are first encountered, and does not currently attempt to merge batches that have already been created. If you are creating Entities over time, this can mean that you will end up with more batches than what you would get if you added all the Entities at once.

    If you want to, it should be possible for you to manually force Entities to be rebatched again. This can be done by setting the EntitiesGraphicsChunkInfo chunk component to
    default
    for any chunks of Entities that you want to re-batch. It's also possible to do the same by removing the chunk component, but this will cause some structural changes so it's less efficient.

    Based on your screenshots, it looks like your batches should still be well utilized (Chunk Capacity is high, Unused Entities is low), so I would expect CPU rendering performance to be good even with the increased batch count, as the draw calls are likely to have high instancing counts.
     
  6. FishFly

    FishFly

    Joined:
    Nov 15, 2013
    Posts:
    8
    Thank you JussiKnuuttila, It's very clear now.
    I will try the re-batch method to deepen my understanding. :):):)