Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Command Buffer : strange CPU "leak" ?

Discussion in 'General Graphics' started by NLongchamps, Sep 11, 2015.

  1. NLongchamps

    NLongchamps

    Joined:
    Sep 9, 2014
    Posts:
    10
    Hi,
    I have set up a command buffer for a fx, everything works fine visually. I set up and attach the command buffer once in a Start() function, and update a value per frame in a OnPreRender() function. Works.

    Problem is i noticed in the stats that CPU main steadily goes up over time. Not by much but creeps up a millisecond or so over every minute or two. It doesn't seem to stabilize at any point, just slowly goes up. The render thread is stable, memory is stable...

    Map starts at under a millisecond for CPU rendering. After a few minutes it is up to 2-3ms and still creeping up. In the profiler i can see this difference is caused solely by my Command Buffer. There's not much going on in my command buffer, memory is stable so buffers are being released... there's something computational going on but cant figure it out. Why would something cost more computationally from frame to frame?

    Anybody else experiencing this?
     
  2. NLongchamps

    NLongchamps

    Joined:
    Sep 9, 2014
    Posts:
    10
    So I've isolated the "leak" to a Buffer.SetGlobalMatrix() call which occurs per frame ( OnPreRender() ). Removing this line stabilizes the cpu render timing.

    I get the impression calling SetGlobalMatrix per frame is actually adding an instruction to set the matrix per frame, rather than updating... so by frame 100 i'm setting the matrix 100 times per frame... I tried adding 50 lines of SetGlobalMatrix to the call and i can see the cpu time creep up quite fast now.

    So i moved the SetGlobalMatrix call to the buffer setup, which happens once, thinking it would set the instruction once and then i just have to update the matrix and the buffer will read it per frame... except this doesn't seem to work. The matrix is set once and that's it.

    The way i understand command buffers, is its a list of commands to be executed per frame... so why is it not getting the matrix per frame? The matrix is updated pre-render, and command buffer occurs of course mid-render. Is this a bug? Can this be done without clearing the buffer and resetting the whole thing per frame?
     
  3. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    612
    It is as you said in your 2nd post. It's a list of commands, you're growing the list every frame. A commandbuffer doesn't 'update' the setmatrix so you can do things like setmatrix A, draw mesh A, setmatrix A to another matrix, draw mesh B.

    Matrices are structs, so they're not references to the original one but copied over by value, which is why it won't get your updated value every frame.

    You probably should make a second tiny buffer with solely the matrix, and .Clear() and set it every frame. Or if it's a value that only needs setting once, it may work with Shader.SetGlobalMatrix() instead of commandbuffers.

    Edit: didn't read carefully
     
    Last edited: Sep 12, 2015
    NLongchamps likes this.
  4. NLongchamps

    NLongchamps

    Joined:
    Sep 9, 2014
    Posts:
    10
    Thanks for confirming my suspicions, and for the tip about a secondary simpler command buffer. i didn't think of that!