Code refactoring to ResourceMap
このコミットが含まれているのは:
1 親
a6ad87200b
コミット
e34d050ab1
5ファイル変更
+61
-59
検索条件に一致するファイルはありません
@@ -1,7 +1,4 @@
|
||||
using ComputeSharp.Core.Interop;
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using TerraFX.Interop;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
@@ -37,55 +34,10 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
/// </summary>
|
||||
internal ID3D12Resource* D3D12Resource => this.d3D12Resource;
|
||||
|
||||
/// <summary>
|
||||
/// Maps the current resource to a specified subresource.
|
||||
/// </summary>
|
||||
/// <returns>A <see cref="MappedResource"/> instance representing the mapped resource.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal MappedResource MapResource()
|
||||
{
|
||||
return new MappedResource(d3D12Resource);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
{
|
||||
d3D12Resource.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* d3D12Resource;
|
||||
|
||||
/// <summary>
|
||||
/// The pointer to the mapped resource.
|
||||
/// </summary>
|
||||
public readonly void* 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* d3d12resource)
|
||||
{
|
||||
this.d3D12Resource = d3d12resource;
|
||||
|
||||
Pointer = d3d12resource->Map();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IDisposable.Dispose"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Dispose()
|
||||
{
|
||||
this.d3D12Resource->Unmap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
using ComputeSharp.Graphics.Buffers.Interop;
|
||||
using ComputeSharp.Graphics.Commands;
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
|
||||
|
||||
@@ -36,7 +38,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
|
||||
using MappedResource resource = transferBuffer.MapResource();
|
||||
using ID3D12ResourceMap resource = transferBuffer.D3D12Resource->Map();
|
||||
|
||||
MemoryHelper.Copy(resource.Pointer, 0, span, count);
|
||||
}
|
||||
@@ -46,7 +48,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
{
|
||||
using Buffer<T> transferBuffer = new Buffer<T>(GraphicsDevice, count, count * ElementSizeInBytes, BufferType.Upload);
|
||||
|
||||
using (MappedResource resource = transferBuffer.MapResource())
|
||||
using (ID3D12ResourceMap resource = transferBuffer.D3D12Resource->Map())
|
||||
{
|
||||
MemoryHelper.Copy(span, resource.Pointer, 0, count);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,9 @@ using ComputeSharp.Exceptions;
|
||||
using ComputeSharp.Graphics;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
using ComputeSharp.Graphics.Buffers.Interop;
|
||||
using ComputeSharp.Graphics.Commands;
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
|
||||
|
||||
@@ -49,7 +51,7 @@ namespace ComputeSharp
|
||||
/// <inheritdoc/>
|
||||
public override unsafe void GetData(Span<T> span, int offset, int count)
|
||||
{
|
||||
using MappedResource resource = MapResource();
|
||||
using ID3D12ResourceMap resource = D3D12Resource->Map();
|
||||
|
||||
if (IsPaddingPresent)
|
||||
{
|
||||
@@ -72,7 +74,7 @@ namespace ComputeSharp
|
||||
/// <inheritdoc/>
|
||||
public override unsafe void SetData(ReadOnlySpan<T> span, int offset, int count)
|
||||
{
|
||||
using MappedResource resource = MapResource();
|
||||
using ID3D12ResourceMap resource = D3D12Resource->Map();
|
||||
|
||||
if (IsPaddingPresent)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
using TerraFX.Interop;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers.Interop
|
||||
{
|
||||
/// <summary>
|
||||
/// A type representing a mapped memory resource.
|
||||
/// </summary>
|
||||
internal readonly unsafe ref struct ID3D12ResourceMap
|
||||
{
|
||||
/// <summary>
|
||||
/// The target <see cref="ID3D12Resource"/> to map.
|
||||
/// </summary>
|
||||
private readonly ID3D12Resource* d3D12Resource;
|
||||
|
||||
/// <summary>
|
||||
/// The pointer to the mapped resource.
|
||||
/// </summary>
|
||||
public readonly void* Pointer;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ID3D12ResourceMap"/> instance for a given <see cref="ID3D12Resource"/> value.
|
||||
/// </summary>
|
||||
/// <param name="d3d12resource">The input <see cref="ID3D12Resource"/> instance to map.</param>
|
||||
/// <param name="pointer">The pointer to the mapped resource.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ID3D12ResourceMap(ID3D12Resource* d3d12resource, void* pointer)
|
||||
{
|
||||
this.d3D12Resource = d3d12resource;
|
||||
|
||||
Pointer = pointer;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IDisposable.Dispose"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Dispose()
|
||||
{
|
||||
this.d3D12Resource->Unmap();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using System;
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics.Buffers.Interop;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using TerraFX.Interop;
|
||||
|
||||
namespace ComputeSharp.Graphics.Extensions
|
||||
@@ -10,18 +12,18 @@ namespace ComputeSharp.Graphics.Extensions
|
||||
internal static unsafe class ID3D12ResourceExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Maps a given <see cref="ID3D12Resource"/> memory area.
|
||||
/// Maps the current resource to a specified subresource.
|
||||
/// </summary>
|
||||
/// <param name="d3d12resource">The target <see cref="ID3D12Resource"/> to map.</param>
|
||||
/// <returns>A pointer to the mapped area for <paramref name="d3d12resource"/>.</returns>
|
||||
/// <exception cref="Exception">Thrown when the mapping operation fails.</exception>
|
||||
public static void* Map(this ref ID3D12Resource d3d12resource)
|
||||
/// <returns>A <see cref="ID3D12ResourceMap"/> instance representing the mapped resource.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ID3D12ResourceMap Map(this ref ID3D12Resource d3d12resource)
|
||||
{
|
||||
void* pointer;
|
||||
|
||||
d3d12resource.Map(0, null, &pointer).Assert();
|
||||
|
||||
return pointer;
|
||||
return new((ID3D12Resource*)Unsafe.AsPointer(ref d3d12resource), pointer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -31,6 +33,7 @@ namespace ComputeSharp.Graphics.Extensions
|
||||
/// <returns>A pointer to the mapped area for <paramref name="d3d12resource"/>.</returns>
|
||||
/// <exception cref="Exception">Thrown when the mapping operation fails.</exception>
|
||||
/// <remarks>Only use this method to unmap resources that were mapped with <see cref="Map(ref ID3D12Resource)"/>.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Unmap(this ref ID3D12Resource d3d12resource)
|
||||
{
|
||||
d3d12resource.Unmap(0, null);
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする