Hi, I’m trying to use a compute shader with a “AppendStruturedBuffer” (hlsl) , but i only get a “Number overflow” when I try to get the size of my buffer.
When I get the size of my buffer, it’s a huge negative value ( - 10e9 ) .
What did I do wrong here ?
Here is my C# code with the function to get the count of the append buffer
public static Vector3[] KeyCam(Vector3 key, Vector3 cam, Vector3[] normal) {
ComputeShader shader = (ComputeShader) Resources.Load("ComputeShader/Normal");
int _kernel = shader.FindKernel("KeyCam");
ComputeBuffer inputBuffer = new ComputeBuffer(normal.Length, sizeof(float) * 3);
inputBuffer.SetData(normal);
ComputeBuffer outputBuffer = new ComputeBuffer(normal.Length, sizeof(float) * 3, ComputeBufferType.Append);
shader.SetBuffer(_kernel, "input", inputBuffer);
shader.SetBuffer(_kernel, "output", outputBuffer);
shader.SetVector("key", key);
shader.SetVector("cam", cam);
shader.Dispatch(_kernel, normal.Length/256, 1, 1);
int c = GetAppendCount(outputBuffer);
Debug.Log("Output Count : "+c + " Normal count : " + normal.Length);
//crash here : ofc it can't create a vector with negative size
Vector3[] output = new Vector3[c];
outputBuffer.GetData(output);
inputBuffer.Dispose();
outputBuffer.Dispose();
return output;
}
//https://sites.google.com/site/aliadevlog/counting-buffers-in-directcompute
private static int GetAppendCount(ComputeBuffer appendBuffer) {
ComputeBuffer countBuffer = new ComputeBuffer(1, sizeof(int), ComputeBufferType.IndirectArguments);
ComputeBuffer.CopyCount(appendBuffer, countBuffer, 0);
Debug.Log("Copy buffer : " + countBuffer.count);
int[] counter = new int[1] { 0 };
countBuffer.GetData(counter);
countBuffer.Dispose();
return counter[0];
}
}
And here my simple ComputeShader
StructuredBuffer<float3> input;
float3 key;
float3 cam;
AppendStructuredBuffer<float3> output;
[numthreads(256,1,1)]
void KeyCam(uint3 id : SV_DispatchThreadID) {
if (dot(input[id.x], cam) >= 0.0)
if (dot(input[id.x], key) <= 0.0)
output.Append(input[id.x]);
}
Thanks