Computeshader array output

Hi Everyone,

is it possible to output for example 8 vertices in output buffer using array in C#, but not using unsafe or fixed struct?

for example, look at this code:

struct outData
{
    public float sdf;
    float v0, v1, v2, v3, v4, v5, v6;
    float v7, v8, v9, v10, v11, v12, v13;
    float v14, v15, v16, v17, v18, v19, v20;
    float v21, v22, v23; };

this is not nice code, is it only solution?

Thanks, bye

Since no one answered this one:
You can only call GetData on blittable data (see Blittable and Non-Blittable Types - .NET Framework | Microsoft Learn)

Now your array is obviously blittable. If you had non-fixed array in it though, it would not be blittable any more. But an array itself is! So this actually works:

float[] data = new float[23 + 1]; // +1 so that we can store sdf in data[23]

ComputeBuffer buffer = new ComputeBuffer(data.Length, sizeof(data[0]));
buffer.SetData(data);

shader.SetBuffer(kernel, "dataBuffer", buffer);
shader.Dispatch(kernel, data.Length, 1,1);
buffer.GetData(data);
float sdf = data[23];
float[] v = data;

// And the shader:
RWStructuredBuffer<float> dataBuffer;

[numthreads(1,1,1)]
void CSMain(uint3 id : SV_DispatchThreadID)
{
    dataBuffer[id.x] = id.x;
}

I hope this answers your question.

Apart from that I would highly recommend you getting used to unsafe code. It will make your live (and the one of your CPU) so much easier.