Search Unity

When is my mesh (re)sent to GPU? Need to optimize with compute buffer?

Discussion in 'General Graphics' started by mgstauff, May 24, 2019.

  1. mgstauff

    mgstauff

    Joined:
    Sep 6, 2017
    Posts:
    25
    Hi,

    For a game object with a mesh, I'm trying to understand the details of what changes to it will trigger a new draw call, and in particular a transfer of the mesh data to the gpu (I figure a mesh transfer is part of a draw call?). I've been looking at the unity graphics ref and elsewhere and can't figure it out. If anyone knows where to read about this, I'd love to know too.

    I figure a draw call can be made without transferring a mesh to the GPU? The docs for mesh.isReadable imply that a mesh can stay in GPU memory and be cleared from CPU memory when isReadable is true. In this case, once a mesh is on GPU and, for example, I change the transform of the mesh's gameObject, I figure a draw call is generated and just the transform data (or some subset of data < full game object with mesh) is sent to GPU??? In this case, if all I'm doing to a mesh each frame is updating position, it seems I could avoid the draw call by doing the move in the vertex shader?

    If the mesh is still in CPU (i.e. isReadable == false) and I change any part of the mesh (verts or vert attributes), even a single UV value, I figure that triggers a re-transfer of the full mesh to the GPU?

    If I run out of GPU memory, I figure the GPU starts dumping the least-recently used meshes first? Can I tell at runtime if my mesh is no longer in GPU memory?

    I'm trying to optimize some code that uses a custom shader and packs a bunch of stuff in mesh attributes to be used in the vertex and frag shaders. One of the UV attributes holds a param that gets updated for a small part of the mesh almost every frame. I figure this is triggering a resend of the whole mesh whenever it happens, and figure I probably want to use a compute buffer to handle this param so it will transfer faster to GPU?

    Thanks
     
  2. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,289
    Every frame your mesh is rendered it will make a drawcall. If you have changed the mesh on the CPU or it is the first frame it is used it will in addition to this upload the mesh to the GPU.
    This drawcall will happen on each frame even if you do not change the transform or any other setting on the Mesh.

    in some cases systems like dynamic batching, instancing etc will manage to render multiple similar meshes as a single drawcall.

    Shadows, multiple lights(Forward), projectors etc will create additional drawcalls as these will render the mesh again to create effects.
     
    mgstauff likes this.
  3. mgstauff

    mgstauff

    Joined:
    Sep 6, 2017
    Posts:
    25
    Thanks for the details.

    So if I change an object's transform there will be a draw call, but will the mesh *not* be uploaded again as long as it's still in gpu memory?

    I've been reading about instancing, batching, etc. - looks like I'll be able to utilize static batching for some of my needs by positioning my objects in my shader instead of via transform changes.
     
  4. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,289
    Yes. The mesh would be there already.
     
    mgstauff likes this.
  5. mgstauff

    mgstauff

    Joined:
    Sep 6, 2017
    Posts:
    25
    Thanks again!