Fixed ConstantBuffer<T>.CopyTo(Buffer<T>) method

このコミットが含まれているのは:
Sergio Pedri committed 2021-02-16 01:19:38 +01:00
1 親 e8f9d918dc
コミット 49e648286f
3ファイル変更
+49 -40

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

+15 -26
ファイルの表示
@@ -8,29 +8,6 @@ namespace ComputeSharp.Graphics.Helpers
/// </summary>
internal static class MemoryHelper
{
/// <summary>
/// Copies the content of a source memory area to a target one.
/// </summary>
/// <param name="source">The source memory area to read from.</param>
/// <param name="destination">The pointer for the destination memory area.</param>
/// <param name="offset">The destination offset to start writing data to.</param>
/// <param name="length">The number of items to copy.</param>
/// <param name="elementSizeInBytes">The size in bytes of each item to copy.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void Copy(
void* source,
void* destination,
uint offset,
uint length,
uint elementSizeInBytes)
{
Buffer.MemoryCopy(
source,
(byte*)destination + offset * elementSizeInBytes,
ulong.MaxValue,
elementSizeInBytes * length);
}
/// <summary>
/// Copies the content of a source memory area to a target one, accounting for padding.
/// The destination memory area has padding for each element, while the source does not.
@@ -40,19 +17,31 @@ namespace ComputeSharp.Graphics.Helpers
/// <param name="offset">The destination offset to start writing data to.</param>
/// <param name="length">The number of items to copy.</param>
/// <param name="elementPitchInBytes">The padded size of each element.</param>
/// <param name="isPaddingInSourceToo">Indicates whether or not the source is padded too.</param>
public static unsafe void Copy<T>(
void* source,
void* destination,
uint offset,
uint length,
uint elementPitchInBytes)
uint elementPitchInBytes,
bool isPaddingInSourceToo)
where T : unmanaged
{
destination = (byte*)destination + offset * elementPitchInBytes;
for (int i = 0; i < length; i++)
if (isPaddingInSourceToo)
{
*(T*)&((byte*)destination)[i * elementPitchInBytes] = ((T*)source)[i];
for (int i = 0; i < length; i++)
{
*(T*)&((byte*)destination)[i * elementPitchInBytes] = *(T*)&((byte*)source)[i * elementPitchInBytes];
}
}
else
{
for (int i = 0; i < length; i++)
{
*(T*)&((byte*)destination)[i * elementPitchInBytes] = ((T*)source)[i];
}
}
}
+5 -3
ファイルの表示
@@ -199,12 +199,14 @@ namespace ComputeSharp.Resources
{
GraphicsDevice.ThrowIfDisposed();
ThrowIfDeviceMismatch(source.GraphicsDevice);
ThrowIfDisposed();
source.ThrowIfDeviceMismatch(GraphicsDevice);
source.ThrowIfDisposed();
if (!source.IsPaddingPresent &&
source.GraphicsDevice == GraphicsDevice)
Guard.IsLessThanOrEqualTo(source.Length, Length, nameof(Length));
if (!source.IsPaddingPresent)
{
// Directly copy the input buffer
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
+29 -11
ファイルの表示
@@ -3,7 +3,6 @@ using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using ComputeSharp.Core.Helpers;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Commands;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Graphics.Resources.Enums;
@@ -11,7 +10,6 @@ using ComputeSharp.Graphics.Resources.Interop;
using ComputeSharp.Resources;
using ComputeSharp.Resources.Debug;
using Microsoft.Toolkit.Diagnostics;
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
using FX = TerraFX.Interop.Windows;
namespace ComputeSharp
@@ -114,7 +112,8 @@ namespace ComputeSharp
resource.Pointer,
(uint)offset,
(uint)size,
(uint)GetPaddedSize());
(uint)GetPaddedSize(),
isPaddingInSourceToo: false);
}
else
{
@@ -133,18 +132,37 @@ namespace ComputeSharp
{
GraphicsDevice.ThrowIfDisposed();
ThrowIfDeviceMismatch(source.GraphicsDevice);
ThrowIfDisposed();
source.ThrowIfDeviceMismatch(GraphicsDevice);
source.ThrowIfDisposed();
if (source is ConstantBuffer<T> &&
source.GraphicsDevice == GraphicsDevice)
{
// Directly copy the input buffer, if possible
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
Guard.IsLessThanOrEqualTo(source.Length, Length, nameof(Length));
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, 0, source.D3D12Resource, 0,(ulong)SizeInBytes);
copyCommandList.ExecuteAndWaitForCompletion();
if (source is ConstantBuffer<T> buffer)
{
using ID3D12ResourceMap sourceMap = buffer.D3D12Resource->Map();
using ID3D12ResourceMap destinationMap = D3D12Resource->Map();
if (IsPaddingPresent)
{
MemoryHelper.Copy<T>(
sourceMap.Pointer,
destinationMap.Pointer,
0,
(uint)source.Length,
(uint)GetPaddedSize(),
isPaddingInSourceToo: true);
}
else
{
MemoryHelper.Copy(
sourceMap.Pointer,
0,
(uint)source.Length,
(uint)sizeof(T),
destinationMap.Pointer);
}
}
else CopyFromWithCpuBuffer(source);
}