diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs index 437d26c5..dcd3c52e 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs @@ -2,6 +2,7 @@ using System.Buffers; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using ComputeSharp.Core.Interop; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Buffers.Enums; @@ -159,7 +160,18 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// /// The input to write data to. /// The offset to start reading data from. - public abstract void GetData(Span destination, int offset); + public void GetData(Span destination, int offset) + { + GetData(ref MemoryMarshal.GetReference(destination), destination.Length, offset); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target memory area. + /// + /// The input memory area to write data to. + /// The size of the memory area to write data to. + /// The offset to start reading data from. + internal abstract void GetData(ref T destination, nint size, int offset); /// /// Writes the contents of a given array to the current instance. @@ -196,12 +208,21 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// /// Writes the contents of a given to a specified area of the current instance. - /// The input data will be written into the buffer starting at the specified offset, and all input items will be copied. /// /// The input to read data from. /// The offset to start writing data to. - /// The number of items to write. - public abstract void SetData(ReadOnlySpan source, int offset); + public void SetData(ReadOnlySpan source, int offset) + { + SetData(ref MemoryMarshal.GetReference(source), source.Length, offset); + } + + /// + /// Writes the contents of a given memory area to a specified area of the current instance. + /// + /// The input memory area to read data from. + /// The size of the input memory area to read data from. + /// The offset to start writing data to. + internal abstract void SetData(ref T source, nint size, int offset); /// /// Writes the contents of a given to the current instance. diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs index ee91333e..b2fcec46 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs @@ -29,18 +29,18 @@ namespace ComputeSharp.Graphics.Buffers.Abstract } /// - public override unsafe void GetData(Span destination, int offset) + internal override unsafe void GetData(ref T destination, nint size, int offset) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); Guard.IsInRange(offset, 0, Length, nameof(offset)); - Guard.IsLessThanOrEqualTo((uint)offset + destination.Length, (uint)Length, nameof(destination)); + Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size)); nint byteOffset = (nint)offset * sizeof(T), - byteSize = (nint)destination.Length * sizeof(T); + byteSize = size * sizeof(T); using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, (ulong)byteSize); @@ -51,40 +51,40 @@ namespace ComputeSharp.Graphics.Buffers.Abstract } using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); - fixed (void* destinationPointer = destination) + fixed (void* destinationPointer = &destination) { MemoryHelper.Copy( resource.Pointer, 0u, - (uint)destination.Length, + (uint)size, (uint)sizeof(T), destinationPointer); } } /// - public override unsafe void SetData(ReadOnlySpan source, int offset) + internal override unsafe void SetData(ref T source, nint size, int offset) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); Guard.IsInRange(offset, 0, Length, nameof(offset)); - Guard.IsLessThanOrEqualTo((uint)offset + source.Length, (uint)Length, nameof(source)); + Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size)); nint byteOffset = (nint)offset * sizeof(T), - byteSize = (nint)source.Length * sizeof(T); + byteSize = size * sizeof(T); using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, (ulong)byteSize); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) - fixed (void* sourcePointer = source) + fixed (void* sourcePointer = &source) { MemoryHelper.Copy( sourcePointer, 0u, - (uint)source.Length, + (uint)size, (uint)sizeof(T), resource.Pointer); } diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index 5935c197..8f422a98 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using ComputeSharp.Core.Interop; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Buffers.Enums; @@ -229,6 +230,20 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The width of the memory area to copy. /// The height of the memory area to copy. public void GetData(Span destination, int x, int y, int width, int height) + { + GetData(ref MemoryMarshal.GetReference(destination), destination.Length, x, y, width, height); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target memory area. + /// + /// The target memory area to write data to. + /// The size of the target memory area to write data to. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + internal void GetData(ref T destination, nint size, int x, int y, int width, int height) { GraphicsDevice.ThrowIfDisposed(); @@ -240,7 +255,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); - Guard.HasSizeGreaterThanOrEqualTo(destination, width * height, nameof(destination)); + Guard.IsGreaterThanOrEqualTo(size, (nint)width * height, nameof(size)); D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(DXGIFormatHelper.GetForType(), (ulong)width, (uint)height); @@ -274,7 +289,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); - fixed (void* destinationPointer = destination) + fixed (void* destinationPointer = &destination) { MemoryHelper.Copy( resource.Pointer, @@ -392,6 +407,20 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The width of the memory area to write to. /// The height of the memory area to write to. public void SetData(ReadOnlySpan source, int x, int y, int width, int height) + { + SetData(ref MemoryMarshal.GetReference(source), source.Length, x, y, width, height); + } + + /// + /// Writes the contents of a given memory area to a specified area of the current instance. + /// + /// The input memory area to read data from. + /// The size of the memory area to read data from. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The width of the memory area to write to. + /// The height of the memory area to write to. + public void SetData(ref T source, nint size, int x, int y, int width, int height) { GraphicsDevice.ThrowIfDisposed(); @@ -403,7 +432,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); - Guard.HasSizeGreaterThanOrEqualTo(source, width * height, nameof(source)); + Guard.IsGreaterThanOrEqualTo(size, (nint)width * height, nameof(size)); D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(DXGIFormatHelper.GetForType(), (ulong)width, (uint)height); @@ -417,7 +446,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) - fixed (void* sourcePointer = source) + fixed (void* sourcePointer = &source) { MemoryHelper.Copy( sourcePointer, diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index 2ee65640..2c8029bf 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using ComputeSharp.Core.Interop; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Buffers.Enums; @@ -221,6 +222,22 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The height of the memory area to copy. /// The depth of the memory area to copy. public void GetData(Span destination, int x, int y, int z, int width, int height, int depth) + { + GetData(ref MemoryMarshal.GetReference(destination), destination.Length, x, y, z, width, height, depth); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target memory area. + /// + /// The target memory area to write data to. + /// The size of the memory area to write data to. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public void GetData(ref T destination, nint size, int x, int y, int z, int width, int height, int depth) { GraphicsDevice.ThrowIfDisposed(); @@ -235,7 +252,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); Guard.IsLessThanOrEqualTo(z + depth, Depth, nameof(z)); - Guard.HasSizeGreaterThanOrEqualTo(destination, width * height * depth, nameof(destination)); + Guard.IsGreaterThanOrEqualTo(size, (nint)width * height * depth, nameof(size)); D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(DXGIFormatHelper.GetForType(), (ulong)width, (uint)height, (ushort)depth); @@ -269,7 +286,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); - fixed (void* destinationPointer = destination) + fixed (void* destinationPointer = &destination) { MemoryHelper.Copy( resource.Pointer, @@ -404,6 +421,22 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The height of the memory area to write to. /// The depth of the memory area to write to. public void SetData(ReadOnlySpan source, int x, int y, int z, int width, int height, int depth) + { + SetData(ref MemoryMarshal.GetReference(source), source.Length, x, y, z, width, height, depth); + } + + /// + /// Writes the contents of a given memory area to a specified area of the current instance. + /// + /// The input memory area to read data from. + /// The size of the memory area to read data from. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The depthwise offseet in the destination texture. + /// The width of the memory area to write to. + /// The height of the memory area to write to. + /// The depth of the memory area to write to. + public void SetData(ref T source, nint size, int x, int y, int z, int width, int height, int depth) { GraphicsDevice.ThrowIfDisposed(); @@ -418,7 +451,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); Guard.IsLessThanOrEqualTo(z + depth, Depth, nameof(z)); - Guard.HasSizeGreaterThanOrEqualTo(source, width * height * depth, nameof(source)); + Guard.IsGreaterThanOrEqualTo(size, (nint)width * height * depth, nameof(size)); D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(DXGIFormatHelper.GetForType(), (ulong)width, (uint)height, (ushort)depth); @@ -432,14 +465,14 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) - fixed (void* sourcePointer = source) + fixed (void* sourcePointer = &source) { MemoryHelper.Copy( sourcePointer, resource.Pointer, (uint)height, (uint)depth, - rowSizeInBytes, + rowSizeInBytes, d3D12PlacedSubresourceFootprint.Footprint.RowPitch, d3D12PlacedSubresourceFootprint.Footprint.RowPitch * (uint)height); } diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 9fc1bc86..0c8d2e08 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -53,24 +53,24 @@ namespace ComputeSharp } /// - public override unsafe void GetData(Span destination, int offset) + internal override unsafe void GetData(ref T destination, nint size, int offset) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); Guard.IsInRange(offset, 0, Length, nameof(offset)); - Guard.IsLessThanOrEqualTo((uint)offset + destination.Length, (uint)Length, nameof(destination)); + Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size)); using ID3D12ResourceMap resource = D3D12Resource->Map(); - fixed (void* destinationPointer = destination) + fixed (void* destinationPointer = &destination) { if (IsPaddingPresent) { MemoryHelper.Copy( resource.Pointer, (uint)offset, - (uint)destination.Length, + (uint)size, (uint)GetPaddedSize(), destinationPointer); } @@ -79,7 +79,7 @@ namespace ComputeSharp MemoryHelper.Copy( resource.Pointer, (uint)offset, - (uint)destination.Length, + (uint)size, (uint)sizeof(T), destinationPointer); } @@ -87,17 +87,17 @@ namespace ComputeSharp } /// - public override unsafe void SetData(ReadOnlySpan source, int offset) + internal override unsafe void SetData(ref T source, nint size, int offset) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); Guard.IsInRange(offset, 0, Length, nameof(offset)); - Guard.IsLessThanOrEqualTo((uint)offset + source.Length, (uint)Length, nameof(source)); + Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size)); using ID3D12ResourceMap resource = D3D12Resource->Map(); - fixed (void* sourcePointer = source) + fixed (void* sourcePointer = &source) { if (IsPaddingPresent) { @@ -105,7 +105,7 @@ namespace ComputeSharp sourcePointer, resource.Pointer, (uint)offset, - (uint)source.Length, + (uint)size, (uint)GetPaddedSize()); } else @@ -113,7 +113,7 @@ namespace ComputeSharp MemoryHelper.Copy( sourcePointer, (uint)offset, - (uint)source.Length, + (uint)size, (uint)sizeof(T), resource.Pointer); }