Fixed resource descriptor handles allocation over 4096

このコミットが含まれているのは:
Sergio Pedri committed 2020-12-07 20:00:12 +01:00
1 親 8dd72da729
コミット b8a991b89d
6ファイル変更
+166 -50

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

+14 -4
ファイルの表示
@@ -23,6 +23,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// </summary>
private ComPtr<ID3D12Resource> d3D12Resource;
/// <summary>
/// The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
/// </summary>
internal readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle;
/// <summary>
/// The <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
/// </summary>
@@ -61,18 +66,18 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, (ulong)SizeInBytes);
device.AllocateShaderResourceViewDescriptorHandles(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
switch (resourceType)
{
case ResourceType.Constant:
device.D3D12Device->CreateConstantBufferView(this.d3D12Resource, SizeInBytes, d3D12CpuDescriptorHandle);
device.D3D12Device->CreateConstantBufferView(this.d3D12Resource, SizeInBytes, D3D12CpuDescriptorHandle);
break;
case ResourceType.ReadOnly:
device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, (uint)length, elementSizeInBytes, d3D12CpuDescriptorHandle);
device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, (uint)length, elementSizeInBytes, D3D12CpuDescriptorHandle);
break;
case ResourceType.ReadWrite:
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource, (uint)length, elementSizeInBytes, d3D12CpuDescriptorHandle);
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource, (uint)length, elementSizeInBytes, D3D12CpuDescriptorHandle);
break;
}
}
@@ -147,6 +152,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
protected override void OnDispose()
{
this.d3D12Resource.Dispose();
if (!GraphicsDevice.IsDisposed)
{
GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle);
}
}
/// <summary>
+13 -3
ファイルの表示
@@ -28,6 +28,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// </summary>
private ComPtr<ID3D12Resource> d3D12Resource;
/// <summary>
/// The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
/// </summary>
internal readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle;
/// <summary>
/// The <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
/// </summary>
@@ -73,15 +78,15 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
? D3D12_COMMAND_LIST_TYPE_COPY
: D3D12_COMMAND_LIST_TYPE_COMPUTE;
device.AllocateShaderResourceViewDescriptorHandles(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
switch (resourceType)
{
case ResourceType.ReadOnly:
device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, DXGIFormatHelper.GetForType<T>(), D3D12_SRV_DIMENSION_TEXTURE2D, d3D12CpuDescriptorHandle);
device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, DXGIFormatHelper.GetForType<T>(), D3D12_SRV_DIMENSION_TEXTURE2D, D3D12CpuDescriptorHandle);
break;
case ResourceType.ReadWrite:
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource, DXGIFormatHelper.GetForType<T>(), D3D12_UAV_DIMENSION_TEXTURE2D, d3D12CpuDescriptorHandle);
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource, DXGIFormatHelper.GetForType<T>(), D3D12_UAV_DIMENSION_TEXTURE2D, D3D12CpuDescriptorHandle);
break;
}
}
@@ -251,6 +256,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
protected override void OnDispose()
{
this.d3D12Resource.Dispose();
if (!GraphicsDevice.IsDisposed)
{
GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle);
}
}
/// <summary>
+13 -3
ファイルの表示
@@ -28,6 +28,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// </summary>
private ComPtr<ID3D12Resource> d3D12Resource;
/// <summary>
/// The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
/// </summary>
internal readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle;
/// <summary>
/// The <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
/// </summary>
@@ -77,15 +82,15 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
? D3D12_COMMAND_LIST_TYPE_COPY
: D3D12_COMMAND_LIST_TYPE_COMPUTE;
device.AllocateShaderResourceViewDescriptorHandles(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
switch (resourceType)
{
case ResourceType.ReadOnly:
device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, DXGIFormatHelper.GetForType<T>(), D3D12_SRV_DIMENSION_TEXTURE3D, d3D12CpuDescriptorHandle);
device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, DXGIFormatHelper.GetForType<T>(), D3D12_SRV_DIMENSION_TEXTURE3D, D3D12CpuDescriptorHandle);
break;
case ResourceType.ReadWrite:
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource, DXGIFormatHelper.GetForType<T>(), D3D12_UAV_DIMENSION_TEXTURE3D, d3D12CpuDescriptorHandle);
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource, DXGIFormatHelper.GetForType<T>(), D3D12_UAV_DIMENSION_TEXTURE3D, D3D12CpuDescriptorHandle);
break;
}
}
@@ -274,6 +279,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
protected override void OnDispose()
{
this.d3D12Resource.Dispose();
if (!GraphicsDevice.IsDisposed)
{
GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle);
}
}
/// <summary>
+108 -30
ファイルの表示
@@ -1,4 +1,5 @@
using ComputeSharp.Graphics.Extensions;
using Microsoft.Toolkit.Diagnostics;
using System;
using TerraFX.Interop;
using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_TYPE;
@@ -21,29 +22,24 @@ namespace ComputeSharp.Graphics
private ComPtr<ID3D12DescriptorHeap> d3D12DescriptorHeap;
/// <summary>
/// The dummy object used to handle concurrent allocation requests.
/// The array of <see cref="D3D12DescriptorHandlePair"/> items with the available descriptor handles.
/// </summary>
private readonly object allocationLock;
private readonly D3D12DescriptorHandlePair[] d3D12DescriptorHandlePairs;
/// <summary>
/// The size of each new descriptor being allocated.
/// The head index for the queue.
/// </summary>
private readonly uint descriptorSize;
private int head;
/// <summary>
/// The current <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> for the <see cref="ID3D12DescriptorHandleAllocator"/> instance in use.
/// The tail index for the queue.
/// </summary>
private D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle;
private int tail;
/// <summary>
/// The current <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> for the <see cref="ID3D12DescriptorHandleAllocator"/> instance in use.
/// The current size of the queue (ie. the number of remaining pairs in <see cref="d3D12DescriptorHandlePairs"/>).
/// </summary>
private D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle;
/// <summary>
/// The number of remaining handles to allocate on the current heap.
/// </summary>
private uint remainingHandles;
private int size;
/// <summary>
/// Creates a new <see cref="ID3D12DescriptorHandleAllocator"/> instance with the specified parameters.
@@ -52,11 +48,23 @@ namespace ComputeSharp.Graphics
public ID3D12DescriptorHandleAllocator(ID3D12Device* device)
{
this.d3D12DescriptorHeap = device->CreateDescriptorHeap(DescriptorsPerHeap);
this.allocationLock = new object();
this.descriptorSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
this.d3D12CpuDescriptorHandle = this.d3D12DescriptorHeap.Get()->GetCPUDescriptorHandleForHeapStart();
this.d3D12GpuDescriptorHandle = this.d3D12DescriptorHeap.Get()->GetGPUDescriptorHandleForHeapStart();
this.remainingHandles = this.d3D12DescriptorHeap.Get()->GetDesc().NumDescriptors;
this.d3D12DescriptorHandlePairs = GC.AllocateUninitializedArray<D3D12DescriptorHandlePair>((int)DescriptorsPerHeap);
this.head = 0;
this.tail = 0;
this.size = (int)DescriptorsPerHeap;
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle = this.d3D12DescriptorHeap.Get()->GetCPUDescriptorHandleForHeapStart();
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandleStart = this.d3D12DescriptorHeap.Get()->GetGPUDescriptorHandleForHeapStart();
uint descriptorIncrementSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
for (int i = 0; i < this.d3D12DescriptorHandlePairs.Length; i++)
{
this.d3D12DescriptorHandlePairs[i] = new D3D12DescriptorHandlePair(
in d3D12CpuDescriptorHandle,
in d3D12GpuDescriptorHandleStart,
i,
descriptorIncrementSize);
}
}
/// <summary>
@@ -65,29 +73,53 @@ namespace ComputeSharp.Graphics
public ID3D12DescriptorHeap* D3D12DescriptorHeap => this.d3D12DescriptorHeap;
/// <summary>
/// Allocates a new CPU and GPU handle pair to use in a memory buffer.
/// Rents a new CPU and GPU handle pair to use in a memory buffer.
/// </summary>
/// <param name="d3D12CpuDescriptorHandle">The resulting <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> value.</param>
/// <param name="d3D12CpuDescriptorHandle">The resulting <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> value.</param>
public void Allocate(
public void Rent(
out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle,
out D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle)
{
lock (this.allocationLock)
lock (this.d3D12DescriptorHandlePairs)
{
if (this.remainingHandles == 0)
Guard.IsGreaterThan(this.size, 0, nameof(size));
ref readonly D3D12DescriptorHandlePair d3D12DescriptorHandlePair = ref this.d3D12DescriptorHandlePairs[this.head++];
d3D12CpuDescriptorHandle = d3D12DescriptorHandlePair.D3D12CpuDescriptorHandle;
d3D12GpuDescriptorHandle = d3D12DescriptorHandlePair.D3D12GpuDescriptorhandle;
if (this.head == DescriptorsPerHeap)
{
this.d3D12CpuDescriptorHandle = this.d3D12DescriptorHeap.Get()->GetCPUDescriptorHandleForHeapStart();
this.d3D12GpuDescriptorHandle = this.d3D12DescriptorHeap.Get()->GetGPUDescriptorHandleForHeapStart();
this.remainingHandles = this.d3D12DescriptorHeap.Get()->GetDesc().NumDescriptors;
this.head = 0;
}
d3D12CpuDescriptorHandle = this.d3D12CpuDescriptorHandle;
d3D12GpuDescriptorHandle = this.d3D12GpuDescriptorHandle;
this.size--;
}
}
this.d3D12CpuDescriptorHandle.ptr += this.descriptorSize;
this.d3D12GpuDescriptorHandle.ptr += this.descriptorSize;
this.remainingHandles--;
/// <summary>
/// Returns a CPU and GPU handle pair for later use.
/// </summary>
/// <param name="d3D12CpuDescriptorHandle">The returned <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> value.</param>
/// <param name="d3D12CpuDescriptorHandle">The returned <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> value.</param>
public void Return(
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle,
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle)
{
lock (this.d3D12DescriptorHandlePairs)
{
Guard.IsLessThan(this.size, DescriptorsPerHeap, nameof(size));
this.d3D12DescriptorHandlePairs[this.tail++] = new D3D12DescriptorHandlePair(d3D12CpuDescriptorHandle, d3D12GpuDescriptorHandle);
if (this.tail == DescriptorsPerHeap)
{
this.tail = 0;
}
this.size++;
}
}
@@ -96,5 +128,51 @@ namespace ComputeSharp.Graphics
{
this.d3D12DescriptorHeap.Dispose();
}
/// <summary>
/// A type representing a pair of reusable descriptor handles.
/// </summary>
private readonly struct D3D12DescriptorHandlePair
{
/// <summary>
/// The <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> value for the current entry.
/// </summary>
public readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle;
/// <summary>
/// The <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> value for the current entry.
/// </summary>
public readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorhandle;
/// <summary>
/// Creates a new <see cref="D3D12DescriptorHandlePair"/> instance with the given parameters.
/// </summary>
/// <param name="d3D12CpuDescriptorHandle">The <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> value to wrap.</param>
/// <param name="d3D12GpuDescriptorHandle">The <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> value to wrap.</param>
public D3D12DescriptorHandlePair(
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle,
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle)
{
D3D12CpuDescriptorHandle = d3D12CpuDescriptorHandle;
D3D12GpuDescriptorhandle = d3D12GpuDescriptorHandle;
}
/// <summary>
/// Creates a new <see cref="D3D12DescriptorHandlePair"/> instance with the given parameters.
/// </summary>
/// <param name="d3D12CpuDescriptorHandleStart">The base <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> value.</param>
/// <param name="d3D12GpuDescriptorHandleStart">The base <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> value.</param>
/// <param name="offset">The offset for the new pair of handles to compute.</param>
/// <param name="descriptorIncrementSize">The increment size for each consecutive handles pair.</param>
public D3D12DescriptorHandlePair(
in D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleStart,
in D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandleStart,
int offset,
uint descriptorIncrementSize)
{
D3D12_CPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12CpuDescriptorHandle, in d3D12CpuDescriptorHandleStart, offset, descriptorIncrementSize);
D3D12_GPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12GpuDescriptorhandle, in d3D12GpuDescriptorHandleStart, offset, descriptorIncrementSize);
}
}
}
}
+10 -2
ファイルの表示
@@ -126,11 +126,19 @@ namespace ComputeSharp.Graphics
internal ID3D12Device* D3D12Device => this.d3D12Device;
/// <inheritdoc cref="ID3D12DescriptorHandleAllocator.Allocate"/>
internal void AllocateShaderResourceViewDescriptorHandles(
internal void RentShaderResourceViewDescriptorHandles(
out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle,
out D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle)
{
this.shaderResourceViewDescriptorAllocator.Allocate(out d3D12CpuDescriptorHandle, out d3D12GpuDescriptorHandle);
this.shaderResourceViewDescriptorAllocator.Rent(out d3D12CpuDescriptorHandle, out d3D12GpuDescriptorHandle);
}
/// <inheritdoc cref="ID3D12DescriptorHandleAllocator.Return"/>
internal void ReturnShaderResourceViewDescriptorHandles(
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle,
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle)
{
this.shaderResourceViewDescriptorAllocator.Return(d3D12CpuDescriptorHandle, d3D12GpuDescriptorHandle);
}
/// <inheritdoc cref="ID3D12CommandAllocatorPool.GetCommandAllocator"/>
+8 -8
ファイルの表示
@@ -8,11 +8,6 @@ namespace ComputeSharp.Core.Interop
/// </summary>
public abstract class NativeObject : IDisposable
{
/// <summary>
/// Indicates whether or not the current instance has already been disposed.
/// </summary>
private bool isDisposed;
/// <summary>
/// Releases unmanaged resources and performs other cleanup operations.
/// </summary>
@@ -21,6 +16,11 @@ namespace ComputeSharp.Core.Interop
CheckAndDispose();
}
/// <summary>
/// Gets whether or not the current instance has already been disposed.
/// </summary>
internal bool IsDisposed { get; private set; }
/// <summary>
/// Releases all the native resources for the current instance.
/// </summary>
@@ -36,9 +36,9 @@ namespace ComputeSharp.Core.Interop
/// </summary>
private void CheckAndDispose()
{
if (!this.isDisposed)
if (!IsDisposed)
{
this.isDisposed = true;
IsDisposed = true;
OnDispose();
}
@@ -55,7 +55,7 @@ namespace ComputeSharp.Core.Interop
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void ThrowIfDisposed()
{
if (this.isDisposed)
if (IsDisposed)
{
// We can't use ThrowHelper here as we only want to invoke ToString if we
// are about to throw an exception. The JIT will recognize this pattern