Implemented GraphicsDevice caching
このコミットが含まれているのは:
1 親
e0838e678b
コミット
8cf3d038d7
6ファイル変更
+73
-8
検索条件に一致するファイルはありません
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする