初始化

This commit is contained in:
xhm
2023-11-21 23:05:03 +08:00
commit 2455630dad
2252 changed files with 466529 additions and 0 deletions

View File

@@ -0,0 +1,106 @@
using CPF.Mac.Foundation;
using CPF.Mac.ObjCRuntime;
using System;
using System.Runtime.InteropServices;
namespace CPF.Mac.OpenGL
{
public class CGLContext : INativeObject, IDisposable
{
internal IntPtr handle;
public IntPtr Handle => handle;
public static CGLContext CurrentContext
{
get
{
IntPtr value = CGLGetCurrentContext();
if (value != IntPtr.Zero)
{
return new CGLContext(value);
}
return null;
}
set
{
if (CGLSetCurrentContext(value.Handle) != 0)
{
throw new Exception("Error setting the Current Context");
}
}
}
public CGLContext(IntPtr handle)
{
if (handle == IntPtr.Zero)
{
throw new Exception("Invalid parameters to context creation");
}
CGLRetainContext(handle);
this.handle = handle;
}
internal CGLContext()
{
}
[Preserve(Conditional = true)]
internal CGLContext(IntPtr handle, bool owns)
{
if (!owns)
{
CGLRetainContext(handle);
}
this.handle = handle;
}
~CGLContext()
{
Dispose(disposing: false);
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
[DllImport("/System/Library/Frameworks/OpenGL.framework/OpenGL")]
private static extern void CGLRetainContext(IntPtr handle);
[DllImport("/System/Library/Frameworks/OpenGL.framework/OpenGL")]
private static extern void CGLReleaseContext(IntPtr handle);
protected virtual void Dispose(bool disposing)
{
if (handle != IntPtr.Zero)
{
CGLReleaseContext(handle);
handle = IntPtr.Zero;
}
}
[DllImport("/System/Library/Frameworks/OpenGL.framework/OpenGL")]
private static extern CGLErrorCode CGLLockContext(IntPtr ctx);
public CGLErrorCode Lock()
{
return CGLLockContext(handle);
}
[DllImport("/System/Library/Frameworks/OpenGL.framework/OpenGL")]
private static extern CGLErrorCode CGLUnlockContext(IntPtr ctx);
public CGLErrorCode Unlock()
{
return CGLUnlockContext(handle);
}
[DllImport("/System/Library/Frameworks/OpenGL.framework/OpenGL")]
private static extern CGLErrorCode CGLSetCurrentContext(IntPtr ctx);
[DllImport("/System/Library/Frameworks/OpenGL.framework/OpenGL")]
private static extern IntPtr CGLGetCurrentContext();
}
}

View File

@@ -0,0 +1,25 @@
namespace CPF.Mac.OpenGL
{
public enum CGLErrorCode
{
NoError = 0,
BadAttribute = 10000,
BadProperty = 10001,
BadPixelFormat = 10002,
BadRendererInfo = 10003,
BadContext = 10004,
BadDrawable = 10005,
BadDisplay = 10006,
BadState = 10007,
BadValue = 10008,
BadMatch = 10009,
BadEnumeration = 10010,
BadOffScreen = 10011,
BadFullScreen = 10012,
BadWindow = 10013,
BadAddress = 10014,
BadCodeModule = 10015,
BadAlloc = 10016,
BadConnection = 10017
}
}

View File

@@ -0,0 +1,163 @@
using CPF.Mac.Foundation;
using CPF.Mac.ObjCRuntime;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace CPF.Mac.OpenGL
{
public class CGLPixelFormat : INativeObject, IDisposable
{
internal IntPtr handle;
private static int ignored;
public IntPtr Handle => handle;
public CGLPixelFormat(IntPtr handle)
{
if (handle == IntPtr.Zero)
{
throw new Exception("Invalid parameters to context creation");
}
CGLRetainPixelFormat(handle);
this.handle = handle;
}
internal CGLPixelFormat()
{
}
[Preserve(Conditional = true)]
internal CGLPixelFormat(IntPtr handle, bool owns)
{
if (!owns)
{
CGLRetainPixelFormat(handle);
}
this.handle = handle;
}
~CGLPixelFormat()
{
Dispose(disposing: false);
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
[DllImport("/System/Library/Frameworks/OpenGL.framework/OpenGL")]
private static extern void CGLRetainPixelFormat(IntPtr handle);
[DllImport("/System/Library/Frameworks/OpenGL.framework/OpenGL")]
private static extern void CGLReleasePixelFormat(IntPtr handle);
protected virtual void Dispose(bool disposing)
{
if (handle != IntPtr.Zero)
{
CGLReleasePixelFormat(handle);
handle = IntPtr.Zero;
}
}
[DllImport("/System/Library/Frameworks/OpenGL.framework/OpenGL")]
private static extern CGLErrorCode CGLChoosePixelFormat(CGLPixelFormatAttribute[] attributes, IntPtr pix, IntPtr npix);
public CGLPixelFormat(CGLPixelFormatAttribute[] attributes, out int npix)
{
if (attributes == null)
{
throw new ArgumentNullException("attributes");
}
IntPtr intPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
IntPtr intPtr2 = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
CGLPixelFormatAttribute[] array = new CGLPixelFormatAttribute[attributes.Length + 1];
Array.Copy(attributes, array, attributes.Length);
CGLErrorCode cGLErrorCode = CGLChoosePixelFormat(array, intPtr, intPtr2);
if (cGLErrorCode != 0)
{
Marshal.FreeHGlobal(intPtr);
Marshal.FreeHGlobal(intPtr2);
throw new Exception("CGLChoosePixelFormat returned: " + cGLErrorCode);
}
handle = Marshal.ReadIntPtr(intPtr);
npix = Marshal.ReadInt32(intPtr2);
Marshal.FreeHGlobal(intPtr);
Marshal.FreeHGlobal(intPtr2);
}
public CGLPixelFormat(params object[] attributes)
: this(ConvertToAttributes(attributes), out ignored)
{
}
public CGLPixelFormat(out int npix, params object[] attributes)
: this(ConvertToAttributes(attributes), out npix)
{
}
private static CGLPixelFormatAttribute[] ConvertToAttributes(object[] args)
{
List<CGLPixelFormatAttribute> list = new List<CGLPixelFormatAttribute>();
for (int i = 0; i < args.Length; i++)
{
CGLPixelFormatAttribute cGLPixelFormatAttribute = (CGLPixelFormatAttribute)args[i];
switch (cGLPixelFormatAttribute)
{
case CGLPixelFormatAttribute.AllRenderers:
case CGLPixelFormatAttribute.DoubleBuffer:
case CGLPixelFormatAttribute.Stereo:
case CGLPixelFormatAttribute.MinimumPolicy:
case CGLPixelFormatAttribute.MaximumPolicy:
case CGLPixelFormatAttribute.OffScreen:
case CGLPixelFormatAttribute.FullScreen:
case CGLPixelFormatAttribute.AuxDepthStencil:
case CGLPixelFormatAttribute.ColorFloat:
case CGLPixelFormatAttribute.Multisample:
case CGLPixelFormatAttribute.Supersample:
case CGLPixelFormatAttribute.SampleAlpha:
case CGLPixelFormatAttribute.SingleRenderer:
case CGLPixelFormatAttribute.NoRecovery:
case CGLPixelFormatAttribute.Accelerated:
case CGLPixelFormatAttribute.ClosestPolicy:
case CGLPixelFormatAttribute.Robust:
case CGLPixelFormatAttribute.BackingStore:
case CGLPixelFormatAttribute.MPSafe:
case CGLPixelFormatAttribute.Window:
case CGLPixelFormatAttribute.MultiScreen:
case CGLPixelFormatAttribute.Compliant:
case CGLPixelFormatAttribute.PixelBuffer:
case CGLPixelFormatAttribute.RemotePixelBuffer:
case CGLPixelFormatAttribute.AllowOfflineRenderers:
case CGLPixelFormatAttribute.AcceleratedCompute:
list.Add(cGLPixelFormatAttribute);
break;
case CGLPixelFormatAttribute.AuxBuffers:
case CGLPixelFormatAttribute.ColorSize:
case CGLPixelFormatAttribute.AlphaSize:
case CGLPixelFormatAttribute.DepthSize:
case CGLPixelFormatAttribute.StencilSize:
case CGLPixelFormatAttribute.AccumSize:
case CGLPixelFormatAttribute.SampleBuffers:
case CGLPixelFormatAttribute.Samples:
case CGLPixelFormatAttribute.RendererID:
case CGLPixelFormatAttribute.ScreenMask:
case CGLPixelFormatAttribute.VirtualScreenCount:
list.Add(cGLPixelFormatAttribute);
i++;
if (i >= args.Length)
{
throw new ArgumentException("Attribute " + cGLPixelFormatAttribute + " needs a value");
}
list.Add((CGLPixelFormatAttribute)args[i]);
break;
}
}
return list.ToArray();
}
}
}

View File

@@ -0,0 +1,43 @@
namespace CPF.Mac.OpenGL
{
public enum CGLPixelFormatAttribute
{
AllRenderers = 1,
DoubleBuffer = 5,
Stereo = 6,
AuxBuffers = 7,
ColorSize = 8,
AlphaSize = 11,
DepthSize = 12,
StencilSize = 13,
AccumSize = 14,
MinimumPolicy = 51,
MaximumPolicy = 52,
OffScreen = 53,
FullScreen = 54,
SampleBuffers = 55,
Samples = 56,
AuxDepthStencil = 57,
ColorFloat = 58,
Multisample = 59,
Supersample = 60,
SampleAlpha = 61,
RendererID = 70,
SingleRenderer = 71,
NoRecovery = 72,
Accelerated = 73,
ClosestPolicy = 74,
Robust = 75,
BackingStore = 76,
MPSafe = 78,
Window = 80,
MultiScreen = 81,
Compliant = 83,
ScreenMask = 84,
PixelBuffer = 90,
RemotePixelBuffer = 91,
AllowOfflineRenderers = 96,
AcceleratedCompute = 97,
VirtualScreenCount = 0x80
}
}