diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs index ff87c3c9..7d26a995 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs @@ -37,7 +37,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(BufferType.ReadBack, byteSize); - using (CommandList copyCommandList = new CommandList(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) + using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) { copyCommandList.CopyBufferRegion(D3D12Resource, byteOffset, d3D12Resource.Get(), 0, byteSize); copyCommandList.ExecuteAndWaitForCompletion(); @@ -62,7 +62,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract MemoryHelper.Copy(span, resource.Pointer, 0, count); } - using CommandList copyCommandList = new CommandList(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); + using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); copyCommandList.CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, byteOffset, byteSize); copyCommandList.ExecuteAndWaitForCompletion(); @@ -74,7 +74,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract if (!buffer.IsPaddingPresent) { // Directly copy the input buffer - using CommandList copyCommandList = new CommandList(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); + using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); copyCommandList.CopyBufferRegion(buffer.D3D12Resource, 0, D3D12Resource, 0, SizeInBytes); copyCommandList.ExecuteAndWaitForCompletion(); diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs index a74b2c31..e685f8e9 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs @@ -5,6 +5,7 @@ using ComputeSharp.Core.Helpers; using ComputeSharp.Core.Interop; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; +using ComputeSharp.Graphics.Helpers; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; using static TerraFX.Interop.D3D12_FEATURE; @@ -12,7 +13,7 @@ using static TerraFX.Interop.D3D12_FEATURE; namespace ComputeSharp.Graphics { /// - /// A that represents a DX12.1-compatible GPU device that can be used to run compute shaders. + /// A that represents a DX12-compatible GPU device that can be used to run compute shaders. /// [DebuggerDisplay("{ToString(),raw}")] public sealed unsafe class GraphicsDevice : NativeObject @@ -84,7 +85,7 @@ namespace ComputeSharp.Graphics this.copyCommandAllocatorPool = new ID3D12CommandAllocatorPool(D3D12_COMMAND_LIST_TYPE_COPY); this.shaderResourceViewDescriptorAllocator = new ID3D12DescriptorHandleAllocator(d3d12device); - Luid = *(Luid*)&dxgiDescription1->AdapterLuid; + Luid = Luid.FromLUID(dxgiDescription1->AdapterLuid); Name = new string((char*)dxgiDescription1->Description); MemorySize = dxgiDescription1->DedicatedVideoMemory; @@ -202,6 +203,8 @@ namespace ComputeSharp.Graphics /// protected override void OnDispose() { + DeviceHelper.NotifyDisposedDevice(this); + this.d3D12Device.Dispose(); this.d3D12ComputeCommandQueue.Dispose(); this.d3D12CopyCommandQueue.Dispose(); diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs index aa7b2817..cb008818 100644 --- a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs @@ -143,7 +143,7 @@ namespace ComputeSharp.Graphics.Helpers &d3d12DeviceGuid, d3d12device.GetVoidAddressOf()).Assert(); - this.graphicsDevice = new GraphicsDevice(d3d12device.Move(), &dxgiDescription1); + this.graphicsDevice = GetOrCreateDevice(d3d12device.Move(), &dxgiDescription1); return true; } diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Factory.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Factory.cs new file mode 100644 index 00000000..d841433d --- /dev/null +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Factory.cs @@ -0,0 +1,49 @@ +using System.Collections.Generic; +using TerraFX.Interop; + +namespace ComputeSharp.Graphics.Helpers +{ + /// + internal static partial class DeviceHelper + { + /// + /// The local cache of instances that are currently usable. + /// + private static readonly Dictionary DevicesCache = new(); + + /// + /// Retrieves a instance for an object. + /// + /// The to use to get a instance. + /// The available info for the instance. + /// A instance for the input device. + private static unsafe GraphicsDevice GetOrCreateDevice(ComPtr d3D12Device, DXGI_ADAPTER_DESC1* dxgiDescription1) + { + lock (DevicesCache) + { + Luid luid = Luid.FromLUID(dxgiDescription1->AdapterLuid); + + if (!DevicesCache.TryGetValue(luid, out GraphicsDevice? device)) + { + device = new GraphicsDevice(d3D12Device, dxgiDescription1); + + DevicesCache.Add(luid, device); + } + + return device; + } + } + + /// + /// Removes a from the internal cache. + /// + /// The instance to remove from the internal cache. + public static void NotifyDisposedDevice(GraphicsDevice device) + { + lock (DevicesCache) + { + DevicesCache.Remove(device.Luid); + } + } + } +} diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs index 13adb844..f8d6982e 100644 --- a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs @@ -39,7 +39,7 @@ namespace ComputeSharp.Graphics.Helpers if (TryGetDefaultDevice(d3d12device.GetAddressOf(), &dxgiDescription1)) { - return new GraphicsDevice(d3d12device.Move(), &dxgiDescription1); + return GetOrCreateDevice(d3d12device.Move(), &dxgiDescription1); } return ThrowHelper.ThrowNotSupportedException("There isn't a supported GPU device on the current machine"); @@ -51,7 +51,7 @@ namespace ComputeSharp.Graphics.Helpers /// A pointer to the object to create, or . /// A pointer to the value for the device found. /// Whether a default device was found with the requested feature level. - public static unsafe bool TryGetDefaultDevice(ID3D12Device** d3d12device, DXGI_ADAPTER_DESC1* dxgiDescription1) + private static unsafe bool TryGetDefaultDevice(ID3D12Device** d3d12device, DXGI_ADAPTER_DESC1* dxgiDescription1) { using ComPtr dxgiFactory4 = default; diff --git a/src/ComputeSharp.Graphics/Luid.cs b/src/ComputeSharp.Graphics/Luid.cs index 275982ed..5057f388 100644 --- a/src/ComputeSharp.Graphics/Luid.cs +++ b/src/ComputeSharp.Graphics/Luid.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; +using TerraFX.Interop; namespace ComputeSharp.Graphics { @@ -16,6 +17,18 @@ namespace ComputeSharp.Graphics #pragma warning restore + /// + /// Creates a new instance from a raw value. + /// + /// The input value. + /// A instance with the same value. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static unsafe Luid FromLUID(LUID luid) + { + return *(Luid*)&luid; + } + /// [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)]