diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource2.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource2.cs new file mode 100644 index 00000000..593dcc5c --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource2.cs @@ -0,0 +1,86 @@ +using ComputeSharp.Core.Interop; +using ComputeSharp.Graphics.Extensions; +using System; +using System.Runtime.CompilerServices; +using TerraFX.Interop; + +namespace ComputeSharp.Graphics.Buffers.Abstract +{ + /// + /// An abstract that represents a graphics resource object. + /// + public abstract unsafe class GraphicsResource2 : NativeObject + { + /// + /// The instance currently mapped. + /// + protected internal readonly ID3D12Resource* D3D12Resource; + + /// + /// Creates a new instance with the specified parameters. + /// + /// The associated with the current instance. + protected GraphicsResource2(GraphicsDevice device, ID3D12Resource* d3d12resource) + { + D3D12Resource = d3d12resource; + + GraphicsDevice = device; + } + + /// + /// Gets the associated with the current instance. + /// + public GraphicsDevice GraphicsDevice { get; } + + /// + /// Maps the current resource to a specified subresource. + /// + /// A instance representing the mapped resource. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal MappedResource MapResource() + { + return new MappedResource(D3D12Resource); + } + + /// + protected override void Dispose(bool disposing) + { + D3D12Resource->Release(); + } + + /// + /// A type representing a mapped memory resource. + /// + internal readonly ref struct MappedResource + { + /// + /// The target to map. + /// + private readonly ID3D12Resource* d3D12Resource; + + /// + /// The pointer to the mapped resource. + /// + public readonly IntPtr Pointer; + + /// + /// Creates a new instance for a given value. + /// + /// The input instance to map. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public MappedResource(ID3D12Resource* d3d12resource) + { + d3D12Resource = d3d12resource; + + Pointer = d3d12resource->Map(); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Dispose() + { + d3D12Resource->Unmap(); + } + } + } +} diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12ResourceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12ResourceExtensions.cs new file mode 100644 index 00000000..6b32626d --- /dev/null +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12ResourceExtensions.cs @@ -0,0 +1,42 @@ +using System; +using System.Runtime.InteropServices; +using TerraFX.Interop; +using FX = TerraFX.Interop.Windows; + +namespace ComputeSharp.Graphics.Extensions +{ + /// + /// A with extensions for the type. + /// + internal static unsafe class ID3D12ResourceExtensions + { + /// + /// Maps a given memory area. + /// + /// The target to map. + /// A pointer to the mapped area for . + /// Thrown when the mapping operation fails. + public static IntPtr Map(this ref ID3D12Resource d3d12resource) + { + IntPtr ptr; + + int result = d3d12resource.Map(0, null, (void**)&ptr); + + if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); + + return ptr; + } + + /// + /// Maps a given memory area. + /// + /// The target to map. + /// A pointer to the mapped area for . + /// Thrown when the mapping operation fails. + /// Only use this method to unmap resources that were mapped with . + public static void Unmap(this ref ID3D12Resource d3d12resource) + { + d3d12resource.Unmap(0, null); + } + } +}