Upgrade to OpenTK 4.2.0 bindings for Wgl

This commit is contained in:
Varon 2020-11-12 17:15:32 +02:00
Родитель ce42895cc1
Коммит b21f308c77
6 изменённых файлов: 26 добавлений и 83 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -7,6 +7,10 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\src\GLWpfControl\GLWpfControl.csproj" />
<ProjectReference Include="..\GLWpfControl\GLWpfControl.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="OpenTK" Version="4.2.0" />
</ItemGroup>
</Project>

Просмотреть файл

@ -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);

Просмотреть файл

@ -6,7 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OpenTK" Version="4.1.0" />
<PackageReference Include="OpenTK" Version="4.2.0" />
<PackageReference Include="SharpDX.Direct3D9" Version="4.2.0" />
</ItemGroup>

Просмотреть файл

@ -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);
}

Просмотреть файл

@ -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<wglDXCloseDeviceNV>();
WglDXLockObjectsNV = Assign<wglDXLockObjectsNV>();
WglDXObjectAccessNV = Assign<wglDXObjectAccessNV>();
WglDXOpenDeviceNV = Assign<wglDXOpenDeviceNV>();
WglDXRegisterObjectNV = Assign<wglDXRegisterObjectNV>();
WglDXSetResourceShareHandleNV = Assign<wglDXSetResourceShareHandleNV>();
WglDXUnlockObjectsNV = Assign<wglDXUnlockObjectsNV>();
WglDXUnregisterObjectNV = Assign<wglDXUnregisterObjectNV>();
}
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<T>()
{
var name = typeof(T).Name;
var address = GetProcAddress(name);
if(address != IntPtr.Zero && IsValid(address))
return Marshal.GetDelegateForFunctionPointer<T>(address);
throw new NotSupportedException();
}
}
}