From 5100ab471abf6d7558fc46738d92dd9c874adea8 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 24 Jul 2022 14:53:29 +0200 Subject: [PATCH] Move all Guard checks at the start of each method --- .../Helpers/SwapChainManager.Rendering.cs | 28 ++- .../Graphics/Resources/Abstract/Buffer{T}.cs | 20 +-- .../Resources/Abstract/StructuredBuffer{T}.cs | 58 +++--- .../Resources/Abstract/Texture2D{T}.cs | 128 +++++++------- .../Resources/Abstract/Texture3D{T}.cs | 166 +++++++++--------- .../Resources/Abstract/TransferBuffer{T}.cs | 6 +- .../Abstract/TransferTexture2D{T}.cs | 6 +- .../Abstract/TransferTexture3D{T}.cs | 8 +- .../Graphics/Resources/ConstantBuffer{T}.cs | 30 ++-- 9 files changed, 224 insertions(+), 226 deletions(-) diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs index dfb1801a..99c5edf2 100644 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs @@ -27,27 +27,25 @@ partial class SwapChainManager /// The instance to use to render frames. public async void StartRenderLoop(IFrameRequestQueue? frameRequestQueue, IShaderRunner shaderRunner) { + Guard.IsNotNull(shaderRunner); + using (GetReferenceTracker().GetLease()) + using (await this.setupSemaphore.LockAsync()) { - Guard.IsNotNull(shaderRunner); + this.renderCancellationTokenSource?.Cancel(); - using (await this.setupSemaphore.LockAsync()) - { - this.renderCancellationTokenSource?.Cancel(); + await this.renderSemaphore.WaitAsync(); - await this.renderSemaphore.WaitAsync(); + Thread newRenderThread = new(static args => ((SwapChainManager)args!).SwitchAndStartRenderLoop()); - Thread newRenderThread = new(static args => ((SwapChainManager)args!).SwitchAndStartRenderLoop()); + this.frameRequestQueue = frameRequestQueue; + this.shaderRunner = shaderRunner; + this.renderCancellationTokenSource = new CancellationTokenSource(); + this.renderThread = newRenderThread; + this.renderSemaphore = new SemaphoreSlim(0, 1); + this.isResizePending = true; - this.frameRequestQueue = frameRequestQueue; - this.shaderRunner = shaderRunner; - this.renderCancellationTokenSource = new CancellationTokenSource(); - this.renderThread = newRenderThread; - this.renderSemaphore = new SemaphoreSlim(0, 1); - this.isResizePending = true; - - newRenderThread.Start(this); - } + newRenderThread.Start(this); } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 1054071b..0a1f4745 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -65,20 +65,20 @@ 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) { + 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); + } + using (device.GetReferenceTracker().GetLease()) { device.ThrowIfDeviceLost(); - 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); - } - if (TypeInfo.IsDoubleOrContainsDoubles && device.D3D12Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS).DoublePrecisionFloatShaderOps == 0) { diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index be9dcddb..1a49abdb 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -32,15 +32,15 @@ public abstract class StructuredBuffer : Buffer /// internal override unsafe void CopyTo(ref T destination, int sourceOffset, int count) { + Guard.IsBetweenOrEqualTo(count, 0, Length); + Guard.IsInRange(sourceOffset, 0, Length); + Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset)); + using (GraphicsDevice.GetReferenceTracker().GetLease()) using (GetReferenceTracker().GetLease()) { GraphicsDevice.ThrowIfDeviceLost(); - Guard.IsBetweenOrEqualTo(count, 0, Length); - Guard.IsInRange(sourceOffset, 0, Length); - Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset)); - if (GraphicsDevice.IsCacheCoherentUMA) { using ID3D12ResourceMap resource = D3D12Resource->Map(); @@ -105,6 +105,13 @@ public abstract class StructuredBuffer : Buffer /// internal override unsafe void CopyTo(Buffer destination, int sourceOffset, int destinationOffset, int count) { + 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)); + using (GraphicsDevice.GetReferenceTracker().GetLease()) using (GetReferenceTracker().GetLease()) using (destination.GetReferenceTracker().GetLease()) @@ -113,13 +120,6 @@ public abstract class StructuredBuffer : Buffer destination.ThrowIfDeviceMismatch(GraphicsDevice); - 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) { nint sourceByteOffset = (nint)sourceOffset * sizeof(T); @@ -151,6 +151,13 @@ public abstract class StructuredBuffer : Buffer /// The number of items to read. internal unsafe void CopyTo(ReadBackBuffer destination, int sourceOffset, int destinationOffset, int count) { + 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)); + using (GraphicsDevice.GetReferenceTracker().GetLease()) using (GetReferenceTracker().GetLease()) using (destination.GetReferenceTracker().GetLease()) @@ -159,13 +166,6 @@ public abstract class StructuredBuffer : Buffer destination.ThrowIfDeviceMismatch(GraphicsDevice); - 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 ID3D12ResourceMap resource = D3D12Resource->Map(); @@ -196,15 +196,15 @@ public abstract class StructuredBuffer : Buffer /// internal override unsafe void CopyFrom(ref T source, int offset, int length) { + Guard.IsBetweenOrEqualTo(length, 0, Length); + Guard.IsInRange(offset, 0, Length); + Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); + using (GraphicsDevice.GetReferenceTracker().GetLease()) using (GetReferenceTracker().GetLease()) { GraphicsDevice.ThrowIfDeviceLost(); - Guard.IsBetweenOrEqualTo(length, 0, Length); - Guard.IsInRange(offset, 0, Length); - Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); - if (GraphicsDevice.IsCacheCoherentUMA) { using ID3D12ResourceMap resource = D3D12Resource->Map(); @@ -273,6 +273,13 @@ public abstract class StructuredBuffer : Buffer /// The number of items to read. internal unsafe void CopyFrom(UploadBuffer source, int sourceOffset, int destinationOffset, int count) { + 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)); + using (GraphicsDevice.GetReferenceTracker().GetLease()) using (GetReferenceTracker().GetLease()) using (source.GetReferenceTracker().GetLease()) @@ -281,13 +288,6 @@ public abstract class StructuredBuffer : Buffer source.ThrowIfDeviceMismatch(GraphicsDevice); - 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 ID3D12ResourceMap resource = D3D12Resource->Map(); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 48b68a5c..4dfd4596 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -72,13 +72,13 @@ 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) { + Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION); + Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION); + using (device.GetReferenceTracker().GetLease()) { device.ThrowIfDeviceLost(); - 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(); @@ -171,26 +171,26 @@ 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) { + 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); + using (GraphicsDevice.GetReferenceTracker().GetLease()) using (GetReferenceTracker().GetLease()) { GraphicsDevice.ThrowIfDeviceLost(); - 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( - DXGIFormatHelper.GetForType(), - (uint)width, - (uint)height, - out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintDestination, - out ulong rowSizeInBytes, - out ulong totalSizeInBytes); + DXGIFormatHelper.GetForType(), + (uint)width, + (uint)height, + out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintDestination, + out ulong rowSizeInBytes, + out ulong totalSizeInBytes); #if NET6_0_OR_GREATER using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( @@ -262,6 +262,19 @@ 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) { + 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 (GraphicsDevice.GetReferenceTracker().GetLease()) using (GetReferenceTracker().GetLease()) using (destination.GetReferenceTracker().GetLease()) @@ -270,19 +283,6 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra 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 @@ -332,6 +332,19 @@ 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) { + 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 (GraphicsDevice.GetReferenceTracker().GetLease()) using (GetReferenceTracker().GetLease()) using (destination.GetReferenceTracker().GetLease()) @@ -340,19 +353,6 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra 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)); - using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) @@ -397,19 +397,19 @@ 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) { + 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); + using (GraphicsDevice.GetReferenceTracker().GetLease()) using (GetReferenceTracker().GetLease()) { GraphicsDevice.ThrowIfDeviceLost(); - 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, @@ -486,6 +486,19 @@ 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) { + 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 (GraphicsDevice.GetReferenceTracker().GetLease()) using (GetReferenceTracker().GetLease()) using (source.GetReferenceTracker().GetLease()) @@ -494,19 +507,6 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra source.ThrowIfDeviceMismatch(GraphicsDevice); - 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) diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 2b85ba0b..7afe5fe3 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -73,14 +73,14 @@ 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) { + 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); + using (device.GetReferenceTracker().GetLease()) { device.ThrowIfDeviceLost(); - 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(); @@ -183,22 +183,22 @@ 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) { + 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); + using (GraphicsDevice.GetReferenceTracker().GetLease()) using (GetReferenceTracker().GetLease()) { GraphicsDevice.ThrowIfDeviceLost(); - 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, @@ -283,6 +283,25 @@ 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) { + 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 (GraphicsDevice.GetReferenceTracker().GetLease()) using (GetReferenceTracker().GetLease()) using (destination.GetReferenceTracker().GetLease()) @@ -291,25 +310,6 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra 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 @@ -362,6 +362,25 @@ 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) { + 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 (GraphicsDevice.GetReferenceTracker().GetLease()) using (GetReferenceTracker().GetLease()) using (destination.GetReferenceTracker().GetLease()) @@ -370,25 +389,6 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra 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)); - using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) @@ -435,22 +435,22 @@ 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) { + 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); + using (GraphicsDevice.GetReferenceTracker().GetLease()) using (GetReferenceTracker().GetLease()) { GraphicsDevice.ThrowIfDeviceLost(); - 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, @@ -533,6 +533,25 @@ 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) { + 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 (GraphicsDevice.GetReferenceTracker().GetLease()) using (GetReferenceTracker().GetLease()) using (source.GetReferenceTracker().GetLease()) @@ -541,25 +560,6 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra source.ThrowIfDeviceMismatch(GraphicsDevice); - 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) diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs index ca7397b2..53f03fcb 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -44,13 +44,13 @@ 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) { + // 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(); - // 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); - GraphicsDevice = device; Length = length; diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs index 783b6146..4cf69cb3 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -50,13 +50,13 @@ 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) { + Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION); + Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION); + using (device.GetReferenceTracker().GetLease()) { device.ThrowIfDeviceLost(); - 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)) { UnsupportedTextureTypeException.ThrowForTexture2D(); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs index 29243629..149c53f9 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs @@ -51,14 +51,14 @@ 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) { + 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); + using (device.GetReferenceTracker().GetLease()) { device.ThrowIfDeviceLost(); - 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)) { UnsupportedTextureTypeException.ThrowForTexture3D(); diff --git a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs index 2958f74e..fcd3b810 100644 --- a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs @@ -57,15 +57,15 @@ public sealed class ConstantBuffer : Buffer /// internal override unsafe void CopyTo(ref T destination, int offset, int length) { + Guard.IsBetweenOrEqualTo(length, 0, Length); + Guard.IsInRange(offset, 0, Length); + Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); + using (GraphicsDevice.GetReferenceTracker().GetLease()) using (GetReferenceTracker().GetLease()) { 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) @@ -85,6 +85,13 @@ public sealed class ConstantBuffer : Buffer /// internal override unsafe void CopyTo(Buffer destination, int sourceOffset, int destinationOffset, int length) { + 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)); + using (GraphicsDevice.GetReferenceTracker().GetLease()) using (GetReferenceTracker().GetLease()) using (destination.GetReferenceTracker().GetLease()) @@ -93,13 +100,6 @@ public sealed class ConstantBuffer : Buffer 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(); @@ -121,15 +121,15 @@ public sealed class ConstantBuffer : Buffer /// internal override unsafe void CopyFrom(ref T source, int offset, int length) { + Guard.IsBetweenOrEqualTo(length, 0, Length); + Guard.IsInRange(offset, 0, Length); + Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); + using (GraphicsDevice.GetReferenceTracker().GetLease()) using (GetReferenceTracker().GetLease()) { 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)