Skip to content

Commit

Permalink
Throw a PlatformNotSupportedException if WGL_NV_DX_interop is not pre…
Browse files Browse the repository at this point in the history
…sent.
  • Loading branch information
NogginBops committed Aug 8, 2024
1 parent 5649081 commit 67129a3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 31 deletions.
84 changes: 53 additions & 31 deletions src/GLWpfControl/DXGLContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,6 @@ internal sealed class DxGlContext : IDisposable

public DxGlContext(GLWpfControlSettings settings)
{
DXInterop.Direct3DCreate9Ex(DXInterop.DefaultSdkVersion, out DXInterop.IDirect3D9Ex dxContext);
DxContext = dxContext;

PresentationParameters deviceParameters = new PresentationParameters
{
Windowed = 1,
SwapEffect = SwapEffect.Discard,
DeviceWindowHandle = IntPtr.Zero,
PresentationInterval = 0,
BackBufferFormat = Format.X8R8G8B8,
BackBufferWidth = 1,
BackBufferHeight = 1,
AutoDepthStencilFormat = Format.Unknown,
BackBufferCount = 1,
EnableAutoDepthStencil = 0,
Flags = 0,
FullScreen_RefreshRateInHz = 0,
MultiSampleQuality = 0,
MultiSampleType = MultisampleType.D3DMULTISAMPLE_NONE,
};

dxContext.CreateDeviceEx(
0,
DeviceType.HAL,
IntPtr.Zero,
CreateFlags.HardwareVertexProcessing | CreateFlags.Multithreaded | CreateFlags.PureDevice | CreateFlags.FpuPreserve,
ref deviceParameters,
IntPtr.Zero,
out DXInterop.IDirect3DDevice9Ex dxDevice);
DxDevice = dxDevice;

// if the graphics context is null, we use the shared context.
if (settings.ContextToUse != null)
{
Expand All @@ -96,13 +65,66 @@ public DxGlContext(GLWpfControlSettings settings)
IBindingsContext provider = settings.BindingsContext ?? new GLFWBindingsContext();
Wgl.LoadBindings(provider);

bool hasNVDXInterop = false;
unsafe
{
IntPtr hwnd = GLFW.GetWin32Window(GlfwWindow.WindowPtr);
IntPtr hdc = DXInterop.GetDC(hwnd);
string exts = Wgl.Arb.GetExtensionsString(hdc);
DXInterop.ReleaseDC(hwnd, hdc);

foreach (string ext in exts.Split(' '))
{
if (ext == "WGL_NV_DX_interop" || ext == "NV_DX_interop")
{
hasNVDXInterop = true;
break;
}
}
}

if (hasNVDXInterop == false)
{
throw new PlatformNotSupportedException("NV_DX_interop extension is not suppored. This extensions is currently needed for GLWpfControl to work.");
}

#if DEBUG
GL.DebugMessageCallback(DebugProcCallback, IntPtr.Zero);
GL.Enable(EnableCap.DebugOutput);
GL.Enable(EnableCap.DebugOutputSynchronous);
#endif
}

DXInterop.Direct3DCreate9Ex(DXInterop.DefaultSdkVersion, out DXInterop.IDirect3D9Ex dxContext);
DxContext = dxContext;

PresentationParameters deviceParameters = new PresentationParameters
{
Windowed = 1,
SwapEffect = SwapEffect.Discard,
DeviceWindowHandle = IntPtr.Zero,
PresentationInterval = 0,
BackBufferFormat = Format.X8R8G8B8,
BackBufferWidth = 1,
BackBufferHeight = 1,
AutoDepthStencilFormat = Format.Unknown,
BackBufferCount = 1,
EnableAutoDepthStencil = 0,
Flags = 0,
FullScreen_RefreshRateInHz = 0,
MultiSampleQuality = 0,
MultiSampleType = MultisampleType.D3DMULTISAMPLE_NONE,
};

dxContext.CreateDeviceEx(
0,
DeviceType.HAL,
IntPtr.Zero,
CreateFlags.HardwareVertexProcessing | CreateFlags.Multithreaded | CreateFlags.PureDevice | CreateFlags.FpuPreserve,
ref deviceParameters,
IntPtr.Zero,
out DXInterop.IDirect3DDevice9Ex dxDevice);
DxDevice = dxDevice;

GLDeviceHandle = Wgl.DXOpenDeviceNV(dxDevice.Handle);
if (GLDeviceHandle == IntPtr.Zero)
Expand Down
6 changes: 6 additions & 0 deletions src/GLWpfControl/Interop/DXInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ internal static class DXInterop
[DllImport("Kernel32.dll")]
public static extern int GetLastError();

[DllImport("User32.dll")]
public static extern IntPtr /* HDC */ GetDC(IntPtr /* HWND */ hWnd);

[DllImport("User32.dll")]
public static extern int ReleaseDC(IntPtr /* HWND */ hWnd, IntPtr /* HDC */ hDC);

public static void Direct3DCreate9Ex(uint SdkVersion, out IDirect3D9Ex context)
{
int result = Direct3DCreate9Ex(SdkVersion, out context);
Expand Down

0 comments on commit 67129a3

Please sign in to comment.