Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Compute shader SetFloats Broken?

Discussion in 'Shaders' started by adehm, Jan 6, 2020.

  1. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    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.

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

    But doing it this way does.

    Code (CSharp):
    1. computeShader.SetFloats("Connection0", 0);
    2. computeShader.SetFloats("Connection1", 0);
    3. computeShader.SetFloats("Connection2", 0);
    4. computeShader.SetFloats("Connection3", 1);
    5. computeShader.SetFloats("Connection4", 0);
    6. computeShader.SetFloats("Connection5", 0);
     
  2. Fewes

    Fewes

    Joined:
    Jul 1, 2014
    Posts:
    259
    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.
     
  3. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,031
    Hi!
    Can you please report a bug?
    Thank you!
     
  4. Fewes

    Fewes

    Joined:
    Jul 1, 2014
    Posts:
    259
    Done! Case 1286053.
     
    qdeanc, adehm and aleksandrk like this.
  5. Mytino

    Mytino

    Joined:
    Jan 28, 2019
    Posts:
    16
    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: https://cmwdexint.com/2017/12/04/computeshader-setfloats/
    Link here to the int issue with a bit more of an explanation on my side: https://forum.unity.com/threads/computeshader-setints-failing-or-me-failing.669829/
     
    voxelltech likes this.