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)
This commit is contained in:
CasualPokePlayer 2024-05-20 21:52:55 -07:00
parent ef05b6ec2f
commit 2dd9284b76
2 changed files with 17 additions and 10 deletions

View File

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

View File

@ -14,10 +14,10 @@ namespace BizHawk.Bizware.Graphics
/// </summary>
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<IDXGIFactory1>();
// 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<IDXGIFactory2>();
#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<IDXGIDevice1>();
dxgiDevice.MaximumFrameLatency = 1;
using var adapter = dxgiDevice.GetAdapter();
Factory1 = adapter.GetParent<IDXGIFactory1>();
// 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<IDXGIFactory2>();
var bd = default(BlendDescription);
bd.AlphaToCoverageEnable = false;
bd.IndependentBlendEnable = false;