From 7e9bd561ca3ce2a655f3c143cf62f9b6b52bbdd0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 5 Feb 2021 22:19:35 +0100 Subject: [PATCH] Code refactoring to texture copies --- .../ID3D12GraphicsCommandListExtensions.cs | 132 +++++++++++++----- .../Resources/Abstract/Texture2D{T}.cs | 121 +++++++++------- .../Resources/Abstract/Texture3D{T}.cs | 130 +++++++++-------- .../Abstract/TransferTexture2D{T}.cs | 16 ++- .../Abstract/TransferTexture3D{T}.cs | 16 ++- .../Resources/Views/TextureView2D{T}.cs | 18 +-- 6 files changed, 271 insertions(+), 162 deletions(-) diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs index 517eac57..04d8fb1a 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs @@ -8,42 +8,19 @@ namespace ComputeSharp.Graphics.Extensions /// internal static unsafe class ID3D12GraphicsCommandListExtensions { - /// - /// Copies a texture memory region from one resource (a buffer) to another (a texture). - /// - /// The instance in use. - /// The destination (a texture) to write to. - /// The horizontal offset in the destination texture. - /// The vertical offset in the destination texture. - /// The depthwise offset in the destination texture. - /// The source (a buffer) to read from. - /// The value describing . - public static void CopyTextureRegion( - this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, - ID3D12Resource* d3D12ResourceDestination, - uint x, - uint y, - ushort z, - ID3D12Resource* d3D12ResourceSource, - D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintSource) - { - D3D12_TEXTURE_COPY_LOCATION - d3D12TextureCopyLocationDestination = new(d3D12ResourceDestination, 0), - d3D12TextureCopyLocationSource = new(d3D12ResourceSource, in *d3D12PlacedSubresourceFootprintSource); - - d3D12GraphicsCommandList.CopyTextureRegion(&d3D12TextureCopyLocationDestination, x, y, z, &d3D12TextureCopyLocationSource, null); - } - /// /// Copies a texture memory region from one resource (a texture) to another (a buffer). /// /// The instance in use. /// The destination (a buffer) to write to. /// The value describing . + /// The horizontal offset in the destination buffer. + /// The vertical offset in the destination buffer. + /// The depthwise offset in the destination buffer. /// The source (a texture) to read from. - /// The horizontal offset in the source texture. - /// The vertical offset in the source texture. - /// The depthwise offset in the source texture. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. /// The width of the memory area to read from. /// The height of the memory area to read from. /// The depth of the memory area to read from. @@ -51,20 +28,103 @@ namespace ComputeSharp.Graphics.Extensions this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, ID3D12Resource* d3D12ResourceDestination, D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintDestination, + uint destinationX, + uint destinationY, + ushort destinationZ, ID3D12Resource* d3D12ResourceSource, - uint x, - uint y, - ushort z, + uint sourceX, + uint sourceY, + ushort sourceZ, + uint width, + uint height, + ushort depth) + { + D3D12_RESOURCE_DESC d3D12ResourceDescriptionSource = d3D12ResourceSource->GetDesc(); + D3D12_TEXTURE_COPY_LOCATION + d3D12TextureCopyLocationDestination = new(d3D12ResourceDestination, in *d3D12PlacedSubresourceFootprintDestination), + d3D12TextureCopyLocationSource = new(d3D12ResourceSource, 0); + D3D12_BOX d3D12Box; + D3D12_BOX* pD3D12Box; + + if (sourceX == 0 && sourceY == 0 && sourceZ == 0 && + width == d3D12ResourceDescriptionSource.Width && + height == d3D12ResourceDescriptionSource.Height && + depth == d3D12ResourceDescriptionSource.Depth) + { + pD3D12Box = null; + } + else + { + d3D12Box = new D3D12_BOX((int)sourceX, (int)sourceY, sourceZ, (int)(sourceX + width), (int)(sourceY + height), sourceZ + depth); + pD3D12Box = &d3D12Box; + } + + d3D12GraphicsCommandList.CopyTextureRegion( + &d3D12TextureCopyLocationDestination, + destinationX, + destinationY, + destinationZ, + &d3D12TextureCopyLocationSource, + pD3D12Box); + } + + /// + /// Copies a texture memory region from one resource (a buffer) to another (a texture). + /// + /// The instance in use. + /// The destination (a texture) to write to. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The depthwise offset in the destination texture. + /// The source (a buffer) to read from. + /// The value describing . + /// The horizontal offset in the source buffer. + /// The vertical offset in the source buffer. + /// The depthwise offset in the source buffer. + /// The width of the memory area to read from. + /// The height of the memory area to read from. + /// The depth of the memory area to read from. + public static void CopyTextureRegion( + this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, + ID3D12Resource* d3D12ResourceDestination, + uint destinationX, + uint destinationY, + ushort destinationZ, + ID3D12Resource* d3D12ResourceSource, + D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintSource, + uint sourceX, + uint sourceY, + ushort sourceZ, uint width, uint height, ushort depth) { D3D12_TEXTURE_COPY_LOCATION - d3D12TextureCopyLocationDestination = new(d3D12ResourceDestination, in *d3D12PlacedSubresourceFootprintDestination), - d3D12TextureCopyLocationSource = new(d3D12ResourceSource, 0); - D3D12_BOX d3D12Box = new((int)x, (int)y, z, (int)(x + width), (int)(y + height), z + depth); + d3D12TextureCopyLocationDestination = new(d3D12ResourceDestination, 0), + d3D12TextureCopyLocationSource = new(d3D12ResourceSource, in *d3D12PlacedSubresourceFootprintSource); + D3D12_BOX d3D12Box; + D3D12_BOX* pD3D12Box; - d3D12GraphicsCommandList.CopyTextureRegion(&d3D12TextureCopyLocationDestination, 0, 0, 0, &d3D12TextureCopyLocationSource, &d3D12Box); + if (sourceX == 0 && sourceY == 0 && sourceZ == 0 && + width == d3D12PlacedSubresourceFootprintSource->Footprint.Width && + height == d3D12PlacedSubresourceFootprintSource->Footprint.Height && + depth == d3D12PlacedSubresourceFootprintSource->Footprint.Depth) + { + pD3D12Box = null; + } + else + { + d3D12Box = new D3D12_BOX((int)sourceX, (int)sourceY, sourceZ, (int)(sourceX + width), (int)(sourceY + height), sourceZ + depth); + pD3D12Box = &d3D12Box; + } + + d3D12GraphicsCommandList.CopyTextureRegion( + &d3D12TextureCopyLocationDestination, + destinationX, + destinationY, + destinationZ, + &d3D12TextureCopyLocationSource, + pD3D12Box); } /// diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs index 18c60bc1..4f935baa 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs @@ -51,6 +51,11 @@ namespace ComputeSharp.Resources /// 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. /// @@ -89,6 +94,14 @@ namespace ComputeSharp.Resources ? 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) @@ -149,7 +162,7 @@ namespace ComputeSharp.Resources DXGIFormatHelper.GetForType(), (uint)width, (uint)height, - out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint, + out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintDestination, out ulong rowSizeInBytes, out ulong totalSizeInBytes); @@ -163,15 +176,18 @@ namespace ComputeSharp.Resources } copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( - d3D12Resource.Get(), - &d3D12PlacedSubresourceFootprint, - D3D12Resource, - (uint)x, - (uint)y, - 0, + d3D12ResourceDestination: d3D12Resource.Get(), + &d3D12PlacedSubresourceFootprintDestination, + destinationX: 0, + destinationY: 0, + destinationZ: 0, + d3D12ResourceSource: D3D12Resource, + sourceX: (uint)x, + sourceY: (uint)y, + sourceZ: 0, (uint)width, (uint)height, - 1); + depth: 1); if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) { @@ -189,7 +205,7 @@ namespace ComputeSharp.Resources resource.Pointer, (uint)height, rowSizeInBytes, - d3D12PlacedSubresourceFootprint.Footprint.RowPitch, + d3D12PlacedSubresourceFootprintDestination.Footprint.RowPitch, destinationPointer); } } @@ -220,14 +236,6 @@ namespace ComputeSharp.Resources Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); - GraphicsDevice.D3D12Device->GetCopyableFootprint( - DXGIFormatHelper.GetForType(), - (uint)destination.Width, - (uint)destination.Height, - out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint, - out ulong rowSizeInBytes, - out ulong totalSizeInBytes); - using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) @@ -235,16 +243,22 @@ namespace ComputeSharp.Resources copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); } - copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( - destination.D3D12Resource, - &d3D12PlacedSubresourceFootprint, - D3D12Resource, - (uint)x, - (uint)y, - 0, - (uint)width, - (uint)height, - 1); + 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) { @@ -281,7 +295,7 @@ namespace ComputeSharp.Resources DXGIFormatHelper.GetForType(), (uint)width, (uint)height, - out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint, + out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintSource, out ulong rowSizeInBytes, out ulong totalSizeInBytes); @@ -295,7 +309,7 @@ namespace ComputeSharp.Resources resource.Pointer, (uint)height, rowSizeInBytes, - d3D12PlacedSubresourceFootprint.Footprint.RowPitch); + d3D12PlacedSubresourceFootprintSource.Footprint.RowPitch); } using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); @@ -306,12 +320,18 @@ namespace ComputeSharp.Resources } copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( - D3D12Resource, - (uint)x, - (uint)y, - 0, - d3D12Resource.Get(), - &d3D12PlacedSubresourceFootprint); + 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) { @@ -347,14 +367,6 @@ namespace ComputeSharp.Resources Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); - GraphicsDevice.D3D12Device->GetCopyableFootprint( - DXGIFormatHelper.GetForType(), - (uint)source.Width, - (uint)source.Height, - out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint, - out ulong rowSizeInBytes, - out ulong totalSizeInBytes); - using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) @@ -362,13 +374,22 @@ namespace ComputeSharp.Resources copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); } - copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( - D3D12Resource, - (uint)x, - (uint)y, - 0, - source.D3D12Resource, - &d3D12PlacedSubresourceFootprint); + 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) { diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs index 90432b64..3385bd96 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs @@ -51,6 +51,11 @@ namespace ComputeSharp.Resources /// 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. /// @@ -93,6 +98,15 @@ namespace ComputeSharp.Resources ? D3D12_COMMAND_LIST_TYPE_COPY : D3D12_COMMAND_LIST_TYPE_COMPUTE; + GraphicsDevice.D3D12Device->GetCopyableFootprint( + DXGIFormatHelper.GetForType(), + (uint)width, + (uint)height, + (ushort)depth, + out this.d3D12PlacedSubresourceFootprint, + out _, + out _); + device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); switch (resourceType) @@ -164,7 +178,7 @@ namespace ComputeSharp.Resources (uint)width, (uint)height, (ushort)depth, - out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint, + out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintDestination, out ulong rowSizeInBytes, out ulong totalSizeInBytes); @@ -178,12 +192,15 @@ namespace ComputeSharp.Resources } copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( - d3D12Resource.Get(), - &d3D12PlacedSubresourceFootprint, - D3D12Resource, - (uint)x, - (uint)y, - (ushort)z, + d3D12ResourceDestination: d3D12Resource.Get(), + &d3D12PlacedSubresourceFootprintDestination, + destinationX: 0, + destinationY: 0, + destinationZ: 0, + d3D12ResourceSource: D3D12Resource, + sourceX: (uint)x, + sourceY: (uint)y, + sourceZ: (ushort)z, (uint)width, (uint)height, (ushort)depth); @@ -205,8 +222,8 @@ namespace ComputeSharp.Resources (uint)height, (uint)depth, rowSizeInBytes, - d3D12PlacedSubresourceFootprint.Footprint.RowPitch, - d3D12PlacedSubresourceFootprint.Footprint.RowPitch * (uint)height, + d3D12PlacedSubresourceFootprintDestination.Footprint.RowPitch, + d3D12PlacedSubresourceFootprintDestination.Footprint.RowPitch * (uint)height, destinationPointer); } } @@ -243,15 +260,6 @@ namespace ComputeSharp.Resources Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); Guard.IsLessThanOrEqualTo(z + depth, Depth, nameof(z)); - GraphicsDevice.D3D12Device->GetCopyableFootprint( - DXGIFormatHelper.GetForType(), - (uint)width, - (uint)height, - (ushort)depth, - out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint, - out ulong rowSizeInBytes, - out ulong totalSizeInBytes); - using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) @@ -259,16 +267,22 @@ namespace ComputeSharp.Resources copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); } - copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( - destination.D3D12Resource, - &d3D12PlacedSubresourceFootprint, - D3D12Resource, - (uint)x, - (uint)y, - (ushort)z, - (uint)width, - (uint)height, - (ushort)depth); + 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: (ushort)z, + (uint)width, + (uint)height, + (ushort)depth); + } if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) { @@ -285,7 +299,7 @@ namespace ComputeSharp.Resources /// 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 depthwise offseet in the destination texture. + /// The depthwise offset in the destination texture. /// The width of the memory area to write to. /// The height of the memory area to write to. /// The depth of the memory area to write to. @@ -311,7 +325,7 @@ namespace ComputeSharp.Resources (uint)width, (uint)height, (ushort)depth, - out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint, + out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintSource, out ulong rowSizeInBytes, out ulong totalSizeInBytes); @@ -326,8 +340,8 @@ namespace ComputeSharp.Resources (uint)height, (uint)depth, rowSizeInBytes, - d3D12PlacedSubresourceFootprint.Footprint.RowPitch, - d3D12PlacedSubresourceFootprint.Footprint.RowPitch * (uint)height); + d3D12PlacedSubresourceFootprintSource.Footprint.RowPitch, + d3D12PlacedSubresourceFootprintSource.Footprint.RowPitch * (uint)height); } using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); @@ -338,12 +352,18 @@ namespace ComputeSharp.Resources } copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( - D3D12Resource, - (uint)x, - (uint)y, - (ushort)z, - d3D12Resource.Get(), - &d3D12PlacedSubresourceFootprint); + d3D12ResourceDestination: D3D12Resource, + destinationX: (uint)x, + destinationY: (uint)y, + destinationZ: (ushort)z, + d3D12ResourceSource: d3D12Resource.Get(), + &d3D12PlacedSubresourceFootprintSource, + sourceX: 0, + sourceY: 0, + sourceZ: 0, + (uint)width, + (uint)height, + (ushort)depth); if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) { @@ -359,7 +379,7 @@ namespace ComputeSharp.Resources /// The input instance to read data from. /// The horizontal offset in the destination texture. /// The vertical offset in the destination texture. - /// The depthwise offseet in the destination texture. + /// The depthwise offset in the destination texture. /// The width of the memory area to write to. /// The height of the memory area to write to. /// The depth of the memory area to write to. @@ -385,15 +405,6 @@ namespace ComputeSharp.Resources Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); Guard.IsLessThanOrEqualTo(z + depth, Depth, nameof(z)); - GraphicsDevice.D3D12Device->GetCopyableFootprint( - DXGIFormatHelper.GetForType(), - (uint)width, - (uint)height, - (ushort)depth, - out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint, - out ulong rowSizeInBytes, - out ulong totalSizeInBytes); - using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) @@ -401,13 +412,22 @@ namespace ComputeSharp.Resources copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); } - copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( - D3D12Resource, - (uint)x, - (uint)y, - (ushort)z, - source.D3D12Resource, - &d3D12PlacedSubresourceFootprint); + fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintSource = &source.D3D12PlacedSubresourceFootprint) + { + copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( + d3D12ResourceDestination: D3D12Resource, + destinationX: (uint)x, + destinationY: (uint)y, + destinationZ: (ushort)z, + d3D12ResourceSource: source.D3D12Resource, + d3D12PlacedSubresourceFootprintSource, + sourceX: 0, + sourceY: 0, + sourceZ: 0, + (uint)width, + (uint)height, + (ushort)depth); + } if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) { diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs index a3a3ae28..f6669ebd 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -29,9 +29,9 @@ namespace ComputeSharp.Resources private readonly T* mappedData; /// - /// The stride in bytes for each row within . + /// The description for the current resource. /// - private readonly int strideInBytes; + private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; /// /// Creates a new instance with the specified parameters. @@ -61,13 +61,12 @@ namespace ComputeSharp.Resources DXGIFormatHelper.GetForType(), (uint)width, (uint)height, - out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint, - out ulong rowSizeInBytes, + out this.d3D12PlacedSubresourceFootprint, + out _, out ulong totalSizeInBytes); this.d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(resourceType, allocationMode, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; - this.strideInBytes = (int)d3D12PlacedSubresourceFootprint.Footprint.RowPitch; } /// @@ -90,6 +89,11 @@ namespace ComputeSharp.Resources /// internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + /// + /// Gets the value for the current resource. + /// + internal ref readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT D3D12PlacedSubresourceFootprint => ref this.d3D12PlacedSubresourceFootprint; + /// public TextureView2D View { @@ -98,7 +102,7 @@ namespace ComputeSharp.Resources { ThrowIfDisposed(); - return new(this.mappedData, Height, Width, this.strideInBytes); + return new(this.mappedData, Height, Width, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch); } } diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture3D{T}.cs index c7a84fe0..a0c7237e 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture3D{T}.cs @@ -29,9 +29,9 @@ namespace ComputeSharp.Resources private readonly T* mappedData; /// - /// The stride in bytes for each row within . + /// The description for the current resource. /// - private readonly int strideInBytes; + private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; /// /// Creates a new instance with the specified parameters. @@ -65,13 +65,12 @@ namespace ComputeSharp.Resources (uint)width, (uint)height, (ushort)depth, - out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint, - out ulong rowSizeInBytes, + out this.d3D12PlacedSubresourceFootprint, + out _, out ulong totalSizeInBytes); using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(resourceType, allocationMode, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; - this.strideInBytes = (int)d3D12PlacedSubresourceFootprint.Footprint.RowPitch; } /// @@ -98,6 +97,11 @@ namespace ComputeSharp.Resources /// 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 { @@ -106,7 +110,7 @@ namespace ComputeSharp.Resources { ThrowIfDisposed(); - return new(this.mappedData, Height, Width, Depth, this.strideInBytes); + return new(this.mappedData, Height, Width, Depth, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch); } } diff --git a/src/ComputeSharp.Graphics/Resources/Views/TextureView2D{T}.cs b/src/ComputeSharp.Graphics/Resources/Views/TextureView2D{T}.cs index 4fda03a4..6bea689f 100644 --- a/src/ComputeSharp.Graphics/Resources/Views/TextureView2D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Views/TextureView2D{T}.cs @@ -54,9 +54,9 @@ namespace ComputeSharp private readonly int height; /// - /// The stride of the specified 2D region. + /// The row pitch of the specified 2D region. /// - private readonly int strideInBytes; + private readonly int pitchInBytes; /// /// Initializes a new instance of the struct with the specified parameters. @@ -64,13 +64,13 @@ namespace ComputeSharp /// The pointer to the start of the memory area to map. /// The width of the 2D memory area to map. /// The height of the 2D memory area to map. - /// The stride in bytes of the 2D memory area to map. - internal TextureView2D(T* pointer, int width, int height, int strideInBytes) + /// The row pitch in bytes of the 2D memory area to map. + internal TextureView2D(T* pointer, int width, int height, int pitchInBytes) { this.pointer = pointer; this.width = width; this.height = height; - this.strideInBytes = strideInBytes; + this.pitchInBytes = pitchInBytes; } /// @@ -134,7 +134,7 @@ namespace ComputeSharp ThrowHelper.ThrowIndexOutOfRangeException(); } - return ref *((T*)((byte*)this.pointer + (y * this.strideInBytes)) + x); + return ref *((T*)((byte*)this.pointer + (y * this.pitchInBytes)) + x); } } @@ -294,7 +294,7 @@ namespace ComputeSharp [MethodImpl(MethodImplOptions.AggressiveInlining)] public T* DangerousGetAddressAndByteStride(out int strideInBytes) { - strideInBytes = this.strideInBytes; + strideInBytes = this.pitchInBytes; return this.pointer; } @@ -313,7 +313,7 @@ namespace ComputeSharp ThrowHelper.ThrowArgumentOutOfRangeExceptionForRow(); } - return new((byte*)this.pointer + (y * this.strideInBytes), this.width); + return new((byte*)this.pointer + (y * this.pitchInBytes), this.width); } /// @@ -323,7 +323,7 @@ namespace ComputeSharp /// Whether or not was correctly assigned. public bool TryGetSpan(out Span span) { - if (this.strideInBytes == this.width) + if (this.pitchInBytes == this.width) { span = new(this.pointer, Length);