diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs index a09e4da2..f0e6d3ee 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs @@ -5,6 +5,7 @@ using ComputeSharp.Graphics.Buffers.Interop; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; +using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; @@ -31,6 +32,15 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// public override unsafe void GetData(Span span, int offset, int count) { + GraphicsDevice.ThrowIfDisposed(); + + ThrowIfDisposed(); + + Guard.IsInRange(offset, 0, Length, nameof(offset)); + Guard.IsInRange(count, 0, Length, nameof(count)); + Guard.IsLessThanOrEqualTo((uint)offset + count, (uint)Length, nameof(count)); + Guard.IsInRangeFor(count, span, nameof(count)); + nint byteOffset = (nint)offset * ElementSizeInBytes, byteSize = (nint)count * ElementSizeInBytes; @@ -51,6 +61,15 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// public override unsafe void SetData(ReadOnlySpan span, int offset, int count) { + GraphicsDevice.ThrowIfDisposed(); + + ThrowIfDisposed(); + + Guard.IsInRange(offset, 0, Length, nameof(offset)); + Guard.IsInRange(count, 0, Length, nameof(count)); + Guard.IsLessThanOrEqualTo((uint)offset + count, (uint)Length, nameof(count)); + Guard.IsInRangeFor(count, span, nameof(count)); + nint byteOffset = (nint)offset * ElementSizeInBytes, byteSize = (nint)count * ElementSizeInBytes; @@ -71,7 +90,14 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// public override unsafe void SetData(Buffer buffer) { - if (!buffer.IsPaddingPresent) + GraphicsDevice.ThrowIfDisposed(); + buffer.GraphicsDevice.ThrowIfDisposed(); + + ThrowIfDisposed(); + buffer.ThrowIfDisposed(); + + if (!buffer.IsPaddingPresent && + buffer.GraphicsDevice == GraphicsDevice) { // Directly copy the input buffer using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 5a5de630..f9547c63 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -12,6 +12,7 @@ using ComputeSharp.Graphics.Buffers.Views; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; +using Microsoft.Toolkit.Diagnostics; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; namespace ComputeSharp @@ -55,6 +56,15 @@ namespace ComputeSharp /// public override unsafe void GetData(Span span, int offset, int count) { + GraphicsDevice.ThrowIfDisposed(); + + ThrowIfDisposed(); + + Guard.IsInRange(offset, 0, Length, nameof(offset)); + Guard.IsInRange(count, 0, Length, nameof(count)); + Guard.IsLessThanOrEqualTo((uint)offset + count, (uint)Length, nameof(count)); + Guard.IsInRangeFor(count, span, nameof(count)); + using ID3D12ResourceMap resource = D3D12Resource->Map(); if (IsPaddingPresent) @@ -78,6 +88,15 @@ namespace ComputeSharp /// public override unsafe void SetData(ReadOnlySpan span, int offset, int count) { + GraphicsDevice.ThrowIfDisposed(); + + ThrowIfDisposed(); + + Guard.IsInRange(offset, 0, Length, nameof(offset)); + Guard.IsInRange(count, 0, Length, nameof(count)); + Guard.IsLessThanOrEqualTo((uint)offset + count, (uint)Length, nameof(count)); + Guard.IsInRangeFor(count, span, nameof(count)); + using ID3D12ResourceMap resource = D3D12Resource->Map(); if (IsPaddingPresent) @@ -101,7 +120,14 @@ namespace ComputeSharp /// public override unsafe void SetData(Buffer buffer) { - if (buffer is ConstantBuffer) + GraphicsDevice.ThrowIfDisposed(); + buffer.GraphicsDevice.ThrowIfDisposed(); + + ThrowIfDisposed(); + buffer.ThrowIfDisposed(); + + if (buffer is ConstantBuffer && + buffer.GraphicsDevice == GraphicsDevice) { // Directly copy the input buffer, if possible using CommandList copyCommandList = new CommandList(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);