29個のファイルの変更、980行の追加、237行の削除

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

+13 -2
ファイルの表示
@@ -33,6 +33,8 @@ internal unsafe struct CommandList : IDisposable
/// </summary>
private ComPtr<ID3D12CommandAllocator> d3D12CommandAllocator;
private ulong d3D12ComputeFenceWaitValue;
/// <summary>
/// Creates a new <see cref="CommandList"/> instance with the specified parameters.
/// </summary>
@@ -42,6 +44,7 @@ internal unsafe struct CommandList : IDisposable
{
this.device = device;
this.d3D12CommandListType = d3D12CommandListType;
this.d3D12ComputeFenceWaitValue = 0;
Unsafe.SkipInit(out this.d3D12GraphicsCommandList);
Unsafe.SkipInit(out this.d3D12CommandAllocator);
@@ -67,6 +70,7 @@ internal unsafe struct CommandList : IDisposable
{
this.device = device;
this.d3D12CommandListType = D3D12_COMMAND_LIST_TYPE_COMPUTE;
this.d3D12ComputeFenceWaitValue = 0;
Unsafe.SkipInit(out this.d3D12GraphicsCommandList);
Unsafe.SkipInit(out this.d3D12CommandAllocator);
@@ -95,6 +99,13 @@ internal unsafe struct CommandList : IDisposable
/// </summary>
public readonly D3D12_COMMAND_LIST_TYPE D3D12CommandListType => this.d3D12CommandListType;
public readonly ulong D3D12ComputeFenceWaitValue => this.d3D12ComputeFenceWaitValue;
public void AddComputeFenceWait(ulong d3D12FenceValue)
{
this.d3D12ComputeFenceWaitValue = Math.Max(this.d3D12ComputeFenceWaitValue, d3D12FenceValue);
}
/// <summary>
/// Gets the <see cref="ID3D12GraphicsCommandList"/> object in use by the current instance.
/// </summary>
@@ -156,11 +167,11 @@ internal unsafe struct CommandList : IDisposable
return this.device.ExecuteCommandListAsync(ref this);
}
public void ExecuteWithoutWaiting()
public void ExecuteWithoutWaiting(ref GraphicsResourceLeaseSet? resourceLeases)
{
this.d3D12GraphicsCommandList.Get()->Close().Assert();
this.device.ExecuteCommandListWithoutWaiting(ref this);
this.device.ExecuteCommandListWithoutWaiting(ref this, ref resourceLeases);
}
/// <inheritdoc/>
+80
ファイルの表示
@@ -0,0 +1,80 @@
using System;
using System.Collections.Concurrent;
using System.Threading;
using ComputeSharp.Interop;
using ComputeSharp.Resources.Interop;
namespace ComputeSharp.Graphics.Commands;
internal sealed class GraphicsResourceLeaseSet
{
private static readonly ConcurrentQueue<GraphicsResourceLeaseSet> LeaseSetQueue = [];
private static volatile int queuedLeaseSetCount;
private ReferenceTracker.Lease[] leases = new ReferenceTracker.Lease[16];
private int count;
public static GraphicsResourceLeaseSet Rent()
{
if (LeaseSetQueue.TryDequeue(out GraphicsResourceLeaseSet? leaseSet))
{
_ = Interlocked.Decrement(ref queuedLeaseSetCount);
}
else
{
leaseSet = new GraphicsResourceLeaseSet();
}
return leaseSet;
}
public void Add(ReferenceTracker.Lease lease)
{
ReferenceTracker.Lease[] leases = this.leases;
if (this.count == leases.Length)
{
Array.Resize(ref this.leases, leases.Length * 2);
leases = this.leases;
}
leases[this.count++] = lease;
}
public void MarkComputeFence(ulong d3D12FenceValue)
{
ReferenceTracker.Lease[] leases = this.leases;
for (int i = 0; i < this.count; i++)
{
if (leases[i].TrackedObject is ID3D12ComputeFenceTrackedResource resource)
{
resource.MarkComputeFence(d3D12FenceValue);
}
}
}
public void Release()
{
ReferenceTracker.Lease[] leases = this.leases;
for (int i = 0; i < this.count; i++)
{
leases[i].Dispose();
}
this.count = 0;
if (Interlocked.Increment(ref queuedLeaseSetCount) < 16)
{
LeaseSetQueue.Enqueue(this);
}
else
{
_ = Interlocked.Decrement(ref queuedLeaseSetCount);
}
}
}
+35 -17
ファイルの表示
@@ -12,16 +12,15 @@ namespace ComputeSharp.Graphics.Commands.Interop;
/// </summary>
/// <param name="d3D12CommandListType">The command list type to use.</param>
/// <param name="d3D12Fence">The fence used to track deferred command list completion.</param>
internal readonly unsafe struct ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE d3D12CommandListType, ID3D12Fence* d3D12Fence) : IDisposable
/// <param name="maximumPendingCommandListCount">The maximum number of pending command lists.</param>
internal readonly unsafe struct ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE d3D12CommandListType, ID3D12Fence* d3D12Fence, int maximumPendingCommandListCount) : IDisposable
{
private const int MaximumPendingCommandListCount = 16;
/// <summary>
/// The queue of <see cref="D3D12CommandListBundle"/> items with the available command lists.
/// </summary>
private readonly Queue<D3D12CommandListBundle> d3D12CommandListBundleQueue = new(MaximumPendingCommandListCount * 2);
private readonly Queue<D3D12CommandListBundle> d3D12CommandListBundleQueue = new(maximumPendingCommandListCount * 2);
private readonly Queue<PendingD3D12CommandListBundle> pendingD3D12CommandListBundleQueue = new(MaximumPendingCommandListCount);
private readonly Queue<PendingD3D12CommandListBundle> pendingD3D12CommandListBundleQueue = new(maximumPendingCommandListCount);
/// <summary>
/// Rents a <see cref="ID3D12GraphicsCommandList"/> and <see cref="ID3D12CommandAllocator"/> pair.
@@ -75,25 +74,37 @@ internal readonly unsafe struct ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE d3
}
}
public void ReturnWhenCompleted(ID3D12GraphicsCommandList* d3D12CommandList, ID3D12CommandAllocator* d3D12CommandAllocator, ulong fenceValue)
public void ReturnWhenCompleted(ID3D12GraphicsCommandList* d3D12CommandList, ID3D12CommandAllocator* d3D12CommandAllocator, ulong fenceValue, GraphicsResourceLeaseSet? resourceLeases)
{
PendingD3D12CommandListBundle pendingD3D12CommandListBundle = default;
bool hasPendingD3D12CommandListBundle = false;
lock (this.d3D12CommandListBundleQueue)
{
ReclaimCompletedCommandLists();
if (this.pendingD3D12CommandListBundleQueue.Count == MaximumPendingCommandListCount)
if (this.pendingD3D12CommandListBundleQueue.Count >= maximumPendingCommandListCount)
{
PendingD3D12CommandListBundle pendingD3D12CommandListBundle = this.pendingD3D12CommandListBundleQueue.Dequeue();
if (pendingD3D12CommandListBundle.FenceValue > d3D12Fence->GetCompletedValue())
{
d3D12Fence->SetEventOnCompletion(pendingD3D12CommandListBundle.FenceValue, default).Assert();
}
this.d3D12CommandListBundleQueue.Enqueue(pendingD3D12CommandListBundle.D3D12CommandListBundle);
pendingD3D12CommandListBundle = this.pendingD3D12CommandListBundleQueue.Dequeue();
hasPendingD3D12CommandListBundle = true;
}
this.pendingD3D12CommandListBundleQueue.Enqueue(new PendingD3D12CommandListBundle(d3D12CommandList, d3D12CommandAllocator, fenceValue));
this.pendingD3D12CommandListBundleQueue.Enqueue(new PendingD3D12CommandListBundle(d3D12CommandList, d3D12CommandAllocator, fenceValue, resourceLeases));
}
if (hasPendingD3D12CommandListBundle)
{
if (pendingD3D12CommandListBundle.FenceValue > d3D12Fence->GetCompletedValue())
{
d3D12Fence->SetEventOnCompletion(pendingD3D12CommandListBundle.FenceValue, default).Assert();
}
pendingD3D12CommandListBundle.ResourceLeases?.Release();
lock (this.d3D12CommandListBundleQueue)
{
this.d3D12CommandListBundleQueue.Enqueue(pendingD3D12CommandListBundle.D3D12CommandListBundle);
}
}
}
@@ -122,6 +133,8 @@ internal readonly unsafe struct ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE d3
foreach (PendingD3D12CommandListBundle current in this.pendingD3D12CommandListBundleQueue)
{
current.ResourceLeases?.Release();
_ = current.D3D12CommandListBundle.D3D12CommandList->Release();
_ = current.D3D12CommandListBundle.D3D12CommandAllocator->Release();
}
@@ -139,6 +152,9 @@ internal readonly unsafe struct ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE d3
pendingD3D12CommandListBundle.FenceValue <= completedValue)
{
_ = this.pendingD3D12CommandListBundleQueue.Dequeue();
pendingD3D12CommandListBundle.ResourceLeases?.Release();
this.d3D12CommandListBundleQueue.Enqueue(pendingD3D12CommandListBundle.D3D12CommandListBundle);
}
}
@@ -182,10 +198,12 @@ internal readonly unsafe struct ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE d3
public readonly ID3D12CommandAllocator* D3D12CommandAllocator = d3D12CommandAllocator;
}
private readonly struct PendingD3D12CommandListBundle(ID3D12GraphicsCommandList* d3D12CommandList, ID3D12CommandAllocator* d3D12CommandAllocator, ulong fenceValue)
private readonly struct PendingD3D12CommandListBundle(ID3D12GraphicsCommandList* d3D12CommandList, ID3D12CommandAllocator* d3D12CommandAllocator, ulong fenceValue, GraphicsResourceLeaseSet? resourceLeases)
{
public readonly D3D12CommandListBundle D3D12CommandListBundle = new(d3D12CommandList, d3D12CommandAllocator);
public readonly ulong FenceValue = fenceValue;
public readonly GraphicsResourceLeaseSet? ResourceLeases = resourceLeases;
}
}
+21 -21
ファイルの表示
@@ -271,16 +271,16 @@ internal static unsafe class ID3D12DeviceExtensions
}
/// <summary>
/// クロス API 共有が可能なコミット型の 2D テクスチャ リソースを生成します。
/// Creates a committed 2D texture resource that can be shared across APIs.
/// </summary>
/// <param name="d3D12Device">使用する <see cref="ID3D12Device"/> インスタンス。</param>
/// <param name="resourceType">生成するリソースの種別。</param>
/// <param name="dxgiFormat">使用する <see cref="DXGI_FORMAT"/> 値。</param>
/// <param name="width">テクスチャの幅。</param>
/// <param name="height">テクスチャの高さ。</param>
/// <param name="isRenderTarget">レンダーターゲットとして使用可能にするかどうか。</param>
/// <param name="d3D12ResourceStates">生成されたリソースの初期 <see cref="D3D12_RESOURCE_STATES"/> 値。</param>
/// <returns>共有可能な <see cref="ID3D12Resource"/> への参照。</returns>
/// <param name="d3D12Device">The <see cref="ID3D12Device"/> instance to use.</param>
/// <param name="resourceType">The resource type of the resource to create.</param>
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> value to use.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="isRenderTarget">Whether the texture can be used as a render target.</param>
/// <param name="d3D12ResourceStates">The initial <see cref="D3D12_RESOURCE_STATES"/> value for the created resource.</param>
/// <returns>A reference to the shareable <see cref="ID3D12Resource"/> created.</returns>
public static ComPtr<ID3D12Resource> CreateSharedCommittedResource(
this ref ID3D12Device d3D12Device,
ResourceType resourceType,
@@ -328,10 +328,10 @@ internal static unsafe class ID3D12DeviceExtensions
}
/// <summary>
/// 他 API と共有可能な <see cref="ID3D12Fence"/> を生成します。
/// Creates an <see cref="ID3D12Fence"/> that can be shared with other APIs.
/// </summary>
/// <param name="d3D12Device">使用する <see cref="ID3D12Device"/> インスタンス。</param>
/// <returns>共有可能な <see cref="ID3D12Fence"/> への参照。</returns>
/// <param name="d3D12Device">The <see cref="ID3D12Device"/> instance to use.</param>
/// <returns>A reference to the shareable <see cref="ID3D12Fence"/> created.</returns>
public static ComPtr<ID3D12Fence> CreateSharedFence(this ref ID3D12Device d3D12Device)
{
using ComPtr<ID3D12Fence> d3D12Fence = default;
@@ -346,11 +346,11 @@ internal static unsafe class ID3D12DeviceExtensions
}
/// <summary>
/// 指定した COM オブジェクトに対する共有 NT ハンドルを生成します。
/// Creates a shared NT handle for the specified COM object.
/// </summary>
/// <param name="d3D12Device">使用する <see cref="ID3D12Device"/> インスタンス。</param>
/// <param name="pObject">共有ハンドルを生成する対象の COM オブジェクト。</param>
/// <returns>生成された共有 NT ハンドル。呼び出し側が <c>CloseHandle</c> で解放する責任を持ちます。</returns>
/// <param name="d3D12Device">The <see cref="ID3D12Device"/> instance to use.</param>
/// <param name="pObject">The COM object to create the shared handle for.</param>
/// <returns>The created shared NT handle. The caller is responsible for releasing it with <c>CloseHandle</c>.</returns>
public static HANDLE CreateSharedHandle(this ref ID3D12Device d3D12Device, IUnknown* pObject)
{
HANDLE handle;
@@ -361,12 +361,12 @@ internal static unsafe class ID3D12DeviceExtensions
}
/// <summary>
/// 指定した共有 NT ハンドルから <typeparamref name="T"/> 型の COM オブジェクトを開きます。
/// Opens a COM object of type <typeparamref name="T"/> from the specified shared NT handle.
/// </summary>
/// <typeparam name="T">開く COM オブジェクトの型。</typeparam>
/// <param name="d3D12Device">使用する <see cref="ID3D12Device"/> インスタンス。</param>
/// <param name="handle">開く対象の共有 NT ハンドル。</param>
/// <returns>開かれた <typeparamref name="T"/> への参照。</returns>
/// <typeparam name="T">The type of COM object to open.</typeparam>
/// <param name="d3D12Device">The <see cref="ID3D12Device"/> instance to use.</param>
/// <param name="handle">The shared NT handle to open.</param>
/// <returns>A reference to the opened <typeparamref name="T"/>.</returns>
public static ComPtr<T> OpenSharedHandle<T>(this ref ID3D12Device d3D12Device, HANDLE handle)
where T : unmanaged, IComObject
{
+43 -6
ファイルの表示
@@ -49,12 +49,23 @@ unsafe partial class GraphicsDevice
default: default(ArgumentException).Throw(nameof(commandList)); return;
}
ID3D12Fence* d3D12WaitFence = null;
ulong d3D12WaitFenceValue = 0;
if (commandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COPY && commandList.D3D12ComputeFenceWaitValue > 0)
{
d3D12WaitFence = this.d3D12ComputeFence.Get();
d3D12WaitFenceValue = commandList.D3D12ComputeFenceWaitValue;
}
ulong updatedFenceValue = ExecuteCommandListCore(
ref commandList,
d3D12CommandQueue,
d3D12Fence,
ref d3D12FenceValue,
d3D12CommandQueueLock);
d3D12CommandQueueLock,
d3D12WaitFence,
d3D12WaitFenceValue);
// If the fence value hasn't been reached, wait until the operation completes
if (updatedFenceValue > d3D12Fence->GetCompletedValue())
@@ -79,7 +90,9 @@ unsafe partial class GraphicsDevice
this.d3D12ComputeCommandQueue.Get(),
this.d3D12ComputeFence.Get(),
ref this.nextD3D12ComputeFenceValue,
this.d3D12ComputeCommandQueueLock);
this.d3D12ComputeCommandQueueLock,
d3D12WaitFence: null,
d3D12WaitFenceValue: 0);
// If the fence value has been reached, complete synchronously
if (updatedFenceValue <= this.d3D12ComputeFence.Get()->GetCompletedValue())
@@ -98,19 +111,36 @@ unsafe partial class GraphicsDevice
commandList.DetachD3D12CommandAllocator()).AsValueTask();
}
internal void ExecuteCommandListWithoutWaiting(ref CommandList commandList)
internal void ExecuteCommandListWithoutWaiting(ref CommandList commandList, ref GraphicsResourceLeaseSet? resourceLeases)
{
ulong updatedFenceValue = ExecuteCommandListCore(
ref commandList,
this.d3D12ComputeCommandQueue.Get(),
this.d3D12ComputeFence.Get(),
ref this.nextD3D12ComputeFenceValue,
this.d3D12ComputeCommandQueueLock);
this.d3D12ComputeCommandQueueLock,
d3D12WaitFence: null,
d3D12WaitFenceValue: 0);
GraphicsResourceLeaseSet? pendingResourceLeases = resourceLeases;
resourceLeases = null;
pendingResourceLeases?.MarkComputeFence(updatedFenceValue);
this.computeCommandListPool.ReturnWhenCompleted(
commandList.DetachD3D12CommandList(),
commandList.DetachD3D12CommandAllocator(),
updatedFenceValue);
updatedFenceValue,
pendingResourceLeases);
}
internal void WaitForComputeFence(ulong d3D12FenceValue)
{
if (d3D12FenceValue > this.d3D12ComputeFence.Get()->GetCompletedValue())
{
this.d3D12ComputeFence.Get()->SetEventOnCompletion(d3D12FenceValue, default).Assert();
}
}
private static ulong ExecuteCommandListCore(
@@ -118,10 +148,17 @@ unsafe partial class GraphicsDevice
ID3D12CommandQueue* d3D12CommandQueue,
ID3D12Fence* d3D12Fence,
ref ulong d3D12FenceValue,
object d3D12CommandQueueLock)
object d3D12CommandQueueLock,
ID3D12Fence* d3D12WaitFence,
ulong d3D12WaitFenceValue)
{
lock (d3D12CommandQueueLock)
{
if (d3D12WaitFence is not null && d3D12WaitFenceValue > d3D12WaitFence->GetCompletedValue())
{
d3D12CommandQueue->Wait(d3D12WaitFence, d3D12WaitFenceValue).Assert();
}
fixed (ID3D12CommandList** d3D12CommandList = &commandList.GetD3D12CommandListPinnableAddressOf())
{
d3D12CommandQueue->ExecuteCommandLists(1, d3D12CommandList);
+25 -25
ファイルの表示
@@ -9,15 +9,15 @@ namespace ComputeSharp;
unsafe partial class GraphicsDevice
{
/// <summary>
/// クロス API 共有が可能な 2D テクスチャ リソースを生成します。
/// Creates a 2D texture resource that can be shared across APIs.
/// </summary>
/// <param name="resourceType">生成するリソースの種別。</param>
/// <param name="dxgiFormat">使用する <see cref="DXGI_FORMAT"/> 値。</param>
/// <param name="width">テクスチャの幅。</param>
/// <param name="height">テクスチャの高さ。</param>
/// <param name="isRenderTarget">レンダーターゲットとして使用可能にするかどうか。</param>
/// <param name="d3D12Resource">生成された <see cref="ID3D12Resource"/> オブジェクト。</param>
/// <param name="d3D12ResourceStates">生成されたリソースの初期 <see cref="D3D12_RESOURCE_STATES"/> 値。</param>
/// <param name="resourceType">The resource type of the resource to create.</param>
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> value to use.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="isRenderTarget">Whether the texture can be used as a render target.</param>
/// <param name="d3D12Resource">The created <see cref="ID3D12Resource"/> object.</param>
/// <param name="d3D12ResourceStates">The initial <see cref="D3D12_RESOURCE_STATES"/> value for the created resource.</param>
internal void CreateSharedResource(
ResourceType resourceType,
DXGI_FORMAT dxgiFormat,
@@ -31,49 +31,49 @@ unsafe partial class GraphicsDevice
}
/// <summary>
/// 指定した COM オブジェクトに対する共有 NT ハンドルを生成します。
/// Creates a shared NT handle for the specified COM object.
/// </summary>
/// <param name="pObject">共有ハンドルを生成する対象の COM オブジェクト。</param>
/// <returns>生成された共有 NT ハンドル。</returns>
/// <param name="pObject">The COM object to create the shared handle for.</param>
/// <returns>The created shared NT handle.</returns>
internal HANDLE CreateSharedHandle(IUnknown* pObject)
{
return this.d3D12Device.Get()->CreateSharedHandle(pObject);
}
/// <summary>
/// 指定した共有 NT ハンドルから <see cref="ID3D12Resource"/> を開きます。
/// Opens an <see cref="ID3D12Resource"/> from the specified shared NT handle.
/// </summary>
/// <param name="handle">開く対象の共有 NT ハンドル。</param>
/// <returns>開かれた <see cref="ID3D12Resource"/> への参照。</returns>
/// <param name="handle">The shared NT handle to open.</param>
/// <returns>A reference to the opened <see cref="ID3D12Resource"/>.</returns>
internal ComPtr<ID3D12Resource> OpenSharedResource(HANDLE handle)
{
return this.d3D12Device.Get()->OpenSharedHandle<ID3D12Resource>(handle);
}
/// <summary>
/// 他 API と共有可能な <see cref="ID3D12Fence"/> を生成します。
/// Creates an <see cref="ID3D12Fence"/> that can be shared with other APIs.
/// </summary>
/// <returns>共有可能な <see cref="ID3D12Fence"/> への参照。</returns>
/// <returns>A reference to the shareable <see cref="ID3D12Fence"/> created.</returns>
internal ComPtr<ID3D12Fence> CreateSharedFence()
{
return this.d3D12Device.Get()->CreateSharedFence();
}
/// <summary>
/// 指定した共有 NT ハンドルから <see cref="ID3D12Fence"/> を開きます。
/// Opens an <see cref="ID3D12Fence"/> from the specified shared NT handle.
/// </summary>
/// <param name="handle">開く対象の共有 NT ハンドル。</param>
/// <returns>開かれた <see cref="ID3D12Fence"/> への参照。</returns>
/// <param name="handle">The shared NT handle to open.</param>
/// <returns>A reference to the opened <see cref="ID3D12Fence"/>.</returns>
internal ComPtr<ID3D12Fence> OpenSharedFence(HANDLE handle)
{
return this.d3D12Device.Get()->OpenSharedHandle<ID3D12Fence>(handle);
}
/// <summary>
/// コンピュート キュー上で指定した共有フェンスに値をシグナルします。
/// Signals the specified shared fence with a value on the compute queue.
/// </summary>
/// <param name="d3D12Fence">対象の <see cref="ID3D12Fence"/>。</param>
/// <param name="value">シグナルする値。</param>
/// <param name="d3D12Fence">The target <see cref="ID3D12Fence"/>.</param>
/// <param name="value">The value to signal.</param>
internal void SignalSharedFence(ID3D12Fence* d3D12Fence, ulong value)
{
lock (this.d3D12ComputeCommandQueueLock)
@@ -83,10 +83,10 @@ unsafe partial class GraphicsDevice
}
/// <summary>
/// コンピュート キュー上で指定した共有フェンスが目標値に達するまで待機するよう登録します。
/// Enqueues a wait on the compute queue until the specified shared fence reaches the target value.
/// </summary>
/// <param name="d3D12Fence">対象の <see cref="ID3D12Fence"/>。</param>
/// <param name="value">待機する目標値。</param>
/// <param name="d3D12Fence">The target <see cref="ID3D12Fence"/>.</param>
/// <param name="value">The target value to wait for.</param>
internal void WaitForSharedFence(ID3D12Fence* d3D12Fence, ulong value)
{
lock (this.d3D12ComputeCommandQueueLock)
+14 -2
ファイルの表示
@@ -153,9 +153,11 @@ public sealed unsafe partial class GraphicsDevice : IReferenceTrackedObject
this.d3D12CopyCommandQueue = d3D12Device->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COPY);
this.d3D12ComputeFence = d3D12Device->CreateFence();
this.d3D12CopyFence = d3D12Device->CreateFence();
int maximumPendingCommandListCount = GetMaximumPendingCommandListCount();
this.shaderResourceViewDescriptorAllocator = new ID3D12DescriptorHandleAllocator(d3D12Device);
this.computeCommandListPool = new ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE_COMPUTE, this.d3D12ComputeFence.Get());
this.copyCommandListPool = new ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE_COPY, this.d3D12CopyFence.Get());
this.computeCommandListPool = new ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE_COMPUTE, this.d3D12ComputeFence.Get(), maximumPendingCommandListCount);
this.copyCommandListPool = new ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE_COPY, this.d3D12CopyFence.Get(), maximumPendingCommandListCount);
this.allocator = new ComPtr<ID3D12MemoryAllocator>(allocator);
Luid = Luid.FromLUID(dxgiDescription1->AdapterLuid);
@@ -342,6 +344,16 @@ public sealed unsafe partial class GraphicsDevice : IReferenceTrackedObject
D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW);
}
private static int GetMaximumPendingCommandListCount()
{
return AppContext.GetData("ComputeSharp.MaximumPendingCommandListCount") switch
{
int count when count > 0 => count,
string text when int.TryParse(text, out int count) && count > 0 => count,
_ => 16
};
}
/// <summary>
/// Registers a <see cref="PipelineData"/> object for the current device to enable early disposal.
/// </summary>
+90 -90
ファイルの表示
@@ -9,18 +9,18 @@ namespace ComputeSharp.Interop;
public static unsafe partial class InteropServices
{
/// <summary>
/// クロス API 共有が可能な <see cref="ReadWriteTexture2D{T}"/> を確保します。
/// Allocates a <see cref="ReadWriteTexture2D{T}"/> that can be shared across APIs.
/// </summary>
/// <typeparam name="T">テクスチャに格納する要素の型。</typeparam>
/// <param name="device">テクスチャの確保に使用する <see cref="GraphicsDevice"/>。</param>
/// <param name="width">テクスチャの幅。</param>
/// <param name="height">テクスチャの高さ。</param>
/// <returns>共有可能な <see cref="ReadWriteTexture2D{T}"/> インスタンス。</returns>
/// <exception cref="ArgumentNullException"><paramref name="device"/> が <see langword="null"/> の場合にスローされます。</exception>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to allocate the texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <returns>A shareable <see cref="ReadWriteTexture2D{T}"/> instance.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="device"/> is <see langword="null"/>.</exception>
/// <remarks>
/// 返されるテクスチャは <see cref="CreateSharedHandle{T}(Texture2D{T})"/> で共有 NT ハンドルをエクスポートでき、
/// 対応する外部 API から開けます。D3D11およびDirect2Dの共有ターゲットには正規化オーバーロードを使用してください。
/// クロス API の同期は共有フェンスで行ってください。
/// The returned texture can export a shared NT handle via <see cref="CreateSharedHandle{T}(Texture2D{T})"/>,
/// which can then be opened from compatible external APIs. Use the normalized overloads for D3D11 and Direct2D shared targets.
/// Use a shared fence for cross API synchronization.
/// </remarks>
public static ReadWriteTexture2D<T> AllocateSharedReadWriteTexture2D<T>(GraphicsDevice device, int width, int height)
where T : unmanaged
@@ -31,19 +31,19 @@ public static unsafe partial class InteropServices
}
/// <summary>
/// クロス API 共有が可能な正規化 <see cref="ReadWriteTexture2D{T, TPixel}"/> を確保します。
/// Allocates a normalized <see cref="ReadWriteTexture2D{T, TPixel}"/> that can be shared across APIs.
/// </summary>
/// <typeparam name="T">CPU側で使用するピクセルの型。</typeparam>
/// <typeparam name="TPixel">GPU側で使用する正規化ピクセルの型。</typeparam>
/// <param name="device">テクスチャの確保に使用する <see cref="GraphicsDevice"/>。</param>
/// <param name="width">テクスチャの幅。</param>
/// <param name="height">テクスチャの高さ。</param>
/// <returns>共有可能な正規化 <see cref="ReadWriteTexture2D{T, TPixel}"/> インスタンス。</returns>
/// <exception cref="ArgumentNullException"><paramref name="device"/> が <see langword="null"/> の場合にスローされます。</exception>
/// <typeparam name="T">The type of pixels used on the CPU side.</typeparam>
/// <typeparam name="TPixel">The type of normalized pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to allocate the texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <returns>A shareable normalized <see cref="ReadWriteTexture2D{T, TPixel}"/> instance.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="device"/> is <see langword="null"/>.</exception>
/// <remarks>
/// 返されるテクスチャはUAV、同時アクセス、レンダーターゲットに対応します。D3D11から共有リソースとして開け、
/// 互換性のある形式ではDirect2Dの共有ターゲットとして開けます。
/// クロス API の同期は共有フェンスで行ってください。
/// The returned texture supports UAV, simultaneous access and render target usage. It can be opened as a shared resource from D3D11,
/// and as a Direct2D shared target for compatible formats.
/// Use a shared fence for cross API synchronization.
/// </remarks>
public static ReadWriteTexture2D<T, TPixel> AllocateSharedReadWriteTexture2D<T, TPixel>(GraphicsDevice device, int width, int height)
where T : unmanaged, IPixel<T, TPixel>
@@ -55,14 +55,14 @@ public static unsafe partial class InteropServices
}
/// <summary>
/// クロス API 共有が可能な <see cref="ReadOnlyTexture2D{T}"/> を確保します。
/// Allocates a <see cref="ReadOnlyTexture2D{T}"/> that can be shared across APIs.
/// </summary>
/// <typeparam name="T">テクスチャに格納する要素の型。</typeparam>
/// <param name="device">テクスチャの確保に使用する <see cref="GraphicsDevice"/>。</param>
/// <param name="width">テクスチャの幅。</param>
/// <param name="height">テクスチャの高さ。</param>
/// <returns>共有可能な <see cref="ReadOnlyTexture2D{T}"/> インスタンス。</returns>
/// <exception cref="ArgumentNullException"><paramref name="device"/> が <see langword="null"/> の場合にスローされます。</exception>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to allocate the texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <returns>A shareable <see cref="ReadOnlyTexture2D{T}"/> instance.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="device"/> is <see langword="null"/>.</exception>
public static ReadOnlyTexture2D<T> AllocateSharedReadOnlyTexture2D<T>(GraphicsDevice device, int width, int height)
where T : unmanaged
{
@@ -72,17 +72,17 @@ public static unsafe partial class InteropServices
}
/// <summary>
/// 外部 API が所有する共有 NT ハンドルを <see cref="ReadWriteTexture2D{T}"/> として開きます。
/// Opens a shared NT handle owned by an external API as a <see cref="ReadWriteTexture2D{T}"/>.
/// </summary>
/// <typeparam name="T">テクスチャに格納する要素の型。</typeparam>
/// <param name="device">リソースを開くために使用する <see cref="GraphicsDevice"/>。</param>
/// <param name="handle">開く対象の共有 NT ハンドル。</param>
/// <returns>共有リソースをラップする <see cref="ReadWriteTexture2D{T}"/> インスタンス。</returns>
/// <exception cref="ArgumentNullException"><paramref name="device"/> が <see langword="null"/> の場合にスローされます。</exception>
/// <exception cref="ArgumentException">共有リソースが 2D テクスチャでない、またはフォーマットが <typeparamref name="T"/> と一致しない場合にスローされます。</exception>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to open the resource.</param>
/// <param name="handle">The shared NT handle to open.</param>
/// <returns>A <see cref="ReadWriteTexture2D{T}"/> instance wrapping the shared resource.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="device"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Thrown if the shared resource is not a 2D texture, or if its format does not match <typeparamref name="T"/>.</exception>
/// <remarks>
/// インポートしたリソースを <see cref="ReadWriteTexture2D{T}"/> として書き込み用に使用する場合、元のリソースは UAV 対応かつ
/// 同時アクセス可能 (simultaneous access) として作成されている必要があります。クロス API の同期は共有フェンスで行ってください。
/// To use the imported resource for writing as a <see cref="ReadWriteTexture2D{T}"/>, the original resource must have been
/// created with UAV support and simultaneous access. Use a shared fence for cross API synchronization.
/// </remarks>
public static ReadWriteTexture2D<T> OpenSharedReadWriteTexture2D<T>(GraphicsDevice device, nint handle)
where T : unmanaged
@@ -96,18 +96,18 @@ public static unsafe partial class InteropServices
}
/// <summary>
/// 外部 API が所有する共有 NT ハンドルを正規化 <see cref="ReadWriteTexture2D{T, TPixel}"/> として開きます。
/// Opens a shared NT handle owned by an external API as a normalized <see cref="ReadWriteTexture2D{T, TPixel}"/>.
/// </summary>
/// <typeparam name="T">CPU側で使用するピクセルの型。</typeparam>
/// <typeparam name="TPixel">GPU側で使用する正規化ピクセルの型。</typeparam>
/// <param name="device">リソースを開くために使用する <see cref="GraphicsDevice"/>。</param>
/// <param name="handle">開く対象の共有 NT ハンドル。</param>
/// <returns>共有リソースをラップする正規化 <see cref="ReadWriteTexture2D{T, TPixel}"/> インスタンス。</returns>
/// <exception cref="ArgumentNullException"><paramref name="device"/> が <see langword="null"/> の場合にスローされます。</exception>
/// <exception cref="ArgumentException">共有リソースが2Dテクスチャでない、またはフォーマットが <typeparamref name="T"/> と一致しない場合にスローされます。</exception>
/// <typeparam name="T">The type of pixels used on the CPU side.</typeparam>
/// <typeparam name="TPixel">The type of normalized pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to open the resource.</param>
/// <param name="handle">The shared NT handle to open.</param>
/// <returns>A normalized <see cref="ReadWriteTexture2D{T, TPixel}"/> instance wrapping the shared resource.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="device"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Thrown if the shared resource is not a 2D texture, or if its format does not match <typeparamref name="T"/>.</exception>
/// <remarks>
/// 書き込み用に使用する場合、元のリソースはUAV対応かつ同時アクセス可能として作成されている必要があります。
/// クロス API の同期は共有フェンスで行ってください。
/// To use the resource for writing, the original resource must have been created with UAV support and simultaneous access.
/// Use a shared fence for cross API synchronization.
/// </remarks>
public static ReadWriteTexture2D<T, TPixel> OpenSharedReadWriteTexture2D<T, TPixel>(GraphicsDevice device, nint handle)
where T : unmanaged, IPixel<T, TPixel>
@@ -122,14 +122,14 @@ public static unsafe partial class InteropServices
}
/// <summary>
/// 外部 API が所有する共有 NT ハンドルを <see cref="ReadOnlyTexture2D{T}"/> として開きます。
/// Opens a shared NT handle owned by an external API as a <see cref="ReadOnlyTexture2D{T}"/>.
/// </summary>
/// <typeparam name="T">テクスチャに格納する要素の型。</typeparam>
/// <param name="device">リソースを開くために使用する <see cref="GraphicsDevice"/>。</param>
/// <param name="handle">開く対象の共有 NT ハンドル。</param>
/// <returns>共有リソースをラップする <see cref="ReadOnlyTexture2D{T}"/> インスタンス。</returns>
/// <exception cref="ArgumentNullException"><paramref name="device"/> が <see langword="null"/> の場合にスローされます。</exception>
/// <exception cref="ArgumentException">共有リソースが 2D テクスチャでない、またはフォーマットが <typeparamref name="T"/> と一致しない場合にスローされます。</exception>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to open the resource.</param>
/// <param name="handle">The shared NT handle to open.</param>
/// <returns>A <see cref="ReadOnlyTexture2D{T}"/> instance wrapping the shared resource.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="device"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Thrown if the shared resource is not a 2D texture, or if its format does not match <typeparamref name="T"/>.</exception>
public static ReadOnlyTexture2D<T> OpenSharedReadOnlyTexture2D<T>(GraphicsDevice device, nint handle)
where T : unmanaged
{
@@ -142,15 +142,15 @@ public static unsafe partial class InteropServices
}
/// <summary>
/// 共有可能なテクスチャの基になる <see cref="ID3D12Resource"/> に対する共有 NT ハンドルをエクスポートします。
/// Exports a shared NT handle for the <see cref="ID3D12Resource"/> backing a shareable texture.
/// </summary>
/// <typeparam name="T">テクスチャに格納する要素の型。</typeparam>
/// <param name="texture">共有ハンドルをエクスポートする対象の <see cref="Texture2D{T}"/>。</param>
/// <returns>エクスポートされた共有 NT ハンドル。呼び出し側が <c>CloseHandle</c> で解放する責任を持ちます。</returns>
/// <exception cref="ArgumentNullException"><paramref name="texture"/> が <see langword="null"/> の場合にスローされます。</exception>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The <see cref="Texture2D{T}"/> to export the shared handle for.</param>
/// <returns>The exported shared NT handle. The caller is responsible for releasing it with <c>CloseHandle</c>.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="texture"/> is <see langword="null"/>.</exception>
/// <remarks>
/// このメソッドは、<see cref="AllocateSharedReadWriteTexture2D{T}(GraphicsDevice, int, int)"/> など共有可能として
/// 作成されたテクスチャに対してのみ成功します。共有用に作成されていないテクスチャに対しては失敗します。
/// This method only succeeds for textures created as shareable, such as with
/// <see cref="AllocateSharedReadWriteTexture2D{T}(GraphicsDevice, int, int)"/>. It fails for textures not created for sharing.
/// </remarks>
public static nint CreateSharedHandle<T>(Texture2D<T> texture)
where T : unmanaged
@@ -165,18 +165,18 @@ public static unsafe partial class InteropServices
}
/// <summary>
/// クロス API 同期に使用する共有フェンスを生成し、要求されたインターフェイスとして取得します。
/// Creates a shared fence to use for cross API synchronization, and retrieves it as the requested interface.
/// </summary>
/// <param name="device">フェンスの生成に使用する <see cref="GraphicsDevice"/>。</param>
/// <param name="riid">取得するフェンス インターフェイスの識別子 (IID) への参照。</param>
/// <param name="ppvFence"><paramref name="riid"/> で指定したインターフェイスへのポインターのアドレス。</param>
/// <param name="sharedHandle">生成されたフェンスの共有 NT ハンドルを書き込む先のアドレス。呼び出し側が <c>CloseHandle</c> で解放する責任を持ちます。</param>
/// <exception cref="ArgumentNullException"><paramref name="device"/> が <see langword="null"/> の場合にスローされます。</exception>
/// <exception cref="System.ComponentModel.Win32Exception">フェンスの生成、ハンドルのエクスポート、またはインターフェイスの取得に失敗した場合にスローされます。</exception>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to create the fence.</param>
/// <param name="riid">A reference to the identifier (IID) of the fence interface to retrieve.</param>
/// <param name="ppvFence">The address of a pointer to the interface specified by <paramref name="riid"/>.</param>
/// <param name="sharedHandle">The address to write the shared NT handle of the created fence to. The caller is responsible for releasing it with <c>CloseHandle</c>.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="device"/> is <see langword="null"/>.</exception>
/// <exception cref="System.ComponentModel.Win32Exception">Thrown if creating the fence, exporting the handle, or retrieving the interface fails.</exception>
/// <remarks>
/// エクスポートされた <paramref name="sharedHandle"/> は、D3D11 側で <c>ID3D11Device5::OpenSharedFence</c> により開けます。
/// 取得した <paramref name="ppvFence"/> は <see cref="SignalSharedFence(GraphicsDevice, void*, ulong)"/> および
/// <see cref="WaitForSharedFence(GraphicsDevice, void*, ulong)"/> に渡して、コンピュート キュー上での同期に使用できます。
/// The exported <paramref name="sharedHandle"/> can be opened on the D3D11 side via <c>ID3D11Device5::OpenSharedFence</c>.
/// The retrieved <paramref name="ppvFence"/> can be passed to <see cref="SignalSharedFence(GraphicsDevice, void*, ulong)"/> and
/// <see cref="WaitForSharedFence(GraphicsDevice, void*, ulong)"/> to synchronize on the compute queue.
/// </remarks>
public static void CreateSharedFence(GraphicsDevice device, Guid* riid, void** ppvFence, nint* sharedHandle)
{
@@ -193,14 +193,14 @@ public static unsafe partial class InteropServices
}
/// <summary>
/// 共有 NT ハンドルから他 API が生成した共有フェンスを開き、要求されたインターフェイスとして取得します。
/// Opens a shared fence created by another API from a shared NT handle, and retrieves it as the requested interface.
/// </summary>
/// <param name="device">フェンスを開くために使用する <see cref="GraphicsDevice"/>。</param>
/// <param name="handle">開く対象の共有 NT ハンドル。</param>
/// <param name="riid">取得するフェンス インターフェイスの識別子 (IID) への参照。</param>
/// <param name="ppvFence"><paramref name="riid"/> で指定したインターフェイスへのポインターのアドレス。</param>
/// <exception cref="ArgumentNullException"><paramref name="device"/> が <see langword="null"/> の場合にスローされます。</exception>
/// <exception cref="System.ComponentModel.Win32Exception">フェンスを開く、またはインターフェイスの取得に失敗した場合にスローされます。</exception>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to open the fence.</param>
/// <param name="handle">The shared NT handle to open.</param>
/// <param name="riid">A reference to the identifier (IID) of the fence interface to retrieve.</param>
/// <param name="ppvFence">The address of a pointer to the interface specified by <paramref name="riid"/>.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="device"/> is <see langword="null"/>.</exception>
/// <exception cref="System.ComponentModel.Win32Exception">Thrown if opening the fence or retrieving the interface fails.</exception>
public static void OpenSharedFence(GraphicsDevice device, nint handle, Guid* riid, void** ppvFence)
{
default(ArgumentNullException).ThrowIfNull(device);
@@ -212,13 +212,13 @@ public static unsafe partial class InteropServices
}
/// <summary>
/// コンピュート キュー上で指定した共有フェンスに値をシグナルします。
/// Signals the specified shared fence with a value on the compute queue.
/// </summary>
/// <param name="device">操作を発行する <see cref="GraphicsDevice"/>。</param>
/// <param name="d3D12Fence">対象の <c>ID3D12Fence</c> COM オブジェクトへのポインター。</param>
/// <param name="value">シグナルする値。</param>
/// <exception cref="ArgumentNullException"><paramref name="device"/> が <see langword="null"/> の場合にスローされます。</exception>
/// <exception cref="System.ComponentModel.Win32Exception">シグナルの発行に失敗した場合にスローされます。</exception>
/// <param name="device">The <see cref="GraphicsDevice"/> issuing the operation.</param>
/// <param name="d3D12Fence">A pointer to the target <c>ID3D12Fence</c> COM object.</param>
/// <param name="value">The value to signal.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="device"/> is <see langword="null"/>.</exception>
/// <exception cref="System.ComponentModel.Win32Exception">Thrown if issuing the signal fails.</exception>
public static void SignalSharedFence(GraphicsDevice device, void* d3D12Fence, ulong value)
{
default(ArgumentNullException).ThrowIfNull(device);
@@ -229,13 +229,13 @@ public static unsafe partial class InteropServices
}
/// <summary>
/// コンピュート キュー上で指定した共有フェンスが目標値に達するまで待機するよう登録します。
/// Enqueues a wait on the compute queue until the specified shared fence reaches the target value.
/// </summary>
/// <param name="device">操作を発行する <see cref="GraphicsDevice"/>。</param>
/// <param name="d3D12Fence">対象の <c>ID3D12Fence</c> COM オブジェクトへのポインター。</param>
/// <param name="value">待機する目標値。</param>
/// <exception cref="ArgumentNullException"><paramref name="device"/> が <see langword="null"/> の場合にスローされます。</exception>
/// <exception cref="System.ComponentModel.Win32Exception">待機の登録に失敗した場合にスローされます。</exception>
/// <param name="device">The <see cref="GraphicsDevice"/> issuing the operation.</param>
/// <param name="d3D12Fence">A pointer to the target <c>ID3D12Fence</c> COM object.</param>
/// <param name="value">The target value to wait for.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="device"/> is <see langword="null"/>.</exception>
/// <exception cref="System.ComponentModel.Win32Exception">Thrown if enqueueing the wait fails.</exception>
public static void WaitForSharedFence(GraphicsDevice device, void* d3D12Fence, ulong value)
{
default(ArgumentNullException).ThrowIfNull(device);
+2
ファイルの表示
@@ -279,6 +279,8 @@ internal struct ReferenceTracker : IDisposable
this.trackedObject = trackedObject;
}
internal readonly IReferenceTrackedObject? TrackedObject => this.trackedObject;
/// <inheritdoc/>
public void Dispose()
{
+11 -1
ファイルの表示
@@ -22,7 +22,7 @@ namespace ComputeSharp.Resources;
/// A <see langword="class"/> representing a typed buffer stored on GPU memory.
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
public abstract unsafe partial class Buffer<T> : IReferenceTrackedObject, IGraphicsResource, ID3D12ReadOnlyResource
public abstract unsafe partial class Buffer<T> : IReferenceTrackedObject, IGraphicsResource, ID3D12ReadOnlyResource, ID3D12ComputeFenceTrackedResource
where T : unmanaged
{
/// <summary>
@@ -55,6 +55,8 @@ public abstract unsafe partial class Buffer<T> : IReferenceTrackedObject, IGraph
/// </summary>
protected readonly nint SizeInBytes;
private D3D12ComputeFenceTracker d3D12ComputeFenceTracker;
/// <summary>
/// Creates a new <see cref="Buffer{T}"/> instance with the specified parameters.
/// </summary>
@@ -154,6 +156,14 @@ public abstract unsafe partial class Buffer<T> : IReferenceTrackedObject, IGraph
/// </summary>
internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
internal ulong D3D12ComputeFenceValue => this.d3D12ComputeFenceTracker.Value;
/// <inheritdoc/>
void ID3D12ComputeFenceTrackedResource.MarkComputeFence(ulong d3D12FenceValue)
{
this.d3D12ComputeFenceTracker.Mark(d3D12FenceValue);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Buffer{T}"/> instance and writes them into a target memory area.
/// </summary>
+19
ファイルの表示
@@ -46,6 +46,8 @@ public abstract class StructuredBuffer<T> : Buffer<T>
if (GraphicsDevice.IsCacheCoherentUMA)
{
GraphicsDevice.WaitForComputeFence(D3D12ComputeFenceValue);
using ID3D12ResourceMap resource = D3D12Resource->Map();
fixed (void* destinationPointer = &destination)
@@ -77,6 +79,8 @@ public abstract class StructuredBuffer<T> : Buffer<T>
using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY))
{
copyCommandList.AddComputeFenceWait(D3D12ComputeFenceValue);
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteLength);
copyCommandList.ExecuteAndWaitForCompletion();
}
@@ -124,6 +128,9 @@ public abstract class StructuredBuffer<T> : Buffer<T>
// Directly copy the input buffer
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
copyCommandList.AddComputeFenceWait(D3D12ComputeFenceValue);
copyCommandList.AddComputeFenceWait(destination.D3D12ComputeFenceValue);
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(
destination.D3D12Resource,
(ulong)destinationByteOffset,
@@ -165,6 +172,8 @@ public abstract class StructuredBuffer<T> : Buffer<T>
if (GraphicsDevice.IsCacheCoherentUMA)
{
GraphicsDevice.WaitForComputeFence(D3D12ComputeFenceValue);
using ID3D12ResourceMap resource = D3D12Resource->Map();
MemoryHelper.Copy<T>(
@@ -184,6 +193,8 @@ public abstract class StructuredBuffer<T> : Buffer<T>
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
copyCommandList.AddComputeFenceWait(D3D12ComputeFenceValue);
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(destination.D3D12Resource, byteDestinationOffset, D3D12Resource, byteOffset, byteLength);
copyCommandList.ExecuteAndWaitForCompletion();
}
@@ -203,6 +214,8 @@ public abstract class StructuredBuffer<T> : Buffer<T>
if (GraphicsDevice.IsCacheCoherentUMA)
{
GraphicsDevice.WaitForComputeFence(D3D12ComputeFenceValue);
using ID3D12ResourceMap resource = D3D12Resource->Map();
fixed (void* sourcePointer = &source)
@@ -249,6 +262,8 @@ public abstract class StructuredBuffer<T> : Buffer<T>
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
copyCommandList.AddComputeFenceWait(D3D12ComputeFenceValue);
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, (ulong)byteOffset, d3D12Resource.Get(), 0, (ulong)byteLength);
copyCommandList.ExecuteAndWaitForCompletion();
}
@@ -280,6 +295,8 @@ public abstract class StructuredBuffer<T> : Buffer<T>
if (GraphicsDevice.IsCacheCoherentUMA)
{
GraphicsDevice.WaitForComputeFence(D3D12ComputeFenceValue);
using ID3D12ResourceMap resource = D3D12Resource->Map();
MemoryHelper.Copy<T>(
@@ -299,6 +316,8 @@ public abstract class StructuredBuffer<T> : Buffer<T>
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
copyCommandList.AddComputeFenceWait(D3D12ComputeFenceValue);
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, byteOffset, source.D3D12Resource, byteSourceOffset, byteLength);
copyCommandList.ExecuteAndWaitForCompletion();
}
+22 -1
ファイルの表示
@@ -24,7 +24,7 @@ namespace ComputeSharp.Resources;
/// A <see langword="class"/> representing a typed 1D texture stored on GPU memory.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
public abstract unsafe partial class Texture1D<T> : IReferenceTrackedObject, IGraphicsResource, ID3D12ReadWriteResource
public abstract unsafe partial class Texture1D<T> : IReferenceTrackedObject, IGraphicsResource, ID3D12ReadWriteResource, ID3D12ComputeFenceTrackedResource
where T : unmanaged
{
/// <summary>
@@ -62,6 +62,8 @@ public abstract unsafe partial class Texture1D<T> : IReferenceTrackedObject, IGr
/// </summary>
private D3D12_RESOURCE_STATES d3D12ResourceState;
private D3D12ComputeFenceTracker d3D12ComputeFenceTracker;
/// <summary>
/// Creates a new <see cref="Texture1D{T}"/> instance with the specified parameters.
/// </summary>
@@ -141,6 +143,14 @@ public abstract unsafe partial class Texture1D<T> : IReferenceTrackedObject, IGr
/// </summary>
internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
internal ulong D3D12ComputeFenceValue => this.d3D12ComputeFenceTracker.Value;
/// <inheritdoc/>
void ID3D12ComputeFenceTrackedResource.MarkComputeFence(ulong d3D12FenceValue)
{
this.d3D12ComputeFenceTracker.Mark(d3D12FenceValue);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture1D{T}"/> instance and writes them into a target memory area.
/// </summary>
@@ -179,6 +189,8 @@ public abstract unsafe partial class Texture1D<T> : IReferenceTrackedObject, IGr
using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType))
{
copyCommandList.AddComputeFenceWait(D3D12ComputeFenceValue);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
@@ -250,6 +262,9 @@ public abstract unsafe partial class Texture1D<T> : IReferenceTrackedObject, IGr
using CommandList copyCommandList = new(GraphicsDevice, d3D12CommandListType);
copyCommandList.AddComputeFenceWait(D3D12ComputeFenceValue);
copyCommandList.AddComputeFenceWait(destination.D3D12ComputeFenceValue);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
@@ -304,6 +319,8 @@ public abstract unsafe partial class Texture1D<T> : IReferenceTrackedObject, IGr
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
copyCommandList.AddComputeFenceWait(D3D12ComputeFenceValue);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
@@ -384,6 +401,8 @@ public abstract unsafe partial class Texture1D<T> : IReferenceTrackedObject, IGr
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
copyCommandList.AddComputeFenceWait(D3D12ComputeFenceValue);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
@@ -437,6 +456,8 @@ public abstract unsafe partial class Texture1D<T> : IReferenceTrackedObject, IGr
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
copyCommandList.AddComputeFenceWait(D3D12ComputeFenceValue);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
+34 -13
ファイルの表示
@@ -25,7 +25,7 @@ namespace ComputeSharp.Resources;
/// A <see langword="class"/> representing a typed 2D texture stored on GPU memory.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
public abstract unsafe partial class Texture2D<T> : IReferenceTrackedObject, IGraphicsResource, ID3D12ReadWriteResource
public abstract unsafe partial class Texture2D<T> : IReferenceTrackedObject, IGraphicsResource, ID3D12ReadWriteResource, ID3D12ComputeFenceTrackedResource
where T : unmanaged
{
/// <summary>
@@ -63,6 +63,8 @@ public abstract unsafe partial class Texture2D<T> : IReferenceTrackedObject, IGr
/// </summary>
private D3D12_RESOURCE_STATES d3D12ResourceState;
private D3D12ComputeFenceTracker d3D12ComputeFenceTracker;
/// <summary>
/// Creates a new <see cref="Texture2D{T}"/> instance with the specified parameters.
/// </summary>
@@ -129,14 +131,14 @@ public abstract unsafe partial class Texture2D<T> : IReferenceTrackedObject, IGr
}
/// <summary>
/// クロス API 共有が可能な <see cref="Texture2D{T}"/> インスタンスを生成します。
/// Creates a new <see cref="Texture2D{T}"/> instance that can be shared across APIs.
/// </summary>
/// <param name="device">現在のインスタンスに関連付ける <see cref="ComputeSharp.GraphicsDevice"/>。</param>
/// <param name="width">テクスチャの幅。</param>
/// <param name="height">テクスチャの高さ。</param>
/// <param name="resourceType">現在のテクスチャのリソース種別。</param>
/// <param name="d3D12FormatSupport">現在のテクスチャ型のフォーマット サポート。</param>
/// <param name="isRenderTarget">レンダーターゲットとして使用可能にするかどうか。</param>
/// <param name="device">The <see cref="ComputeSharp.GraphicsDevice"/> associated with the current instance.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="resourceType">The resource type for the current texture.</param>
/// <param name="d3D12FormatSupport">The format support for the current texture type.</param>
/// <param name="isRenderTarget">Whether the texture can be used as a render target.</param>
private protected Texture2D(GraphicsDevice device, int width, int height, ResourceType resourceType, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport, bool isRenderTarget)
{
using ReferenceTracker.Lease _0 = ReferenceTracker.Create(this, out this.referenceTracker);
@@ -193,12 +195,12 @@ public abstract unsafe partial class Texture2D<T> : IReferenceTrackedObject, IGr
}
/// <summary>
/// 外部 API が所有する共有リソースをラップする <see cref="Texture2D{T}"/> インスタンスを生成します。
/// Creates a new <see cref="Texture2D{T}"/> instance wrapping a shared resource owned by an external API.
/// </summary>
/// <param name="device">現在のインスタンスに関連付ける <see cref="ComputeSharp.GraphicsDevice"/>。</param>
/// <param name="d3D12Resource">ラップ対象の、共有ハンドルから開かれた <see cref="ID3D12Resource"/>。</param>
/// <param name="resourceType">現在のテクスチャのリソース種別。</param>
/// <param name="d3D12FormatSupport">現在のテクスチャ型のフォーマット サポート。</param>
/// <param name="device">The <see cref="ComputeSharp.GraphicsDevice"/> associated with the current instance.</param>
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> opened from a shared handle to wrap.</param>
/// <param name="resourceType">The resource type for the current texture.</param>
/// <param name="d3D12FormatSupport">The format support for the current texture type.</param>
private protected Texture2D(GraphicsDevice device, ID3D12Resource* d3D12Resource, ResourceType resourceType, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport)
{
using ReferenceTracker.Lease _0 = ReferenceTracker.Create(this, out this.referenceTracker);
@@ -288,6 +290,14 @@ public abstract unsafe partial class Texture2D<T> : IReferenceTrackedObject, IGr
/// </summary>
internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
internal ulong D3D12ComputeFenceValue => this.d3D12ComputeFenceTracker.Value;
/// <inheritdoc/>
void ID3D12ComputeFenceTrackedResource.MarkComputeFence(ulong d3D12FenceValue)
{
this.d3D12ComputeFenceTracker.Mark(d3D12FenceValue);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target memory area.
/// </summary>
@@ -332,6 +342,8 @@ public abstract unsafe partial class Texture2D<T> : IReferenceTrackedObject, IGr
using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType))
{
copyCommandList.AddComputeFenceWait(D3D12ComputeFenceValue);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
@@ -413,6 +425,9 @@ public abstract unsafe partial class Texture2D<T> : IReferenceTrackedObject, IGr
using CommandList copyCommandList = new(GraphicsDevice, d3D12CommandListType);
copyCommandList.AddComputeFenceWait(D3D12ComputeFenceValue);
copyCommandList.AddComputeFenceWait(destination.D3D12ComputeFenceValue);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
@@ -476,6 +491,8 @@ public abstract unsafe partial class Texture2D<T> : IReferenceTrackedObject, IGr
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
copyCommandList.AddComputeFenceWait(D3D12ComputeFenceValue);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
@@ -563,6 +580,8 @@ public abstract unsafe partial class Texture2D<T> : IReferenceTrackedObject, IGr
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
copyCommandList.AddComputeFenceWait(D3D12ComputeFenceValue);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
@@ -625,6 +644,8 @@ public abstract unsafe partial class Texture2D<T> : IReferenceTrackedObject, IGr
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
copyCommandList.AddComputeFenceWait(D3D12ComputeFenceValue);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
+22 -1
ファイルの表示
@@ -24,7 +24,7 @@ namespace ComputeSharp.Resources;
/// A <see langword="class"/> representing a typed 3D texture stored on GPU memory.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
public abstract unsafe partial class Texture3D<T> : IReferenceTrackedObject, IGraphicsResource, ID3D12ReadWriteResource
public abstract unsafe partial class Texture3D<T> : IReferenceTrackedObject, IGraphicsResource, ID3D12ReadWriteResource, ID3D12ComputeFenceTrackedResource
where T : unmanaged
{
/// <summary>
@@ -62,6 +62,8 @@ public abstract unsafe partial class Texture3D<T> : IReferenceTrackedObject, IGr
/// </summary>
private D3D12_RESOURCE_STATES d3D12ResourceState;
private D3D12ComputeFenceTracker d3D12ComputeFenceTracker;
/// <summary>
/// Creates a new <see cref="Texture3D{T}"/> instance with the specified parameters.
/// </summary>
@@ -159,6 +161,14 @@ public abstract unsafe partial class Texture3D<T> : IReferenceTrackedObject, IGr
/// </summary>
internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
internal ulong D3D12ComputeFenceValue => this.d3D12ComputeFenceTracker.Value;
/// <inheritdoc/>
void ID3D12ComputeFenceTrackedResource.MarkComputeFence(ulong d3D12FenceValue)
{
this.d3D12ComputeFenceTracker.Mark(d3D12FenceValue);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture3D{T}"/> instance and writes them into a target memory area.
/// </summary>
@@ -209,6 +219,8 @@ public abstract unsafe partial class Texture3D<T> : IReferenceTrackedObject, IGr
using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType))
{
copyCommandList.AddComputeFenceWait(D3D12ComputeFenceValue);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
@@ -301,6 +313,9 @@ public abstract unsafe partial class Texture3D<T> : IReferenceTrackedObject, IGr
using CommandList copyCommandList = new(GraphicsDevice, d3D12CommandListType);
copyCommandList.AddComputeFenceWait(D3D12ComputeFenceValue);
copyCommandList.AddComputeFenceWait(destination.D3D12ComputeFenceValue);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
@@ -373,6 +388,8 @@ public abstract unsafe partial class Texture3D<T> : IReferenceTrackedObject, IGr
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
copyCommandList.AddComputeFenceWait(D3D12ComputeFenceValue);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
@@ -468,6 +485,8 @@ public abstract unsafe partial class Texture3D<T> : IReferenceTrackedObject, IGr
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
copyCommandList.AddComputeFenceWait(D3D12ComputeFenceValue);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
@@ -539,6 +558,8 @@ public abstract unsafe partial class Texture3D<T> : IReferenceTrackedObject, IGr
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
copyCommandList.AddComputeFenceWait(D3D12ComputeFenceValue);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
@@ -0,0 +1,6 @@
namespace ComputeSharp.Resources.Interop;
internal interface ID3D12ComputeFenceTrackedResource
{
void MarkComputeFence(ulong d3D12FenceValue);
}
+20
ファイルの表示
@@ -0,0 +1,20 @@
using System.Threading;
namespace ComputeSharp.Resources.Interop;
internal struct D3D12ComputeFenceTracker
{
private ulong d3D12FenceValue;
public ulong Value => Volatile.Read(ref this.d3D12FenceValue);
public void Mark(ulong d3D12FenceValue)
{
ulong currentValue;
while ((currentValue = Volatile.Read(ref this.d3D12FenceValue)) < d3D12FenceValue &&
Interlocked.CompareExchange(ref this.d3D12FenceValue, d3D12FenceValue, currentValue) != currentValue)
{
}
}
}
+7 -7
ファイルの表示
@@ -29,21 +29,21 @@ public sealed class ReadOnlyTexture2D<T> : Texture2D<T>
}
/// <summary>
/// クロス API 共有が可能な <see cref="ReadOnlyTexture2D{T}"/> インスタンスを生成します。
/// Creates a new <see cref="ReadOnlyTexture2D{T}"/> instance that can be shared across APIs.
/// </summary>
/// <param name="device">現在のインスタンスに関連付ける <see cref="GraphicsDevice"/>。</param>
/// <param name="width">テクスチャの幅。</param>
/// <param name="height">テクスチャの高さ。</param>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
internal ReadOnlyTexture2D(GraphicsDevice device, int width, int height)
: base(device, width, height, ResourceType.ReadOnly, D3D12_FORMAT_SUPPORT1_TEXTURE2D, false)
{
}
/// <summary>
/// 外部 API が所有する共有リソースをラップする <see cref="ReadOnlyTexture2D{T}"/> インスタンスを生成します。
/// Creates a new <see cref="ReadOnlyTexture2D{T}"/> instance wrapping a shared resource owned by an external API.
/// </summary>
/// <param name="device">現在のインスタンスに関連付ける <see cref="GraphicsDevice"/>。</param>
/// <param name="d3D12Resource">ラップ対象の、共有ハンドルから開かれた <see cref="ID3D12Resource"/>。</param>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> opened from a shared handle to wrap.</param>
internal unsafe ReadOnlyTexture2D(GraphicsDevice device, ID3D12Resource* d3D12Resource)
: base(device, d3D12Resource, ResourceType.ReadOnly, D3D12_FORMAT_SUPPORT1_TEXTURE2D)
{
+7 -1
ファイルの表示
@@ -56,7 +56,7 @@ partial class ReadWriteTexture1D<T, TPixel>
/// <summary>
/// A wrapper for a <see cref="ReadWriteTexture1D{T, TPixel}"/> resource that has been temporarily transitioned to readonly.
/// </summary>
private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyNormalizedTexture1D<TPixel>, ID3D12ReadOnlyResource
private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyNormalizedTexture1D<TPixel>, ID3D12ReadOnlyResource, ID3D12ComputeFenceTrackedResource
{
/// <summary>
/// The owning <see cref="ReadWriteTexture1D{T, TPixel}"/> instance being wrapped.
@@ -120,6 +120,12 @@ partial class ReadWriteTexture1D<T, TPixel>
return this.owner.D3D12Resource;
}
/// <inheritdoc/>
void ID3D12ComputeFenceTrackedResource.MarkComputeFence(ulong d3D12FenceValue)
{
((ID3D12ComputeFenceTrackedResource)this.owner).MarkComputeFence(d3D12FenceValue);
}
/// <inheritdoc/>
protected override void DangerousOnDispose()
{
+7 -1
ファイルの表示
@@ -56,7 +56,7 @@ partial class ReadWriteTexture1D<T>
/// <summary>
/// A wrapper for a <see cref="ReadWriteTexture1D{T}"/> resource that has been temporarily transitioned to readonly.
/// </summary>
private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyTexture1D<T>, ID3D12ReadOnlyResource
private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyTexture1D<T>, ID3D12ReadOnlyResource, ID3D12ComputeFenceTrackedResource
{
/// <summary>
/// The owning <see cref="ReadWriteTexture1D{T}"/> instance being wrapped.
@@ -120,6 +120,12 @@ partial class ReadWriteTexture1D<T>
return this.owner.D3D12Resource;
}
/// <inheritdoc/>
void ID3D12ComputeFenceTrackedResource.MarkComputeFence(ulong d3D12FenceValue)
{
((ID3D12ComputeFenceTrackedResource)this.owner).MarkComputeFence(d3D12FenceValue);
}
/// <inheritdoc/>
protected override void DangerousOnDispose()
{
+7 -1
ファイルの表示
@@ -56,7 +56,7 @@ partial class ReadWriteTexture2D<T, TPixel>
/// <summary>
/// A wrapper for a <see cref="ReadWriteTexture2D{T, TPixel}"/> resource that has been temporarily transitioned to readonly.
/// </summary>
private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyNormalizedTexture2D<TPixel>, ID3D12ReadOnlyResource
private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyNormalizedTexture2D<TPixel>, ID3D12ReadOnlyResource, ID3D12ComputeFenceTrackedResource
{
/// <summary>
/// The owning <see cref="ReadWriteTexture2D{T, TPixel}"/> instance being wrapped.
@@ -132,6 +132,12 @@ partial class ReadWriteTexture2D<T, TPixel>
return this.owner.D3D12Resource;
}
/// <inheritdoc/>
void ID3D12ComputeFenceTrackedResource.MarkComputeFence(ulong d3D12FenceValue)
{
((ID3D12ComputeFenceTrackedResource)this.owner).MarkComputeFence(d3D12FenceValue);
}
/// <inheritdoc/>
protected override void DangerousOnDispose()
{
+7 -1
ファイルの表示
@@ -59,7 +59,7 @@ partial class ReadWriteTexture2D<T>
/// <summary>
/// A wrapper for a <see cref="ReadWriteTexture2D{T}"/> resource that has been temporarily transitioned to readonly.
/// </summary>
private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyTexture2D<T>, ID3D12ReadOnlyResource
private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyTexture2D<T>, ID3D12ReadOnlyResource, ID3D12ComputeFenceTrackedResource
{
/// <summary>
/// The owning <see cref="ReadWriteTexture2D{T}"/> instance being wrapped.
@@ -132,6 +132,12 @@ partial class ReadWriteTexture2D<T>
return this.owner.D3D12Resource;
}
/// <inheritdoc/>
void ID3D12ComputeFenceTrackedResource.MarkComputeFence(ulong d3D12FenceValue)
{
((ID3D12ComputeFenceTrackedResource)this.owner).MarkComputeFence(d3D12FenceValue);
}
/// <inheritdoc/>
protected override void DangerousOnDispose()
{
+7 -7
ファイルの表示
@@ -29,21 +29,21 @@ public sealed partial class ReadWriteTexture2D<T> : Texture2D<T>
}
/// <summary>
/// クロス API 共有が可能な <see cref="ReadWriteTexture2D{T}"/> インスタンスを生成します。
/// Creates a new <see cref="ReadWriteTexture2D{T}"/> instance that can be shared across APIs.
/// </summary>
/// <param name="device">現在のインスタンスに関連付ける <see cref="GraphicsDevice"/>。</param>
/// <param name="width">テクスチャの幅。</param>
/// <param name="height">テクスチャの高さ。</param>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
internal ReadWriteTexture2D(GraphicsDevice device, int width, int height)
: base(device, width, height, ResourceType.ReadWrite, D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW, false)
{
}
/// <summary>
/// 外部 API が所有する共有リソースをラップする <see cref="ReadWriteTexture2D{T}"/> インスタンスを生成します。
/// Creates a new <see cref="ReadWriteTexture2D{T}"/> instance wrapping a shared resource owned by an external API.
/// </summary>
/// <param name="device">現在のインスタンスに関連付ける <see cref="GraphicsDevice"/>。</param>
/// <param name="d3D12Resource">ラップ対象の、共有ハンドルから開かれた <see cref="ID3D12Resource"/>。</param>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> opened from a shared handle to wrap.</param>
internal unsafe ReadWriteTexture2D(GraphicsDevice device, ID3D12Resource* d3D12Resource)
: base(device, d3D12Resource, ResourceType.ReadWrite, D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW)
{
+7 -1
ファイルの表示
@@ -56,7 +56,7 @@ partial class ReadWriteTexture3D<T, TPixel>
/// <summary>
/// A wrapper for a <see cref="ReadWriteTexture3D{T, TPixel}"/> resource that has been temporarily transitioned to readonly.
/// </summary>
private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyNormalizedTexture3D<TPixel>, ID3D12ReadOnlyResource
private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyNormalizedTexture3D<TPixel>, ID3D12ReadOnlyResource, ID3D12ComputeFenceTrackedResource
{
/// <summary>
/// The owning <see cref="ReadWriteTexture3D{T, TPixel}"/> instance being wrapped.
@@ -132,6 +132,12 @@ partial class ReadWriteTexture3D<T, TPixel>
return this.owner.D3D12Resource;
}
/// <inheritdoc/>
void ID3D12ComputeFenceTrackedResource.MarkComputeFence(ulong d3D12FenceValue)
{
((ID3D12ComputeFenceTrackedResource)this.owner).MarkComputeFence(d3D12FenceValue);
}
/// <inheritdoc/>
protected override void DangerousOnDispose()
{
+7 -1
ファイルの表示
@@ -56,7 +56,7 @@ partial class ReadWriteTexture3D<T>
/// <summary>
/// A wrapper for a <see cref="ReadWriteTexture3D{T}"/> resource that has been temporarily transitioned to readonly.
/// </summary>
private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyTexture3D<T>, ID3D12ReadOnlyResource
private sealed unsafe class ReadOnly : ReferenceTrackedObject, IReadOnlyTexture3D<T>, ID3D12ReadOnlyResource, ID3D12ComputeFenceTrackedResource
{
/// <summary>
/// The owning <see cref="ReadWriteTexture3D{T}"/> instance being wrapped.
@@ -132,6 +132,12 @@ partial class ReadWriteTexture3D<T>
return this.owner.D3D12Resource;
}
/// <inheritdoc/>
void ID3D12ComputeFenceTrackedResource.MarkComputeFence(ulong d3D12FenceValue)
{
((ID3D12ComputeFenceTrackedResource)this.owner).MarkComputeFence(d3D12FenceValue);
}
/// <inheritdoc/>
protected override void DangerousOnDispose()
{
+123 -35
ファイルの表示
@@ -5,6 +5,7 @@ using System.Threading.Tasks;
using ComputeSharp.Descriptors;
using ComputeSharp.Graphics.Commands;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Interop;
using ComputeSharp.Resources.Interop;
using ComputeSharp.Shaders.Dispatching;
using ComputeSharp.Shaders.Loading;
@@ -29,14 +30,16 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
/// <summary>
/// The <see cref="GraphicsDevice"/> instance owning the current context.
/// </summary>
private GraphicsDevice? device;
private readonly GraphicsDevice? device;
/// <summary>
/// The current <see cref="CommandList"/> instance used to dispatch shaders.
/// </summary>
private CommandList commandList;
private bool isSubmitted;
private GraphicsResourceLeaseSet? resourceLeases;
private ContextState state;
/// <summary>
/// Creates a new <see cref="ComputeContext"/> instance with the specified parameters.
@@ -46,7 +49,8 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
{
this.device = device;
this.commandList = default;
this.isSubmitted = false;
this.resourceLeases = null;
this.state = ContextState.Recording;
// Increment the reference count for the device. This has to be released when disposing the context.
// Not disposing the context is undefined behavior, so we can rely on that to release the reference.
@@ -60,9 +64,9 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
{
get
{
default(InvalidOperationException).ThrowIf(this.device is null);
default(InvalidOperationException).ThrowIf(this.state is not ContextState.Recording);
return this.device;
return this.device!;
}
}
@@ -72,7 +76,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> to insert the barrier for.</param>
internal readonly unsafe void Barrier(ID3D12Resource* d3D12Resource)
{
default(InvalidOperationException).ThrowIf(this.device is null);
default(InvalidOperationException).ThrowIf(this.state is not ContextState.Recording);
ref CommandList commandList = ref GetCommandList();
@@ -92,7 +96,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle,
bool isNormalized)
{
default(InvalidOperationException).ThrowIf(this.device is null);
default(InvalidOperationException).ThrowIf(this.state is not ContextState.Recording);
ref CommandList commandList = ref GetCommandList(pipelineState: null);
@@ -112,7 +116,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle,
Float4 value)
{
default(InvalidOperationException).ThrowIf(this.device is null);
default(InvalidOperationException).ThrowIf(this.state is not ContextState.Recording);
ref CommandList commandList = ref GetCommandList(pipelineState: null);
@@ -153,7 +157,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
internal readonly unsafe void Run<T>(int x, int y, int z, in T shader)
where T : struct, IComputeShader, IComputeShaderDescriptor<T>
{
default(InvalidOperationException).ThrowIf(this.device is null);
default(InvalidOperationException).ThrowIf(this.state is not ContextState.Recording);
default(ArgumentOutOfRangeException).ThrowIfNegativeOrZero(x);
default(ArgumentOutOfRangeException).ThrowIfNegativeOrZero(y);
default(ArgumentOutOfRangeException).ThrowIfNegativeOrZero(z);
@@ -166,7 +170,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsY, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsZ, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
PipelineData pipelineData = PipelineDataLoader<T>.GetPipelineData(this.device);
PipelineData pipelineData = PipelineDataLoader<T>.GetPipelineData(this.device!);
ref CommandList commandList = ref GetCommandList(pipelineData.D3D12PipelineState);
@@ -176,7 +180,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
T.LoadConstantBuffer(in shader, ref dataLoader, x, y, z);
D3D12GraphicsCommandListGraphicsResourceLoader graphicsResourceLoader = new(commandList.D3D12GraphicsCommandList, this.device, rootParameterOffset: 1);
D3D12GraphicsCommandListGraphicsResourceLoader graphicsResourceLoader = new(commandList.D3D12GraphicsCommandList, this.device!, rootParameterOffset: 1, this.resourceLeases!);
T.LoadGraphicsResources(in shader, ref graphicsResourceLoader);
@@ -194,7 +198,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
where T : struct, IComputeShader<TPixel>, IComputeShaderDescriptor<T>
where TPixel : unmanaged
{
default(InvalidOperationException).ThrowIf(this.device is null);
default(InvalidOperationException).ThrowIf(this.state is not ContextState.Recording);
int x = texture.Width;
int y = texture.Height;
@@ -204,7 +208,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsX, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION);
default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsY, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
PipelineData pipelineData = PipelineDataLoader<T>.GetPipelineData(this.device);
PipelineData pipelineData = PipelineDataLoader<T>.GetPipelineData(this.device!);
ref CommandList commandList = ref GetCommandList(pipelineData.D3D12PipelineState);
@@ -214,14 +218,18 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
T.LoadConstantBuffer(in shader, ref constantBufferLoader, x, y, 1);
D3D12GraphicsCommandListGraphicsResourceLoader graphicsResourceLoader = new(commandList.D3D12GraphicsCommandList, this.device, rootParameterOffset: 2);
D3D12GraphicsCommandListGraphicsResourceLoader graphicsResourceLoader = new(commandList.D3D12GraphicsCommandList, this.device!, rootParameterOffset: 2, this.resourceLeases!);
T.LoadGraphicsResources(in shader, ref graphicsResourceLoader);
_ = ((ID3D12ReadOnlyResource)texture).ValidateAndGetID3D12Resource(this.device!, out ReferenceTracker.Lease textureLease);
this.resourceLeases!.Add(textureLease);
// Load the implicit output texture
commandList.D3D12GraphicsCommandList->SetComputeRootDescriptorTable(
1,
((ID3D12ReadOnlyResource)texture).ValidateAndGetGpuDescriptorHandle(this.device));
((ID3D12ReadOnlyResource)texture).ValidateAndGetGpuDescriptorHandle(this.device!));
commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, 1);
}
@@ -237,7 +245,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
D3D12_RESOURCE_STATES d3D12ResourceStatesBefore,
D3D12_RESOURCE_STATES d3D12ResourceStatesAfter)
{
default(InvalidOperationException).ThrowIf(this.device is null);
default(InvalidOperationException).ThrowIf(this.state is not ContextState.Recording);
ref CommandList commandList = ref GetCommandList(pipelineState: null);
@@ -248,19 +256,25 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
if (this.isSubmitted)
if (this.state is ContextState.Submitted)
{
return;
}
default(InvalidOperationException).ThrowIf(this.device is null);
default(InvalidOperationException).ThrowIf(this.state is not ContextState.Recording);
GraphicsDevice device = this.device;
GraphicsDevice device = this.device!;
this.device = null;
this.state = ContextState.Disposed;
GraphicsResourceLeaseSet? resourceLeases = this.resourceLeases;
this.resourceLeases = null;
if (!this.commandList.IsAllocated)
{
resourceLeases?.Release();
device.GetReferenceTracker().DangerousRelease();
return;
@@ -272,6 +286,8 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
}
finally
{
resourceLeases?.Release();
device.GetReferenceTracker().DangerousRelease();
}
}
@@ -280,19 +296,25 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ValueTask DisposeAsync()
{
if (this.isSubmitted)
if (this.state is ContextState.Submitted)
{
return ValueTask.CompletedTask;
}
default(InvalidOperationException).ThrowIf(this.device is null);
default(InvalidOperationException).ThrowIf(this.state is not ContextState.Recording);
GraphicsDevice device = this.device;
GraphicsDevice device = this.device!;
this.device = null;
this.state = ContextState.Disposed;
GraphicsResourceLeaseSet? resourceLeases = this.resourceLeases;
this.resourceLeases = null;
if (!this.commandList.IsAllocated)
{
resourceLeases?.Release();
device.GetReferenceTracker().DangerousRelease();
return ValueTask.CompletedTask;
@@ -300,7 +322,31 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
try
{
return this.commandList.ExecuteAndWaitForCompletionAsync();
ValueTask executeTask = this.commandList.ExecuteAndWaitForCompletionAsync();
if (resourceLeases is null)
{
return executeTask;
}
if (executeTask.IsCompletedSuccessfully)
{
resourceLeases.Release();
return executeTask;
}
ValueTask releaseTask = ReleaseWhenCompletedAsync(executeTask, resourceLeases);
resourceLeases = null;
return releaseTask;
}
catch
{
resourceLeases?.Release();
throw;
}
finally
{
@@ -308,27 +354,44 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
}
}
private static async ValueTask ReleaseWhenCompletedAsync(ValueTask executeTask, GraphicsResourceLeaseSet resourceLeases)
{
try
{
await executeTask.ConfigureAwait(false);
}
finally
{
resourceLeases.Release();
}
}
/// <summary>
/// 現在のコンテキストに記録されたコマンドをGPUへ送信します。
/// Submits the commands recorded in the current context to the GPU.
/// </summary>
/// <remarks>
/// 通常はGPUでの完了を待たずに戻ります。内部の保留上限に達した場合は、最古の送信が完了するまで待機します。
/// コンテキストが参照するすべてのGPUリソースは、共有フェンスなどで送信した処理の完了を確認するまで保持する必要があります。
/// このメソッドを呼び出した後、スコープ終端の<see cref="Dispose()"/>および<see cref="DisposeAsync()"/>は何も行いません。
/// This method normally returns without waiting for the GPU to complete. If the internal pending limit is reached, it waits until the oldest submission has completed.
/// All GPU resources referenced by the context must be kept alive until the submitted work is confirmed to be completed, such as with a shared fence.
/// After calling this method, <see cref="Dispose()"/> and <see cref="DisposeAsync()"/> at the end of the scope do nothing.
/// </remarks>
/// <exception cref="InvalidOperationException">コンテキストが既に破棄または送信されている場合にスローされます。</exception>
/// <exception cref="InvalidOperationException">Thrown if the context has already been disposed or submitted.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Submit()
{
default(InvalidOperationException).ThrowIf(this.device is null);
default(InvalidOperationException).ThrowIf(this.state is not ContextState.Recording);
GraphicsDevice device = this.device;
GraphicsDevice device = this.device!;
this.device = null;
this.isSubmitted = true;
this.state = ContextState.Submitted;
GraphicsResourceLeaseSet? resourceLeases = this.resourceLeases;
this.resourceLeases = null;
if (!this.commandList.IsAllocated)
{
resourceLeases?.Release();
device.GetReferenceTracker().DangerousRelease();
return;
@@ -336,10 +399,12 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
try
{
this.commandList.ExecuteWithoutWaiting();
this.commandList.ExecuteWithoutWaiting(ref resourceLeases);
}
finally
{
resourceLeases?.Release();
device.GetReferenceTracker().DangerousRelease();
}
}
@@ -386,8 +451,31 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
else
{
commandList = new CommandList(this.device!, pipelineState);
ref GraphicsResourceLeaseSet? resourceLeases = ref Unsafe.AsRef(in this.resourceLeases);
resourceLeases ??= GraphicsResourceLeaseSet.Rent();
}
return ref commandList;
}
internal readonly void TrackResourceLease(ref ReferenceTracker.Lease lease)
{
ref GraphicsResourceLeaseSet? resourceLeases = ref Unsafe.AsRef(in this.resourceLeases);
resourceLeases ??= GraphicsResourceLeaseSet.Rent();
resourceLeases.Add(lease);
lease = default;
}
private enum ContextState
{
None,
Recording,
Submitted,
Disposed
}
}
+12 -1
ファイルの表示
@@ -1,5 +1,7 @@
using System;
using System.Runtime.CompilerServices;
using ComputeSharp.Graphics.Commands;
using ComputeSharp.Interop;
using ComputeSharp.Resources.Interop;
using ComputeSharp.Descriptors;
using ComputeSharp.Win32;
@@ -39,21 +41,26 @@ internal readonly unsafe struct D3D12GraphicsCommandListGraphicsResourceLoader :
/// </remarks>
private readonly uint rootParameterOffset;
private readonly GraphicsResourceLeaseSet resourceLeases;
/// <summary>
/// Creates a new <see cref="D3D12GraphicsCommandListGraphicsResourceLoader"/> instance.
/// </summary>
/// <param name="d3D12GraphicsCommandList">The <see cref="ID3D12GraphicsCommandList"/> object to use.</param>
/// <param name="graphicsDevice">The <see cref="GraphicsDevice"/> instance that <paramref name="d3D12GraphicsCommandList"/> is tied to.</param>
/// <param name="rootParameterOffset">The offset into the compute root descriptor table for loaded resources.</param>
/// <param name="resourceLeases">The <see cref="GraphicsResourceLeaseSet"/> instance holding the leases for loaded resources.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal D3D12GraphicsCommandListGraphicsResourceLoader(
ID3D12GraphicsCommandList* d3D12GraphicsCommandList,
GraphicsDevice graphicsDevice,
uint rootParameterOffset)
uint rootParameterOffset,
GraphicsResourceLeaseSet resourceLeases)
{
this.d3D12GraphicsCommandList = d3D12GraphicsCommandList;
this.graphicsDevice = graphicsDevice;
this.rootParameterOffset = rootParameterOffset;
this.resourceLeases = resourceLeases;
}
/// <inheritdoc/>
@@ -68,6 +75,10 @@ internal readonly unsafe struct D3D12GraphicsCommandListGraphicsResourceLoader :
return;
}
_ = readOnlyResource.ValidateAndGetID3D12Resource(this.graphicsDevice, out ReferenceTracker.Lease lease);
this.resourceLeases.Add(lease);
this.d3D12GraphicsCommandList->SetComputeRootDescriptorTable(
RootParameterIndex: this.rootParameterOffset + index,
BaseDescriptor: readOnlyResource.ValidateAndGetGpuDescriptorHandle(this.graphicsDevice));
+62
ファイルの表示
@@ -26,6 +26,8 @@ public static class ComputeContextExtensions
using ReferenceTracker.Lease lease = default;
context.Barrier(buffer.ValidateAndGetID3D12Resource(context.GraphicsDevice, out *&lease));
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -42,6 +44,8 @@ public static class ComputeContextExtensions
using ReferenceTracker.Lease lease = default;
context.Barrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice, out *&lease));
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -58,6 +62,8 @@ public static class ComputeContextExtensions
using ReferenceTracker.Lease lease = default;
context.Barrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice, out *&lease));
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -74,6 +80,8 @@ public static class ComputeContextExtensions
using ReferenceTracker.Lease lease = default;
context.Barrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice, out *&lease));
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -92,6 +100,8 @@ public static class ComputeContextExtensions
using ReferenceTracker.Lease lease = default;
context.Barrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice, out *&lease));
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -110,6 +120,8 @@ public static class ComputeContextExtensions
using ReferenceTracker.Lease lease = default;
context.Barrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice, out *&lease));
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -128,6 +140,8 @@ public static class ComputeContextExtensions
using ReferenceTracker.Lease lease = default;
context.Barrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice, out *&lease));
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -144,6 +158,8 @@ public static class ComputeContextExtensions
using ReferenceTracker.Lease lease = default;
context.Barrier(((ID3D12ReadOnlyResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out *&lease));
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -160,6 +176,8 @@ public static class ComputeContextExtensions
using ReferenceTracker.Lease lease = default;
context.Barrier(((ID3D12ReadOnlyResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out *&lease));
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -176,6 +194,8 @@ public static class ComputeContextExtensions
using ReferenceTracker.Lease lease = default;
context.Barrier(((ID3D12ReadOnlyResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out *&lease));
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -312,6 +332,8 @@ public static class ComputeContextExtensions
using ReferenceTracker.Lease lease = default;
context.Clear(((ID3D12ReadOnlyResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out *&lease), handles.Gpu, handles.Cpu, true);
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -330,6 +352,8 @@ public static class ComputeContextExtensions
using ReferenceTracker.Lease lease = default;
context.Clear(((ID3D12ReadOnlyResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out *&lease), handles.Gpu, handles.Cpu, true);
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -348,6 +372,8 @@ public static class ComputeContextExtensions
using ReferenceTracker.Lease lease = default;
context.Clear(((ID3D12ReadOnlyResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out *&lease), handles.Gpu, handles.Cpu, true);
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -428,6 +454,8 @@ public static class ComputeContextExtensions
handles.Gpu,
handles.Cpu,
DXGIFormatHelper.ExtendToNormalizedValue(value));
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -451,6 +479,8 @@ public static class ComputeContextExtensions
handles.Gpu,
handles.Cpu,
DXGIFormatHelper.ExtendToNormalizedValue(value));
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -474,6 +504,8 @@ public static class ComputeContextExtensions
handles.Gpu,
handles.Cpu,
DXGIFormatHelper.ExtendToNormalizedValue(value));
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -566,6 +598,8 @@ public static class ComputeContextExtensions
(D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource, out *&lease);
context.Transition(d3D12Resource, states.Before, states.After);
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -583,6 +617,8 @@ public static class ComputeContextExtensions
(D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource, out *&lease);
context.Transition(d3D12Resource, states.Before, states.After);
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -600,6 +636,8 @@ public static class ComputeContextExtensions
(D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource, out *&lease);
context.Transition(d3D12Resource, states.Before, states.After);
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -617,6 +655,8 @@ public static class ComputeContextExtensions
(D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource, out *&lease);
context.Transition(d3D12Resource, states.Before, states.After);
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -638,6 +678,8 @@ public static class ComputeContextExtensions
(D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource, out *&lease);
context.Transition(d3D12Resource, states.Before, states.After);
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -655,6 +697,8 @@ public static class ComputeContextExtensions
(D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource, out *&lease);
context.Transition(d3D12Resource, states.Before, states.After);
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -672,6 +716,8 @@ public static class ComputeContextExtensions
(D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource, out *&lease);
context.Transition(d3D12Resource, states.Before, states.After);
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -689,6 +735,8 @@ public static class ComputeContextExtensions
(D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource, out *&lease);
context.Transition(d3D12Resource, states.Before, states.After);
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -706,6 +754,8 @@ public static class ComputeContextExtensions
(D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource, out *&lease);
context.Transition(d3D12Resource, states.Before, states.After);
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -727,6 +777,8 @@ public static class ComputeContextExtensions
(D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource, out *&lease);
context.Transition(d3D12Resource, states.Before, states.After);
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -744,6 +796,8 @@ public static class ComputeContextExtensions
(D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource, out *&lease);
context.Transition(d3D12Resource, states.Before, states.After);
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -761,6 +815,8 @@ public static class ComputeContextExtensions
(D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource, out *&lease);
context.Transition(d3D12Resource, states.Before, states.After);
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -778,6 +834,8 @@ public static class ComputeContextExtensions
(D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource, out *&lease);
context.Transition(d3D12Resource, states.Before, states.After);
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -795,6 +853,8 @@ public static class ComputeContextExtensions
(D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource, out *&lease);
context.Transition(d3D12Resource, states.Before, states.After);
context.TrackResourceLease(ref *&lease);
}
/// <summary>
@@ -816,5 +876,7 @@ public static class ComputeContextExtensions
(D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource, out *&lease);
context.Transition(d3D12Resource, states.Before, states.After);
context.TrackResourceLease(ref *&lease);
}
}
+2 -2
ファイルの表示
@@ -21,7 +21,7 @@ internal static unsafe class PipelineDataLoader<T>
/// </summary>
private static readonly ConditionalWeakTable<GraphicsDevice, PipelineData> CachedPipelines = [];
private static readonly object PipelineCreationLock = new();
private static readonly ConditionalWeakTable<GraphicsDevice, object> PipelineCreationLocks = [];
/// <summary>
/// Gets the <see cref="PipelineData"/> instance for a given shader.
@@ -35,7 +35,7 @@ internal static unsafe class PipelineDataLoader<T>
return pipelineData;
}
lock (PipelineCreationLock)
lock (PipelineCreationLocks.GetOrCreateValue(device))
{
if (CachedPipelines.TryGetValue(device, out pipelineData))
{
+268
ファイルの表示
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
@@ -340,6 +341,260 @@ public unsafe partial class InteropServicesTests
}
}
[CombinatorialTestMethod]
[AllDevices]
public void ComputeContext_SubmitTwiceThrows(Device device)
{
using ReadWriteTexture2D<float> texture = InteropServices.AllocateSharedReadWriteTexture2D<float>(device.Get(), 16, 16);
using ComputeContext context = device.Get().CreateComputeContext();
context.For(16, 16, new SharedTextureFillShader(texture));
context.Submit();
_ = Assert.ThrowsExactly<InvalidOperationException>(() => context.Submit());
}
[CombinatorialTestMethod]
[AllDevices]
public void ComputeContext_DispatchAfterSubmitThrows(Device device)
{
using ReadWriteTexture2D<float> texture = InteropServices.AllocateSharedReadWriteTexture2D<float>(device.Get(), 16, 16);
using ComputeContext context = device.Get().CreateComputeContext();
context.For(16, 16, new SharedTextureFillShader(texture));
context.Submit();
_ = Assert.ThrowsExactly<InvalidOperationException>(() => context.For(16, 16, new SharedTextureFillShader(texture)));
}
[CombinatorialTestMethod]
[AllDevices]
public void ComputeContext_SubmitAfterDisposeThrows(Device device)
{
using ReadWriteTexture2D<float> texture = InteropServices.AllocateSharedReadWriteTexture2D<float>(device.Get(), 16, 16);
ComputeContext context = device.Get().CreateComputeContext();
context.For(16, 16, new SharedTextureFillShader(texture));
context.Dispose();
_ = Assert.ThrowsExactly<InvalidOperationException>(() => context.Submit());
}
[CombinatorialTestMethod]
[AllDevices]
public void ComputeContext_SubmitWithoutCommandsKeepsDeviceUsable(Device device)
{
using ReadWriteTexture2D<float> texture = InteropServices.AllocateSharedReadWriteTexture2D<float>(device.Get(), 16, 16);
using (ComputeContext context = device.Get().CreateComputeContext())
{
context.Submit();
}
device.Get().For(16, 16, new SharedTextureFillShader(texture));
float[] result = new float[16 * 16];
texture.CopyTo(result);
Assert.AreEqual(30, result[(16 * 16) - 1]);
}
[CombinatorialTestMethod]
[AllDevices]
public void ComputeContext_SubmitsCompleteBeforeLaterBlockingDispatch(Device device)
{
using ReadWriteTexture2D<float> texture = InteropServices.AllocateSharedReadWriteTexture2D<float>(device.Get(), 16, 16);
device.Get().For(16, 16, new SharedTextureFillShader(texture));
for (int i = 0; i < 16; i++)
{
using ComputeContext context = device.Get().CreateComputeContext();
context.For(16, 16, new SharedTextureIncrementShader(texture));
context.Submit();
}
device.Get().For(16, 16, new SharedTextureIncrementShader(texture));
float[] result = new float[16 * 16];
texture.CopyTo(result);
for (int y = 0; y < 16; y++)
{
for (int x = 0; x < 16; x++)
{
Assert.AreEqual(x + y + 17, result[(y * 16) + x]);
}
}
}
[CombinatorialTestMethod]
[AllDevices]
public void ComputeContext_SustainedSubmitsBeyondPendingLimit(Device device)
{
using ReadWriteTexture2D<Bgra32, Float4> texture = InteropServices.AllocateSharedReadWriteTexture2D<Bgra32, Float4>(device.Get(), 16, 16);
using ComPtr<ID3D12Fence> d3D12Fence = default;
nint handle = 0;
InteropServices.CreateSharedFence(device.Get(), Windows.__uuidof<ID3D12Fence>(), (void**)d3D12Fence.GetAddressOf(), &handle);
try
{
for (int i = 0; i < 64; i++)
{
using ComputeContext context = device.Get().CreateComputeContext();
context.For(16, 16, new SharedNormalizedTextureFillShader(texture));
context.Submit();
}
InteropServices.SignalSharedFence(device.Get(), d3D12Fence.Get(), 1);
while (d3D12Fence.Get()->GetCompletedValue() < 1)
{
_ = Thread.Yield();
}
Bgra32[] result = new Bgra32[16 * 16];
texture.CopyTo(result);
foreach (Bgra32 pixel in result)
{
Assert.AreEqual(0xFF808080u, pixel.PackedValue);
}
}
finally
{
_ = CloseHandle(handle);
}
}
[CombinatorialTestMethod]
[AllDevices]
public void ComputeContext_ConcurrentSubmitsFromMultipleThreads(Device device)
{
const int threadCount = 4;
const int iterationCount = 24;
ReadWriteTexture2D<float>[] textures = new ReadWriteTexture2D<float>[threadCount];
try
{
for (int i = 0; i < threadCount; i++)
{
textures[i] = InteropServices.AllocateSharedReadWriteTexture2D<float>(device.Get(), 16, 16);
}
_ = Parallel.For(0, threadCount, i =>
{
using ComPtr<ID3D12Fence> d3D12Fence = default;
nint handle = 0;
InteropServices.CreateSharedFence(device.Get(), Windows.__uuidof<ID3D12Fence>(), (void**)d3D12Fence.GetAddressOf(), &handle);
try
{
for (int j = 0; j < iterationCount; j++)
{
using ComputeContext context = device.Get().CreateComputeContext();
context.For(16, 16, new SharedTextureFillShader(textures[i]));
context.Submit();
}
InteropServices.SignalSharedFence(device.Get(), d3D12Fence.Get(), 1);
while (d3D12Fence.Get()->GetCompletedValue() < 1)
{
_ = Thread.Yield();
}
}
finally
{
_ = CloseHandle(handle);
}
});
float[] result = new float[16 * 16];
for (int i = 0; i < threadCount; i++)
{
textures[i].CopyTo(result);
for (int y = 0; y < 16; y++)
{
for (int x = 0; x < 16; x++)
{
Assert.AreEqual(x + y, result[(y * 16) + x]);
}
}
}
}
finally
{
foreach (ReadWriteTexture2D<float>? texture in textures)
{
texture?.Dispose();
}
}
}
[CombinatorialTestMethod]
[AllDevices]
public void OpenSharedReadWriteTexture2D_FormatMismatchThrows(Device device)
{
using ReadWriteTexture2D<float> source = InteropServices.AllocateSharedReadWriteTexture2D<float>(device.Get(), 16, 16);
nint handle = InteropServices.CreateSharedHandle(source);
try
{
_ = Assert.ThrowsExactly<ArgumentException>(() => InteropServices.OpenSharedReadWriteTexture2D<int>(device.Get(), handle));
}
finally
{
_ = CloseHandle(handle);
}
}
[CombinatorialTestMethod]
[AllDevices]
public void OpenSharedReadWriteTexture2D_FenceHandleThrows(Device device)
{
using ComPtr<ID3D12Fence> d3D12Fence = default;
nint handle = 0;
InteropServices.CreateSharedFence(device.Get(), Windows.__uuidof<ID3D12Fence>(), (void**)d3D12Fence.GetAddressOf(), &handle);
nint openedHandle = handle;
try
{
_ = Assert.ThrowsExactly<Win32Exception>(() => InteropServices.OpenSharedReadWriteTexture2D<float>(device.Get(), openedHandle));
}
finally
{
_ = CloseHandle(handle);
}
}
[CombinatorialTestMethod]
[AllDevices]
public void CreateSharedHandle_NonSharedTextureThrows(Device device)
{
using ReadWriteTexture2D<float> texture = device.Get().AllocateReadWriteTexture2D<float>(16, 16);
_ = Assert.ThrowsExactly<Win32Exception>(() => InteropServices.CreateSharedHandle(texture));
}
[AutoConstructor]
[ThreadGroupSize(DefaultThreadGroupSizes.XY)]
[GeneratedComputeShaderDescriptor]
@@ -366,6 +621,19 @@ public unsafe partial class InteropServicesTests
}
}
[AutoConstructor]
[ThreadGroupSize(DefaultThreadGroupSizes.XY)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct SharedTextureIncrementShader : IComputeShader
{
public readonly ReadWriteTexture2D<float> texture;
public void Execute()
{
this.texture[ThreadIds.XY] += 1f;
}
}
private static void SubmitAndWait(GraphicsDevice device, ReadWriteTexture2D<Bgra32, Float4> texture, ID3D12Fence* d3D12Fence, ref ulong fenceValue)
{
using ComputeContext context = device.CreateComputeContext();