Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Official Dynamic Batching Feedback

Discussion in 'General Graphics' started by mingwai, Sep 27, 2018.

?

Does dynamic batching helps optimising your project?

  1. It reduces draw calls but I didn't check on profiler numbers.

  2. It reduces draw calls and my game runs faster!

  3. No idea what dynamic batching is.

  4. I don't need it.

  5. It makes performance worse.

  6. I want to use it but my assets do not match the requirements to be batched.

Results are only viewable after voting.
  1. mingwai

    mingwai

    Small Graphics Potato Unity Technologies

    Joined:
    Jan 16, 2017
    Posts:
    52
    Dynamic batching is one of the optimisation feature in Unity which it batches the draw calls of dynamic objects. It exists for years so we believe a lot of you have heard of it / have been using it.
    We hope to gather feedback from users to see what will be the next step we should take.

    *If you think dynamic batching has been helping quite a bit in terms of actual performance gain, we would very much appreciate if you can leave a comment and tell us about what your use case + target platform is.

    Any comments / concerns / ideas are also welcomed. Thanks :)
     
    Lars-Steenhoff and debhbcunity like this.
  2. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    I would like to be able to set the vertex indices threshold without modifying source.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    We've pretty much always got it on, as it gives pretty good results for batch numbers - in our (completely unoptimized atm) game, it goes down from 13000 to 8000. The game's currently CPU bound, and we haven't started any kind of optimization, so the exact value of dynamic batching is hard to tell.

    We mostly have issues with the static batching, so if you're looking for input on that, give a call.
     
    MadeFromPolygons likes this.
  4. mingwai

    mingwai

    Small Graphics Potato Unity Technologies

    Joined:
    Jan 16, 2017
    Posts:
    52
    I'll highly recommend you to check on profiler numbers. Depending on the contents of dynamic batching might actually slow down the performance even though it reduces amount of draw calls. This is valid to all platforms.
     
  5. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    584
    If your game is CPU bound most likely disabling dynamic batching will help you. It's creating vertex data in CPU every frame.
     
    MadeFromPolygons likes this.
  6. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    Dynamic Batching does not work when having Graphics Jobs enabled (link).

    The vertex limit of DB was causing that many objects simply did not batch in our game. For example, our game uses a very simplistic art-style, where shading is simple, but meshes are still detailed. The "Mobile/Diffuse" shader seemed like a good fit at first.

    Frame Debugger then revealed that a lot of objects could not get batched during the ShadowCaster Pass, because meshes exceeded the 300 vertices limit. Switching to "Legacy Shaders/Diffuse", which supports GPU Instancing in the ShadowCaster Pass, reduced draw-calls and CPU Processing.

    DB might work well for rendering very simply geometry, like billboards, but we reach the 300 verts limit quite easily and thus it's not really working for us.

    GPU Instancing seems like the better option for our current PC/Console project.
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    That's strange - I did try to run both with dynamic batching on and off, but there was no difference. That being said;

    We do have Graphics Jobs turned on. Does this mean that the Dynamic Batching setting is simply ignored?
     
  8. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I didn't check but it's such a limiting feature that I turned it off and invested energy into instancing instead as to me that would be logically faster most of the time.
     
  9. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    According to the link I posted earlier, yes dynamic batching does not work with graphics jobs turned on in a build/player. It does work in the editor. Not sure if this has been changed in the meantime, I doubt it.
     
  10. mingwai

    mingwai

    Small Graphics Potato Unity Technologies

    Joined:
    Jan 16, 2017
    Posts:
    52
    Graphics Jobs is not supported in Editor so dynamic batching will work.
     
    Peter77 likes this.
  11. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Is there an advantage of dynamic batching over GPU instancing?
     
  12. jRocket

    jRocket

    Joined:
    Jul 12, 2012
    Posts:
    699
    I don't have any numbers to back it up, but I think in some platforms like WebGL, draw calls have an especially high CPU cost- so dynamic batching would be worth it in those cases.
     
  13. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Wait... Are you saying GPU instancing is a lot of CPU-driven draw call and not a single one where the GPU render multiple times?
     
  14. jRocket

    jRocket

    Joined:
    Jul 12, 2012
    Posts:
    699
    No, I was referring to dynamic batching. The advantage that dynamic batching has is that you can dynamically batch objects with different meshes(as long as they have the same material), while GPU instancing requires the same mesh.
     
  15. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,461
    We abused dynamic batching in The Walking Dead : March to War, and the details are in my Unite 2017 talk, but we had to modify source to raise the limit on attributes. We also had a persistant version of DrawMesh which didn't require us to do the costly resubmit each frame. However, it was really supporting openGLES 2.0 that pushed us down this route- had we been an opengles 3.0 and up game, instancing would have been a better choice. So I suspect when openGLES 2.0 games are a thing of the past, dynamic batching stops becoming useful.
     
    Lars-Steenhoff and hippocoder like this.
  16. Kronnect

    Kronnect

    Joined:
    Nov 16, 2014
    Posts:
    2,896
    Dynamic batching + geometry shaders are nice friends.
     
    Last edited: Oct 9, 2018
  17. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Geometry shaders aren't nice though, it is better to use 2x compute shaders if we are looking for performance, and on hardware where compute shaders are a good thing, dynamic batching is slower, so I cannot agree with both of your points.
     
  18. Kronnect

    Kronnect

    Joined:
    Nov 16, 2014
    Posts:
    2,896
    Well, didn't say it's the best approach but they work fine together for what they are designed. As you say, compute shaders were not supported or generally available.
     
    Last edited: Oct 9, 2018
  19. DavidSWu

    DavidSWu

    Joined:
    Jun 20, 2016
    Posts:
    183
    Dynamic batching is good for legacy drivers, terrible art assets, projects that do absolutely nothing on the CPU (or don't bother profiling the heavy CPU cases.) and budget projects that do not want to invest in more appropriate optimization techniques.

    I hate it. I would prefer it if you did not spend any more of your valuable Engineering time on supporting or improving it.
    There is always a better solution.
     
    mingwai likes this.
  20. fdsagizi2

    fdsagizi2

    Joined:
    Nov 4, 2013
    Posts:
    70
    of course no - because it take CPU time, we cannot use Instancing - because it takes CPU time! :(
     
  21. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    I have a problem the dynamic batching can not disable per-material.
    I can add the "DisableBatching" = "True" in the sub shader tag and this shader will not dynamic batching. But if I add the tag to the pass there is nothing happend. This is reasonable, disable batching should be in sub shader level.
    But when I use the Material.SetOverrideTag, it seems add the pass tag only, so I can not find a way to control the dynamic batching per material. I have to copy by shader with exactly the same code but just add the tag to the sub shader level, and let my material should a different shader.

    Is there a way to disable batching per material with the same shader?
     
  22. Torbach

    Torbach

    Joined:
    Jan 3, 2013
    Posts:
    10
    Isnt there always going to be a use case for having this option enable-able ? And other pipelines/techniques that allow it to be disabled
     
  23. BakeMyCake

    BakeMyCake

    Joined:
    May 8, 2017
    Posts:
    175
    After doing some testing of my own I can say that for my mobile game it reduced the number of batches considerably(from 130-140 batches down to 50-60), but in the actual game it manifested itself as a petty 8 or so additional average frames per second. But I guess a tiny bit of performance for free is better than nothing.
     
  24. DavidSWu

    DavidSWu

    Joined:
    Jun 20, 2016
    Posts:
    183
    You can usually find other ways to batch (but of course that is engineering time). For us, there is a very modest reduction in batches and no fps increase. There is an FPS loss when "bad" things happen and we have a lot of physics going on or other things that tax the CPU. This is the worse time to lose FPS - i.e. when you are already low.
    Also when you are CPU bound and you can't run in real time very bad things happen. With GPU you can at least drop resolution or skip frames.
     
  25. bluescrn

    bluescrn

    Joined:
    Feb 25, 2013
    Posts:
    642
    One thing that dynamic batching can help with is if you've used individual gameObjects/MeshRenderers for rendering a number of quads (or other very simple meshes) for something like camera-facing glows or blob shadows.

    While you can obviously write your own code to batch such things into a a single mesh, an advantage of leaving it to Unity is that you still get transparent object sorting (so if you've got some distant glow quads, then a big smoke particle effect in the middle distance, then some nearer glow quads, it'll behave itself better than if you batch all glows into one mesh and draw them together)
     
    BakeMyCake likes this.
  26. DavidSWu

    DavidSWu

    Joined:
    Jun 20, 2016
    Posts:
    183
    That is a fair and valid point. It can save engineering time if you are willing to accept "good enough". For many projects spending time writing custom, optimal solutions is not a luxury that the team can afford.
     
  27. blaze1

    blaze1

    Joined:
    Dec 25, 2014
    Posts:
    9
  28. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,051
    I think dynamic batching has its place, but so far I've found it to be more detrimental to performance than a benefit.

    One project where it would have been really useful I ended up writing my own custom batching method as the overhead of dynamic batching was absurd. I guess as in many cases its that developers frequently have more information than Unity as to the needs and requirements and as such a generalized solution rarely works out.

    In my cases its a modelling program using platonic solids ( so perfect for dynamic batching due to low vertex counts ), but with potential to place many thousands of objects. The update cost per a frame of dynamic batching hammered performance in this case, making it impractical. Which is a shame since frame to frame the scene data rarely if ever changes. If I could maintain the batched geometry between frames and issue calls to refresh it when required it would have worked far better and avoided having to write a custom batcher.

    My custom solution simply involved making 'mega' meshes that combined hundreds of models together and which would only be updated when adding/removing models to the scene. It was a bit more involved and required a lot of testing but thats the general gist.

    If i update the project now I'd probably switch to instancing, though that would mean increasing base system requirements on mobile. Assuming performance improves instancing would be a far better solution anyway since it would drastically cut down the memory requirements that a 'mega' mesh would have etc. This is however ignoring the potential issue of gameObject count, as i'm fortunate that the project does not manipulate or move the models/gameobjects once placed.
     
    Last edited: Jul 14, 2019
  29. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    Poll is missing an option for “it causes more problems than it solves”.
     
    Cynicat likes this.
  30. friuns3

    friuns3

    Joined:
    Oct 30, 2009
    Posts:
    307
    i would suggest first fix material GPU Instancing before removing actually working optimization features.
    material GPU Instancing not only decreases batches but also your fps
     
    Lars-Steenhoff likes this.
  31. Cynicat

    Cynicat

    Joined:
    Jun 12, 2013
    Posts:
    290
    I personally just build with instancing and manual batching in mind and i get much better performance. Dynamic batching is more cross-compatible but i almost never use it. Instancing has been faster for every use-case I've tested and involves less CPU overhead, especially when combined with DrawInstancedIndirect. Dynamic batching is better in the "just works" aspect, but I generally beat it by a mile with instancing and manual batching. =/
     
  32. ron-bohn

    ron-bohn

    Joined:
    Oct 5, 2015
    Posts:
    271
    For one, if you are targeting prior to dx11 (like dx9), then dynamic batching is better because GPU instancing won't work! So to reach older lower end devices...

    Dynamic batching also might be better if you have a wide diversity of moving/destrucable/dynamic etc prefabs with different meshes (but still share same material).

    GPU instancing might be a better choice if you want to draw thousands of the same moving object.

    Your project might have GPU instancing or maybe it doesn't. Either way, it's good to take advantage of sharing materials (atlases) whenever possible... whether game objects are dynamic, static, combined, or separate meshes.
     
    friuns3 likes this.
  33. AlejMC

    AlejMC

    Joined:
    Oct 15, 2013
    Posts:
    149
    I'm having troubles with Dynamic Batching.
    I'm putting "DisableBatching"="True" for the Shadowcaster pass tags, yet it behaves as if it were batched (doing some vertex based animation that expects the mesh's pivot to be left untouched).
    Maybe shadow caster batching can't be overridden? if there's any insight on that.

    EDIT: Unity 2021.1.14f1. Android build.