Search Unity

Is it a bad idea to create a new CommandBuffer every frame?

Discussion in 'General Graphics' started by hazel_koop, Oct 20, 2020.

  1. hazel_koop

    hazel_koop

    Joined:
    Apr 9, 2019
    Posts:
    29
    I know this is kind of a vague question, and I intend to get into profiling if necessary, but I thought I'd at least check if the answer is a clear yes or clear no...

    I'm working on a game that draws with a fairly complex commandbuffer (~2kb). Until now, I've been able to create a single commandbuffer on startup, and only feed it changes by changing values on materials. However, I've just finally hit a situation where I think the only way to proceed is to make a change to the commandbuffer itself every frame.

    My questions are mostly:

    1) is compiling a new commandbuffer every frame a bad idea? I've seen some people complaining about the performance, but is this one of those things that's only a problem on mobile? (This project is for desktop and modern consoles...)

    2) is it a reasonable workaround if I split it into three commandbuffers, one of which is only the line that changes, and only recompile that middle one every frame?

    3) all I'm doing is changing the values of the camera matrix -- is there any way to just change the *values* that a CommandBuffer uses, without recompiling the buffer to do so? (Like how you can tell a commandbuffer to draw with a particular material, and then change the values in that material at runtime, but for values that I can't easily shoehorn into materials...)
     
  2. xgonzal2

    xgonzal2

    Joined:
    Jul 3, 2012
    Posts:
    62
    My advice would be to profile it once you've implemented the change and see for yourself. I've used command buffers for several years now and haven't really run into any major performance issue that I didn't create myself but it doesn't mean there aren't any issues with them. You should just create the command buffers you need on start and clear them when you have to before adding commands to them again. It should also be fine to split your work into multiple command buffers and only update the one.

    If you happen to work with SRP API they use a command buffer pool that gets cleared when it is released so, effectively they rebuild the command buffers per frame. You can also do something similar with the legacy pipeline if you needed to though a little more work has to be done to make it work as you have to bind those command buffers to a specific camera or light.
     
  3. JotaRata

    JotaRata

    Joined:
    Dec 8, 2014
    Posts:
    61
    Same question here:

    It is better to create the CommandBuffer inside a ScriptableRenderPass before any Execute method and clear it only inside the FrameCleanup method, or it is better to constantly create and release them only inside the Execute method?