From 4435d1a5da99dddeedfcf48a687e6d4ebee7227d Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Tue, 8 Sep 2020 08:36:51 +0000 Subject: [PATCH] 1.0.3pre1 --- GLWidget/GLWidget.cs | 55 ++++++++++++++++++++++++++++-------- GLWidget/GTKBindingHelper.cs | 18 +++++++++++- GLWidget/GraphicsContext.cs | 44 +++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 13 deletions(-) diff --git a/GLWidget/GLWidget.cs b/GLWidget/GLWidget.cs index af8e0e0..511c645 100644 --- a/GLWidget/GLWidget.cs +++ b/GLWidget/GLWidget.cs @@ -174,6 +174,7 @@ namespace OpenTK private int _error; private IGraphicsContext _context; + private GLContext _gdkGlContext; public bool ForwardCompatible { get; } public DeviceContext DeviceContext { get => _deviceContext; set => _deviceContext = value; } @@ -209,10 +210,12 @@ namespace OpenTK { OnShuttingDown(); - DeviceContext.DeleteContext(_graphicsContext); + DeviceContext?.DeleteContext(_graphicsContext); - DeviceContext.Dispose(); - } + DeviceContext?.Dispose(); + + _gdkGlContext?.Dispose(); + } } #endregion @@ -271,7 +274,7 @@ namespace OpenTK public void Swapbuffers() { - DeviceContext?.SwapBuffers(); + _context?.SwapBuffers(); } public void MakeCurrent() @@ -285,9 +288,15 @@ namespace OpenTK public void ClearCurrent() { - //Gdk.GLContext.ClearCurrent(); - DeviceContext?.MakeCurrent(IntPtr.Zero); - } + if (GTKBindingHelper.CurrentPlatform == OSPlatform.Windows) + { + DeviceContext?.MakeCurrent(IntPtr.Zero); + } + else + { + Gdk.GLContext.ClearCurrent(); + } + } private void CreateContext() { @@ -404,20 +413,42 @@ namespace OpenTK #endregion } - private void Initialize() + private void CreateGdkGlContext() + { + _gdkGlContext = Window.CreateGlContext(); + + _gdkGlContext.SetRequiredVersion(GLVersionMajor, GLVersionMinor); + + _gdkGlContext.ForwardCompatible = ForwardCompatible; + + _gdkGlContext.SetUseEs(0); + + _gdkGlContext.Realize(); + + _gdkGlContext.MakeCurrent(); + } + + private void Initialize() { ClearCurrent(); Khronos.KhronosApi.LogEnabled = true; - Wgl.ErrorHandling = Wgl.ErrorHandlingMode.Normal; Window.EnsureNative(); - CreateDeviceContext(ControlPixelFormat); + if (GTKBindingHelper.CurrentPlatform == OSPlatform.Windows) + { + CreateDeviceContext(ControlPixelFormat); - CreateContext(); + CreateContext(); - DeviceContext.MakeCurrent(_graphicsContext); + DeviceContext.MakeCurrent(_graphicsContext); + } + else { + GraphicsContext.Display = Display.Handle; + + CreateGdkGlContext(); + } _context = GraphicsContext.GetCurrentContext(Window.Handle); diff --git a/GLWidget/GTKBindingHelper.cs b/GLWidget/GTKBindingHelper.cs index 2451e3f..7e16306 100644 --- a/GLWidget/GTKBindingHelper.cs +++ b/GLWidget/GTKBindingHelper.cs @@ -67,7 +67,7 @@ namespace OpenTK } else if(CurrentPlatform == OSPlatform.OSX) { - var osxAddr = !IsGlxRequired ? GetProcAddressOSX(procName) : GetProcAddressGlx(procName); + var osxAddr = GetProcAddressOSX(procName); if (osxAddr != IntPtr.Zero) { Loaded = true; @@ -242,6 +242,22 @@ namespace OpenTK [DllImport(OSXLibrary, EntryPoint = "NSAddressOfSymbol")] public static extern IntPtr NSAddressOfSymbol(IntPtr symbol); + [DllImport(OSXLibrary)] + public extern static int CGLSetCurrentContext(IntPtr ctx); + + [DllImport(OSXLibrary)] + public extern static IntPtr CGLGetCurrentContext(); + [DllImport(OSXLibrary)] + public extern static void CGLReleaseContext(IntPtr ctx); + [DllImport(OSXLibrary)] + public extern static int CGLFlushDrawable(IntPtr ctx); + + [DllImport(OSXLibrary)] + internal static extern int CGLDestroyContext(IntPtr ctx); + + [DllImport(OSXLibrary)] + internal static extern int CGLSetParameter(IntPtr ctx, int parameter, ref int value); + [DllImport(GlxLibrary, EntryPoint = "glXGetCurrentContext")] public static extern IntPtr glXGetCurrentContext(); diff --git a/GLWidget/GraphicsContext.cs b/GLWidget/GraphicsContext.cs index 191bb80..f9f9961 100644 --- a/GLWidget/GraphicsContext.cs +++ b/GLWidget/GraphicsContext.cs @@ -54,6 +54,10 @@ namespace OpenTK } return GlxGraphicsContext.GetCurrent(handle, Display); } + else if (currentPlatform == OSPlatform.OSX) + { + return CglGraphicsContext.GetCurrent(); + } return null; } @@ -157,4 +161,44 @@ namespace OpenTK UnsafeNativeMethods.glXSwapIntervalEXT(interval); } } + + public class CglGraphicsContext : GraphicsContext + { + private IntPtr _windowHandle; + + public static CglGraphicsContext GetCurrent() + { + var gc = UnsafeNativeMethods.CGLGetCurrentContext(); + + return new CglGraphicsContext(gc); + } + + public CglGraphicsContext(IntPtr graphicsContext) + { + _graphicsContext = graphicsContext; + } + + private IntPtr _graphicsContext; + private IntPtr _display; + + public override void MakeCurrent() + { + UnsafeNativeMethods.CGLSetCurrentContext(_graphicsContext); + } + + public override void SwapBuffers() + { + UnsafeNativeMethods.CGLFlushDrawable(_graphicsContext); + } + + public override void ClearCurrent() + { + UnsafeNativeMethods.CGLSetCurrentContext(IntPtr.Zero); + } + + public override void SwapInterval(int interval) + { + UnsafeNativeMethods.CGLSetParameter(_graphicsContext, 222 , ref interval); + } + } } \ No newline at end of file