Search Unity

Multiple Command Buffers single command vs Single command buffer multiple commands

Discussion in 'General Graphics' started by Lockthav, Jan 22, 2020.

  1. Lockthav

    Lockthav

    Joined:
    Dec 6, 2018
    Posts:
    6
    Hello everyone,
    writing a script that attaches a command buffer in the main camera to draw some instanced elements.

    Doing something like:

    Code (CSharp):
    1. CommandBuffer commandBuffer1 = new CommandBuffer();
    2. commandBuffer1.DrawMeshInstanced( params1 );
    3. camera.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, commandBuffer1);
    4.  
    5. CommandBuffer commandBuffer2 = new CommandBuffer();
    6. commandBuffer2.DrawMeshInstanced( params2 );
    7. camera.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, commandBuffer2);
    Just wondering if change to a single command buffer:

    Code (CSharp):
    1. CommandBuffer commandBuffer = new CommandBuffer;
    2. commandBuffer.DrawMeshInstanced( params1 );
    3. commandBuffer.DrawMeshInstanced( params2 );
    4. camera.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, commandBuffer);
    There will be any performance improvement? What if (param1 == param2)?
    (the second seems more optimized due to the single command buffer but still two batches)
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Try both? I suspect a single command buffer will be faster too, but it might depend on what you're doing. I know there's a lot of code in the background for handing going in and out of command buffers, to ensure things like the current render target is set back to the camera, etc., so I definitely wouldn't expect multiple buffers to be better.
     
    MUGIK likes this.
  3. MUGIK

    MUGIK

    Joined:
    Jul 2, 2015
    Posts:
    481
    Maybe you know any way to chain command buffers together?
    For example, I have tons of objects to render. For each of them I cached a separate CommandBuffer that will render specific object to render target.
    I'm using separate command buffers and not a single one, because want to use some "fancy" culling, and exclude from rendering process objects that outside camera view.

    It's just an example and pretty much repeats the original question.
    But maybe you know a better way of doing something like that.
    Thanks
     
    Last edited: Mar 21, 2021