Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using a Job to write data into ComputeBuffer

Discussion in 'Entity Component System' started by Avalix, Apr 5, 2018.

  1. Avalix

    Avalix

    Joined:
    Dec 10, 2012
    Posts:
    15
    Hey all!

    I'm messing around with getting indirect instancing working, and I want to write my instance data into some compute buffers with a job (As implied in the Nordeus talk here)



    But given that the ComputerBuffer is a reference type, how can you pass it into a job?

    My test code:
    Code (CSharp):
    1.  struct FillInstanceComputeBuffersJob : IJobParallelForBatch
    2.     {
    3.         [ReadOnly] public NativeArray<InstanceData> InstanceData;
    4.         public ComputeBuffer TargetComputeBuffer;
    5.  
    6.         public void Execute(int startIndex, int count)
    7.         {
    8.             TargetComputeBuffer.SetData(InstanceData, startIndex, startIndex, count);
    9.         }
    10.     }
     
  2. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    you can not. you need to add the data to the compute buffer as normal on the main thread.
     
  3. Avalix

    Avalix

    Joined:
    Dec 10, 2012
    Posts:
    15
    Ahh thank you for the confirmation!