From 6da306e7d29e34efb3feb51ca1ec6aeb9be7db6a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 2 Jan 2021 16:06:32 +0100 Subject: [PATCH] Added initial WARP fallback device property --- .../Helpers/DeviceHelper.cs | 55 +++++++++++++++++++ src/ComputeSharp/Gpu.cs | 14 +++++ 2 files changed, 69 insertions(+) diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs index 4f4e08fe..1f9052d9 100644 --- a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs @@ -60,6 +60,25 @@ namespace ComputeSharp.Graphics.Helpers return ThrowHelper.ThrowNotSupportedException("There isn't a supported GPU device on the current machine"); } + /// + /// Gets the warp instance. + /// + /// The default instance supporting at least DX12.0. + /// Thrown when a default device is not available. + public static unsafe GraphicsDevice GetWarpDevice() + { + using ComPtr d3D12Device = default; + + DXGI_ADAPTER_DESC1 dxgiDescription1; + + if (TryGetWarpDevice(d3D12Device.GetAddressOf(), &dxgiDescription1)) + { + return GetOrCreateDevice(d3D12Device.Move(), &dxgiDescription1); + } + + return ThrowHelper.ThrowNotSupportedException("There isn't a supported device on the current machine"); + } + /// /// Tries to check or create a default object. /// @@ -106,6 +125,42 @@ namespace ComputeSharp.Graphics.Helpers } } + /// + /// Tries to check or create a warp object. + /// + /// A pointer to the object to create, or . + /// A pointer to the value for the device found. + /// Whether a warp device was created successfully. + private static unsafe bool TryGetWarpDevice(ID3D12Device** d3D12Device, DXGI_ADAPTER_DESC1* dxgiDescription1) + { + using ComPtr dxgiFactory4 = default; + + EnableDebugMode(); + + FX.CreateDXGIFactory2(IDXGIFactoryCreationFlags, FX.__uuidof(), dxgiFactory4.GetVoidAddressOf()).Assert(); + + using ComPtr dxgiAdapter1 = default; + + HRESULT enumWarpAdapterResult = dxgiFactory4.Get()->EnumWarpAdapter(FX.__uuidof(), dxgiAdapter1.GetVoidAddressOf()); + + if (enumWarpAdapterResult == FX.DXGI_ERROR_NOT_FOUND) + { + return false; + } + + enumWarpAdapterResult.Assert(); + + dxgiAdapter1.Get()->GetDesc1(dxgiDescription1).Assert(); + + HRESULT createDeviceResult = FX.D3D12CreateDevice( + dxgiAdapter1.AsIUnknown().Get(), + D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, + FX.__uuidof(), + (void**)d3D12Device); + + return FX.SUCCEEDED(createDeviceResult); + } + /// /// The creation flags for instances. /// diff --git a/src/ComputeSharp/Gpu.cs b/src/ComputeSharp/Gpu.cs index c138b30b..10619a36 100644 --- a/src/ComputeSharp/Gpu.cs +++ b/src/ComputeSharp/Gpu.cs @@ -16,6 +16,11 @@ namespace ComputeSharp /// private static readonly Lazy DefaultFactory = new(DeviceHelper.GetDefaultDevice); + /// + /// The instance used to produce the warp device for . + /// + private static readonly Lazy WarpFactory = new(DeviceHelper.GetWarpDevice); + /// /// Gets whether or not the APIs can be used on the /// current machine (ie. if there is at least a supported GPU device). @@ -31,6 +36,15 @@ namespace ComputeSharp /// Make sure to check before accessing this property. public static GraphicsDevice Default => DefaultFactory.Value; + /// + /// Gets the fallback instance for the current machine. + /// This is a software version of a device that can be used when no supported GPU is available. + /// This instance cannot be manually disposed - attempting to do so is safe and it will + /// not cause an exception, but it will simply do nothing and not dispose the device. + /// + /// Thrown when a fallback device is not available. + public static GraphicsDevice Fallback => WarpFactory.Value; + /// /// Enumerates all the currently available DX12.0 devices. Note that creating a device is a relatively /// expensive operation, so consider using to be able to filter the available