Implemented GraphicsDevice caching

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-21 13:17:19 +01:00
1 親 e0838e678b
コミット 8cf3d038d7
6ファイル変更
+73 -8

検索条件に一致するファイルはありません

+3 -3
ファイルの表示
@@ -37,7 +37,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
using ComPtr<ID3D12Resource> 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();
+5 -2
ファイルの表示
@@ -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
{
/// <summary>
/// A <see langword="class"/> that represents a DX12.1-compatible GPU device that can be used to run compute shaders.
/// A <see langword="class"/> that represents a DX12-compatible GPU device that can be used to run compute shaders.
/// </summary>
[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
/// <inheritdoc/>
protected override void OnDispose()
{
DeviceHelper.NotifyDisposedDevice(this);
this.d3D12Device.Dispose();
this.d3D12ComputeCommandQueue.Dispose();
this.d3D12CopyCommandQueue.Dispose();
+1 -1
ファイルの表示
@@ -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;
}
+49
ファイルの表示
@@ -0,0 +1,49 @@
using System.Collections.Generic;
using TerraFX.Interop;
namespace ComputeSharp.Graphics.Helpers
{
/// <inheritdoc/>
internal static partial class DeviceHelper
{
/// <summary>
/// The local cache of <see cref="GraphicsDevice"/> instances that are currently usable.
/// </summary>
private static readonly Dictionary<Luid, GraphicsDevice> DevicesCache = new();
/// <summary>
/// Retrieves a <see cref="GraphicsDevice"/> instance for an <see cref="ID3D12Device"/> object.
/// </summary>
/// <param name="d3D12Device">The <see cref="ID3D12Device"/> to use to get a <see cref="GraphicsDevice"/> instance.</param>
/// <param name="dxgiDescription1">The available info for the <see cref="GraphicsDevice"/> instance.</param>
/// <returns>A <see cref="GraphicsDevice"/> instance for the input device.</returns>
private static unsafe GraphicsDevice GetOrCreateDevice(ComPtr<ID3D12Device> 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;
}
}
/// <summary>
/// Removes a <see cref="GraphicsDevice"/> from the internal cache.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to remove from the internal cache.</param>
public static void NotifyDisposedDevice(GraphicsDevice device)
{
lock (DevicesCache)
{
DevicesCache.Remove(device.Luid);
}
}
}
}
+2 -2
ファイルの表示
@@ -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<GraphicsDevice>("There isn't a supported GPU device on the current machine");
@@ -51,7 +51,7 @@ namespace ComputeSharp.Graphics.Helpers
/// <param name="d3d12device">A pointer to the <see cref="ID3D12Device"/> object to create, or <see langword="null"/>.</param>
/// <param name="dxgiDescription1">A pointer to the <see cref="DXGI_ADAPTER_DESC1"/> value for the device found.</param>
/// <returns>Whether a default device was found with the requested feature level.</returns>
public static unsafe bool TryGetDefaultDevice(ID3D12Device** d3d12device, DXGI_ADAPTER_DESC1* dxgiDescription1)
private static unsafe bool TryGetDefaultDevice(ID3D12Device** d3d12device, DXGI_ADAPTER_DESC1* dxgiDescription1)
{
using ComPtr<IDXGIFactory4> dxgiFactory4 = default;
+13
ファイルの表示
@@ -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
/// <summary>
/// Creates a new <see cref="Luid"/> instance from a raw <see cref="LUID"/> value.
/// </summary>
/// <param name="luid">The input <see cref="LUID"/> value.</param>
/// <returns>A <see cref="Luid"/> instance with the same value.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe Luid FromLUID(LUID luid)
{
return *(Luid*)&luid;
}
/// <inheritdoc/>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]