using System.Runtime.CompilerServices; using ComputeSharp.__Internals; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Graphics.Resources.Enums; using ComputeSharp.Graphics.Resources.Interop; using ComputeSharp.Interop; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; using static TerraFX.Interop.D3D12_RESOURCE_STATES; using static TerraFX.Interop.D3D12_SRV_DIMENSION; using static TerraFX.Interop.D3D12_UAV_DIMENSION; using FX = TerraFX.Interop.Windows; #pragma warning disable CS0618 namespace ComputeSharp.Resources { /// /// A representing a typed 2D texture stored on GPU memory. /// /// The type of items stored on the texture. public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper.IGraphicsResource where T : unmanaged { /// /// The instance currently mapped. /// private ComPtr d3D12Resource; /// /// The instance for the current resource. /// private readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; /// /// The instance for the current resource. /// internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle; /// /// The default value for the current resource. /// private readonly D3D12_RESOURCE_STATES d3D12ResourceState; /// /// The value to use for copy operations. /// private readonly D3D12_COMMAND_LIST_TYPE d3D12CommandListType; /// /// 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 height of the texture. /// The width of the texture. /// The resource type for the current texture. /// The allocation mode to use for the new resource. /// The format support for the current texture type. private protected Texture2D(GraphicsDevice device, int width, int height, ResourceType resourceType, AllocationMode allocationMode, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport) { device.ThrowIfDisposed(); Guard.IsBetweenOrEqualTo(width, 1, FX.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION, nameof(width)); Guard.IsBetweenOrEqualTo(height, 1, FX.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION, nameof(height)); if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), d3D12FormatSupport)) { UnsupportedTextureTypeException.ThrowForTexture2D(); } GraphicsDevice = device; Width = width; Height = height; this.d3D12Resource = device.D3D12Device->CreateCommittedResource( resourceType, allocationMode, DXGIFormatHelper.GetForType(), (uint)width, (uint)height, device.IsCacheCoherentUMA, out this.d3D12ResourceState); this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON ? D3D12_COMMAND_LIST_TYPE_COPY : D3D12_COMMAND_LIST_TYPE_COMPUTE; GraphicsDevice.D3D12Device->GetCopyableFootprint( DXGIFormatHelper.GetForType(), (uint)width, (uint)height, out this.d3D12PlacedSubresourceFootprint, out _, out _); device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); switch (resourceType) { case ResourceType.ReadOnly: device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE2D, D3D12CpuDescriptorHandle); break; case ResourceType.ReadWrite: device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, D3D12CpuDescriptorHandle); break; } } /// /// Gets the associated with the current instance. /// public GraphicsDevice GraphicsDevice { get; } /// /// Gets the width of the current texture. /// public int Width { get; } /// /// Gets the height of the current texture. /// public int Height { get; } /// /// Gets the instance currently mapped. /// internal ID3D12Resource* D3D12Resource => this.d3D12Resource; /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// /// The target memory area to write data to. /// The size of the target memory area to write data to. /// The horizontal offset in the source texture. /// The vertical offset in the source texture. /// The width of the memory area to copy. /// The height of the memory area to copy. internal void CopyTo(ref T destination, int size, int x, int y, int width, int height) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); Guard.IsInRange(x, 0, Width, nameof(x)); Guard.IsInRange(y, 0, Height, nameof(y)); Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width)); Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); Guard.IsGreaterThanOrEqualTo(size, (nint)width * height, nameof(size)); GraphicsDevice.D3D12Device->GetCopyableFootprint( DXGIFormatHelper.GetForType(), (uint)width, (uint)height, out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintDestination, out ulong rowSizeInBytes, out ulong totalSizeInBytes); using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, AllocationMode.Default, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) { if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) { copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); } copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( d3D12ResourceDestination: d3D12Resource.Get(), &d3D12PlacedSubresourceFootprintDestination, destinationX: 0, destinationY: 0, destinationZ: 0, d3D12ResourceSource: D3D12Resource, sourceX: (uint)x, sourceY: (uint)y, sourceZ: 0, (uint)width, (uint)height, depth: 1); if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) { copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState); } copyCommandList.ExecuteAndWaitForCompletion(); } using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); fixed (void* destinationPointer = &destination) { MemoryHelper.Copy( resource.Pointer, (uint)height, rowSizeInBytes, d3D12PlacedSubresourceFootprintDestination.Footprint.RowPitch, destinationPointer); } } /// /// Reads the contents of the specified range from the current instance and writes them into a target instance. /// /// The target instance to write data to. /// The horizontal offset in the source texture. /// The vertical offset in the source texture. /// The width of the memory area to copy. /// The height of the memory area to copy. internal void CopyTo(ReadBackTexture2D destination, int x, int y, int width, int height) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); destination.ThrowIfDeviceMismatch(GraphicsDevice); destination.ThrowIfDisposed(); Guard.IsInRange(x, 0, Width, nameof(x)); Guard.IsInRange(y, 0, Height, nameof(y)); Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width)); Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); Guard.IsBetweenOrEqualTo(width, 1, destination.Width, nameof(width)); Guard.IsBetweenOrEqualTo(height, 1, destination.Height, nameof(height)); Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) { copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); } fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintDestination = &destination.D3D12PlacedSubresourceFootprint) { copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( d3D12ResourceDestination: destination.D3D12Resource, d3D12PlacedSubresourceFootprintDestination, destinationX: 0, destinationY: 0, destinationZ: 0, d3D12ResourceSource: D3D12Resource, sourceX: (uint)x, sourceY: (uint)y, sourceZ: 0, (uint)width, (uint)height, 1); } if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) { copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState); } copyCommandList.ExecuteAndWaitForCompletion(); } /// /// Writes the contents of a given memory area to a specified area of the current instance. /// /// The input memory area to read data from. /// The size of the memory area to read data from. /// The horizontal offset in the destination texture. /// The vertical offset in the destination texture. /// The width of the memory area to write to. /// The height of the memory area to write to. internal void CopyFrom(ref T source, int size, int x, int y, int width, int height) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); Guard.IsInRange(x, 0, Width, nameof(x)); Guard.IsInRange(y, 0, Height, nameof(y)); Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width)); Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); Guard.IsGreaterThanOrEqualTo(size, (nint)width * height, nameof(size)); GraphicsDevice.D3D12Device->GetCopyableFootprint( DXGIFormatHelper.GetForType(), (uint)width, (uint)height, out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintSource, out ulong rowSizeInBytes, out ulong totalSizeInBytes); using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, AllocationMode.Default, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) fixed (void* sourcePointer = &source) { MemoryHelper.Copy( sourcePointer, resource.Pointer, (uint)height, rowSizeInBytes, d3D12PlacedSubresourceFootprintSource.Footprint.RowPitch); } using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) { copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); } copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( d3D12ResourceDestination: D3D12Resource, destinationX: (uint)x, destinationY: (uint)y, destinationZ: 0, d3D12ResourceSource: d3D12Resource.Get(), &d3D12PlacedSubresourceFootprintSource, sourceX: 0, sourceY: 0, sourceZ: 0, (uint)width, (uint)height, depth: 1); if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) { copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState); } copyCommandList.ExecuteAndWaitForCompletion(); } /// /// Writes the contents of a given instance to a specified area of the current instance. /// /// The input instance to read data from. /// The horizontal offset in the destination texture. /// The vertical offset in the destination texture. /// The width of the memory area to write to. /// The height of the memory area to write to. internal void CopyFrom(UploadTexture2D source, int x, int y, int width, int height) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); source.ThrowIfDeviceMismatch(GraphicsDevice); source.ThrowIfDisposed(); Guard.IsInRange(x, 0, Width, nameof(x)); Guard.IsInRange(y, 0, Height, nameof(y)); Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width)); Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); Guard.IsBetweenOrEqualTo(width, 1, source.Width, nameof(width)); Guard.IsBetweenOrEqualTo(height, 1, source.Height, nameof(height)); Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) { copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); } fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintSource = &source.D3D12PlacedSubresourceFootprint) { copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( d3D12ResourceDestination: D3D12Resource, destinationX: (uint)x, destinationY: (uint)y, destinationZ: 0, d3D12ResourceSource: source.D3D12Resource, d3D12PlacedSubresourceFootprintSource, sourceX: 0, sourceY: 0, sourceZ: 0, (uint)width, (uint)height, depth: 1); } if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) { copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState); } copyCommandList.ExecuteAndWaitForCompletion(); } /// protected override bool OnDispose() { this.d3D12Resource.Dispose(); if (GraphicsDevice?.IsDisposed == false) { GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); } return true; } /// /// 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); } } /// D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) { ThrowIfDisposed(); ThrowIfDeviceMismatch(device); return D3D12GpuDescriptorHandle; } } }