Search Unity

Command Buffer rendering performance issues

Discussion in 'General Graphics' started by plamenm, Sep 27, 2021.

  1. plamenm

    plamenm

    Joined:
    Jul 30, 2018
    Posts:
    2
    Hello, it's really nice that Unity exposed low-level Command Buffer API which can be used for rendering custom generated geometry.
    However we're experiencing some performance issues using Unity's API.
    In our product we have 2D rendering library which sends High-level rendering commands ((Un)MapVertex/IndexBuffer, SetRenderTarget, DrawIndexed, etc.)
    which we have to translate into Unity's Command Buffer commands.
    To set a pipeline state we're using different Materials where we set the properties for the state and cache it for later usage. The properties are:
    - Blend, BlendOp, ZTest, ZWrite, Cull, ColorMask,
    Stencil:
    {
    Ref,
    ReadMask,
    WriteMask,
    CompFront,
    PassFront,
    FailFront,
    ZFailFront,
    CompBack,
    PassBack,
    FailBack,
    ZFailBack,
    }
    We have 7 constant buffers and 4 pixel textures. To update them we're using a Material Property Block (https://docs.unity3d.com/ScriptReference/MaterialPropertyBlock.html) which is updated before every draw call.
    To simulate DrawIndexed command we've created a Mesh for every vertex buffer & index buffer pair and for every DrawIndexed we set a sub mesh using https://docs.unity3d.com/ScriptReference/Mesh.SetSubMesh.html which we draw using https://docs.unity3d.com/ScriptReference/Rendering.CommandBuffer.DrawMesh.html where we set the last requested material and pass the updated property block.

    The mean execution time on the Main Thread for 100 calls:
    - DrawMesh is around ~0.20ms.
    - SetSubMesh is around ~0.15ms

    Is there any way to optimize SetSubMesh & DrawMesh calls and is there a better approach to draw custom generated geometry?