diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index d8305f6e..a9e40779 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -14,7 +14,6 @@ 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; namespace ComputeSharp.Graphics.Buffers.Abstract { @@ -243,12 +242,16 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); Guard.HasSizeGreaterThanOrEqualTo(destination, width * height, nameof(destination)); - ulong - rowByteSize = (ulong)((nint)width * Unsafe.SizeOf()), - rowPitchSize = (rowByteSize + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((ulong)(FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)), - resourceByteSize = rowPitchSize * (uint)height; + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(DXGIFormatHelper.GetForType(), (ulong)width, (uint)height); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, resourceByteSize); + GraphicsDevice.D3D12Device->GetCopyableFootprint( + &d3D12ResourceDescription, + out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint, + out _, + out ulong rowSizeInBytes, + out ulong totalSizeInBytes); + + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, totalSizeInBytes); using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE)) { @@ -256,9 +259,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract copyCommandList.CopyTextureRegion( d3D12Resource.Get(), - (uint)Unsafe.SizeOf(), + &d3D12PlacedSubresourceFootprint, D3D12Resource, - DXGIFormatHelper.GetForType(), (uint)x, (uint)y, 0, @@ -272,7 +274,13 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); - MemoryHelper.Copy(resource.Pointer, width, height, rowByteSize, rowPitchSize, destination); + MemoryHelper.Copy( + resource.Pointer, + width, + height, + rowSizeInBytes, + d3D12PlacedSubresourceFootprint.Footprint.RowPitch, + destination); } /// @@ -395,16 +403,26 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); Guard.HasSizeGreaterThanOrEqualTo(source, width * height, nameof(source)); - ulong - rowByteSize = (ulong)((nint)width * Unsafe.SizeOf()), - rowPitchSize = (rowByteSize + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((ulong)(FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)), - resourceByteSize = rowPitchSize * (uint)height; + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(DXGIFormatHelper.GetForType(), (ulong)width, (uint)height); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, resourceByteSize); + GraphicsDevice.D3D12Device->GetCopyableFootprint( + &d3D12ResourceDescription, + out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint, + out _, + out ulong rowSizeInBytes, + out ulong totalSizeInBytes); + + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) { - MemoryHelper.Copy(source, resource.Pointer, width, height, rowByteSize, rowPitchSize); + MemoryHelper.Copy( + source, + resource.Pointer, + width, + height, + rowSizeInBytes, + d3D12PlacedSubresourceFootprint.Footprint.RowPitch); } using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE); @@ -413,15 +431,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract copyCommandList.CopyTextureRegion( D3D12Resource, - DXGIFormatHelper.GetForType(), (uint)x, (uint)y, 0, - (uint)width, - (uint)height, - 1, d3D12Resource.Get(), - (uint)Unsafe.SizeOf()); + &d3D12PlacedSubresourceFootprint); copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, D3D12ResourceState); copyCommandList.ExecuteAndWaitForCompletion(); diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index e65ee1ab..58797a40 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -254,9 +254,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract copyCommandList.CopyTextureRegion( d3D12Resource.Get(), - (uint)Unsafe.SizeOf(), + &d3D12PlacedSubresourceFootprint, D3D12Resource, - DXGIFormatHelper.GetForType(), (uint)x, (uint)y, (ushort)z, @@ -449,15 +448,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract copyCommandList.CopyTextureRegion( D3D12Resource, - DXGIFormatHelper.GetForType(), (uint)x, (uint)y, (ushort)z, - (uint)width, - (uint)height, - (ushort)depth, d3D12Resource.Get(), - (uint)Unsafe.SizeOf()); + &d3D12PlacedSubresourceFootprint); copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, D3D12ResourceState); copyCommandList.ExecuteAndWaitForCompletion(); diff --git a/src/ComputeSharp.Graphics/Commands/CommandList.cs b/src/ComputeSharp.Graphics/Commands/CommandList.cs index 94472905..3be6492e 100644 --- a/src/ComputeSharp.Graphics/Commands/CommandList.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandList.cs @@ -95,47 +95,38 @@ namespace ComputeSharp.Graphics.Commands /// Copies a texture memory region from one resource (a buffer) to another (a texture). /// /// The destination (a texture) to write to. - /// The parameter curreently in use by the texture. /// The horizontal offset in the destination texture. /// The vertical offset 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. /// The source (a buffer) to read from. - /// The size of each element to copy. + /// The value describing . public readonly void CopyTextureRegion( ID3D12Resource* d3D12ResourceDestination, - DXGI_FORMAT dxgiFormat, uint x, uint y, ushort z, - uint width, - uint height, - ushort depth, ID3D12Resource* d3D12ResourceSource, - uint elementSizeInBytes) + D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintSource) { - this.d3D12GraphicsCommandList.Get()->CopyTextureRegion(d3D12ResourceDestination, dxgiFormat, x, y, z, width, height, depth, d3D12ResourceSource, elementSizeInBytes); + this.d3D12GraphicsCommandList.Get()->CopyTextureRegion(d3D12ResourceDestination, x, y, z, d3D12ResourceSource, d3D12PlacedSubresourceFootprintSource); } /// /// Copies a texture memory region from one resource (a texture) to another (a buffer). /// /// The destination (a buffer) to write to. - /// The size of each element to copy. + /// The value describing . /// The source (a texture) to read from. - /// The parameter curreently in use by the texture. /// 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. + /// 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 readonly void CopyTextureRegion( ID3D12Resource* d3D12ResourceDestination, - uint elementSizeInBytes, + D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintDestination, ID3D12Resource* d3D12ResourceSource, - DXGI_FORMAT dxgiFormat, uint x, uint y, ushort z, @@ -143,7 +134,7 @@ namespace ComputeSharp.Graphics.Commands uint height, ushort depth) { - this.d3D12GraphicsCommandList.Get()->CopyTextureRegion(d3D12ResourceDestination, elementSizeInBytes, d3D12ResourceSource, dxgiFormat, x, y, z, width, height, depth); + this.d3D12GraphicsCommandList.Get()->CopyTextureRegion(d3D12ResourceDestination, d3D12PlacedSubresourceFootprintDestination, d3D12ResourceSource, x, y, z, width, height, depth); } /// diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs index 5b0e14af..a228f3ed 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs @@ -1,5 +1,4 @@ using TerraFX.Interop; -using FX = TerraFX.Interop.Windows; namespace ComputeSharp.Graphics.Extensions { @@ -13,41 +12,24 @@ namespace ComputeSharp.Graphics.Extensions /// /// The instance in use. /// The destination (a texture) to write to. - /// The parameter curreently in use by the texture. /// The horizontal offset in the destination texture. /// The vertical offset 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. /// The source (a buffer) to read from. - /// The size of each element to copy. + /// The value describing . public static void CopyTextureRegion( this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, ID3D12Resource* d3D12ResourceDestination, - DXGI_FORMAT dxgiFormat, uint x, uint y, ushort z, - uint width, - uint height, - ushort depth, ID3D12Resource* d3D12ResourceSource, - uint elementSizeInBytes) + D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintSource) { - D3D12_SUBRESOURCE_FOOTPRINT d3D12SubresourceFootprint = new( - dxgiFormat, - width, - height, - depth, - (elementSizeInBytes * width + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((uint)FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)); - D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; - d3D12PlacedSubresourceFootprint.Offset = 0; - d3D12PlacedSubresourceFootprint.Footprint = d3D12SubresourceFootprint; D3D12_TEXTURE_COPY_LOCATION d3D12TextureCopyLocationDestination = new(d3D12ResourceDestination, 0), - d3D12TextureCopyLocationSource = new(d3D12ResourceSource, d3D12PlacedSubresourceFootprint); + d3D12TextureCopyLocationSource = new(d3D12ResourceSource, in *d3D12PlacedSubresourceFootprintSource); d3D12GraphicsCommandList.CopyTextureRegion(&d3D12TextureCopyLocationDestination, x, y, z, &d3D12TextureCopyLocationSource, null); } @@ -57,9 +39,8 @@ namespace ComputeSharp.Graphics.Extensions /// /// The instance in use. /// The destination (a buffer) to write to. - /// The size of each element to copy. + /// The value describing . /// The source (a texture) to read from. - /// The parameter curreently in use by the texture. /// The horizontal offset in the source texture. /// The vertical offset in the source texture. /// The depthwise offset in the source texture. @@ -69,9 +50,8 @@ namespace ComputeSharp.Graphics.Extensions public static void CopyTextureRegion( this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, ID3D12Resource* d3D12ResourceDestination, - uint elementSizeInBytes, + D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintDestination, ID3D12Resource* d3D12ResourceSource, - DXGI_FORMAT dxgiFormat, uint x, uint y, ushort z, @@ -79,17 +59,8 @@ namespace ComputeSharp.Graphics.Extensions uint height, ushort depth) { - D3D12_SUBRESOURCE_FOOTPRINT d3D12SubresourceFootprint = new( - dxgiFormat, - width, - height, - depth, - (elementSizeInBytes * width + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((uint)FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)); - D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; - d3D12PlacedSubresourceFootprint.Offset = 0; - d3D12PlacedSubresourceFootprint.Footprint = d3D12SubresourceFootprint; D3D12_TEXTURE_COPY_LOCATION - d3D12TextureCopyLocationDestination = new(d3D12ResourceDestination, d3D12PlacedSubresourceFootprint), + 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);