From 2dd9284b76582796f81dbe9bd3ab9d268dfcdd44 Mon Sep 17 00:00:00 2001 From: CasualPokePlayer <50538166+CasualPokePlayer@users.noreply.github.com> Date: Mon, 20 May 2024 21:52:55 -0700 Subject: [PATCH] Get DXGIFactory*s from the created D3D11 device instead of creating our own Creating our own usually ends up working, but for some setups it does not (seen in Win7 VirtualBox setup) --- .../D3D11/D3D11GLInterop.cs | 11 ++++++++--- .../D3D11/D3D11Resources.cs | 16 +++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/BizHawk.Bizware.Graphics/D3D11/D3D11GLInterop.cs b/src/BizHawk.Bizware.Graphics/D3D11/D3D11GLInterop.cs index c38ca16408..3a21ada274 100644 --- a/src/BizHawk.Bizware.Graphics/D3D11/D3D11GLInterop.cs +++ b/src/BizHawk.Bizware.Graphics/D3D11/D3D11GLInterop.cs @@ -58,7 +58,7 @@ namespace BizHawk.Bizware.Graphics return; } - // note: Silk.NET's WGL.TryGetExtension function seemed to be bugged and just result in NREs... + // note: Silk.NET's WGL.IsExtensionPresent function seemed to be bugged and just result in NREs... NVDXInterop = new(new LamdaNativeContext(SDL2OpenGLContext.GetGLProcAddress)); if (NVDXInterop.CurrentVTable.Load("wglDXOpenDeviceNV") == IntPtr.Zero || NVDXInterop.CurrentVTable.Load("wglDXCloseDeviceNV") == IntPtr.Zero @@ -70,6 +70,11 @@ namespace BizHawk.Bizware.Graphics return; } + // using these NVDXInterop functions shouldn't need a context active (see above Kronos comment) + // however, some buggy drivers will end up failing if we don't have a context + // explicitly make no context active to catch these buggy drivers + SDL2OpenGLContext.MakeNoneCurrent(); + ID3D11Device device = null; var dxInteropDevice = IntPtr.Zero; try @@ -109,9 +114,9 @@ namespace BizHawk.Bizware.Graphics } } } - catch (Exception ex) + catch { - Console.WriteLine(ex); + // ignored } } } diff --git a/src/BizHawk.Bizware.Graphics/D3D11/D3D11Resources.cs b/src/BizHawk.Bizware.Graphics/D3D11/D3D11Resources.cs index 9053eeb496..16efe46909 100644 --- a/src/BizHawk.Bizware.Graphics/D3D11/D3D11Resources.cs +++ b/src/BizHawk.Bizware.Graphics/D3D11/D3D11Resources.cs @@ -14,10 +14,10 @@ namespace BizHawk.Bizware.Graphics /// internal sealed class D3D11Resources : IDisposable { - public IDXGIFactory1 Factory1; - public IDXGIFactory2 Factory2; public ID3D11Device Device; public ID3D11DeviceContext Context; + public IDXGIFactory1 Factory1; + public IDXGIFactory2 Factory2; public ID3D11BlendState BlendEnableState; public ID3D11BlendState BlendDisableState; public ID3D11SamplerState PointSamplerState; @@ -36,13 +36,9 @@ namespace BizHawk.Bizware.Graphics { try { - Factory1 = DXGI.CreateDXGIFactory1(); - // we want IDXGIFactory2 for CreateSwapChainForHwnd - // however, it's not guaranteed to be available (only available in Win8+ or Win7 with the Platform Update) - Factory2 = Factory1.QueryInterfaceOrNull(); #if false // use this to debug D3D11 calls - // note debug layer requires extra steps to use: https://learn.microsoft.com/en-us/windows/win32/direct3d11/overviews-direct3d-11-devices-layers#debug-layer + // note the debug layer requires extra steps to use: https://learn.microsoft.com/en-us/windows/win32/direct3d11/overviews-direct3d-11-devices-layers#debug-layer // also debug output will only be present with a "native debugger" attached (pure managed debugger can't see this output) var creationFlags = DeviceCreationFlags.Singlethreaded | DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug; #else @@ -67,6 +63,12 @@ namespace BizHawk.Bizware.Graphics using var dxgiDevice = Device.QueryInterface(); dxgiDevice.MaximumFrameLatency = 1; + using var adapter = dxgiDevice.GetAdapter(); + Factory1 = adapter.GetParent(); + // we want IDXGIFactory2 for CreateSwapChainForHwnd + // however, it's not guaranteed to be available (only available in Win8+ or Win7 with the Platform Update) + Factory2 = Factory1.QueryInterfaceOrNull(); + var bd = default(BlendDescription); bd.AlphaToCoverageEnable = false; bd.IndependentBlendEnable = false;