From b21f308c77c164a3138fa17ee84f85b1f7cf3203 Mon Sep 17 00:00:00 2001 From: Varon Date: Thu, 12 Nov 2020 17:15:32 +0200 Subject: [PATCH] Upgrade to OpenTK 4.2.0 bindings for Wgl --- GLWpfControl.sln | 6 ++ src/Example/Example.csproj | 6 +- src/GLWpfControl/GLWpfControl.cs | 4 ++ src/GLWpfControl/GLWpfControl.csproj | 2 +- src/GLWpfControl/GLWpfControlRendererDX.cs | 19 +++--- src/GLWpfControl/WglBindings.cs | 72 ---------------------- 6 files changed, 26 insertions(+), 83 deletions(-) delete mode 100644 src/GLWpfControl/WglBindings.cs diff --git a/GLWpfControl.sln b/GLWpfControl.sln index 6ba6939..0478b52 100644 --- a/GLWpfControl.sln +++ b/GLWpfControl.sln @@ -18,6 +18,8 @@ EndProjectSection EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GLWpfControl", "src\GLWpfControl\GLWpfControl.csproj", "{E201C66F-F247-4E23-AB4D-0EB50005091D}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example", "src\Example\Example.csproj", "{7E12D07B-0DD6-4288-A4F8-92EB75848ECF}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -28,5 +30,9 @@ Global {E201C66F-F247-4E23-AB4D-0EB50005091D}.Debug|Any CPU.Build.0 = Debug|Any CPU {E201C66F-F247-4E23-AB4D-0EB50005091D}.Release|Any CPU.ActiveCfg = Release|Any CPU {E201C66F-F247-4E23-AB4D-0EB50005091D}.Release|Any CPU.Build.0 = Release|Any CPU + {7E12D07B-0DD6-4288-A4F8-92EB75848ECF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7E12D07B-0DD6-4288-A4F8-92EB75848ECF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7E12D07B-0DD6-4288-A4F8-92EB75848ECF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7E12D07B-0DD6-4288-A4F8-92EB75848ECF}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/src/Example/Example.csproj b/src/Example/Example.csproj index 828dd90..f1d5410 100644 --- a/src/Example/Example.csproj +++ b/src/Example/Example.csproj @@ -7,6 +7,10 @@ - + + + + + diff --git a/src/GLWpfControl/GLWpfControl.cs b/src/GLWpfControl/GLWpfControl.cs index a4ca716..84885c6 100644 --- a/src/GLWpfControl/GLWpfControl.cs +++ b/src/GLWpfControl/GLWpfControl.cs @@ -4,8 +4,10 @@ using System.Threading; using System.Windows; using System.Windows.Interop; using System.Windows.Media; +using OpenTK.Graphics.Wgl; using OpenTK.Windowing.Common; using OpenTK.Windowing.Desktop; +using OpenTK.Windowing.GraphicsLibraryFramework; using Window = System.Windows.Window; namespace OpenTK.Wpf @@ -135,6 +137,8 @@ namespace OpenTK.Wpf nws.APIVersion = new Version(3,2); nws.Profile = ContextProfile.Compatability; _glfwWindow = new NativeWindow(nws) {IsVisible = false}; + var provider = new GLFWBindingsContext(); + Wgl.LoadBindings(provider); // retrieve window handle/info var window = Window.GetWindow(this); diff --git a/src/GLWpfControl/GLWpfControl.csproj b/src/GLWpfControl/GLWpfControl.csproj index 75aed02..9d78d0f 100644 --- a/src/GLWpfControl/GLWpfControl.csproj +++ b/src/GLWpfControl/GLWpfControl.csproj @@ -6,7 +6,7 @@ - + diff --git a/src/GLWpfControl/GLWpfControlRendererDX.cs b/src/GLWpfControl/GLWpfControlRendererDX.cs index 65f3930..c8fd6e1 100644 --- a/src/GLWpfControl/GLWpfControlRendererDX.cs +++ b/src/GLWpfControl/GLWpfControlRendererDX.cs @@ -1,8 +1,9 @@ using System; using System.Windows; using System.Windows.Interop; -using GLWpfControl; using OpenTK.Graphics.OpenGL; +using OpenTK.Graphics.Wgl; +using OpenTK.Platform.Windows; using SharpDX.Direct3D9; namespace OpenTK.Wpf { @@ -20,7 +21,7 @@ namespace OpenTK.Wpf { private readonly Surface _dxSurface; private readonly D3DImage _image; - private readonly WGLInterop _wglInterop; + // private readonly WGLInterop _wglInterop; private readonly bool _hasSyncFenceAvailable; private IntPtr _syncFence; @@ -28,7 +29,7 @@ namespace OpenTK.Wpf { public int FrameBuffer => _glFrameBuffer; public GLWpfControlRendererDx(int width, int height, D3DImage imageControl, bool hasSyncFenceAvailable) { - _wglInterop = new WGLInterop(); + // _wglInterop = new WGLInterop(); _hasSyncFenceAvailable = hasSyncFenceAvailable; @@ -69,15 +70,15 @@ namespace OpenTK.Wpf { _glFrameBuffer = GL.GenFramebuffer(); _glSharedTexture = GL.GenTexture(); - _glHandle = _wglInterop.WglDXOpenDeviceNV(_dxDevice.NativePointer); - _wglInterop.WglDXSetResourceShareHandleNV(_dxSurface.NativePointer, _dxSharedhandle); + _glHandle = Wgl.DXOpenDeviceNV(_dxDevice.NativePointer); + Wgl.DXSetResourceShareHandleNV(_dxSurface.NativePointer, _dxSharedhandle); - var genHandle = _wglInterop.WglDXRegisterObjectNV( + var genHandle = Wgl.DXRegisterObjectNV( _glHandle, _dxSurface.NativePointer, (uint)_glSharedTexture, (uint)TextureTarget.Texture2D, - WGLInterop.WGL_ACCESS_READ_WRITE_NV); + WGL_NV_DX_interop.AccessReadWrite); _glDxInteropSharedHandles = new[] { genHandle }; GL.BindFramebuffer(FramebufferTarget.Framebuffer, _glFrameBuffer); @@ -124,7 +125,7 @@ namespace OpenTK.Wpf { // } // GL.Flush(); - _wglInterop.WglDXUnlockObjectsNV(_glHandle, 1, _glDxInteropSharedHandles); + Wgl.DXUnlockObjectsNV(_glHandle, 1, _glDxInteropSharedHandles); _image.SetBackBuffer(D3DResourceType.IDirect3DSurface9, _dxSurface.NativePointer); _image.AddDirtyRect(new Int32Rect(0, 0, _image.PixelWidth, _image.PixelHeight)); _image.Unlock(); @@ -133,7 +134,7 @@ namespace OpenTK.Wpf { public void PreRender() { _image.Lock(); - _wglInterop.WglDXLockObjectsNV(_glHandle, 1, _glDxInteropSharedHandles); + Wgl.DXLockObjectsNV(_glHandle, 1, _glDxInteropSharedHandles); GL.BindFramebuffer(FramebufferTarget.Framebuffer, _glFrameBuffer); GL.Viewport(0, 0, _image.PixelWidth, _image.PixelHeight); } diff --git a/src/GLWpfControl/WglBindings.cs b/src/GLWpfControl/WglBindings.cs deleted file mode 100644 index 820cf64..0000000 --- a/src/GLWpfControl/WglBindings.cs +++ /dev/null @@ -1,72 +0,0 @@ -using System; -using System.Runtime.InteropServices; - -namespace GLWpfControl -{ - internal sealed class WGLInterop - { - [DllImport("OPENGL32.dll", EntryPoint = "wglGetProcAddress", ExactSpelling = true, SetLastError = true)] - internal extern static IntPtr GetProcAddress(string lpszProc); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - internal delegate bool wglDXSetResourceShareHandleNV(IntPtr dxObject, IntPtr shareHandle); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - internal delegate IntPtr wglDXOpenDeviceNV(IntPtr dxDevice); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - internal delegate bool wglDXCloseDeviceNV(IntPtr hDevice); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - internal delegate IntPtr wglDXRegisterObjectNV(IntPtr hDevice, IntPtr dxObject, uint name, uint typeEnum, uint accessEnum); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - internal delegate bool wglDXUnregisterObjectNV(IntPtr hDevice, IntPtr hObject); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - internal delegate bool wglDXObjectAccessNV(IntPtr hObject, uint accessEnum); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - internal delegate bool wglDXLockObjectsNV(IntPtr hDevice, int count, IntPtr[] hObjectsPtr); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - internal delegate bool wglDXUnlockObjectsNV(IntPtr hDevice, int count, IntPtr[] hObjectsPtr); - - internal const uint WGL_ACCESS_READ_ONLY_NV = 0x0000; - internal const uint WGL_ACCESS_READ_WRITE_NV = 0x0001; - internal const uint WGL_ACCESS_WRITE_DISCARD_NV = 0x0002; - - internal wglDXCloseDeviceNV WglDXCloseDeviceNV; - internal wglDXLockObjectsNV WglDXLockObjectsNV; - internal wglDXObjectAccessNV WglDXObjectAccessNV; - internal wglDXOpenDeviceNV WglDXOpenDeviceNV; - internal wglDXRegisterObjectNV WglDXRegisterObjectNV; - internal wglDXSetResourceShareHandleNV WglDXSetResourceShareHandleNV; - internal wglDXUnlockObjectsNV WglDXUnlockObjectsNV; - internal wglDXUnregisterObjectNV WglDXUnregisterObjectNV; - - internal WGLInterop() - { - WglDXCloseDeviceNV = Assign(); - WglDXLockObjectsNV = Assign(); - WglDXObjectAccessNV = Assign(); - WglDXOpenDeviceNV = Assign(); - WglDXRegisterObjectNV = Assign(); - WglDXSetResourceShareHandleNV = Assign(); - WglDXUnlockObjectsNV = Assign(); - WglDXUnregisterObjectNV = Assign(); - } - - private bool IsValid(IntPtr address) - { - // See https://www.opengl.org/wiki/Load_OpenGL_Functions - long a = address.ToInt64(); - bool is_valid = (a < -1) || (a > 3); - return is_valid; - } - - private T Assign() - { - var name = typeof(T).Name; - var address = GetProcAddress(name); - - if(address != IntPtr.Zero && IsValid(address)) - return Marshal.GetDelegateForFunctionPointer(address); - - throw new NotSupportedException(); - } - } -}