diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs index c9ae84e9..e04ba09a 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs @@ -582,5 +582,22 @@ namespace ComputeSharp.Graphics.Extensions return (d3D12FeatureDataFormatSupport.Support1 & d3D12FormatSupport1) == d3D12FormatSupport1; } + + /// + /// Checks whether or not a given shader model is supported. + /// + /// The target to use to check features for. + /// The value to check support for. + /// Whether or not the input device supports the requested shader model. + [Pure] + public static unsafe bool IsShaderModelSupported(this ref ID3D12Device d3D12Device, D3D_SHADER_MODEL d3DShaderModel) + { + D3D12_FEATURE_DATA_SHADER_MODEL d3D12ShaderModel = default; + d3D12ShaderModel.HighestShaderModel = d3DShaderModel; + + d3D12Device.CheckFeatureSupport(D3D12_FEATURE_SHADER_MODEL, &d3D12ShaderModel, (uint)sizeof(D3D12_FEATURE_DATA_SHADER_MODEL)).Assert(); + + return d3D12ShaderModel.HighestShaderModel == d3DShaderModel; + } } } diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs index 72ab52b1..69bee9a5 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs @@ -2,10 +2,13 @@ using System.Collections; using System.Collections.Generic; using ComputeSharp.Core.Extensions; +using ComputeSharp.Graphics.Extensions; using ComputeSharp.Interop; using TerraFX.Interop; using FX = TerraFX.Interop.Windows; using HRESULT = System.Int32; +using static TerraFX.Interop.D3D_FEATURE_LEVEL; +using static TerraFX.Interop.D3D_SHADER_MODEL; namespace ComputeSharp.Graphics.Helpers { @@ -126,7 +129,7 @@ namespace ComputeSharp.Graphics.Helpers HRESULT createDeviceResult = FX.D3D12CreateDevice( dxgiAdapter1.AsIUnknown().Get(), - D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, + D3D_FEATURE_LEVEL_11_0, FX.__uuidof(), null); @@ -137,7 +140,7 @@ namespace ComputeSharp.Graphics.Helpers FX.D3D12CreateDevice( dxgiAdapter1.AsIUnknown().Get(), - D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, + D3D_FEATURE_LEVEL_11_0, FX.__uuidof(), d3D12Device.GetVoidAddressOf()).Assert(); @@ -167,7 +170,7 @@ namespace ComputeSharp.Graphics.Helpers HRESULT createDeviceResult = FX.D3D12CreateDevice( dxgiAdapter1.AsIUnknown().Get(), - D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, + D3D_FEATURE_LEVEL_11_0, FX.__uuidof(), null); @@ -178,13 +181,16 @@ namespace ComputeSharp.Graphics.Helpers FX.D3D12CreateDevice( dxgiAdapter1.AsIUnknown().Get(), - D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, + D3D_FEATURE_LEVEL_11_0, FX.__uuidof(), d3D12Device.GetVoidAddressOf()).Assert(); - this.graphicsDevice = GetOrCreateDevice(d3D12Device.Get(), (IDXGIAdapter*)dxgiAdapter1.Get(), &dxgiDescription1); + if (d3D12Device.Get()->IsShaderModelSupported(D3D_SHADER_MODEL_6_0)) + { + this.graphicsDevice = GetOrCreateDevice(d3D12Device.Get(), (IDXGIAdapter*)dxgiAdapter1.Get(), &dxgiDescription1); - return true; + return true; + } } } } diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs index b06bf427..5b5b2f14 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs @@ -2,10 +2,13 @@ using System.Diagnostics; using System.Diagnostics.Contracts; using ComputeSharp.Core.Extensions; +using ComputeSharp.Graphics.Extensions; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using FX = TerraFX.Interop.Windows; using HRESULT = System.Int32; +using static TerraFX.Interop.D3D_FEATURE_LEVEL; +using static TerraFX.Interop.D3D_SHADER_MODEL; namespace ComputeSharp.Graphics.Helpers { @@ -98,17 +101,54 @@ namespace ComputeSharp.Graphics.Helpers continue; } - HRESULT createDeviceResult = FX.D3D12CreateDevice( - dxgiAdapter1.AsIUnknown().Get(), - D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, - FX.__uuidof(), - (void**)d3D12Device); - - if (FX.SUCCEEDED(createDeviceResult)) + // Explicit paths for when a device is being retrieved or not, with special handling + // for the additional check that is required for the SM6 level. This can't be checked + // without creating a device first, so the path for when the target device pointer is + // null is useful to do an initial filtering using D3D12CreateDevice to avoid creating + // a device for adapters that would've failed at the FL11 check already. + if (d3D12Device == null) { - dxgiAdapter1.CopyTo(dxgiAdapter); + HRESULT createDeviceResult = FX.D3D12CreateDevice( + dxgiAdapter1.AsIUnknown().Get(), + D3D_FEATURE_LEVEL_11_0, + FX.__uuidof(), + null); - return true; + if (FX.SUCCEEDED(createDeviceResult)) + { + using ComPtr d3D12DeviceCandidate = default; + + createDeviceResult = FX.D3D12CreateDevice( + dxgiAdapter1.AsIUnknown().Get(), + D3D_FEATURE_LEVEL_11_0, + FX.__uuidof(), + d3D12DeviceCandidate.GetVoidAddressOf()); + + if (FX.SUCCEEDED(createDeviceResult) && + d3D12DeviceCandidate.Get()->IsShaderModelSupported(D3D_SHADER_MODEL_6_0)) + { + return true; + } + } + } + else + { + using ComPtr d3D12DeviceCandidate = default; + + HRESULT createDeviceResult = FX.D3D12CreateDevice( + dxgiAdapter1.AsIUnknown().Get(), + D3D_FEATURE_LEVEL_11_0, + FX.__uuidof(), + d3D12DeviceCandidate.GetVoidAddressOf()); + + if (FX.SUCCEEDED(createDeviceResult) && + d3D12DeviceCandidate.Get()->IsShaderModelSupported(D3D_SHADER_MODEL_6_0)) + { + d3D12DeviceCandidate.CopyTo(d3D12Device); + dxgiAdapter1.CopyTo(dxgiAdapter); + + return true; + } } } } @@ -136,7 +176,7 @@ namespace ComputeSharp.Graphics.Helpers HRESULT createDeviceResult = FX.D3D12CreateDevice( dxgiAdapter1.AsIUnknown().Get(), - D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, + D3D_FEATURE_LEVEL_11_0, FX.__uuidof(), (void**)d3D12Device);