# Send an array of struct to compute shader

I am using Unity to send two types of data to the Compute Shader:

A struct:

struct Flock
{
public Vector3 position;
public Vector3 velocity;
}


And an Array of the above struct.

C# Code:

Flock[] ComputeFlock()
{
Flock[] output = new Flock[Amount];

// Flock struct
int size = sizeof(float) * 3 + sizeof(float) * 3;
ComputeBuffer buffer = new ComputeBuffer(_flockAI.Length, size);
buffer.SetData(_flockAI);

// Flock array
ComputeBuffer neighboursBuffer = new ComputeBuffer(_flockAI.Length, size * _flockAI.Length);
neighboursBuffer.SetData(_flockAI);

FlockComputeShader.Dispatch(kernel, _flockAI.Length / 16, 1, 1);
buffer.GetData(output);
buffer.Dispose();

return output;
}



#pragma kernel MoveFlock

struct FlockVec3Float
{
float3 position;
float3 velocity;
};

RWStructuredBuffer<FlockVec3Float> dataBuffer;

StructuredBuffer<FlockVec3Float> neighbours[1024];

{
for (uint i = 0; i < 1024; i++) {
dataBuffer[id.x].position.xyz = dataBuffer[id.x].position.xyz + neighbours[i].position.xyz;
}
}


I am trying to implemented a form of boids algorithm in the compute shader. The struct is the boid and the array of structs is to identify positions of it's neighbours. What I can't figure out is how I can send the full array of struct to the shader and have it iterate over it.

I can send the struct and have the shader read and write to it without any issues. Can someone point out what I might be doing wrong? I am wondering if it is something to do with the stride in the buffer. Thanks!

• As far as I can tell, your code is already sending the array correctly in the buffer.SetData(_flockAI) line. Could you elaborate as to what specific problem you are having? Also, I am curious as to why you are putting the same data into two separate buffers. That is, you could just iterate over the dataBuffer inside of the compute shader, as long as you are only writing into a single position from each thread. Jun 26 at 19:10
• @PepeOjeda Thank you, that makes a lot of sense. I didn't realize I could just iterate over the array buffer. Jun 27 at 20:31
• Hey, for some reason this popped up in my head while doing dishes today and I realized that I lied to you. Using two buffers (what you were initially doing) is actually correct. Using a single buffer causes a race condition, as you are reading a value that might or might not already have been modified by another thread. In fairness, this is not too important for something like Boids, as changes from one frame to the next are going to be small, but it is still wrong. Sorry! Jul 2 at 16:42

For anyone that might have a similar question, here's how I iterate over the buffer:

uint _length;
uint _stride;

Buffer.GetDimensions(_length, _stride);

for (uint i = 0; i < _length; i++)
{
Buffer[i];
}
$$$$
`
• If this solved your problem, be sure to click the checkmark icon on the left to mark it as "Accepted". Jul 27 at 22:42