using System;
using System.Runtime.CompilerServices;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Interop;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using static TerraFX.Interop.DirectX.D3D12_FORMAT_SUPPORT1;
using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType;
#pragma warning disable CA1063
namespace ComputeSharp.Resources;
///
/// A representing a typed 3D texture stored on on CPU memory, that can be used to transfer data to/from the GPU.
///
/// The type of items stored on the texture.
public abstract unsafe partial class TransferTexture3D : IReferenceTrackedObject, IGraphicsResource
where T : unmanaged
{
///
/// The value for the current instance.
///
private ReferenceTracker referenceTracker;
#if NET6_0_OR_GREATER
///
/// The instance used to retrieve .
///
private ComPtr allocation;
#endif
///
/// The instance currently mapped.
///
private ComPtr d3D12Resource;
///
/// The pointer to the start of the mapped buffer data.
///
private readonly T* mappedData;
///
/// The description for the current resource.
///
private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint;
///
/// Creates a new instance with the specified parameters.
///
/// The associated with the current instance.
/// The width of the texture.
/// The height of the texture.
/// The depth of the texture.
/// The resource type for the current texture.
/// The allocation mode to use for the new resource.
private protected TransferTexture3D(GraphicsDevice device, int width, int height, int depth, ResourceType resourceType, AllocationMode allocationMode)
{
this.referenceTracker = new ReferenceTracker(this);
default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(depth, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
using ReferenceTracker.Lease _0 = device.GetReferenceTracker().GetLease();
device.ThrowIfDeviceLost();
if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE3D))
{
UnsupportedTextureTypeException.ThrowForTexture3D();
}
GraphicsDevice = device;
device.D3D12Device->GetCopyableFootprint(
DXGIFormatHelper.GetForType(),
(uint)width,
(uint)height,
(ushort)depth,
out this.d3D12PlacedSubresourceFootprint,
out _,
out ulong totalSizeInBytes);
#if NET6_0_OR_GREATER
this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes);
this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource());
#else
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, totalSizeInBytes, device.IsCacheCoherentUMA);
#endif
device.RegisterAllocatedResource();
this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
this.d3D12Resource.Get()->SetName(this);
}
///
public GraphicsDevice GraphicsDevice { get; }
///
/// Gets the width of the current texture.
///
public int Width => (int)this.d3D12PlacedSubresourceFootprint.Footprint.Width;
///
/// Gets the height of the current texture.
///
public int Height => (int)this.d3D12PlacedSubresourceFootprint.Footprint.Height;
///
/// Gets the depth of the current texture.
///
public int Depth => (int)this.d3D12PlacedSubresourceFootprint.Footprint.Depth;
///
/// Gets the instance currently mapped.
///
internal ID3D12Resource* D3D12Resource => this.d3D12Resource;
///
/// Gets the value for the current resource.
///
internal ref readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT D3D12PlacedSubresourceFootprint => ref this.d3D12PlacedSubresourceFootprint;
///
public TextureView3D View
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
return new(this.mappedData, Width, Height, Depth, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch);
}
}
///
void IReferenceTrackedObject.DangerousOnDispose()
{
this.d3D12Resource.Dispose();
#if NET6_0_OR_GREATER
this.allocation.Dispose();
#endif
if (GraphicsDevice is GraphicsDevice device)
{
device.UnregisterAllocatedResource();
}
}
///
/// Throws a if the target device doesn't match the current one.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void ThrowIfDeviceMismatch(GraphicsDevice device)
{
if (GraphicsDevice != device)
{
GraphicsDeviceMismatchException.Throw(this, device);
}
}
}