Search Unity

SetVectorArray not working as expected (compute shader)

Discussion in 'General Graphics' started by keromonkey, Dec 14, 2021.

  1. keromonkey

    keromonkey

    Joined:
    Jan 16, 2019
    Posts:
    42
    Hello all,
    I need to draw a large amount of moving objects, each of which has its own position and a target that is shared between groups of them.

    All objects are rendered with Graphics.DrawMeshInstanced successfully.
    Code (CSharp):
    1. Graphics.DrawMeshInstancedIndirect( Mesh, 0, Material, bounds, argsBuffer, 0);
    I've written a custom compute shader to move these objects.
    I pass in a small VectorArray of Vector4 positions (which I then constantly update right before compute shader dispatch), but it only seems to take the first value.

    This issue seems similar to this one here, but using a compute shader I can't set anything to "gpu instanced" to fix it (I tried with the material that draws the objects but not change).

    Have I missed something, or is compute.SetVectorArray() buggy in Unity2021.2 hdrp?

    EDIT (12/14/21): It turns out the SetVectorArray is being set correctly, its just not being indexed correctly for some reason. (I tested it by using explicit indices)...

    EDIT (12/14/21): Resolved. The issue was the data in the C# struct wasn't in the same order as the data in the compute shader struct :pepoblanket:
     
    Last edited: Dec 14, 2021
  2. mabulous

    mabulous

    Joined:
    Jan 4, 2013
    Posts:
    198
    SetVectorArray copies the elements, so changing the entries in your vector after calling SetVectorArray does nothing
     
  3. keromonkey

    keromonkey

    Joined:
    Jan 16, 2019
    Posts:
    42
    I understand that, which is why I set the values in the array. Then call set vector array with the updated list, then dispatch -- each update.

    Is that incorrect?
     
  4. keromonkey

    keromonkey

    Joined:
    Jan 16, 2019
    Posts:
    42
    Example:

    //Init function
    //x is of type global Vector4[]
    x = new Vector4[length].

    //update
    UpdateVec4ArrayValues()
    compute.SetVectorArray("targets", x)
     
  5. keromonkey

    keromonkey

    Joined:
    Jan 16, 2019
    Posts:
    42
    EDIT (12/14/21): It turns out the SetVectorArray is being set correctly, its just not being indexed correctly for some reason. (I tested it by using explicit indices)...