Refactored core buffer/texture APIs to use ref-s

このコミットが含まれているのは:
Sergio Pedri committed 2020-12-06 19:50:28 +01:00
1 親 e029b41488
コミット ccec496316
5ファイル変更
+116 -33

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

+25 -4
ファイルの表示
@@ -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
/// </summary>
/// <param name="destination">The input <see cref="Span{T}"/> to write data to.</param>
/// <param name="offset">The offset to start reading data from.</param>
public abstract void GetData(Span<T> destination, int offset);
public void GetData(Span<T> destination, int offset)
{
GetData(ref MemoryMarshal.GetReference(destination), destination.Length, offset);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Buffer{T}"/> instance and writes them into a target memory area.
/// </summary>
/// <param name="destination">The input memory area to write data to.</param>
/// <param name="size">The size of the memory area to write data to.</param>
/// <param name="offset">The offset to start reading data from.</param>
internal abstract void GetData(ref T destination, nint size, int offset);
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to the current <see cref="Buffer{T}"/> instance.
@@ -196,12 +208,21 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// <summary>
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to a specified area of the current <see cref="Buffer{T}"/> instance.
/// The input data will be written into the buffer starting at the specified offset, and all input items will be copied.
/// </summary>
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> to read data from.</param>
/// <param name="offset">The offset to start writing data to.</param>
/// <param name="count">The number of items to write.</param>
public abstract void SetData(ReadOnlySpan<T> source, int offset);
public void SetData(ReadOnlySpan<T> source, int offset)
{
SetData(ref MemoryMarshal.GetReference(source), source.Length, offset);
}
/// <summary>
/// Writes the contents of a given memory area to a specified area of the current <see cref="Buffer{T}"/> instance.
/// </summary>
/// <param name="source">The input memory area to read data from.</param>
/// <param name="size">The size of the input memory area to read data from.</param>
/// <param name="offset">The offset to start writing data to.</param>
internal abstract void SetData(ref T source, nint size, int offset);
/// <summary>
/// Writes the contents of a given <see cref="Buffer{T}"/> to the current <see cref="Buffer{T}"/> instance.
+10 -10
ファイルの表示
@@ -29,18 +29,18 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
}
/// <inheritdoc/>
public override unsafe void GetData(Span<T> 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<ID3D12Resource> 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);
}
}
/// <inheritdoc/>
public override unsafe void SetData(ReadOnlySpan<T> 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<ID3D12Resource> 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);
}
+33 -4
ファイルの表示
@@ -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
/// <param name="width">The width of the memory area to copy.</param>
/// <param name="height">The height of the memory area to copy.</param>
public void GetData(Span<T> destination, int x, int y, int width, int height)
{
GetData(ref MemoryMarshal.GetReference(destination), destination.Length, x, y, width, height);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target memory area.
/// </summary>
/// <param name="destination">The target memory area to write data to.</param>
/// <param name="size">The size of the target memory area to write data to.</param>
/// <param name="x">The horizontal offset in the source texture.</param>
/// <param name="y">The vertical offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
/// <param name="height">The height of the memory area to copy.</param>
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<T>(), (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
/// <param name="width">The width of the memory area to write to.</param>
/// <param name="height">The height of the memory area to write to.</param>
public void SetData(ReadOnlySpan<T> source, int x, int y, int width, int height)
{
SetData(ref MemoryMarshal.GetReference(source), source.Length, x, y, width, height);
}
/// <summary>
/// Writes the contents of a given memory area to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <param name="source">The input memory area to read data from.</param>
/// <param name="size">The size of the memory area to read data from.</param>
/// <param name="x">The horizontal offset in the destination texture.</param>
/// <param name="y">The vertical offset in the destination texture.</param>
/// <param name="width">The width of the memory area to write to.</param>
/// <param name="height">The height of the memory area to write to.</param>
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<T>(), (ulong)width, (uint)height);
@@ -417,7 +446,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes);
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
fixed (void* sourcePointer = source)
fixed (void* sourcePointer = &source)
{
MemoryHelper.Copy(
sourcePointer,
+38 -5
ファイルの表示
@@ -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
/// <param name="height">The height of the memory area to copy.</param>
/// <param name="depth">The depth of the memory area to copy.</param>
public void GetData(Span<T> 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);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture3D{T}"/> instance and writes them into a target memory area.
/// </summary>
/// <param name="destination">The target memory area to write data to.</param>
/// <param name="size">The size of the memory area to write data to.</param>
/// <param name="x">The horizontal offset in the source texture.</param>
/// <param name="y">The vertical offset in the source texture.</param>
/// <param name="z">The depthwise offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
/// <param name="height">The height of the memory area to copy.</param>
/// <param name="depth">The depth of the memory area to copy.</param>
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<T>(), (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
/// <param name="height">The height of the memory area to write to.</param>
/// <param name="depth">The depth of the memory area to write to.</param>
public void SetData(ReadOnlySpan<T> 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);
}
/// <summary>
/// Writes the contents of a given memory area to a specified area of the current <see cref="Texture3D{T}"/> instance.
/// </summary>
/// <param name="source">The input memory area to read data from.</param>
/// <param name="size">The size of the memory area to read data from.</param>
/// <param name="x">The horizontal offset in the destination texture.</param>
/// <param name="y">The vertical offset in the destination texture.</param>
/// <param name="z">The depthwise offseet in the destination texture.</param>
/// <param name="width">The width of the memory area to write to.</param>
/// <param name="height">The height of the memory area to write to.</param>
/// <param name="depth">The depth of the memory area to write to.</param>
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<T>(), (ulong)width, (uint)height, (ushort)depth);
@@ -432,14 +465,14 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
using ComPtr<ID3D12Resource> 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);
}
+10 -10
ファイルの表示
@@ -53,24 +53,24 @@ namespace ComputeSharp
}
/// <inheritdoc/>
public override unsafe void GetData(Span<T> 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<T>(
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
}
/// <inheritdoc/>
public override unsafe void SetData(ReadOnlySpan<T> 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);
}