-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Unity NativeArray as vector type (#319)
* Add support for Unity NativeArray vector type. * Ensure NativeArray serialization occurs only on aligned types.
- Loading branch information
Showing
23 changed files
with
725 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,4 +26,5 @@ public enum VectorType | |
Memory, | ||
ReadOnlyMemory, | ||
IIndexedVector, | ||
UnityNativeArray | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/FlatSharp.UnityPolyfills/FlatSharp.UnityPolyfills.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Import Project="..\common.props" /> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0</TargetFrameworks> | ||
<RootNamespace>FlatSharp.UnityPolyfills</RootNamespace> | ||
<Description>FlatSharp.UnityPolyfills is a utility assembly to expose the Unity API needed for FlatSharp support of UnityEngine.Collections.NativeArray.</Description> | ||
<Nullable>annotations</Nullable> | ||
<DefineConstants>$(DefineContants);FLATSHARP_UNITY_POLYFILLS</DefineConstants> | ||
<PackageId>FlatSharp.UnityPolyfills</PackageId> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition=" '$(TargetFramework)' == 'net6.0' "> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="System.Memory" Version="4.5.5" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\FlatSharp.Runtime\FlatSharp.Runtime.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/* | ||
* Copyright 2022 Unity Technologies | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Unity.Collections | ||
{ | ||
public enum NativeArrayOptions | ||
{ | ||
UninitializedMemory = 0, | ||
ClearMemory = 1 | ||
} | ||
|
||
public enum Allocator | ||
{ | ||
Invalid = 0, | ||
None = 1, | ||
Temp = 2, | ||
TempJob = 3, | ||
Persistent = 4, | ||
} | ||
|
||
// for testing, we remove the 'where T : struct' constraint and enforce this in UnityNativeArrayVectorTypeModel | ||
public unsafe struct NativeArray<T> : IEnumerable<T>, IEquatable<NativeArray<T>> /* where T : struct */ | ||
{ | ||
internal Memory<T> m_Buffer; | ||
|
||
internal void* m_Data; | ||
internal int m_Length; | ||
|
||
public NativeArray(int length, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory) | ||
{ | ||
Allocate(length, allocator, out this); | ||
} | ||
|
||
public NativeArray(T[] array, Allocator allocator) | ||
{ | ||
Allocate(array.Length, allocator, out this); | ||
array.AsSpan().CopyTo(m_Buffer.Span); | ||
} | ||
|
||
public NativeArray(NativeArray<T> array, Allocator allocator) | ||
{ | ||
Allocate(array.Length, allocator, out this); | ||
array.AsSpan().CopyTo(this.AsSpan()); | ||
} | ||
|
||
static void Allocate(int length, Allocator allocator, out NativeArray<T> nativeArray) | ||
{ | ||
var backing = new T[length]; | ||
nativeArray = new NativeArray<T>() | ||
{ | ||
m_Buffer = new Memory<T>(backing) | ||
}; | ||
} | ||
|
||
struct Enumerator : IEnumerator<T> | ||
{ | ||
private readonly NativeArray<T> m_Array; | ||
private int m_Index; | ||
|
||
public Enumerator(NativeArray<T> nativeArray) | ||
{ | ||
m_Array = nativeArray; | ||
m_Index = -1; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
|
||
public bool MoveNext() | ||
{ | ||
m_Index++; | ||
return m_Index < m_Array.Length; | ||
} | ||
|
||
public void Reset() | ||
{ | ||
m_Index = -1; | ||
} | ||
|
||
// Let NativeArray indexer check for out of range. | ||
public T Current => m_Array[m_Index]; | ||
|
||
object IEnumerator.Current => Current; | ||
} | ||
|
||
public IEnumerator<T> GetEnumerator() | ||
{ | ||
return new Enumerator(this); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
|
||
public bool Equals(NativeArray<T> other) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public int Length => m_Data != null ? m_Length : m_Buffer.Length; | ||
|
||
public T this[int index] | ||
{ | ||
get => AsSpan()[index]; | ||
|
||
set => AsSpan()[index] = value; | ||
} | ||
|
||
public readonly Span<T> AsSpan() | ||
{ | ||
return m_Data != null ? new Span<T>(m_Data, m_Length) : m_Buffer.Span; | ||
} | ||
} | ||
} | ||
|
||
namespace Unity.Collections.LowLevel.Unsafe | ||
{ | ||
public static unsafe class NativeArrayUnsafeUtility | ||
{ | ||
public static NativeArray<T> ConvertExistingDataToNativeArray<T>(void* dataPointer, int length, Allocator allocator) where T : struct | ||
{ | ||
var newArray = new NativeArray<T> | ||
{ | ||
m_Data = dataPointer, | ||
m_Length = length, | ||
}; | ||
|
||
return newArray; | ||
} | ||
} | ||
|
||
|
||
public static unsafe class NativeArrayUnsafeUtilityEx | ||
{ | ||
public static NativeArray<T> ConvertExistingDataToNativeArray<T>(Span<T> span, Allocator allocator) where T : struct | ||
{ | ||
var newArray = new NativeArray<T> | ||
{ | ||
m_Data = System.Runtime.CompilerServices.Unsafe.AsPointer(ref span.GetPinnableReference()), | ||
m_Length = span.Length, | ||
}; | ||
|
||
return newArray; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.