daily update on 20240407 by nitload youmu
This commit is contained in:
@@ -44,58 +44,146 @@ namespace StgSharp.Graphics
|
||||
/// <summary>
|
||||
/// A handle to an OpenGL object,
|
||||
/// must be indexed from a <see cref="glHandleSet"/>.
|
||||
/// Attempt at direct creating an instance will cause <see cref="InvalidOperationException"/>
|
||||
/// Direct call will cause <see langword="InvalidOperationException"/>
|
||||
/// </summary>
|
||||
public unsafe struct glHandle
|
||||
public unsafe class glHandle
|
||||
{
|
||||
private static uint* zeroHandle = (uint*)Marshal.AllocHGlobal(Marshal.SizeOf<uint>());
|
||||
|
||||
internal uint* source;
|
||||
internal uint* valuePtr;
|
||||
|
||||
internal glHandle(int index, GCHandle arrayHandle)
|
||||
private glHandle(IntPtr handle)
|
||||
{
|
||||
valuePtr = ((uint*)GCHandle.ToIntPtr(arrayHandle)) + index;
|
||||
this.valuePtr = (uint*)handle;
|
||||
source = (uint*)0;
|
||||
}
|
||||
|
||||
private glHandle(uint value)
|
||||
{
|
||||
source = (uint*)0;
|
||||
valuePtr = (uint*)Marshal.AllocHGlobal(sizeof(uint));
|
||||
*valuePtr = value;
|
||||
}
|
||||
|
||||
internal glHandle(uint* val, glHandleSet set)
|
||||
{
|
||||
source = set!=null ? set!.handle:(uint*)0;
|
||||
valuePtr = val;
|
||||
}
|
||||
|
||||
[Obsolete("Cannot init instance of this type", true)]
|
||||
public glHandle()
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
public IntPtr Handle
|
||||
{
|
||||
get
|
||||
{
|
||||
return (IntPtr)valuePtr;
|
||||
}
|
||||
get=> (IntPtr)valuePtr;
|
||||
}
|
||||
|
||||
public bool IsIsolated
|
||||
{
|
||||
get => source == (uint*)0;
|
||||
}
|
||||
|
||||
public uint Value
|
||||
{
|
||||
get
|
||||
{
|
||||
return *valuePtr;
|
||||
}
|
||||
set
|
||||
{
|
||||
*valuePtr = value;
|
||||
}
|
||||
get => *valuePtr;
|
||||
set => *valuePtr = value;
|
||||
}
|
||||
|
||||
public static glHandle Zero => Alloc(0);
|
||||
|
||||
internal uint DirectValue => *valuePtr;
|
||||
public static glHandle Alloc(uint value)
|
||||
{
|
||||
return new glHandle(value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static glHandle FromPtr(IntPtr p)
|
||||
{
|
||||
return new glHandle(p);
|
||||
}
|
||||
|
||||
~glHandle()
|
||||
{
|
||||
if (valuePtr != zeroHandle && IsIsolated)
|
||||
{
|
||||
Marshal.FreeHGlobal((IntPtr)valuePtr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe struct glSHandle
|
||||
public unsafe class glSHandle
|
||||
{
|
||||
private static int* zeroHandle = (int*)Marshal.AllocHGlobal(Marshal.SizeOf<int>());
|
||||
|
||||
internal int* source;
|
||||
internal int* valuePtr;
|
||||
|
||||
internal glSHandle(int index, glHandleSet set)
|
||||
private glSHandle(IntPtr handle)
|
||||
{
|
||||
|
||||
this.valuePtr = (int*)handle;
|
||||
source = (int*)0;
|
||||
}
|
||||
|
||||
private glSHandle(int value)
|
||||
{
|
||||
source = (int*)0;
|
||||
valuePtr = (int*)Marshal.AllocHGlobal(sizeof(int));
|
||||
*valuePtr = value;
|
||||
}
|
||||
|
||||
internal glSHandle(int* val, glHandleSet set)
|
||||
{
|
||||
source = set != null ? (int*)set!.handle : (int*)0;
|
||||
valuePtr = val;
|
||||
}
|
||||
|
||||
|
||||
[Obsolete("Cannot init instance of this type", true)]
|
||||
public glSHandle()
|
||||
{
|
||||
}
|
||||
|
||||
public IntPtr Handle
|
||||
{
|
||||
get => (IntPtr)valuePtr;
|
||||
}
|
||||
|
||||
public bool IsIsolated
|
||||
{
|
||||
get => source == (uint*)0;
|
||||
}
|
||||
|
||||
public int Value
|
||||
{
|
||||
get => *valuePtr;
|
||||
set => *valuePtr = value;
|
||||
}
|
||||
|
||||
public static glSHandle Zero => Alloc(0);
|
||||
|
||||
public static glSHandle Alloc(int value)
|
||||
{
|
||||
return new glSHandle(value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static glSHandle FromPtr(IntPtr p)
|
||||
{
|
||||
return new glSHandle(p);
|
||||
}
|
||||
|
||||
~glSHandle()
|
||||
{
|
||||
if (valuePtr != zeroHandle && IsIsolated)
|
||||
{
|
||||
Marshal.FreeHGlobal((IntPtr)valuePtr);
|
||||
}
|
||||
}
|
||||
|
||||
internal int DirectValue => *valuePtr;
|
||||
}
|
||||
|
||||
public unsafe class glArrayHandle
|
||||
@@ -151,20 +239,20 @@ namespace StgSharp.Graphics
|
||||
public unsafe class glHandleSet
|
||||
{
|
||||
|
||||
private uint[] _value;
|
||||
internal GCHandle handle;
|
||||
private readonly uint[] _value;
|
||||
internal uint* handle;
|
||||
internal int length;
|
||||
|
||||
public glHandleSet(int count)
|
||||
{
|
||||
_value = new uint[count];
|
||||
length = count;
|
||||
handle = GCHandle.Alloc(_value);
|
||||
handle = (uint*)GCHandle.Alloc(_value, GCHandleType.Pinned).AddrOfPinnedObject();
|
||||
}
|
||||
|
||||
public IntPtr Handle
|
||||
{
|
||||
get => GCHandle.ToIntPtr(handle);
|
||||
get => (IntPtr)handle;
|
||||
}
|
||||
|
||||
public glHandle this[int index]
|
||||
@@ -175,7 +263,7 @@ namespace StgSharp.Graphics
|
||||
{
|
||||
throw new IndexOutOfRangeException();
|
||||
}
|
||||
return new glHandle(index, this.handle);
|
||||
return new glHandle(handle + index, this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,7 +275,7 @@ namespace StgSharp.Graphics
|
||||
{
|
||||
throw new IndexOutOfRangeException();
|
||||
}
|
||||
return new glSHandle((int)index, this);
|
||||
return new glSHandle(((int*)handle) + index, this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,6 +295,15 @@ namespace StgSharp.Graphics
|
||||
}
|
||||
}
|
||||
|
||||
public void Pin()
|
||||
{
|
||||
if (_value == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
handle = (uint*)GCHandle.Alloc(_value, GCHandleType.Pinned).AddrOfPinnedObject();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Release all resource in this handle.
|
||||
/// This will release all resource in this <see cref="glHandle"/>.
|
||||
@@ -214,24 +311,26 @@ namespace StgSharp.Graphics
|
||||
/// </summary>
|
||||
public void Release()
|
||||
{
|
||||
if (handle == default)
|
||||
if (_value == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
handle.Free();
|
||||
_value = null;
|
||||
GCHandle.FromIntPtr((IntPtr)handle).Free();
|
||||
handle = (uint*)IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetValue(int index, uint value)
|
||||
{
|
||||
_value[index] = value;
|
||||
}
|
||||
|
||||
~glHandleSet()
|
||||
{
|
||||
//unsafe, 不确定这个句柄组的单一索引是不是完全卸载
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user