Hello,
I would like to use my GPU to perform some tasks instead of the CPU.
Indeed, my program needs to perform A LOT of small operations, and it is rapidly saturated and slows down a lot....
So I though about giving all this little operations to the GPU, because it's very efficient at this kind of tasks...
And so, I tried to use a compute shader to do so, but after a lot of tries, I'm totaly stuck with an error that I don't know how to resolve...
First, here's my ComputeShader :
#pragma kernel Calculate
float value;
float power;
float coef;
RWStructuredBuffer Result;
[numthreads(1, 1, 1)]
void Calculate(uint id : SV_DispatchThreadID)
{
float temp = coef * (pow(abs(value), power));
if (power % 2 != 0 && value < 0)
{
temp = -temp;
}
Result[id] = temp;
}
And here's the part of my script that is supposed to call the compute shader :
computeShader.SetFloat("value", v);
computeShader.SetFloat("power", float.Parse(powers[i][o]));
computeShader.SetFloat("coef", coefes[i][o]);
buffers[i][o] = new ComputeBuffer(1, sizeof(float));
computeShader.SetBuffer(indexOfKernel, "Result", buffers[i][o]);
computeShader.Dispatch(indexOfKernel, 1, 1, 1);
____________________________________
int c1 = coefes.Count - 1;
while (c1 >= 0)
{
int c2 = coefes[c1].Length - 1;
while (c2 >= 0)
{
if (buffers[c1][c2] != null && structu[c1][c2] != null)
{
float[] data = new float[1] { 0f };
buffers[c1][c2].GetData(data,0,0,1);
buffers[c1][c2].Release();
float c = data[0];
value += c;
}
c2--;
}
c1--;
}
So basically, I have a two dimensional array with values in it.
For each value, I perform an operation, and at the end, I add all the results together.
So for each value, I call the compute shader with a specific buffer (that's why i have a two dimensional array of buffers), and at the end, for each buffer, I recover the result (with GetData) and add it to a variable ('value').
Everything seems fine to me.. But I'm a total beginner with Compute Shaders....
I got this error in the console :
> ArgumentNullException: Argument cannot> be null. Parameter name: _unity_self> UnityEngine.ComputeBuffer.GetData> (System.Array data, Int32> managedBufferStartIndex, Int32> computeBufferStartIndex, Int32 count)> (at> C:/buildslave/unity/build/Runtime/Export/ComputeShader.bindings.cs:243)
What should I do to make it work ?
↧