Add NativeArray<>.AsMemory

This commit is contained in:
hadashiA 2026-07-30 10:16:19 +09:00
parent f0a79d1a34
commit 358e0f025d
2 changed files with 85 additions and 2 deletions

View File

@ -0,0 +1,70 @@
using System;
using System.Buffers;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
namespace UniGLTF
{
public static class NativeArrayExtensions
{
#if !UNITY_2023_2_OR_NEWER
public static unsafe Span<T> AsSpan<T>(this NativeArray<T> nativeArray) where T : unmanaged
{
return new Span<T>(nativeArray.GetUnsafePtr(), nativeArray.Length);
}
#endif
public static unsafe Memory<T> AsMemory<T>(this NativeArray<T> nativeArray) where T : unmanaged
{
return new NativeArrayMemoryManager<T>((T*)nativeArray.GetUnsafeReadOnlyPtr(), nativeArray.Length).Memory;
}
public static unsafe ReadOnlyMemory<T> AsMemory<T>(this NativeArray<T>.ReadOnly nativeArray) where T : unmanaged
{
return new NativeArrayMemoryManager<T>((T*)nativeArray.GetUnsafeReadOnlyPtr(), nativeArray.Length).Memory;
}
}
unsafe class NativeArrayMemoryManager<T> : MemoryManager<T> where T : unmanaged
{
public T* Ptr { get; set; }
public int Length { get; set; }
public NativeArrayMemoryManager(NativeArray<T> nativeArray)
: this((T*)nativeArray.GetUnsafeReadOnlyPtr(), nativeArray.Length)
{
}
public NativeArrayMemoryManager(T* ptr, int length)
{
if (length < 0) throw new ArgumentOutOfRangeException(nameof(length));
Ptr = ptr;
Length = length;
}
public override Span<T> GetSpan() => new(Ptr, Length);
/// <summary>
/// Provides access to a pointer that represents the data (note: no actual pin occurs)
/// </summary>
public override MemoryHandle Pin(int elementIndex = 0)
{
if (elementIndex < 0 || elementIndex >= Length)
{
throw new ArgumentOutOfRangeException(nameof(Length));
}
return new MemoryHandle(Ptr + elementIndex);
}
/// <summary>
/// Has no effect
/// </summary>
public override void Unpin()
{
}
protected override void Dispose(bool disposing)
{
}
}
}

View File

@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UniJSON;
using Unity.Collections;
namespace UniGLTF
{
@ -17,7 +17,7 @@ namespace UniGLTF
private static readonly Regex _removeUniqueFixResourceSuffix = new Regex($@"^(.+){UniqueFixResourceSuffix}(\d+)$");
private readonly string _path;
private readonly byte[] _binary;
private readonly ReadOnlyMemory<byte> _binary;
public GlbLowLevelParser(string path, byte[] specifiedBinary)
{
@ -25,8 +25,20 @@ namespace UniGLTF
_binary = specifiedBinary;
}
public GlbLowLevelParser(string path, NativeArray<byte> specifiedBinary)
{
_path = path;
_binary = specifiedBinary.AsMemory();
}
public GltfData Parse() => Parse(_path, _binary);
public static GltfData Parse(string path, NativeArray<byte> binary) =>
Parse(path, binary.AsMemory());
public static GltfData Parse(string path, byte[] binary) =>
Parse(path, binary.AsMemory());
public static GltfData Parse(string path, ReadOnlyMemory<byte> binary)
{
try
@ -87,6 +99,7 @@ namespace UniGLTF
{
var jsonNode = json.ParseAsJson();
var GLTF = GltfDeserializer.Deserialize(jsonNode);
if (!GLTF.asset.version.StartsWith("2"))
{
throw new UniGLTFException("unknown gltf version {0}", GLTF.asset.version);
}