Search Unity

Compute Shaders and varying output sizes

Discussion in 'Shaders' started by Atair, May 4, 2018.

  1. Atair

    Atair

    Joined:
    Oct 16, 2015
    Posts:
    42
    I started with compute shaders a few weeks back, and i think i got the basics down, but i am not sure how to approach this:

    Lets say i have a compute shader that does a calculation with a structured buffer as input, e.g. construct a kd-tree or determine which entries are culled by a camera.
    While the input is fixed in size, the output size may vary on the calculation.

    Haven't gotten there yet, but especially for culling, i read that i can have an buffer with transforms that i feed to a DrawMeshInstancedIndirect call..

    Now the question: How to make my buffer the right size? The input has a determined size (a struct array with my data), but what when my results are possibly bigger / smaller than that?

    The best i can come up with is to use a buffer that is for sure big enough, and some kind of bool mask to mark which entries are not to be processed (in the next step / shader / compute etc.)

    That seems not very efficient, and in the case of DrawMeshInstancedIndirect it would not work anyway as it needs a buffer with arguments, and that obviously needs to be the right size.
     
  2. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    you can create an append compute buffer. You allocate this big enough and add to this buffer in the compute shader.
    This buffer you can set on the material and use with DrawMeshInstancedIndirect.
     
  3. Atair

    Atair

    Joined:
    Oct 16, 2015
    Posts:
    42
    i suspected that the answer has to do with Append Buffers.. how do they behave when it comes to ordering? I guess order can't be guaranteed?

    E.g when i have a kd-tree with different sized leaves, in a single threaded approach i can fill a long array with all leaf data, and a second array with pointers to the starting index / count for each leaf.

    Probably that approach wouldn't work with AppendBuffers?

    Or put simpler - with appendbuffers it is possible to have dynamic sizes in one dimension, but what about two / more (equivalent to lists of lists in c#)

    Edit: I think i answered the question myself: HLSL has a set of atomic functions where you can globally increment decrement etc. https://msdn.microsoft.com/en-us/library/windows/desktop/ff476334(v=vs.85).aspx
    With that and Append Buffers it should be easy
     
    Last edited: May 4, 2018