Last active
December 26, 2024 04:19
-
-
Save NepNet/e4e75defb3748f76f10302b585b9839b to your computer and use it in GitHub Desktop.
Bindings contexts for OpenTK 4. If you need to load the bindings for windows use the wgl one, for linux glx and if you use it in a cross platform app like a gtk one use the native bindings context, it will check the platform and use either wgl or glx depending on the platform. For Mac ¯\_(ツ)_/¯ idk, I don't have a mac to test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.ConstrainedExecution; | |
using System.Runtime.InteropServices; | |
using OpenTK; | |
public class GlxBindingsContext : IBindingsContext | |
{ | |
[DllImport("libGL", CharSet = CharSet.Ansi)] | |
private static extern IntPtr glXGetProcAddress(string procName); | |
public IntPtr GetProcAddress(string procName) | |
{ | |
return glXGetProcAddress(procName); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.ConstrainedExecution; | |
using System.Runtime.InteropServices; | |
using OpenTK; | |
public class NativeBindingsContext : IBindingsContext | |
{ | |
private static IBindingsContext _context; | |
public NativeBindingsContext() | |
{ | |
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | |
{ | |
_context = new WglBindingsContext(); | |
} | |
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | |
{ | |
_context = new GlxBindingsContext(); | |
} | |
else | |
{ | |
throw new PlatformNotSupportedException(); | |
} | |
} | |
public IntPtr GetProcAddress(string procName) | |
{ | |
return _context.GetProcAddress(procName); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
MIT License | |
Copyright (c) 2019 Eschryn <https://github.com/Eschryn> | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
using System; | |
using System.Runtime.ConstrainedExecution; | |
using System.Runtime.InteropServices; | |
using System.Security.Permissions; | |
using Microsoft.Win32.SafeHandles; | |
using OpenTK; | |
public class WglBindingsContext : IBindingsContext | |
{ | |
[DllImport("opengl32.dll", CharSet = CharSet.Ansi)] | |
private static extern IntPtr wglGetProcAddress(string procName); | |
private readonly ModuleSafeHandle _openGlHandle; | |
public WglBindingsContext() | |
{ | |
_openGlHandle = Kernel32.LoadLibrary("opengl32.dll"); | |
} | |
public IntPtr GetProcAddress(string procName) | |
{ | |
IntPtr addr = wglGetProcAddress(procName); | |
return addr != IntPtr.Zero ? addr : Kernel32.GetProcAddress(_openGlHandle, procName); | |
} | |
private static class Kernel32 | |
{ | |
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Ansi)] | |
public static extern ModuleSafeHandle LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpFileName); | |
[DllImport("kernel32", SetLastError = true)] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
public static extern bool FreeLibrary(IntPtr hModule); | |
[DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)] | |
public static extern IntPtr GetProcAddress(ModuleSafeHandle hModule, string procName); | |
} | |
[SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)] | |
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)] | |
// ReSharper disable once ClassNeverInstantiated.Local | |
private class ModuleSafeHandle : SafeHandleZeroOrMinusOneIsInvalid | |
{ | |
public ModuleSafeHandle() : base(true) | |
{ | |
} | |
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] | |
protected override bool ReleaseHandle() | |
{ | |
return Kernel32.FreeLibrary(handle); | |
} | |
} | |
} |
Has linux not more then just 1 dll?
https://github.com/opentk/opentk/blob/opentk5.0/src/OpenTK.Platform.Native/DllResolver.cs
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As a remark, the wgl bindings context require an active opengl context.
For Gtk I suggest loading the bindings inside the OnRealized event of an opengl area after making the context of it current because wgl requires an active context