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];
int kernel = FlockComputeShader.FindKernel("MoveFlock");
// Flock struct
int size = sizeof(float) * 3 + sizeof(float) * 3;
ComputeBuffer buffer = new ComputeBuffer(_flockAI.Length, size);
buffer.SetData(_flockAI);
FlockComputeShader.SetBuffer(kernel, "dataBuffer", buffer);
// Flock array
ComputeBuffer neighboursBuffer = new ComputeBuffer(_flockAI.Length, size * _flockAI.Length);
neighboursBuffer.SetData(_flockAI);
FlockComputeShader.SetBuffer(kernel, "neighbours", neighboursBuffer);
FlockComputeShader.Dispatch(kernel, _flockAI.Length / 16, 1, 1);
buffer.GetData(output);
buffer.Dispose();
return output;
}
Compute Shader:
#pragma kernel MoveFlock
struct FlockVec3Float
{
float3 position;
float3 velocity;
};
RWStructuredBuffer<FlockVec3Float> dataBuffer;
StructuredBuffer<FlockVec3Float> neighbours[1024];
[numthreads(16,1,1)]
void MoveFlock(uint3 id : SV_DispatchThreadID)
{
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!
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 thedataBuffer
inside of the compute shader, as long as you are only writing into a single position from each thread. \$\endgroup\$