From 9824936c13fe71660ac310c974b6dc6435f1c430 Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Mon, 24 Feb 2025 11:04:18 +0800 Subject: [PATCH] polishing --- Runtime/Gizmos/DebugX.base.cs | 2 + Runtime/Internal/PinnedArray.cs | 45 +++ .../PinnedArray.cs.meta} | 0 Runtime/Internal/StructList.cs | 279 +++++++++++++++ Runtime/Internal/StructList.cs.meta | 2 + Runtime/Utils/DebugX.utils.cs | 319 ------------------ 6 files changed, 328 insertions(+), 319 deletions(-) create mode 100644 Runtime/Internal/PinnedArray.cs rename Runtime/{Utils/DebugX.utils.cs.meta => Internal/PinnedArray.cs.meta} (100%) create mode 100644 Runtime/Internal/StructList.cs create mode 100644 Runtime/Internal/StructList.cs.meta delete mode 100644 Runtime/Utils/DebugX.utils.cs diff --git a/Runtime/Gizmos/DebugX.base.cs b/Runtime/Gizmos/DebugX.base.cs index 4dcb1fb..9d952c2 100644 --- a/Runtime/Gizmos/DebugX.base.cs +++ b/Runtime/Gizmos/DebugX.base.cs @@ -5,6 +5,8 @@ using Unity.Jobs; using UnityEngine; using UnityEngine.Rendering; using static DCFApixels.DebugXCore.DebugXUtility; +using DCFApixels.DebugXCore.Internal; + #if UNITY_EDITOR using UnityEditor; #endif diff --git a/Runtime/Internal/PinnedArray.cs b/Runtime/Internal/PinnedArray.cs new file mode 100644 index 0000000..411c347 --- /dev/null +++ b/Runtime/Internal/PinnedArray.cs @@ -0,0 +1,45 @@ +#undef DEBUG + +using System; +using Unity.Collections.LowLevel.Unsafe; + +namespace DCFApixels.DebugXCore.Internal +{ + internal static class DummyArray + { + private readonly static T[] _array = new T[2]; + public static T[] Get() + { + return _array; + } + } + internal unsafe readonly struct PinnedArray : IDisposable where T : unmanaged + { + public readonly T[] Array; + public readonly T* Ptr; + public readonly ulong Handle; + public PinnedArray(T[] array, T* ptr, ulong handle) + { + Array = array; + Ptr = ptr; + Handle = handle; + } + public static PinnedArray Pin(T[] array) + { + return new PinnedArray(array, (T*)UnsafeUtility.PinGCArrayAndGetDataAddress(array, out ulong handle), handle); + } + public void Dispose() + { + if (Ptr != null) + { + UnsafeUtility.ReleaseGCObject(Handle); + } + } + public PinnedArray As() where U : unmanaged + { + T[] array = Array; + U[] newArray = UnsafeUtility.As(ref array); + return new PinnedArray(newArray, (U*)Ptr, Handle); + } + } +} \ No newline at end of file diff --git a/Runtime/Utils/DebugX.utils.cs.meta b/Runtime/Internal/PinnedArray.cs.meta similarity index 100% rename from Runtime/Utils/DebugX.utils.cs.meta rename to Runtime/Internal/PinnedArray.cs.meta diff --git a/Runtime/Internal/StructList.cs b/Runtime/Internal/StructList.cs new file mode 100644 index 0000000..1ac20a5 --- /dev/null +++ b/Runtime/Internal/StructList.cs @@ -0,0 +1,279 @@ +#undef DEBUG + +#if DEBUG +#define DEV_MODE +#endif +using System; +using System.Buffers; +using System.Collections.Generic; +using System.Linq; +using Unity.Collections.LowLevel.Unsafe; +using static DCFApixels.DebugX; + +namespace DCFApixels.DebugXCore.Internal +{ + using IN = System.Runtime.CompilerServices.MethodImplAttribute; + internal interface IStructListElement + { + void OnSwap(ref T element); + } + [System.Diagnostics.DebuggerDisplay("Count: {Count}")] + internal struct StructList + { + internal Array _items; + internal int _count; + internal readonly bool _isUseArrayPool; + public int Count + { + [IN(LINE)] + get { return _count; } + } + private StructList(Array items, int count, bool isUseArrayPool) + { + _items = items; + _count = count; + _isUseArrayPool = isUseArrayPool; + } + public static StructList ConvertFrom(StructList list) + { + return new StructList(list._items, list._count, list._isUseArrayPool); + } + } + [System.Diagnostics.DebuggerDisplay("Count: {Count}")] + internal struct StructList : IDisposable + { + //private struct Dummy : IStructListElement + //{ + // public void OnSwap(ref T element) { } + //} + //private static IStructListElement _internal = default(T) as IStructListElement ?? default(Dummy); + internal T[] _items; + internal int _count; + internal readonly bool _isUseArrayPool; + private static readonly bool _isUnmanaged = UnsafeUtility.IsUnmanaged(); + + public bool IsCreated + { + [IN(LINE)] + get { return _items != null; } + } + public int Count + { + [IN(LINE)] + get { return _count; } + } + public int Capacity + { + [IN(LINE)] + get { return _items.Length; } + set + { + UpSize(value); + } + } + public T this[int index] + { + [IN(LINE)] + get + { +#if DEV_MODE + if (index < 0 || index >= _count) { new ArgumentOutOfRangeException(); } +#endif + return _items[index]; + } + [IN(LINE)] + set + { +#if DEV_MODE + if (index < 0 || index >= _count) { new ArgumentOutOfRangeException(); } +#endif + _items[index] = value; + } + } + + [IN(LINE)] + public StructList(int minCapacity, bool useArrayPool = false) + { + minCapacity = NextPow2(minCapacity); + if (useArrayPool) + { + _items = ArrayPool.Shared.Rent(minCapacity); + } + else + { + _items = new T[minCapacity]; + } + _count = 0; + _isUseArrayPool = useArrayPool; + } + private StructList(StructList list) + { + _items = (T[])list._items; + _count = list._count; + _isUseArrayPool = list._isUseArrayPool; + } + + [IN(LINE)] + public void Add(T item) + { + UpSize(_count + 1); + //_internal.OnSwap(ref item); + _items[_count++] = item; + } + [IN(LINE)] + public void AddRange(ReadOnlySpan items) + { + UpSize(_count + items.Length); + for (int i = 0; i < items.Length; i++) + { + //_internal.OnSwap(ref item); + _items[_count++] = items[i]; + } + } + public void UpSize(int newMinSize) + { + if (newMinSize <= _items.Length) { return; } + newMinSize = NextPow2(newMinSize); + if (newMinSize <= _items.Length) { return; } + if (_isUseArrayPool) + { + var newItems = ArrayPool.Shared.Rent(newMinSize); + for (int i = 0, iMax = _count; i < iMax; i++) + { + newItems[i] = _items[i]; + } + ArrayPool.Shared.Return(_items, !_isUnmanaged); + _items = newItems; + } + else + { + Array.Resize(ref _items, newMinSize); + } + } + [IN(LINE)] + public int IndexOf(T item) + { + return Array.IndexOf(_items, item, 0, _count); + } + [IN(LINE)] + public void SwapAt(int idnex1, int idnex2) + { + T tmp = _items[idnex1]; + _items[idnex1] = _items[idnex2]; + _items[idnex2] = tmp; + //_internal.OnSwap(ref _items[idnex1]); + //_internal.OnSwap(ref _items[idnex2]); + } + [IN(LINE)] + public void FastRemoveAt(int index) + { +#if DEV_MODE + if (index < 0 || index >= _count) { new ArgumentOutOfRangeException(); } +#endif + _items[index] = _items[--_count]; + //_internal.OnSwap(ref _items[index]); + } + [IN(LINE)] + public void RemoveAt(int index) + { +#if DEV_MODE + if (index < 0 || index >= _count) { new ArgumentOutOfRangeException(); } +#endif + _items[index] = _items[--_count]; + //_internal.OnSwap(ref _items[index]); + _items[_count] = default; + } + [IN(LINE)] + public void RemoveAtWithOrder(int index) + { +#if DEV_MODE + if (index < 0 || index >= _count) { new ArgumentOutOfRangeException(); } +#endif + for (int i = index; i < _count; i++) + { + _items[i] = _items[i + 1]; + //_internal.OnSwap(ref _items[i]); + } + } + [IN(LINE)] + public bool Remove(T item) + { + int index = IndexOf(item); + if (index >= 0) + { + RemoveAt(index); + return true; + } + return false; + } + [IN(LINE)] + public bool RemoveWithOrder(T item) + { + int index = IndexOf(item); + if (index >= 0) + { + RemoveAtWithOrder(index); + return true; + } + return false; + } + [IN(LINE)] + public void FastClear() + { + _count = 0; + } + [IN(LINE)] + public void Clear() + { + for (int i = 0; i < _count; i++) + { + _items[i] = default; + } + _count = 0; + } + [IN(LINE)] + public ReadOnlySpan.Enumerator GetEnumerator() + { + return new ReadOnlySpan(_items, 0, _count).GetEnumerator(); + } + [IN(LINE)] + public Span AsSpan() + { + return new Span(_items, 0, _count); + } + [IN(LINE)] + public ReadOnlySpan AsReadOnlySpan() + { + return new ReadOnlySpan(_items, 0, _count); + } + [IN(LINE)] + public IEnumerable ToEnumerable() + { + return _items.Take(_count); + } + [IN(LINE)] + private static int NextPow2(int v) + { + v--; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + return ++v; + } + + public void Dispose() + { + if (_isUseArrayPool) + { + ArrayPool.Shared.Return(_items); + } + _items = null; + _count = 0; + } + + public static implicit operator StructList(StructList a) => StructList.ConvertFrom(a); + public static explicit operator StructList(StructList a) => new StructList(a); + } +} \ No newline at end of file diff --git a/Runtime/Internal/StructList.cs.meta b/Runtime/Internal/StructList.cs.meta new file mode 100644 index 0000000..5901899 --- /dev/null +++ b/Runtime/Internal/StructList.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 437f2eea401828744a893f9cfb84ebc1 \ No newline at end of file diff --git a/Runtime/Utils/DebugX.utils.cs b/Runtime/Utils/DebugX.utils.cs deleted file mode 100644 index 03e9ca0..0000000 --- a/Runtime/Utils/DebugX.utils.cs +++ /dev/null @@ -1,319 +0,0 @@ -#undef DEBUG - -#if DEBUG -#define DEV_MODE -#endif -using System; -using System.Buffers; -using System.Collections.Generic; -using System.Linq; -using Unity.Collections.LowLevel.Unsafe; - - -namespace DCFApixels -{ - using IN = System.Runtime.CompilerServices.MethodImplAttribute; - - public static unsafe partial class DebugX - { - #region private StructList - private interface IStructListElement - { - void OnSwap(ref T element); - } - private struct StructList - { - internal Array _items; - internal int _count; - internal readonly bool _isUseArrayPool; - private StructList(Array items, int count, bool isUseArrayPool) - { - _items = items; - _count = count; - _isUseArrayPool = isUseArrayPool; - } - public static StructList ConvertFrom(StructList list) - { - return new StructList(list._items, list._count, list._isUseArrayPool); - } - } - [System.Diagnostics.DebuggerDisplay("Count: {Count}")] - private struct StructList : IDisposable - { - //private struct Dummy : IStructListElement - //{ - // public void OnSwap(ref T element) { } - //} - //private static IStructListElement _internal = default(T) as IStructListElement ?? default(Dummy); - internal T[] _items; - internal int _count; - internal readonly bool _isUseArrayPool; - private static readonly bool _isUnmanaged = UnsafeUtility.IsUnmanaged(); - - public bool IsCreated - { - [IN(LINE)] - get { return _items != null; } - } - public int Count - { - [IN(LINE)] - get { return _count; } - } - public int Capacity - { - [IN(LINE)] - get { return _items.Length; } - set - { - UpSize(value); - } - } - public T this[int index] - { - [IN(LINE)] - get - { -#if DEV_MODE - if (index < 0 || index >= _count) { new ArgumentOutOfRangeException(); } -#endif - return _items[index]; - } - [IN(LINE)] - set - { -#if DEV_MODE - if (index < 0 || index >= _count) { new ArgumentOutOfRangeException(); } -#endif - _items[index] = value; - } - } - - [IN(LINE)] - public StructList(int minCapacity, bool useArrayPool = false) - { - minCapacity = NextPow2(minCapacity); - if (useArrayPool) - { - _items = ArrayPool.Shared.Rent(minCapacity); - } - else - { - _items = new T[minCapacity]; - } - _count = 0; - _isUseArrayPool = useArrayPool; - } - private StructList(StructList list) - { - _items = (T[])list._items; - _count = list._count; - _isUseArrayPool = list._isUseArrayPool; - } - - [IN(LINE)] - public void Add(T item) - { - UpSize(_count + 1); - //_internal.OnSwap(ref item); - _items[_count++] = item; - } - [IN(LINE)] - public void AddRange(ReadOnlySpan items) - { - UpSize(_count + items.Length); - for (int i = 0; i < items.Length; i++) - { - //_internal.OnSwap(ref item); - _items[_count++] = items[i]; - } - } - public void UpSize(int newMinSize) - { - if (newMinSize <= _items.Length) { return; } - newMinSize = NextPow2(newMinSize); - if (newMinSize <= _items.Length) { return; } - if (_isUseArrayPool) - { - var newItems = ArrayPool.Shared.Rent(newMinSize); - for (int i = 0, iMax = _count; i < iMax; i++) - { - newItems[i] = _items[i]; - } - ArrayPool.Shared.Return(_items, !_isUnmanaged); - _items = newItems; - } - else - { - Array.Resize(ref _items, newMinSize); - } - } - [IN(LINE)] - public int IndexOf(T item) - { - return Array.IndexOf(_items, item, 0, _count); - } - [IN(LINE)] - public void SwapAt(int idnex1, int idnex2) - { - T tmp = _items[idnex1]; - _items[idnex1] = _items[idnex2]; - _items[idnex2] = tmp; - //_internal.OnSwap(ref _items[idnex1]); - //_internal.OnSwap(ref _items[idnex2]); - } - [IN(LINE)] - public void FastRemoveAt(int index) - { -#if DEV_MODE - if (index < 0 || index >= _count) { new ArgumentOutOfRangeException(); } -#endif - _items[index] = _items[--_count]; - //_internal.OnSwap(ref _items[index]); - } - [IN(LINE)] - public void RemoveAt(int index) - { -#if DEV_MODE - if (index < 0 || index >= _count) { new ArgumentOutOfRangeException(); } -#endif - _items[index] = _items[--_count]; - //_internal.OnSwap(ref _items[index]); - _items[_count] = default; - } - [IN(LINE)] - public void RemoveAtWithOrder(int index) - { -#if DEV_MODE - if (index < 0 || index >= _count) { new ArgumentOutOfRangeException(); } -#endif - for (int i = index; i < _count; i++) - { - _items[i] = _items[i + 1]; - //_internal.OnSwap(ref _items[i]); - } - } - [IN(LINE)] - public bool Remove(T item) - { - int index = IndexOf(item); - if (index >= 0) - { - RemoveAt(index); - return true; - } - return false; - } - [IN(LINE)] - public bool RemoveWithOrder(T item) - { - int index = IndexOf(item); - if (index >= 0) - { - RemoveAtWithOrder(index); - return true; - } - return false; - } - [IN(LINE)] - public void FastClear() - { - _count = 0; - } - [IN(LINE)] - public void Clear() - { - for (int i = 0; i < _count; i++) - { - _items[i] = default; - } - _count = 0; - } - [IN(LINE)] - public ReadOnlySpan.Enumerator GetEnumerator() - { - return new ReadOnlySpan(_items, 0, _count).GetEnumerator(); - } - [IN(LINE)] - public Span AsSpan() - { - return new Span(_items, 0, _count); - } - [IN(LINE)] - public ReadOnlySpan AsReadOnlySpan() - { - return new ReadOnlySpan(_items, 0, _count); - } - [IN(LINE)] - public IEnumerable ToEnumerable() - { - return _items.Take(_count); - } - [IN(LINE)] - private static int NextPow2(int v) - { - v--; - v |= v >> 1; - v |= v >> 2; - v |= v >> 4; - v |= v >> 8; - v |= v >> 16; - return ++v; - } - - public void Dispose() - { - if (_isUseArrayPool) - { - ArrayPool.Shared.Return(_items); - } - _items = null; - _count = 0; - } - - public static implicit operator StructList(StructList a) => StructList.ConvertFrom(a); - public static explicit operator StructList(StructList a) => new StructList(a); - } - #endregion - - #region private PinnedArray - private static class DummyArray - { - private readonly static T[] _array = new T[2]; - public static T[] Get() - { - return _array; - } - } - private readonly struct PinnedArray : IDisposable where T : unmanaged - { - public readonly T[] Array; - public readonly T* Ptr; - public readonly ulong Handle; - public PinnedArray(T[] array, T* ptr, ulong handle) - { - Array = array; - Ptr = ptr; - Handle = handle; - } - public static PinnedArray Pin(T[] array) - { - return new PinnedArray(array, (T*)UnsafeUtility.PinGCArrayAndGetDataAddress(array, out ulong handle), handle); - } - public void Dispose() - { - if (Ptr != null) - { - UnsafeUtility.ReleaseGCObject(Handle); - } - } - public PinnedArray As() where U : unmanaged - { - T[] array = Array; - U[] newArray = UnsafeUtility.As(ref array); - return new PinnedArray(newArray, (U*)Ptr, Handle); - } - } - #endregion - } -} \ No newline at end of file