diff --git a/src/ComputeSharp/Graphics/Exceptions/GraphicsDeviceMismatchException.cs b/src/ComputeSharp/Graphics/Exceptions/GraphicsDeviceMismatchException.cs
index 6283a35b..814a9d05 100644
--- a/src/ComputeSharp/Graphics/Exceptions/GraphicsDeviceMismatchException.cs
+++ b/src/ComputeSharp/Graphics/Exceptions/GraphicsDeviceMismatchException.cs
@@ -22,15 +22,15 @@ public sealed class GraphicsDeviceMismatchException : InvalidOperationException
///
/// Creates a new instance from the specified parameters.
///
- /// The input that was used.
+ /// The input that was used.
/// The source instance tied to .
/// The target instance that was used.
/// A new instance with a formatted error message.
///
- /// This method only takes a instance and the associated instance as
+ /// This method only takes a instance and the associated instance as
/// will still be available, but without the unnecessary generic type specializations for the method.
///
- private static GraphicsDeviceMismatchException Create(NativeObject resource, GraphicsDevice sourceDevice, GraphicsDevice destinationDevice)
+ private static GraphicsDeviceMismatchException Create(ReferenceTracker.ITrackedObject resource, GraphicsDevice sourceDevice, GraphicsDevice destinationDevice)
{
StringBuilder builder = new(512);
diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs
index 47d37914..afdae583 100644
--- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs
+++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs
@@ -150,9 +150,11 @@ public static partial class GraphicsDeviceExtensions
{
Guard.IsNotNull(device);
- device.ThrowIfDisposed();
- device.ThrowIfDeviceLost();
+ using (device.GetReferenceTracker().GetLease())
+ {
+ device.ThrowIfDeviceLost();
- return new(device);
+ return new(device);
+ }
}
}
diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.DeviceLost.cs b/src/ComputeSharp/Graphics/GraphicsDevice.DeviceLost.cs
index 931b14cb..c0f91f06 100644
--- a/src/ComputeSharp/Graphics/GraphicsDevice.DeviceLost.cs
+++ b/src/ComputeSharp/Graphics/GraphicsDevice.DeviceLost.cs
@@ -48,42 +48,32 @@ unsafe partial class GraphicsDevice
///
private void QueueRaiseDeviceLostEventIfNeeded()
{
- if (IsDisposed)
+ using (this.referenceTracker.TryGetLease(out bool leaseTaken))
{
- return;
- }
-
- // If the current device removed reason is not S_OK, it means the event
- // has already been raised (but the device will keep reporting that reason).
- // In that case, do nothing to avoid raising it again while the device is alive.
- if (Volatile.Read(ref Unsafe.As(ref this.deviceRemovedReason)) != S.S_OK)
- {
- return;
- }
-
- HRESULT result;
-
- // Make sure to guard against concurrent accesses from the dispose logic
- lock (this.d3D12DeviceLock)
- {
- ID3D12Device* d3D12Device = this.d3D12Device.Get();
-
- if (d3D12Device is null)
+ if (!leaseTaken)
{
return;
}
- result = d3D12Device->GetDeviceRemovedReason();
- }
+ // If the current device removed reason is not S_OK, it means the event
+ // has already been raised (but the device will keep reporting that reason).
+ // In that case, do nothing to avoid raising it again while the device is alive.
+ if (Volatile.Read(ref Unsafe.As(ref this.deviceRemovedReason)) != S.S_OK)
+ {
+ return;
+ }
- // Only raise the event once, and store the device removed reason to track it
- if (result != S.S_OK &&
- Interlocked.CompareExchange(
- ref Unsafe.As(ref this.deviceRemovedReason),
- (int)result,
- S.S_OK) == S.S_OK)
- {
- QueueRaiseDeviceLostEvent();
+ HRESULT result = this.d3D12Device.Get()->GetDeviceRemovedReason();
+
+ // Only raise the event once, and store the device removed reason to track it
+ if (result != S.S_OK &&
+ Interlocked.CompareExchange(
+ ref Unsafe.As(ref this.deviceRemovedReason),
+ (int)result,
+ S.S_OK) == S.S_OK)
+ {
+ QueueRaiseDeviceLostEvent();
+ }
}
}
diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs
index 031ed041..00d8b987 100644
--- a/src/ComputeSharp/Graphics/GraphicsDevice.cs
+++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs
@@ -23,8 +23,13 @@ namespace ComputeSharp;
/// A that represents an instance that can be used to run compute shaders.
///
[DebuggerDisplay("{ToString(),raw}")]
-public sealed unsafe partial class GraphicsDevice : NativeObject
+public sealed unsafe partial class GraphicsDevice : ReferenceTracker.ITrackedObject, IDisposable
{
+ ///
+ /// The owning object for the current instance.
+ ///
+ private ReferenceTracker referenceTracker;
+
///
/// The underlying wrapped by the current instance.
///
@@ -107,13 +112,6 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
///
private HRESULT deviceRemovedReason;
- ///
- /// A lcok object used to synchronize accesses to between and .
- /// This is needed because the device lost callback might race with a dispose call and end up trying to invoke a method on an invalid object.
- /// We can't just check whether it's , as a null condition might still cause a racing thread to access a freed up object.
- ///
- private readonly object d3D12DeviceLock = new();
-
///
/// Raised whenever the device is lost.
///
@@ -141,6 +139,8 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
/// The available info for the new instance.
internal GraphicsDevice(ID3D12Device* d3D12Device, IDXGIAdapter* dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1)
{
+ this.referenceTracker = new ReferenceTracker(this);
+
this.d3D12Device = new ComPtr(d3D12Device);
this.d3D12ComputeCommandQueue = d3D12Device->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COMPUTE);
@@ -181,6 +181,14 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
RegisterDeviceLostCallback(this, out this.deviceHandle, out this.deviceRemovedEvent, out this.deviceRemovedWaitHandle);
}
+ ///
+ /// Releases unmanaged resources for the current instance.
+ ///
+ ~GraphicsDevice()
+ {
+ this.referenceTracker.Dispose();
+ }
+
///
/// Gets the locally unique identifier for the current device.
///
@@ -245,12 +253,14 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
/// Whether the current device supports double precision floating point operations in shaders.
public bool IsDoublePrecisionSupportAvailable()
{
- ThrowIfDisposed();
- ThrowIfDeviceLost();
+ using (this.referenceTracker.GetLease())
+ {
+ ThrowIfDeviceLost();
- var d3D12OptionsData = this.d3D12Device.Get()->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS);
+ var d3D12OptionsData = this.d3D12Device.Get()->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS);
- return d3D12OptionsData.DoublePrecisionFloatShaderOps;
+ return d3D12OptionsData.DoublePrecisionFloatShaderOps;
+ }
}
///
@@ -262,10 +272,12 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
public bool IsReadOnlyTexture2DSupportedForType()
where T : unmanaged
{
- ThrowIfDisposed();
- ThrowIfDeviceLost();
+ using (this.referenceTracker.GetLease())
+ {
+ ThrowIfDeviceLost();
- return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE2D);
+ return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE2D);
+ }
}
///
@@ -277,12 +289,14 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
public bool IsReadWriteTexture2DSupportedForType()
where T : unmanaged
{
- ThrowIfDisposed();
- ThrowIfDeviceLost();
+ using (this.referenceTracker.GetLease())
+ {
+ ThrowIfDeviceLost();
- return this.d3D12Device.Get()->IsDxgiFormatSupported(
- DXGIFormatHelper.GetForType(),
- D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW);
+ return this.d3D12Device.Get()->IsDxgiFormatSupported(
+ DXGIFormatHelper.GetForType(),
+ D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW);
+ }
}
///
@@ -294,10 +308,12 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
public bool IsReadOnlyTexture3DSupportedForType()
where T : unmanaged
{
- ThrowIfDisposed();
- ThrowIfDeviceLost();
+ using (this.referenceTracker.GetLease())
+ {
+ ThrowIfDeviceLost();
- return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE3D);
+ return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE3D);
+ }
}
///
@@ -309,12 +325,14 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
public bool IsReadWriteTexture3DSupportedForType()
where T : unmanaged
{
- ThrowIfDisposed();
- ThrowIfDeviceLost();
+ using (this.referenceTracker.GetLease())
+ {
+ ThrowIfDeviceLost();
- return this.d3D12Device.Get()->IsDxgiFormatSupported(
- DXGIFormatHelper.GetForType(),
- D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW);
+ return this.d3D12Device.Get()->IsDxgiFormatSupported(
+ DXGIFormatHelper.GetForType(),
+ D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW);
+ }
}
///
@@ -405,16 +423,32 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
}
///
- protected override void OnDispose()
+ public void Dispose()
+ {
+ this.referenceTracker.Dispose();
+
+ GC.SuppressFinalize(this);
+ }
+
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal ref ReferenceTracker GetReferenceTracker()
+ {
+ return ref this.referenceTracker;
+ }
+
+ ///
+ ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
+ {
+ return ref this.referenceTracker;
+ }
+
+ ///
+ void ReferenceTracker.ITrackedObject.DangerousRelease()
{
DeviceHelper.NotifyDisposedDevice(this);
- // Guard the dispose call around the native device to avoid device lost races
- lock (this.d3D12DeviceLock)
- {
- this.d3D12Device.Dispose();
- }
-
+ this.d3D12Device.Dispose();
this.d3D12ComputeCommandQueue.Dispose();
this.d3D12CopyCommandQueue.Dispose();
this.d3D12ComputeFence.Dispose();
diff --git a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs
index 57b879ab..29317385 100644
--- a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs
+++ b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs
@@ -64,12 +64,14 @@ public static class GraphicsResourceHelper
{
Guard.IsNotNull(buffer);
- buffer.ThrowIfDisposed();
- buffer.ThrowIfDeviceMismatch(device);
+ using (buffer.GetReferenceTracker().GetLease())
+ {
+ buffer.ThrowIfDeviceMismatch(device);
- D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = buffer.D3D12GpuDescriptorHandle;
+ D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = buffer.D3D12GpuDescriptorHandle;
- return *(ulong*)&d3D12GpuDescriptorHandle;
+ return *(ulong*)&d3D12GpuDescriptorHandle;
+ }
}
///
@@ -85,12 +87,14 @@ public static class GraphicsResourceHelper
{
Guard.IsNotNull(texture);
- texture.ThrowIfDisposed();
- texture.ThrowIfDeviceMismatch(device);
+ using (texture.GetReferenceTracker().GetLease())
+ {
+ texture.ThrowIfDeviceMismatch(device);
- D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle;
+ D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle;
- return *(ulong*)&d3D12GpuDescriptorHandle;
+ return *(ulong*)&d3D12GpuDescriptorHandle;
+ }
}
///
@@ -106,12 +110,14 @@ public static class GraphicsResourceHelper
{
Guard.IsNotNull(texture);
- texture.ThrowIfDisposed();
- texture.ThrowIfDeviceMismatch(device);
+ using (texture.GetReferenceTracker().GetLease())
+ {
+ texture.ThrowIfDeviceMismatch(device);
- D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle;
+ D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle;
- return *(ulong*)&d3D12GpuDescriptorHandle;
+ return *(ulong*)&d3D12GpuDescriptorHandle;
+ }
}
///
@@ -196,12 +202,14 @@ public static class GraphicsResourceHelper
{
Guard.IsNotNull(texture);
- texture.ThrowIfDisposed();
- texture.ThrowIfDeviceMismatch(device);
+ using (texture.GetReferenceTracker().GetLease())
+ {
+ texture.ThrowIfDeviceMismatch(device);
- D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle;
+ D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle;
- return *(ulong*)&d3D12GpuDescriptorHandle;
+ return *(ulong*)&d3D12GpuDescriptorHandle;
+ }
}
///
@@ -217,12 +225,14 @@ public static class GraphicsResourceHelper
{
Guard.IsNotNull(texture);
- texture.ThrowIfDisposed();
- texture.ThrowIfDeviceMismatch(device);
+ using (texture.GetReferenceTracker().GetLease())
+ {
+ texture.ThrowIfDeviceMismatch(device);
- D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle;
+ D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle;
- return *(ulong*)&d3D12GpuDescriptorHandle;
+ return *(ulong*)&d3D12GpuDescriptorHandle;
+ }
}
///
diff --git a/src/ComputeSharp/Graphics/Interop/NativeObject.cs b/src/ComputeSharp/Graphics/Interop/NativeObject.cs
index f0310f90..d96a06f9 100644
--- a/src/ComputeSharp/Graphics/Interop/NativeObject.cs
+++ b/src/ComputeSharp/Graphics/Interop/NativeObject.cs
@@ -7,7 +7,7 @@ namespace ComputeSharp.Interop;
///
/// Base class for a class.
///
-public abstract class NativeObject : IDisposable
+internal abstract class NativeObject : IDisposable
{
///
/// Indicates whether or not the current instance has been disposed.
diff --git a/src/ComputeSharp/Graphics/Interop/ReferenceTracker.cs b/src/ComputeSharp/Graphics/Interop/ReferenceTracker.cs
index b55b26c5..3f26d23b 100644
--- a/src/ComputeSharp/Graphics/Interop/ReferenceTracker.cs
+++ b/src/ComputeSharp/Graphics/Interop/ReferenceTracker.cs
@@ -154,7 +154,7 @@ internal struct ReferenceTracker : IDisposable
///
/// A reference tracking lease to extend the lifetime of a given tracked object while in a given scope.
///
- public struct Lease
+ public struct Lease : IDisposable
{
///
/// The tracked object being wrapped, if any.
diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs
index b830a105..7ef4b0b7 100644
--- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs
@@ -1,4 +1,5 @@
-using System.Buffers;
+using System;
+using System.Buffers;
using System.Runtime.CompilerServices;
using CommunityToolkit.Diagnostics;
using ComputeSharp.Core.Helpers;
@@ -24,9 +25,14 @@ namespace ComputeSharp.Resources;
/// A representing a typed buffer stored on GPU memory.
///
/// The type of items stored on the buffer.
-public unsafe abstract class Buffer : NativeObject, IGraphicsResource
+public unsafe abstract class Buffer : ReferenceTracker.ITrackedObject, IDisposable, IGraphicsResource
where T : unmanaged
{
+ ///
+ /// The owning object for the current instance.
+ ///
+ private ReferenceTracker referenceTracker;
+
#if NET6_0_OR_GREATER
///
/// The instance used to retrieve .
@@ -64,63 +70,75 @@ public unsafe abstract class Buffer : NativeObject, IGraphicsResource
/// The allocation mode to use for the new resource.
private protected Buffer(GraphicsDevice device, int length, uint elementSizeInBytes, ResourceType resourceType, AllocationMode allocationMode)
{
- device.ThrowIfDisposed();
- device.ThrowIfDeviceLost();
+ this.referenceTracker = new ReferenceTracker(this);
- if (resourceType == ResourceType.Constant)
+ using (device.GetReferenceTracker().GetLease())
{
- Guard.IsBetweenOrEqualTo(length, 1, D3D12.D3D12_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
- }
- else
- {
- // The maximum length is set such that the aligned buffer size can't exceed uint.MaxValue
- Guard.IsBetweenOrEqualTo(length, 1, (uint.MaxValue / elementSizeInBytes) & ~255);
- }
+ device.ThrowIfDeviceLost();
- if (TypeInfo.IsDoubleOrContainsDoubles &&
- device.D3D12Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS).DoublePrecisionFloatShaderOps == 0)
- {
- UnsupportedDoubleOperationException.Throw();
- }
+ if (resourceType == ResourceType.Constant)
+ {
+ Guard.IsBetweenOrEqualTo(length, 1, D3D12.D3D12_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
+ }
+ else
+ {
+ // The maximum length is set such that the aligned buffer size can't exceed uint.MaxValue
+ Guard.IsBetweenOrEqualTo(length, 1, (uint.MaxValue / elementSizeInBytes) & ~255);
+ }
- nint usableSizeInBytes = checked((nint)(length * elementSizeInBytes));
- nint effectiveSizeInBytes = resourceType == ResourceType.Constant ? AlignmentHelper.Pad(usableSizeInBytes, D3D12.D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT) : usableSizeInBytes;
+ if (TypeInfo.IsDoubleOrContainsDoubles &&
+ device.D3D12Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS).DoublePrecisionFloatShaderOps == 0)
+ {
+ UnsupportedDoubleOperationException.Throw();
+ }
- SizeInBytes = usableSizeInBytes;
- GraphicsDevice = device;
- Length = length;
+ nint usableSizeInBytes = checked((nint)(length * elementSizeInBytes));
+ nint effectiveSizeInBytes = resourceType == ResourceType.Constant ? AlignmentHelper.Pad(usableSizeInBytes, D3D12.D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT) : usableSizeInBytes;
+
+ SizeInBytes = usableSizeInBytes;
+ GraphicsDevice = device;
+ Length = length;
#if NET6_0_OR_GREATER
- this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, (ulong)effectiveSizeInBytes);
- this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource());
+ this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, (ulong)effectiveSizeInBytes);
+ this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource());
#else
- this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, (ulong)effectiveSizeInBytes, device.IsCacheCoherentUMA);
+ this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, (ulong)effectiveSizeInBytes, device.IsCacheCoherentUMA);
#endif
- device.RegisterAllocatedResource();
- device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
- device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView);
+ device.RegisterAllocatedResource();
+ device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
+ device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView);
- switch (resourceType)
- {
- case ResourceType.Constant:
- device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), effectiveSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
- break;
- case ResourceType.ReadOnly:
- device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
- break;
- case ResourceType.ReadWrite:
- device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
- device.D3D12Device->CreateUnorderedAccessViewForClear(
- this.d3D12Resource.Get(),
- DXGI_FORMAT_R32_UINT,
- (uint)(usableSizeInBytes / sizeof(uint)),
- this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandle,
- this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandleNonShaderVisible);
- break;
+ switch (resourceType)
+ {
+ case ResourceType.Constant:
+ device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), effectiveSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
+ break;
+ case ResourceType.ReadOnly:
+ device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
+ break;
+ case ResourceType.ReadWrite:
+ device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
+ device.D3D12Device->CreateUnorderedAccessViewForClear(
+ this.d3D12Resource.Get(),
+ DXGI_FORMAT_R32_UINT,
+ (uint)(usableSizeInBytes / sizeof(uint)),
+ this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandle,
+ this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandleNonShaderVisible);
+ break;
+ }
+
+ this.d3D12Resource.Get()->SetName(this);
}
+ }
- this.d3D12Resource.Get()->SetName(this);
+ ///
+ /// Releases unmanaged resources for the current instance.
+ ///
+ ~Buffer()
+ {
+ this.referenceTracker.Dispose();
}
///
@@ -201,7 +219,28 @@ public unsafe abstract class Buffer : NativeObject, IGraphicsResource
internal abstract void CopyFrom(ref T source, int destinationOffset, int count);
///
- protected override void OnDispose()
+ public void Dispose()
+ {
+ this.referenceTracker.Dispose();
+
+ GC.SuppressFinalize(this);
+ }
+
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal ref ReferenceTracker GetReferenceTracker()
+ {
+ return ref this.referenceTracker;
+ }
+
+ ///
+ ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
+ {
+ return ref this.referenceTracker;
+ }
+
+ ///
+ void ReferenceTracker.ITrackedObject.DangerousRelease()
{
this.d3D12Resource.Dispose();
#if NET6_0_OR_GREATER
@@ -231,20 +270,24 @@ public unsafe abstract class Buffer : NativeObject, IGraphicsResource
///
internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device)
{
- ThrowIfDisposed();
- ThrowIfDeviceMismatch(device);
+ using (this.referenceTracker.GetLease())
+ {
+ ThrowIfDeviceMismatch(device);
- return (
- this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12GpuDescriptorHandle,
- this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandleNonShaderVisible);
+ return (
+ this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12GpuDescriptorHandle,
+ this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandleNonShaderVisible);
+ }
}
///
internal unsafe ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device)
{
- ThrowIfDisposed();
- ThrowIfDeviceMismatch(device);
+ using (this.referenceTracker.GetLease())
+ {
+ ThrowIfDeviceMismatch(device);
- return D3D12Resource;
+ return D3D12Resource;
+ }
}
}
diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs
index c2693cea..be9dcddb 100644
--- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs
@@ -32,71 +32,72 @@ public abstract class StructuredBuffer : Buffer
///
internal override unsafe void CopyTo(ref T destination, int sourceOffset, int count)
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
-
- ThrowIfDisposed();
-
- Guard.IsBetweenOrEqualTo(count, 0, Length);
- Guard.IsInRange(sourceOffset, 0, Length);
- Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset));
-
- if (GraphicsDevice.IsCacheCoherentUMA)
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (GetReferenceTracker().GetLease())
{
- using ID3D12ResourceMap resource = D3D12Resource->Map();
+ GraphicsDevice.ThrowIfDeviceLost();
- fixed (void* destinationPointer = &destination)
+ Guard.IsBetweenOrEqualTo(count, 0, Length);
+ Guard.IsInRange(sourceOffset, 0, Length);
+ Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset));
+
+ if (GraphicsDevice.IsCacheCoherentUMA)
{
- MemoryHelper.Copy(
- source: resource.Pointer,
- destination: destinationPointer,
- sourceElementOffset: (uint)sourceOffset,
- destinationElementOffset: 0,
- sourceElementPitchInBytes: (uint)sizeof(T),
- destinationElementPitchInBytes: (uint)sizeof(T),
- count: (uint)count);
+ using ID3D12ResourceMap resource = D3D12Resource->Map();
+
+ fixed (void* destinationPointer = &destination)
+ {
+ MemoryHelper.Copy(
+ source: resource.Pointer,
+ destination: destinationPointer,
+ sourceElementOffset: (uint)sourceOffset,
+ destinationElementOffset: 0,
+ sourceElementPitchInBytes: (uint)sizeof(T),
+ destinationElementPitchInBytes: (uint)sizeof(T),
+ count: (uint)count);
+ }
}
- }
- else
- {
- nint byteOffset = (nint)sourceOffset * sizeof(T);
- nint byteLength = count * sizeof(T);
-
-#if NET6_0_OR_GREATER
- using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.ReadBack, AllocationMode.Default, (ulong)byteLength);
-#else
- using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
- ResourceType.ReadBack,
- (ulong)byteLength,
- GraphicsDevice.IsCacheCoherentUMA);
-#endif
-
- using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY))
+ else
{
-#if NET6_0_OR_GREATER
- copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(allocation.Get()->GetResource(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteLength);
-#else
- copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteLength);
-#endif
- copyCommandList.ExecuteAndWaitForCompletion();
- }
+ nint byteOffset = (nint)sourceOffset * sizeof(T);
+ nint byteLength = count * sizeof(T);
#if NET6_0_OR_GREATER
- using ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map();
+ using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.ReadBack, AllocationMode.Default, (ulong)byteLength);
#else
- using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
+ using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
+ ResourceType.ReadBack,
+ (ulong)byteLength,
+ GraphicsDevice.IsCacheCoherentUMA);
#endif
- fixed (void* destinationPointer = &destination)
- {
- MemoryHelper.Copy(
- source: resource.Pointer,
- destination: destinationPointer,
- sourceElementOffset: 0,
- destinationElementOffset: 0,
- sourceElementPitchInBytes: (uint)sizeof(T),
- destinationElementPitchInBytes: (uint)sizeof(T),
- count: (uint)count);
+ using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY))
+ {
+#if NET6_0_OR_GREATER
+ copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(allocation.Get()->GetResource(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteLength);
+#else
+ copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteLength);
+#endif
+ copyCommandList.ExecuteAndWaitForCompletion();
+ }
+
+#if NET6_0_OR_GREATER
+ using ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map();
+#else
+ using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
+#endif
+
+ fixed (void* destinationPointer = &destination)
+ {
+ MemoryHelper.Copy(
+ source: resource.Pointer,
+ destination: destinationPointer,
+ sourceElementOffset: 0,
+ destinationElementOffset: 0,
+ sourceElementPitchInBytes: (uint)sizeof(T),
+ destinationElementPitchInBytes: (uint)sizeof(T),
+ count: (uint)count);
+ }
}
}
}
@@ -104,40 +105,41 @@ public abstract class StructuredBuffer : Buffer
///
internal override unsafe void CopyTo(Buffer destination, int sourceOffset, int destinationOffset, int count)
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
-
- ThrowIfDisposed();
-
- destination.ThrowIfDeviceMismatch(GraphicsDevice);
- destination.ThrowIfDisposed();
-
- Guard.IsBetweenOrEqualTo(count, 0, Length);
- Guard.IsBetweenOrEqualTo(count, 0, destination.Length);
- Guard.IsInRange(sourceOffset, 0, Length);
- Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset));
- Guard.IsInRange(destinationOffset, 0, destination.Length);
- Guard.IsLessThanOrEqualTo(destinationOffset + count, destination.Length, nameof(destinationOffset));
-
- if (!destination.IsPaddingPresent)
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (GetReferenceTracker().GetLease())
+ using (destination.GetReferenceTracker().GetLease())
{
- nint sourceByteOffset = (nint)sourceOffset * sizeof(T);
- nint destinationByteOffset = (nint)destinationOffset * sizeof(T);
- nint byteLength = count * sizeof(T);
+ GraphicsDevice.ThrowIfDeviceLost();
- // Directly copy the input buffer
- using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
+ destination.ThrowIfDeviceMismatch(GraphicsDevice);
- copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(
- destination.D3D12Resource,
- (ulong)destinationByteOffset,
- D3D12Resource,
- (ulong)sourceByteOffset,
- (ulong)byteLength);
+ Guard.IsBetweenOrEqualTo(count, 0, Length);
+ Guard.IsBetweenOrEqualTo(count, 0, destination.Length);
+ Guard.IsInRange(sourceOffset, 0, Length);
+ Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset));
+ Guard.IsInRange(destinationOffset, 0, destination.Length);
+ Guard.IsLessThanOrEqualTo(destinationOffset + count, destination.Length, nameof(destinationOffset));
- copyCommandList.ExecuteAndWaitForCompletion();
+ if (!destination.IsPaddingPresent)
+ {
+ nint sourceByteOffset = (nint)sourceOffset * sizeof(T);
+ nint destinationByteOffset = (nint)destinationOffset * sizeof(T);
+ nint byteLength = count * sizeof(T);
+
+ // Directly copy the input buffer
+ using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
+
+ copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(
+ destination.D3D12Resource,
+ (ulong)destinationByteOffset,
+ D3D12Resource,
+ (ulong)sourceByteOffset,
+ (ulong)byteLength);
+
+ copyCommandList.ExecuteAndWaitForCompletion();
+ }
+ else CopyToWithCpuBuffer(destination, sourceOffset, destinationOffset, count);
}
- else CopyToWithCpuBuffer(destination, sourceOffset, destinationOffset, count);
}
///
@@ -149,114 +151,116 @@ public abstract class StructuredBuffer : Buffer
/// The number of items to read.
internal unsafe void CopyTo(ReadBackBuffer destination, int sourceOffset, int destinationOffset, int count)
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
-
- ThrowIfDisposed();
-
- destination.ThrowIfDeviceMismatch(GraphicsDevice);
- destination.ThrowIfDisposed();
-
- Guard.IsBetweenOrEqualTo(count, 0, Length);
- Guard.IsBetweenOrEqualTo(count, 0, destination.Length);
- Guard.IsInRange(sourceOffset, 0, Length);
- Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset));
- Guard.IsInRange(destinationOffset, 0, destination.Length);
- Guard.IsLessThanOrEqualTo(destinationOffset + count, destination.Length, nameof(destinationOffset));
-
- if (GraphicsDevice.IsCacheCoherentUMA)
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (GetReferenceTracker().GetLease())
+ using (destination.GetReferenceTracker().GetLease())
{
- using ID3D12ResourceMap resource = D3D12Resource->Map();
+ GraphicsDevice.ThrowIfDeviceLost();
- MemoryHelper.Copy(
- source: resource.Pointer,
- destination: destination.MappedData,
- sourceElementOffset: (uint)sourceOffset,
- destinationElementOffset: (uint)destinationOffset,
- sourceElementPitchInBytes: (uint)sizeof(T),
- destinationElementPitchInBytes: (uint)sizeof(T),
- count: (uint)count);
- }
- else
- {
- ulong byteDestinationOffset = (uint)destinationOffset * (uint)sizeof(T);
- ulong byteOffset = (uint)sourceOffset * (uint)sizeof(T);
- ulong byteLength = (uint)count * (uint)sizeof(T);
+ destination.ThrowIfDeviceMismatch(GraphicsDevice);
- using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
+ Guard.IsBetweenOrEqualTo(count, 0, Length);
+ Guard.IsBetweenOrEqualTo(count, 0, destination.Length);
+ Guard.IsInRange(sourceOffset, 0, Length);
+ Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset));
+ Guard.IsInRange(destinationOffset, 0, destination.Length);
+ Guard.IsLessThanOrEqualTo(destinationOffset + count, destination.Length, nameof(destinationOffset));
- copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(destination.D3D12Resource, byteDestinationOffset, D3D12Resource, byteOffset, byteLength);
- copyCommandList.ExecuteAndWaitForCompletion();
+ if (GraphicsDevice.IsCacheCoherentUMA)
+ {
+ using ID3D12ResourceMap resource = D3D12Resource->Map();
+
+ MemoryHelper.Copy(
+ source: resource.Pointer,
+ destination: destination.MappedData,
+ sourceElementOffset: (uint)sourceOffset,
+ destinationElementOffset: (uint)destinationOffset,
+ sourceElementPitchInBytes: (uint)sizeof(T),
+ destinationElementPitchInBytes: (uint)sizeof(T),
+ count: (uint)count);
+ }
+ else
+ {
+ ulong byteDestinationOffset = (uint)destinationOffset * (uint)sizeof(T);
+ ulong byteOffset = (uint)sourceOffset * (uint)sizeof(T);
+ ulong byteLength = (uint)count * (uint)sizeof(T);
+
+ using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
+
+ copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(destination.D3D12Resource, byteDestinationOffset, D3D12Resource, byteOffset, byteLength);
+ copyCommandList.ExecuteAndWaitForCompletion();
+ }
}
}
///
internal override unsafe void CopyFrom(ref T source, int offset, int length)
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
-
- ThrowIfDisposed();
-
- Guard.IsBetweenOrEqualTo(length, 0, Length);
- Guard.IsInRange(offset, 0, Length);
- Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset));
-
- if (GraphicsDevice.IsCacheCoherentUMA)
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (GetReferenceTracker().GetLease())
{
- using ID3D12ResourceMap resource = D3D12Resource->Map();
+ GraphicsDevice.ThrowIfDeviceLost();
- fixed (void* sourcePointer = &source)
+ Guard.IsBetweenOrEqualTo(length, 0, Length);
+ Guard.IsInRange(offset, 0, Length);
+ Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset));
+
+ if (GraphicsDevice.IsCacheCoherentUMA)
{
- MemoryHelper.Copy(
- source: sourcePointer,
- destination: resource.Pointer,
- sourceElementOffset: 0,
- destinationElementOffset: (uint)offset,
- sourceElementPitchInBytes: (uint)sizeof(T),
- destinationElementPitchInBytes: (uint)sizeof(T),
- count: (uint)length);
+ using ID3D12ResourceMap resource = D3D12Resource->Map();
+
+ fixed (void* sourcePointer = &source)
+ {
+ MemoryHelper.Copy(
+ source: sourcePointer,
+ destination: resource.Pointer,
+ sourceElementOffset: 0,
+ destinationElementOffset: (uint)offset,
+ sourceElementPitchInBytes: (uint)sizeof(T),
+ destinationElementPitchInBytes: (uint)sizeof(T),
+ count: (uint)length);
+ }
}
- }
- else
- {
- nint byteOffset = (nint)offset * sizeof(T);
- nint byteLength = length * sizeof(T);
-
-#if NET6_0_OR_GREATER
- using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.Upload, AllocationMode.Default, (ulong)byteLength);
-#else
- using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
- ResourceType.Upload,
- (ulong)byteLength,
- GraphicsDevice.IsCacheCoherentUMA);
-#endif
-
-#if NET6_0_OR_GREATER
- using (ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map())
-#else
- using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
-#endif
- fixed (void* sourcePointer = &source)
+ else
{
- MemoryHelper.Copy(
- source: sourcePointer,
- destination: resource.Pointer,
- sourceElementOffset: 0,
- destinationElementOffset: 0,
- sourceElementPitchInBytes: (uint)sizeof(T),
- destinationElementPitchInBytes: (uint)sizeof(T),
- count: (uint)length);
- }
-
- using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
+ nint byteOffset = (nint)offset * sizeof(T);
+ nint byteLength = length * sizeof(T);
#if NET6_0_OR_GREATER
- copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, (ulong)byteOffset, allocation.Get()->GetResource(), 0, (ulong)byteLength);
+ using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.Upload, AllocationMode.Default, (ulong)byteLength);
#else
- copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, (ulong)byteOffset, d3D12Resource.Get(), 0, (ulong)byteLength);
+ using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
+ ResourceType.Upload,
+ (ulong)byteLength,
+ GraphicsDevice.IsCacheCoherentUMA);
#endif
- copyCommandList.ExecuteAndWaitForCompletion();
+
+#if NET6_0_OR_GREATER
+ using (ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map())
+#else
+ using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
+#endif
+ fixed (void* sourcePointer = &source)
+ {
+ MemoryHelper.Copy(
+ source: sourcePointer,
+ destination: resource.Pointer,
+ sourceElementOffset: 0,
+ destinationElementOffset: 0,
+ sourceElementPitchInBytes: (uint)sizeof(T),
+ destinationElementPitchInBytes: (uint)sizeof(T),
+ count: (uint)length);
+ }
+
+ using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
+
+#if NET6_0_OR_GREATER
+ copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, (ulong)byteOffset, allocation.Get()->GetResource(), 0, (ulong)byteLength);
+#else
+ copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, (ulong)byteOffset, d3D12Resource.Get(), 0, (ulong)byteLength);
+#endif
+ copyCommandList.ExecuteAndWaitForCompletion();
+ }
}
}
@@ -269,44 +273,45 @@ public abstract class StructuredBuffer : Buffer
/// The number of items to read.
internal unsafe void CopyFrom(UploadBuffer source, int sourceOffset, int destinationOffset, int count)
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
-
- ThrowIfDisposed();
-
- source.ThrowIfDeviceMismatch(GraphicsDevice);
- source.ThrowIfDisposed();
-
- Guard.IsBetweenOrEqualTo(count, 0, Length);
- Guard.IsBetweenOrEqualTo(count, 0, source.Length);
- Guard.IsInRange(sourceOffset, 0, source.Length);
- Guard.IsLessThanOrEqualTo(sourceOffset + count, source.Length, nameof(sourceOffset));
- Guard.IsInRange(destinationOffset, 0, Length);
- Guard.IsLessThanOrEqualTo(destinationOffset + count, Length, nameof(destinationOffset));
-
- if (GraphicsDevice.IsCacheCoherentUMA)
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (GetReferenceTracker().GetLease())
+ using (source.GetReferenceTracker().GetLease())
{
- using ID3D12ResourceMap resource = D3D12Resource->Map();
+ GraphicsDevice.ThrowIfDeviceLost();
- MemoryHelper.Copy(
- source: source.MappedData,
- destination: resource.Pointer,
- sourceElementOffset: (uint)sourceOffset,
- destinationElementOffset: (uint)destinationOffset,
- sourceElementPitchInBytes: (uint)sizeof(T),
- destinationElementPitchInBytes: (uint)sizeof(T),
- count: (uint)count);
- }
- else
- {
- ulong byteSourceOffset = (uint)sourceOffset * (uint)sizeof(T);
- ulong byteOffset = (uint)destinationOffset * (uint)sizeof(T);
- ulong byteLength = (uint)count * (uint)sizeof(T);
+ source.ThrowIfDeviceMismatch(GraphicsDevice);
- using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
+ Guard.IsBetweenOrEqualTo(count, 0, Length);
+ Guard.IsBetweenOrEqualTo(count, 0, source.Length);
+ Guard.IsInRange(sourceOffset, 0, source.Length);
+ Guard.IsLessThanOrEqualTo(sourceOffset + count, source.Length, nameof(sourceOffset));
+ Guard.IsInRange(destinationOffset, 0, Length);
+ Guard.IsLessThanOrEqualTo(destinationOffset + count, Length, nameof(destinationOffset));
- copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, byteOffset, source.D3D12Resource, byteSourceOffset, byteLength);
- copyCommandList.ExecuteAndWaitForCompletion();
+ if (GraphicsDevice.IsCacheCoherentUMA)
+ {
+ using ID3D12ResourceMap resource = D3D12Resource->Map();
+
+ MemoryHelper.Copy(
+ source: source.MappedData,
+ destination: resource.Pointer,
+ sourceElementOffset: (uint)sourceOffset,
+ destinationElementOffset: (uint)destinationOffset,
+ sourceElementPitchInBytes: (uint)sizeof(T),
+ destinationElementPitchInBytes: (uint)sizeof(T),
+ count: (uint)count);
+ }
+ else
+ {
+ ulong byteSourceOffset = (uint)sourceOffset * (uint)sizeof(T);
+ ulong byteOffset = (uint)destinationOffset * (uint)sizeof(T);
+ ulong byteLength = (uint)count * (uint)sizeof(T);
+
+ using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
+
+ copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, byteOffset, source.D3D12Resource, byteSourceOffset, byteLength);
+ copyCommandList.ExecuteAndWaitForCompletion();
+ }
}
}
}
diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs
index e6190b93..b351356e 100644
--- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs
@@ -26,9 +26,14 @@ namespace ComputeSharp.Resources;
/// A representing a typed 2D texture stored on GPU memory.
///
/// The type of items stored on the texture.
-public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, GraphicsResourceHelper.IGraphicsResource
+public unsafe abstract class Texture2D : ReferenceTracker.ITrackedObject, IDisposable, IGraphicsResource, GraphicsResourceHelper.IGraphicsResource
where T : unmanaged
{
+ ///
+ /// The owning object for the current instance.
+ ///
+ private ReferenceTracker referenceTracker;
+
#if NET6_0_OR_GREATER
///
/// The instance used to retrieve .
@@ -72,67 +77,79 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra
/// The format support for the current texture type.
private protected Texture2D(GraphicsDevice device, int width, int height, ResourceType resourceType, AllocationMode allocationMode, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport)
{
- device.ThrowIfDisposed();
- device.ThrowIfDeviceLost();
+ this.referenceTracker = new ReferenceTracker(this);
- Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
- Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
-
- if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), d3D12FormatSupport))
+ using (device.GetReferenceTracker().GetLease())
{
- UnsupportedTextureTypeException.ThrowForTexture2D();
- }
+ device.ThrowIfDeviceLost();
- GraphicsDevice = device;
+ Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
+ Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
+
+ if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), d3D12FormatSupport))
+ {
+ UnsupportedTextureTypeException.ThrowForTexture2D();
+ }
+
+ GraphicsDevice = device;
#if NET6_0_OR_GREATER
- this.allocation = device.Allocator->CreateResource(
- device.Pool,
- resourceType,
- allocationMode,
- DXGIFormatHelper.GetForType(),
- (uint)width,
- (uint)height,
- out this.d3D12ResourceState);
+ this.allocation = device.Allocator->CreateResource(
+ device.Pool,
+ resourceType,
+ allocationMode,
+ DXGIFormatHelper.GetForType(),
+ (uint)width,
+ (uint)height,
+ out this.d3D12ResourceState);
- this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource());
+ this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource());
#else
- this.d3D12Resource = device.D3D12Device->CreateCommittedResource(
- resourceType,
- DXGIFormatHelper.GetForType(),
- (uint)width,
- (uint)height,
- device.IsCacheCoherentUMA,
- out this.d3D12ResourceState);
+ this.d3D12Resource = device.D3D12Device->CreateCommittedResource(
+ resourceType,
+ DXGIFormatHelper.GetForType(),
+ (uint)width,
+ (uint)height,
+ device.IsCacheCoherentUMA,
+ out this.d3D12ResourceState);
#endif
- this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON
- ? D3D12_COMMAND_LIST_TYPE_COPY
- : D3D12_COMMAND_LIST_TYPE_COMPUTE;
+ this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON
+ ? D3D12_COMMAND_LIST_TYPE_COPY
+ : D3D12_COMMAND_LIST_TYPE_COMPUTE;
- device.D3D12Device->GetCopyableFootprint(
- DXGIFormatHelper.GetForType(),
- (uint)width,
- (uint)height,
- out this.d3D12PlacedSubresourceFootprint,
- out _,
- out _);
+ device.D3D12Device->GetCopyableFootprint(
+ DXGIFormatHelper.GetForType(),
+ (uint)width,
+ (uint)height,
+ out this.d3D12PlacedSubresourceFootprint,
+ out _,
+ out _);
- device.RegisterAllocatedResource();
- device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
+ device.RegisterAllocatedResource();
+ device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
- switch (resourceType)
- {
- case ResourceType.ReadOnly:
- device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
- break;
- case ResourceType.ReadWrite:
- device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
- device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
- break;
+ switch (resourceType)
+ {
+ case ResourceType.ReadOnly:
+ device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
+ break;
+ case ResourceType.ReadWrite:
+ device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
+ device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
+ break;
+ }
+
+ this.d3D12Resource.Get()->SetName(this);
}
+ }
- this.d3D12Resource.Get()->SetName(this);
+ ///
+ /// Releases unmanaged resources for the current instance.
+ ///
+ ~Texture2D()
+ {
+ this.referenceTracker.Dispose();
}
///
@@ -169,20 +186,20 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra
/// The height of the memory area to copy.
internal void CopyTo(ref T destination, int size, int sourceOffsetX, int sourceOffsetY, int width, int height)
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (this.referenceTracker.GetLease())
+ {
+ GraphicsDevice.ThrowIfDeviceLost();
- ThrowIfDisposed();
+ Guard.IsInRange(sourceOffsetX, 0, Width);
+ Guard.IsInRange(sourceOffsetY, 0, Height);
+ Guard.IsBetweenOrEqualTo(width, 1, Width);
+ Guard.IsBetweenOrEqualTo(height, 1, Height);
+ Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
+ Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
+ Guard.IsGreaterThanOrEqualTo(size, (nint)width * height);
- Guard.IsInRange(sourceOffsetX, 0, Width);
- Guard.IsInRange(sourceOffsetY, 0, Height);
- Guard.IsBetweenOrEqualTo(width, 1, Width);
- Guard.IsBetweenOrEqualTo(height, 1, Height);
- Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
- Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
- Guard.IsGreaterThanOrEqualTo(size, (nint)width * height);
-
- GraphicsDevice.D3D12Device->GetCopyableFootprint(
+ GraphicsDevice.D3D12Device->GetCopyableFootprint(
DXGIFormatHelper.GetForType(),
(uint)width,
(uint)height,
@@ -191,59 +208,60 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra
out ulong totalSizeInBytes);
#if NET6_0_OR_GREATER
- using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(
- GraphicsDevice.Pool,
- ResourceType.ReadBack,
- AllocationMode.Default,
- totalSizeInBytes);
+ using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(
+ GraphicsDevice.Pool,
+ ResourceType.ReadBack,
+ AllocationMode.Default,
+ totalSizeInBytes);
- using ComPtr d3D12Resource = new(allocation.Get()->GetResource());
+ using ComPtr d3D12Resource = new(allocation.Get()->GetResource());
#else
- using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
- ResourceType.ReadBack,
- totalSizeInBytes,
- GraphicsDevice.IsCacheCoherentUMA);
+ using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
+ ResourceType.ReadBack,
+ totalSizeInBytes,
+ GraphicsDevice.IsCacheCoherentUMA);
#endif
- using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType))
- {
- if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType))
{
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
+ }
+
+ copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
+ d3D12ResourceDestination: d3D12Resource.Get(),
+ &d3D12PlacedSubresourceFootprintDestination,
+ destinationX: 0,
+ destinationY: 0,
+ destinationZ: 0,
+ d3D12ResourceSource: D3D12Resource,
+ sourceX: (uint)sourceOffsetX,
+ sourceY: (uint)sourceOffsetY,
+ sourceZ: 0,
+ (uint)width,
+ (uint)height,
+ depth: 1);
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
+ }
+
+ copyCommandList.ExecuteAndWaitForCompletion();
}
- copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
- d3D12ResourceDestination: d3D12Resource.Get(),
- &d3D12PlacedSubresourceFootprintDestination,
- destinationX: 0,
- destinationY: 0,
- destinationZ: 0,
- d3D12ResourceSource: D3D12Resource,
- sourceX: (uint)sourceOffsetX,
- sourceY: (uint)sourceOffsetY,
- sourceZ: 0,
- (uint)width,
- (uint)height,
- depth: 1);
+ using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
- if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ fixed (void* destinationPointer = &destination)
{
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
+ MemoryHelper.Copy(
+ resource.Pointer,
+ (uint)height,
+ rowSizeInBytes,
+ d3D12PlacedSubresourceFootprintDestination.Footprint.RowPitch,
+ destinationPointer);
}
-
- copyCommandList.ExecuteAndWaitForCompletion();
- }
-
- using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
-
- fixed (void* destinationPointer = &destination)
- {
- MemoryHelper.Copy(
- resource.Pointer,
- (uint)height,
- rowSizeInBytes,
- d3D12PlacedSubresourceFootprintDestination.Footprint.RowPitch,
- destinationPointer);
}
}
@@ -259,61 +277,62 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra
/// The height of the memory area to copy.
internal void CopyTo(Texture2D destination, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height)
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
-
- ThrowIfDisposed();
-
- destination.ThrowIfDeviceMismatch(GraphicsDevice);
- destination.ThrowIfDisposed();
-
- Guard.IsInRange(sourceOffsetX, 0, Width);
- Guard.IsInRange(sourceOffsetY, 0, Height);
- Guard.IsInRange(destinationOffsetX, 0, destination.Width);
- Guard.IsInRange(destinationOffsetY, 0, destination.Height);
- Guard.IsBetweenOrEqualTo(width, 1, Width);
- Guard.IsBetweenOrEqualTo(height, 1, Height);
- Guard.IsBetweenOrEqualTo(width, 1, destination.Width);
- Guard.IsBetweenOrEqualTo(height, 1, destination.Height);
- Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX));
- Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY));
- Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
- Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
-
- D3D12_COMMAND_LIST_TYPE d3D12CommandListType =
- this.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE ||
- destination.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE
- ? D3D12_COMMAND_LIST_TYPE_COMPUTE
- : D3D12_COMMAND_LIST_TYPE_COPY;
-
- using CommandList copyCommandList = new(GraphicsDevice, d3D12CommandListType);
-
- if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (this.referenceTracker.GetLease())
+ using (destination.referenceTracker.GetLease())
{
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(destination.D3D12Resource, destination.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
+ GraphicsDevice.ThrowIfDeviceLost();
+
+ destination.ThrowIfDeviceMismatch(GraphicsDevice);
+
+ Guard.IsInRange(sourceOffsetX, 0, Width);
+ Guard.IsInRange(sourceOffsetY, 0, Height);
+ Guard.IsInRange(destinationOffsetX, 0, destination.Width);
+ Guard.IsInRange(destinationOffsetY, 0, destination.Height);
+ Guard.IsBetweenOrEqualTo(width, 1, Width);
+ Guard.IsBetweenOrEqualTo(height, 1, Height);
+ Guard.IsBetweenOrEqualTo(width, 1, destination.Width);
+ Guard.IsBetweenOrEqualTo(height, 1, destination.Height);
+ Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX));
+ Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY));
+ Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
+ Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
+
+ D3D12_COMMAND_LIST_TYPE d3D12CommandListType =
+ this.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE ||
+ destination.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE
+ ? D3D12_COMMAND_LIST_TYPE_COMPUTE
+ : D3D12_COMMAND_LIST_TYPE_COPY;
+
+ using CommandList copyCommandList = new(GraphicsDevice, d3D12CommandListType);
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(destination.D3D12Resource, destination.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
+ }
+
+ copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
+ d3D12ResourceDestination: destination.D3D12Resource,
+ (uint)destinationOffsetX,
+ (uint)destinationOffsetY,
+ destinationZ: 0,
+ d3D12ResourceSource: D3D12Resource,
+ (uint)sourceOffsetX,
+ (uint)sourceOffsetY,
+ sourceZ: 0,
+ (uint)width,
+ (uint)height,
+ 1);
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(destination.D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, destination.d3D12ResourceState);
+ }
+
+ copyCommandList.ExecuteAndWaitForCompletion();
}
-
- copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
- d3D12ResourceDestination: destination.D3D12Resource,
- (uint)destinationOffsetX,
- (uint)destinationOffsetY,
- destinationZ: 0,
- d3D12ResourceSource: D3D12Resource,
- (uint)sourceOffsetX,
- (uint)sourceOffsetY,
- sourceZ: 0,
- (uint)width,
- (uint)height,
- 1);
-
- if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
- {
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(destination.D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, destination.d3D12ResourceState);
- }
-
- copyCommandList.ExecuteAndWaitForCompletion();
}
///
@@ -328,57 +347,58 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra
/// The height of the memory area to copy.
internal void CopyTo(ReadBackTexture2D destination, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height)
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
-
- ThrowIfDisposed();
-
- destination.ThrowIfDeviceMismatch(GraphicsDevice);
- destination.ThrowIfDisposed();
-
- Guard.IsInRange(sourceOffsetX, 0, Width);
- Guard.IsInRange(sourceOffsetY, 0, Height);
- Guard.IsInRange(destinationOffsetX, 0, destination.Width);
- Guard.IsInRange(destinationOffsetY, 0, destination.Height);
- Guard.IsBetweenOrEqualTo(width, 1, Width);
- Guard.IsBetweenOrEqualTo(height, 1, Height);
- Guard.IsBetweenOrEqualTo(width, 1, destination.Width);
- Guard.IsBetweenOrEqualTo(height, 1, destination.Height);
- Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX));
- Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY));
- Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
- Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
-
- using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
-
- if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (this.referenceTracker.GetLease())
+ using (destination.GetReferenceTracker().GetLease())
{
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
- }
+ GraphicsDevice.ThrowIfDeviceLost();
- fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintDestination = &destination.D3D12PlacedSubresourceFootprint)
- {
- copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
- d3D12ResourceDestination: destination.D3D12Resource,
- d3D12PlacedSubresourceFootprintDestination,
- (uint)destinationOffsetX,
- (uint)destinationOffsetY,
- destinationZ: 0,
- d3D12ResourceSource: D3D12Resource,
- (uint)sourceOffsetX,
- (uint)sourceOffsetY,
- sourceZ: 0,
- (uint)width,
- (uint)height,
- 1);
- }
+ destination.ThrowIfDeviceMismatch(GraphicsDevice);
- if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
- {
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
- }
+ Guard.IsInRange(sourceOffsetX, 0, Width);
+ Guard.IsInRange(sourceOffsetY, 0, Height);
+ Guard.IsInRange(destinationOffsetX, 0, destination.Width);
+ Guard.IsInRange(destinationOffsetY, 0, destination.Height);
+ Guard.IsBetweenOrEqualTo(width, 1, Width);
+ Guard.IsBetweenOrEqualTo(height, 1, Height);
+ Guard.IsBetweenOrEqualTo(width, 1, destination.Width);
+ Guard.IsBetweenOrEqualTo(height, 1, destination.Height);
+ Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX));
+ Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY));
+ Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
+ Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
- copyCommandList.ExecuteAndWaitForCompletion();
+ using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
+ }
+
+ fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintDestination = &destination.D3D12PlacedSubresourceFootprint)
+ {
+ copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
+ d3D12ResourceDestination: destination.D3D12Resource,
+ d3D12PlacedSubresourceFootprintDestination,
+ (uint)destinationOffsetX,
+ (uint)destinationOffsetY,
+ destinationZ: 0,
+ d3D12ResourceSource: D3D12Resource,
+ (uint)sourceOffsetX,
+ (uint)sourceOffsetY,
+ sourceZ: 0,
+ (uint)width,
+ (uint)height,
+ 1);
+ }
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
+ }
+
+ copyCommandList.ExecuteAndWaitForCompletion();
+ }
}
///
@@ -392,80 +412,81 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra
/// The height of the memory area to write to.
internal void CopyFrom(ref T source, int size, int destinationOffsetX, int destinationOffsetY, int width, int height)
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (this.referenceTracker.GetLease())
+ {
+ GraphicsDevice.ThrowIfDeviceLost();
- ThrowIfDisposed();
+ Guard.IsInRange(destinationOffsetX, 0, Width);
+ Guard.IsInRange(destinationOffsetY, 0, Height);
+ Guard.IsBetweenOrEqualTo(width, 1, Width);
+ Guard.IsBetweenOrEqualTo(height, 1, Height);
+ Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX));
+ Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY));
+ Guard.IsGreaterThanOrEqualTo(size, (nint)width * height);
- Guard.IsInRange(destinationOffsetX, 0, Width);
- Guard.IsInRange(destinationOffsetY, 0, Height);
- Guard.IsBetweenOrEqualTo(width, 1, Width);
- Guard.IsBetweenOrEqualTo(height, 1, Height);
- Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX));
- Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY));
- Guard.IsGreaterThanOrEqualTo(size, (nint)width * height);
-
- GraphicsDevice.D3D12Device->GetCopyableFootprint(
- DXGIFormatHelper.GetForType(),
- (uint)width,
- (uint)height,
- out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintSource,
- out ulong rowSizeInBytes,
- out ulong totalSizeInBytes);
+ GraphicsDevice.D3D12Device->GetCopyableFootprint(
+ DXGIFormatHelper.GetForType(),
+ (uint)width,
+ (uint)height,
+ out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintSource,
+ out ulong rowSizeInBytes,
+ out ulong totalSizeInBytes);
#if NET6_0_OR_GREATER
- using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(
- GraphicsDevice.Pool,
- ResourceType.Upload,
- AllocationMode.Default,
- totalSizeInBytes);
+ using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(
+ GraphicsDevice.Pool,
+ ResourceType.Upload,
+ AllocationMode.Default,
+ totalSizeInBytes);
- using ComPtr d3D12Resource = new(allocation.Get()->GetResource());
+ using ComPtr d3D12Resource = new(allocation.Get()->GetResource());
#else
- using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
- ResourceType.Upload,
- totalSizeInBytes,
- GraphicsDevice.IsCacheCoherentUMA);
+ using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
+ ResourceType.Upload,
+ totalSizeInBytes,
+ GraphicsDevice.IsCacheCoherentUMA);
#endif
- using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
- fixed (void* sourcePointer = &source)
- {
- MemoryHelper.Copy(
- sourcePointer,
- resource.Pointer,
+ using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
+ fixed (void* sourcePointer = &source)
+ {
+ MemoryHelper.Copy(
+ sourcePointer,
+ resource.Pointer,
+ (uint)height,
+ rowSizeInBytes,
+ d3D12PlacedSubresourceFootprintSource.Footprint.RowPitch);
+ }
+
+ using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
+ }
+
+ copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
+ d3D12ResourceDestination: D3D12Resource,
+ destinationX: (uint)destinationOffsetX,
+ destinationY: (uint)destinationOffsetY,
+ destinationZ: 0,
+ d3D12ResourceSource: d3D12Resource.Get(),
+ &d3D12PlacedSubresourceFootprintSource,
+ sourceX: 0,
+ sourceY: 0,
+ sourceZ: 0,
+ (uint)width,
(uint)height,
- rowSizeInBytes,
- d3D12PlacedSubresourceFootprintSource.Footprint.RowPitch);
+ depth: 1);
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
+ }
+
+ copyCommandList.ExecuteAndWaitForCompletion();
}
-
- using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
-
- if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
- {
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
- }
-
- copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
- d3D12ResourceDestination: D3D12Resource,
- destinationX: (uint)destinationOffsetX,
- destinationY: (uint)destinationOffsetY,
- destinationZ: 0,
- d3D12ResourceSource: d3D12Resource.Get(),
- &d3D12PlacedSubresourceFootprintSource,
- sourceX: 0,
- sourceY: 0,
- sourceZ: 0,
- (uint)width,
- (uint)height,
- depth: 1);
-
- if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
- {
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
- }
-
- copyCommandList.ExecuteAndWaitForCompletion();
}
///
@@ -480,61 +501,83 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra
/// The height of the memory area to write to.
internal void CopyFrom(UploadTexture2D source, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height)
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
-
- ThrowIfDisposed();
-
- source.ThrowIfDeviceMismatch(GraphicsDevice);
- source.ThrowIfDisposed();
-
- Guard.IsInRange(sourceOffsetX, 0, source.Width);
- Guard.IsInRange(sourceOffsetY, 0, source.Height);
- Guard.IsInRange(destinationOffsetX, 0, Width);
- Guard.IsInRange(destinationOffsetY, 0, Height);
- Guard.IsBetweenOrEqualTo(width, 1, Width);
- Guard.IsBetweenOrEqualTo(height, 1, Height);
- Guard.IsBetweenOrEqualTo(width, 1, source.Width);
- Guard.IsBetweenOrEqualTo(height, 1, source.Height);
- Guard.IsLessThanOrEqualTo(sourceOffsetX + width, source.Width, nameof(sourceOffsetX));
- Guard.IsLessThanOrEqualTo(sourceOffsetY + height, source.Height, nameof(sourceOffsetY));
- Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX));
- Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY));
-
- using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
-
- if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (this.referenceTracker.GetLease())
+ using (source.GetReferenceTracker().GetLease())
{
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
- }
+ GraphicsDevice.ThrowIfDeviceLost();
- fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintSource = &source.D3D12PlacedSubresourceFootprint)
- {
- copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
- d3D12ResourceDestination: D3D12Resource,
- (uint)destinationOffsetX,
- (uint)destinationOffsetY,
- destinationZ: 0,
- d3D12ResourceSource: source.D3D12Resource,
- d3D12PlacedSubresourceFootprintSource,
- (uint)sourceOffsetX,
- (uint)sourceOffsetY,
- sourceZ: 0,
- (uint)width,
- (uint)height,
- depth: 1);
- }
+ source.ThrowIfDeviceMismatch(GraphicsDevice);
- if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
- {
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
- }
+ Guard.IsInRange(sourceOffsetX, 0, source.Width);
+ Guard.IsInRange(sourceOffsetY, 0, source.Height);
+ Guard.IsInRange(destinationOffsetX, 0, Width);
+ Guard.IsInRange(destinationOffsetY, 0, Height);
+ Guard.IsBetweenOrEqualTo(width, 1, Width);
+ Guard.IsBetweenOrEqualTo(height, 1, Height);
+ Guard.IsBetweenOrEqualTo(width, 1, source.Width);
+ Guard.IsBetweenOrEqualTo(height, 1, source.Height);
+ Guard.IsLessThanOrEqualTo(sourceOffsetX + width, source.Width, nameof(sourceOffsetX));
+ Guard.IsLessThanOrEqualTo(sourceOffsetY + height, source.Height, nameof(sourceOffsetY));
+ Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX));
+ Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY));
- copyCommandList.ExecuteAndWaitForCompletion();
+ using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
+ }
+
+ fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintSource = &source.D3D12PlacedSubresourceFootprint)
+ {
+ copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
+ d3D12ResourceDestination: D3D12Resource,
+ (uint)destinationOffsetX,
+ (uint)destinationOffsetY,
+ destinationZ: 0,
+ d3D12ResourceSource: source.D3D12Resource,
+ d3D12PlacedSubresourceFootprintSource,
+ (uint)sourceOffsetX,
+ (uint)sourceOffsetY,
+ sourceZ: 0,
+ (uint)width,
+ (uint)height,
+ depth: 1);
+ }
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
+ }
+
+ copyCommandList.ExecuteAndWaitForCompletion();
+ }
}
///
- protected override void OnDispose()
+ public virtual void Dispose()
+ {
+ this.referenceTracker.Dispose();
+
+ GC.SuppressFinalize(this);
+ }
+
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal ref ReferenceTracker GetReferenceTracker()
+ {
+ return ref this.referenceTracker;
+ }
+
+ ///
+ ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
+ {
+ return ref this.referenceTracker;
+ }
+
+ ///
+ void ReferenceTracker.ITrackedObject.DangerousRelease()
{
this.d3D12Resource.Dispose();
#if NET6_0_OR_GREATER
@@ -582,46 +625,54 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra
///
internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized)
{
- ThrowIfDisposed();
- ThrowIfDeviceMismatch(device);
+ using (this.referenceTracker.GetLease())
+ {
+ ThrowIfDeviceMismatch(device);
- isNormalized = DXGIFormatHelper.IsNormalizedType();
+ isNormalized = DXGIFormatHelper.IsNormalizedType();
- return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
+ return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
+ }
}
///
internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device)
{
- ThrowIfDisposed();
- ThrowIfDeviceMismatch(device);
+ using (this.referenceTracker.GetLease())
+ {
+ ThrowIfDeviceMismatch(device);
- return D3D12Resource;
+ return D3D12Resource;
+ }
}
///
internal (D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource)
{
- ThrowIfDisposed();
- ThrowIfDeviceMismatch(device);
+ using (this.referenceTracker.GetLease())
+ {
+ ThrowIfDeviceMismatch(device);
- D3D12_RESOURCE_STATES d3D12ResourceStatesBefore = this.d3D12ResourceState;
- D3D12_RESOURCE_STATES d3D12ResourceStatesAfter = ResourceStateHelper.GetD3D12ResourceStates(resourceState);
+ D3D12_RESOURCE_STATES d3D12ResourceStatesBefore = this.d3D12ResourceState;
+ D3D12_RESOURCE_STATES d3D12ResourceStatesAfter = ResourceStateHelper.GetD3D12ResourceStates(resourceState);
- this.d3D12ResourceState = d3D12ResourceStatesAfter;
+ this.d3D12ResourceState = d3D12ResourceStatesAfter;
- d3D12Resource = D3D12Resource;
+ d3D12Resource = D3D12Resource;
- return (d3D12ResourceStatesBefore, d3D12ResourceStatesAfter);
+ return (d3D12ResourceStatesBefore, d3D12ResourceStatesAfter);
+ }
}
///
D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device)
{
- ThrowIfDisposed();
- ThrowIfDeviceMismatch(device);
+ using (this.referenceTracker.GetLease())
+ {
+ ThrowIfDeviceMismatch(device);
- return D3D12GpuDescriptorHandle;
+ return D3D12GpuDescriptorHandle;
+ }
}
///
diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs
index b12dc576..e4f5635d 100644
--- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs
@@ -26,9 +26,14 @@ namespace ComputeSharp.Resources;
/// A representing a typed 3D texture stored on GPU memory.
///
/// The type of items stored on the texture.
-public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, GraphicsResourceHelper.IGraphicsResource
+public unsafe abstract class Texture3D : ReferenceTracker.ITrackedObject, IDisposable, IGraphicsResource, GraphicsResourceHelper.IGraphicsResource
where T : unmanaged
{
+ ///
+ /// The owning object for the current instance.
+ ///
+ private ReferenceTracker referenceTracker;
+
#if NET6_0_OR_GREATER
///
/// The instance used to retrieve .
@@ -73,71 +78,83 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra
/// The format support for the current texture type.
private protected Texture3D(GraphicsDevice device, int width, int height, int depth, ResourceType resourceType, AllocationMode allocationMode, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport)
{
- device.ThrowIfDisposed();
- device.ThrowIfDeviceLost();
+ this.referenceTracker = new ReferenceTracker(this);
- Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
- Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
- Guard.IsBetweenOrEqualTo(depth, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
-
- if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), d3D12FormatSupport))
+ using (device.GetReferenceTracker().GetLease())
{
- UnsupportedTextureTypeException.ThrowForTexture3D();
- }
+ device.ThrowIfDeviceLost();
- GraphicsDevice = device;
+ Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
+ Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
+ Guard.IsBetweenOrEqualTo(depth, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
+
+ if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), d3D12FormatSupport))
+ {
+ UnsupportedTextureTypeException.ThrowForTexture3D();
+ }
+
+ GraphicsDevice = device;
#if NET6_0_OR_GREATER
- this.allocation = device.Allocator->CreateResource(
- device.Pool,
- resourceType,
- allocationMode,
- DXGIFormatHelper.GetForType(),
- (uint)width,
- (uint)height,
- (ushort)depth,
- out this.d3D12ResourceState);
+ this.allocation = device.Allocator->CreateResource(
+ device.Pool,
+ resourceType,
+ allocationMode,
+ DXGIFormatHelper.GetForType(),
+ (uint)width,
+ (uint)height,
+ (ushort)depth,
+ out this.d3D12ResourceState);
- this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource());
+ this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource());
#else
- this.d3D12Resource = device.D3D12Device->CreateCommittedResource(
- resourceType,
- DXGIFormatHelper.GetForType(),
- (uint)width,
- (uint)height,
- (ushort)depth,
- device.IsCacheCoherentUMA,
- out this.d3D12ResourceState);
+ this.d3D12Resource = device.D3D12Device->CreateCommittedResource(
+ resourceType,
+ DXGIFormatHelper.GetForType(),
+ (uint)width,
+ (uint)height,
+ (ushort)depth,
+ device.IsCacheCoherentUMA,
+ out this.d3D12ResourceState);
#endif
- this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON
- ? D3D12_COMMAND_LIST_TYPE_COPY
- : D3D12_COMMAND_LIST_TYPE_COMPUTE;
+ this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON
+ ? D3D12_COMMAND_LIST_TYPE_COPY
+ : D3D12_COMMAND_LIST_TYPE_COMPUTE;
- device.D3D12Device->GetCopyableFootprint(
- DXGIFormatHelper.GetForType(),
- (uint)width,
- (uint)height,
- (ushort)depth,
- out this.d3D12PlacedSubresourceFootprint,
- out _,
- out _);
+ device.D3D12Device->GetCopyableFootprint(
+ DXGIFormatHelper.GetForType(),
+ (uint)width,
+ (uint)height,
+ (ushort)depth,
+ out this.d3D12PlacedSubresourceFootprint,
+ out _,
+ out _);
- device.RegisterAllocatedResource();
- device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
+ device.RegisterAllocatedResource();
+ device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
- switch (resourceType)
- {
- case ResourceType.ReadOnly:
- device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
- break;
- case ResourceType.ReadWrite:
- device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
- device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
- break;
+ switch (resourceType)
+ {
+ case ResourceType.ReadOnly:
+ device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
+ break;
+ case ResourceType.ReadWrite:
+ device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
+ device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
+ break;
+ }
+
+ this.d3D12Resource.Get()->SetName(this);
}
+ }
- this.d3D12Resource.Get()->SetName(this);
+ ///
+ /// Releases unmanaged resources for the current instance.
+ ///
+ ~Texture3D()
+ {
+ this.referenceTracker.Dispose();
}
///
@@ -181,87 +198,88 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra
/// The depth of the memory area to copy.
internal void CopyTo(ref T destination, int size, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth)
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
-
- ThrowIfDisposed();
-
- Guard.IsInRange(sourceOffsetX, 0, Width);
- Guard.IsInRange(sourceOffsetY, 0, Height);
- Guard.IsInRange(sourceOffsetZ, 0, Depth);
- Guard.IsBetweenOrEqualTo(width, 1, Width);
- Guard.IsBetweenOrEqualTo(height, 1, Height);
- Guard.IsBetweenOrEqualTo(depth, 1, Depth);
- Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
- Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
- Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ));
- Guard.IsGreaterThanOrEqualTo(size, (nint)width * height * depth);
-
- GraphicsDevice.D3D12Device->GetCopyableFootprint(
- DXGIFormatHelper.GetForType(),
- (uint)width,
- (uint)height,
- (ushort)depth,
- out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintDestination,
- out ulong rowSizeInBytes,
- out ulong totalSizeInBytes);
-
-#if NET6_0_OR_GREATER
- using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(
- GraphicsDevice.Pool,
- ResourceType.ReadBack,
- AllocationMode.Default,
- totalSizeInBytes);
-
- using ComPtr d3D12Resource = new(allocation.Get()->GetResource());
-#else
- using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
- ResourceType.ReadBack,
- totalSizeInBytes,
- GraphicsDevice.IsCacheCoherentUMA);
-#endif
-
- using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType))
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (this.referenceTracker.GetLease())
{
- if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
- {
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
- }
+ GraphicsDevice.ThrowIfDeviceLost();
- copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
- d3D12ResourceDestination: d3D12Resource.Get(),
- &d3D12PlacedSubresourceFootprintDestination,
- destinationX: 0,
- destinationY: 0,
- destinationZ: 0,
- d3D12ResourceSource: D3D12Resource,
- sourceX: (uint)sourceOffsetX,
- sourceY: (uint)sourceOffsetY,
- sourceZ: (ushort)sourceOffsetZ,
+ Guard.IsInRange(sourceOffsetX, 0, Width);
+ Guard.IsInRange(sourceOffsetY, 0, Height);
+ Guard.IsInRange(sourceOffsetZ, 0, Depth);
+ Guard.IsBetweenOrEqualTo(width, 1, Width);
+ Guard.IsBetweenOrEqualTo(height, 1, Height);
+ Guard.IsBetweenOrEqualTo(depth, 1, Depth);
+ Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
+ Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
+ Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ));
+ Guard.IsGreaterThanOrEqualTo(size, (nint)width * height * depth);
+
+ GraphicsDevice.D3D12Device->GetCopyableFootprint(
+ DXGIFormatHelper.GetForType(),
(uint)width,
(uint)height,
- (ushort)depth);
+ (ushort)depth,
+ out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintDestination,
+ out ulong rowSizeInBytes,
+ out ulong totalSizeInBytes);
- if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+#if NET6_0_OR_GREATER
+ using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(
+ GraphicsDevice.Pool,
+ ResourceType.ReadBack,
+ AllocationMode.Default,
+ totalSizeInBytes);
+
+ using ComPtr d3D12Resource = new(allocation.Get()->GetResource());
+#else
+ using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
+ ResourceType.ReadBack,
+ totalSizeInBytes,
+ GraphicsDevice.IsCacheCoherentUMA);
+#endif
+
+ using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType))
{
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
+ }
+
+ copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
+ d3D12ResourceDestination: d3D12Resource.Get(),
+ &d3D12PlacedSubresourceFootprintDestination,
+ destinationX: 0,
+ destinationY: 0,
+ destinationZ: 0,
+ d3D12ResourceSource: D3D12Resource,
+ sourceX: (uint)sourceOffsetX,
+ sourceY: (uint)sourceOffsetY,
+ sourceZ: (ushort)sourceOffsetZ,
+ (uint)width,
+ (uint)height,
+ (ushort)depth);
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
+ }
+
+ copyCommandList.ExecuteAndWaitForCompletion();
}
- copyCommandList.ExecuteAndWaitForCompletion();
- }
+ using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
- using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
-
- fixed (void* destinationPointer = &destination)
- {
- MemoryHelper.Copy(
- resource.Pointer,
- (uint)height,
- (uint)depth,
- rowSizeInBytes,
- d3D12PlacedSubresourceFootprintDestination.Footprint.RowPitch,
- d3D12PlacedSubresourceFootprintDestination.Footprint.RowPitch * (uint)height,
- destinationPointer);
+ fixed (void* destinationPointer = &destination)
+ {
+ MemoryHelper.Copy(
+ resource.Pointer,
+ (uint)height,
+ (uint)depth,
+ rowSizeInBytes,
+ d3D12PlacedSubresourceFootprintDestination.Footprint.RowPitch,
+ d3D12PlacedSubresourceFootprintDestination.Footprint.RowPitch * (uint)height,
+ destinationPointer);
+ }
}
}
@@ -280,67 +298,68 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra
/// The depth of the memory area to copy.
internal void CopyTo(Texture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth)
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
-
- ThrowIfDisposed();
-
- destination.ThrowIfDeviceMismatch(GraphicsDevice);
- destination.ThrowIfDisposed();
-
- Guard.IsInRange(sourceOffsetX, 0, Width);
- Guard.IsInRange(sourceOffsetY, 0, Height);
- Guard.IsInRange(sourceOffsetZ, 0, Depth);
- Guard.IsInRange(destinationOffsetX, 0, destination.Width);
- Guard.IsInRange(destinationOffsetY, 0, destination.Height);
- Guard.IsInRange(destinationOffsetZ, 0, destination.Depth);
- Guard.IsBetweenOrEqualTo(width, 1, Width);
- Guard.IsBetweenOrEqualTo(height, 1, Height);
- Guard.IsBetweenOrEqualTo(depth, 1, Depth);
- Guard.IsBetweenOrEqualTo(width, 1, destination.Width);
- Guard.IsBetweenOrEqualTo(height, 1, destination.Height);
- Guard.IsBetweenOrEqualTo(depth, 1, destination.Depth);
- Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX));
- Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY));
- Guard.IsBetweenOrEqualTo(destinationOffsetZ + depth, 1, destination.Depth, nameof(destinationOffsetZ));
- Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
- Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
- Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ));
-
- D3D12_COMMAND_LIST_TYPE d3D12CommandListType =
- this.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE ||
- destination.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE
- ? D3D12_COMMAND_LIST_TYPE_COMPUTE
- : D3D12_COMMAND_LIST_TYPE_COPY;
-
- using CommandList copyCommandList = new(GraphicsDevice, d3D12CommandListType);
-
- if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (this.referenceTracker.GetLease())
+ using (destination.referenceTracker.GetLease())
{
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(destination.D3D12Resource, destination.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
+ GraphicsDevice.ThrowIfDeviceLost();
+
+ destination.ThrowIfDeviceMismatch(GraphicsDevice);
+
+ Guard.IsInRange(sourceOffsetX, 0, Width);
+ Guard.IsInRange(sourceOffsetY, 0, Height);
+ Guard.IsInRange(sourceOffsetZ, 0, Depth);
+ Guard.IsInRange(destinationOffsetX, 0, destination.Width);
+ Guard.IsInRange(destinationOffsetY, 0, destination.Height);
+ Guard.IsInRange(destinationOffsetZ, 0, destination.Depth);
+ Guard.IsBetweenOrEqualTo(width, 1, Width);
+ Guard.IsBetweenOrEqualTo(height, 1, Height);
+ Guard.IsBetweenOrEqualTo(depth, 1, Depth);
+ Guard.IsBetweenOrEqualTo(width, 1, destination.Width);
+ Guard.IsBetweenOrEqualTo(height, 1, destination.Height);
+ Guard.IsBetweenOrEqualTo(depth, 1, destination.Depth);
+ Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX));
+ Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY));
+ Guard.IsBetweenOrEqualTo(destinationOffsetZ + depth, 1, destination.Depth, nameof(destinationOffsetZ));
+ Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
+ Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
+ Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ));
+
+ D3D12_COMMAND_LIST_TYPE d3D12CommandListType =
+ this.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE ||
+ destination.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE
+ ? D3D12_COMMAND_LIST_TYPE_COMPUTE
+ : D3D12_COMMAND_LIST_TYPE_COPY;
+
+ using CommandList copyCommandList = new(GraphicsDevice, d3D12CommandListType);
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(destination.D3D12Resource, destination.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
+ }
+
+ copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
+ d3D12ResourceDestination: destination.D3D12Resource,
+ (uint)destinationOffsetX,
+ (uint)destinationOffsetY,
+ (ushort)destinationOffsetZ,
+ d3D12ResourceSource: D3D12Resource,
+ (uint)sourceOffsetX,
+ (uint)sourceOffsetY,
+ (ushort)sourceOffsetZ,
+ (uint)width,
+ (uint)height,
+ (ushort)depth);
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(destination.D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, destination.d3D12ResourceState);
+ }
+
+ copyCommandList.ExecuteAndWaitForCompletion();
}
-
- copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
- d3D12ResourceDestination: destination.D3D12Resource,
- (uint)destinationOffsetX,
- (uint)destinationOffsetY,
- (ushort)destinationOffsetZ,
- d3D12ResourceSource: D3D12Resource,
- (uint)sourceOffsetX,
- (uint)sourceOffsetY,
- (ushort)sourceOffsetZ,
- (uint)width,
- (uint)height,
- (ushort)depth);
-
- if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
- {
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(destination.D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, destination.d3D12ResourceState);
- }
-
- copyCommandList.ExecuteAndWaitForCompletion();
}
///
@@ -358,63 +377,64 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra
/// The depth of the memory area to copy.
internal void CopyTo(ReadBackTexture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth)
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
-
- ThrowIfDisposed();
-
- destination.ThrowIfDeviceMismatch(GraphicsDevice);
- destination.ThrowIfDisposed();
-
- Guard.IsInRange(sourceOffsetX, 0, Width);
- Guard.IsInRange(sourceOffsetY, 0, Height);
- Guard.IsInRange(sourceOffsetZ, 0, Depth);
- Guard.IsInRange(destinationOffsetX, 0, destination.Width);
- Guard.IsInRange(destinationOffsetY, 0, destination.Height);
- Guard.IsInRange(destinationOffsetZ, 0, destination.Depth);
- Guard.IsBetweenOrEqualTo(width, 1, Width);
- Guard.IsBetweenOrEqualTo(height, 1, Height);
- Guard.IsBetweenOrEqualTo(depth, 1, Depth);
- Guard.IsBetweenOrEqualTo(width, 1, destination.Width);
- Guard.IsBetweenOrEqualTo(height, 1, destination.Height);
- Guard.IsBetweenOrEqualTo(depth, 1, destination.Depth);
- Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX));
- Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY));
- Guard.IsBetweenOrEqualTo(destinationOffsetZ + depth, 1, destination.Depth, nameof(destinationOffsetZ));
- Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
- Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
- Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ));
-
- using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
-
- if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (this.referenceTracker.GetLease())
+ using (destination.GetReferenceTracker().GetLease())
{
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
- }
+ GraphicsDevice.ThrowIfDeviceLost();
- fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintDestination = &destination.D3D12PlacedSubresourceFootprint)
- {
- copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
- d3D12ResourceDestination: destination.D3D12Resource,
- d3D12PlacedSubresourceFootprintDestination,
- (uint)destinationOffsetX,
- (uint)destinationOffsetY,
- (ushort)destinationOffsetZ,
- d3D12ResourceSource: D3D12Resource,
- (uint)sourceOffsetX,
- (uint)sourceOffsetY,
- (ushort)sourceOffsetZ,
- (uint)width,
- (uint)height,
- (ushort)depth);
- }
+ destination.ThrowIfDeviceMismatch(GraphicsDevice);
- if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
- {
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
- }
+ Guard.IsInRange(sourceOffsetX, 0, Width);
+ Guard.IsInRange(sourceOffsetY, 0, Height);
+ Guard.IsInRange(sourceOffsetZ, 0, Depth);
+ Guard.IsInRange(destinationOffsetX, 0, destination.Width);
+ Guard.IsInRange(destinationOffsetY, 0, destination.Height);
+ Guard.IsInRange(destinationOffsetZ, 0, destination.Depth);
+ Guard.IsBetweenOrEqualTo(width, 1, Width);
+ Guard.IsBetweenOrEqualTo(height, 1, Height);
+ Guard.IsBetweenOrEqualTo(depth, 1, Depth);
+ Guard.IsBetweenOrEqualTo(width, 1, destination.Width);
+ Guard.IsBetweenOrEqualTo(height, 1, destination.Height);
+ Guard.IsBetweenOrEqualTo(depth, 1, destination.Depth);
+ Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX));
+ Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY));
+ Guard.IsBetweenOrEqualTo(destinationOffsetZ + depth, 1, destination.Depth, nameof(destinationOffsetZ));
+ Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
+ Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
+ Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ));
- copyCommandList.ExecuteAndWaitForCompletion();
+ using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
+ }
+
+ fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintDestination = &destination.D3D12PlacedSubresourceFootprint)
+ {
+ copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
+ d3D12ResourceDestination: destination.D3D12Resource,
+ d3D12PlacedSubresourceFootprintDestination,
+ (uint)destinationOffsetX,
+ (uint)destinationOffsetY,
+ (ushort)destinationOffsetZ,
+ d3D12ResourceSource: D3D12Resource,
+ (uint)sourceOffsetX,
+ (uint)sourceOffsetY,
+ (ushort)sourceOffsetZ,
+ (uint)width,
+ (uint)height,
+ (ushort)depth);
+ }
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
+ }
+
+ copyCommandList.ExecuteAndWaitForCompletion();
+ }
}
///
@@ -430,86 +450,87 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra
/// The depth of the memory area to write to.
internal void CopyFrom(ref T source, int size, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth)
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (this.referenceTracker.GetLease())
+ {
+ GraphicsDevice.ThrowIfDeviceLost();
- ThrowIfDisposed();
+ Guard.IsInRange(destinationOffsetX, 0, Width);
+ Guard.IsInRange(destinationOffsetY, 0, Height);
+ Guard.IsInRange(destinationOffsetZ, 0, Depth);
+ Guard.IsBetweenOrEqualTo(width, 1, Width);
+ Guard.IsBetweenOrEqualTo(height, 1, Height);
+ Guard.IsBetweenOrEqualTo(depth, 1, Depth);
+ Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX));
+ Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY));
+ Guard.IsLessThanOrEqualTo(destinationOffsetZ + depth, Depth, nameof(destinationOffsetZ));
+ Guard.IsGreaterThanOrEqualTo(size, (nint)width * height * depth);
- Guard.IsInRange(destinationOffsetX, 0, Width);
- Guard.IsInRange(destinationOffsetY, 0, Height);
- Guard.IsInRange(destinationOffsetZ, 0, Depth);
- Guard.IsBetweenOrEqualTo(width, 1, Width);
- Guard.IsBetweenOrEqualTo(height, 1, Height);
- Guard.IsBetweenOrEqualTo(depth, 1, Depth);
- Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX));
- Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY));
- Guard.IsLessThanOrEqualTo(destinationOffsetZ + depth, Depth, nameof(destinationOffsetZ));
- Guard.IsGreaterThanOrEqualTo(size, (nint)width * height * depth);
-
- GraphicsDevice.D3D12Device->GetCopyableFootprint(
- DXGIFormatHelper.GetForType(),
- (uint)width,
- (uint)height,
- (ushort)depth,
- out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintSource,
- out ulong rowSizeInBytes,
- out ulong totalSizeInBytes);
+ GraphicsDevice.D3D12Device->GetCopyableFootprint(
+ DXGIFormatHelper.GetForType(),
+ (uint)width,
+ (uint)height,
+ (ushort)depth,
+ out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintSource,
+ out ulong rowSizeInBytes,
+ out ulong totalSizeInBytes);
#if NET6_0_OR_GREATER
- using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(
- GraphicsDevice.Pool,
- ResourceType.Upload,
- AllocationMode.Default,
- totalSizeInBytes);
+ using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(
+ GraphicsDevice.Pool,
+ ResourceType.Upload,
+ AllocationMode.Default,
+ totalSizeInBytes);
- using ComPtr d3D12Resource = new(allocation.Get()->GetResource());
+ using ComPtr d3D12Resource = new(allocation.Get()->GetResource());
#else
- using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
- ResourceType.Upload,
- totalSizeInBytes,
- GraphicsDevice.IsCacheCoherentUMA);
+ using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
+ ResourceType.Upload,
+ totalSizeInBytes,
+ GraphicsDevice.IsCacheCoherentUMA);
#endif
- using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
- fixed (void* sourcePointer = &source)
- {
- MemoryHelper.Copy(
- sourcePointer,
- resource.Pointer,
+ using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
+ fixed (void* sourcePointer = &source)
+ {
+ MemoryHelper.Copy(
+ sourcePointer,
+ resource.Pointer,
+ (uint)height,
+ (uint)depth,
+ rowSizeInBytes,
+ d3D12PlacedSubresourceFootprintSource.Footprint.RowPitch,
+ d3D12PlacedSubresourceFootprintSource.Footprint.RowPitch * (uint)height);
+ }
+
+ using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
+ }
+
+ copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
+ d3D12ResourceDestination: D3D12Resource,
+ destinationX: (uint)destinationOffsetX,
+ destinationY: (uint)destinationOffsetY,
+ destinationZ: (ushort)destinationOffsetZ,
+ d3D12ResourceSource: d3D12Resource.Get(),
+ &d3D12PlacedSubresourceFootprintSource,
+ sourceX: 0,
+ sourceY: 0,
+ sourceZ: 0,
+ (uint)width,
(uint)height,
- (uint)depth,
- rowSizeInBytes,
- d3D12PlacedSubresourceFootprintSource.Footprint.RowPitch,
- d3D12PlacedSubresourceFootprintSource.Footprint.RowPitch * (uint)height);
+ (ushort)depth);
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
+ }
+
+ copyCommandList.ExecuteAndWaitForCompletion();
}
-
- using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
-
- if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
- {
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
- }
-
- copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
- d3D12ResourceDestination: D3D12Resource,
- destinationX: (uint)destinationOffsetX,
- destinationY: (uint)destinationOffsetY,
- destinationZ: (ushort)destinationOffsetZ,
- d3D12ResourceSource: d3D12Resource.Get(),
- &d3D12PlacedSubresourceFootprintSource,
- sourceX: 0,
- sourceY: 0,
- sourceZ: 0,
- (uint)width,
- (uint)height,
- (ushort)depth);
-
- if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
- {
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
- }
-
- copyCommandList.ExecuteAndWaitForCompletion();
}
///
@@ -527,67 +548,89 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra
/// The depth of the memory area to write to.
internal void CopyFrom(UploadTexture3D source, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth)
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
-
- ThrowIfDisposed();
-
- source.ThrowIfDeviceMismatch(GraphicsDevice);
- source.ThrowIfDisposed();
-
- Guard.IsInRange(sourceOffsetX, 0, source.Width);
- Guard.IsInRange(sourceOffsetY, 0, source.Height);
- Guard.IsInRange(sourceOffsetZ, 0, source.Depth);
- Guard.IsInRange(destinationOffsetX, 0, Width);
- Guard.IsInRange(destinationOffsetY, 0, Height);
- Guard.IsInRange(destinationOffsetZ, 0, Depth);
- Guard.IsBetweenOrEqualTo(width, 1, Width);
- Guard.IsBetweenOrEqualTo(height, 1, Height);
- Guard.IsBetweenOrEqualTo(depth, 1, Depth);
- Guard.IsBetweenOrEqualTo(width, 1, source.Width);
- Guard.IsBetweenOrEqualTo(height, 1, source.Height);
- Guard.IsBetweenOrEqualTo(depth, 1, source.Depth);
- Guard.IsLessThanOrEqualTo(sourceOffsetX + width, source.Width, nameof(sourceOffsetX));
- Guard.IsLessThanOrEqualTo(sourceOffsetY + height, source.Height, nameof(sourceOffsetY));
- Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, source.Depth, nameof(sourceOffsetZ));
- Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX));
- Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY));
- Guard.IsLessThanOrEqualTo(destinationOffsetZ + depth, Depth, nameof(destinationOffsetZ));
-
- using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
-
- if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (this.referenceTracker.GetLease())
+ using (source.GetReferenceTracker().GetLease())
{
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
- }
+ GraphicsDevice.ThrowIfDeviceLost();
- fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintSource = &source.D3D12PlacedSubresourceFootprint)
- {
- copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
- d3D12ResourceDestination: D3D12Resource,
- (uint)destinationOffsetX,
- (uint)destinationOffsetY,
- (ushort)destinationOffsetZ,
- d3D12ResourceSource: source.D3D12Resource,
- d3D12PlacedSubresourceFootprintSource,
- (uint)sourceOffsetX,
- (uint)sourceOffsetY,
- (ushort)sourceOffsetZ,
- (uint)width,
- (uint)height,
- (ushort)depth);
- }
+ source.ThrowIfDeviceMismatch(GraphicsDevice);
- if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
- {
- copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
- }
+ Guard.IsInRange(sourceOffsetX, 0, source.Width);
+ Guard.IsInRange(sourceOffsetY, 0, source.Height);
+ Guard.IsInRange(sourceOffsetZ, 0, source.Depth);
+ Guard.IsInRange(destinationOffsetX, 0, Width);
+ Guard.IsInRange(destinationOffsetY, 0, Height);
+ Guard.IsInRange(destinationOffsetZ, 0, Depth);
+ Guard.IsBetweenOrEqualTo(width, 1, Width);
+ Guard.IsBetweenOrEqualTo(height, 1, Height);
+ Guard.IsBetweenOrEqualTo(depth, 1, Depth);
+ Guard.IsBetweenOrEqualTo(width, 1, source.Width);
+ Guard.IsBetweenOrEqualTo(height, 1, source.Height);
+ Guard.IsBetweenOrEqualTo(depth, 1, source.Depth);
+ Guard.IsLessThanOrEqualTo(sourceOffsetX + width, source.Width, nameof(sourceOffsetX));
+ Guard.IsLessThanOrEqualTo(sourceOffsetY + height, source.Height, nameof(sourceOffsetY));
+ Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, source.Depth, nameof(sourceOffsetZ));
+ Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX));
+ Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY));
+ Guard.IsLessThanOrEqualTo(destinationOffsetZ + depth, Depth, nameof(destinationOffsetZ));
- copyCommandList.ExecuteAndWaitForCompletion();
+ using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
+ }
+
+ fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintSource = &source.D3D12PlacedSubresourceFootprint)
+ {
+ copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
+ d3D12ResourceDestination: D3D12Resource,
+ (uint)destinationOffsetX,
+ (uint)destinationOffsetY,
+ (ushort)destinationOffsetZ,
+ d3D12ResourceSource: source.D3D12Resource,
+ d3D12PlacedSubresourceFootprintSource,
+ (uint)sourceOffsetX,
+ (uint)sourceOffsetY,
+ (ushort)sourceOffsetZ,
+ (uint)width,
+ (uint)height,
+ (ushort)depth);
+ }
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
+ }
+
+ copyCommandList.ExecuteAndWaitForCompletion();
+ }
}
///
- protected override void OnDispose()
+ public virtual void Dispose()
+ {
+ this.referenceTracker.Dispose();
+
+ GC.SuppressFinalize(this);
+ }
+
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal ref ReferenceTracker GetReferenceTracker()
+ {
+ return ref this.referenceTracker;
+ }
+
+ ///
+ ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
+ {
+ return ref this.referenceTracker;
+ }
+
+ ///
+ void ReferenceTracker.ITrackedObject.DangerousRelease()
{
this.d3D12Resource.Dispose();
#if NET6_0_OR_GREATER
@@ -635,46 +678,54 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra
///
internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized)
{
- ThrowIfDisposed();
- ThrowIfDeviceMismatch(device);
+ using (this.referenceTracker.GetLease())
+ {
+ ThrowIfDeviceMismatch(device);
- isNormalized = DXGIFormatHelper.IsNormalizedType();
+ isNormalized = DXGIFormatHelper.IsNormalizedType();
- return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
+ return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
+ }
}
///
internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device)
{
- ThrowIfDisposed();
- ThrowIfDeviceMismatch(device);
+ using (this.referenceTracker.GetLease())
+ {
+ ThrowIfDeviceMismatch(device);
- return D3D12Resource;
+ return D3D12Resource;
+ }
}
///
internal (D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource)
{
- ThrowIfDisposed();
- ThrowIfDeviceMismatch(device);
+ using (this.referenceTracker.GetLease())
+ {
+ ThrowIfDeviceMismatch(device);
- D3D12_RESOURCE_STATES d3D12ResourceStatesBefore = this.d3D12ResourceState;
- D3D12_RESOURCE_STATES d3D12ResourceStatesAfter = ResourceStateHelper.GetD3D12ResourceStates(resourceState);
+ D3D12_RESOURCE_STATES d3D12ResourceStatesBefore = this.d3D12ResourceState;
+ D3D12_RESOURCE_STATES d3D12ResourceStatesAfter = ResourceStateHelper.GetD3D12ResourceStates(resourceState);
- this.d3D12ResourceState = d3D12ResourceStatesAfter;
+ this.d3D12ResourceState = d3D12ResourceStatesAfter;
- d3D12Resource = D3D12Resource;
+ d3D12Resource = D3D12Resource;
- return (d3D12ResourceStatesBefore, d3D12ResourceStatesAfter);
+ return (d3D12ResourceStatesBefore, d3D12ResourceStatesAfter);
+ }
}
///
D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device)
{
- ThrowIfDisposed();
- ThrowIfDeviceMismatch(device);
+ using (this.referenceTracker.GetLease())
+ {
+ ThrowIfDeviceMismatch(device);
- return D3D12GpuDescriptorHandle;
+ return D3D12GpuDescriptorHandle;
+ }
}
///
diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs
index 8e42995a..d4583bfb 100644
--- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs
@@ -15,9 +15,14 @@ namespace ComputeSharp.Resources;
/// A representing a typed buffer stored on CPU memory, that can be used to transfer data to/from the GPU.
///
/// The type of items stored on the buffer.
-public abstract unsafe class TransferBuffer : NativeObject, IGraphicsResource, IMemoryOwner
+public abstract unsafe class TransferBuffer : ReferenceTracker.ITrackedObject, IGraphicsResource, IMemoryOwner
where T : unmanaged
{
+ ///
+ /// The owning object for the current instance.
+ ///
+ private ReferenceTracker referenceTracker;
+
#if NET6_0_OR_GREATER
///
/// The instance used to retrieve .
@@ -44,29 +49,41 @@ public abstract unsafe class TransferBuffer : NativeObject, IGraphicsResource
/// The allocation mode to use for the new resource.
private protected TransferBuffer(GraphicsDevice device, int length, ResourceType resourceType, AllocationMode allocationMode)
{
- device.ThrowIfDisposed();
- device.ThrowIfDeviceLost();
+ this.referenceTracker = new ReferenceTracker(this);
- // The maximum length is set such that the aligned buffer size can't exceed uint.MaxValue
- Guard.IsBetweenOrEqualTo(length, 1, (uint.MaxValue / (uint)sizeof(T)) & ~255);
+ using (device.GetReferenceTracker().GetLease())
+ {
+ device.ThrowIfDeviceLost();
- GraphicsDevice = device;
- Length = length;
+ // The maximum length is set such that the aligned buffer size can't exceed uint.MaxValue
+ Guard.IsBetweenOrEqualTo(length, 1, (uint.MaxValue / (uint)sizeof(T)) & ~255);
- ulong sizeInBytes = (uint)length * (uint)sizeof(T);
+ GraphicsDevice = device;
+ Length = length;
+
+ ulong sizeInBytes = (uint)length * (uint)sizeof(T);
#if NET6_0_OR_GREATER
- this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, sizeInBytes);
- this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource());
+ this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, sizeInBytes);
+ this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource());
#else
- this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, sizeInBytes, device.IsCacheCoherentUMA);
+ this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, sizeInBytes, device.IsCacheCoherentUMA);
#endif
- device.RegisterAllocatedResource();
+ device.RegisterAllocatedResource();
- this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
+ this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
- this.d3D12Resource.Get()->SetName(this);
+ this.d3D12Resource.Get()->SetName(this);
+ }
+ }
+
+ ///
+ /// Releases unmanaged resources for the current instance.
+ ///
+ ~TransferBuffer()
+ {
+ this.referenceTracker.Dispose();
}
///
@@ -93,9 +110,10 @@ public abstract unsafe class TransferBuffer : NativeObject, IGraphicsResource
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
- ThrowIfDisposed();
-
- return new MemoryManager(this).Memory;
+ using (this.referenceTracker.GetLease())
+ {
+ return new MemoryManager(this).Memory;
+ }
}
}
@@ -105,14 +123,36 @@ public abstract unsafe class TransferBuffer : NativeObject, IGraphicsResource
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
- ThrowIfDisposed();
-
- return new(this.mappedData, Length);
+ using (this.referenceTracker.GetLease())
+ {
+ return new(this.mappedData, Length);
+ }
}
}
///
- protected override void OnDispose()
+ public void Dispose()
+ {
+ this.referenceTracker.Dispose();
+
+ GC.SuppressFinalize(this);
+ }
+
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal ref ReferenceTracker GetReferenceTracker()
+ {
+ return ref this.referenceTracker;
+ }
+
+ ///
+ ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
+ {
+ return ref this.referenceTracker;
+ }
+
+ ///
+ void ReferenceTracker.ITrackedObject.DangerousRelease()
{
this.d3D12Resource.Dispose();
#if NET6_0_OR_GREATER
@@ -171,11 +211,12 @@ public abstract unsafe class TransferBuffer : NativeObject, IGraphicsResource
///
public override MemoryHandle Pin(int elementIndex = 0)
{
- Guard.IsEqualTo(elementIndex, 0);
+ using (this.buffer.referenceTracker.GetLease())
+ {
+ Guard.IsEqualTo(elementIndex, 0);
- this.buffer.ThrowIfDisposed();
-
- return new(this.buffer.mappedData);
+ return new(this.buffer.mappedData);
+ }
}
///
diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs
index 8e546be2..cd0d9a99 100644
--- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs
@@ -1,4 +1,5 @@
-using System.Runtime.CompilerServices;
+using System;
+using System.Runtime.CompilerServices;
using CommunityToolkit.Diagnostics;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Extensions;
@@ -15,9 +16,14 @@ namespace ComputeSharp.Resources;
/// A representing a typed 2D texture stored on on CPU memory, that can be used to transfer data to/from the GPU.
///
/// The type of items stored on the texture.
-public unsafe abstract class TransferTexture2D : NativeObject, IGraphicsResource
+public unsafe abstract class TransferTexture2D : ReferenceTracker.ITrackedObject, IDisposable, IGraphicsResource
where T : unmanaged
{
+ ///
+ /// The owning object for the current instance.
+ ///
+ private ReferenceTracker referenceTracker;
+
#if NET6_0_OR_GREATER
///
/// The instance used to retrieve .
@@ -50,39 +56,51 @@ public unsafe abstract class TransferTexture2D : NativeObject, IGraphicsResou
/// The allocation mode to use for the new resource.
private protected TransferTexture2D(GraphicsDevice device, int width, int height, ResourceType resourceType, AllocationMode allocationMode)
{
- device.ThrowIfDisposed();
- device.ThrowIfDeviceLost();
+ this.referenceTracker = new ReferenceTracker(this);
- Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
- Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
-
- if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE2D))
+ using (device.GetReferenceTracker().GetLease())
{
- UnsupportedTextureTypeException.ThrowForTexture2D();
- }
+ device.ThrowIfDeviceLost();
- GraphicsDevice = device;
+ Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
+ Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
- device.D3D12Device->GetCopyableFootprint(
- DXGIFormatHelper.GetForType(),
- (uint)width,
- (uint)height,
- out this.d3D12PlacedSubresourceFootprint,
- out _,
- out ulong totalSizeInBytes);
+ if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE2D))
+ {
+ UnsupportedTextureTypeException.ThrowForTexture2D();
+ }
+
+ GraphicsDevice = device;
+
+ device.D3D12Device->GetCopyableFootprint(
+ DXGIFormatHelper.GetForType(),
+ (uint)width,
+ (uint)height,
+ out this.d3D12PlacedSubresourceFootprint,
+ out _,
+ out ulong totalSizeInBytes);
#if NET6_0_OR_GREATER
- this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes);
- this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource());
+ this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes);
+ this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource());
#else
- this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, totalSizeInBytes, device.IsCacheCoherentUMA);
+ this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, totalSizeInBytes, device.IsCacheCoherentUMA);
#endif
- device.RegisterAllocatedResource();
+ device.RegisterAllocatedResource();
- this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
+ this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
- this.d3D12Resource.Get()->SetName(this);
+ this.d3D12Resource.Get()->SetName(this);
+ }
+ }
+
+ ///
+ /// Releases unmanaged resources for the current instance.
+ ///
+ ~TransferTexture2D()
+ {
+ this.referenceTracker.Dispose();
}
///
@@ -114,14 +132,36 @@ public unsafe abstract class TransferTexture2D : NativeObject, IGraphicsResou
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
- ThrowIfDisposed();
-
- return new(this.mappedData, Width, Height, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch);
+ using (this.referenceTracker.GetLease())
+ {
+ return new(this.mappedData, Width, Height, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch);
+ }
}
}
///
- protected override void OnDispose()
+ public void Dispose()
+ {
+ this.referenceTracker.Dispose();
+
+ GC.SuppressFinalize(this);
+ }
+
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal ref ReferenceTracker GetReferenceTracker()
+ {
+ return ref this.referenceTracker;
+ }
+
+ ///
+ ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
+ {
+ return ref this.referenceTracker;
+ }
+
+ ///
+ void ReferenceTracker.ITrackedObject.DangerousRelease()
{
this.d3D12Resource.Dispose();
#if NET6_0_OR_GREATER
diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs
index 72201ccb..da347e36 100644
--- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs
@@ -1,4 +1,5 @@
-using System.Runtime.CompilerServices;
+using System;
+using System.Runtime.CompilerServices;
using CommunityToolkit.Diagnostics;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Extensions;
@@ -15,9 +16,14 @@ namespace ComputeSharp.Resources;
/// A representing a typed 3D texture stored on on CPU memory, that can be used to transfer data to/from the GPU.
///
/// The type of items stored on the texture.
-public unsafe abstract class TransferTexture3D : NativeObject, IGraphicsResource
+public unsafe abstract class TransferTexture3D : ReferenceTracker.ITrackedObject, IDisposable, IGraphicsResource
where T : unmanaged
{
+ ///
+ /// The owning object for the current instance.
+ ///
+ private ReferenceTracker referenceTracker;
+
#if NET6_0_OR_GREATER
///
/// The instance used to retrieve .
@@ -51,41 +57,53 @@ public unsafe abstract class TransferTexture3D : NativeObject, IGraphicsResou
/// The allocation mode to use for the new resource.
private protected TransferTexture3D(GraphicsDevice device, int width, int height, int depth, ResourceType resourceType, AllocationMode allocationMode)
{
- device.ThrowIfDisposed();
- device.ThrowIfDeviceLost();
+ this.referenceTracker = new ReferenceTracker(this);
- Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
- Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
- Guard.IsBetweenOrEqualTo(depth, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
-
- if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE3D))
+ using (device.GetReferenceTracker().GetLease())
{
- UnsupportedTextureTypeException.ThrowForTexture3D();
- }
+ device.ThrowIfDeviceLost();
- GraphicsDevice = device;
+ Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
+ Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
+ Guard.IsBetweenOrEqualTo(depth, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
- device.D3D12Device->GetCopyableFootprint(
- DXGIFormatHelper.GetForType(),
- (uint)width,
- (uint)height,
- (ushort)depth,
- out this.d3D12PlacedSubresourceFootprint,
- out _,
- out ulong totalSizeInBytes);
+ if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE3D))
+ {
+ UnsupportedTextureTypeException.ThrowForTexture3D();
+ }
+
+ GraphicsDevice = device;
+
+ device.D3D12Device->GetCopyableFootprint(
+ DXGIFormatHelper.GetForType(),
+ (uint)width,
+ (uint)height,
+ (ushort)depth,
+ out this.d3D12PlacedSubresourceFootprint,
+ out _,
+ out ulong totalSizeInBytes);
#if NET6_0_OR_GREATER
- this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes);
- this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource());
+ this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes);
+ this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource());
#else
- this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, totalSizeInBytes, device.IsCacheCoherentUMA);
+ this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, totalSizeInBytes, device.IsCacheCoherentUMA);
#endif
- device.RegisterAllocatedResource();
+ device.RegisterAllocatedResource();
- this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
+ this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
- this.d3D12Resource.Get()->SetName(this);
+ this.d3D12Resource.Get()->SetName(this);
+ }
+ }
+
+ ///
+ /// Releases unmanaged resources for the current instance.
+ ///
+ ~TransferTexture3D()
+ {
+ this.referenceTracker.Dispose();
}
///
@@ -122,14 +140,36 @@ public unsafe abstract class TransferTexture3D : NativeObject, IGraphicsResou
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
- ThrowIfDisposed();
-
- return new(this.mappedData, Width, Height, Depth, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch);
+ using (this.referenceTracker.GetLease())
+ {
+ return new(this.mappedData, Width, Height, Depth, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch);
+ }
}
}
///
- protected override void OnDispose()
+ public void Dispose()
+ {
+ this.referenceTracker.Dispose();
+
+ GC.SuppressFinalize(this);
+ }
+
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal ref ReferenceTracker GetReferenceTracker()
+ {
+ return ref this.referenceTracker;
+ }
+
+ ///
+ ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
+ {
+ return ref this.referenceTracker;
+ }
+
+ ///
+ void ReferenceTracker.ITrackedObject.DangerousRelease()
{
this.d3D12Resource.Dispose();
#if NET6_0_OR_GREATER
diff --git a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs
index c51fca8b..2958f74e 100644
--- a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs
@@ -57,89 +57,92 @@ public sealed class ConstantBuffer : Buffer
///
internal override unsafe void CopyTo(ref T destination, int offset, int length)
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
-
- ThrowIfDisposed();
-
- Guard.IsBetweenOrEqualTo(length, 0, Length);
- Guard.IsInRange(offset, 0, Length);
- Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset));
-
- using ID3D12ResourceMap resource = D3D12Resource->Map();
-
- fixed (void* destinationPointer = &destination)
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (GetReferenceTracker().GetLease())
{
- MemoryHelper.Copy(
- source: resource.Pointer,
- destination: destinationPointer,
- sourceElementOffset: (uint)offset,
- destinationElementOffset: 0,
- sourceElementPitchInBytes: (uint)GetPaddedSize(),
- destinationElementPitchInBytes: (uint)sizeof(T),
- count: (uint)length);
+ GraphicsDevice.ThrowIfDeviceLost();
+
+ Guard.IsBetweenOrEqualTo(length, 0, Length);
+ Guard.IsInRange(offset, 0, Length);
+ Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset));
+
+ using ID3D12ResourceMap resource = D3D12Resource->Map();
+
+ fixed (void* destinationPointer = &destination)
+ {
+ MemoryHelper.Copy(
+ source: resource.Pointer,
+ destination: destinationPointer,
+ sourceElementOffset: (uint)offset,
+ destinationElementOffset: 0,
+ sourceElementPitchInBytes: (uint)GetPaddedSize(),
+ destinationElementPitchInBytes: (uint)sizeof(T),
+ count: (uint)length);
+ }
}
}
///
internal override unsafe void CopyTo(Buffer destination, int sourceOffset, int destinationOffset, int length)
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
-
- ThrowIfDisposed();
-
- destination.ThrowIfDeviceMismatch(GraphicsDevice);
- destination.ThrowIfDisposed();
-
- Guard.IsBetweenOrEqualTo(length, 0, Length);
- Guard.IsBetweenOrEqualTo(length, 0, destination.Length);
- Guard.IsInRange(sourceOffset, 0, Length);
- Guard.IsLessThanOrEqualTo(sourceOffset + length, Length, nameof(sourceOffset));
- Guard.IsInRange(destinationOffset, 0, destination.Length);
- Guard.IsLessThanOrEqualTo(destinationOffset + length, destination.Length, nameof(destinationOffset));
-
- if (destination is ConstantBuffer buffer)
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (GetReferenceTracker().GetLease())
+ using (destination.GetReferenceTracker().GetLease())
{
- using ID3D12ResourceMap sourceMap = D3D12Resource->Map();
- using ID3D12ResourceMap destinationMap = buffer.D3D12Resource->Map();
+ GraphicsDevice.ThrowIfDeviceLost();
- MemoryHelper.Copy(
- source: sourceMap.Pointer,
- destination: destinationMap.Pointer,
- sourceElementOffset: (uint)sourceOffset,
- destinationElementOffset: (uint)destinationOffset,
- sourceElementPitchInBytes: (uint)GetPaddedSize(),
- destinationElementPitchInBytes: (uint)GetPaddedSize(),
- count: (uint)length);
+ destination.ThrowIfDeviceMismatch(GraphicsDevice);
+
+ Guard.IsBetweenOrEqualTo(length, 0, Length);
+ Guard.IsBetweenOrEqualTo(length, 0, destination.Length);
+ Guard.IsInRange(sourceOffset, 0, Length);
+ Guard.IsLessThanOrEqualTo(sourceOffset + length, Length, nameof(sourceOffset));
+ Guard.IsInRange(destinationOffset, 0, destination.Length);
+ Guard.IsLessThanOrEqualTo(destinationOffset + length, destination.Length, nameof(destinationOffset));
+
+ if (destination is ConstantBuffer buffer)
+ {
+ using ID3D12ResourceMap sourceMap = D3D12Resource->Map();
+ using ID3D12ResourceMap destinationMap = buffer.D3D12Resource->Map();
+
+ MemoryHelper.Copy(
+ source: sourceMap.Pointer,
+ destination: destinationMap.Pointer,
+ sourceElementOffset: (uint)sourceOffset,
+ destinationElementOffset: (uint)destinationOffset,
+ sourceElementPitchInBytes: (uint)GetPaddedSize(),
+ destinationElementPitchInBytes: (uint)GetPaddedSize(),
+ count: (uint)length);
+ }
+ else CopyToWithCpuBuffer(destination, sourceOffset, destinationOffset, length);
}
- else CopyToWithCpuBuffer(destination, sourceOffset, destinationOffset, length);
}
///
internal override unsafe void CopyFrom(ref T source, int offset, int length)
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
-
- ThrowIfDisposed();
-
- Guard.IsBetweenOrEqualTo(length, 0, Length);
- Guard.IsInRange(offset, 0, Length);
- Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset));
-
- using ID3D12ResourceMap resource = D3D12Resource->Map();
-
- fixed (void* sourcePointer = &source)
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (GetReferenceTracker().GetLease())
{
- MemoryHelper.Copy(
- source: sourcePointer,
- destination: resource.Pointer,
- sourceElementOffset: 0,
- destinationElementOffset: (uint)offset,
- sourceElementPitchInBytes: (uint)sizeof(T),
- destinationElementPitchInBytes: (uint)GetPaddedSize(),
- count: (uint)length);
+ GraphicsDevice.ThrowIfDeviceLost();
+
+ Guard.IsBetweenOrEqualTo(length, 0, Length);
+ Guard.IsInRange(offset, 0, Length);
+ Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset));
+
+ using ID3D12ResourceMap resource = D3D12Resource->Map();
+
+ fixed (void* sourcePointer = &source)
+ {
+ MemoryHelper.Copy(
+ source: sourcePointer,
+ destination: resource.Pointer,
+ sourceElementOffset: 0,
+ destinationElementOffset: (uint)offset,
+ sourceElementPitchInBytes: (uint)sizeof(T),
+ destinationElementPitchInBytes: (uint)GetPaddedSize(),
+ count: (uint)length);
+ }
}
}
diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs
index dee9a948..35aa2d73 100644
--- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs
+++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs
@@ -24,32 +24,34 @@ partial class ReadWriteTexture2D
///
internal IReadOnlyNormalizedTexture2D AsReadOnly()
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
-
- ThrowIfDisposed();
- ThrowIfIsNotInReadOnlyState();
-
- ReadOnly? readOnlyWrapper = this.readOnlyWrapper;
-
- if (readOnlyWrapper is not null)
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (GetReferenceTracker().GetLease())
{
- return readOnlyWrapper;
- }
+ GraphicsDevice.ThrowIfDeviceLost();
- [MethodImpl(MethodImplOptions.NoInlining)]
- static ReadOnly InitializeWrapper(ReadWriteTexture2D texture)
- {
- return texture.readOnlyWrapper = new(texture);
- }
+ ThrowIfIsNotInReadOnlyState();
- return InitializeWrapper(this);
+ ReadOnly? readOnlyWrapper = this.readOnlyWrapper;
+
+ if (readOnlyWrapper is not null)
+ {
+ return readOnlyWrapper;
+ }
+
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ static ReadOnly InitializeWrapper(ReadWriteTexture2D texture)
+ {
+ return texture.readOnlyWrapper = new(texture);
+ }
+
+ return InitializeWrapper(this);
+ }
}
///
- protected override void OnDispose()
+ public override void Dispose()
{
- base.OnDispose();
+ base.Dispose();
this.readOnlyWrapper?.Dispose();
}
@@ -57,8 +59,13 @@ partial class ReadWriteTexture2D
///
/// A wrapper for a resource that has been temporarily transitioned to readonly.
///
- private sealed unsafe class ReadOnly : NativeObject, IReadOnlyNormalizedTexture2D, GraphicsResourceHelper.IGraphicsResource
+ private sealed unsafe class ReadOnly : ReferenceTracker.ITrackedObject, IDisposable, IReadOnlyNormalizedTexture2D, GraphicsResourceHelper.IGraphicsResource
{
+ ///
+ /// The owning object for the current instance.
+ ///
+ private ReferenceTracker referenceTracker;
+
///
/// The owning instance being wrapped.
///
@@ -75,6 +82,7 @@ partial class ReadWriteTexture2D
/// The owning instance to wrap.
public ReadOnly(ReadWriteTexture2D owner)
{
+ this.referenceTracker = new ReferenceTracker(this);
this.owner = owner;
owner.GraphicsDevice.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
@@ -106,12 +114,13 @@ partial class ReadWriteTexture2D
///
D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device)
{
- ThrowIfDisposed();
+ using (this.referenceTracker.GetLease())
+ using (this.owner.GetReferenceTracker().GetLease())
+ {
+ this.owner.ThrowIfDeviceMismatch(device);
- this.owner.ThrowIfDisposed();
- this.owner.ThrowIfDeviceMismatch(device);
-
- return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
+ return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
+ }
}
///
@@ -123,12 +132,13 @@ partial class ReadWriteTexture2D
///
ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device)
{
- ThrowIfDisposed();
+ using (this.referenceTracker.GetLease())
+ using (this.owner.GetReferenceTracker().GetLease())
+ {
+ this.owner.ThrowIfDeviceMismatch(device);
- this.owner.ThrowIfDisposed();
- this.owner.ThrowIfDeviceMismatch(device);
-
- return this.owner.D3D12Resource;
+ return this.owner.D3D12Resource;
+ }
}
///
@@ -138,7 +148,21 @@ partial class ReadWriteTexture2D
}
///
- protected override void OnDispose()
+ public void Dispose()
+ {
+ this.referenceTracker.Dispose();
+
+ GC.SuppressFinalize(this);
+ }
+
+ ///
+ ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
+ {
+ return ref this.referenceTracker;
+ }
+
+ ///
+ void ReferenceTracker.ITrackedObject.DangerousRelease()
{
if (this.owner.GraphicsDevice is GraphicsDevice device)
{
diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs
index 12c4c5ec..056d8ee4 100644
--- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs
+++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs
@@ -24,35 +24,37 @@ partial class ReadWriteTexture2D
///
public IReadOnlyTexture2D AsReadOnly()
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
-
- ThrowIfDisposed();
- ThrowIfIsNotInReadOnlyState();
-
- ReadOnly? readOnlyWrapper = this.readOnlyWrapper;
-
- if (readOnlyWrapper is not null)
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (GetReferenceTracker().GetLease())
{
- return readOnlyWrapper;
- }
+ GraphicsDevice.ThrowIfDeviceLost();
- // The wrapper initialization is moved to a non inlined helper to keep the codegen of this method
- // as compact as possible. If the current wrapper is not initialized, control goes directly to the
- // helper that will initialize and return it, which translates to a single jump.
- [MethodImpl(MethodImplOptions.NoInlining)]
- static ReadOnly InitializeWrapper(ReadWriteTexture2D texture)
- {
- return texture.readOnlyWrapper = new(texture);
- }
+ ThrowIfIsNotInReadOnlyState();
- return InitializeWrapper(this);
+ ReadOnly? readOnlyWrapper = this.readOnlyWrapper;
+
+ if (readOnlyWrapper is not null)
+ {
+ return readOnlyWrapper;
+ }
+
+ // The wrapper initialization is moved to a non inlined helper to keep the codegen of this method
+ // as compact as possible. If the current wrapper is not initialized, control goes directly to the
+ // helper that will initialize and return it, which translates to a single jump.
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ static ReadOnly InitializeWrapper(ReadWriteTexture2D texture)
+ {
+ return texture.readOnlyWrapper = new(texture);
+ }
+
+ return InitializeWrapper(this);
+ }
}
///
- protected override void OnDispose()
+ public override void Dispose()
{
- base.OnDispose();
+ base.Dispose();
this.readOnlyWrapper?.Dispose();
}
@@ -60,8 +62,13 @@ partial class ReadWriteTexture2D
///
/// A wrapper for a resource that has been temporarily transitioned to readonly.
///
- private sealed unsafe class ReadOnly : NativeObject, IReadOnlyTexture2D, GraphicsResourceHelper.IGraphicsResource
+ private sealed unsafe class ReadOnly : ReferenceTracker.ITrackedObject, IDisposable, IReadOnlyTexture2D, GraphicsResourceHelper.IGraphicsResource
{
+ ///
+ /// The owning object for the current instance.
+ ///
+ private ReferenceTracker referenceTracker;
+
///
/// The owning instance being wrapped.
///
@@ -78,6 +85,7 @@ partial class ReadWriteTexture2D
/// The owning instance to wrap.
public ReadOnly(ReadWriteTexture2D owner)
{
+ this.referenceTracker = new ReferenceTracker(this);
this.owner = owner;
owner.GraphicsDevice.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
@@ -109,12 +117,13 @@ partial class ReadWriteTexture2D
///
D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device)
{
- ThrowIfDisposed();
+ using (this.referenceTracker.GetLease())
+ using (this.owner.GetReferenceTracker().GetLease())
+ {
+ this.owner.ThrowIfDeviceMismatch(device);
- this.owner.ThrowIfDisposed();
- this.owner.ThrowIfDeviceMismatch(device);
-
- return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
+ return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
+ }
}
///
@@ -126,12 +135,13 @@ partial class ReadWriteTexture2D
///
ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device)
{
- ThrowIfDisposed();
+ using (this.referenceTracker.GetLease())
+ using (this.owner.GetReferenceTracker().GetLease())
+ {
+ this.owner.ThrowIfDeviceMismatch(device);
- this.owner.ThrowIfDisposed();
- this.owner.ThrowIfDeviceMismatch(device);
-
- return this.owner.D3D12Resource;
+ return this.owner.D3D12Resource;
+ }
}
///
@@ -141,7 +151,21 @@ partial class ReadWriteTexture2D
}
///
- protected override void OnDispose()
+ public void Dispose()
+ {
+ this.referenceTracker.Dispose();
+
+ GC.SuppressFinalize(this);
+ }
+
+ ///
+ ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
+ {
+ return ref this.referenceTracker;
+ }
+
+ ///
+ void ReferenceTracker.ITrackedObject.DangerousRelease()
{
if (this.owner.GraphicsDevice is GraphicsDevice device)
{
diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs
index f9e7ccfb..8db4d870 100644
--- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs
+++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs
@@ -24,32 +24,34 @@ partial class ReadWriteTexture3D
///
public IReadOnlyNormalizedTexture3D AsReadOnly()
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
-
- ThrowIfDisposed();
- ThrowIfIsNotInReadOnlyState();
-
- ReadOnly? readOnlyWrapper = this.readOnlyWrapper;
-
- if (readOnlyWrapper is not null)
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (GetReferenceTracker().GetLease())
{
- return readOnlyWrapper;
- }
+ GraphicsDevice.ThrowIfDeviceLost();
- [MethodImpl(MethodImplOptions.NoInlining)]
- static ReadOnly InitializeWrapper(ReadWriteTexture3D texture)
- {
- return texture.readOnlyWrapper = new(texture);
- }
+ ThrowIfIsNotInReadOnlyState();
- return InitializeWrapper(this);
+ ReadOnly? readOnlyWrapper = this.readOnlyWrapper;
+
+ if (readOnlyWrapper is not null)
+ {
+ return readOnlyWrapper;
+ }
+
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ static ReadOnly InitializeWrapper(ReadWriteTexture3D texture)
+ {
+ return texture.readOnlyWrapper = new(texture);
+ }
+
+ return InitializeWrapper(this);
+ }
}
///
- protected override void OnDispose()
+ public override void Dispose()
{
- base.OnDispose();
+ base.Dispose();
this.readOnlyWrapper?.Dispose();
}
@@ -57,8 +59,13 @@ partial class ReadWriteTexture3D
///
/// A wrapper for a resource that has been temporarily transitioned to readonly.
///
- private sealed unsafe class ReadOnly : NativeObject, IReadOnlyNormalizedTexture3D, GraphicsResourceHelper.IGraphicsResource
+ private sealed unsafe class ReadOnly : ReferenceTracker.ITrackedObject, IDisposable, IReadOnlyNormalizedTexture3D, GraphicsResourceHelper.IGraphicsResource
{
+ ///
+ /// The owning object for the current instance.
+ ///
+ private ReferenceTracker referenceTracker;
+
///
/// The owning instance being wrapped.
///
@@ -75,6 +82,7 @@ partial class ReadWriteTexture3D
/// The owning instance to wrap.
public ReadOnly(ReadWriteTexture3D owner)
{
+ this.referenceTracker = new ReferenceTracker(this);
this.owner = owner;
owner.GraphicsDevice.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
@@ -109,12 +117,13 @@ partial class ReadWriteTexture3D
///
D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device)
{
- ThrowIfDisposed();
+ using (this.referenceTracker.GetLease())
+ using (this.owner.GetReferenceTracker().GetLease())
+ {
+ this.owner.ThrowIfDeviceMismatch(device);
- this.owner.ThrowIfDisposed();
- this.owner.ThrowIfDeviceMismatch(device);
-
- return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
+ return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
+ }
}
///
@@ -126,12 +135,13 @@ partial class ReadWriteTexture3D
///
ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device)
{
- ThrowIfDisposed();
+ using (this.referenceTracker.GetLease())
+ using (this.owner.GetReferenceTracker().GetLease())
+ {
+ this.owner.ThrowIfDeviceMismatch(device);
- this.owner.ThrowIfDisposed();
- this.owner.ThrowIfDeviceMismatch(device);
-
- return this.owner.D3D12Resource;
+ return this.owner.D3D12Resource;
+ }
}
///
@@ -141,7 +151,21 @@ partial class ReadWriteTexture3D
}
///
- protected override void OnDispose()
+ public void Dispose()
+ {
+ this.referenceTracker.Dispose();
+
+ GC.SuppressFinalize(this);
+ }
+
+ ///
+ ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
+ {
+ return ref this.referenceTracker;
+ }
+
+ ///
+ void ReferenceTracker.ITrackedObject.DangerousRelease()
{
if (this.owner.GraphicsDevice is GraphicsDevice device)
{
diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs
index c825d5f7..f4e7b075 100644
--- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs
+++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs
@@ -24,32 +24,34 @@ partial class ReadWriteTexture3D
///
public IReadOnlyTexture3D AsReadOnly()
{
- GraphicsDevice.ThrowIfDisposed();
- GraphicsDevice.ThrowIfDeviceLost();
-
- ThrowIfDisposed();
- ThrowIfIsNotInReadOnlyState();
-
- ReadOnly? readOnlyWrapper = this.readOnlyWrapper;
-
- if (readOnlyWrapper is not null)
+ using (GraphicsDevice.GetReferenceTracker().GetLease())
+ using (GetReferenceTracker().GetLease())
{
- return readOnlyWrapper;
- }
+ GraphicsDevice.ThrowIfDeviceLost();
- [MethodImpl(MethodImplOptions.NoInlining)]
- static ReadOnly InitializeWrapper(ReadWriteTexture3D texture)
- {
- return texture.readOnlyWrapper = new(texture);
- }
+ ThrowIfIsNotInReadOnlyState();
- return InitializeWrapper(this);
+ ReadOnly? readOnlyWrapper = this.readOnlyWrapper;
+
+ if (readOnlyWrapper is not null)
+ {
+ return readOnlyWrapper;
+ }
+
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ static ReadOnly InitializeWrapper(ReadWriteTexture3D texture)
+ {
+ return texture.readOnlyWrapper = new(texture);
+ }
+
+ return InitializeWrapper(this);
+ }
}
///
- protected override void OnDispose()
+ public override void Dispose()
{
- base.OnDispose();
+ base.Dispose();
this.readOnlyWrapper?.Dispose();
}
@@ -57,8 +59,13 @@ partial class ReadWriteTexture3D
///
/// A wrapper for a resource that has been temporarily transitioned to readonly.
///
- private sealed unsafe class ReadOnly : NativeObject, IReadOnlyTexture3D, GraphicsResourceHelper.IGraphicsResource
+ private sealed unsafe class ReadOnly : ReferenceTracker.ITrackedObject, IDisposable, IReadOnlyTexture3D, GraphicsResourceHelper.IGraphicsResource
{
+ ///
+ /// The owning object for the current instance.
+ ///
+ private ReferenceTracker referenceTracker;
+
///
/// The owning instance being wrapped.
///
@@ -75,6 +82,7 @@ partial class ReadWriteTexture3D
/// The owning instance to wrap.
public ReadOnly(ReadWriteTexture3D owner)
{
+ this.referenceTracker = new ReferenceTracker(this);
this.owner = owner;
owner.GraphicsDevice.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
@@ -109,12 +117,13 @@ partial class ReadWriteTexture3D
///
D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device)
{
- ThrowIfDisposed();
+ using (this.referenceTracker.GetLease())
+ using (this.owner.GetReferenceTracker().GetLease())
+ {
+ this.owner.ThrowIfDeviceMismatch(device);
- this.owner.ThrowIfDisposed();
- this.owner.ThrowIfDeviceMismatch(device);
-
- return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
+ return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
+ }
}
///
@@ -126,12 +135,13 @@ partial class ReadWriteTexture3D
///
ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device)
{
- ThrowIfDisposed();
+ using (this.referenceTracker.GetLease())
+ using (this.owner.GetReferenceTracker().GetLease())
+ {
+ this.owner.ThrowIfDeviceMismatch(device);
- this.owner.ThrowIfDisposed();
- this.owner.ThrowIfDeviceMismatch(device);
-
- return this.owner.D3D12Resource;
+ return this.owner.D3D12Resource;
+ }
}
///
@@ -141,7 +151,21 @@ partial class ReadWriteTexture3D
}
///
- protected override void OnDispose()
+ public void Dispose()
+ {
+ this.referenceTracker.Dispose();
+
+ GC.SuppressFinalize(this);
+ }
+
+ ///
+ ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
+ {
+ return ref this.referenceTracker;
+ }
+
+ ///
+ void ReferenceTracker.ITrackedObject.DangerousRelease()
{
if (this.owner.GraphicsDevice is GraphicsDevice device)
{