From 61d85829cbb503c5958cc9015d9bddafaf98464c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 29 Aug 2022 01:13:24 +0200 Subject: [PATCH] Add Texture1D type --- .../GraphicsDeviceMismatchException.cs | 24 + .../Extensions/Interop/AllocatorExtensions.cs | 51 ++ .../Interop/ID3D12DeviceExtensions.cs | 92 +++ .../Resources/Abstract/Texture1D{T}.cs | 598 ++++++++++++++++++ .../Resources/Abstract/Texture2D{T}.cs | 2 +- .../Resources/Abstract/Texture3D{T}.cs | 2 +- 6 files changed, 767 insertions(+), 2 deletions(-) create mode 100644 src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs diff --git a/src/ComputeSharp/Graphics/Exceptions/GraphicsDeviceMismatchException.cs b/src/ComputeSharp/Graphics/Exceptions/GraphicsDeviceMismatchException.cs index 6283a35b..2569600e 100644 --- a/src/ComputeSharp/Graphics/Exceptions/GraphicsDeviceMismatchException.cs +++ b/src/ComputeSharp/Graphics/Exceptions/GraphicsDeviceMismatchException.cs @@ -55,6 +55,18 @@ public sealed class GraphicsDeviceMismatchException : InvalidOperationException throw Create(buffer, buffer.GraphicsDevice, device); } + /// + /// Throws a new instance from the specified parameters. + /// + /// The type of values in the input buffer. + /// The input that was used. + /// The target instance that was used. + internal static void Throw(Texture1D texture, GraphicsDevice device) + where T : unmanaged + { + throw Create(texture, texture.GraphicsDevice, device); + } + /// /// Throws a new instance from the specified parameters. /// @@ -91,6 +103,18 @@ public sealed class GraphicsDeviceMismatchException : InvalidOperationException throw Create(buffer, buffer.GraphicsDevice, device); } + /// + /// Throws a new instance from the specified parameters. + /// + /// The type of values in the input buffer. + /// The input that was used. + /// The target instance that was used. + internal static void Throw(TransferTexture1D texture, GraphicsDevice device) + where T : unmanaged + { + throw Create(texture, texture.GraphicsDevice, device); + } + /// /// Throws a new instance from the specified parameters. /// diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs index fd451d38..c765cd21 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs @@ -90,6 +90,57 @@ internal static unsafe class AllocatorExtensions return allocation.Move(); } + /// + /// Creates a resource for a given 1D texture type. + /// + /// The instance in use. + /// The instance to use, if any. + /// The resource type currently in use. + /// The allocation mode to use for the new resource. + /// The value to use. + /// The width of the texture resource. + /// The default value for the resource. + /// An reference for the current object. + public static ComPtr CreateResource( + this ref D3D12MA_Allocator allocator, + D3D12MA_Pool* pool, + ResourceType resourceType, + AllocationMode allocationMode, + DXGI_FORMAT dxgiFormat, + uint width, + out D3D12_RESOURCE_STATES d3D12ResourceStates) + { + D3D12MA_ALLOCATION_FLAGS allocationFlags = allocationMode == AllocationMode.Default ? D3D12MA_ALLOCATION_FLAG_NONE : D3D12MA_ALLOCATION_FLAG_COMMITTED; + D3D12_RESOURCE_FLAGS d3D12ResourceFlags; + + (d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch + { + ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON), + ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS), + _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() + }; + + using ComPtr allocation = default; + + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex1D(dxgiFormat, width, mipLevels: 1, flags: d3D12ResourceFlags); + + D3D12MA_ALLOCATION_DESC allocationDesc = default; + allocationDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT; + allocationDesc.Flags = allocationFlags; + allocationDesc.CustomPool = pool; + + allocator.CreateResource( + &allocationDesc, + &d3D12ResourceDescription, + d3D12ResourceStates, + null, + allocation.GetAddressOf(), + null, + null).Assert(); + + return allocation.Move(); + } + /// /// Creates a resource for a given 2D texture type. /// diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs index 74ce050d..4bf28a0a 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs @@ -117,6 +117,66 @@ internal static unsafe class ID3D12DeviceExtensions return d3D12Resource.Move(); } + /// + /// Creates a committed resource for a given 1D texture type. + /// + /// The instance in use. + /// The resource type currently in use. + /// The value to use. + /// The width of the texture resource. + /// Indicates whether or not the current device has a cache coherent UMA architecture. + /// The default value for the resource. + /// An reference for the current texture. + public static ComPtr CreateCommittedResource( + this ref ID3D12Device d3D12Device, + ResourceType resourceType, + DXGI_FORMAT dxgiFormat, + uint width, + bool isCacheCoherentUMA, + out D3D12_RESOURCE_STATES d3D12ResourceStates) + { + D3D12_RESOURCE_FLAGS d3D12ResourceFlags; + + (d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch + { + ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON), + ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS), + _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() + }; + + using ComPtr d3D12Resource = default; + + D3D12_HEAP_PROPERTIES d3D12HeapProperties; + d3D12HeapProperties.CreationNodeMask = 1; + d3D12HeapProperties.VisibleNodeMask = 1; + + if (isCacheCoherentUMA) + { + d3D12HeapProperties.Type = D3D12_HEAP_TYPE_CUSTOM; + d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK; + d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0; + } + else + { + d3D12HeapProperties.Type = D3D12_HEAP_TYPE_DEFAULT; + d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; + d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; + } + + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex1D(dxgiFormat, width, mipLevels: 1, flags: d3D12ResourceFlags); + + d3D12Device.CreateCommittedResource( + &d3D12HeapProperties, + D3D12_HEAP_FLAG_NONE, + &d3D12ResourceDescription, + d3D12ResourceStates, + null, + Windows.__uuidof(), + d3D12Resource.GetVoidAddressOf()).Assert(); + + return d3D12Resource.Move(); + } + /// /// Creates a committed resource for a given 2D texture type. /// @@ -551,6 +611,38 @@ internal static unsafe class ID3D12DeviceExtensions return d3D12GraphicsCommandList.Move(); } + /// + /// Gets the layout data for a target resource. + /// + /// The target to use to get the layout info. + /// The value to use. + /// The width of the texture resource. + /// The resulting layout info for the resource. + /// The size in bytes of each row in the resource. + /// The total number of bytes for the resource. + public static void GetCopyableFootprint( + this ref ID3D12Device d3D12Device, + DXGI_FORMAT dxgiFormat, + uint width, + out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint, + out ulong rowSizeInBytes, + out ulong totalSizeInBytes) + { + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex1D(dxgiFormat, width); + ulong a; + ulong b; + + fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* p = &d3D12PlacedSubresourceFootprint) + { + uint _; + + d3D12Device.GetCopyableFootprints(&d3D12ResourceDescription, 0, 1, 0, p, &_, &a, &b); + } + + rowSizeInBytes = a; + totalSizeInBytes = b; + } + /// /// Gets the layout data for a target resource. /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs new file mode 100644 index 00000000..daf616b7 --- /dev/null +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs @@ -0,0 +1,598 @@ +using System; +using System.Runtime.CompilerServices; +using CommunityToolkit.Diagnostics; +using ComputeSharp.__Internals; +using ComputeSharp.Exceptions; +using ComputeSharp.Graphics.Commands; +using ComputeSharp.Graphics.Commands.Interop; +using ComputeSharp.Graphics.Extensions; +using ComputeSharp.Graphics.Helpers; +using ComputeSharp.Graphics.Resources.Helpers; +using ComputeSharp.Graphics.Resources.Interop; +using ComputeSharp.Interop; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; +using static TerraFX.Interop.DirectX.D3D12_COMMAND_LIST_TYPE; +using static TerraFX.Interop.DirectX.D3D12_RESOURCE_STATES; +using static TerraFX.Interop.DirectX.D3D12_SRV_DIMENSION; +using static TerraFX.Interop.DirectX.D3D12_UAV_DIMENSION; +using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType; + +#pragma warning disable CS0618 + +namespace ComputeSharp.Resources; + +/// +/// A representing a typed 1D texture stored on GPU memory. +/// +/// The type of items stored on the texture. +public unsafe abstract class Texture1D : NativeObject, IGraphicsResource, GraphicsResourceHelper.IGraphicsResource + where T : unmanaged +{ +#if NET6_0_OR_GREATER + /// + /// The instance used to retrieve . + /// + private ComPtr allocation; +#endif + + /// + /// The instance currently mapped. + /// + private ComPtr d3D12Resource; + + /// + /// The instance for the current resource. + /// + private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles; + + /// + /// 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; + + /// + /// The current value for the current resource. + /// + private D3D12_RESOURCE_STATES d3D12ResourceState; + + /// + /// Creates a new instance with the specified parameters. + /// + /// The associated with the current instance. + /// 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 Texture1D(GraphicsDevice device, int width, ResourceType resourceType, AllocationMode allocationMode, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport) + { + Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE1D_U_DIMENSION); + + using var _0 = device.GetReferenceTrackingLease(); + + device.ThrowIfDeviceLost(); + + if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), d3D12FormatSupport)) + { + UnsupportedTextureTypeException.ThrowForTexture1D(); + } + + GraphicsDevice = device; + +#if NET6_0_OR_GREATER + this.allocation = device.Allocator->CreateResource( + device.Pool, + resourceType, + allocationMode, + DXGIFormatHelper.GetForType(), + (uint)width, + out this.d3D12ResourceState); + + this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); +#else + this.d3D12Resource = device.D3D12Device->CreateCommittedResource( + resourceType, + DXGIFormatHelper.GetForType(), + (uint)width, + device.IsCacheCoherentUMA, + out this.d3D12ResourceState); +#endif + + this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON + ? D3D12_COMMAND_LIST_TYPE_COPY + : D3D12_COMMAND_LIST_TYPE_COMPUTE; + + device.D3D12Device->GetCopyableFootprint( + DXGIFormatHelper.GetForType(), + (uint)width, + out this.d3D12PlacedSubresourceFootprint, + out _, + out _); + + device.RegisterAllocatedResource(); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles); + + switch (resourceType) + { + case ResourceType.ReadOnly: + device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE1D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); + break; + case ResourceType.ReadWrite: + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE1D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE1D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); + break; + } + + 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 instance currently mapped. + /// + internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + + /// + /// Gets the instance for the current resource. + /// + internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle; + + /// + /// 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 width of the memory area to copy. + internal void CopyTo(ref T destination, int size, int sourceOffsetX, int width) + { + Guard.IsInRange(sourceOffsetX, 0, Width); + Guard.IsBetweenOrEqualTo(width, 1, Width); + Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); + Guard.IsGreaterThanOrEqualTo(size, width); + + using var _0 = GraphicsDevice.GetReferenceTrackingLease(); + using var _1 = GetReferenceTrackingLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + GraphicsDevice.D3D12Device->GetCopyableFootprint( + DXGIFormatHelper.GetForType(), + (uint)width, + out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintDestination, + out ulong rowSizeInBytes, + out ulong totalSizeInBytes); + +#if NET6_0_OR_GREATER + using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( + GraphicsDevice.Pool, + ResourceType.ReadBack, + AllocationMode.Default, + totalSizeInBytes); + + using ComPtr d3D12Resource = new(allocation.Get()->GetResource()); +#else + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( + ResourceType.ReadBack, + totalSizeInBytes, + GraphicsDevice.IsCacheCoherentUMA); +#endif + + using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) + { + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(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)sourceOffsetX, + sourceY: 0, + sourceZ: 0, + (uint)width, + height: 1, + depth: 1); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState); + } + + copyCommandList.ExecuteAndWaitForCompletion(); + } + + using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); + + fixed (void* destinationPointer = &destination) + { + Buffer.MemoryCopy( + source: resource.Pointer, + destination: destinationPointer, + destinationSizeInBytes: rowSizeInBytes, + sourceBytesToCopy: rowSizeInBytes); + } + } + + /// + /// 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 within . + /// The horizontal offset in the source texture. + /// The width of the memory area to copy. + internal void CopyTo(Texture1D destination, int sourceOffsetX, int destinationOffsetX, int width) + { + Guard.IsInRange(sourceOffsetX, 0, Width); + Guard.IsInRange(destinationOffsetX, 0, destination.Width); + Guard.IsBetweenOrEqualTo(width, 1, Width); + Guard.IsBetweenOrEqualTo(width, 1, destination.Width); + Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); + Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); + + using var _0 = GraphicsDevice.GetReferenceTrackingLease(); + using var _1 = GetReferenceTrackingLease(); + using var _2 = destination.GetReferenceTrackingLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + destination.ThrowIfDeviceMismatch(GraphicsDevice); + + D3D12_COMMAND_LIST_TYPE d3D12CommandListType = + this.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE || + destination.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE + ? D3D12_COMMAND_LIST_TYPE_COMPUTE + : D3D12_COMMAND_LIST_TYPE_COPY; + + using CommandList copyCommandList = new(GraphicsDevice, d3D12CommandListType); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); + copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(destination.D3D12Resource, destination.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); + } + + copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( + d3D12ResourceDestination: destination.D3D12Resource, + (uint)destinationOffsetX, + destinationY: 0, + destinationZ: 0, + d3D12ResourceSource: D3D12Resource, + (uint)sourceOffsetX, + sourceY: 0, + sourceZ: 0, + (uint)width, + height: 1, + depth: 1); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState); + copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(destination.D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, destination.d3D12ResourceState); + } + + copyCommandList.ExecuteAndWaitForCompletion(); + } + + /// + /// 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 horizontal offset within . + /// The width of the memory area to copy. + internal void CopyTo(ReadBackTexture1D destination, int sourceOffsetX, int destinationOffsetX, int width) + { + Guard.IsInRange(sourceOffsetX, 0, Width); + Guard.IsInRange(destinationOffsetX, 0, destination.Width); + Guard.IsBetweenOrEqualTo(width, 1, Width); + Guard.IsBetweenOrEqualTo(width, 1, destination.Width); + Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); + Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); + + using var _0 = GraphicsDevice.GetReferenceTrackingLease(); + using var _1 = GetReferenceTrackingLease(); + using var _2 = destination.GetReferenceTrackingLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + destination.ThrowIfDeviceMismatch(GraphicsDevice); + + using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); + } + + fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintDestination = &destination.D3D12PlacedSubresourceFootprint) + { + copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( + d3D12ResourceDestination: destination.D3D12Resource, + d3D12PlacedSubresourceFootprintDestination, + (uint)destinationOffsetX, + destinationY: 0, + destinationZ: 0, + d3D12ResourceSource: D3D12Resource, + (uint)sourceOffsetX, + sourceY: 0, + sourceZ: 0, + (uint)width, + height: 1, + depth: 1); + } + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(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 width of the memory area to write to. + internal void CopyFrom(ref T source, int size, int destinationOffsetX, int width) + { + Guard.IsInRange(destinationOffsetX, 0, Width); + Guard.IsBetweenOrEqualTo(width, 1, Width); + Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX)); + Guard.IsGreaterThanOrEqualTo(size, width); + + using var _0 = GraphicsDevice.GetReferenceTrackingLease(); + using var _1 = GetReferenceTrackingLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + GraphicsDevice.D3D12Device->GetCopyableFootprint( + DXGIFormatHelper.GetForType(), + (uint)width, + out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintSource, + out ulong rowSizeInBytes, + out ulong totalSizeInBytes); + +#if NET6_0_OR_GREATER + using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( + GraphicsDevice.Pool, + ResourceType.Upload, + AllocationMode.Default, + totalSizeInBytes); + + using ComPtr d3D12Resource = new(allocation.Get()->GetResource()); +#else + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( + ResourceType.Upload, + totalSizeInBytes, + GraphicsDevice.IsCacheCoherentUMA); +#endif + + using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) + fixed (void* sourcePointer = &source) + { + Buffer.MemoryCopy( + source: sourcePointer, + destination: resource.Pointer, + destinationSizeInBytes: rowSizeInBytes, + sourceBytesToCopy: rowSizeInBytes); + } + + using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); + } + + copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( + d3D12ResourceDestination: D3D12Resource, + destinationX: (uint)destinationOffsetX, + destinationY: 0, + destinationZ: 0, + d3D12ResourceSource: d3D12Resource.Get(), + &d3D12PlacedSubresourceFootprintSource, + sourceX: 0, + sourceY: 0, + sourceZ: 0, + (uint)width, + height: 1, + depth: 1); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(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 within . + /// The horizontal offset in the destination texture. + /// The width of the memory area to write to. + internal void CopyFrom(UploadTexture1D source, int sourceOffsetX, int destinationOffsetX, int width) + { + Guard.IsInRange(sourceOffsetX, 0, source.Width); + Guard.IsInRange(destinationOffsetX, 0, Width); + Guard.IsBetweenOrEqualTo(width, 1, Width); + Guard.IsBetweenOrEqualTo(width, 1, source.Width); + Guard.IsLessThanOrEqualTo(sourceOffsetX + width, source.Width, nameof(sourceOffsetX)); + Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX)); + + using var _0 = GraphicsDevice.GetReferenceTrackingLease(); + using var _1 = GetReferenceTrackingLease(); + using var _2 = source.GetReferenceTrackingLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + source.ThrowIfDeviceMismatch(GraphicsDevice); + + using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); + } + + fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintSource = &source.D3D12PlacedSubresourceFootprint) + { + copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( + d3D12ResourceDestination: D3D12Resource, + (uint)destinationOffsetX, + destinationY: 0, + destinationZ: 0, + d3D12ResourceSource: source.D3D12Resource, + d3D12PlacedSubresourceFootprintSource, + (uint)sourceOffsetX, + sourceY: 0, + sourceZ: 0, + (uint)width, + height: 1, + depth: 1); + } + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState); + } + + copyCommandList.ExecuteAndWaitForCompletion(); + } + + /// + private protected override void OnDispose() + { + this.d3D12Resource.Dispose(); +#if NET6_0_OR_GREATER + this.allocation.Dispose(); +#endif + + if (GraphicsDevice is GraphicsDevice device) + { + device.UnregisterAllocatedResource(); + device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles); + } + } + + /// + /// Throws an if the current resource is not in a readonly state. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + protected void ThrowIfIsNotInReadOnlyState() + { + if (this.d3D12ResourceState == D3D12_RESOURCE_STATE_UNORDERED_ACCESS) + { + static void Throw() + { + throw new InvalidOperationException( + "The texture is not currently in readonly mode. This API can only be used when creating a compute graph with " + + "the ComputeContext type, and after having used ComputeContext.Transition() to change the state of the texture."); + } + + Throw(); + } + } + + /// + /// 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); + } + } + + /// + internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) + { + using var _0 = GetReferenceTrackingLease(); + + ThrowIfDeviceMismatch(device); + + isNormalized = DXGIFormatHelper.IsNormalizedType(); + + return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); + } + + /// + internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device, out Lease lease) + { + lease = GetReferenceTrackingLease(); + + ThrowIfDeviceMismatch(device); + + return D3D12Resource; + } + + /// + internal (D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out Lease lease) + { + lease = GetReferenceTrackingLease(); + + ThrowIfDeviceMismatch(device); + + D3D12_RESOURCE_STATES d3D12ResourceStatesBefore = this.d3D12ResourceState; + D3D12_RESOURCE_STATES d3D12ResourceStatesAfter = ResourceStateHelper.GetD3D12ResourceStates(resourceState); + + this.d3D12ResourceState = d3D12ResourceStatesAfter; + + d3D12Resource = D3D12Resource; + + return (d3D12ResourceStatesBefore, d3D12ResourceStatesAfter); + } + + /// + D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) + { + using var _0 = GetReferenceTrackingLease(); + + ThrowIfDeviceMismatch(device); + + return D3D12GpuDescriptorHandle; + } + + /// + (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) + { + return ValidateAndGetGpuAndCpuDescriptorHandlesForClear(device, out isNormalized); + } + + /// + ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out Lease lease) + { + return ValidateAndGetID3D12Resource(device, out lease); + } + + /// + (D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out Lease lease) + { + return ValidateAndGetID3D12ResourceAndTransitionStates(device, resourceState, out d3D12Resource, out lease); + } +} diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 8527dc70..65d237f1 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -65,8 +65,8 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra /// 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 height 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. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 7871b946..8f36b574 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -65,8 +65,8 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra /// 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 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.