From 6913a358815d6819c267a65f6202b250ca2e2f21 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 30 Jul 2021 14:09:34 +0200 Subject: [PATCH 1/2] Added IDXGIFactory4 fallback layer, enabled IDXGIFactory6 --- .../Helpers/DeviceHelper.Enumerator.cs | 20 ++- .../Helpers/DeviceHelper.IDXGIFactory6.cs | 153 ++++++++++++++++++ .../Graphics/Helpers/DeviceHelper.cs | 46 +----- 3 files changed, 168 insertions(+), 51 deletions(-) create mode 100644 src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs index 80ac5a7d..093ecadf 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs @@ -57,17 +57,17 @@ namespace ComputeSharp.Graphics.Helpers private readonly Predicate? predicate; /// - /// The instance used to enumerate devices. + /// The instance used to enumerate devices. /// - private ComPtr dxgiFactory4; + private ComPtr dxgiFactory6; /// - /// Indicates whether or not the enumerator has already been initialized and is set. + /// Indicates whether or not the enumerator has already been initialized and is set. /// private bool isInitialized; /// - /// The current adapter index to enumerate adapters from . + /// The current adapter index to enumerate adapters from . /// private uint index; @@ -103,11 +103,9 @@ namespace ComputeSharp.Graphics.Helpers { this.isInitialized = true; - fixed (IDXGIFactory4** dxgiFactory4 = this.dxgiFactory4) + fixed (IDXGIFactory6** dxgiFactory6 = this.dxgiFactory6) { - EnableDebugMode(); - - FX.CreateDXGIFactory2(IDXGIFactoryCreationFlags, FX.__uuidof(), (void**)dxgiFactory4).Assert(); + CreateDXGIFactory6(dxgiFactory6); } } @@ -117,11 +115,11 @@ namespace ComputeSharp.Graphics.Helpers { using ComPtr dxgiAdapter1 = default; - HRESULT enumAdapters1Result = this.dxgiFactory4.Get()->EnumAdapters1(this.index, dxgiAdapter1.GetAddressOf()); + HRESULT enumAdapters1Result = this.dxgiFactory6.Get()->EnumAdapters1(this.index, dxgiAdapter1.GetAddressOf()); if (enumAdapters1Result == FX.DXGI_ERROR_NOT_FOUND) { - this.dxgiFactory4.Get()->EnumWarpAdapter(FX.__uuidof(), dxgiAdapter1.GetVoidAddressOf()).Assert(); + this.dxgiFactory6.Get()->EnumWarpAdapter(FX.__uuidof(), dxgiAdapter1.GetVoidAddressOf()).Assert(); DXGI_ADAPTER_DESC1 dxgiDescription1; @@ -205,7 +203,7 @@ namespace ComputeSharp.Graphics.Helpers /// protected override void OnDispose() { - this.dxgiFactory4.Dispose(); + this.dxgiFactory6.Dispose(); } } } diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs new file mode 100644 index 00000000..1bd56c18 --- /dev/null +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs @@ -0,0 +1,153 @@ +using System; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ComputeSharp.Core.Extensions; +using TerraFX.Interop; +using FX = TerraFX.Interop.Windows; + +namespace ComputeSharp.Graphics.Helpers +{ + /// + internal static partial class DeviceHelper + { + /// + /// The creation flags for instances. + /// + private const uint IDXGIFactoryCreationFlags = +#if DEBUG + FX.DXGI_CREATE_FACTORY_DEBUG; +#else + 0; +#endif + + /// + /// Creates a new instance to be used to enumerate devices. + /// + /// The resulting instance. + private static unsafe void CreateDXGIFactory6(IDXGIFactory6** dxgiFactory6) + { + using ComPtr dxgiFactory4 = default; + + EnableDebugMode(); + + FX.CreateDXGIFactory2(IDXGIFactoryCreationFlags, FX.__uuidof(), dxgiFactory4.GetVoidAddressOf()).Assert(); + + int result = dxgiFactory4.CopyTo(dxgiFactory6); + + if (result == FX.S_OK) + { + return; + } + + if (result == FX.E_NOINTERFACE) + { + IDXGIFactory4As6Backcompat.Create(dxgiFactory4.Get(), dxgiFactory6); + + return; + } + + result.Assert(); + + return; + } + + /// + /// Enables the debug layer for DirectX APIs. + /// + [Conditional("DEBUG")] + private static unsafe void EnableDebugMode() + { + using ComPtr d3D12Debug = default; + using ComPtr d3D12Debug1 = default; + + FX.D3D12GetDebugInterface(FX.__uuidof(), d3D12Debug.GetVoidAddressOf()).Assert(); + + d3D12Debug.Get()->EnableDebugLayer(); + + if (FX.SUCCEEDED(d3D12Debug.CopyTo(d3D12Debug1.GetAddressOf()))) + { + d3D12Debug1.Get()->SetEnableGPUBasedValidation(FX.TRUE); + d3D12Debug1.Get()->SetEnableSynchronizedCommandQueueValidation(FX.TRUE); + } + } + + /// + /// A custom fallback implementation to use on systems with no support for it. + /// + private unsafe struct IDXGIFactory4As6Backcompat + { + /// + /// The shared method table pointer for all instances. + /// + private static readonly void** Vtbl = InitVtbl(); + + /// + /// Builds the custom method table pointer for . + /// + /// The method table pointer for . + private static void** InitVtbl() + { + void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(IDXGIFactory4As6Backcompat), sizeof(void*) * 30); + + new Span(lpVtbl, 30).Clear(); + + lpVtbl[2] = (delegate* unmanaged)&Release; + lpVtbl[27] = (delegate* unmanaged)&EnumWarpAdapter; + lpVtbl[29] = (delegate* unmanaged)&EnumAdapterByGpuPreference; + + return lpVtbl; + } + + /// + /// The method table pointer for the current instance. + /// + private void** lpVtbl; + + /// + /// The wrapped instance. + /// + private IDXGIFactory4* dxgiFactory4; + + /// + /// Creates and initializes a new instance. + /// + /// The instance to wrap. + /// The resulting instance. + public static void Create(IDXGIFactory4* dxgiFactory4, IDXGIFactory6** dxgiFactory6) + { + IDXGIFactory4As6Backcompat* @this = (IDXGIFactory4As6Backcompat*)Marshal.AllocHGlobal(sizeof(IDXGIFactory4As6Backcompat)); + + @this->lpVtbl = Vtbl; + @this->dxgiFactory4 = dxgiFactory4; + + *dxgiFactory6 = (IDXGIFactory6*)@this; + } + + /// + [UnmanagedCallersOnly] + public static uint Release(IDXGIFactory4As6Backcompat* @this) + { + @this->dxgiFactory4->Release(); + + Marshal.FreeHGlobal((IntPtr)@this); + + return 0; + } + + /// + [UnmanagedCallersOnly] + public static int EnumWarpAdapter(IDXGIFactory4As6Backcompat* @this, Guid* riid, void** ppvAdapter) + { + return @this->dxgiFactory4->EnumWarpAdapter(riid, ppvAdapter); + } + + /// + [UnmanagedCallersOnly] + public static int EnumAdapterByGpuPreference(IDXGIFactory4As6Backcompat* @this, uint Adapter, DXGI_GPU_PREFERENCE GpuPreference, Guid* riid, void** ppvAdapter) + { + return @this->dxgiFactory4->EnumAdapters1(Adapter, (IDXGIAdapter1**)ppvAdapter); + } + } + } +} diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs index 8837c23c..d48938e4 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs @@ -82,11 +82,9 @@ namespace ComputeSharp.Graphics.Helpers /// Whether a default device was found with the requested feature level. private static unsafe bool TryGetDefaultDevice(ID3D12Device** d3D12Device, IDXGIAdapter** dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1) { - using ComPtr dxgiFactory4 = default; + using ComPtr dxgiFactory6 = default; - EnableDebugMode(); - - FX.CreateDXGIFactory2(IDXGIFactoryCreationFlags, FX.__uuidof(), dxgiFactory4.GetVoidAddressOf()).Assert(); + CreateDXGIFactory6(dxgiFactory6.GetAddressOf()); uint i = 0; @@ -94,7 +92,7 @@ namespace ComputeSharp.Graphics.Helpers { using ComPtr dxgiAdapter1 = default; - HRESULT enumAdapters1Result = dxgiFactory4.Get()->EnumAdapters1(i++, dxgiAdapter1.GetAddressOf()); + HRESULT enumAdapters1Result = dxgiFactory6.Get()->EnumAdapters1(i++, dxgiAdapter1.GetAddressOf()); if (enumAdapters1Result == FX.DXGI_ERROR_NOT_FOUND) { @@ -172,15 +170,13 @@ namespace ComputeSharp.Graphics.Helpers /// Whether a warp device was created successfully. private static unsafe bool TryGetWarpDevice(ID3D12Device** d3D12Device, IDXGIAdapter** dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1) { - using ComPtr dxgiFactory4 = default; + using ComPtr dxgiFactory6 = default; - EnableDebugMode(); - - FX.CreateDXGIFactory2(IDXGIFactoryCreationFlags, FX.__uuidof(), dxgiFactory4.GetVoidAddressOf()).Assert(); + CreateDXGIFactory6(dxgiFactory6.GetAddressOf()); using ComPtr dxgiAdapter1 = default; - dxgiFactory4.Get()->EnumWarpAdapter(FX.__uuidof(), dxgiAdapter1.GetVoidAddressOf()).Assert(); + dxgiFactory6.Get()->EnumWarpAdapter(FX.__uuidof(), dxgiAdapter1.GetVoidAddressOf()).Assert(); dxgiAdapter1.Get()->GetDesc1(dxgiDescription1).Assert(); @@ -194,35 +190,5 @@ namespace ComputeSharp.Graphics.Helpers return FX.SUCCEEDED(createDeviceResult); } - - /// - /// The creation flags for instances. - /// - private const uint IDXGIFactoryCreationFlags = -#if DEBUG - FX.DXGI_CREATE_FACTORY_DEBUG; -#else - 0; -#endif - - /// - /// Enables the debug layer for DirectX APIs. - /// - [Conditional("DEBUG")] - private static unsafe void EnableDebugMode() - { - using ComPtr d3D12Debug = default; - using ComPtr d3D12Debug1 = default; - - FX.D3D12GetDebugInterface(FX.__uuidof(), d3D12Debug.GetVoidAddressOf()).Assert(); - - d3D12Debug.Get()->EnableDebugLayer(); - - if (FX.SUCCEEDED(d3D12Debug.CopyTo(d3D12Debug1.GetAddressOf()))) - { - d3D12Debug1.Get()->SetEnableGPUBasedValidation(FX.TRUE); - d3D12Debug1.Get()->SetEnableSynchronizedCommandQueueValidation(FX.TRUE); - } - } } } From f180dd9cd82e16891e25e39a7a83e40762e8ae49 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 30 Jul 2021 14:14:35 +0200 Subject: [PATCH 2/2] Enabled high performance GPU devices enumeration --- .../Graphics/Helpers/DeviceHelper.Enumerator.cs | 7 ++++++- src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs | 8 ++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs index 093ecadf..0eba5434 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs @@ -9,6 +9,7 @@ using FX = TerraFX.Interop.Windows; using HRESULT = System.Int32; using static TerraFX.Interop.D3D_FEATURE_LEVEL; using static TerraFX.Interop.D3D_SHADER_MODEL; +using static TerraFX.Interop.DXGI_GPU_PREFERENCE; namespace ComputeSharp.Graphics.Helpers { @@ -115,7 +116,11 @@ namespace ComputeSharp.Graphics.Helpers { using ComPtr dxgiAdapter1 = default; - HRESULT enumAdapters1Result = this.dxgiFactory6.Get()->EnumAdapters1(this.index, dxgiAdapter1.GetAddressOf()); + HRESULT enumAdapters1Result = this.dxgiFactory6.Get()->EnumAdapterByGpuPreference( + this.index, + DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, + FX.__uuidof(), + dxgiAdapter1.GetVoidAddressOf()); if (enumAdapters1Result == FX.DXGI_ERROR_NOT_FOUND) { diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs index d48938e4..f04fa75f 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics; using System.Diagnostics.Contracts; using ComputeSharp.Core.Extensions; using ComputeSharp.Graphics.Extensions; @@ -9,6 +8,7 @@ using FX = TerraFX.Interop.Windows; using HRESULT = System.Int32; using static TerraFX.Interop.D3D_FEATURE_LEVEL; using static TerraFX.Interop.D3D_SHADER_MODEL; +using static TerraFX.Interop.DXGI_GPU_PREFERENCE; namespace ComputeSharp.Graphics.Helpers { @@ -92,7 +92,11 @@ namespace ComputeSharp.Graphics.Helpers { using ComPtr dxgiAdapter1 = default; - HRESULT enumAdapters1Result = dxgiFactory6.Get()->EnumAdapters1(i++, dxgiAdapter1.GetAddressOf()); + HRESULT enumAdapters1Result = dxgiFactory6.Get()->EnumAdapterByGpuPreference( + i++, + DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, + FX.__uuidof(), + dxgiAdapter1.GetVoidAddressOf()); if (enumAdapters1Result == FX.DXGI_ERROR_NOT_FOUND) {