Search code examples
c#unity-game-enginehlsl

NativeSlice as NativeArray, find NativeArray via NativeSlice


I'm running into an issue with getting ARFoundation, C#, and HLSL to talk - I'm trying to get ARFoundation's point cloud position data on the graphics card in a way that will be reliable in different frameworks. I've heard that using BeginWrite is best practice, but also that BeginWrite only works on Vulkan, which I can't use due to the conflict with ARFoundation.

The point cloud's confidence values can be read as a NativeArray, but all of the other attributes including position are in NativeSlices, which unfortunately does not seem to be compatible with SetData for a Compute Buffer.

I would rather not have to convert the NativeSlice to a C# array just to have to send that to a compute buffer, which is already a performance concern. I've read a claim that there is some way to treat a NativeSlice as a NativeArray, but I can't locate it in the docs. My specific questions are these:

  1. Is that claim true? Is there a way to treat a NativeSlice as a NativeArray without copying values over?
  2. Is there a way to find the NativeArray that a NativeSlice is part of?
  3. Is there a better way to do this data transfer? In the future I could just send the changes over, but it's still going to be a pain.

Any advice highly appreciated.


Solution

  • NativeSlice to NativeArray:

    As for whether a slice is part of an array, just do it C-like way:

    if slicePtr >= arrayPtr && slicePtr + sliceLength <= arrayPtr + arrayLength