Code refactoring, improved resource mapping

このコミットが含まれているのは:
Sergio Pedri committed 2020-04-14 12:44:44 +02:00
1 親 7fcf755e64
コミット fa779e678d
3ファイル変更
+61 -36

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

+40 -18
ファイルの表示
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;
using Vortice.Direct3D12;
namespace ComputeSharp.Graphics.Buffers.Abstract
@@ -22,11 +23,6 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// </summary>
public GraphicsDevice GraphicsDevice { get; }
/// <summary>
/// Gets the <see cref="IntPtr"/> associated to the currently mapped resource
/// </summary>
internal IntPtr MappedResource { get; private set; }
/// <summary>
/// Gets the <see cref="ID3D12Resource"/> object currently mapped
/// </summary>
@@ -45,24 +41,50 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// <summary>
/// Maps the current resource to a specified subresource
/// </summary>
/// <returns>An <see cref="IntPtr"/> for the newly mapped resource</returns>
internal IntPtr Map()
/// <returns>A <see cref="MappedResource"/> instance representing the mapped resource</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal MappedResource MapResource()
{
IntPtr mappedResource = NativeResource?.Map(0) ?? throw new InvalidOperationException("Missing resource");
MappedResource = mappedResource;
return mappedResource;
}
var resource = NativeResource ?? throw new InvalidOperationException("Missing resource");
/// <summary>
/// Unmaps a subresource specified by a given index
/// </summary>
internal void Unmap()
{
NativeResource?.Unmap(0);
MappedResource = IntPtr.Zero;
return new MappedResource(resource);
}
/// <inheritdoc/>
public virtual void Dispose() => NativeResource?.Dispose();
/// <summary>
/// A type representing a mapped memory resource
/// </summary>
internal readonly ref struct MappedResource
{
/// <summary>
/// The target <see cref="ID3D12Resource"/> to map
/// </summary>
private readonly ID3D12Resource Resource;
/// <summary>
/// The pointer to the mapped resource
/// </summary>
public readonly IntPtr Pointer;
/// <summary>
/// Creates a new <see cref="MappedResource"/> instance for a given <see cref="ID3D12Resource"/> value
/// </summary>
/// <param name="resource">The input <see cref="ID3D12Resource"/> instance to map</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public MappedResource(ID3D12Resource resource)
{
Resource = resource;
Pointer = resource.Map(0);
}
/// <inheritdoc cref="IDisposable.Dispose"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
Resource.Unmap(0);
}
}
}
}
+7 -6
ファイルの表示
@@ -30,9 +30,9 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
copyCommandList.CopyBufferRegion(this, offset * ElementSizeInBytes, transferBuffer, 0, count * ElementSizeInBytes);
copyCommandList.Flush();
transferBuffer.Map();
MemoryHelper.Copy(transferBuffer.MappedResource, 0, span, 0, count);
transferBuffer.Unmap();
using MappedResource resource = transferBuffer.MapResource();
MemoryHelper.Copy(resource.Pointer, 0, span, 0, count);
}
/// <inheritdoc/>
@@ -40,9 +40,10 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
{
using Buffer<T> transferBuffer = new Buffer<T>(GraphicsDevice, count, count * ElementSizeInBytes, BufferType.Transfer);
transferBuffer.Map();
MemoryHelper.Copy(span, transferBuffer.MappedResource, 0, count);
transferBuffer.Unmap();
using (MappedResource resource = transferBuffer.MapResource())
{
MemoryHelper.Copy(span, resource.Pointer, 0, count);
}
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
+14 -12
ファイルの表示
@@ -53,9 +53,10 @@ namespace ComputeSharp
Span<byte> temporarySpan = temporaryArray.AsSpan(0, count * PaddedElementSizeInBytes);
// Copy the padded data to the temporary array
Map();
MemoryHelper.Copy(MappedResource, offset * PaddedElementSizeInBytes, temporarySpan, 0, count * PaddedElementSizeInBytes);
Unmap();
using (MappedResource resource = MapResource())
{
MemoryHelper.Copy(resource.Pointer, offset * PaddedElementSizeInBytes, temporarySpan, 0, count * PaddedElementSizeInBytes);
}
ref byte tin = ref temporarySpan.GetPinnableReference();
ref T tout = ref span.GetPinnableReference();
@@ -72,9 +73,9 @@ namespace ComputeSharp
else
{
// Directly copy the data back if there is no padding
Map();
MemoryHelper.Copy(MappedResource, offset, span, 0, count);
Unmap();
using MappedResource resource = MapResource();
MemoryHelper.Copy(resource.Pointer, offset, span, 0, count);
}
}
@@ -97,18 +98,19 @@ namespace ComputeSharp
}
// Copy the padded data to the GPU
Map();
MemoryHelper.Copy(temporarySpan, MappedResource, offset * PaddedElementSizeInBytes, count * PaddedElementSizeInBytes);
Unmap();
using (MappedResource resource = MapResource())
{
MemoryHelper.Copy(temporarySpan, resource.Pointer, offset * PaddedElementSizeInBytes, count * PaddedElementSizeInBytes);
}
ArrayPool<byte>.Shared.Return(temporaryArray);
}
else
{
// Directly copy the input span if there is no padding
Map();
MemoryHelper.Copy(span, MappedResource, offset, count);
Unmap();
using MappedResource resource = MapResource();
MemoryHelper.Copy(span, resource.Pointer, offset, count);
}
}