Compute shader SetFloats Broken?

I can’t seem to get SetFloats to work. If I have 6 floats and update them one by one with SetFloat I will get my expected result but creating a float array and using SetFloats it appears that Connections[3] never gets updated to 1.

This does not work for me.

computeShader.SetFloats("Connections", new float[6] { 0, 0, 0, 1, 0, 0 });

But doing it this way does.

computeShader.SetFloats("Connection0", 0);
computeShader.SetFloats("Connection1", 0);
computeShader.SetFloats("Connection2", 0);
computeShader.SetFloats("Connection3", 1);
computeShader.SetFloats("Connection4", 0);
computeShader.SetFloats("Connection5", 0);

Seeing the same behaviour here on Unity 2020.1.5f1: SetFloats does not work when trying to use it with a float array in the compute shader. It’s easy to get around this by just using SetVectorArray and using the x component of a float4, but it feels a bit silly.

Hi!
Can you please report a bug?
Thank you!

Done! Case 1286053.

3 Likes

Tldr; I had the same issue with SetInts. It probably works as long as each of your CPU-side floats have 3 unused floats of padding following it, and it seems to be intentional to follow the HLSL constant buffer data layout rules, as the manual says:

You don’t even have to consider the padding inside the compute shader, so you can keep it nice and legible in there. See this link for more info: ComputeShader.SetFloats() – Ming Wai Chan
Link here to the int issue with a bit more of an explanation on my side: ComputeShader.SetInts failing (or me failing)?

1 Like