Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Array passed to ComputeBuffer.SetData(array) must be blittable

Discussion in 'Scripting' started by mgto, May 25, 2020.

  1. mgto

    mgto

    Joined:
    May 30, 2013
    Posts:
    22
    Not sure if this is the right board for compute shader related topics..

    My question is: can I use a class array to send data to and from a compute buffer?

    Like:

    Code (CSharp):
    1.  public class TestClass
    2. {          
    3.     public int value;
    4. }
    5.  
    6. ...
    7.  
    8. public TestClass[] m_Classes;
    9.  
    10. ...
    11.  
    12. // this throws: Array passed to ComputeBuffer.SetData(array) must be blittable. TestClass is not blittable because it is not of value type ...
    13. myComputeBuffer.SetData(m_Classes);
    14.  
    15.  
    I was hoping that using
    [StructLayout(LayoutKind.Sequential)]
    and / or something like
    [MarshalAs(UnmanagedType.LPArray, SizeConst = ...)]
    would allow me to use a class like a struct.

    But after seeing this check in
    https://github.com/Unity-Technologi...master/Runtime/Export/Unsafe/UnsafeUtility.cs
    Code (CSharp):
    1.  if (!t.IsValueType)
    2.                 return String.Format("{0} is not blittable because it is not of value type ({1})\n", name, t);
    It seems Unity just prevents non value types in general.

    Could someone provide more insight into this?
     
    DDeathlonger likes this.
  2. PixelMind

    PixelMind

    Joined:
    Aug 16, 2013
    Posts:
    101
    Old thread but it pops up in Google searches.

    The problem above is that you are trying to pass a class type (TestClass) to ComputeBuffer. It should work if you change it to struct instead of class.
    The struct also can't contain class fields or other complex types.