Search Unity

Dynamic batching leads to a performance drop

Discussion in 'Editor & General Support' started by TheSaviour, Mar 12, 2019.

  1. TheSaviour

    TheSaviour

    Joined:
    Apr 20, 2015
    Posts:
    68
    I've been working on an platformer for android. My scene consists of a lot of platforms moving across the screen. I initially used the Legacy/Diffuse shader for all my platforms and enabled GPU instancing. The performance was fine. But then I tried to compare the performance with dynamic batching (with GPU instancing turned off)

    Turns out, the performance got worse. I noticed that every now and then, the platforms' movement get a bit jerky. I thought maybe I should use mobile shaders instead. The performance was still bad (once again, with dynamic batching enabled). So I connected my game to the profiler to find out what's going on.

    Here's what the profiler showed when I was using Mobile/VertexLit shader for all my platforms (with dynamic batching):

    DB_MVL.png

    Here's what the profiler showed when I was using Mobile/VertexLit (only 1 directional light) shader for all my platforms (with dynamic batching):

    DB_MVL (1DL).png

    Here's what the profiler showed when I was using Mobile/Diffuse shader for all my platforms (with dynamic batching):

    DB_MDiffuse.png

    As you can see, the game produces a lot of Gfx.WaitForPresent spikes, which leads to the stutter. But when I use GPU instancing instead of dynamic batching, those spikes don't show up as much (and when they do, they're not that big in size). Also, the performance is really good. Even when I'm using Mobile/BumpedSpecular (Only 1 directional light) shader (with GPU instancing), those spikes barely show up and the performance is really good.

    So is this normal? I was always under the impression that dynamic batching would be really good for my performance (and give better performance than GPU instancing). But it seems that's not the case.
     
  2. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,453
    Batching and instancing are not really my area of expertise but I'll try to hazard a guess:
    Assumption 1: Dynamic batching happens on the CPU
    Assumption 2: GPU Instancing happens on the GPU

    Assumption 3: By switching the work from the GPU to the CPU, you delay the point in time in which the CPU hands drawing over to the GPU which leads to the GPU missing a vSync point so you get a dropped frame and the Gfx.WaitForPresent.

    I'd look at this in Timeline view. Timing issues across threads and CPU/GPU are hard to decipher when just looking at the main thread samples shown in Hierarchy view.