Search Unity

Best way to clear values in a computebuffer?

Discussion in 'Shaders' started by Brian-Kehrer, May 4, 2016.

  1. Brian-Kehrer

    Brian-Kehrer

    Joined:
    Nov 7, 2006
    Posts:
    411
    I have a compute buffer:
    RWStructuredBuffer<uint>

    I 'm using InterlockedOr to perform scene voxelization.

    This buffer never needs to leave the GPU.

    I've tried a couple approaches:

    A 1D compute shader that just sets all values to zero. This takes a ms or two - I think I can schedule this at the AFTER rendering completes, though, and it shouldn't have a large impact.
    _MyBuffer[id] = 0;

    Setting empty data every frame - this gets costly
    buffer.SetData(emptyData);

    Is there something else I should consider? Something clever? Or something obvious? The GL.Clear() equivalent for a buffer?

    Maybe I should just clear them in the compute shader after I've used them?
     
  2. scrawk

    scrawk

    Joined:
    Nov 22, 2012
    Posts:
    804
    There is no clear function like GL Clear for compute. You will have to use a compute shader to manually clear the contents.
     
    Brian-Kehrer likes this.
  3. Brian-Kehrer

    Brian-Kehrer

    Joined:
    Nov 7, 2006
    Posts:
    411
    Thanks - doing it in compute is easy enough.