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(GraphicsDevice2 device, ID3D12Resource* d3d12resource)
{
D3D12Resource = d3d12resource;
GraphicsDevice = device;
}
///
/// Gets the associated with the current instance.
///
public GraphicsDevice2 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 OnDispose()
{
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();
}
}
}
}