using System;
using Vortice.DirectX.Direct3D12;
namespace ComputeSharp.Graphics.Buffers.Abstract
{
///
/// An abstract that represents a graphics resource object
///
public abstract class GraphicsResource : IDisposable
{
///
/// Creates a new instance with the specified parameters
///
/// The associated with the current instance
protected GraphicsResource(GraphicsDevice device)
{
GraphicsDevice = device;
}
///
/// Gets the associated with the current instance
///
public GraphicsDevice GraphicsDevice { get; }
///
/// Gets the associated to the currently mapped resource
///
internal IntPtr MappedResource { get; private set; }
///
/// Gets the object currently mapped
///
protected internal ID3D12Resource? NativeResource { get; set; }
///
/// Gets the instance for the current resource
///
protected internal CpuDescriptorHandle? NativeCpuDescriptorHandle { get; protected set; }
///
/// Gets the instance for the current resource
///
protected internal GpuDescriptorHandle? NativeGpuDescriptorHandle { get; protected set; }
///
/// Maps the current resource to a specified subresource
///
/// The index of the target subresource to map
/// An for the newly mapped resource
internal IntPtr Map(int subresource)
{
IntPtr mappedResource = NativeResource?.Map(subresource) ?? throw new InvalidOperationException("Missing resource");
MappedResource = mappedResource;
return mappedResource;
}
///
/// Unmaps a subresource specified by a given index
///
/// The index of the subresource to unmap
internal void Unmap(int subresource)
{
NativeResource?.Unmap(subresource);
MappedResource = IntPtr.Zero;
}
///
public virtual void Dispose() => NativeResource?.Dispose();
}
}