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

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.