From 4e370a17a661d700f0542b77e93adbba5f411577 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 4 Dec 2020 18:44:50 +0100 Subject: [PATCH 01/36] Added 3D parameters to CopyTextureRegion APIs --- .../Buffers/Abstract/Texture2D{T}.cs | 28 +++++++++++++-- .../Commands/CommandList.cs | 16 ++++++--- .../ID3D12GraphicsCommandListExtensions.cs | 34 ++++++++++++------- 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index c909e598..61f61fe6 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -248,7 +248,19 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE)) { copyCommandList.ResourceBarrier(D3D12Resource, D3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); - copyCommandList.CopyTextureRegion(d3D12Resource.Get(), (uint)Unsafe.SizeOf(), D3D12Resource, DXGIFormatHelper.GetForType(), (uint)x, (uint)y, (uint)width, (uint)height); + + copyCommandList.CopyTextureRegion( + d3D12Resource.Get(), + (uint)Unsafe.SizeOf(), + D3D12Resource, + DXGIFormatHelper.GetForType(), + (uint)x, + (uint)y, + 0, + (uint)width, + (uint)height, + 1); + copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12ResourceState); copyCommandList.ExecuteAndWaitForCompletion(); } @@ -390,7 +402,19 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE); copyCommandList.ResourceBarrier(D3D12Resource, D3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); - copyCommandList.CopyTextureRegion(D3D12Resource, DXGIFormatHelper.GetForType(), (uint)x, (uint)y, d3D12Resource.Get(), (uint)width, (uint)height, (uint)Unsafe.SizeOf()); + + copyCommandList.CopyTextureRegion( + D3D12Resource, + DXGIFormatHelper.GetForType(), + (uint)x, + (uint)y, + 0, + (uint)width, + (uint)height, + 1, + d3D12Resource.Get(), + (uint)Unsafe.SizeOf()); + 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 2eb12aaa..94472905 100644 --- a/src/ComputeSharp.Graphics/Commands/CommandList.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandList.cs @@ -98,9 +98,11 @@ namespace ComputeSharp.Graphics.Commands /// The parameter curreently in use by the texture. /// The horizontal offset in the destination texture. /// The vertical offset in the destination texture. - /// The source (a buffer) to read from. + /// 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. public readonly void CopyTextureRegion( @@ -108,12 +110,14 @@ namespace ComputeSharp.Graphics.Commands DXGI_FORMAT dxgiFormat, uint x, uint y, - ID3D12Resource* d3D12ResourceSource, + ushort z, uint width, uint height, + ushort depth, + ID3D12Resource* d3D12ResourceSource, uint elementSizeInBytes) { - this.d3D12GraphicsCommandList.Get()->CopyTextureRegion(d3D12ResourceDestination, dxgiFormat, x, y, d3D12ResourceSource, width, height, elementSizeInBytes); + this.d3D12GraphicsCommandList.Get()->CopyTextureRegion(d3D12ResourceDestination, dxgiFormat, x, y, z, width, height, depth, d3D12ResourceSource, elementSizeInBytes); } /// @@ -134,10 +138,12 @@ namespace ComputeSharp.Graphics.Commands DXGI_FORMAT dxgiFormat, uint x, uint y, + ushort z, uint width, - uint height) + uint height, + ushort depth) { - this.d3D12GraphicsCommandList.Get()->CopyTextureRegion(d3D12ResourceDestination, elementSizeInBytes, d3D12ResourceSource, dxgiFormat, x, y, width, height); + this.d3D12GraphicsCommandList.Get()->CopyTextureRegion(d3D12ResourceDestination, elementSizeInBytes, d3D12ResourceSource, dxgiFormat, x, y, z, width, height, depth); } /// diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs index 4f7effdd..1c52d59e 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs @@ -16,9 +16,11 @@ namespace ComputeSharp.Graphics.Extensions /// The parameter curreently in use by the texture. /// The horizontal offset in the destination texture. /// The vertical offset in the destination texture. - /// The source (a buffer) to read from. + /// 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. public static void CopyTextureRegion( @@ -27,17 +29,19 @@ namespace ComputeSharp.Graphics.Extensions DXGI_FORMAT dxgiFormat, uint x, uint y, - ID3D12Resource* d3D12ResourceSource, + ushort z, uint width, uint height, + ushort depth, + ID3D12Resource* d3D12ResourceSource, uint elementSizeInBytes) { D3D12_SUBRESOURCE_FOOTPRINT d3D12SubresourceFootprint = new( dxgiFormat, width, height, - 1, - (elementSizeInBytes * width + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((uint)FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)); + depth, + (elementSizeInBytes * width * depth + 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; @@ -45,7 +49,7 @@ namespace ComputeSharp.Graphics.Extensions d3D12TextureCopyLocationDestination = new(d3D12ResourceDestination, 0), d3D12TextureCopyLocationSource = new(d3D12ResourceSource, d3D12PlacedSubresourceFootprint); - d3D12GraphicsCommandList.CopyTextureRegion(&d3D12TextureCopyLocationDestination, x, y, 0, &d3D12TextureCopyLocationSource, null); + d3D12GraphicsCommandList.CopyTextureRegion(&d3D12TextureCopyLocationDestination, x, y, z, &d3D12TextureCopyLocationSource, null); } /// @@ -56,10 +60,12 @@ namespace ComputeSharp.Graphics.Extensions /// The size of each element to copy. /// 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 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. public static void CopyTextureRegion( this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, ID3D12Resource* d3D12ResourceDestination, @@ -68,22 +74,24 @@ namespace ComputeSharp.Graphics.Extensions DXGI_FORMAT dxgiFormat, uint x, uint y, + ushort z, uint width, - uint height) + uint height, + ushort depth) { D3D12_SUBRESOURCE_FOOTPRINT d3D12SubresourceFootprint = new( dxgiFormat, width, height, - 1, - (elementSizeInBytes * width + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((uint)FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)); + depth, + (elementSizeInBytes * width * depth + 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), d3D12TextureCopyLocationSource = new(d3D12ResourceSource, 0); - D3D12_BOX d3D12Box = new((int)x, (int)y, (int)(x + width), (int)(y + height)); + D3D12_BOX d3D12Box = new((int)x, (int)y, z, (int)(x + width), (int)(y + height), z + depth); d3D12GraphicsCommandList.CopyTextureRegion(&d3D12TextureCopyLocationDestination, 0, 0, 0, &d3D12TextureCopyLocationSource, &d3D12Box); } From 42049c7a62e54d789de0edad23686b6e414c12bc Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 4 Dec 2020 18:49:21 +0100 Subject: [PATCH 02/36] Minor code refactoring --- .../Buffers/Abstract/Texture2D{T}.cs | 6 ++++-- .../Extensions/ID3D12DeviceExtensions.cs | 8 ++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index 61f61fe6..1636198d 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -12,6 +12,8 @@ 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; namespace ComputeSharp.Graphics.Buffers.Abstract @@ -69,10 +71,10 @@ namespace ComputeSharp.Graphics.Buffers.Abstract switch (resourceType) { case ResourceType.ReadOnly: - device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, DXGIFormatHelper.GetForType(), d3D12CpuDescriptorHandle); + device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE2D, d3D12CpuDescriptorHandle); break; case ResourceType.ReadWrite: - device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource, DXGIFormatHelper.GetForType(), d3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource, DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, d3D12CpuDescriptorHandle); break; } } diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index abe70867..6e171a2b 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -242,15 +242,17 @@ namespace ComputeSharp.Graphics.Extensions /// The target instance in use. /// The to create a view for. /// The value to use. + /// The value for the view to create. /// The instance for the current resource. public static void CreateShaderResourceView( this ref ID3D12Device d3D12Device, ID3D12Resource* d3D12Resource, DXGI_FORMAT dxgiFormat, + D3D12_SRV_DIMENSION d3D12SrvDimension, D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle) { D3D12_SHADER_RESOURCE_VIEW_DESC d3D12ShaderResourceViewDescription = default; - d3D12ShaderResourceViewDescription.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; + d3D12ShaderResourceViewDescription.ViewDimension = d3D12SrvDimension; d3D12ShaderResourceViewDescription.Format = dxgiFormat; d3D12ShaderResourceViewDescription.Shader4ComponentMapping = FX.D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; d3D12ShaderResourceViewDescription.Texture2D.MipLevels = uint.MaxValue; @@ -287,15 +289,17 @@ namespace ComputeSharp.Graphics.Extensions /// The target instance in use. /// The to create a view for. /// The value to use. + /// The value for the view to create. /// The instance for the current resource. public static void CreateUnorderedAccessView( this ref ID3D12Device d3D12Device, ID3D12Resource* d3D12Resource, DXGI_FORMAT dxgiFormat, + D3D12_UAV_DIMENSION d3D12UavDimension, D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle) { D3D12_UNORDERED_ACCESS_VIEW_DESC d3D12UnorderedAccessViewDescription = default; - d3D12UnorderedAccessViewDescription.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D; + d3D12UnorderedAccessViewDescription.ViewDimension = d3D12UavDimension; d3D12UnorderedAccessViewDescription.Format = dxgiFormat; d3D12Device.CreateUnorderedAccessView(d3D12Resource, null, &d3D12UnorderedAccessViewDescription, d3D12CpuDescriptorHandle); From 57a43c48b83c838f8be69366cddf581800d568b7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 4 Dec 2020 19:03:02 +0100 Subject: [PATCH 03/36] Added base Texture3D class (with no APIs) --- .../Buffers/Abstract/Texture3D{T}.cs | 122 ++++++++++++++++++ .../GraphicsDeviceMismatchException.cs | 13 ++ .../Extensions/ID3D12DeviceExtensions.cs | 51 ++++++++ 3 files changed, 186 insertions(+) create mode 100644 src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs new file mode 100644 index 00000000..f7ed889c --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -0,0 +1,122 @@ +using System.Runtime.CompilerServices; +using ComputeSharp.Core.Interop; +using ComputeSharp.Exceptions; +using ComputeSharp.Graphics.Buffers.Enums; +using ComputeSharp.Graphics.Extensions; +using ComputeSharp.Graphics.Helpers; +using Microsoft.Toolkit.Diagnostics; +using TerraFX.Interop; +using static TerraFX.Interop.D3D12_SRV_DIMENSION; +using static TerraFX.Interop.D3D12_UAV_DIMENSION; + +namespace ComputeSharp.Graphics.Buffers.Abstract +{ + /// + /// A representing a typed 3D texture stored on GPU memory. + /// + /// The type of items stored on the texture. + public unsafe abstract class Texture3D : NativeObject + where T : unmanaged + { + /// + /// The instance currently mapped. + /// + private ComPtr d3D12Resource; + + /// + /// The instance for the current resource. + /// + /// This field is dynamically accessed when loading shader dispatch data. + internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle; + + /// + /// The default value for the current resource. + /// + private readonly D3D12_RESOURCE_STATES D3D12ResourceState; + + /// + /// 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 depth of the texture. + /// The resource type for the current texture. + private protected Texture3D(GraphicsDevice device, int width, int height, int depth, ResourceType resourceType) + { + device.ThrowIfDisposed(); + + Guard.IsGreaterThanOrEqualTo(width, 0, nameof(width)); + Guard.IsGreaterThanOrEqualTo(height, 0, nameof(height)); + Guard.IsBetweenOrEqualTo(depth, 0, ushort.MaxValue, nameof(depth)); + + GraphicsDevice = device; + Width = width; + Height = height; + Depth = depth; + + this.d3D12Resource = device.D3D12Device->CreateCommittedResource( + resourceType, + DXGIFormatHelper.GetForType(), + (uint)width, + (uint)height, + (ushort)depth, + out D3D12ResourceState); + + device.AllocateShaderResourceViewDescriptorHandles(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); + + switch (resourceType) + { + case ResourceType.ReadOnly: + device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE3D, d3D12CpuDescriptorHandle); + break; + case ResourceType.ReadWrite: + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource, DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, 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 depth of the current texture. + /// + public int Depth { get; } + + /// + /// Gets the instance currently mapped. + /// + internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + + /// + protected override void OnDispose() + { + this.d3D12Resource.Dispose(); + } + + /// + /// 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); + } + } + } +} diff --git a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs index 8c7eb70c..4abb6981 100644 --- a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs +++ b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs @@ -71,5 +71,18 @@ namespace ComputeSharp.Exceptions { throw Create(texture, texture.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. + [Pure] + internal static void Throw(Texture3D texture, GraphicsDevice device) + where T : unmanaged + { + throw Create(texture, texture.GraphicsDevice, device); + } } } \ No newline at end of file diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index 6e171a2b..d0efc1d7 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -190,6 +190,57 @@ namespace ComputeSharp.Graphics.Extensions return d3D12Resource.Move(); } + /// + /// Creates a committed resource for a given 3D texture type. + /// + /// The instance in use. + /// The resource type currently in use. + /// The value to use. + /// The width of the texture resource. + /// The height of the texture resource. + /// The depth of the texture resource. + /// 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, + uint height, + ushort depth, + out D3D12_RESOURCE_STATES d3D12ResourceStates) + { + D3D12_RESOURCE_FLAGS d3D12ResourceFlags; + + (d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch + { + ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE), + 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.Type = D3D12_HEAP_TYPE_DEFAULT; + d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; + d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; + d3D12HeapProperties.CreationNodeMask = 1; + d3D12HeapProperties.VisibleNodeMask = 1; + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(dxgiFormat, width, height, depth, flags: d3D12ResourceFlags); + + d3D12Device.CreateCommittedResource( + &d3D12HeapProperties, + D3D12_HEAP_FLAG_NONE, + &d3D12ResourceDescription, + d3D12ResourceStates, + null, + FX.__uuidof(), + d3D12Resource.GetVoidAddressOf()).Assert(); + + return d3D12Resource.Move(); + } + /// /// Creates a view for a constant buffer. /// From 6904416d0bfae2a311296506b404bc378a1aecc3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 4 Dec 2020 19:07:19 +0100 Subject: [PATCH 04/36] Added public texture 3D types --- .../Buffers/ReadOnlyTexture3D{T}.cs | 42 +++++++++++++++++++ .../Buffers/ReadWriteTexture3D{T}.cs | 42 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs create mode 100644 src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs new file mode 100644 index 00000000..560ad224 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs @@ -0,0 +1,42 @@ +using System.Diagnostics; +using ComputeSharp.Exceptions; +using ComputeSharp.Graphics; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Buffers.Enums; + +namespace ComputeSharp +{ + /// + /// A representing a typed readonly 3D texture stored on GPU memory. + /// + /// The type of items stored on the texture. + [DebuggerDisplay("{ToString(),raw}")] + public sealed class ReadOnlyTexture3D : Texture3D + where T : unmanaged + { + /// + /// 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. + internal ReadOnlyTexture3D(GraphicsDevice device, int width, int height, int depth) + : base(device, width, height, depth, ResourceType.ReadOnly) + { + } + + /// + /// Gets a single value from the current readonly texture. + /// + /// The coordinates of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public T this[UInt3 xyz] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture3D)}[UInt3]"); + + /// + public override string ToString() + { + return $"ComputeSharp.ReadOnlyTexture3D<{typeof(T)}>[{Width}, {Height}, {Depth}]"; + } + } +} diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs new file mode 100644 index 00000000..9e5e950d --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs @@ -0,0 +1,42 @@ +using System.Diagnostics; +using ComputeSharp.Exceptions; +using ComputeSharp.Graphics; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Buffers.Enums; + +namespace ComputeSharp +{ + /// + /// A representing a typed readonly 3D texture stored on GPU memory. + /// + /// The type of items stored on the texture. + [DebuggerDisplay("{ToString(),raw}")] + public sealed class ReadWriteTexture3D : Texture3D + where T : unmanaged + { + /// + /// 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. + internal ReadWriteTexture3D(GraphicsDevice device, int width, int height, int depth) + : base(device, width, height, depth, ResourceType.ReadWrite) + { + } + + /// + /// Gets a single value from the current readonly texture. + /// + /// The coordinates of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public T this[UInt3 xyz] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture3D)}[UInt3]"); + + /// + public override string ToString() + { + return $"ComputeSharp.ReadOnlyTexture3D<{typeof(T)}>[{Width}, {Height}, {Depth}]"; + } + } +} From eef8e418997e8c148f74c5960006ddb2343a3cb7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 5 Dec 2020 15:49:14 +0100 Subject: [PATCH 05/36] Added Texture2D unit tests --- tests/ComputeSharp.Tests/BufferTests.cs | 21 ++- .../ComputeSharp.Tests.csproj | 1 + .../Extensions/GraphicsDeviceExtensions.cs | 43 ++++++ tests/ComputeSharp.Tests/Texture2DTests.cs | 141 ++++++++++++++++++ 4 files changed, 202 insertions(+), 4 deletions(-) create mode 100644 tests/ComputeSharp.Tests/Texture2DTests.cs diff --git a/tests/ComputeSharp.Tests/BufferTests.cs b/tests/ComputeSharp.Tests/BufferTests.cs index 7810777e..280c5c24 100644 --- a/tests/ComputeSharp.Tests/BufferTests.cs +++ b/tests/ComputeSharp.Tests/BufferTests.cs @@ -7,14 +7,14 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests { [TestClass] - [TestCategory("Buffers")] + [TestCategory("Buffer")] public class BufferTests { [TestMethod] [DataRow(typeof(ConstantBuffer<>))] [DataRow(typeof(ReadOnlyBuffer<>))] [DataRow(typeof(ReadWriteBuffer<>))] - public void AllocateBuffer_Uninitialized(Type bufferType) + public void Allocate_Uninitialized_Ok(Type bufferType) { using Buffer buffer = Gpu.Default.AllocateBuffer(bufferType, 128); @@ -23,11 +23,24 @@ namespace ComputeSharp.Tests Assert.AreSame(buffer.GraphicsDevice, Gpu.Default); } + [TestMethod] + [DataRow(typeof(ConstantBuffer<>), -247824)] + [DataRow(typeof(ConstantBuffer<>), -1)] + [DataRow(typeof(ConstantBuffer<>), -247824)] + [DataRow(typeof(ConstantBuffer<>), -1)] + [DataRow(typeof(ConstantBuffer<>), -247824)] + [DataRow(typeof(ConstantBuffer<>), -1)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Allocate_Uninitialized_Fail(Type bufferType, int length) + { + using Buffer buffer = Gpu.Default.AllocateBuffer(bufferType, length); + } + [TestMethod] [DataRow(typeof(ConstantBuffer<>))] [DataRow(typeof(ReadOnlyBuffer<>))] [DataRow(typeof(ReadWriteBuffer<>))] - public void AllocateBuffer_FromArray(Type bufferType) + public void Allocate_FromArray(Type bufferType) { float[] data = Enumerable.Range(0, 128).Select(static i => (float)i).ToArray(); @@ -54,7 +67,7 @@ namespace ComputeSharp.Tests [DataRow(typeof(ReadWriteBuffer<>), typeof(ConstantBuffer<>))] [DataRow(typeof(ReadWriteBuffer<>), typeof(ReadOnlyBuffer<>))] [DataRow(typeof(ReadWriteBuffer<>), typeof(ReadWriteBuffer<>))] - public void AllocateBuffer_FromBuffer(Type sourceType, Type destinationType) + public void Allocate_FromBuffer(Type sourceType, Type destinationType) { float[] data = Enumerable.Range(0, 128).Select(static i => (float)i).ToArray(); diff --git a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj index 385ffb57..9e45c3d9 100644 --- a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj +++ b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj @@ -7,6 +7,7 @@ + diff --git a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs index 48aa0554..9ffb2d25 100644 --- a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs +++ b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs @@ -31,6 +31,27 @@ namespace ComputeSharp.Tests.Extensions }; } + /// + /// Allocates a new instance of the specified type. + /// + /// The type of items in the texture. + /// The target instance to allocate the texture for. + /// The type of texture to allocate. + /// The width of the texture to create. + /// The height of the texture to create. + /// A instance of the requested size. + [Pure] + public static Texture2D AllocateTexture2D(this GraphicsDevice device, Type type, int width, int height) + where T : unmanaged + { + return type switch + { + _ when type == typeof(ReadOnlyTexture2D<>) => device.AllocateReadOnlyTexture2D(width, height), + _ when type == typeof(ReadWriteTexture2D<>) => device.AllocateReadWriteTexture2D(width, height), + _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) + }; + } + /// /// Allocates a new instance of the specified type. /// @@ -52,6 +73,28 @@ namespace ComputeSharp.Tests.Extensions }; } + /// + /// Allocates a new instance of the specified type. + /// + /// The type of items in the texture. + /// The target instance to allocate the texture for. + /// The type of buffer to allocate. + /// The data to load on the texture. + /// The width of the texture to create. + /// The height of the texture to create. + /// A instance of the requested type. + [Pure] + public static Texture2D AllocateTexture2D(this GraphicsDevice device, Type type, T[] data, int width, int height) + where T : unmanaged + { + return type switch + { + _ when type == typeof(ReadOnlyTexture2D<>) => device.AllocateReadOnlyTexture2D(data, width, height), + _ when type == typeof(ReadWriteTexture2D<>) => device.AllocateReadWriteTexture2D(data, width, height), + _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) + }; + } + /// /// Allocates a new instance of the specified type. /// diff --git a/tests/ComputeSharp.Tests/Texture2DTests.cs b/tests/ComputeSharp.Tests/Texture2DTests.cs new file mode 100644 index 00000000..08a79c1c --- /dev/null +++ b/tests/ComputeSharp.Tests/Texture2DTests.cs @@ -0,0 +1,141 @@ +using System; +using System.Linq; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Tests.Extensions; +using Microsoft.Toolkit.HighPerformance.Memory; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests +{ + [TestClass] + [TestCategory("Texture2D")] + public class Texture2DTests + { + [TestMethod] + [DataRow(typeof(ReadOnlyTexture2D<>))] + [DataRow(typeof(ReadWriteTexture2D<>))] + public void Allocate_Uninitialized_Ok(Type textureType) + { + using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, 128, 128); + + Assert.IsNotNull(texture); + Assert.AreEqual(texture.Width, 128); + Assert.AreEqual(texture.Height, 128); + Assert.AreSame(texture.GraphicsDevice, Gpu.Default); + } + + [TestMethod] + [DataRow(typeof(ReadOnlyTexture2D<>), 128, -14253)] + [DataRow(typeof(ReadOnlyTexture2D<>), 128, -1)] + [DataRow(typeof(ReadOnlyTexture2D<>), 0, -4314)] + [DataRow(typeof(ReadOnlyTexture2D<>), -14, -53)] + [DataRow(typeof(ReadWriteTexture2D<>), 128, -14253)] + [DataRow(typeof(ReadWriteTexture2D<>), 128, -1)] + [DataRow(typeof(ReadWriteTexture2D<>), 0, -4314)] + [DataRow(typeof(ReadWriteTexture2D<>), -14, -53)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Allocate_Uninitialized_Fail(Type textureType, int width, int height) + { + using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, width, height); + } + + [TestMethod] + [DataRow(typeof(ReadOnlyTexture2D<>))] + [DataRow(typeof(ReadWriteTexture2D<>))] + public void Allocate_FromArray(Type textureType) + { + float[] data = Enumerable.Range(0, 128 * 128).Select(static i => (float)i).ToArray(); + + using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, data, 128, 128); + + Assert.IsNotNull(texture); + Assert.AreEqual(texture.Width, 128); + Assert.AreEqual(texture.Height, 128); + Assert.AreSame(texture.GraphicsDevice, Gpu.Default); + + float[,] result = texture.GetData(); + + Assert.IsNotNull(result); + Assert.AreEqual(data.Length, result.Length); + Assert.IsTrue(data.SequenceEqual(result.Cast())); + } + + [TestMethod] + [DataRow(typeof(ReadOnlyTexture2D<>))] + [DataRow(typeof(ReadWriteTexture2D<>))] + [ExpectedException(typeof(ObjectDisposedException))] + public void UsageAfterDispose(Type textureType) + { + using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, 10, 10); + + texture.Dispose(); + + _ = texture.GetData(); + } + + [TestMethod] + [DataRow(typeof(ReadOnlyTexture2D<>), 0, 0, 64, 64)] + [DataRow(typeof(ReadOnlyTexture2D<>), 0, 14, 64, 50)] + [DataRow(typeof(ReadOnlyTexture2D<>), 14, 0, 50, 64)] + [DataRow(typeof(ReadOnlyTexture2D<>), 10, 10, 54, 54)] + [DataRow(typeof(ReadOnlyTexture2D<>), 20, 20, 32, 27)] + [DataRow(typeof(ReadOnlyTexture2D<>), 60, 0, 4, 64)] + [DataRow(typeof(ReadOnlyTexture2D<>), 0, 60, 64, 4)] + [DataRow(typeof(ReadOnlyTexture2D<>), 63, 2, 1, 60)] + [DataRow(typeof(ReadOnlyTexture2D<>), 2, 63, 60, 1)] + [DataRow(typeof(ReadWriteTexture2D<>), 0, 0, 64, 64)] + [DataRow(typeof(ReadWriteTexture2D<>), 0, 14, 64, 50)] + [DataRow(typeof(ReadWriteTexture2D<>), 14, 0, 50, 64)] + [DataRow(typeof(ReadWriteTexture2D<>), 10, 10, 54, 54)] + [DataRow(typeof(ReadWriteTexture2D<>), 20, 20, 32, 27)] + [DataRow(typeof(ReadWriteTexture2D<>), 60, 0, 4, 64)] + [DataRow(typeof(ReadWriteTexture2D<>), 0, 60, 64, 4)] + [DataRow(typeof(ReadWriteTexture2D<>), 63, 2, 1, 60)] + [DataRow(typeof(ReadWriteTexture2D<>), 2, 63, 60, 1)] + public void GetData_RangeToVoid_Ok(Type textureType, int x, int y, int width, int height) + { + float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray(); + + using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, array, 64, 64); + + float[] result = new float[width * height]; + + texture.GetData(result, x, y, width, height); + + Span2D + expected = new Span2D(array, 64, 64).Slice(y, x, height, width), + data = new(result, height, width); + + CollectionAssert.AreEqual(expected.ToArray(), data.ToArray()); + } + + [TestMethod] + [DataRow(typeof(ReadOnlyTexture2D<>), 0, -1, 50, 50)] + [DataRow(typeof(ReadOnlyTexture2D<>), -1, 0, 50, 50)] + [DataRow(typeof(ReadOnlyTexture2D<>), 12, 0, -1, 50)] + [DataRow(typeof(ReadOnlyTexture2D<>), 12, 0, 20, -1)] + [DataRow(typeof(ReadOnlyTexture2D<>), 12, 20, 20, 50)] + [DataRow(typeof(ReadOnlyTexture2D<>), 12, 20, 60, 20)] + [DataRow(typeof(ReadOnlyTexture2D<>), 80, 20, 40, 20)] + [DataRow(typeof(ReadOnlyTexture2D<>), 0, 80, 40, 20)] + [DataRow(typeof(ReadWriteTexture2D<>), 0, -1, 50, 50)] + [DataRow(typeof(ReadWriteTexture2D<>), -1, 0, 50, 50)] + [DataRow(typeof(ReadWriteTexture2D<>), 12, 0, -1, 50)] + [DataRow(typeof(ReadWriteTexture2D<>), 12, 0, 20, -1)] + [DataRow(typeof(ReadWriteTexture2D<>), 12, 20, 20, 50)] + [DataRow(typeof(ReadWriteTexture2D<>), 12, 20, 60, 20)] + [DataRow(typeof(ReadWriteTexture2D<>), 80, 20, 40, 20)] + [DataRow(typeof(ReadWriteTexture2D<>), 0, 80, 40, 20)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void GetData_RangeToVoid_Fail(Type textureType, int x, int y, int width, int height) + { + float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray(); + + using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, array, 64, 64); + + float[] result = new float[4096]; + + texture.GetData(result, x, y, width, height); + } + } +} From 251dddc17c7eacc6b9db815447944c5568505281 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 5 Dec 2020 17:01:33 +0100 Subject: [PATCH 06/36] Initial Texture3D get/set support for whole volume --- .../Buffers/Abstract/Texture3D{T}.cs | 149 +++++++++++++++++- .../Extensions/ID3D12DeviceExtensions.cs | 30 ++++ .../ID3D12GraphicsCommandListExtensions.cs | 4 +- .../Helpers/MemoryHelper.cs | 96 ++++++++++- 4 files changed, 274 insertions(+), 5 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index f7ed889c..36fe6f9b 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -1,11 +1,16 @@ -using System.Runtime.CompilerServices; +using System; +using System.Runtime.CompilerServices; using ComputeSharp.Core.Interop; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Buffers.Enums; +using ComputeSharp.Graphics.Buffers.Interop; +using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; 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; @@ -101,6 +106,148 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + /// + /// Reads the contents of the specified range from the current instance and writes them into a target . + /// + /// The input to write data to. + /// 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 copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public void GetData(Span destination, int x, int y, int z, int width, int height, int depth) + { + GraphicsDevice.ThrowIfDisposed(); + + ThrowIfDisposed(); + + Guard.IsInRange(x, 0, Width, nameof(x)); + Guard.IsInRange(y, 0, Height, nameof(y)); + Guard.IsInRange(z, 0, Depth, nameof(z)); + Guard.IsBetweenOrEqualTo(width, 0, Width, nameof(width)); + Guard.IsBetweenOrEqualTo(height, 0, Height, nameof(height)); + Guard.IsBetweenOrEqualTo(depth, 0, Depth, nameof(depth)); + Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); + Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); + Guard.IsLessThanOrEqualTo(z + depth, Depth, nameof(z)); + Guard.HasSizeGreaterThanOrEqualTo(destination, width * height * depth, nameof(destination)); + + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(DXGIFormatHelper.GetForType(), (ulong)width, (uint)height, (ushort)depth); + + 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)) + { + copyCommandList.ResourceBarrier(D3D12Resource, D3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); + + copyCommandList.CopyTextureRegion( + d3D12Resource.Get(), + (uint)Unsafe.SizeOf(), + D3D12Resource, + DXGIFormatHelper.GetForType(), + (uint)x, + (uint)y, + (ushort)z, + (uint)width, + (uint)height, + (ushort)depth); + + copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12ResourceState); + copyCommandList.ExecuteAndWaitForCompletion(); + } + + using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); + + MemoryHelper.Copy( + resource.Pointer, + width, + height, + depth, + rowSizeInBytes, + d3D12PlacedSubresourceFootprint.Footprint.RowPitch, + d3D12PlacedSubresourceFootprint.Footprint.RowPitch * (uint)height, + destination); + } + + /// + /// Writes the contents of a given to a specified area of the current instance. + /// + /// The input 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 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. + public void SetData(ReadOnlySpan source, int x, int y, int z, int width, int height, int depth) + { + GraphicsDevice.ThrowIfDisposed(); + + ThrowIfDisposed(); + + Guard.IsInRange(x, 0, Width, nameof(x)); + Guard.IsInRange(y, 0, Height, nameof(y)); + Guard.IsInRange(z, 0, Depth, nameof(z)); + Guard.IsBetweenOrEqualTo(width, 0, Width, nameof(width)); + Guard.IsBetweenOrEqualTo(height, 0, Height, nameof(height)); + Guard.IsBetweenOrEqualTo(depth, 0, Depth, nameof(depth)); + Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); + Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); + Guard.IsLessThanOrEqualTo(z + depth, Depth, nameof(z)); + Guard.HasSizeGreaterThanOrEqualTo(source, width * height * depth, nameof(source)); + + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(DXGIFormatHelper.GetForType(), (ulong)width, (uint)height, (ushort)depth); + + 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, + depth, + rowSizeInBytes, + d3D12PlacedSubresourceFootprint.Footprint.RowPitch, + d3D12PlacedSubresourceFootprint.Footprint.RowPitch * (uint)height); + } + + using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE); + + copyCommandList.ResourceBarrier(D3D12Resource, D3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); + + copyCommandList.CopyTextureRegion( + D3D12Resource, + DXGIFormatHelper.GetForType(), + (uint)x, + (uint)y, + (ushort)z, + (uint)width, + (uint)height, + (ushort)depth, + d3D12Resource.Get(), + (uint)Unsafe.SizeOf()); + + copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, D3D12ResourceState); + copyCommandList.ExecuteAndWaitForCompletion(); + } + /// protected override void OnDispose() { diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index d0efc1d7..0112a51f 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -402,6 +402,36 @@ namespace ComputeSharp.Graphics.Extensions return d3D12GraphicsCommandList.Move(); } + /// + /// Gets the layout data for a target resource. + /// + /// The target to use to get the layout info. + /// The value for the target resource. + /// The resulting layout info for the resource. + /// The number of rows in 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, + D3D12_RESOURCE_DESC* d3D12ResourceDesc, + out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint, + out uint numRows, + out ulong rowSizeInBytes, + out ulong totalSizeInBytes) + { + uint a; + ulong b, c; + + fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* p = &d3D12PlacedSubresourceFootprint) + { + d3D12Device.GetCopyableFootprints(d3D12ResourceDesc, 0, 1, 0, p, &a, &b, &c); + } + + numRows = a; + rowSizeInBytes = b; + totalSizeInBytes = c; + } + /// /// Checks the feature support of a given type for a given device. /// diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs index 1c52d59e..5b0e14af 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs @@ -41,7 +41,7 @@ namespace ComputeSharp.Graphics.Extensions width, height, depth, - (elementSizeInBytes * width * depth + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((uint)FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)); + (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; @@ -84,7 +84,7 @@ namespace ComputeSharp.Graphics.Extensions width, height, depth, - (elementSizeInBytes * width * depth + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((uint)FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)); + (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; diff --git a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs index 9115977c..a9a4c4fb 100644 --- a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs @@ -42,8 +42,8 @@ namespace ComputeSharp.Graphics.Helpers /// The type of values to read. /// The destination to read from. /// The pointer that indicates the 2D memory area to write to. - /// The width of the 2D memory area to read. - /// The height of the 2D memory area to read. + /// The width of the 2D memory area to write to. + /// The height of the 2D memory area to write to. /// The width of the memory area in bytes. /// The pitch (padded width) of the memory area in bytes. [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -107,5 +107,97 @@ namespace ComputeSharp.Graphics.Helpers } } } + + /// + /// Copies the content of a to the 3D area pointed by an input pointer. + /// + /// The type of values to read. + /// The destination to read from. + /// The pointer that indicates the 3D memory area to write to. + /// The width of the 3D memory area to write to. + /// The height of the 3D memory area to write to. + /// The depth of the 3D memory area to write to. + /// The width of the memory area in bytes. + /// The pitch (padded width) of the memory area in bytes. + /// The size in bytes of each 2D slice within the target 3D memory area. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe void Copy( + ReadOnlySpan source, + void* destination, + int width, + int height, + int depth, + ulong widthInBytes, + ulong pitchInBytes, + ulong sliceInBytes) + where T : unmanaged + { + if (widthInBytes == pitchInBytes) + { + source.CopyTo(new Span(destination, width * height * depth)); + } + else + { + for (int z = 0; z < depth; z++) + { + int sourceZOffset = width * height * z; + ulong destinationZOffset = sliceInBytes * (uint)z; + + for (int i = 0; i < height; i++) + { + ReadOnlySpan sourceRow = source.Slice(sourceZOffset + i * width, width); + Span destinationRow = new((byte*)destination + destinationZOffset + (uint)i * pitchInBytes, width); + + sourceRow.CopyTo(destinationRow); + } + } + } + } + + /// + /// Copies a 3D memory area pointed by a pointer value to a target . + /// + /// The type of values to read. + /// The pointer that indicates the memory area to read from. + /// The width of the 3D memory area to read. + /// The height of the 3D memory area to read. + /// The depth of the 3D memory area to read. + /// The width of the memory area in bytes. + /// The pitch (padded width) of the memory area in bytes. + /// The size in bytes of each 2D slice within the source 3D memory area. + /// The destination to write. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe void Copy( + void* source, + int width, + int height, + int depth, + ulong widthInBytes, + ulong pitchInBytes, + ulong sliceInBytes, + Span destination) + where T : unmanaged + { + if (widthInBytes == pitchInBytes) + { + new Span(source, width * height * depth).CopyTo(destination); + } + else + { + for (int z = 0; z < depth; z++) + { + ulong sourceZOffset = sliceInBytes * (uint)z; + int destinationZOffset = width * height * z; + + for (int i = 0; i < height; i++) + { + ReadOnlySpan sourceRow = new((byte*)source + sourceZOffset + (uint)i * pitchInBytes, width); + Span destinationRow = destination.Slice(destinationZOffset + i * width, width); + + sourceRow.CopyTo(destinationRow); + } + } + } + } } } From 1de7a7c9c0ed1043938917e2566ba64641ac8f0b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 5 Dec 2020 19:41:19 +0100 Subject: [PATCH 07/36] Added overloads to Texture3D APIs --- .../Buffers/Abstract/Texture2D{T}.cs | 6 + .../Buffers/Abstract/Texture3D{T}.cs | 217 +++++++++++++++++- 2 files changed, 222 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index 1636198d..1140055e 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -119,6 +119,9 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The input array to write data to. public void GetData(T[,] destination) { + Guard.IsEqualTo(destination.GetLength(0), Height, nameof(destination)); + Guard.IsEqualTo(destination.GetLength(1), Width, nameof(destination)); + fixed (T* p = destination) { GetData(new Span(p, destination.Length)); @@ -278,6 +281,9 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The input array to read data from. public void SetData(T[,] source) { + Guard.IsEqualTo(source.GetLength(0), Height, nameof(source)); + Guard.IsEqualTo(source.GetLength(1), Width, nameof(source)); + fixed (T* p = source) { SetData(new Span(p, source.Length)); diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index 36fe6f9b..08063ddd 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using ComputeSharp.Core.Interop; using ComputeSharp.Exceptions; @@ -106,6 +107,109 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + /// + /// Reads the contents of the current instance and returns an array. + /// + /// A array with the contents of the current buffer. + /// + /// The returned array will be using the same memory layout as the texture, that is, each 2D plane + /// in the 3D volume represented by the texture is contiguous in memory, and planes are stacked in + /// the depth dimension. This means that the resulting 3D array will have a size of [D, H, W]. + /// + [Pure] + public T[,,] GetData() + { + T[,,] data = new T[Depth, Height, Width]; + + GetData(data); + + return data; + } + + /// + /// Reads the contents of the current instance and writes them into a target array. + /// + /// The input array to write data to. + /// + /// The input 3D array needs to have each 2D plane stacked on the depth axis. That is, unlike .NET arrays + /// which are traditionally of size [H, W, D], the input one needs to have a layout of [D, H, W]. + /// + public void GetData(T[,,] destination) + { + Guard.IsEqualTo(destination.GetLength(0), Depth, nameof(destination)); + Guard.IsEqualTo(destination.GetLength(1), Height, nameof(destination)); + Guard.IsEqualTo(destination.GetLength(2), Width, nameof(destination)); + + fixed (T* p = destination) + { + GetData(new Span(p, destination.Length)); + } + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target array. + /// + /// The input array to write data to. + /// The starting offset within to write data to. + public void GetData(T[] destination, int offset) + { + GetData(destination.AsSpan(offset), 0, 0, 0, Width, Height, Depth); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target array. + /// + /// The input array to write data to. + /// The starting offset within to write data to. + /// The horizontal range of items to copy. + /// The vertical range of items to copy. + /// The depthwise range of items to copy. + public void GetData(T[] destination, int offset, Range x, Range y, Range z) + { + GetData(destination.AsSpan(offset), x, y, z); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target array. + /// + /// The input array to write data to. + /// The starting offset within to write data to. + /// 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 copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public void GetData(T[] destination, int offset, int x, int y, int z, int width, int height, int depth) + { + GetData(destination.AsSpan(offset), x, y, z, width, height, depth); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target . + /// + /// The input to write data to. + public void GetData(Span destination) + { + GetData(destination, 0, 0, 0, Width, Height, Depth); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target . + /// + /// The input to write data to. + /// The horizontal range in the source texture. + /// The vertical range in the source texture. + /// The depthwise range in the source texture. + public void GetData(Span destination, Range x, Range y, Range z) + { + var (offsetX, width) = x.GetOffsetAndLength(Width); + var (offsetY, height) = y.GetOffsetAndLength(Height); + var (offsetZ, depth) = z.GetOffsetAndLength(Depth); + + GetData(destination, offsetX, offsetY, offsetZ, width, height, depth); + } + /// /// Reads the contents of the specified range from the current instance and writes them into a target . /// @@ -178,7 +282,118 @@ namespace ComputeSharp.Graphics.Buffers.Abstract } /// - /// Writes the contents of a given to a specified area of the current instance. + /// Writes the contents of a given array to the current instance. + /// + /// The input array to read data from. + /// + /// The input 3D array needs to have each 2D plane stacked on the depth axis. That is, unlike .NET arrays + /// which are traditionally of size [H, W, D], the input one needs to have a layout of [D, H, W]. + /// + public void SetData(T[,,] source) + { + Guard.IsEqualTo(source.GetLength(0), Depth, nameof(source)); + Guard.IsEqualTo(source.GetLength(1), Height, nameof(source)); + Guard.IsEqualTo(source.GetLength(2), Width, nameof(source)); + + fixed (T* p = source) + { + SetData(new Span(p, source.Length)); + } + } + + /// + /// Writes the contents of a given array to the current instance. + /// + /// The input array to read data from. + public void SetData(T[] source) + { + SetData(source.AsSpan(), 0, 0, 0, Width, Height, Depth); + } + + /// + /// Writes the contents of a given array to a specified area of the current instance. + /// + /// The input array to read data from. + /// The horizontal range of items to write. + /// The vertical range of items to write. + /// The depthwise range of items to write. + public void SetData(T[] source, Range x, Range y, Range z) + { + SetData(source.AsSpan(), x, y, z); + } + + /// + /// Writes the contents of a given array to a specified area of the current instance. + /// + /// The input array to read data from. + /// 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. + public void SetData(T[] source, int x, int y, int z, int width, int height, int depth) + { + SetData(source.AsSpan(), x, y, z, width, height, depth); + } + + /// + /// Writes the contents of a given array to a specified area of the current instance. + /// + /// The input array to read data from. + /// The starting offset within to read data from. + /// The horizontal range of items to write. + /// The vertical range of items to write. + /// The depthwise range of items to write. + public void SetData(T[] source, int offset, Range x, Range y, Range z) + { + SetData(source.AsSpan(offset), x, y, z); + } + + /// + /// Writes the contents of a given array to a specified area of the current instance. + /// + /// The input array to read data from. + /// The starting offset within to read data from. + /// 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. + public void SetData(T[] source, int offset, int x, int y, int z, int width, int height, int depth) + { + SetData(source.AsSpan(offset), x, y, z, width, height, depth); + } + + /// + /// Writes the contents of a given to the current instance. + /// The input data will be written to the start of the texture, and all input items will be copied. + /// + /// The input to read data from. + public void SetData(ReadOnlySpan source) + { + SetData(source, 0, 0, 0, Width, Height, Depth); + } + + /// + /// Writes the contents of a given to a specified area of the current instance. + /// + /// The input to read data from. + /// The horizontal range of items to write. + /// The vertical range of items to write. + /// The depthwise range of items to write. + public void SetData(ReadOnlySpan source, Range x, Range y, Range z) + { + var (offsetX, width) = x.GetOffsetAndLength(Width); + var (offsetY, height) = y.GetOffsetAndLength(Height); + var (offsetZ, depth) = z.GetOffsetAndLength(Depth); + + SetData(source, offsetX, offsetY, offsetZ, width, height, depth); + } + + /// + /// Writes the contents of a given to a specified area of the current instance. /// /// The input to read data from. /// The horizontal offset in the destination texture. From 7d85f7b6af73db8c1c43fa8e6d75085478874b52 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 5 Dec 2020 19:48:11 +0100 Subject: [PATCH 08/36] Added public APIs to allocate Texture3D-s --- .../Extensions/GraphicsDeviceExtensions.cs | 188 ++++++++++++++++++ 1 file changed, 188 insertions(+) diff --git a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs index 303397c2..9e889be2 100644 --- a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs +++ b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs @@ -375,6 +375,194 @@ namespace ComputeSharp return texture; } + /// + /// Allocates a new readonly 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The instance to use to allocate the texture. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A zeroed instance of size [, , ]. + [Pure] + public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, int width, int height, int depth) + where T : unmanaged + { + return new(device, width, height, depth); + } + + /// + /// Allocates a new readonly 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, T[] array, int width, int height, int depth) + where T : unmanaged + { + return device.AllocateReadOnlyTexture3D(array.AsSpan(), width, height, depth); + } + + /// + /// Allocates a new readonly 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The starting offset within to read data from. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, T[] array, int offset, int width, int height, int depth) + where T : unmanaged + { + return device.AllocateReadOnlyTexture3D(array.AsSpan(offset), width, height, depth); + } + + /// + /// Allocates a new readonly 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// A read write instance with the contents of the input array. + /// + /// The input 3D array needs to have each 2D plane stacked on the depth axis. That is, unlike .NET arrays + /// which are traditionally of size [H, W, D], the input one needs to have a layout of [D, H, W]. + /// + [Pure] + public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, T[,,] array) + where T : unmanaged + { + ReadOnlyTexture3D texture = new(device, array.GetLength(2), array.GetLength(1), array.GetLength(0)); + + texture.SetData(array); + + return texture; + } + + /// + /// Allocates a new readonly 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The instance to use to allocate the texture. + /// The input with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A instance with the contents of the input . + [Pure] + public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, ReadOnlySpan span, int width, int height, int depth) + where T : unmanaged + { + ReadOnlyTexture3D texture = new(device, width, height, depth); + + texture.SetData(span); + + return texture; + } + + /// + /// Allocates a new writeable 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The instance to use to allocate the texture. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A zeroed instance of size [, ]. + [Pure] + public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, int width, int height, int depth) + where T : unmanaged + { + return new(device, width, height, depth); + } + + /// + /// Allocates a new writeable 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, T[] array, int width, int height, int depth) + where T : unmanaged + { + return device.AllocateReadWriteTexture3D(array.AsSpan(), width, height, depth); + } + + /// + /// Allocates a new writeable 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The starting offset within to read data from. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, T[] array, int offset, int width, int height, int depth) + where T : unmanaged + { + return device.AllocateReadWriteTexture3D(array.AsSpan(offset), width, height, depth); + } + + /// + /// Allocates a new writeable 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// A read write instance with the contents of the input array. + /// + /// The input 3D array needs to have each 2D plane stacked on the depth axis. That is, unlike .NET arrays + /// which are traditionally of size [H, W, D], the input one needs to have a layout of [D, H, W]. + /// + [Pure] + public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, T[,,] array) + where T : unmanaged + { + ReadWriteTexture3D texture = new(device, array.GetLength(2), array.GetLength(1), array.GetLength(0)); + + texture.SetData(array); + + return texture; + } + + /// + /// Allocates a new writeable 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The instance to use to allocate the texture. + /// The input with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A instance with the contents of the input . + [Pure] + public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, ReadOnlySpan span, int width, int height, int depth) + where T : unmanaged + { + ReadWriteTexture3D texture = new(device, width, height, depth); + + texture.SetData(span); + + return texture; + } + /// /// Compiles and runs the input shader on a target instance, with the specified parameters. /// From 7b4f85b8ad49c2e7535758e5f2733da306edccd4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 5 Dec 2020 19:49:40 +0100 Subject: [PATCH 09/36] Reordered some public APIs --- .../Extensions/GraphicsDeviceExtensions.cs | 300 +++++++++--------- 1 file changed, 150 insertions(+), 150 deletions(-) diff --git a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs index 9e889be2..d2a6e53d 100644 --- a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs +++ b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs @@ -139,70 +139,6 @@ namespace ComputeSharp return readWriteBuffer; } - /// - /// Allocates a new read write buffer with the specified parameters. - /// - /// The type of items to store in the buffer. - /// The instance to use to allocate the buffer. - /// The length of the buffer to allocate. - /// A zeroed instance of size . - [Pure] - public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, int length) - where T : unmanaged - { - return new(device, length); - } - - /// - /// Allocates a new read write buffer with the specified parameters. - /// - /// The type of items to store in the buffer. - /// The instance to use to allocate the buffer. - /// The input array with the data to copy on the allocated buffer. - /// A read write instance with the contents of the input array. - [Pure] - public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, T[] array) - where T : unmanaged - { - return device.AllocateReadWriteBuffer(array.AsSpan()); - } - - /// - /// Allocates a new read write buffer with the specified parameters. - /// - /// The type of items to store in the buffer. - /// The instance to use to allocate the buffer. - /// The input with the data to copy on the allocated buffer. - /// A read write instance with the contents of the input . - [Pure] - public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, ReadOnlySpan span) - where T : unmanaged - { - ReadWriteBuffer buffer = new(device, span.Length); - - buffer.SetData(span); - - return buffer; - } - - /// - /// Allocates a new read write buffer with the specified parameters. - /// - /// The type of items to store in the buffer. - /// The instance to use to allocate the buffer. - /// The input with the data to copy on the allocated buffer. - /// A read write instance with the contents of the input . - [Pure] - public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, Buffer buffer) - where T : unmanaged - { - ReadWriteBuffer readWriteBuffer = new(device, buffer.Length); - - readWriteBuffer.SetData(buffer); - - return readWriteBuffer; - } - /// /// Allocates a new readonly 2D texture with the specified parameters. /// @@ -289,92 +225,6 @@ namespace ComputeSharp return texture; } - /// - /// Allocates a new writeable 2D texture with the specified parameters. - /// - /// The type of items to store in the texture. - /// The instance to use to allocate the texture. - /// The width of the texture. - /// The height of the texture. - /// A zeroed instance of size [, ]. - [Pure] - public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, int width, int height) - where T : unmanaged - { - return new(device, width, height); - } - - /// - /// Allocates a new writeable 2D texture with the specified parameters. - /// - /// The type of items to store in the texture. - /// The instance to use to allocate the texture. - /// The input array with the data to copy on the allocated texture. - /// The width of the texture. - /// The height of the texture. - /// A instance with the contents of the input array. - [Pure] - public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[] array, int width, int height) - where T : unmanaged - { - return device.AllocateReadWriteTexture2D(array.AsSpan(), width, height); - } - - /// - /// Allocates a new writeable 2D texture with the specified parameters. - /// - /// The type of items to store in the texture. - /// The instance to use to allocate the texture. - /// The input array with the data to copy on the allocated texture. - /// The starting offset within to read data from. - /// The width of the texture. - /// The height of the texture. - /// A instance with the contents of the input array. - [Pure] - public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[] array, int offset, int width, int height) - where T : unmanaged - { - return device.AllocateReadWriteTexture2D(array.AsSpan(offset), width, height); - } - - /// - /// Allocates a new writeable 2D texture with the specified parameters. - /// - /// The type of items to store in the texture. - /// The instance to use to allocate the texture. - /// The input array with the data to copy on the allocated texture. - /// A read write instance with the contents of the input array. - [Pure] - public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[,] array) - where T : unmanaged - { - ReadWriteTexture2D texture = new(device, array.GetLength(1), array.GetLength(0)); - - texture.SetData(array); - - return texture; - } - - /// - /// Allocates a new writeable 2D texture with the specified parameters. - /// - /// The type of items to store in the texture. - /// The instance to use to allocate the texture. - /// The input with the data to copy on the allocated texture. - /// The width of the texture. - /// The height of the texture. - /// A instance with the contents of the input . - [Pure] - public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, ReadOnlySpan span, int width, int height) - where T : unmanaged - { - ReadWriteTexture2D texture = new(device, width, height); - - texture.SetData(span); - - return texture; - } - /// /// Allocates a new readonly 3D texture with the specified parameters. /// @@ -469,6 +319,156 @@ namespace ComputeSharp return texture; } + /// + /// Allocates a new read write buffer with the specified parameters. + /// + /// The type of items to store in the buffer. + /// The instance to use to allocate the buffer. + /// The length of the buffer to allocate. + /// A zeroed instance of size . + [Pure] + public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, int length) + where T : unmanaged + { + return new(device, length); + } + + /// + /// Allocates a new read write buffer with the specified parameters. + /// + /// The type of items to store in the buffer. + /// The instance to use to allocate the buffer. + /// The input array with the data to copy on the allocated buffer. + /// A read write instance with the contents of the input array. + [Pure] + public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, T[] array) + where T : unmanaged + { + return device.AllocateReadWriteBuffer(array.AsSpan()); + } + + /// + /// Allocates a new read write buffer with the specified parameters. + /// + /// The type of items to store in the buffer. + /// The instance to use to allocate the buffer. + /// The input with the data to copy on the allocated buffer. + /// A read write instance with the contents of the input . + [Pure] + public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, ReadOnlySpan span) + where T : unmanaged + { + ReadWriteBuffer buffer = new(device, span.Length); + + buffer.SetData(span); + + return buffer; + } + + /// + /// Allocates a new read write buffer with the specified parameters. + /// + /// The type of items to store in the buffer. + /// The instance to use to allocate the buffer. + /// The input with the data to copy on the allocated buffer. + /// A read write instance with the contents of the input . + [Pure] + public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, Buffer buffer) + where T : unmanaged + { + ReadWriteBuffer readWriteBuffer = new(device, buffer.Length); + + readWriteBuffer.SetData(buffer); + + return readWriteBuffer; + } + + /// + /// Allocates a new writeable 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The instance to use to allocate the texture. + /// The width of the texture. + /// The height of the texture. + /// A zeroed instance of size [, ]. + [Pure] + public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, int width, int height) + where T : unmanaged + { + return new(device, width, height); + } + + /// + /// Allocates a new writeable 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[] array, int width, int height) + where T : unmanaged + { + return device.AllocateReadWriteTexture2D(array.AsSpan(), width, height); + } + + /// + /// Allocates a new writeable 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The starting offset within to read data from. + /// The width of the texture. + /// The height of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[] array, int offset, int width, int height) + where T : unmanaged + { + return device.AllocateReadWriteTexture2D(array.AsSpan(offset), width, height); + } + + /// + /// Allocates a new writeable 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// A read write instance with the contents of the input array. + [Pure] + public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[,] array) + where T : unmanaged + { + ReadWriteTexture2D texture = new(device, array.GetLength(1), array.GetLength(0)); + + texture.SetData(array); + + return texture; + } + + /// + /// Allocates a new writeable 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The instance to use to allocate the texture. + /// The input with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// A instance with the contents of the input . + [Pure] + public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, ReadOnlySpan span, int width, int height) + where T : unmanaged + { + ReadWriteTexture2D texture = new(device, width, height); + + texture.SetData(span); + + return texture; + } + /// /// Allocates a new writeable 3D texture with the specified parameters. /// From f2e93a6c88377c9ae5bc1f0ccacc0aae4975af5c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 6 Dec 2020 00:23:07 +0100 Subject: [PATCH 10/36] Fixed a bug in the ObjectCreationExpressionSyntax visitor --- .../SyntaxRewriters/ShaderSourceRewriter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index d6f2a027..3cc8a414 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -158,7 +158,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters { var updatedNode = (ObjectCreationExpressionSyntax)base.VisitObjectCreationExpression(node)!; - updatedNode = updatedNode.ReplaceAndTrackType(updatedNode.Type, node.Type, this.semanticModel, this.discoveredTypes); + updatedNode = updatedNode.ReplaceAndTrackType(updatedNode.Type, node, this.semanticModel, this.discoveredTypes); // New objects use the default HLSL cast syntax, eg. (float4)0 if (updatedNode.ArgumentList!.Arguments.Count == 0) From 05757e4c12b1454a6bcf72e0ae2a6beb797d8d7c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 6 Dec 2020 00:24:26 +0100 Subject: [PATCH 11/36] Enabled support for 3D textures in shader generator --- .../Mappings/HlslKnownTypes.cs | 10 ++++++---- .../Mappings/HlslKnownTypes.cs | 18 +++++++++++++----- 2 files changed, 19 insertions(+), 9 deletions(-) rename src/ComputeSharp.Shaders/{ => Translation}/Mappings/HlslKnownTypes.cs (91%) diff --git a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.Shaders/Translation/Mappings/HlslKnownTypes.cs similarity index 91% rename from src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs rename to src/ComputeSharp.Shaders/Translation/Mappings/HlslKnownTypes.cs index 7ade5bc9..b3fe7b73 100644 --- a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.Shaders/Translation/Mappings/HlslKnownTypes.cs @@ -63,7 +63,7 @@ namespace ComputeSharp.Shaders.Mappings type.GetGenericTypeDefinition() == typeof(ConstantBuffer<>); /// - /// Checks whether or not the input type is a or value. + /// Checks whether or not the input type is a known readonly resource type (buffer or texture). /// /// The input instance to check. /// A indicating whether the input is a readonly resource type. @@ -76,11 +76,12 @@ namespace ComputeSharp.Shaders.Mappings return genericType == typeof(ReadOnlyBuffer<>) || - genericType == typeof(ReadOnlyTexture2D<>); + genericType == typeof(ReadOnlyTexture2D<>) || + genericType == typeof(ReadOnlyTexture3D<>); } /// - /// Checks whether or not the input type is a or value. + /// Checks whether or not the input type is a known writeable resource type (buffer or texture). /// /// The input instance to check. /// A indicating whether the input is a writeable resource type. @@ -93,7 +94,8 @@ namespace ComputeSharp.Shaders.Mappings return genericType == typeof(ReadWriteBuffer<>) || - genericType == typeof(ReadWriteTexture2D<>); + genericType == typeof(ReadWriteTexture2D<>) || + genericType == typeof(ReadWriteTexture3D<>); } } } diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs index 5b3b9299..bffcb65a 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -63,11 +63,17 @@ namespace ComputeSharp.SourceGenerators.Mappings /// Whether or not represents a typed resource type. public static bool IsTypedResourceType(string typeName) { - return - typeName == "ComputeSharp.ReadOnlyBuffer`1" || - typeName == "ComputeSharp.ReadWriteBuffer`1" || - typeName == "ComputeSharp.ReadOnlyTexture2D`1" || - typeName == "ComputeSharp.ReadWriteTexture2D`1"; + switch (typeName) + { + case "ComputeSharp.ReadOnlyBuffer`1": + case "ComputeSharp.ReadWriteBuffer`1": + case "ComputeSharp.ReadOnlyTexture2D`1": + case "ComputeSharp.ReadWriteTexture2D`1": + case "ComputeSharp.ReadOnlyTexture3D`1": + case "ComputeSharp.ReadWriteTexture3D`1": + return true; + default: return false; + }; } /// @@ -102,6 +108,8 @@ namespace ComputeSharp.SourceGenerators.Mappings "ComputeSharp.ReadWriteBuffer`1" => $"RWStructuredBuffer<{mapped}>", "ComputeSharp.ReadOnlyTexture2D`1" => $"Texture2D<{mapped}>", "ComputeSharp.ReadWriteTexture2D`1" => $"RWTexture2D<{mapped}>", + "ComputeSharp.ReadOnlyTexture3D`1" => $"Texture3D<{mapped}>", + "ComputeSharp.ReadWriteTexture3D`1" => $"RWTexture3D<{mapped}>", _ => throw new ArgumentException() }; } From 06fdff4c7788b7d5827c9b808ca26dacafcedfc1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 6 Dec 2020 00:28:42 +0100 Subject: [PATCH 12/36] Improved some XML docs --- .../Buffers/Abstract/Texture3D{T}.cs | 8 ++++---- src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index 08063ddd..f386bdfe 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -131,8 +131,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// /// The input array to write data to. /// - /// The input 3D array needs to have each 2D plane stacked on the depth axis. That is, unlike .NET arrays - /// which are traditionally of size [H, W, D], the input one needs to have a layout of [D, H, W]. + /// The input 3D array needs to have each 2D plane stacked on the depth axis. That is, the expected + /// layout of the input array has to be [, , ]. /// public void GetData(T[,,] destination) { @@ -286,8 +286,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// /// The input array to read data from. /// - /// The input 3D array needs to have each 2D plane stacked on the depth axis. That is, unlike .NET arrays - /// which are traditionally of size [H, W, D], the input one needs to have a layout of [D, H, W]. + /// The source 3D array needs to have each 2D plane stacked on the depth axis. That is, the expected + /// layout of the input array has to be [, , ]. /// public void SetData(T[,,] source) { diff --git a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs index d2a6e53d..e22ed529 100644 --- a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs +++ b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs @@ -284,8 +284,8 @@ namespace ComputeSharp /// The input array with the data to copy on the allocated texture. /// A read write instance with the contents of the input array. /// - /// The input 3D array needs to have each 2D plane stacked on the depth axis. That is, unlike .NET arrays - /// which are traditionally of size [H, W, D], the input one needs to have a layout of [D, H, W]. + /// The source 3D array needs to have each 2D plane stacked on the depth axis. + /// That is, the expected layout of the input array has to be [depth, height, width]. /// [Pure] public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, T[,,] array) @@ -528,8 +528,8 @@ namespace ComputeSharp /// The input array with the data to copy on the allocated texture. /// A read write instance with the contents of the input array. /// - /// The input 3D array needs to have each 2D plane stacked on the depth axis. That is, unlike .NET arrays - /// which are traditionally of size [H, W, D], the input one needs to have a layout of [D, H, W]. + /// The source 3D array needs to have each 2D plane stacked on the depth axis. + /// That is, the expected layout of the input array has to be [depth, height, width]. /// [Pure] public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, T[,,] array) From 0b9786d29329a0a5aaae6889ba8ab20af0ed053c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 6 Dec 2020 02:37:26 +0100 Subject: [PATCH 13/36] Fixed error when creating UAVs over texture 3D --- src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index 0112a51f..c22ef9ac 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -352,6 +352,7 @@ namespace ComputeSharp.Graphics.Extensions D3D12_UNORDERED_ACCESS_VIEW_DESC d3D12UnorderedAccessViewDescription = default; d3D12UnorderedAccessViewDescription.ViewDimension = d3D12UavDimension; d3D12UnorderedAccessViewDescription.Format = dxgiFormat; + d3D12UnorderedAccessViewDescription.Texture3D.WSize = uint.MaxValue; d3D12Device.CreateUnorderedAccessView(d3D12Resource, null, &d3D12UnorderedAccessViewDescription, d3D12CpuDescriptorHandle); } From 3165d09234629a85d4e9b0d6bd35550d3fc2507c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 6 Dec 2020 02:48:02 +0100 Subject: [PATCH 14/36] Fixed input checks for resource APIs --- .../Buffers/Abstract/Buffer{T}.cs | 2 +- .../Buffers/Abstract/Texture2D{T}.cs | 12 ++++++------ .../Buffers/Abstract/Texture3D{T}.cs | 18 +++++++++--------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs index b04c95b4..5331e22d 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs @@ -45,7 +45,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract { device.ThrowIfDisposed(); - Guard.IsGreaterThanOrEqualTo(length, 0, nameof(length)); + Guard.IsGreaterThan(length, 0, nameof(length)); SizeInBytes = checked((nint)(length * elementSizeInBytes)); GraphicsDevice = device; diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index 1140055e..d8305f6e 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -52,8 +52,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract { device.ThrowIfDisposed(); - Guard.IsGreaterThanOrEqualTo(width, 0, nameof(width)); - Guard.IsGreaterThanOrEqualTo(height, 0, nameof(height)); + Guard.IsGreaterThan(width, 0, nameof(width)); + Guard.IsGreaterThan(height, 0, nameof(height)); GraphicsDevice = device; Width = width; @@ -237,8 +237,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsInRange(x, 0, Width, nameof(x)); Guard.IsInRange(y, 0, Height, nameof(y)); - Guard.IsBetweenOrEqualTo(width, 0, Width, nameof(width)); - Guard.IsBetweenOrEqualTo(height, 0, Height, nameof(height)); + 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.HasSizeGreaterThanOrEqualTo(destination, width * height, nameof(destination)); @@ -389,8 +389,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsInRange(x, 0, Width, nameof(x)); Guard.IsInRange(y, 0, Height, nameof(y)); - Guard.IsBetweenOrEqualTo(width, 0, Width, nameof(width)); - Guard.IsBetweenOrEqualTo(height, 0, Height, nameof(height)); + 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.HasSizeGreaterThanOrEqualTo(source, width * height, nameof(source)); diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index f386bdfe..e65ee1ab 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -52,9 +52,9 @@ namespace ComputeSharp.Graphics.Buffers.Abstract { device.ThrowIfDisposed(); - Guard.IsGreaterThanOrEqualTo(width, 0, nameof(width)); - Guard.IsGreaterThanOrEqualTo(height, 0, nameof(height)); - Guard.IsBetweenOrEqualTo(depth, 0, ushort.MaxValue, nameof(depth)); + Guard.IsGreaterThan(width, 0, nameof(width)); + Guard.IsGreaterThan(height, 0, nameof(height)); + Guard.IsBetweenOrEqualTo(depth, 1, ushort.MaxValue, nameof(depth)); GraphicsDevice = device; Width = width; @@ -229,9 +229,9 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsInRange(x, 0, Width, nameof(x)); Guard.IsInRange(y, 0, Height, nameof(y)); Guard.IsInRange(z, 0, Depth, nameof(z)); - Guard.IsBetweenOrEqualTo(width, 0, Width, nameof(width)); - Guard.IsBetweenOrEqualTo(height, 0, Height, nameof(height)); - Guard.IsBetweenOrEqualTo(depth, 0, Depth, nameof(depth)); + Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width)); + Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); + Guard.IsBetweenOrEqualTo(depth, 1, Depth, nameof(depth)); Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); Guard.IsLessThanOrEqualTo(z + depth, Depth, nameof(z)); @@ -411,9 +411,9 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsInRange(x, 0, Width, nameof(x)); Guard.IsInRange(y, 0, Height, nameof(y)); Guard.IsInRange(z, 0, Depth, nameof(z)); - Guard.IsBetweenOrEqualTo(width, 0, Width, nameof(width)); - Guard.IsBetweenOrEqualTo(height, 0, Height, nameof(height)); - Guard.IsBetweenOrEqualTo(depth, 0, Depth, nameof(depth)); + Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width)); + Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); + Guard.IsBetweenOrEqualTo(depth, 1, Depth, nameof(depth)); Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); Guard.IsLessThanOrEqualTo(z + depth, Depth, nameof(z)); From e056d58d4ac372873c2f0e7379a2d8d40a41be8b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 6 Dec 2020 02:48:56 +0100 Subject: [PATCH 15/36] Added Texture3D unit tests --- .../Extensions/GraphicsDeviceExtensions.cs | 64 +++++++ tests/ComputeSharp.Tests/Texture3DTests.cs | 175 ++++++++++++++++++ 2 files changed, 239 insertions(+) create mode 100644 tests/ComputeSharp.Tests/Texture3DTests.cs diff --git a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs index 9ffb2d25..ff9ba47e 100644 --- a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs +++ b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs @@ -52,6 +52,28 @@ namespace ComputeSharp.Tests.Extensions }; } + /// + /// Allocates a new instance of the specified type. + /// + /// The type of items in the texture. + /// The target instance to allocate the texture for. + /// The type of texture to allocate. + /// The width of the texture to create. + /// The height of the texture to create. + /// The depth of the texture to create. + /// A instance of the requested size. + [Pure] + public static Texture3D AllocateTexture3D(this GraphicsDevice device, Type type, int width, int height, int depth) + where T : unmanaged + { + return type switch + { + _ when type == typeof(ReadOnlyTexture3D<>) => device.AllocateReadOnlyTexture3D(width, height, depth), + _ when type == typeof(ReadWriteTexture3D<>) => device.AllocateReadWriteTexture3D(width, height, depth), + _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) + }; + } + /// /// Allocates a new instance of the specified type. /// @@ -95,6 +117,48 @@ namespace ComputeSharp.Tests.Extensions }; } + /// + /// Allocates a new instance of the specified type. + /// + /// The type of items in the texture. + /// The target instance to allocate the texture for. + /// The type of buffer to allocate. + /// The data to load on the texture. + /// The width of the texture to create. + /// The height of the texture to create. + /// The depth of the texture to create. + /// A instance of the requested type. + [Pure] + public static Texture3D AllocateTexture3D(this GraphicsDevice device, Type type, T[] data, int width, int height, int depth) + where T : unmanaged + { + return type switch + { + _ when type == typeof(ReadOnlyTexture3D<>) => device.AllocateReadOnlyTexture3D(data, width, height, depth), + _ when type == typeof(ReadWriteTexture3D<>) => device.AllocateReadWriteTexture3D(data, width, height, depth), + _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) + }; + } + + /// + /// Allocates a new instance of the specified type. + /// + /// The type of items in the texture. + /// The target instance to allocate the texture for. + /// The type of texture to allocate. + /// A instance of the requested size. + [Pure] + public static Texture3D AllocateTexture3D(this GraphicsDevice device, Type type, T[,,] data) + where T : unmanaged + { + return type switch + { + _ when type == typeof(ReadOnlyTexture3D<>) => device.AllocateReadOnlyTexture3D(data), + _ when type == typeof(ReadWriteTexture3D<>) => device.AllocateReadWriteTexture3D(data), + _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) + }; + } + /// /// Allocates a new instance of the specified type. /// diff --git a/tests/ComputeSharp.Tests/Texture3DTests.cs b/tests/ComputeSharp.Tests/Texture3DTests.cs new file mode 100644 index 00000000..a6764388 --- /dev/null +++ b/tests/ComputeSharp.Tests/Texture3DTests.cs @@ -0,0 +1,175 @@ +using System; +using System.Linq; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Tests.Extensions; +using Microsoft.Toolkit.HighPerformance.Extensions; +using Microsoft.Toolkit.HighPerformance.Memory; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests +{ + [TestClass] + [TestCategory("Texture3D")] + public class Texture3DTests + { + [TestMethod] + [DataRow(typeof(ReadOnlyTexture3D<>))] + [DataRow(typeof(ReadWriteTexture3D<>))] + public void Allocate_Uninitialized_Ok(Type textureType) + { + using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, 64, 64, 12); + + Assert.IsNotNull(texture); + Assert.AreEqual(texture.Width, 64); + Assert.AreEqual(texture.Height, 64); + Assert.AreEqual(texture.Depth, 12); + Assert.AreSame(texture.GraphicsDevice, Gpu.Default); + } + + [TestMethod] + [DataRow(typeof(ReadOnlyTexture3D<>), 128, -14253, 4)] + [DataRow(typeof(ReadOnlyTexture3D<>), 128, -1, 4)] + [DataRow(typeof(ReadOnlyTexture3D<>), 0, -4314, 4)] + [DataRow(typeof(ReadOnlyTexture3D<>), -14, -53, 4)] + [DataRow(typeof(ReadOnlyTexture3D<>), 12, 12, -1)] + [DataRow(typeof(ReadOnlyTexture3D<>), 2, 4, ushort.MaxValue + 1)] + [DataRow(typeof(ReadWriteTexture3D<>), 128, -14253, 4)] + [DataRow(typeof(ReadWriteTexture3D<>), 128, -1, 4)] + [DataRow(typeof(ReadWriteTexture3D<>), 0, -4314, 4)] + [DataRow(typeof(ReadWriteTexture3D<>), -14, -53, 4)] + [DataRow(typeof(ReadWriteTexture3D<>), 12, 12, -1)] + [DataRow(typeof(ReadWriteTexture3D<>), 2, 4, ushort.MaxValue + 1)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Allocate_Uninitialized_Fail(Type textureType, int width, int height, int depth) + { + using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, width, height, depth); + } + + [TestMethod] + [DataRow(typeof(ReadOnlyTexture3D<>), 12, 12, 4)] + [DataRow(typeof(ReadOnlyTexture3D<>), 2, 12, 4)] + [DataRow(typeof(ReadOnlyTexture3D<>), 64, 64, 1)] + [DataRow(typeof(ReadOnlyTexture3D<>), 64, 27, 3)] + [DataRow(typeof(ReadWriteTexture3D<>), 12, 12, 4)] + [DataRow(typeof(ReadWriteTexture3D<>), 2, 12, 4)] + [DataRow(typeof(ReadWriteTexture3D<>), 64, 64, 1)] + [DataRow(typeof(ReadWriteTexture3D<>), 64, 27, 3)] + public void Allocate_FromArray(Type textureType, int width, int height, int depth) + { + float[] data = Enumerable.Range(0, width * height * depth).Select(static i => (float)i).ToArray(); + + using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, data, width, height, depth); + + Assert.IsNotNull(texture); + Assert.AreEqual(texture.Width, width); + Assert.AreEqual(texture.Height, height); + Assert.AreEqual(texture.Depth, depth); + Assert.AreSame(texture.GraphicsDevice, Gpu.Default); + + float[,,] result = texture.GetData(); + + Assert.IsNotNull(result); + Assert.AreEqual(result.GetLength(0), depth); + Assert.AreEqual(result.GetLength(1), height); + Assert.AreEqual(result.GetLength(2), width); + Assert.AreEqual(data.Length, result.Length); + Assert.IsTrue(data.SequenceEqual(result.Cast())); + + Array.Clear(result, 0, result.Length); + + float[] flat = new float[result.Length]; + + texture.GetData(flat); + + CollectionAssert.AreEqual(flat, data); + } + + [TestMethod] + [DataRow(typeof(ReadOnlyTexture3D<>))] + [DataRow(typeof(ReadWriteTexture3D<>))] + [ExpectedException(typeof(ObjectDisposedException))] + public void UsageAfterDispose(Type textureType) + { + using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, 10, 10, 4); + + texture.Dispose(); + + _ = texture.GetData(); + } + + [TestMethod] + [DataRow(typeof(ReadOnlyTexture3D<>), 0, 0, 0, 64, 64, 3)] + [DataRow(typeof(ReadOnlyTexture3D<>), 0, 14, 0, 64, 50, 3)] + [DataRow(typeof(ReadOnlyTexture3D<>), 0, 14, 1, 64, 50, 2)] + [DataRow(typeof(ReadOnlyTexture3D<>), 14, 0, 1, 50, 64, 2)] + [DataRow(typeof(ReadOnlyTexture3D<>), 10, 10, 2, 54, 54, 1)] + [DataRow(typeof(ReadOnlyTexture3D<>), 20, 20, 0, 32, 27, 3)] + [DataRow(typeof(ReadOnlyTexture3D<>), 60, 0, 0, 4, 64, 3)] + [DataRow(typeof(ReadOnlyTexture3D<>), 0, 60, 2, 64, 4, 1)] + [DataRow(typeof(ReadOnlyTexture3D<>), 63, 2, 0, 1, 60, 2)] + [DataRow(typeof(ReadOnlyTexture3D<>), 2, 63, 1, 60, 1, 2)] + [DataRow(typeof(ReadWriteTexture3D<>), 0, 0, 0, 64, 64, 3)] + [DataRow(typeof(ReadWriteTexture3D<>), 0, 14, 0, 64, 50, 3)] + [DataRow(typeof(ReadWriteTexture3D<>), 0, 14, 1, 64, 50, 2)] + [DataRow(typeof(ReadWriteTexture3D<>), 14, 0, 1, 50, 64, 2)] + [DataRow(typeof(ReadWriteTexture3D<>), 10, 10, 2, 54, 54, 1)] + [DataRow(typeof(ReadWriteTexture3D<>), 20, 20, 0, 32, 27, 3)] + [DataRow(typeof(ReadWriteTexture3D<>), 60, 0, 0, 4, 64, 3)] + [DataRow(typeof(ReadWriteTexture3D<>), 0, 60, 2, 64, 4, 1)] + [DataRow(typeof(ReadWriteTexture3D<>), 63, 2, 0, 1, 60, 2)] + [DataRow(typeof(ReadWriteTexture3D<>), 2, 63, 1, 60, 1, 2)] + public void GetData_RangeToVoid_Ok(Type textureType, int x, int y, int z, int width, int height, int depth) + { + int[,,] array = new int[3, 64, 64]; + + foreach (var item in array.AsSpan().Enumerate()) + item.Value = item.Index; + + using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, array); + + int[,,] result = new int[depth, height, width]; + + texture.GetData(result.AsSpan(), x, y, z, width, height, depth); + + for (int k = 0; k < depth; k++) + { + Span2D + source = array.AsSpan2D(z + k).Slice(y, x, height, width), + destination = result.AsSpan2D(k); + + CollectionAssert.AreEqual(source.ToArray(), destination.ToArray()); + } + } + + [TestMethod] + [DataRow(typeof(ReadOnlyTexture3D<>), 0, -1, 0, 50, 50, 2)] + [DataRow(typeof(ReadOnlyTexture3D<>), -1, 0, 0, 50, 50, 3)] + [DataRow(typeof(ReadOnlyTexture3D<>), 12, 0, 0, -1, 50, 1)] + [DataRow(typeof(ReadOnlyTexture3D<>), 12, 0, 0, 20, 50, 0)] + [DataRow(typeof(ReadOnlyTexture3D<>), 12, 0, 1, 20, -1, 2)] + [DataRow(typeof(ReadOnlyTexture3D<>), 12, 20, 2, 20, 50, 1)] + [DataRow(typeof(ReadOnlyTexture3D<>), 12, 20, 0, 60, 20, 3)] + [DataRow(typeof(ReadOnlyTexture3D<>), 80, 20, 2, 40, 20, 1)] + [DataRow(typeof(ReadOnlyTexture3D<>), 0, 80, 2, 40, 20, 1)] + [DataRow(typeof(ReadWriteTexture3D<>), 0, -1, 0, 50, 50, 2)] + [DataRow(typeof(ReadWriteTexture3D<>), -1, 0, 0, 50, 50, 3)] + [DataRow(typeof(ReadWriteTexture3D<>), 12, 0, 0, -1, 50, 1)] + [DataRow(typeof(ReadWriteTexture3D<>), 12, 0, 0, 20, 50, 0)] + [DataRow(typeof(ReadWriteTexture3D<>), 12, 0, 1, 20, -1, 2)] + [DataRow(typeof(ReadWriteTexture3D<>), 12, 20, 2, 20, 50, 1)] + [DataRow(typeof(ReadWriteTexture3D<>), 12, 20, 0, 60, 20, 3)] + [DataRow(typeof(ReadWriteTexture3D<>), 80, 20, 2, 40, 20, 1)] + [DataRow(typeof(ReadWriteTexture3D<>), 0, 80, 2, 40, 20, 1)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void GetData_RangeToVoid_Fail(Type textureType, int x, int y, int z, int width, int height, int depth) + { + float[] array = Enumerable.Range(0, 64 * 64 * 3).Select(static i => (float)i).ToArray(); + + using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, array, 64, 64, 3); + + float[] result = new float[array.Length]; + + texture.GetData(result, x, y, z, width, height, depth); + } + } +} From 111bcec623fff58e798c74419492241e5df5f787 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 6 Dec 2020 12:44:17 +0100 Subject: [PATCH 16/36] Minor code refactoring --- .../Buffers/Abstract/Texture2D{T}.cs | 54 ++++++++++++------- .../Buffers/Abstract/Texture3D{T}.cs | 9 +--- .../Commands/CommandList.cs | 27 ++++------ .../ID3D12GraphicsCommandListExtensions.cs | 41 +++----------- 4 files changed, 51 insertions(+), 80 deletions(-) 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); From 0af9f32c618c825b41125bba41efb6828b269552 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 6 Dec 2020 19:16:49 +0100 Subject: [PATCH 17/36] Code refactoring in MemoryHelper type --- .../Buffers/Abstract/StructuredBuffer{T}.cs | 19 +- .../Buffers/Abstract/Texture2D{T}.cs | 22 +- .../Buffers/Abstract/Texture3D{T}.cs | 28 +-- .../Buffers/ConstantBuffer{T}.cs | 65 +++--- .../Helpers/MemoryHelper.cs | 218 +++++++++++------- 5 files changed, 217 insertions(+), 135 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs index 41926a7a..78fad45f 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs @@ -52,8 +52,15 @@ namespace ComputeSharp.Graphics.Buffers.Abstract } using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); - - MemoryHelper.Copy(resource.Pointer, 0, destination); + fixed (void* destinationPointer = destination) + { + MemoryHelper.Copy( + resource.Pointer, + 0u, + (uint)destination.Length, + (uint)sizeof(T), + destinationPointer); + } } /// @@ -73,8 +80,14 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, (ulong)byteSize); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) + fixed (void* sourcePointer = source) { - MemoryHelper.Copy(source, resource.Pointer, 0); + MemoryHelper.Copy( + sourcePointer, + 0u, + (uint)source.Length, + (uint)sizeof(T), + resource.Pointer); } using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index a9e40779..5935c197 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -274,13 +274,15 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); - MemoryHelper.Copy( - resource.Pointer, - width, - height, - rowSizeInBytes, - d3D12PlacedSubresourceFootprint.Footprint.RowPitch, - destination); + fixed (void* destinationPointer = destination) + { + MemoryHelper.Copy( + resource.Pointer, + (uint)height, + rowSizeInBytes, + d3D12PlacedSubresourceFootprint.Footprint.RowPitch, + destinationPointer); + } } /// @@ -415,12 +417,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) + fixed (void* sourcePointer = source) { MemoryHelper.Copy( - source, + sourcePointer, resource.Pointer, - width, - height, + (uint)height, rowSizeInBytes, d3D12PlacedSubresourceFootprint.Footprint.RowPitch); } diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index 58797a40..2ee65640 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -269,15 +269,17 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); - MemoryHelper.Copy( - resource.Pointer, - width, - height, - depth, - rowSizeInBytes, - d3D12PlacedSubresourceFootprint.Footprint.RowPitch, - d3D12PlacedSubresourceFootprint.Footprint.RowPitch * (uint)height, - destination); + fixed (void* destinationPointer = destination) + { + MemoryHelper.Copy( + resource.Pointer, + (uint)height, + (uint)depth, + rowSizeInBytes, + d3D12PlacedSubresourceFootprint.Footprint.RowPitch, + d3D12PlacedSubresourceFootprint.Footprint.RowPitch * (uint)height, + destinationPointer); + } } /// @@ -430,13 +432,13 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) + fixed (void* sourcePointer = source) { MemoryHelper.Copy( - source, + sourcePointer, resource.Pointer, - width, - height, - depth, + (uint)height, + (uint)depth, rowSizeInBytes, d3D12PlacedSubresourceFootprint.Footprint.RowPitch, d3D12PlacedSubresourceFootprint.Footprint.RowPitch * (uint)height); diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 2789b764..16c005bb 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -2,7 +2,6 @@ using System.Diagnostics; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; using ComputeSharp.Exceptions; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers.Abstract; @@ -64,24 +63,27 @@ namespace ComputeSharp Guard.IsLessThanOrEqualTo((uint)offset + destination.Length, (uint)Length, nameof(destination)); using ID3D12ResourceMap resource = D3D12Resource->Map(); - - if (IsPaddingPresent) + fixed (void* destinationPointer = destination) { - int count = destination.Length; - ref T spanRef = ref MemoryMarshal.GetReference(destination); - ref byte resourceRef = ref Unsafe.AsRef(resource.Pointer); - - // Move to the initial ref - resourceRef = ref Unsafe.Add(ref resourceRef, offset * GetPaddedSize()); - - for (var i = 0; i < count; i++) + if (IsPaddingPresent) { - ref byte targetRef = ref Unsafe.Add(ref resourceRef, i * GetPaddedSize()); - - Unsafe.Add(ref spanRef, i) = Unsafe.As(ref targetRef); + MemoryHelper.Copy( + resource.Pointer, + (uint)offset, + (uint)destination.Length, + (uint)GetPaddedSize(), + destinationPointer); + } + else + { + MemoryHelper.Copy( + resource.Pointer, + (uint)offset, + (uint)destination.Length, + (uint)sizeof(T), + destinationPointer); } } - else MemoryHelper.Copy(resource.Pointer, offset, destination); } /// @@ -95,24 +97,27 @@ namespace ComputeSharp Guard.IsLessThanOrEqualTo((uint)offset + source.Length, (uint)Length, nameof(source)); using ID3D12ResourceMap resource = D3D12Resource->Map(); - - if (IsPaddingPresent) + fixed (void* sourcePointer = source) { - int count = source.Length; - ref T spanRef = ref MemoryMarshal.GetReference(source); - ref byte resourceRef = ref Unsafe.AsRef(resource.Pointer); - - // Move to the initial offset - resourceRef = ref Unsafe.Add(ref resourceRef, offset * GetPaddedSize()); - - for (var i = 0; i < count; i++) + if (IsPaddingPresent) { - ref byte targetRef = ref Unsafe.Add(ref resourceRef, i * GetPaddedSize()); - - Unsafe.As(ref targetRef) = Unsafe.Add(ref spanRef, i); + MemoryHelper.Copy( + sourcePointer, + resource.Pointer, + (uint)offset, + (uint)source.Length, + (uint)GetPaddedSize()); + } + else + { + MemoryHelper.Copy( + sourcePointer, + (uint)offset, + (uint)source.Length, + (uint)sizeof(T), + resource.Pointer); } } - else MemoryHelper.Copy(source, resource.Pointer, offset); } /// @@ -128,7 +133,7 @@ namespace ComputeSharp source.GraphicsDevice == GraphicsDevice) { // Directly copy the input buffer, if possible - using CommandList copyCommandList = new CommandList(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); + using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); copyCommandList.CopyBufferRegion(D3D12Resource, 0, source.D3D12Resource, 0,(ulong)SizeInBytes); copyCommandList.ExecuteAndWaitForCompletion(); diff --git a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs index a9a4c4fb..4842f413 100644 --- a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs @@ -9,192 +9,252 @@ namespace ComputeSharp.Graphics.Helpers internal static class MemoryHelper { /// - /// Copies the content of a to the area pointed by an input pointer. + /// Copies the content of a source memory area to a target one. /// - /// The type of values in the input . - /// The source to read. + /// The source memory area to read from. /// The pointer for the destination memory area. - /// The destination offset to start writing data to. + /// The destination offset to start writing data to. + /// The number of items to copy. + /// The size in bytes of each item to copy. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe void Copy(ReadOnlySpan source, void* destination, int destinationOffset) - where T : unmanaged + public static unsafe void Copy( + void* source, + void* destination, + uint offset, + uint length, + uint elementSizeInBytes) { - source.CopyTo(new Span((T*)destination + destinationOffset, source.Length)); + Buffer.MemoryCopy( + source, + (byte*)destination + offset * elementSizeInBytes, + ulong.MaxValue, + elementSizeInBytes * length); } /// - /// Copies a memory area pointed by a pointer to a target . + /// Copies the content of a source memory area to a target one, accounting for padding. + /// The destination memory area has padding for each element, while the source does not. /// - /// The type of values to read. - /// The pointer that indicates the memory area to read from. - /// The source offset to start reading data from. - /// The destination to write. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe void Copy(void* source, int sourceOffset, Span destination) + /// The source memory area to read from. + /// The pointer for the destination memory area. + /// The destination offset to start writing data to. + /// The number of items to copy. + /// The padded size of each element. + public static unsafe void Copy( + void* source, + void* destination, + uint offset, + uint length, + uint elementPitchInBytes) where T : unmanaged { - new Span((T*)source + sourceOffset, destination.Length).CopyTo(destination); + destination = (byte*)destination + offset * elementPitchInBytes; + + for (int i = 0; i < length; i++) + { + *(T*)&((byte*)destination)[i * elementPitchInBytes] = ((T*)source)[i]; + } } /// - /// Copies the content of a to the 2D area pointed by an input pointer. + /// Copies the content of a source memory area to a target one. /// - /// The type of values to read. - /// The destination to read from. + /// The source memory area to read from. + /// The source offset to start reading data from. + /// The number of items to copy. + /// The size in bytes of each item to copy. + /// The pointer for the destination memory area. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe void Copy( + void* source, + uint offset, + uint length, + uint elementSizeInBytes, + void* destination) + { + Buffer.MemoryCopy( + (byte*)source + offset * elementSizeInBytes, + destination, + ulong.MaxValue, + elementSizeInBytes * length); + } + + /// + /// Copies the content of a source memory area to a target one, accounting for padding. + /// The source memory area has padding for each element, while the destination does not. + /// + /// The source memory area to read from. + /// The source offset to start reading data from. + /// The number of items to copy. + /// The padded size of each element. + /// The pointer for the destination memory area. + public static unsafe void Copy( + void* source, + uint offset, + uint length, + uint elementPitchInBytes, + void* destination) + where T : unmanaged + { + source = (byte*)source + offset * elementPitchInBytes; + + for (int i = 0; i < length; i++) + { + ((T*)destination)[i] = *(T*)&((byte*)source)[i * elementPitchInBytes]; + } + } + + /// + /// Copies the content of a source memory area to the 2D area pointed by an input pointer. + /// The destination memory area has padding in each row, while the source does not. + /// + /// The source pointer to read from. /// The pointer that indicates the 2D memory area to write to. - /// The width of the 2D memory area to write to. /// The height of the 2D memory area to write to. /// The width of the memory area in bytes. /// The pitch (padded width) of the memory area in bytes. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe void Copy( - ReadOnlySpan source, + public static unsafe void Copy( + void* source, void* destination, - int width, - int height, + uint height, ulong widthInBytes, ulong pitchInBytes) - where T : unmanaged { if (widthInBytes == pitchInBytes) { - source.CopyTo(new Span(destination, width * height)); + Buffer.MemoryCopy(source, destination, ulong.MaxValue, widthInBytes * height); } else { for (int i = 0; i < height; i++) { - ReadOnlySpan sourceRow = source.Slice(i * width, width); - Span destinationRow = new((byte*)destination + (uint)i * pitchInBytes, width); - - sourceRow.CopyTo(destinationRow); + Buffer.MemoryCopy( + (byte*)source + (uint)i * widthInBytes, + (byte*)destination + (uint)i * pitchInBytes, + ulong.MaxValue, + widthInBytes); } } } /// - /// Copies a 2D memory area pointed by a pointer value to a target . + /// Copies a 2D memory area pointed by a pointer value to a target memory area. + /// The source memory area has padding in each row, while the target does not. /// - /// The type of values to read. /// The pointer that indicates the memory area to read from. - /// The width of the 2D memory area to read. /// The height of the 2D memory area to read. /// The width of the memory area in bytes. /// The pitch (padded width) of the memory area in bytes. - /// The destination to write. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe void Copy( + /// The destination pointer to write to. + public static unsafe void Copy( void* source, - int width, - int height, + uint height, ulong widthInBytes, ulong pitchInBytes, - Span destination) - where T : unmanaged + void* destination) { if (widthInBytes == pitchInBytes) { - new Span(source, width * height).CopyTo(destination); + Buffer.MemoryCopy(source, destination, ulong.MaxValue, widthInBytes * height); } else { for (int i = 0; i < height; i++) { - ReadOnlySpan sourceRow = new((byte*)source + (uint)i * pitchInBytes, width); - Span destinationRow = destination.Slice(i * width, width); - - sourceRow.CopyTo(destinationRow); + Buffer.MemoryCopy( + (byte*)source + (uint)i * pitchInBytes, + (byte*)destination + (uint)i * widthInBytes, + ulong.MaxValue, + widthInBytes); } } } /// - /// Copies the content of a to the 3D area pointed by an input pointer. + /// Copies the content of a source memory area to the 3D area pointed by an input pointer. + /// The destination memory area has padding in each row, while the source does not. /// /// The type of values to read. - /// The destination to read from. + /// The source memory area to read from. /// The pointer that indicates the 3D memory area to write to. - /// The width of the 3D memory area to write to. /// The height of the 3D memory area to write to. /// The depth of the 3D memory area to write to. /// The width of the memory area in bytes. /// The pitch (padded width) of the memory area in bytes. /// The size in bytes of each 2D slice within the target 3D memory area. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe void Copy( - ReadOnlySpan source, + public static unsafe void Copy( + void* source, void* destination, - int width, - int height, - int depth, + uint height, + uint depth, ulong widthInBytes, ulong pitchInBytes, ulong sliceInBytes) - where T : unmanaged { if (widthInBytes == pitchInBytes) { - source.CopyTo(new Span(destination, width * height * depth)); + Buffer.MemoryCopy(source, destination, ulong.MaxValue, widthInBytes * height * depth); } else { for (int z = 0; z < depth; z++) { - int sourceZOffset = width * height * z; - ulong destinationZOffset = sliceInBytes * (uint)z; + ulong + sourceZOffset = (uint)z * widthInBytes * height, + destinationZOffset = (uint)z * sliceInBytes; for (int i = 0; i < height; i++) { - ReadOnlySpan sourceRow = source.Slice(sourceZOffset + i * width, width); - Span destinationRow = new((byte*)destination + destinationZOffset + (uint)i * pitchInBytes, width); - - sourceRow.CopyTo(destinationRow); + Buffer.MemoryCopy( + (byte*)source + sourceZOffset + (uint)i * widthInBytes, + (byte*)destination + destinationZOffset + (uint)i * pitchInBytes, + ulong.MaxValue, + widthInBytes); } } } } /// - /// Copies a 3D memory area pointed by a pointer value to a target . + /// Copies a 3D memory area pointed by a pointer value to a target memory area. + /// The source memory area has padding in each row, while the target does not. /// /// The type of values to read. /// The pointer that indicates the memory area to read from. - /// The width of the 3D memory area to read. /// The height of the 3D memory area to read. /// The depth of the 3D memory area to read. /// The width of the memory area in bytes. /// The pitch (padded width) of the memory area in bytes. /// The size in bytes of each 2D slice within the source 3D memory area. - /// The destination to write. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe void Copy( + /// The destination pointer to write. + public static unsafe void Copy( void* source, - int width, - int height, - int depth, + uint height, + uint depth, ulong widthInBytes, ulong pitchInBytes, ulong sliceInBytes, - Span destination) - where T : unmanaged + void* destination) { if (widthInBytes == pitchInBytes) { - new Span(source, width * height * depth).CopyTo(destination); + Buffer.MemoryCopy(source, destination, ulong.MaxValue, widthInBytes * height * depth); } else { for (int z = 0; z < depth; z++) { - ulong sourceZOffset = sliceInBytes * (uint)z; - int destinationZOffset = width * height * z; + ulong + sourceZOffset = (uint)z * sliceInBytes, + destinationZOffset = (uint)z * widthInBytes * height; for (int i = 0; i < height; i++) { - ReadOnlySpan sourceRow = new((byte*)source + sourceZOffset + (uint)i * pitchInBytes, width); - Span destinationRow = destination.Slice(destinationZOffset + i * width, width); - - sourceRow.CopyTo(destinationRow); + Buffer.MemoryCopy( + (byte*)source + sourceZOffset + (uint)i * pitchInBytes, + (byte*)destination + destinationZOffset + (uint)i * widthInBytes, + ulong.MaxValue, + widthInBytes); } } } From e029b414883d1763abbd2b4ec099800a4825b097 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 6 Dec 2020 19:19:30 +0100 Subject: [PATCH 18/36] Minor code tweaks --- .../Buffers/Abstract/Buffer{T}.cs | 11 +---------- .../Buffers/Abstract/StructuredBuffer{T}.cs | 13 ++++++------- .../Buffers/ConstantBuffer{T}.cs | 4 ++-- 3 files changed, 9 insertions(+), 19 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs index 5331e22d..437d26c5 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs @@ -79,22 +79,13 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// public int Length { get; } - /// - /// Gets the size in bytes of each value contained in the buffer. - /// - protected int ElementSizeInBytes - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => Unsafe.SizeOf(); - } - /// /// Gets whether or not there is some padding between elements in the current buffer. /// internal bool IsPaddingPresent { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => SizeInBytes > Length * Unsafe.SizeOf(); + get => SizeInBytes > (nint)Length * sizeof(T); } /// diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs index 78fad45f..ee91333e 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.CompilerServices; using ComputeSharp.Graphics.Buffers.Enums; using ComputeSharp.Graphics.Buffers.Interop; using ComputeSharp.Graphics.Commands; @@ -24,8 +23,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The associated with the current instance. /// The number of items to store in the current buffer. /// The buffer type for the current buffer. - private protected StructuredBuffer(GraphicsDevice device, int length, ResourceType resourceType) - : base(device, length, (uint)Unsafe.SizeOf(), resourceType) + private protected unsafe StructuredBuffer(GraphicsDevice device, int length, ResourceType resourceType) + : base(device, length, (uint)sizeof(T), resourceType) { } @@ -40,8 +39,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsLessThanOrEqualTo((uint)offset + destination.Length, (uint)Length, nameof(destination)); nint - byteOffset = (nint)offset * ElementSizeInBytes, - byteSize = (nint)destination.Length * ElementSizeInBytes; + byteOffset = (nint)offset * sizeof(T), + byteSize = (nint)destination.Length * sizeof(T); using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, (ulong)byteSize); @@ -74,8 +73,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsLessThanOrEqualTo((uint)offset + source.Length, (uint)Length, nameof(source)); nint - byteOffset = (nint)offset * ElementSizeInBytes, - byteSize = (nint)source.Length * ElementSizeInBytes; + byteOffset = (nint)offset * sizeof(T), + byteSize = (nint)source.Length * sizeof(T); using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, (ulong)byteSize); diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 16c005bb..9fc1bc86 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -47,9 +47,9 @@ namespace ComputeSharp /// [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static int GetPaddedSize() + private static unsafe int GetPaddedSize() { - return (Unsafe.SizeOf() + 15) & ~15; + return (sizeof(T) + 15) & ~15; } /// From ccec496316c38ea7889ec76d257c7d43213d206a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 6 Dec 2020 19:50:28 +0100 Subject: [PATCH 19/36] Refactored core buffer/texture APIs to use ref-s --- .../Buffers/Abstract/Buffer{T}.cs | 29 +++++++++++-- .../Buffers/Abstract/StructuredBuffer{T}.cs | 20 ++++----- .../Buffers/Abstract/Texture2D{T}.cs | 37 ++++++++++++++-- .../Buffers/Abstract/Texture3D{T}.cs | 43 ++++++++++++++++--- .../Buffers/ConstantBuffer{T}.cs | 20 ++++----- 5 files changed, 116 insertions(+), 33 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs index 437d26c5..dcd3c52e 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs @@ -2,6 +2,7 @@ using System.Buffers; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using ComputeSharp.Core.Interop; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Buffers.Enums; @@ -159,7 +160,18 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// /// The input to write data to. /// The offset to start reading data from. - public abstract void GetData(Span destination, int offset); + public void GetData(Span destination, int offset) + { + GetData(ref MemoryMarshal.GetReference(destination), destination.Length, offset); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target memory area. + /// + /// The input memory area to write data to. + /// The size of the memory area to write data to. + /// The offset to start reading data from. + internal abstract void GetData(ref T destination, nint size, int offset); /// /// Writes the contents of a given array to the current instance. @@ -196,12 +208,21 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// /// Writes the contents of a given to a specified area of the current instance. - /// The input data will be written into the buffer starting at the specified offset, and all input items will be copied. /// /// The input to read data from. /// The offset to start writing data to. - /// The number of items to write. - public abstract void SetData(ReadOnlySpan source, int offset); + public void SetData(ReadOnlySpan source, int offset) + { + SetData(ref MemoryMarshal.GetReference(source), source.Length, offset); + } + + /// + /// 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 input memory area to read data from. + /// The offset to start writing data to. + internal abstract void SetData(ref T source, nint size, int offset); /// /// Writes the contents of a given to the current instance. diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs index ee91333e..b2fcec46 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs @@ -29,18 +29,18 @@ namespace ComputeSharp.Graphics.Buffers.Abstract } /// - public override unsafe void GetData(Span destination, int offset) + internal override unsafe void GetData(ref T destination, nint size, int offset) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); Guard.IsInRange(offset, 0, Length, nameof(offset)); - Guard.IsLessThanOrEqualTo((uint)offset + destination.Length, (uint)Length, nameof(destination)); + Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size)); nint byteOffset = (nint)offset * sizeof(T), - byteSize = (nint)destination.Length * sizeof(T); + byteSize = size * sizeof(T); using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, (ulong)byteSize); @@ -51,40 +51,40 @@ namespace ComputeSharp.Graphics.Buffers.Abstract } using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); - fixed (void* destinationPointer = destination) + fixed (void* destinationPointer = &destination) { MemoryHelper.Copy( resource.Pointer, 0u, - (uint)destination.Length, + (uint)size, (uint)sizeof(T), destinationPointer); } } /// - public override unsafe void SetData(ReadOnlySpan source, int offset) + internal override unsafe void SetData(ref T source, nint size, int offset) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); Guard.IsInRange(offset, 0, Length, nameof(offset)); - Guard.IsLessThanOrEqualTo((uint)offset + source.Length, (uint)Length, nameof(source)); + Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size)); nint byteOffset = (nint)offset * sizeof(T), - byteSize = (nint)source.Length * sizeof(T); + byteSize = size * sizeof(T); using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, (ulong)byteSize); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) - fixed (void* sourcePointer = source) + fixed (void* sourcePointer = &source) { MemoryHelper.Copy( sourcePointer, 0u, - (uint)source.Length, + (uint)size, (uint)sizeof(T), resource.Pointer); } diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index 5935c197..8f422a98 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using ComputeSharp.Core.Interop; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Buffers.Enums; @@ -229,6 +230,20 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The width of the memory area to copy. /// The height of the memory area to copy. public void GetData(Span destination, int x, int y, int width, int height) + { + GetData(ref MemoryMarshal.GetReference(destination), destination.Length, x, y, width, height); + } + + /// + /// 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 GetData(ref T destination, nint size, int x, int y, int width, int height) { GraphicsDevice.ThrowIfDisposed(); @@ -240,7 +255,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); - Guard.HasSizeGreaterThanOrEqualTo(destination, width * height, nameof(destination)); + Guard.IsGreaterThanOrEqualTo(size, (nint)width * height, nameof(size)); D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(DXGIFormatHelper.GetForType(), (ulong)width, (uint)height); @@ -274,7 +289,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); - fixed (void* destinationPointer = destination) + fixed (void* destinationPointer = &destination) { MemoryHelper.Copy( resource.Pointer, @@ -392,6 +407,20 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The width of the memory area to write to. /// The height of the memory area to write to. public void SetData(ReadOnlySpan source, int x, int y, int width, int height) + { + SetData(ref MemoryMarshal.GetReference(source), source.Length, x, y, width, height); + } + + /// + /// 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. + public void SetData(ref T source, nint size, int x, int y, int width, int height) { GraphicsDevice.ThrowIfDisposed(); @@ -403,7 +432,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); - Guard.HasSizeGreaterThanOrEqualTo(source, width * height, nameof(source)); + Guard.IsGreaterThanOrEqualTo(size, (nint)width * height, nameof(size)); D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(DXGIFormatHelper.GetForType(), (ulong)width, (uint)height); @@ -417,7 +446,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) - fixed (void* sourcePointer = source) + fixed (void* sourcePointer = &source) { MemoryHelper.Copy( sourcePointer, diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index 2ee65640..2c8029bf 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using ComputeSharp.Core.Interop; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Buffers.Enums; @@ -221,6 +222,22 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The height of the memory area to copy. /// The depth of the memory area to copy. public void GetData(Span destination, int x, int y, int z, int width, int height, int depth) + { + GetData(ref MemoryMarshal.GetReference(destination), destination.Length, x, y, z, width, height, depth); + } + + /// + /// 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 memory area to write data to. + /// 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 copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public void GetData(ref T destination, nint size, int x, int y, int z, int width, int height, int depth) { GraphicsDevice.ThrowIfDisposed(); @@ -235,7 +252,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); Guard.IsLessThanOrEqualTo(z + depth, Depth, nameof(z)); - Guard.HasSizeGreaterThanOrEqualTo(destination, width * height * depth, nameof(destination)); + Guard.IsGreaterThanOrEqualTo(size, (nint)width * height * depth, nameof(size)); D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(DXGIFormatHelper.GetForType(), (ulong)width, (uint)height, (ushort)depth); @@ -269,7 +286,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); - fixed (void* destinationPointer = destination) + fixed (void* destinationPointer = &destination) { MemoryHelper.Copy( resource.Pointer, @@ -404,6 +421,22 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The height of the memory area to write to. /// The depth of the memory area to write to. public void SetData(ReadOnlySpan source, int x, int y, int z, int width, int height, int depth) + { + SetData(ref MemoryMarshal.GetReference(source), source.Length, x, y, z, width, height, depth); + } + + /// + /// 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 depthwise offseet 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. + public void SetData(ref T source, nint size, int x, int y, int z, int width, int height, int depth) { GraphicsDevice.ThrowIfDisposed(); @@ -418,7 +451,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); Guard.IsLessThanOrEqualTo(z + depth, Depth, nameof(z)); - Guard.HasSizeGreaterThanOrEqualTo(source, width * height * depth, nameof(source)); + Guard.IsGreaterThanOrEqualTo(size, (nint)width * height * depth, nameof(size)); D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(DXGIFormatHelper.GetForType(), (ulong)width, (uint)height, (ushort)depth); @@ -432,14 +465,14 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) - fixed (void* sourcePointer = source) + fixed (void* sourcePointer = &source) { MemoryHelper.Copy( sourcePointer, resource.Pointer, (uint)height, (uint)depth, - rowSizeInBytes, + rowSizeInBytes, d3D12PlacedSubresourceFootprint.Footprint.RowPitch, d3D12PlacedSubresourceFootprint.Footprint.RowPitch * (uint)height); } diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 9fc1bc86..0c8d2e08 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -53,24 +53,24 @@ namespace ComputeSharp } /// - public override unsafe void GetData(Span destination, int offset) + internal override unsafe void GetData(ref T destination, nint size, int offset) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); Guard.IsInRange(offset, 0, Length, nameof(offset)); - Guard.IsLessThanOrEqualTo((uint)offset + destination.Length, (uint)Length, nameof(destination)); + Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size)); using ID3D12ResourceMap resource = D3D12Resource->Map(); - fixed (void* destinationPointer = destination) + fixed (void* destinationPointer = &destination) { if (IsPaddingPresent) { MemoryHelper.Copy( resource.Pointer, (uint)offset, - (uint)destination.Length, + (uint)size, (uint)GetPaddedSize(), destinationPointer); } @@ -79,7 +79,7 @@ namespace ComputeSharp MemoryHelper.Copy( resource.Pointer, (uint)offset, - (uint)destination.Length, + (uint)size, (uint)sizeof(T), destinationPointer); } @@ -87,17 +87,17 @@ namespace ComputeSharp } /// - public override unsafe void SetData(ReadOnlySpan source, int offset) + internal override unsafe void SetData(ref T source, nint size, int offset) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); Guard.IsInRange(offset, 0, Length, nameof(offset)); - Guard.IsLessThanOrEqualTo((uint)offset + source.Length, (uint)Length, nameof(source)); + Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size)); using ID3D12ResourceMap resource = D3D12Resource->Map(); - fixed (void* sourcePointer = source) + fixed (void* sourcePointer = &source) { if (IsPaddingPresent) { @@ -105,7 +105,7 @@ namespace ComputeSharp sourcePointer, resource.Pointer, (uint)offset, - (uint)source.Length, + (uint)size, (uint)GetPaddedSize()); } else @@ -113,7 +113,7 @@ namespace ComputeSharp MemoryHelper.Copy( sourcePointer, (uint)offset, - (uint)source.Length, + (uint)size, (uint)sizeof(T), resource.Pointer); } From 0e2d604f47ab9739201b69ca6cc2e5d47388fcc1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 6 Dec 2020 21:46:43 +0100 Subject: [PATCH 20/36] Refactored public Buffer APIs --- .../Buffers/Abstract/Buffer{T}.cs | 124 +------------- .../Buffers/Views/BufferDebugView{T}.cs | 13 +- src/ComputeSharp.Shaders/ShaderRunner{T}.cs | 4 +- .../Extensions/BufferExtensions.cs | 156 ++++++++++++++++++ 4 files changed, 173 insertions(+), 124 deletions(-) create mode 100644 src/ComputeSharp/Extensions/BufferExtensions.cs diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs index dcd3c52e..a7b5c7f9 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs @@ -1,6 +1,4 @@ -using System; -using System.Buffers; -using System.Diagnostics.Contracts; +using System.Buffers; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ComputeSharp.Core.Interop; @@ -94,77 +92,6 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// internal ID3D12Resource* D3D12Resource => this.d3D12Resource; - /// - /// Reads the contents of the current instance and returns an array. - /// - /// A array with the contents of the current buffer. - [Pure] - public T[] GetData() - { - return GetData(0, Length); - } - - /// - /// Reads the contents of the current instance in a given range and returns an array. - /// - /// The offset to start reading data from. - /// The number of items to read. - /// A array with the contents of the specified range from the current buffer. - [Pure] - public T[] GetData(int offset, int count) - { - Guard.IsGreaterThanOrEqualTo(count, 0, nameof(count)); - - T[] data = GC.AllocateUninitializedArray(count); - - GetData(data.AsSpan(), offset); - - return data; - } - - /// - /// Reads the contents of the current instance and writes them into a target array. - /// - /// The input array to write data to. - public void GetData(T[] destination) - { - GetData(destination.AsSpan(), 0); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target array. - /// - /// The input array to write data to. - /// The starting offset within to write data to. - /// The offset to start reading data from. - /// The number of items to read. - public void GetData(T[] destination, int destinationOffset, int bufferOffset, int count) - { - Span span = destination.AsSpan(destinationOffset, count); - - GetData(span, bufferOffset); - } - - /// - /// Reads the contents of the current instance and writes them into a target . - /// The input data will be read from the start of the buffer. - /// - /// The input to write data to. - public void GetData(Span destination) - { - GetData(destination, 0); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target . - /// - /// The input to write data to. - /// The offset to start reading data from. - public void GetData(Span destination, int offset) - { - GetData(ref MemoryMarshal.GetReference(destination), destination.Length, offset); - } - /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -173,49 +100,6 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The offset to start reading data from. internal abstract void GetData(ref T destination, nint size, int offset); - /// - /// Writes the contents of a given array to the current instance. - /// - /// The input array to read data from. - public void SetData(T[] source) - { - SetData(source.AsSpan()); - } - - /// - /// Writes the contents of a given array to a specified area of the current instance. - /// - /// The input array to read data from. - /// The starting offset within to read data from. - /// The offset to start writing data to. - /// The number of items to write. - public void SetData(T[] source, int sourceOffset, int bufferOffset, int count) - { - ReadOnlySpan span = source.AsSpan(sourceOffset, count); - - SetData(span, bufferOffset); - } - - /// - /// Writes the contents of a given to the current instance. - /// The input data will be written to the start of the buffer, and all input items will be copied. - /// - /// The input to read data from. - public void SetData(ReadOnlySpan source) - { - SetData(source, 0); - } - - /// - /// Writes the contents of a given to a specified area of the current instance. - /// - /// The input to read data from. - /// The offset to start writing data to. - public void SetData(ReadOnlySpan source, int offset) - { - SetData(ref MemoryMarshal.GetReference(source), source.Length, offset); - } - /// /// Writes the contents of a given memory area to a specified area of the current instance. /// @@ -240,11 +124,9 @@ namespace ComputeSharp.Graphics.Buffers.Abstract try { - Span span = array.AsSpan(0, source.Length); + source.GetData(ref MemoryMarshal.GetArrayDataReference(array), source.Length, 0); - source.GetData(span); - - SetData(span); + SetData(ref MemoryMarshal.GetArrayDataReference(array), source.Length, 0); } finally { diff --git a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs b/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs index ad14b604..76b35a60 100644 --- a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs @@ -1,4 +1,6 @@ -using System.Diagnostics; +using System; +using System.Diagnostics; +using System.Runtime.InteropServices; using ComputeSharp.Graphics.Buffers.Abstract; namespace ComputeSharp.Graphics.Buffers.Views @@ -16,7 +18,14 @@ namespace ComputeSharp.Graphics.Buffers.Views /// The input instance with the items to display. public BufferDebugView(Buffer? buffer) { - Items = buffer?.GetData(); + if (buffer is not null) + { + var items = GC.AllocateUninitializedArray(buffer.Length); + + buffer.GetData(ref MemoryMarshal.GetArrayDataReference(items), buffer.Length, 0); + + Items = items; + } } /// diff --git a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs index 7f9248a2..5bec10c2 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using ComputeSharp.Core.Extensions; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Commands; @@ -136,8 +137,9 @@ namespace ComputeSharp.Shaders // Initialize the loop targets and the captured values ConstantBuffer variablesBuffer = ShaderRunner.GetVariablesBuffer(device); + ReadOnlySpan variables = dispatchData.Variables; - variablesBuffer.SetData(dispatchData.Variables); + variablesBuffer.SetData(ref MemoryMarshal.GetReference(variables), variables.Length, 0); commandList.SetComputeRootDescriptorTable(0, variablesBuffer.D3D12GpuDescriptorHandle); diff --git a/src/ComputeSharp/Extensions/BufferExtensions.cs b/src/ComputeSharp/Extensions/BufferExtensions.cs new file mode 100644 index 00000000..95ddd2f5 --- /dev/null +++ b/src/ComputeSharp/Extensions/BufferExtensions.cs @@ -0,0 +1,156 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.InteropServices; +using ComputeSharp.Graphics.Buffers.Abstract; +using Microsoft.Toolkit.Diagnostics; + +namespace ComputeSharp +{ + /// + /// A that contains extension methods for the type. + /// + public static class BufferExtensions + { + /// + /// Reads the contents of a instance and returns an array. + /// + /// The type of items stored on the buffer. + /// The input instance to read data from. + /// A array with the contents of the input buffer. + [Pure] + public static T[] GetData(this Buffer buffer) + where T : unmanaged + { + return buffer.GetData(0, buffer.Length); + } + + /// + /// Reads the contents of a instance in a given range and returns an array. + /// + /// The type of items stored on the buffer. + /// The input instance to read data from. + /// The offset to start reading data from. + /// The number of items to read. + /// A array with the contents of the specified range from the current buffer. + [Pure] + public static T[] GetData(this Buffer buffer, int offset, int count) + where T : unmanaged + { + Guard.IsGreaterThanOrEqualTo(count, 0, nameof(count)); + + T[] data = GC.AllocateUninitializedArray(count); + + buffer.GetData(data.AsSpan(), offset); + + return data; + } + + /// + /// Reads the contents of s instance and writes them into a target array. + /// + /// The type of items stored on the buffer. + /// The input instance to read data from. + /// The input array to write data to. + public static void GetData(this Buffer buffer, T[] destination) + where T : unmanaged + { + buffer.GetData(destination.AsSpan(), 0); + } + + /// + /// Reads the contents of the specified range from s instance and writes them into a target array. + /// + /// The type of items stored on the buffer. + /// The input instance to read data from. + /// The input array to write data to. + /// The starting offset within to write data to. + /// The offset to start reading data from. + /// The number of items to read. + public static void GetData(this Buffer buffer, T[] destination, int destinationOffset, int bufferOffset, int count) + where T : unmanaged + { + Span span = destination.AsSpan(destinationOffset, count); + + buffer.GetData(span, bufferOffset); + } + + /// + /// Reads the contents of a instance and writes them into a target . + /// + /// The type of items stored on the buffer. + /// The input instance to read data from. + /// The input to write data to. + public static void GetData(this Buffer buffer, Span destination) + where T : unmanaged + { + buffer.GetData(destination, 0); + } + + /// + /// Reads the contents of the specified range from a instance and writes them into a target . + /// + /// The type of items stored on the buffer. + /// The input instance to read data from. + /// The input to write data to. + /// The offset to start reading data from. + public static void GetData(this Buffer buffer, Span destination, int offset) + where T : unmanaged + { + buffer.GetData(ref MemoryMarshal.GetReference(destination), destination.Length, offset); + } + + /// + /// Writes the contents of a given array to a instance. + /// + /// The type of items stored on the buffer. + /// The target instance to write data to. + /// The input array to read data from. + public static void SetData(this Buffer buffer, T[] source) + where T : unmanaged + { + buffer.SetData(source.AsSpan(), 0); + } + + /// + /// Writes the contents of a given array to a specified area of a instance. + /// + /// The type of items stored on the buffer. + /// The target instance to write data to. + /// The input array to read data from. + /// The starting offset within to read data from. + /// The offset to start writing data to. + /// The number of items to write. + public static void SetData(this Buffer buffer, T[] source, int sourceOffset, int bufferOffset, int count) + where T : unmanaged + { + ReadOnlySpan span = source.AsSpan(sourceOffset, count); + + buffer.SetData(span, bufferOffset); + } + + /// + /// Writes the contents of a given to a instance. + /// + /// The type of items stored on the buffer. + /// The target instance to write data to. + /// The input to read data from. + public static void SetData(this Buffer buffer, ReadOnlySpan source) + where T : unmanaged + { + buffer.SetData(source, 0); + } + + /// + /// Writes the contents of a given to a specified area of a instance. + /// + /// The type of items stored on the buffer. + /// The target instance to write data to. + /// The input to read data from. + /// The offset to start writing data to. + public static void SetData(this Buffer buffer, ReadOnlySpan source, int offset) + where T : unmanaged + { + buffer.SetData(ref MemoryMarshal.GetReference(source), source.Length, offset); + } + } +} From 4c5eeb9a9a18b3fa77ab3ba2347a1527b548a50a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 6 Dec 2020 22:21:07 +0100 Subject: [PATCH 21/36] Refactored public Texture2D APIs --- .../Buffers/Abstract/Texture2D{T}.cs | 251 +------------- .../Buffers/ConstantBuffer{T}.cs | 3 +- .../Buffers/Views/BufferDebugView2D{T}.cs | 9 +- .../Buffers/Views/BufferDebugView{T}.cs | 3 +- src/ComputeSharp/ComputeSharp.csproj | 1 + .../Extensions/Texture2DExtensions.cs | 314 ++++++++++++++++++ 6 files changed, 326 insertions(+), 255 deletions(-) create mode 100644 src/ComputeSharp/Extensions/Texture2DExtensions.cs diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index 8f422a98..780e81de 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -1,7 +1,4 @@ -using System; -using System.Diagnostics.Contracts; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; +using System.Runtime.CompilerServices; using ComputeSharp.Core.Interop; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Buffers.Enums; @@ -99,141 +96,6 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// internal ID3D12Resource* D3D12Resource => this.d3D12Resource; - /// - /// Reads the contents of the current instance and returns an array. - /// - /// A array with the contents of the current buffer. - [Pure] - public T[,] GetData() - { - T[,] data = new T[Height, Width]; - - GetData(data); - - return data; - } - - /// - /// Reads the contents of the current instance and writes them into a target array. - /// - /// The input array to write data to. - public void GetData(T[,] destination) - { - Guard.IsEqualTo(destination.GetLength(0), Height, nameof(destination)); - Guard.IsEqualTo(destination.GetLength(1), Width, nameof(destination)); - - fixed (T* p = destination) - { - GetData(new Span(p, destination.Length)); - } - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target array. - /// - /// The input array to write data to. - public void GetData(T[] destination) - { - GetData(destination.AsSpan(), 0, 0, Width, Height); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target array. - /// - /// The input array to write data to. - /// The horizontal range of items to copy. - /// The vertical range of items to copy. - public void GetData(T[] destination, Range x, Range y) - { - GetData(destination.AsSpan(), x, y); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target array. - /// - /// The input array 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. - public void GetData(T[] destination, int x, int y, int width, int height) - { - GetData(destination.AsSpan(), x, y, width, height); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target array. - /// - /// The input array to write data to. - /// The starting offset within to write data to. - public void GetData(T[] destination, int offset) - { - GetData(destination.AsSpan(offset), 0, 0, Width, Height); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target array. - /// - /// The input array to write data to. - /// The starting offset within to write data to. - /// The horizontal range of items to copy. - /// The vertical range of items to copy. - public void GetData(T[] destination, int offset, Range x, Range y) - { - GetData(destination.AsSpan(offset), x, y); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target array. - /// - /// The input array to write data to. - /// The starting offset within 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. - public void GetData(T[] destination, int offset, int x, int y, int width, int height) - { - GetData(destination.AsSpan(offset), x, y, width, height); - } - - /// - /// Reads the contents of the current instance and writes them into a target . - /// The input data will be read from the start of the texture. - /// - /// The input to write data to. - public void GetData(Span destination) - { - GetData(destination, 0, 0, Width, Height); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target . - /// - /// The input to write data to. - /// The horizontal range of items to copy. - /// The vertical range of items to copy. - public void GetData(Span destination, Range x, Range y) - { - var (offsetX, width) = x.GetOffsetAndLength(Width); - var (offsetY, height) = y.GetOffsetAndLength(Height); - - GetData(destination, offsetX, offsetY, width, height); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target . - /// - /// The input 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. - public void GetData(Span destination, int x, int y, int width, int height) - { - GetData(ref MemoryMarshal.GetReference(destination), destination.Length, x, y, width, height); - } - /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -300,117 +162,6 @@ namespace ComputeSharp.Graphics.Buffers.Abstract } } - /// - /// Writes the contents of a given array to the current instance. - /// - /// The input array to read data from. - public void SetData(T[,] source) - { - Guard.IsEqualTo(source.GetLength(0), Height, nameof(source)); - Guard.IsEqualTo(source.GetLength(1), Width, nameof(source)); - - fixed (T* p = source) - { - SetData(new Span(p, source.Length)); - } - } - - /// - /// Writes the contents of a given array to the current instance. - /// - /// The input array to read data from. - public void SetData(T[] source) - { - SetData(source.AsSpan(), 0, 0, Width, Height); - } - - /// - /// Writes the contents of a given array to a specified area of the current instance. - /// - /// The input array to read data from. - /// The horizontal range of items to write. - /// The vertical range of items to write. - public void SetData(T[] source, Range x, Range y) - { - SetData(source.AsSpan(), x, y); - } - - /// - /// Writes the contents of a given array to a specified area of the current instance. - /// - /// The input array 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. - public void SetData(T[] source, int x, int y, int width, int height) - { - SetData(source.AsSpan(), x, y, width, height); - } - - /// - /// Writes the contents of a given array to a specified area of the current instance. - /// - /// The input array to read data from. - /// The starting offset within to read data from. - /// The horizontal range of items to write. - /// The vertical range of items to write. - public void SetData(T[] source, int offset, Range x, Range y) - { - SetData(source.AsSpan(offset), x, y); - } - - /// - /// Writes the contents of a given array to a specified area of the current instance. - /// - /// The input array to read data from. - /// The starting offset within 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. - public void SetData(T[] source, int offset, int x, int y, int width, int height) - { - SetData(source.AsSpan(offset), x, y, width, height); - } - - /// - /// Writes the contents of a given to the current instance. - /// The input data will be written to the start of the texture, and all input items will be copied. - /// - /// The input to read data from. - public void SetData(ReadOnlySpan source) - { - SetData(source, 0, 0, Width, Height); - } - - /// - /// Writes the contents of a given to a specified area of the current instance. - /// - /// The input to read data from. - /// The horizontal range of items to write. - /// The vertical range of items to write. - public void SetData(ReadOnlySpan source, Range x, Range y) - { - var (offsetX, width) = x.GetOffsetAndLength(Width); - var (offsetY, height) = y.GetOffsetAndLength(Height); - - SetData(source, offsetX, offsetY, width, height); - } - - /// - /// Writes the contents of a given to a specified area of the current instance. - /// - /// The input 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. - public void SetData(ReadOnlySpan source, int x, int y, int width, int height) - { - SetData(ref MemoryMarshal.GetReference(source), source.Length, x, y, width, height); - } - /// /// Writes the contents of a given memory area to a specified area of the current instance. /// diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 0c8d2e08..732c78f2 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using ComputeSharp.Exceptions; diff --git a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView2D{T}.cs index c3061ea1..720d46df 100644 --- a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView2D{T}.cs @@ -16,7 +16,14 @@ namespace ComputeSharp.Graphics.Buffers.Views /// The input instance with the items to display. public BufferDebugView2D(Texture2D? texture) { - Items = texture?.GetData(); + if (texture is not null) + { + var items = new T[texture.Height, texture.Width]; + + texture.GetData(ref items[0, 0], items.Length, 0, 0, texture.Height, texture.Width); + + Items = items; + } } /// diff --git a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs b/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs index 76b35a60..59950292 100644 --- a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs @@ -1,6 +1,5 @@ using System; using System.Diagnostics; -using System.Runtime.InteropServices; using ComputeSharp.Graphics.Buffers.Abstract; namespace ComputeSharp.Graphics.Buffers.Views @@ -22,7 +21,7 @@ namespace ComputeSharp.Graphics.Buffers.Views { var items = GC.AllocateUninitializedArray(buffer.Length); - buffer.GetData(ref MemoryMarshal.GetArrayDataReference(items), buffer.Length, 0); + buffer.GetData(ref items[0], buffer.Length, 0); Items = items; } diff --git a/src/ComputeSharp/ComputeSharp.csproj b/src/ComputeSharp/ComputeSharp.csproj index e2f82a06..a4aa8e9e 100644 --- a/src/ComputeSharp/ComputeSharp.csproj +++ b/src/ComputeSharp/ComputeSharp.csproj @@ -4,6 +4,7 @@ net5.0-windows10.0.19041.0 preview enable + true diff --git a/src/ComputeSharp/Extensions/Texture2DExtensions.cs b/src/ComputeSharp/Extensions/Texture2DExtensions.cs new file mode 100644 index 00000000..4c1b03e2 --- /dev/null +++ b/src/ComputeSharp/Extensions/Texture2DExtensions.cs @@ -0,0 +1,314 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.InteropServices; +using ComputeSharp.Graphics.Buffers.Abstract; +using Microsoft.Toolkit.Diagnostics; + +namespace ComputeSharp +{ + /// + /// A that contains extension methods for the type. + /// + public static class Texture2DExtensions + { + /// + /// Reads the contents of the current instance and returns an array. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// A array with the contents of the current texture. + [Pure] + public static T[,] GetData(this Texture2D texture) + where T : unmanaged + { + T[,] data = new T[texture.Height, texture.Width]; + + texture.GetData(data); + + return data; + } + + /// + /// Reads the contents of the current instance and writes them into a target array. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input array to write data to. + public static void GetData(this Texture2D texture, T[,] destination) + where T : unmanaged + { + Guard.IsEqualTo(destination.GetLength(0), texture.Height, nameof(destination)); + Guard.IsEqualTo(destination.GetLength(1), texture.Width, nameof(destination)); + + texture.GetData(ref destination[0, 0], destination.Length, 0, 0, texture.Width, texture.Height); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target array. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input array to write data to. + public static void GetData(this Texture2D texture, T[] destination) + where T : unmanaged + { + texture.GetData(destination.AsSpan(), 0, 0, texture.Width, texture.Height); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target array. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input array to write data to. + /// The horizontal range of items to copy. + /// The vertical range of items to copy. + public static void GetData(this Texture2D texture, T[] destination, Range x, Range y) + where T : unmanaged + { + texture.GetData(destination.AsSpan(), x, y); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target array. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input array 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. + public static void GetData(this Texture2D texture, T[] destination, int x, int y, int width, int height) + where T : unmanaged + { + texture.GetData(destination.AsSpan(), x, y, width, height); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target array. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input array to write data to. + /// The starting offset within to write data to. + public static void GetData(this Texture2D texture, T[] destination, int offset) + where T : unmanaged + { + texture.GetData(destination.AsSpan(offset), 0, 0, texture.Width, texture.Height); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target array. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input array to write data to. + /// The starting offset within to write data to. + /// The horizontal range of items to copy. + /// The vertical range of items to copy. + public static void GetData(this Texture2D texture, T[] destination, int offset, Range x, Range y) + where T : unmanaged + { + texture.GetData(destination.AsSpan(offset), x, y); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target array. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input array to write data to. + /// The starting offset within 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. + public static void GetData(this Texture2D texture, T[] destination, int offset, int x, int y, int width, int height) + where T : unmanaged + { + texture.GetData(destination.AsSpan(offset), x, y, width, height); + } + + /// + /// Reads the contents of the current instance and writes them into a target . + /// The input data will be read from the start of the texture. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input to write data to. + public static void GetData(this Texture2D texture, Span destination) + where T : unmanaged + { + texture.GetData(destination, 0, 0, texture.Width, texture.Height); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target . + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input to write data to. + /// The horizontal range of items to copy. + /// The vertical range of items to copy. + public static void GetData(this Texture2D texture, Span destination, Range x, Range y) + where T : unmanaged + { + var (offsetX, width) = x.GetOffsetAndLength(texture.Width); + var (offsetY, height) = y.GetOffsetAndLength(texture.Height); + + texture.GetData(destination, offsetX, offsetY, width, height); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target . + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input 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. + public static void GetData(this Texture2D texture, Span destination, int x, int y, int width, int height) + where T : unmanaged + { + texture.GetData(ref MemoryMarshal.GetReference(destination), destination.Length, x, y, width, height); + } + + /// + /// Writes the contents of a given array to the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input array to read data from. + public static unsafe void SetData(this Texture2D texture, T[,] source) + where T : unmanaged + { + Guard.IsEqualTo(source.GetLength(0), texture.Height, nameof(source)); + Guard.IsEqualTo(source.GetLength(1), texture.Width, nameof(source)); + + texture.SetData(ref source[0, 0], source.Length, 0, 0, texture.Width, texture.Height); + } + + /// + /// Writes the contents of a given array to the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input array to read data from. + public static void SetData(this Texture2D texture, T[] source) + where T : unmanaged + { + texture.SetData(source.AsSpan(), 0, 0, texture.Width, texture.Height); + } + + /// + /// Writes the contents of a given array to a specified area of the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input array to read data from. + /// The horizontal range of items to write. + /// The vertical range of items to write. + public static void SetData(this Texture2D texture, T[] source, Range x, Range y) + where T : unmanaged + { + texture.SetData(source.AsSpan(), x, y); + } + + /// + /// Writes the contents of a given array to a specified area of the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input array 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. + public static void SetData(this Texture2D texture, T[] source, int x, int y, int width, int height) + where T : unmanaged + { + texture.SetData(source.AsSpan(), x, y, width, height); + } + + /// + /// Writes the contents of a given array to a specified area of the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input array to read data from. + /// The starting offset within to read data from. + /// The horizontal range of items to write. + /// The vertical range of items to write. + public static void SetData(this Texture2D texture, T[] source, int offset, Range x, Range y) + where T : unmanaged + { + texture.SetData(source.AsSpan(offset), x, y); + } + + /// + /// Writes the contents of a given array to a specified area of the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input array to read data from. + /// The starting offset within 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. + public static void SetData(this Texture2D texture, T[] source, int offset, int x, int y, int width, int height) + where T : unmanaged + { + texture.SetData(source.AsSpan(offset), x, y, width, height); + } + + /// + /// Writes the contents of a given to the current instance. + /// The input data will be written to the start of the texture, and all input items will be copied. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input to read data from. + public static void SetData(this Texture2D texture, ReadOnlySpan source) + where T : unmanaged + { + texture.SetData(source, 0, 0, texture.Width, texture.Height); + } + + /// + /// Writes the contents of a given to a specified area of the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input to read data from. + /// The horizontal range of items to write. + /// The vertical range of items to write. + public static void SetData(this Texture2D texture, ReadOnlySpan source, Range x, Range y) + where T : unmanaged + { + var (offsetX, width) = x.GetOffsetAndLength(texture.Width); + var (offsetY, height) = y.GetOffsetAndLength(texture.Height); + + texture.SetData(source, offsetX, offsetY, width, height); + } + + /// + /// Writes the contents of a given to a specified area of the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input 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. + public static void SetData(this Texture2D texture, ReadOnlySpan source, int x, int y, int width, int height) + where T : unmanaged + { + texture.SetData(ref MemoryMarshal.GetReference(source), source.Length, x, y, width, height); + } + } +} From 9806d02279fe8d9313632d214ddc98d9bb397587 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 6 Dec 2020 22:40:15 +0100 Subject: [PATCH 22/36] Refactored public Texture3D APIs --- .../Buffers/Abstract/Texture3D{T}.cs | 249 +------------- .../Extensions/Texture3DExtensions.cs | 303 ++++++++++++++++++ 2 files changed, 304 insertions(+), 248 deletions(-) create mode 100644 src/ComputeSharp/Extensions/Texture3DExtensions.cs diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index 2c8029bf..c86d5618 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -1,7 +1,4 @@ -using System; -using System.Diagnostics.Contracts; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; +using System.Runtime.CompilerServices; using ComputeSharp.Core.Interop; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Buffers.Enums; @@ -108,124 +105,6 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// internal ID3D12Resource* D3D12Resource => this.d3D12Resource; - /// - /// Reads the contents of the current instance and returns an array. - /// - /// A array with the contents of the current buffer. - /// - /// The returned array will be using the same memory layout as the texture, that is, each 2D plane - /// in the 3D volume represented by the texture is contiguous in memory, and planes are stacked in - /// the depth dimension. This means that the resulting 3D array will have a size of [D, H, W]. - /// - [Pure] - public T[,,] GetData() - { - T[,,] data = new T[Depth, Height, Width]; - - GetData(data); - - return data; - } - - /// - /// Reads the contents of the current instance and writes them into a target array. - /// - /// The input array to write data to. - /// - /// The input 3D array needs to have each 2D plane stacked on the depth axis. That is, the expected - /// layout of the input array has to be [, , ]. - /// - public void GetData(T[,,] destination) - { - Guard.IsEqualTo(destination.GetLength(0), Depth, nameof(destination)); - Guard.IsEqualTo(destination.GetLength(1), Height, nameof(destination)); - Guard.IsEqualTo(destination.GetLength(2), Width, nameof(destination)); - - fixed (T* p = destination) - { - GetData(new Span(p, destination.Length)); - } - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target array. - /// - /// The input array to write data to. - /// The starting offset within to write data to. - public void GetData(T[] destination, int offset) - { - GetData(destination.AsSpan(offset), 0, 0, 0, Width, Height, Depth); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target array. - /// - /// The input array to write data to. - /// The starting offset within to write data to. - /// The horizontal range of items to copy. - /// The vertical range of items to copy. - /// The depthwise range of items to copy. - public void GetData(T[] destination, int offset, Range x, Range y, Range z) - { - GetData(destination.AsSpan(offset), x, y, z); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target array. - /// - /// The input array to write data to. - /// The starting offset within to write data to. - /// 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 copy. - /// The height of the memory area to copy. - /// The depth of the memory area to copy. - public void GetData(T[] destination, int offset, int x, int y, int z, int width, int height, int depth) - { - GetData(destination.AsSpan(offset), x, y, z, width, height, depth); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target . - /// - /// The input to write data to. - public void GetData(Span destination) - { - GetData(destination, 0, 0, 0, Width, Height, Depth); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target . - /// - /// The input to write data to. - /// The horizontal range in the source texture. - /// The vertical range in the source texture. - /// The depthwise range in the source texture. - public void GetData(Span destination, Range x, Range y, Range z) - { - var (offsetX, width) = x.GetOffsetAndLength(Width); - var (offsetY, height) = y.GetOffsetAndLength(Height); - var (offsetZ, depth) = z.GetOffsetAndLength(Depth); - - GetData(destination, offsetX, offsetY, offsetZ, width, height, depth); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target . - /// - /// The input to write data to. - /// 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 copy. - /// The height of the memory area to copy. - /// The depth of the memory area to copy. - public void GetData(Span destination, int x, int y, int z, int width, int height, int depth) - { - GetData(ref MemoryMarshal.GetReference(destination), destination.Length, x, y, z, width, height, depth); - } - /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -299,132 +178,6 @@ namespace ComputeSharp.Graphics.Buffers.Abstract } } - /// - /// Writes the contents of a given array to the current instance. - /// - /// The input array to read data from. - /// - /// The source 3D array needs to have each 2D plane stacked on the depth axis. That is, the expected - /// layout of the input array has to be [, , ]. - /// - public void SetData(T[,,] source) - { - Guard.IsEqualTo(source.GetLength(0), Depth, nameof(source)); - Guard.IsEqualTo(source.GetLength(1), Height, nameof(source)); - Guard.IsEqualTo(source.GetLength(2), Width, nameof(source)); - - fixed (T* p = source) - { - SetData(new Span(p, source.Length)); - } - } - - /// - /// Writes the contents of a given array to the current instance. - /// - /// The input array to read data from. - public void SetData(T[] source) - { - SetData(source.AsSpan(), 0, 0, 0, Width, Height, Depth); - } - - /// - /// Writes the contents of a given array to a specified area of the current instance. - /// - /// The input array to read data from. - /// The horizontal range of items to write. - /// The vertical range of items to write. - /// The depthwise range of items to write. - public void SetData(T[] source, Range x, Range y, Range z) - { - SetData(source.AsSpan(), x, y, z); - } - - /// - /// Writes the contents of a given array to a specified area of the current instance. - /// - /// The input array to read data from. - /// 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. - public void SetData(T[] source, int x, int y, int z, int width, int height, int depth) - { - SetData(source.AsSpan(), x, y, z, width, height, depth); - } - - /// - /// Writes the contents of a given array to a specified area of the current instance. - /// - /// The input array to read data from. - /// The starting offset within to read data from. - /// The horizontal range of items to write. - /// The vertical range of items to write. - /// The depthwise range of items to write. - public void SetData(T[] source, int offset, Range x, Range y, Range z) - { - SetData(source.AsSpan(offset), x, y, z); - } - - /// - /// Writes the contents of a given array to a specified area of the current instance. - /// - /// The input array to read data from. - /// The starting offset within to read data from. - /// 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. - public void SetData(T[] source, int offset, int x, int y, int z, int width, int height, int depth) - { - SetData(source.AsSpan(offset), x, y, z, width, height, depth); - } - - /// - /// Writes the contents of a given to the current instance. - /// The input data will be written to the start of the texture, and all input items will be copied. - /// - /// The input to read data from. - public void SetData(ReadOnlySpan source) - { - SetData(source, 0, 0, 0, Width, Height, Depth); - } - - /// - /// Writes the contents of a given to a specified area of the current instance. - /// - /// The input to read data from. - /// The horizontal range of items to write. - /// The vertical range of items to write. - /// The depthwise range of items to write. - public void SetData(ReadOnlySpan source, Range x, Range y, Range z) - { - var (offsetX, width) = x.GetOffsetAndLength(Width); - var (offsetY, height) = y.GetOffsetAndLength(Height); - var (offsetZ, depth) = z.GetOffsetAndLength(Depth); - - SetData(source, offsetX, offsetY, offsetZ, width, height, depth); - } - - /// - /// Writes the contents of a given to a specified area of the current instance. - /// - /// The input 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 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. - public void SetData(ReadOnlySpan source, int x, int y, int z, int width, int height, int depth) - { - SetData(ref MemoryMarshal.GetReference(source), source.Length, x, y, z, width, height, depth); - } - /// /// Writes the contents of a given memory area to a specified area of the current instance. /// diff --git a/src/ComputeSharp/Extensions/Texture3DExtensions.cs b/src/ComputeSharp/Extensions/Texture3DExtensions.cs new file mode 100644 index 00000000..850378a5 --- /dev/null +++ b/src/ComputeSharp/Extensions/Texture3DExtensions.cs @@ -0,0 +1,303 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.InteropServices; +using ComputeSharp.Graphics.Buffers.Abstract; +using Microsoft.Toolkit.Diagnostics; + +namespace ComputeSharp +{ + /// + /// A that contains extension methods for the type. + /// + public static class Texture3DExtensions + { + /// + /// Reads the contents of the current instance and returns an array. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// A array with the contents of the current buffer. + /// + /// The returned array will be using the same memory layout as the texture, that is, each 2D plane + /// in the 3D volume represented by the texture is contiguous in memory, and planes are stacked in + /// the depth dimension. This means that the resulting 3D array will have a size of [D, H, W]. + /// + [Pure] + public static T[,,] GetData(this Texture3D texture) + where T : unmanaged + { + T[,,] data = new T[texture.Depth, texture.Height, texture.Width]; + + texture.GetData(data); + + return data; + } + + /// + /// Reads the contents of the current instance and writes them into a target array. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input array to write data to. + /// + /// The input 3D array needs to have each 2D plane stacked on the depth axis. That is, the expected + /// layout of the input array has to be [, , ]. + /// + public static void GetData(this Texture3D texture, T[,,] destination) + where T : unmanaged + { + Guard.IsEqualTo(destination.GetLength(0), texture.Depth, nameof(destination)); + Guard.IsEqualTo(destination.GetLength(1), texture.Height, nameof(destination)); + Guard.IsEqualTo(destination.GetLength(2), texture.Width, nameof(destination)); + + texture.GetData(ref destination[0, 0, 0], destination.Length, 0, 0, 0, texture.Width, texture.Height, texture.Depth); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target array. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input array to write data to. + /// The starting offset within to write data to. + public static void GetData(this Texture3D texture, T[] destination, int offset) + where T : unmanaged + { + texture.GetData(destination.AsSpan(offset), 0, 0, 0, texture.Width, texture.Height, texture.Depth); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target array. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input array to write data to. + /// The starting offset within to write data to. + /// The horizontal range of items to copy. + /// The vertical range of items to copy. + /// The depthwise range of items to copy. + public static void GetData(this Texture3D texture, T[] destination, int offset, Range x, Range y, Range z) + where T : unmanaged + { + texture.GetData(destination.AsSpan(offset), x, y, z); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target array. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input array to write data to. + /// The starting offset within to write data to. + /// 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 copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public static void GetData(this Texture3D texture, T[] destination, int offset, int x, int y, int z, int width, int height, int depth) + where T : unmanaged + { + texture.GetData(destination.AsSpan(offset), x, y, z, width, height, depth); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target . + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input to write data to. + public static void GetData(this Texture3D texture, Span destination) + where T : unmanaged + { + texture.GetData(destination, 0, 0, 0, texture.Width, texture.Height, texture.Depth); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target . + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input to write data to. + /// The horizontal range in the source texture. + /// The vertical range in the source texture. + /// The depthwise range in the source texture. + public static void GetData(this Texture3D texture, Span destination, Range x, Range y, Range z) + where T : unmanaged + { + var (offsetX, width) = x.GetOffsetAndLength(texture.Width); + var (offsetY, height) = y.GetOffsetAndLength(texture.Height); + var (offsetZ, depth) = z.GetOffsetAndLength(texture.Depth); + + texture.GetData(destination, offsetX, offsetY, offsetZ, width, height, depth); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target . + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input to write data to. + /// 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 copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public static void GetData(this Texture3D texture, Span destination, int x, int y, int z, int width, int height, int depth) + where T : unmanaged + { + texture.GetData(ref MemoryMarshal.GetReference(destination), destination.Length, x, y, z, width, height, depth); + } + + /// + /// Writes the contents of a given array to the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input array to read data from. + /// + /// The source 3D array needs to have each 2D plane stacked on the depth axis. That is, the expected + /// layout of the input array has to be [, , ]. + /// + public static void SetData(this Texture3D texture, T[,,] source) + where T : unmanaged + { + Guard.IsEqualTo(source.GetLength(0), texture.Depth, nameof(source)); + Guard.IsEqualTo(source.GetLength(1), texture.Height, nameof(source)); + Guard.IsEqualTo(source.GetLength(2), texture.Width, nameof(source)); + + texture.SetData(ref source[0, 0, 0], source.Length, 0, 0, 0, texture.Width, texture.Height, texture.Depth); + } + + /// + /// Writes the contents of a given array to the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input array to read data from. + public static void SetData(this Texture3D texture, T[] source) + where T : unmanaged + { + texture.SetData(source.AsSpan(), 0, 0, 0, texture.Width, texture.Height, texture.Depth); + } + + /// + /// Writes the contents of a given array to a specified area of the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input array to read data from. + /// The horizontal range of items to write. + /// The vertical range of items to write. + /// The depthwise range of items to write. + public static void SetData(this Texture3D texture, T[] source, Range x, Range y, Range z) + where T : unmanaged + { + texture.SetData(source.AsSpan(), x, y, z); + } + + /// + /// Writes the contents of a given array to a specified area of the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input array to read data from. + /// 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. + public static void SetData(this Texture3D texture, T[] source, int x, int y, int z, int width, int height, int depth) + where T : unmanaged + { + texture.SetData(source.AsSpan(), x, y, z, width, height, depth); + } + + /// + /// Writes the contents of a given array to a specified area of the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input array to read data from. + /// The starting offset within to read data from. + /// The horizontal range of items to write. + /// The vertical range of items to write. + /// The depthwise range of items to write. + public static void SetData(this Texture3D texture, T[] source, int offset, Range x, Range y, Range z) + where T : unmanaged + { + texture.SetData(source.AsSpan(offset), x, y, z); + } + + /// + /// Writes the contents of a given array to a specified area of the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input array to read data from. + /// The starting offset within to read data from. + /// 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. + public static void SetData(this Texture3D texture, T[] source, int offset, int x, int y, int z, int width, int height, int depth) + where T : unmanaged + { + texture.SetData(source.AsSpan(offset), x, y, z, width, height, depth); + } + + /// + /// Writes the contents of a given to the current instance. + /// The input data will be written to the start of the texture, and all input items will be copied. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input to read data from. + public static void SetData(this Texture3D texture, ReadOnlySpan source) + where T : unmanaged + { + texture.SetData(source, 0, 0, 0, texture.Width, texture.Height, texture.Depth); + } + + /// + /// Writes the contents of a given to a specified area of the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input to read data from. + /// The horizontal range of items to write. + /// The vertical range of items to write. + /// The depthwise range of items to write. + public static void SetData(this Texture3D texture, ReadOnlySpan source, Range x, Range y, Range z) + where T : unmanaged + { + var (offsetX, width) = x.GetOffsetAndLength(texture.Width); + var (offsetY, height) = y.GetOffsetAndLength(texture.Height); + var (offsetZ, depth) = z.GetOffsetAndLength(texture.Depth); + + texture.SetData(source, offsetX, offsetY, offsetZ, width, height, depth); + } + + /// + /// Writes the contents of a given to a specified area of the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input 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 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. + public static void SetData(this Texture3D texture, ReadOnlySpan source, int x, int y, int z, int width, int height, int depth) + where T : unmanaged + { + texture.SetData(ref MemoryMarshal.GetReference(source), source.Length, x, y, z, width, height, depth); + } + } +} From d557d7264e284e1e23d5c4e0ebd2797ca3d8cf6f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 6 Dec 2020 22:58:03 +0100 Subject: [PATCH 23/36] Added BufferDebugView3D type, minor bug fixes --- .../Buffers/ReadOnlyTexture3D{T}.cs | 2 ++ .../Buffers/ReadWriteTexture3D{T}.cs | 4 ++- .../Buffers/Views/BufferDebugView2D{T}.cs | 2 +- .../Buffers/Views/BufferDebugView3D{T}.cs | 35 +++++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView3D{T}.cs diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs index 560ad224..11ee7869 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs @@ -3,6 +3,7 @@ using ComputeSharp.Exceptions; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Buffers.Enums; +using ComputeSharp.Graphics.Buffers.Views; namespace ComputeSharp { @@ -10,6 +11,7 @@ namespace ComputeSharp /// A representing a typed readonly 3D texture stored on GPU memory. /// /// The type of items stored on the texture. + [DebuggerTypeProxy(typeof(BufferDebugView3D<>))] [DebuggerDisplay("{ToString(),raw}")] public sealed class ReadOnlyTexture3D : Texture3D where T : unmanaged diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs index 9e5e950d..b4a341d3 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs @@ -3,6 +3,7 @@ using ComputeSharp.Exceptions; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Buffers.Enums; +using ComputeSharp.Graphics.Buffers.Views; namespace ComputeSharp { @@ -10,6 +11,7 @@ namespace ComputeSharp /// A representing a typed readonly 3D texture stored on GPU memory. /// /// The type of items stored on the texture. + [DebuggerTypeProxy(typeof(BufferDebugView3D<>))] [DebuggerDisplay("{ToString(),raw}")] public sealed class ReadWriteTexture3D : Texture3D where T : unmanaged @@ -36,7 +38,7 @@ namespace ComputeSharp /// public override string ToString() { - return $"ComputeSharp.ReadOnlyTexture3D<{typeof(T)}>[{Width}, {Height}, {Depth}]"; + return $"ComputeSharp.ReadWriteTexture3D<{typeof(T)}>[{Width}, {Height}, {Depth}]"; } } } diff --git a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView2D{T}.cs index 720d46df..6acf3c20 100644 --- a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView2D{T}.cs @@ -20,7 +20,7 @@ namespace ComputeSharp.Graphics.Buffers.Views { var items = new T[texture.Height, texture.Width]; - texture.GetData(ref items[0, 0], items.Length, 0, 0, texture.Height, texture.Width); + texture.GetData(ref items[0, 0], items.Length, 0, 0, texture.Width, texture.Height); Items = items; } diff --git a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView3D{T}.cs new file mode 100644 index 00000000..24e2a4a5 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView3D{T}.cs @@ -0,0 +1,35 @@ +using System.Diagnostics; +using ComputeSharp.Graphics.Buffers.Abstract; + +namespace ComputeSharp.Graphics.Buffers.Views +{ + /// + /// A debug proxy used to display items in a instance. + /// + /// The type of items to display. + internal sealed class BufferDebugView3D + where T : unmanaged + { + /// + /// Initializes a new instance of the class with the specified parameters. + /// + /// The input instance with the items to display. + public BufferDebugView3D(Texture3D? texture) + { + if (texture is not null) + { + var items = new T[texture.Depth, texture.Height, texture.Width]; + + texture.GetData(ref items[0, 0, 0], items.Length, 0, 0, 0, texture.Width, texture.Height, texture.Depth); + + Items = items; + } + } + + /// + /// Gets the items to display for the current instance. + /// + [DebuggerBrowsable(DebuggerBrowsableState.Collapsed)] + public T[,,]? Items { get; } + } +} From 3d9e1899c75de8578f818e60d8585bd744a54a10 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 6 Dec 2020 22:59:58 +0100 Subject: [PATCH 24/36] Renamed resource debug view types --- src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{T}.cs | 2 +- src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs | 2 +- src/ComputeSharp.Graphics/Buffers/ReadWriteTexture2D{T}.cs | 2 +- src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs | 2 +- .../{BufferDebugView2D{T}.cs => Texture2DDebugView{T}.cs} | 6 +++--- .../{BufferDebugView3D{T}.cs => Texture3DDebugView{T}.cs} | 6 +++--- 6 files changed, 10 insertions(+), 10 deletions(-) rename src/ComputeSharp.Graphics/Buffers/Views/{BufferDebugView2D{T}.cs => Texture2DDebugView{T}.cs} (81%) rename src/ComputeSharp.Graphics/Buffers/Views/{BufferDebugView3D{T}.cs => Texture3DDebugView{T}.cs} (82%) diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{T}.cs index 2c7c9130..6928d322 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{T}.cs @@ -11,7 +11,7 @@ namespace ComputeSharp /// A representing a typed readonly 2D texture stored on GPU memory. /// /// The type of items stored on the texture. - [DebuggerTypeProxy(typeof(BufferDebugView2D<>))] + [DebuggerTypeProxy(typeof(Texture2DDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] public sealed class ReadOnlyTexture2D : Texture2D where T : unmanaged diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs index 11ee7869..2d17d352 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs @@ -11,7 +11,7 @@ namespace ComputeSharp /// A representing a typed readonly 3D texture stored on GPU memory. /// /// The type of items stored on the texture. - [DebuggerTypeProxy(typeof(BufferDebugView3D<>))] + [DebuggerTypeProxy(typeof(Texture3DDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] public sealed class ReadOnlyTexture3D : Texture3D where T : unmanaged diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture2D{T}.cs index 779bba67..9ff4aae9 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture2D{T}.cs @@ -11,7 +11,7 @@ namespace ComputeSharp /// A representing a typed read write 2D texture stored on GPU memory. /// /// The type of items stored on the texture. - [DebuggerTypeProxy(typeof(BufferDebugView2D<>))] + [DebuggerTypeProxy(typeof(Texture2DDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] public sealed class ReadWriteTexture2D : Texture2D where T : unmanaged diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs index b4a341d3..bb335285 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs @@ -11,7 +11,7 @@ namespace ComputeSharp /// A representing a typed readonly 3D texture stored on GPU memory. /// /// The type of items stored on the texture. - [DebuggerTypeProxy(typeof(BufferDebugView3D<>))] + [DebuggerTypeProxy(typeof(Texture3DDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] public sealed class ReadWriteTexture3D : Texture3D where T : unmanaged diff --git a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Views/Texture2DDebugView{T}.cs similarity index 81% rename from src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView2D{T}.cs rename to src/ComputeSharp.Graphics/Buffers/Views/Texture2DDebugView{T}.cs index 6acf3c20..def44462 100644 --- a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Views/Texture2DDebugView{T}.cs @@ -7,14 +7,14 @@ namespace ComputeSharp.Graphics.Buffers.Views /// A debug proxy used to display items in a instance. /// /// The type of items to display. - internal sealed class BufferDebugView2D + internal sealed class Texture2DDebugView where T : unmanaged { /// - /// Initializes a new instance of the class with the specified parameters. + /// Initializes a new instance of the class with the specified parameters. /// /// The input instance with the items to display. - public BufferDebugView2D(Texture2D? texture) + public Texture2DDebugView(Texture2D? texture) { if (texture is not null) { diff --git a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Views/Texture3DDebugView{T}.cs similarity index 82% rename from src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView3D{T}.cs rename to src/ComputeSharp.Graphics/Buffers/Views/Texture3DDebugView{T}.cs index 24e2a4a5..d4e43269 100644 --- a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Views/Texture3DDebugView{T}.cs @@ -7,14 +7,14 @@ namespace ComputeSharp.Graphics.Buffers.Views /// A debug proxy used to display items in a instance. /// /// The type of items to display. - internal sealed class BufferDebugView3D + internal sealed class Texture3DDebugView where T : unmanaged { /// - /// Initializes a new instance of the class with the specified parameters. + /// Initializes a new instance of the class with the specified parameters. /// /// The input instance with the items to display. - public BufferDebugView3D(Texture3D? texture) + public Texture3DDebugView(Texture3D? texture) { if (texture is not null) { From 8240052dd39b4f610e017090b0f43f2295a18b38 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 6 Dec 2020 23:01:18 +0100 Subject: [PATCH 25/36] Moved public resource extension types --- src/ComputeSharp/Extensions/{ => Buffers}/BufferExtensions.cs | 0 src/ComputeSharp/Extensions/{ => Buffers}/Texture2DExtensions.cs | 0 src/ComputeSharp/Extensions/{ => Buffers}/Texture3DExtensions.cs | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename src/ComputeSharp/Extensions/{ => Buffers}/BufferExtensions.cs (100%) rename src/ComputeSharp/Extensions/{ => Buffers}/Texture2DExtensions.cs (100%) rename src/ComputeSharp/Extensions/{ => Buffers}/Texture3DExtensions.cs (100%) diff --git a/src/ComputeSharp/Extensions/BufferExtensions.cs b/src/ComputeSharp/Extensions/Buffers/BufferExtensions.cs similarity index 100% rename from src/ComputeSharp/Extensions/BufferExtensions.cs rename to src/ComputeSharp/Extensions/Buffers/BufferExtensions.cs diff --git a/src/ComputeSharp/Extensions/Texture2DExtensions.cs b/src/ComputeSharp/Extensions/Buffers/Texture2DExtensions.cs similarity index 100% rename from src/ComputeSharp/Extensions/Texture2DExtensions.cs rename to src/ComputeSharp/Extensions/Buffers/Texture2DExtensions.cs diff --git a/src/ComputeSharp/Extensions/Texture3DExtensions.cs b/src/ComputeSharp/Extensions/Buffers/Texture3DExtensions.cs similarity index 100% rename from src/ComputeSharp/Extensions/Texture3DExtensions.cs rename to src/ComputeSharp/Extensions/Buffers/Texture3DExtensions.cs From 5efee8ee2480734e58505beb2ffc208feff78214 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 6 Dec 2020 23:03:04 +0100 Subject: [PATCH 26/36] Fixed visibility of some resource APIs --- .../Buffers/Abstract/StructuredBuffer{T}.cs | 3 +-- src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs | 2 +- src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs index b2fcec46..86528ee7 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs @@ -1,5 +1,4 @@ -using System; -using ComputeSharp.Graphics.Buffers.Enums; +using ComputeSharp.Graphics.Buffers.Enums; using ComputeSharp.Graphics.Buffers.Interop; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index 780e81de..a3c9cee6 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -171,7 +171,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// 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. - public void SetData(ref T source, nint size, int x, int y, int width, int height) + internal void SetData(ref T source, nint size, int x, int y, int width, int height) { GraphicsDevice.ThrowIfDisposed(); diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index c86d5618..adcb4106 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -116,7 +116,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The width of the memory area to copy. /// The height of the memory area to copy. /// The depth of the memory area to copy. - public void GetData(ref T destination, nint size, int x, int y, int z, int width, int height, int depth) + internal void GetData(ref T destination, nint size, int x, int y, int z, int width, int height, int depth) { GraphicsDevice.ThrowIfDisposed(); @@ -189,7 +189,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// 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. - public void SetData(ref T source, nint size, int x, int y, int z, int width, int height, int depth) + internal void SetData(ref T source, nint size, int x, int y, int z, int width, int height, int depth) { GraphicsDevice.ThrowIfDisposed(); From abfe4d815c69acf8684c48ff5310dde8fdc382b0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 7 Dec 2020 02:37:00 +0100 Subject: [PATCH 27/36] Fixed ConstantBuffer loading in shaders --- src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs index bffcb65a..d4719ae8 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -65,6 +65,7 @@ namespace ComputeSharp.SourceGenerators.Mappings { switch (typeName) { + case "ComputeSharp.ConstantBuffer`1": case "ComputeSharp.ReadOnlyBuffer`1": case "ComputeSharp.ReadWriteBuffer`1": case "ComputeSharp.ReadOnlyTexture2D`1": @@ -104,6 +105,7 @@ namespace ComputeSharp.SourceGenerators.Mappings // Construct the HLSL type name return typeName switch { + "ComputeSharp.ConstantBuffer`1" => mapped, "ComputeSharp.ReadOnlyBuffer`1" => $"StructuredBuffer<{mapped}>", "ComputeSharp.ReadWriteBuffer`1" => $"RWStructuredBuffer<{mapped}>", "ComputeSharp.ReadOnlyTexture2D`1" => $"Texture2D<{mapped}>", From b229b327b96a6910a5e8baf75eb8c93795c56dde Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 7 Dec 2020 02:50:37 +0100 Subject: [PATCH 28/36] Fixed ReadWriteTexture3D indexer --- src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs index bb335285..414c4854 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs @@ -33,7 +33,7 @@ namespace ComputeSharp /// /// The coordinates of the value to get. /// This API can only be used from a compute shader, and will always throw if used anywhere else. - public T this[UInt3 xyz] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture3D)}[UInt3]"); + public ref T this[UInt3 xyz] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture3D)}[UInt3]"); /// public override string ToString() From 5fafd3d9acfc0f471a9476e9f6b724eb4c265f94 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 7 Dec 2020 02:50:53 +0100 Subject: [PATCH 29/36] Added dispatch unit tests to resource types --- tests/ComputeSharp.Tests/BufferTests.cs | 83 +++++++++++++++++++++- tests/ComputeSharp.Tests/Texture2DTests.cs | 58 ++++++++++++++- tests/ComputeSharp.Tests/Texture3DTests.cs | 58 ++++++++++++++- 3 files changed, 196 insertions(+), 3 deletions(-) diff --git a/tests/ComputeSharp.Tests/BufferTests.cs b/tests/ComputeSharp.Tests/BufferTests.cs index 280c5c24..6731b5fa 100644 --- a/tests/ComputeSharp.Tests/BufferTests.cs +++ b/tests/ComputeSharp.Tests/BufferTests.cs @@ -8,7 +8,7 @@ namespace ComputeSharp.Tests { [TestClass] [TestCategory("Buffer")] - public class BufferTests + public partial class BufferTests { [TestMethod] [DataRow(typeof(ConstantBuffer<>))] @@ -201,5 +201,86 @@ namespace ComputeSharp.Tests buffer.GetData(result, destinationOffset, bufferOffset, count); } + + [TestMethod] + public void Dispatch_ConstantBuffer() + { + int[] data = Enumerable.Range(0, 1024).ToArray(); + + using ConstantBuffer source = Gpu.Default.AllocateConstantBuffer(data); + using ReadWriteBuffer destination = Gpu.Default.AllocateReadWriteBuffer(data.Length); + + Gpu.Default.For(source.Length, new ConstantBufferKernel(source, destination)); + + int[] result = destination.GetData(); + + CollectionAssert.AreEqual(data, result); + } + + [AutoConstructor] + private readonly partial struct ConstantBufferKernel : IComputeShader + { + public readonly ConstantBuffer source; + public readonly ReadWriteBuffer destination; + + public void Execute(ThreadIds ids) + { + destination[ids.X] = source[ids.X]; + } + } + + [TestMethod] + public void Dispatch_ReadOnlyBuffer() + { + int[] data = Enumerable.Range(0, 1024).ToArray(); + + using ReadOnlyBuffer source = Gpu.Default.AllocateReadOnlyBuffer(data); + using ReadWriteBuffer destination = Gpu.Default.AllocateReadWriteBuffer(data.Length); + + Gpu.Default.For(source.Length, new ReadOnlyBufferKernel(source, destination)); + + int[] result = destination.GetData(); + + CollectionAssert.AreEqual(data, result); + } + + [AutoConstructor] + private readonly partial struct ReadOnlyBufferKernel : IComputeShader + { + public readonly ReadOnlyBuffer source; + public readonly ReadWriteBuffer destination; + + public void Execute(ThreadIds ids) + { + destination[ids.X] = source[ids.X]; + } + } + + [TestMethod] + public void Dispatch_ReadWriteBuffer() + { + int[] data = Enumerable.Range(0, 1024).ToArray(); + + using ReadWriteBuffer source = Gpu.Default.AllocateReadWriteBuffer(data); + using ReadWriteBuffer destination = Gpu.Default.AllocateReadWriteBuffer(data.Length); + + Gpu.Default.For(source.Length, new ReadWriteBufferKernel(source, destination)); + + int[] result = destination.GetData(); + + CollectionAssert.AreEqual(data, result); + } + + [AutoConstructor] + private readonly partial struct ReadWriteBufferKernel : IComputeShader + { + public readonly ReadWriteBuffer source; + public readonly ReadWriteBuffer destination; + + public void Execute(ThreadIds ids) + { + destination[ids.X] = source[ids.X]; + } + } } } diff --git a/tests/ComputeSharp.Tests/Texture2DTests.cs b/tests/ComputeSharp.Tests/Texture2DTests.cs index 08a79c1c..73e37112 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.cs @@ -9,7 +9,7 @@ namespace ComputeSharp.Tests { [TestClass] [TestCategory("Texture2D")] - public class Texture2DTests + public partial class Texture2DTests { [TestMethod] [DataRow(typeof(ReadOnlyTexture2D<>))] @@ -137,5 +137,61 @@ namespace ComputeSharp.Tests texture.GetData(result, x, y, width, height); } + + [TestMethod] + public void Dispatch_ReadOnlyTexture2D() + { + int[] data = Enumerable.Range(0, 32 * 32).ToArray(); + + using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(data, 32, 32); + using ReadWriteBuffer destination = Gpu.Default.AllocateReadWriteBuffer(data.Length); + + Gpu.Default.For(source.Width, source.Height, new ReadOnlyTexture2DKernel(source, destination)); + + int[] result = destination.GetData(); + + CollectionAssert.AreEqual(data, result); + } + + [AutoConstructor] + private readonly partial struct ReadOnlyTexture2DKernel : IComputeShader + { + public readonly ReadOnlyTexture2D source; + public readonly ReadWriteBuffer destination; + + public void Execute(ThreadIds ids) + { + destination[ids.Y * 32 + ids.X] = source[new((uint)ids.X, (uint)ids.Y)]; + } + } + + [TestMethod] + public void Dispatch_ReadWriteTexture2D() + { + int[] data = Enumerable.Range(0, 32 * 32).ToArray(); + + using ReadWriteTexture2D source = Gpu.Default.AllocateReadWriteTexture2D(data, 32, 32); + using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(32, 32); + + Gpu.Default.For(source.Width, source.Height, new ReadWriteTexture2DKernel(source, destination)); + + int[] result = new int[data.Length]; + + destination.GetData(result); + + CollectionAssert.AreEqual(data, result); + } + + [AutoConstructor] + private readonly partial struct ReadWriteTexture2DKernel : IComputeShader + { + public readonly ReadWriteTexture2D source; + public readonly ReadWriteTexture2D destination; + + public void Execute(ThreadIds ids) + { + destination[new((uint)ids.X, (uint)ids.Y)] = source[new((uint)ids.X, (uint)ids.Y)]; + } + } } } diff --git a/tests/ComputeSharp.Tests/Texture3DTests.cs b/tests/ComputeSharp.Tests/Texture3DTests.cs index a6764388..43145dd4 100644 --- a/tests/ComputeSharp.Tests/Texture3DTests.cs +++ b/tests/ComputeSharp.Tests/Texture3DTests.cs @@ -10,7 +10,7 @@ namespace ComputeSharp.Tests { [TestClass] [TestCategory("Texture3D")] - public class Texture3DTests + public partial class Texture3DTests { [TestMethod] [DataRow(typeof(ReadOnlyTexture3D<>))] @@ -171,5 +171,61 @@ namespace ComputeSharp.Tests texture.GetData(result, x, y, z, width, height, depth); } + + [TestMethod] + public void Dispatch_ReadOnlyTexture3D() + { + int[] data = Enumerable.Range(0, 32 * 32 * 3).ToArray(); + + using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(data, 32, 32, 3); + using ReadWriteBuffer destination = Gpu.Default.AllocateReadWriteBuffer(data.Length); + + Gpu.Default.For(source.Width, source.Height, source.Depth, new ReadOnlyTexture3DKernel(source, destination)); + + int[] result = destination.GetData(); + + CollectionAssert.AreEqual(data, result); + } + + [AutoConstructor] + private readonly partial struct ReadOnlyTexture3DKernel : IComputeShader + { + public readonly ReadOnlyTexture3D source; + public readonly ReadWriteBuffer destination; + + public void Execute(ThreadIds ids) + { + destination[ids.Z * 32 * 32 + ids.Y * 32 + ids.X] = source[new((uint)ids.X, (uint)ids.Y, (uint)ids.Z)]; + } + } + + [TestMethod] + public void Dispatch_ReadWriteTexture2D() + { + int[] data = Enumerable.Range(0, 32 * 32 * 3).ToArray(); + + using ReadWriteTexture3D source = Gpu.Default.AllocateReadWriteTexture3D(data, 32, 32, 3); + using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(32, 32, 3); + + Gpu.Default.For(source.Width, source.Height, source.Depth, new ReadWriteTexture3DKernel(source, destination)); + + int[] result = new int[data.Length]; + + destination.GetData(result); + + CollectionAssert.AreEqual(data, result); + } + + [AutoConstructor] + private readonly partial struct ReadWriteTexture3DKernel : IComputeShader + { + public readonly ReadWriteTexture3D source; + public readonly ReadWriteTexture3D destination; + + public void Execute(ThreadIds ids) + { + destination[new((uint)ids.X, (uint)ids.Y, (uint)ids.Z)] = source[new((uint)ids.X, (uint)ids.Y, (uint)ids.Z)]; + } + } } } From 03bbbe7724ad2f3c16cb641dd631570fb17094db Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 7 Dec 2020 03:12:28 +0100 Subject: [PATCH 30/36] Enabled copy command list for readonly textures --- .../Buffers/Abstract/Texture2D{T}.cs | 39 +++++++++++++++---- .../Buffers/Abstract/Texture3D{T}.cs | 39 +++++++++++++++---- .../Commands/CommandList.cs | 2 +- .../Extensions/ID3D12DeviceExtensions.cs | 4 +- src/ComputeSharp.Graphics/GraphicsDevice.cs | 2 +- 5 files changed, 66 insertions(+), 20 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index a3c9cee6..dfd28d3f 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -36,7 +36,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// /// The default value for the current resource. /// - private readonly D3D12_RESOURCE_STATES D3D12ResourceState; + private readonly D3D12_RESOURCE_STATES d3D12ResourceState; + + /// + /// The value to use for copy operations. + /// + private readonly D3D12_COMMAND_LIST_TYPE d3D12CommandListType; /// /// Creates a new instance with the specified parameters. @@ -61,7 +66,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract DXGIFormatHelper.GetForType(), (uint)width, (uint)height, - out D3D12ResourceState); + out this.d3D12ResourceState); + + this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON + ? D3D12_COMMAND_LIST_TYPE_COPY + : D3D12_COMMAND_LIST_TYPE_COMPUTE; device.AllocateShaderResourceViewDescriptorHandles(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); @@ -130,9 +139,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, totalSizeInBytes); - using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE)) + using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) { - copyCommandList.ResourceBarrier(D3D12Resource, D3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); + } copyCommandList.CopyTextureRegion( d3D12Resource.Get(), @@ -145,7 +157,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract (uint)height, 1); - copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12ResourceState); + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState); + } + copyCommandList.ExecuteAndWaitForCompletion(); } @@ -207,9 +223,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract d3D12PlacedSubresourceFootprint.Footprint.RowPitch); } - using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE); + using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); - copyCommandList.ResourceBarrier(D3D12Resource, D3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); + } copyCommandList.CopyTextureRegion( D3D12Resource, @@ -219,7 +238,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract d3D12Resource.Get(), &d3D12PlacedSubresourceFootprint); - copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, D3D12ResourceState); + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState); + } + copyCommandList.ExecuteAndWaitForCompletion(); } diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index adcb4106..837d6f55 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -36,7 +36,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// /// The default value for the current resource. /// - private readonly D3D12_RESOURCE_STATES D3D12ResourceState; + private readonly D3D12_RESOURCE_STATES d3D12ResourceState; + + /// + /// The value to use for copy operations. + /// + private readonly D3D12_COMMAND_LIST_TYPE d3D12CommandListType; /// /// Creates a new instance with the specified parameters. @@ -65,7 +70,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract (uint)width, (uint)height, (ushort)depth, - out D3D12ResourceState); + out this.d3D12ResourceState); + + this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON + ? D3D12_COMMAND_LIST_TYPE_COPY + : D3D12_COMMAND_LIST_TYPE_COMPUTE; device.AllocateShaderResourceViewDescriptorHandles(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); @@ -144,9 +153,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, totalSizeInBytes); - using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE)) + using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) { - copyCommandList.ResourceBarrier(D3D12Resource, D3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); + } copyCommandList.CopyTextureRegion( d3D12Resource.Get(), @@ -159,7 +171,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract (uint)height, (ushort)depth); - copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12ResourceState); + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState); + } + copyCommandList.ExecuteAndWaitForCompletion(); } @@ -230,9 +246,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract d3D12PlacedSubresourceFootprint.Footprint.RowPitch * (uint)height); } - using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE); + using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); - copyCommandList.ResourceBarrier(D3D12Resource, D3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); + } copyCommandList.CopyTextureRegion( D3D12Resource, @@ -242,7 +261,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract d3D12Resource.Get(), &d3D12PlacedSubresourceFootprint); - copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, D3D12ResourceState); + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState); + } + copyCommandList.ExecuteAndWaitForCompletion(); } diff --git a/src/ComputeSharp.Graphics/Commands/CommandList.cs b/src/ComputeSharp.Graphics/Commands/CommandList.cs index 3be6492e..13bc89bc 100644 --- a/src/ComputeSharp.Graphics/Commands/CommandList.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandList.cs @@ -55,7 +55,7 @@ namespace ComputeSharp.Graphics.Commands /// /// Gets the command list type being used by the current instance. /// - public readonly D3D12_COMMAND_LIST_TYPE D3d12CommandListType => this.d3D12CommandListType; + public readonly D3D12_COMMAND_LIST_TYPE D3D12CommandListType => this.d3D12CommandListType; /// /// Detaches the object in use by the current instance. diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index c22ef9ac..3576f368 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -163,7 +163,7 @@ namespace ComputeSharp.Graphics.Extensions (d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch { - ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE), + 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)>() }; @@ -214,7 +214,7 @@ namespace ComputeSharp.Graphics.Extensions (d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch { - ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE), + 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)>() }; diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs index e49cee8f..cdfad124 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs @@ -168,7 +168,7 @@ namespace ComputeSharp.Graphics ulong d3D12FenceValue; // Get the target command queue, fence and pool for the list type - switch (commandList.D3d12CommandListType) + switch (commandList.D3D12CommandListType) { case D3D12_COMMAND_LIST_TYPE_COMPUTE: commandAllocatorPool = ref this.computeCommandAllocatorPool; From af9a39121458637b8e6cffeb82ec343a8b8cad90 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 7 Dec 2020 12:05:59 +0100 Subject: [PATCH 31/36] Switched texture indexers to signed int --- src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{T}.cs | 2 +- src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs | 2 +- src/ComputeSharp.Graphics/Buffers/ReadWriteTexture2D{T}.cs | 2 +- src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs | 2 +- tests/ComputeSharp.Tests/Texture2DTests.cs | 4 ++-- tests/ComputeSharp.Tests/Texture3DTests.cs | 4 ++-- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{T}.cs index 6928d322..c2d93771 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{T}.cs @@ -32,7 +32,7 @@ namespace ComputeSharp /// /// The coordinates of the value to get. /// This API can only be used from a compute shader, and will always throw if used anywhere else. - public T this[UInt2 xy] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture2D)}[UInt2]"); + public T this[Int2 xy] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture2D)}[Int2]"); /// public override string ToString() diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs index 2d17d352..26bf0c7d 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs @@ -33,7 +33,7 @@ namespace ComputeSharp /// /// The coordinates of the value to get. /// This API can only be used from a compute shader, and will always throw if used anywhere else. - public T this[UInt3 xyz] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture3D)}[UInt3]"); + public T this[Int3 xyz] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture3D)}[Int3]"); /// public override string ToString() diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture2D{T}.cs index 9ff4aae9..925be95d 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture2D{T}.cs @@ -32,7 +32,7 @@ namespace ComputeSharp /// /// The coordinates of the value to get. /// This API can only be used from a compute shader, and will always throw if used anywhere else. - public ref T this[UInt2 xy] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture2D)}[UInt2]"); + public ref T this[Int2 xy] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture2D)}[Int2]"); /// public override string ToString() diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs index 414c4854..ee042fa4 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs @@ -33,7 +33,7 @@ namespace ComputeSharp /// /// The coordinates of the value to get. /// This API can only be used from a compute shader, and will always throw if used anywhere else. - public ref T this[UInt3 xyz] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture3D)}[UInt3]"); + public ref T this[Int3 xyz] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture3D)}[Int3]"); /// public override string ToString() diff --git a/tests/ComputeSharp.Tests/Texture2DTests.cs b/tests/ComputeSharp.Tests/Texture2DTests.cs index 73e37112..c6496a8a 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.cs @@ -161,7 +161,7 @@ namespace ComputeSharp.Tests public void Execute(ThreadIds ids) { - destination[ids.Y * 32 + ids.X] = source[new((uint)ids.X, (uint)ids.Y)]; + destination[ids.Y * 32 + ids.X] = source[new(ids.X, ids.Y)]; } } @@ -190,7 +190,7 @@ namespace ComputeSharp.Tests public void Execute(ThreadIds ids) { - destination[new((uint)ids.X, (uint)ids.Y)] = source[new((uint)ids.X, (uint)ids.Y)]; + destination[new(ids.X, ids.Y)] = source[new(ids.X, ids.Y)]; } } } diff --git a/tests/ComputeSharp.Tests/Texture3DTests.cs b/tests/ComputeSharp.Tests/Texture3DTests.cs index 43145dd4..c2218a55 100644 --- a/tests/ComputeSharp.Tests/Texture3DTests.cs +++ b/tests/ComputeSharp.Tests/Texture3DTests.cs @@ -195,7 +195,7 @@ namespace ComputeSharp.Tests public void Execute(ThreadIds ids) { - destination[ids.Z * 32 * 32 + ids.Y * 32 + ids.X] = source[new((uint)ids.X, (uint)ids.Y, (uint)ids.Z)]; + destination[ids.Z * 32 * 32 + ids.Y * 32 + ids.X] = source[new(ids.X, ids.Y, ids.Z)]; } } @@ -224,7 +224,7 @@ namespace ComputeSharp.Tests public void Execute(ThreadIds ids) { - destination[new((uint)ids.X, (uint)ids.Y, (uint)ids.Z)] = source[new((uint)ids.X, (uint)ids.Y, (uint)ids.Z)]; + destination[new(ids.X, ids.Y, ids.Z)] = source[new(ids.X, ids.Y, ids.Z)]; } } } From 92817af9995d11a38ee0fae1ff4304989d886850 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 7 Dec 2020 12:32:37 +0100 Subject: [PATCH 32/36] Added vector indexers to ThreadIds type --- src/ComputeSharp.Core/Primitives/ThreadIds.cs | 182 +++++++++++++++++- .../Mappings/HlslKnownMembers.cs | 4 - .../Mappings/HlslKnownTypes.cs | 5 +- tests/ComputeSharp.Tests/Texture2DTests.cs | 4 +- tests/ComputeSharp.Tests/Texture3DTests.cs | 4 +- 5 files changed, 188 insertions(+), 11 deletions(-) diff --git a/src/ComputeSharp.Core/Primitives/ThreadIds.cs b/src/ComputeSharp.Core/Primitives/ThreadIds.cs index f5370ec8..69f58032 100644 --- a/src/ComputeSharp.Core/Primitives/ThreadIds.cs +++ b/src/ComputeSharp.Core/Primitives/ThreadIds.cs @@ -5,7 +5,7 @@ namespace ComputeSharp /// /// A that indicates the ids of a given GPU thread running a compute shader. /// - public readonly struct ThreadIds + public readonly ref struct ThreadIds { /// /// Gets the X id of the current thread. @@ -21,5 +21,185 @@ namespace ComputeSharp /// Gets the Z id of the current thread. /// public int Z => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(Z)}"); + + /// + /// Gets an value with the value for all components. + /// + public Int2 XX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XX)}"); + + /// + /// Gets or sets an value with the and values. + /// + public Int2 XY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XY)}"); + + /// + /// Gets or sets an value with the and values. + /// + public Int2 XZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XZ)}"); + + /// + /// Gets or sets an value with the and values. + /// + public Int2 YX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YX)}"); + + /// + /// Gets an value with the value for all components. + /// + public Int2 YY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YY)}"); + + /// + /// Gets or sets an value with the and values. + /// + public Int2 YZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YZ)}"); + + /// + /// Gets or sets an value with the and values. + /// + public Int2 ZX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZX)}"); + + /// + /// Gets or sets an value with the and values. + /// + public Int2 ZY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZY)}"); + + /// + /// Gets an value with the value for all components. + /// + public Int2 ZZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZZ)}"); + + /// + /// Gets an value with the value for all components. + /// + public Int3 XXX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XXX)}"); + + /// + /// Gets an value with the , and values. + /// + public Int3 XXY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XXY)}"); + + /// + /// Gets an value with the , and values. + /// + public Int3 XXZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XXZ)}"); + + /// + /// Gets an value with the , and values. + /// + public Int3 XYX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XYX)}"); + + /// + /// Gets an value with the , and values. + /// + public Int3 XYY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XYY)}"); + + /// + /// Gets or sets an value with the , and values. + /// + public Int3 XYZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XYZ)}"); + + /// + /// Gets an value with the , and values. + /// + public Int3 XZX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XZX)}"); + + /// + /// Gets or sets an value with the , and values. + /// + public Int3 XZY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XZY)}"); + + /// + /// Gets an value with the , and values. + /// + public Int3 XZZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XZZ)}"); + + /// + /// Gets an value with the , and values. + /// + public Int3 YXX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YXX)}"); + + /// + /// Gets an value with the , and values. + /// + public Int3 YXY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YXY)}"); + + /// + /// Gets or sets an value with the , and values. + /// + public Int3 YXZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YXZ)}"); + + /// + /// Gets an value with the , and values. + /// + public Int3 YYX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YYX)}"); + + /// + /// Gets an value with the value for all components. + /// + public Int3 YYY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YYY)}"); + + /// + /// Gets an value with the , and values. + /// + public Int3 YYZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YYZ)}"); + + /// + /// Gets or sets an value with the , and values. + /// + public Int3 YZX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YZX)}"); + + /// + /// Gets an value with the , and values. + /// + public Int3 YZY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YZY)}"); + + /// + /// Gets an value with the , and values. + /// + public Int3 YZZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YZZ)}"); + + /// + /// Gets an value with the , and values. + /// + public Int3 ZXX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZXX)}"); + + /// + /// Gets or sets an value with the , and values. + /// + public Int3 ZXY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZXY)}"); + + /// + /// Gets an value with the , and values. + /// + public Int3 ZXZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZXZ)}"); + + /// + /// Gets or sets an value with the , and values. + /// + public Int3 ZYX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZYX)}"); + + /// + /// Gets an value with the , and values. + /// + public Int3 ZYY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZYY)}"); + + /// + /// Gets an value with the , and values. + /// + public Int3 ZYZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZYZ)}"); + + /// + /// Gets an value with the , and values. + /// + public Int3 ZZX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZZX)}"); + + /// + /// Gets an value with the , and values. + /// + public Int3 ZZY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZZY)}"); + + /// + /// Gets an value with the value for all components. + /// + public Int3 ZZZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZZZ)}"); } } diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs index 874b4cfe..2ac0c8bf 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs @@ -25,10 +25,6 @@ namespace ComputeSharp.SourceGenerators.Mappings { Dictionary knownMembers = new() { - [$"{typeof(ThreadIds).FullName}.{nameof(ThreadIds.X)}"] = "x", - [$"{typeof(ThreadIds).FullName}.{nameof(ThreadIds.Y)}"] = "y", - [$"{typeof(ThreadIds).FullName}.{nameof(ThreadIds.Z)}"] = "z", - [$"{typeof(Vector2).FullName}.{nameof(Vector2.X)}"] = "x", [$"{typeof(Vector2).FullName}.{nameof(Vector2.Y)}"] = "y", [$"{typeof(Vector2).FullName}.{nameof(Vector2.Zero)}"] = "(float2)0", diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs index d4719ae8..2d2b0f32 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -47,14 +47,15 @@ namespace ComputeSharp.SourceGenerators.Mappings /// /// Gets the known HLSL vector types available as mapped types. /// - public static IReadOnlyCollection HlslMappedVectorTypes { get; } = new HashSet(new[] + public static IReadOnlyCollection HlslMappedVectorTypes { get; } = new[] { + typeof(ThreadIds), typeof(Bool2), typeof(Bool3), typeof(Bool4), typeof(Int2), typeof(Int3), typeof(Int4), typeof(UInt2), typeof(UInt3), typeof(UInt4), typeof(Float2), typeof(Float3), typeof(Float4), typeof(Double2), typeof(Double3), typeof(Double4) - }); + }; /// /// Checks whether or not a given type name matches a typed resource type. diff --git a/tests/ComputeSharp.Tests/Texture2DTests.cs b/tests/ComputeSharp.Tests/Texture2DTests.cs index c6496a8a..cf23220f 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.cs @@ -161,7 +161,7 @@ namespace ComputeSharp.Tests public void Execute(ThreadIds ids) { - destination[ids.Y * 32 + ids.X] = source[new(ids.X, ids.Y)]; + destination[ids.Y * 32 + ids.X] = source[ids.XY]; } } @@ -190,7 +190,7 @@ namespace ComputeSharp.Tests public void Execute(ThreadIds ids) { - destination[new(ids.X, ids.Y)] = source[new(ids.X, ids.Y)]; + destination[ids.XY] = source[ids.XY]; } } } diff --git a/tests/ComputeSharp.Tests/Texture3DTests.cs b/tests/ComputeSharp.Tests/Texture3DTests.cs index c2218a55..b68e02f6 100644 --- a/tests/ComputeSharp.Tests/Texture3DTests.cs +++ b/tests/ComputeSharp.Tests/Texture3DTests.cs @@ -195,7 +195,7 @@ namespace ComputeSharp.Tests public void Execute(ThreadIds ids) { - destination[ids.Z * 32 * 32 + ids.Y * 32 + ids.X] = source[new(ids.X, ids.Y, ids.Z)]; + destination[ids.Z * 32 * 32 + ids.Y * 32 + ids.X] = source[ids.XYZ]; } } @@ -224,7 +224,7 @@ namespace ComputeSharp.Tests public void Execute(ThreadIds ids) { - destination[new(ids.X, ids.Y, ids.Z)] = source[new(ids.X, ids.Y, ids.Z)]; + destination[ids.XYZ] = source[ids.XYZ]; } } } From 08a6f94453e4a4c9157d40c93b193b231f43dd1d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 7 Dec 2020 12:38:44 +0100 Subject: [PATCH 33/36] Fixed primitive types vector3 mappings, code tweaks --- src/ComputeSharp.Core/Primitives/Bool/Bool.cs | 24 +++- .../Primitives/Bool/Bool2.cs | 6 +- .../Primitives/Bool/Bool3.cs | 116 +++++++++--------- .../Primitives/Bool/Bool4.cs | 10 +- .../Primitives/Double/Double2.cs | 6 +- .../Primitives/Double/Double3.cs | 116 +++++++++--------- .../Primitives/Double/Double4.cs | 10 +- .../Primitives/Float/Float2.cs | 6 +- .../Primitives/Float/Float3.cs | 116 +++++++++--------- .../Primitives/Float/Float4.cs | 10 +- src/ComputeSharp.Core/Primitives/Int/Int2.cs | 6 +- src/ComputeSharp.Core/Primitives/Int/Int3.cs | 116 +++++++++--------- src/ComputeSharp.Core/Primitives/Int/Int4.cs | 10 +- .../Primitives/UInt/UInt2.cs | 6 +- .../Primitives/UInt/UInt3.cs | 116 +++++++++--------- .../Primitives/UInt/UInt4.cs | 10 +- 16 files changed, 348 insertions(+), 336 deletions(-) diff --git a/src/ComputeSharp.Core/Primitives/Bool/Bool.cs b/src/ComputeSharp.Core/Primitives/Bool/Bool.cs index ff6bbb70..b1fe373a 100644 --- a/src/ComputeSharp.Core/Primitives/Bool/Bool.cs +++ b/src/ComputeSharp.Core/Primitives/Bool/Bool.cs @@ -20,16 +20,28 @@ namespace ComputeSharp /// Creates a new instance for a given value. /// /// . - private Bool(bool value) => Value = value; + private Bool(bool value) + { + Value = value; + } /// - public override bool Equals(object? obj) => obj is Bool x && Value == x.Value; + public override bool Equals(object? obj) + { + return obj is Bool x && Value == x.Value; + } /// - public override int GetHashCode() => Value.GetHashCode(); + public override int GetHashCode() + { + return Value.GetHashCode(); + } /// - public override string ToString() => Value.ToString(); + public override string ToString() + { + return Value.ToString(); + } /// /// Checks whether or not two instances represent the same value. @@ -49,7 +61,7 @@ namespace ComputeSharp /// Inverts the value represented by a given instance. /// /// The input instance. - public static Bool operator !(Bool x) => new Bool(!x.Value); + public static Bool operator !(Bool x) => new(!x.Value); /// /// Converts a given instance to its corresponding value. @@ -61,6 +73,6 @@ namespace ComputeSharp /// Converts a value to a corresponding instance. /// /// The input value. - public static implicit operator Bool(bool x) => new Bool(x); + public static implicit operator Bool(bool x) => new(x); } } diff --git a/src/ComputeSharp.Core/Primitives/Bool/Bool2.cs b/src/ComputeSharp.Core/Primitives/Bool/Bool2.cs index 13d32e3c..581bdcf5 100644 --- a/src/ComputeSharp.Core/Primitives/Bool/Bool2.cs +++ b/src/ComputeSharp.Core/Primitives/Bool/Bool2.cs @@ -24,12 +24,12 @@ namespace ComputeSharp /// /// Gets an value with the component set to , and the others to . /// - public static Bool2 TrueX => new Bool2(true, false); + public static Bool2 TrueX => new(true, false); /// /// Gets an value with the component set to , and the others to . /// - public static Bool2 TrueY => new Bool2(false, true); + public static Bool2 TrueY => new(false, true); /// /// Creates a new instance with the specified parameters. @@ -154,6 +154,6 @@ namespace ComputeSharp /// Creates a new value with the same value for all its components. /// /// The value to use for the components of the new instance. - public static implicit operator Bool2(bool x) => new Bool2(x, x); + public static implicit operator Bool2(bool x) => new(x, x); } } diff --git a/src/ComputeSharp.Core/Primitives/Bool/Bool3.cs b/src/ComputeSharp.Core/Primitives/Bool/Bool3.cs index 6450c6b1..67ad7199 100644 --- a/src/ComputeSharp.Core/Primitives/Bool/Bool3.cs +++ b/src/ComputeSharp.Core/Primitives/Bool/Bool3.cs @@ -24,17 +24,17 @@ namespace ComputeSharp /// /// Gets an value with the component set to , and the others to . /// - public static Bool3 TrueX => new Bool3(true, false, false); + public static Bool3 TrueX => new(true, false, false); /// /// Gets an value with the component set to , and the others to . /// - public static Bool3 TrueY => new Bool3(false, true, false); + public static Bool3 TrueY => new(false, true, false); /// /// Gets an value with the component set to , and the others to . /// - public static Bool3 TrueZ => new Bool3(false, false, true); + public static Bool3 TrueZ => new(false, false, true); /// /// Creates a new instance with the specified parameters. @@ -263,32 +263,32 @@ namespace ComputeSharp /// /// Gets an value with the value for all components. /// - public Bool2 XXX => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(XXX)}"); + public Bool3 XXX => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(XXX)}"); /// /// Gets an value with the , and values. /// - public Bool2 XXY => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(XXY)}"); + public Bool3 XXY => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(XXY)}"); /// /// Gets an value with the , and values. /// - public Bool2 XXZ => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(XXZ)}"); + public Bool3 XXZ => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(XXZ)}"); /// /// Gets an value with the , and values. /// - public Bool2 XYX => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(XYX)}"); + public Bool3 XYX => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(XYX)}"); /// /// Gets an value with the , and values. /// - public Bool2 XYY => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(XYY)}"); + public Bool3 XYY => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(XYY)}"); /// /// Gets or sets an value with the , and values. /// - public Bool2 XYZ + public Bool3 XYZ { get => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(XYZ)}"); set => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(XYZ)}"); @@ -297,12 +297,12 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Bool2 XZX => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(XZX)}"); + public Bool3 XZX => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(XZX)}"); /// /// Gets or sets an value with the , and values. /// - public Bool2 XZY + public Bool3 XZY { get => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(XZY)}"); set => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(XZY)}"); @@ -311,22 +311,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Bool2 XZZ => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(XZZ)}"); + public Bool3 XZZ => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(XZZ)}"); /// /// Gets an value with the , and values. /// - public Bool2 YXX => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(YXX)}"); + public Bool3 YXX => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(YXX)}"); /// /// Gets an value with the , and values. /// - public Bool2 YXY => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(YXY)}"); + public Bool3 YXY => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(YXY)}"); /// /// Gets or sets an value with the , and values. /// - public Bool2 YXZ + public Bool3 YXZ { get => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(YXZ)}"); set => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(YXZ)}"); @@ -335,22 +335,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Bool2 YYX => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(YYX)}"); + public Bool3 YYX => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(YYX)}"); /// /// Gets an value with the value for all components. /// - public Bool2 YYY => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(YYY)}"); + public Bool3 YYY => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(YYY)}"); /// /// Gets an value with the , and values. /// - public Bool2 YYZ => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(YYZ)}"); + public Bool3 YYZ => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(YYZ)}"); /// /// Gets or sets an value with the , and values. /// - public Bool2 YZX + public Bool3 YZX { get => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(YZX)}"); set => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(YZX)}"); @@ -359,22 +359,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Bool2 YZY => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(YZY)}"); + public Bool3 YZY => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(YZY)}"); /// /// Gets an value with the , and values. /// - public Bool2 YZZ => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(YZZ)}"); + public Bool3 YZZ => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(YZZ)}"); /// /// Gets an value with the , and values. /// - public Bool2 ZXX => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(ZXX)}"); + public Bool3 ZXX => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(ZXX)}"); /// /// Gets or sets an value with the , and values. /// - public Bool2 ZXY + public Bool3 ZXY { get => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(ZXY)}"); set => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(ZXY)}"); @@ -383,12 +383,12 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Bool2 ZXZ => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(ZXZ)}"); + public Bool3 ZXZ => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(ZXZ)}"); /// /// Gets or sets an value with the , and values. /// - public Bool2 ZYX + public Bool3 ZYX { get => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(ZYX)}"); set => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(ZYX)}"); @@ -397,57 +397,57 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Bool2 ZYY => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(ZYY)}"); + public Bool3 ZYY => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(ZYY)}"); /// /// Gets an value with the , and values. /// - public Bool2 ZYZ => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(ZYZ)}"); + public Bool3 ZYZ => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(ZYZ)}"); /// /// Gets an value with the , and values. /// - public Bool2 ZZX => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(ZZX)}"); + public Bool3 ZZX => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(ZZX)}"); /// /// Gets an value with the , and values. /// - public Bool2 ZZY => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(ZZY)}"); + public Bool3 ZZY => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(ZZY)}"); /// /// Gets an value with the value for all components. /// - public Bool2 ZZZ => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(ZZZ)}"); + public Bool3 ZZZ => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(ZZZ)}"); /// /// Gets an value with the value for all components. /// - public Bool2 RRR => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(RRR)}"); + public Bool3 RRR => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(RRR)}"); /// /// Gets an value with the , and values. /// - public Bool2 RRG => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(RRG)}"); + public Bool3 RRG => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(RRG)}"); /// /// Gets an value with the , and values. /// - public Bool2 RRB => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(RRB)}"); + public Bool3 RRB => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(RRB)}"); /// /// Gets an value with the , and values. /// - public Bool2 RGR => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(RGR)}"); + public Bool3 RGR => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(RGR)}"); /// /// Gets an value with the , and values. /// - public Bool2 RGG => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(RGG)}"); + public Bool3 RGG => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(RGG)}"); /// /// Gets or sets an value with the , and values. /// - public Bool2 RGB + public Bool3 RGB { get => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(RGB)}"); set => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(RGB)}"); @@ -456,12 +456,12 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Bool2 RBR => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(RBR)}"); + public Bool3 RBR => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(RBR)}"); /// /// Gets or sets an value with the , and values. /// - public Bool2 RBG + public Bool3 RBG { get => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(RBG)}"); set => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(RBG)}"); @@ -470,22 +470,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Bool2 RBB => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(RBB)}"); + public Bool3 RBB => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(RBB)}"); /// /// Gets an value with the , and values. /// - public Bool2 GRR => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(GRR)}"); + public Bool3 GRR => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(GRR)}"); /// /// Gets an value with the , and values. /// - public Bool2 GRG => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(GRG)}"); + public Bool3 GRG => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(GRG)}"); /// /// Gets or sets an value with the , and values. /// - public Bool2 GRB + public Bool3 GRB { get => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(GRB)}"); set => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(GRB)}"); @@ -494,22 +494,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Bool2 GGR => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(GGR)}"); + public Bool3 GGR => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(GGR)}"); /// /// Gets an value with the value for all components. /// - public Bool2 GGG => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(GGG)}"); + public Bool3 GGG => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(GGG)}"); /// /// Gets an value with the , and values. /// - public Bool2 GGB => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(GGB)}"); + public Bool3 GGB => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(GGB)}"); /// /// Gets or sets an value with the , and values. /// - public Bool2 GBR + public Bool3 GBR { get => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(GBR)}"); set => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(GBR)}"); @@ -518,22 +518,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Bool2 GBG => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(GBG)}"); + public Bool3 GBG => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(GBG)}"); /// /// Gets an value with the , and values. /// - public Bool2 GBB => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(GBB)}"); + public Bool3 GBB => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(GBB)}"); /// /// Gets an value with the , and values. /// - public Bool2 BRR => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(BRR)}"); + public Bool3 BRR => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(BRR)}"); /// /// Gets or sets an value with the , and values. /// - public Bool2 BRG + public Bool3 BRG { get => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(BRG)}"); set => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(BRG)}"); @@ -542,12 +542,12 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Bool2 BRB => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(BRB)}"); + public Bool3 BRB => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(BRB)}"); /// /// Gets or sets an value with the , and values. /// - public Bool2 BGR + public Bool3 BGR { get => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(BGR)}"); set => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(BGR)}"); @@ -556,32 +556,32 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Bool2 BGG => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(BGG)}"); + public Bool3 BGG => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(BGG)}"); /// /// Gets an value with the , and values. /// - public Bool2 BGB => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(BGB)}"); + public Bool3 BGB => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(BGB)}"); /// /// Gets an value with the , and values. /// - public Bool2 BBR => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(BBR)}"); + public Bool3 BBR => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(BBR)}"); /// /// Gets an value with the , and values. /// - public Bool2 BBG => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(BBG)}"); + public Bool3 BBG => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(BBG)}"); /// /// Gets an value with the value for all components. /// - public Bool2 BBB => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(BBB)}"); + public Bool3 BBB => throw new InvalidExecutionContextException($"{nameof(Bool3)}.{nameof(BBB)}"); /// /// Creates a new value with the same value for all its components. /// /// The value to use for the components of the new instance. - public static implicit operator Bool3(bool x) => new Bool3(x, x, x); + public static implicit operator Bool3(bool x) => new(x, x, x); } } diff --git a/src/ComputeSharp.Core/Primitives/Bool/Bool4.cs b/src/ComputeSharp.Core/Primitives/Bool/Bool4.cs index cfb92514..2ca262e2 100644 --- a/src/ComputeSharp.Core/Primitives/Bool/Bool4.cs +++ b/src/ComputeSharp.Core/Primitives/Bool/Bool4.cs @@ -24,22 +24,22 @@ namespace ComputeSharp /// /// Gets an value with the component set to , and the others to . /// - public static Bool4 TrueX => new Bool4(true, false, false, false); + public static Bool4 TrueX => new(true, false, false, false); /// /// Gets an value with the component set to , and the others to . /// - public static Bool4 TrueY => new Bool4(false, true, false, false); + public static Bool4 TrueY => new(false, true, false, false); /// /// Gets an value with the component set to , and the others to . /// - public static Bool4 TrueZ => new Bool4(false, false, true, false); + public static Bool4 TrueZ => new(false, false, true, false); /// /// Gets an value with the component set to , and the others to . /// - public static Bool4 TrueW => new Bool4(false, false, false, true); + public static Bool4 TrueW => new(false, false, false, true); /// /// Creates a new instance with the specified parameters. @@ -3994,6 +3994,6 @@ namespace ComputeSharp /// Creates a new value with the same value for all its components. /// /// The value to use for the components of the new instance. - public static implicit operator Bool4(bool x) => new Bool4(x, x, x, x); + public static implicit operator Bool4(bool x) => new(x, x, x, x); } } diff --git a/src/ComputeSharp.Core/Primitives/Double/Double2.cs b/src/ComputeSharp.Core/Primitives/Double/Double2.cs index 5809961b..7409862c 100644 --- a/src/ComputeSharp.Core/Primitives/Double/Double2.cs +++ b/src/ComputeSharp.Core/Primitives/Double/Double2.cs @@ -24,12 +24,12 @@ namespace ComputeSharp /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Double2 UnitX => new Double2(1, 0); + public static Double2 UnitX => new(1, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Double2 UnitY => new Double2(0, 1); + public static Double2 UnitY => new(0, 1); /// /// Creates a new instance with the specified parameters. @@ -140,7 +140,7 @@ namespace ComputeSharp /// Creates a new value with the same value for all its components. /// /// The value to use for the components of the new instance. - public static implicit operator Double2(double x) => new Double2(x, x); + public static implicit operator Double2(double x) => new(x, x); /// /// Sums two values. diff --git a/src/ComputeSharp.Core/Primitives/Double/Double3.cs b/src/ComputeSharp.Core/Primitives/Double/Double3.cs index a9e01edc..27eb3b80 100644 --- a/src/ComputeSharp.Core/Primitives/Double/Double3.cs +++ b/src/ComputeSharp.Core/Primitives/Double/Double3.cs @@ -24,17 +24,17 @@ namespace ComputeSharp /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Double3 UnitX => new Double3(1, 0, 0); + public static Double3 UnitX => new(1, 0, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Double3 UnitY => new Double3(0, 1, 0); + public static Double3 UnitY => new(0, 1, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Double3 UnitZ => new Double3(0, 0, 1); + public static Double3 UnitZ => new(0, 0, 1); /// /// Creates a new instance with the specified parameters. @@ -242,32 +242,32 @@ namespace ComputeSharp /// /// Gets an value with the value for all components. /// - public Double2 XXX => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(XXX)}"); + public Double3 XXX => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(XXX)}"); /// /// Gets an value with the , and values. /// - public Double2 XXY => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(XXY)}"); + public Double3 XXY => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(XXY)}"); /// /// Gets an value with the , and values. /// - public Double2 XXZ => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(XXZ)}"); + public Double3 XXZ => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(XXZ)}"); /// /// Gets an value with the , and values. /// - public Double2 XYX => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(XYX)}"); + public Double3 XYX => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(XYX)}"); /// /// Gets an value with the , and values. /// - public Double2 XYY => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(XYY)}"); + public Double3 XYY => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(XYY)}"); /// /// Gets or sets an value with the , and values. /// - public Double2 XYZ + public Double3 XYZ { get => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(XYZ)}"); set => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(XYZ)}"); @@ -276,12 +276,12 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Double2 XZX => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(XZX)}"); + public Double3 XZX => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(XZX)}"); /// /// Gets or sets an value with the , and values. /// - public Double2 XZY + public Double3 XZY { get => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(XZY)}"); set => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(XZY)}"); @@ -290,22 +290,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Double2 XZZ => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(XZZ)}"); + public Double3 XZZ => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(XZZ)}"); /// /// Gets an value with the , and values. /// - public Double2 YXX => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(YXX)}"); + public Double3 YXX => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(YXX)}"); /// /// Gets an value with the , and values. /// - public Double2 YXY => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(YXY)}"); + public Double3 YXY => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(YXY)}"); /// /// Gets or sets an value with the , and values. /// - public Double2 YXZ + public Double3 YXZ { get => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(YXZ)}"); set => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(YXZ)}"); @@ -314,22 +314,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Double2 YYX => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(YYX)}"); + public Double3 YYX => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(YYX)}"); /// /// Gets an value with the value for all components. /// - public Double2 YYY => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(YYY)}"); + public Double3 YYY => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(YYY)}"); /// /// Gets an value with the , and values. /// - public Double2 YYZ => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(YYZ)}"); + public Double3 YYZ => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(YYZ)}"); /// /// Gets or sets an value with the , and values. /// - public Double2 YZX + public Double3 YZX { get => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(YZX)}"); set => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(YZX)}"); @@ -338,22 +338,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Double2 YZY => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(YZY)}"); + public Double3 YZY => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(YZY)}"); /// /// Gets an value with the , and values. /// - public Double2 YZZ => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(YZZ)}"); + public Double3 YZZ => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(YZZ)}"); /// /// Gets an value with the , and values. /// - public Double2 ZXX => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(ZXX)}"); + public Double3 ZXX => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(ZXX)}"); /// /// Gets or sets an value with the , and values. /// - public Double2 ZXY + public Double3 ZXY { get => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(ZXY)}"); set => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(ZXY)}"); @@ -362,12 +362,12 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Double2 ZXZ => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(ZXZ)}"); + public Double3 ZXZ => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(ZXZ)}"); /// /// Gets or sets an value with the , and values. /// - public Double2 ZYX + public Double3 ZYX { get => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(ZYX)}"); set => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(ZYX)}"); @@ -376,57 +376,57 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Double2 ZYY => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(ZYY)}"); + public Double3 ZYY => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(ZYY)}"); /// /// Gets an value with the , and values. /// - public Double2 ZYZ => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(ZYZ)}"); + public Double3 ZYZ => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(ZYZ)}"); /// /// Gets an value with the , and values. /// - public Double2 ZZX => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(ZZX)}"); + public Double3 ZZX => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(ZZX)}"); /// /// Gets an value with the , and values. /// - public Double2 ZZY => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(ZZY)}"); + public Double3 ZZY => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(ZZY)}"); /// /// Gets an value with the value for all components. /// - public Double2 ZZZ => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(ZZZ)}"); + public Double3 ZZZ => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(ZZZ)}"); /// /// Gets an value with the value for all components. /// - public Double2 RRR => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(RRR)}"); + public Double3 RRR => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(RRR)}"); /// /// Gets an value with the , and values. /// - public Double2 RRG => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(RRG)}"); + public Double3 RRG => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(RRG)}"); /// /// Gets an value with the , and values. /// - public Double2 RRB => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(RRB)}"); + public Double3 RRB => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(RRB)}"); /// /// Gets an value with the , and values. /// - public Double2 RGR => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(RGR)}"); + public Double3 RGR => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(RGR)}"); /// /// Gets an value with the , and values. /// - public Double2 RGG => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(RGG)}"); + public Double3 RGG => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(RGG)}"); /// /// Gets or sets an value with the , and values. /// - public Double2 RGB + public Double3 RGB { get => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(RGB)}"); set => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(RGB)}"); @@ -435,12 +435,12 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Double2 RBR => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(RBR)}"); + public Double3 RBR => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(RBR)}"); /// /// Gets or sets an value with the , and values. /// - public Double2 RBG + public Double3 RBG { get => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(RBG)}"); set => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(RBG)}"); @@ -449,22 +449,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Double2 RBB => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(RBB)}"); + public Double3 RBB => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(RBB)}"); /// /// Gets an value with the , and values. /// - public Double2 GRR => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(GRR)}"); + public Double3 GRR => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(GRR)}"); /// /// Gets an value with the , and values. /// - public Double2 GRG => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(GRG)}"); + public Double3 GRG => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(GRG)}"); /// /// Gets or sets an value with the , and values. /// - public Double2 GRB + public Double3 GRB { get => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(GRB)}"); set => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(GRB)}"); @@ -473,22 +473,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Double2 GGR => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(GGR)}"); + public Double3 GGR => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(GGR)}"); /// /// Gets an value with the value for all components. /// - public Double2 GGG => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(GGG)}"); + public Double3 GGG => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(GGG)}"); /// /// Gets an value with the , and values. /// - public Double2 GGB => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(GGB)}"); + public Double3 GGB => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(GGB)}"); /// /// Gets or sets an value with the , and values. /// - public Double2 GBR + public Double3 GBR { get => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(GBR)}"); set => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(GBR)}"); @@ -497,22 +497,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Double2 GBG => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(GBG)}"); + public Double3 GBG => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(GBG)}"); /// /// Gets an value with the , and values. /// - public Double2 GBB => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(GBB)}"); + public Double3 GBB => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(GBB)}"); /// /// Gets an value with the , and values. /// - public Double2 BRR => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(BRR)}"); + public Double3 BRR => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(BRR)}"); /// /// Gets or sets an value with the , and values. /// - public Double2 BRG + public Double3 BRG { get => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(BRG)}"); set => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(BRG)}"); @@ -521,12 +521,12 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Double2 BRB => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(BRB)}"); + public Double3 BRB => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(BRB)}"); /// /// Gets or sets an value with the , and values. /// - public Double2 BGR + public Double3 BGR { get => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(BGR)}"); set => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(BGR)}"); @@ -535,33 +535,33 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Double2 BGG => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(BGG)}"); + public Double3 BGG => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(BGG)}"); /// /// Gets an value with the , and values. /// - public Double2 BGB => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(BGB)}"); + public Double3 BGB => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(BGB)}"); /// /// Gets an value with the , and values. /// - public Double2 BBR => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(BBR)}"); + public Double3 BBR => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(BBR)}"); /// /// Gets an value with the , and values. /// - public Double2 BBG => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(BBG)}"); + public Double3 BBG => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(BBG)}"); /// /// Gets an value with the value for all components. /// - public Double2 BBB => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(BBB)}"); + public Double3 BBB => throw new InvalidExecutionContextException($"{nameof(Double3)}.{nameof(BBB)}"); /// /// Creates a new value with the same value for all its components. /// /// The value to use for the components of the new instance. - public static implicit operator Double3(double x) => new Double3(x, x, x); + public static implicit operator Double3(double x) => new(x, x, x); /// /// Sums two values. diff --git a/src/ComputeSharp.Core/Primitives/Double/Double4.cs b/src/ComputeSharp.Core/Primitives/Double/Double4.cs index a87dcfba..d59ddf09 100644 --- a/src/ComputeSharp.Core/Primitives/Double/Double4.cs +++ b/src/ComputeSharp.Core/Primitives/Double/Double4.cs @@ -24,22 +24,22 @@ namespace ComputeSharp /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Double4 UnitX => new Double4(1, 0, 0, 0); + public static Double4 UnitX => new(1, 0, 0, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Double4 UnitY => new Double4(0, 1, 0, 0); + public static Double4 UnitY => new(0, 1, 0, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Double4 UnitZ => new Double4(0, 0, 1, 0); + public static Double4 UnitZ => new(0, 0, 1, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Double4 UnitW => new Double4(0, 0, 0, 1); + public static Double4 UnitW => new(0, 0, 0, 1); /// /// Creates a new instance with the specified parameters. @@ -3966,7 +3966,7 @@ namespace ComputeSharp /// Creates a new value with the same value for all its components. /// /// The value to use for the components of the new instance. - public static implicit operator Double4(double x) => new Double4(x, x, x, x); + public static implicit operator Double4(double x) => new(x, x, x, x); /// /// Sums two values. diff --git a/src/ComputeSharp.Core/Primitives/Float/Float2.cs b/src/ComputeSharp.Core/Primitives/Float/Float2.cs index 69ff3eec..266cbe77 100644 --- a/src/ComputeSharp.Core/Primitives/Float/Float2.cs +++ b/src/ComputeSharp.Core/Primitives/Float/Float2.cs @@ -24,12 +24,12 @@ namespace ComputeSharp /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Float2 UnitX => new Float2(1, 0); + public static Float2 UnitX => new(1, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Float2 UnitY => new Float2(0, 1); + public static Float2 UnitY => new(0, 1); /// /// Creates a new instance with the specified parameters. @@ -140,7 +140,7 @@ namespace ComputeSharp /// Creates a new value with the same value for all its components. /// /// The value to use for the components of the new instance. - public static implicit operator Float2(float x) => new Float2(x, x); + public static implicit operator Float2(float x) => new(x, x); /// /// Sums two values. diff --git a/src/ComputeSharp.Core/Primitives/Float/Float3.cs b/src/ComputeSharp.Core/Primitives/Float/Float3.cs index 20b9c1f2..d8d9675b 100644 --- a/src/ComputeSharp.Core/Primitives/Float/Float3.cs +++ b/src/ComputeSharp.Core/Primitives/Float/Float3.cs @@ -24,17 +24,17 @@ namespace ComputeSharp /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Float3 UnitX => new Float3(1, 0, 0); + public static Float3 UnitX => new(1, 0, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Float3 UnitY => new Float3(0, 1, 0); + public static Float3 UnitY => new(0, 1, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Float3 UnitZ => new Float3(0, 0, 1); + public static Float3 UnitZ => new(0, 0, 1); /// /// Creates a new instance with the specified parameters. @@ -242,32 +242,32 @@ namespace ComputeSharp /// /// Gets an value with the value for all components. /// - public Float2 XXX => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(XXX)}"); + public Float3 XXX => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(XXX)}"); /// /// Gets an value with the , and values. /// - public Float2 XXY => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(XXY)}"); + public Float3 XXY => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(XXY)}"); /// /// Gets an value with the , and values. /// - public Float2 XXZ => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(XXZ)}"); + public Float3 XXZ => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(XXZ)}"); /// /// Gets an value with the , and values. /// - public Float2 XYX => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(XYX)}"); + public Float3 XYX => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(XYX)}"); /// /// Gets an value with the , and values. /// - public Float2 XYY => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(XYY)}"); + public Float3 XYY => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(XYY)}"); /// /// Gets or sets an value with the , and values. /// - public Float2 XYZ + public Float3 XYZ { get => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(XYZ)}"); set => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(XYZ)}"); @@ -276,12 +276,12 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Float2 XZX => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(XZX)}"); + public Float3 XZX => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(XZX)}"); /// /// Gets or sets an value with the , and values. /// - public Float2 XZY + public Float3 XZY { get => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(XZY)}"); set => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(XZY)}"); @@ -290,22 +290,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Float2 XZZ => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(XZZ)}"); + public Float3 XZZ => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(XZZ)}"); /// /// Gets an value with the , and values. /// - public Float2 YXX => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(YXX)}"); + public Float3 YXX => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(YXX)}"); /// /// Gets an value with the , and values. /// - public Float2 YXY => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(YXY)}"); + public Float3 YXY => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(YXY)}"); /// /// Gets or sets an value with the , and values. /// - public Float2 YXZ + public Float3 YXZ { get => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(YXZ)}"); set => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(YXZ)}"); @@ -314,22 +314,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Float2 YYX => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(YYX)}"); + public Float3 YYX => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(YYX)}"); /// /// Gets an value with the value for all components. /// - public Float2 YYY => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(YYY)}"); + public Float3 YYY => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(YYY)}"); /// /// Gets an value with the , and values. /// - public Float2 YYZ => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(YYZ)}"); + public Float3 YYZ => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(YYZ)}"); /// /// Gets or sets an value with the , and values. /// - public Float2 YZX + public Float3 YZX { get => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(YZX)}"); set => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(YZX)}"); @@ -338,22 +338,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Float2 YZY => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(YZY)}"); + public Float3 YZY => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(YZY)}"); /// /// Gets an value with the , and values. /// - public Float2 YZZ => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(YZZ)}"); + public Float3 YZZ => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(YZZ)}"); /// /// Gets an value with the , and values. /// - public Float2 ZXX => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(ZXX)}"); + public Float3 ZXX => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(ZXX)}"); /// /// Gets or sets an value with the , and values. /// - public Float2 ZXY + public Float3 ZXY { get => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(ZXY)}"); set => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(ZXY)}"); @@ -362,12 +362,12 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Float2 ZXZ => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(ZXZ)}"); + public Float3 ZXZ => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(ZXZ)}"); /// /// Gets or sets an value with the , and values. /// - public Float2 ZYX + public Float3 ZYX { get => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(ZYX)}"); set => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(ZYX)}"); @@ -376,57 +376,57 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Float2 ZYY => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(ZYY)}"); + public Float3 ZYY => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(ZYY)}"); /// /// Gets an value with the , and values. /// - public Float2 ZYZ => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(ZYZ)}"); + public Float3 ZYZ => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(ZYZ)}"); /// /// Gets an value with the , and values. /// - public Float2 ZZX => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(ZZX)}"); + public Float3 ZZX => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(ZZX)}"); /// /// Gets an value with the , and values. /// - public Float2 ZZY => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(ZZY)}"); + public Float3 ZZY => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(ZZY)}"); /// /// Gets an value with the value for all components. /// - public Float2 ZZZ => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(ZZZ)}"); + public Float3 ZZZ => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(ZZZ)}"); /// /// Gets an value with the value for all components. /// - public Float2 RRR => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(RRR)}"); + public Float3 RRR => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(RRR)}"); /// /// Gets an value with the , and values. /// - public Float2 RRG => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(RRG)}"); + public Float3 RRG => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(RRG)}"); /// /// Gets an value with the , and values. /// - public Float2 RRB => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(RRB)}"); + public Float3 RRB => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(RRB)}"); /// /// Gets an value with the , and values. /// - public Float2 RGR => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(RGR)}"); + public Float3 RGR => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(RGR)}"); /// /// Gets an value with the , and values. /// - public Float2 RGG => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(RGG)}"); + public Float3 RGG => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(RGG)}"); /// /// Gets or sets an value with the , and values. /// - public Float2 RGB + public Float3 RGB { get => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(RGB)}"); set => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(RGB)}"); @@ -435,12 +435,12 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Float2 RBR => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(RBR)}"); + public Float3 RBR => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(RBR)}"); /// /// Gets or sets an value with the , and values. /// - public Float2 RBG + public Float3 RBG { get => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(RBG)}"); set => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(RBG)}"); @@ -449,22 +449,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Float2 RBB => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(RBB)}"); + public Float3 RBB => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(RBB)}"); /// /// Gets an value with the , and values. /// - public Float2 GRR => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(GRR)}"); + public Float3 GRR => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(GRR)}"); /// /// Gets an value with the , and values. /// - public Float2 GRG => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(GRG)}"); + public Float3 GRG => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(GRG)}"); /// /// Gets or sets an value with the , and values. /// - public Float2 GRB + public Float3 GRB { get => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(GRB)}"); set => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(GRB)}"); @@ -473,22 +473,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Float2 GGR => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(GGR)}"); + public Float3 GGR => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(GGR)}"); /// /// Gets an value with the value for all components. /// - public Float2 GGG => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(GGG)}"); + public Float3 GGG => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(GGG)}"); /// /// Gets an value with the , and values. /// - public Float2 GGB => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(GGB)}"); + public Float3 GGB => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(GGB)}"); /// /// Gets or sets an value with the , and values. /// - public Float2 GBR + public Float3 GBR { get => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(GBR)}"); set => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(GBR)}"); @@ -497,22 +497,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Float2 GBG => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(GBG)}"); + public Float3 GBG => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(GBG)}"); /// /// Gets an value with the , and values. /// - public Float2 GBB => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(GBB)}"); + public Float3 GBB => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(GBB)}"); /// /// Gets an value with the , and values. /// - public Float2 BRR => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(BRR)}"); + public Float3 BRR => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(BRR)}"); /// /// Gets or sets an value with the , and values. /// - public Float2 BRG + public Float3 BRG { get => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(BRG)}"); set => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(BRG)}"); @@ -521,12 +521,12 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Float2 BRB => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(BRB)}"); + public Float3 BRB => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(BRB)}"); /// /// Gets or sets an value with the , and values. /// - public Float2 BGR + public Float3 BGR { get => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(BGR)}"); set => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(BGR)}"); @@ -535,33 +535,33 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Float2 BGG => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(BGG)}"); + public Float3 BGG => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(BGG)}"); /// /// Gets an value with the , and values. /// - public Float2 BGB => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(BGB)}"); + public Float3 BGB => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(BGB)}"); /// /// Gets an value with the , and values. /// - public Float2 BBR => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(BBR)}"); + public Float3 BBR => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(BBR)}"); /// /// Gets an value with the , and values. /// - public Float2 BBG => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(BBG)}"); + public Float3 BBG => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(BBG)}"); /// /// Gets an value with the value for all components. /// - public Float2 BBB => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(BBB)}"); + public Float3 BBB => throw new InvalidExecutionContextException($"{nameof(Float3)}.{nameof(BBB)}"); /// /// Creates a new value with the same value for all its components. /// /// The value to use for the components of the new instance. - public static implicit operator Float3(float x) => new Float3(x, x, x); + public static implicit operator Float3(float x) => new(x, x, x); /// /// Sums two values. diff --git a/src/ComputeSharp.Core/Primitives/Float/Float4.cs b/src/ComputeSharp.Core/Primitives/Float/Float4.cs index 3dbaa07e..e112ab95 100644 --- a/src/ComputeSharp.Core/Primitives/Float/Float4.cs +++ b/src/ComputeSharp.Core/Primitives/Float/Float4.cs @@ -24,22 +24,22 @@ namespace ComputeSharp /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Float4 UnitX => new Float4(1, 0, 0, 0); + public static Float4 UnitX => new(1, 0, 0, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Float4 UnitY => new Float4(0, 1, 0, 0); + public static Float4 UnitY => new(0, 1, 0, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Float4 UnitZ => new Float4(0, 0, 1, 0); + public static Float4 UnitZ => new(0, 0, 1, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Float4 UnitW => new Float4(0, 0, 0, 1); + public static Float4 UnitW => new(0, 0, 0, 1); /// /// Creates a new instance with the specified parameters. @@ -3966,7 +3966,7 @@ namespace ComputeSharp /// Creates a new value with the same value for all its components. /// /// The value to use for the components of the new instance. - public static implicit operator Float4(float x) => new Float4(x, x, x, x); + public static implicit operator Float4(float x) => new(x, x, x, x); /// /// Sums two values. diff --git a/src/ComputeSharp.Core/Primitives/Int/Int2.cs b/src/ComputeSharp.Core/Primitives/Int/Int2.cs index dc41c1ca..76e238a5 100644 --- a/src/ComputeSharp.Core/Primitives/Int/Int2.cs +++ b/src/ComputeSharp.Core/Primitives/Int/Int2.cs @@ -24,12 +24,12 @@ namespace ComputeSharp /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Int2 UnitX => new Int2(1, 0); + public static Int2 UnitX => new(1, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Int2 UnitY => new Int2(0, 1); + public static Int2 UnitY => new(0, 1); /// /// Creates a new instance with the specified parameters. @@ -140,7 +140,7 @@ namespace ComputeSharp /// Creates a new value with the same value for all its components. /// /// The value to use for the components of the new instance. - public static implicit operator Int2(int x) => new Int2(x, x); + public static implicit operator Int2(int x) => new(x, x); /// /// Sums two values. diff --git a/src/ComputeSharp.Core/Primitives/Int/Int3.cs b/src/ComputeSharp.Core/Primitives/Int/Int3.cs index 620ebb81..cb2f09f2 100644 --- a/src/ComputeSharp.Core/Primitives/Int/Int3.cs +++ b/src/ComputeSharp.Core/Primitives/Int/Int3.cs @@ -24,17 +24,17 @@ namespace ComputeSharp /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Int3 UnitX => new Int3(1, 0, 0); + public static Int3 UnitX => new(1, 0, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Int3 UnitY => new Int3(0, 1, 0); + public static Int3 UnitY => new(0, 1, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Int3 UnitZ => new Int3(0, 0, 1); + public static Int3 UnitZ => new(0, 0, 1); /// /// Creates a new instance with the specified parameters. @@ -242,32 +242,32 @@ namespace ComputeSharp /// /// Gets an value with the value for all components. /// - public Int2 XXX => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(XXX)}"); + public Int3 XXX => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(XXX)}"); /// /// Gets an value with the , and values. /// - public Int2 XXY => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(XXY)}"); + public Int3 XXY => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(XXY)}"); /// /// Gets an value with the , and values. /// - public Int2 XXZ => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(XXZ)}"); + public Int3 XXZ => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(XXZ)}"); /// /// Gets an value with the , and values. /// - public Int2 XYX => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(XYX)}"); + public Int3 XYX => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(XYX)}"); /// /// Gets an value with the , and values. /// - public Int2 XYY => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(XYY)}"); + public Int3 XYY => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(XYY)}"); /// /// Gets or sets an value with the , and values. /// - public Int2 XYZ + public Int3 XYZ { get => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(XYZ)}"); set => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(XYZ)}"); @@ -276,12 +276,12 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Int2 XZX => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(XZX)}"); + public Int3 XZX => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(XZX)}"); /// /// Gets or sets an value with the , and values. /// - public Int2 XZY + public Int3 XZY { get => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(XZY)}"); set => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(XZY)}"); @@ -290,22 +290,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Int2 XZZ => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(XZZ)}"); + public Int3 XZZ => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(XZZ)}"); /// /// Gets an value with the , and values. /// - public Int2 YXX => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(YXX)}"); + public Int3 YXX => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(YXX)}"); /// /// Gets an value with the , and values. /// - public Int2 YXY => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(YXY)}"); + public Int3 YXY => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(YXY)}"); /// /// Gets or sets an value with the , and values. /// - public Int2 YXZ + public Int3 YXZ { get => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(YXZ)}"); set => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(YXZ)}"); @@ -314,22 +314,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Int2 YYX => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(YYX)}"); + public Int3 YYX => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(YYX)}"); /// /// Gets an value with the value for all components. /// - public Int2 YYY => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(YYY)}"); + public Int3 YYY => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(YYY)}"); /// /// Gets an value with the , and values. /// - public Int2 YYZ => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(YYZ)}"); + public Int3 YYZ => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(YYZ)}"); /// /// Gets or sets an value with the , and values. /// - public Int2 YZX + public Int3 YZX { get => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(YZX)}"); set => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(YZX)}"); @@ -338,22 +338,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Int2 YZY => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(YZY)}"); + public Int3 YZY => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(YZY)}"); /// /// Gets an value with the , and values. /// - public Int2 YZZ => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(YZZ)}"); + public Int3 YZZ => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(YZZ)}"); /// /// Gets an value with the , and values. /// - public Int2 ZXX => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(ZXX)}"); + public Int3 ZXX => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(ZXX)}"); /// /// Gets or sets an value with the , and values. /// - public Int2 ZXY + public Int3 ZXY { get => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(ZXY)}"); set => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(ZXY)}"); @@ -362,12 +362,12 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Int2 ZXZ => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(ZXZ)}"); + public Int3 ZXZ => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(ZXZ)}"); /// /// Gets or sets an value with the , and values. /// - public Int2 ZYX + public Int3 ZYX { get => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(ZYX)}"); set => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(ZYX)}"); @@ -376,57 +376,57 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Int2 ZYY => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(ZYY)}"); + public Int3 ZYY => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(ZYY)}"); /// /// Gets an value with the , and values. /// - public Int2 ZYZ => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(ZYZ)}"); + public Int3 ZYZ => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(ZYZ)}"); /// /// Gets an value with the , and values. /// - public Int2 ZZX => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(ZZX)}"); + public Int3 ZZX => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(ZZX)}"); /// /// Gets an value with the , and values. /// - public Int2 ZZY => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(ZZY)}"); + public Int3 ZZY => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(ZZY)}"); /// /// Gets an value with the value for all components. /// - public Int2 ZZZ => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(ZZZ)}"); + public Int3 ZZZ => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(ZZZ)}"); /// /// Gets an value with the value for all components. /// - public Int2 RRR => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(RRR)}"); + public Int3 RRR => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(RRR)}"); /// /// Gets an value with the , and values. /// - public Int2 RRG => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(RRG)}"); + public Int3 RRG => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(RRG)}"); /// /// Gets an value with the , and values. /// - public Int2 RRB => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(RRB)}"); + public Int3 RRB => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(RRB)}"); /// /// Gets an value with the , and values. /// - public Int2 RGR => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(RGR)}"); + public Int3 RGR => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(RGR)}"); /// /// Gets an value with the , and values. /// - public Int2 RGG => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(RGG)}"); + public Int3 RGG => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(RGG)}"); /// /// Gets or sets an value with the , and values. /// - public Int2 RGB + public Int3 RGB { get => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(RGB)}"); set => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(RGB)}"); @@ -435,12 +435,12 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Int2 RBR => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(RBR)}"); + public Int3 RBR => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(RBR)}"); /// /// Gets or sets an value with the , and values. /// - public Int2 RBG + public Int3 RBG { get => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(RBG)}"); set => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(RBG)}"); @@ -449,22 +449,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Int2 RBB => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(RBB)}"); + public Int3 RBB => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(RBB)}"); /// /// Gets an value with the , and values. /// - public Int2 GRR => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(GRR)}"); + public Int3 GRR => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(GRR)}"); /// /// Gets an value with the , and values. /// - public Int2 GRG => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(GRG)}"); + public Int3 GRG => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(GRG)}"); /// /// Gets or sets an value with the , and values. /// - public Int2 GRB + public Int3 GRB { get => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(GRB)}"); set => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(GRB)}"); @@ -473,22 +473,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Int2 GGR => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(GGR)}"); + public Int3 GGR => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(GGR)}"); /// /// Gets an value with the value for all components. /// - public Int2 GGG => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(GGG)}"); + public Int3 GGG => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(GGG)}"); /// /// Gets an value with the , and values. /// - public Int2 GGB => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(GGB)}"); + public Int3 GGB => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(GGB)}"); /// /// Gets or sets an value with the , and values. /// - public Int2 GBR + public Int3 GBR { get => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(GBR)}"); set => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(GBR)}"); @@ -497,22 +497,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Int2 GBG => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(GBG)}"); + public Int3 GBG => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(GBG)}"); /// /// Gets an value with the , and values. /// - public Int2 GBB => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(GBB)}"); + public Int3 GBB => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(GBB)}"); /// /// Gets an value with the , and values. /// - public Int2 BRR => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(BRR)}"); + public Int3 BRR => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(BRR)}"); /// /// Gets or sets an value with the , and values. /// - public Int2 BRG + public Int3 BRG { get => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(BRG)}"); set => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(BRG)}"); @@ -521,12 +521,12 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Int2 BRB => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(BRB)}"); + public Int3 BRB => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(BRB)}"); /// /// Gets or sets an value with the , and values. /// - public Int2 BGR + public Int3 BGR { get => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(BGR)}"); set => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(BGR)}"); @@ -535,33 +535,33 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public Int2 BGG => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(BGG)}"); + public Int3 BGG => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(BGG)}"); /// /// Gets an value with the , and values. /// - public Int2 BGB => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(BGB)}"); + public Int3 BGB => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(BGB)}"); /// /// Gets an value with the , and values. /// - public Int2 BBR => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(BBR)}"); + public Int3 BBR => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(BBR)}"); /// /// Gets an value with the , and values. /// - public Int2 BBG => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(BBG)}"); + public Int3 BBG => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(BBG)}"); /// /// Gets an value with the value for all components. /// - public Int2 BBB => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(BBB)}"); + public Int3 BBB => throw new InvalidExecutionContextException($"{nameof(Int3)}.{nameof(BBB)}"); /// /// Creates a new value with the same value for all its components. /// /// The value to use for the components of the new instance. - public static implicit operator Int3(int x) => new Int3(x, x, x); + public static implicit operator Int3(int x) => new(x, x, x); /// /// Sums two values. diff --git a/src/ComputeSharp.Core/Primitives/Int/Int4.cs b/src/ComputeSharp.Core/Primitives/Int/Int4.cs index ed11b46b..692f5e66 100644 --- a/src/ComputeSharp.Core/Primitives/Int/Int4.cs +++ b/src/ComputeSharp.Core/Primitives/Int/Int4.cs @@ -24,22 +24,22 @@ namespace ComputeSharp /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Int4 UnitX => new Int4(1, 0, 0, 0); + public static Int4 UnitX => new(1, 0, 0, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Int4 UnitY => new Int4(0, 1, 0, 0); + public static Int4 UnitY => new(0, 1, 0, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Int4 UnitZ => new Int4(0, 0, 1, 0); + public static Int4 UnitZ => new(0, 0, 1, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static Int4 UnitW => new Int4(0, 0, 0, 1); + public static Int4 UnitW => new(0, 0, 0, 1); /// /// Creates a new instance with the specified parameters. @@ -3966,7 +3966,7 @@ namespace ComputeSharp /// Creates a new value with the same value for all its components. /// /// The value to use for the components of the new instance. - public static implicit operator Int4(int x) => new Int4(x, x, x, x); + public static implicit operator Int4(int x) => new(x, x, x, x); /// /// Sums two values. diff --git a/src/ComputeSharp.Core/Primitives/UInt/UInt2.cs b/src/ComputeSharp.Core/Primitives/UInt/UInt2.cs index 5f40d45f..8865306e 100644 --- a/src/ComputeSharp.Core/Primitives/UInt/UInt2.cs +++ b/src/ComputeSharp.Core/Primitives/UInt/UInt2.cs @@ -24,12 +24,12 @@ namespace ComputeSharp /// /// Gets an value with the component set to 1, and the others to 0. /// - public static UInt2 UnitX => new UInt2(1, 0); + public static UInt2 UnitX => new(1, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static UInt2 UnitY => new UInt2(0, 1); + public static UInt2 UnitY => new(0, 1); /// /// Creates a new instance with the specified parameters. @@ -140,7 +140,7 @@ namespace ComputeSharp /// Creates a new value with the same value for all its components. /// /// The value to use for the components of the new instance. - public static implicit operator UInt2(uint x) => new UInt2(x, x); + public static implicit operator UInt2(uint x) => new(x, x); /// /// Sums two values. diff --git a/src/ComputeSharp.Core/Primitives/UInt/UInt3.cs b/src/ComputeSharp.Core/Primitives/UInt/UInt3.cs index ccaa7d12..7f4bfc83 100644 --- a/src/ComputeSharp.Core/Primitives/UInt/UInt3.cs +++ b/src/ComputeSharp.Core/Primitives/UInt/UInt3.cs @@ -24,17 +24,17 @@ namespace ComputeSharp /// /// Gets an value with the component set to 1, and the others to 0. /// - public static UInt3 UnitX => new UInt3(1, 0, 0); + public static UInt3 UnitX => new(1, 0, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static UInt3 UnitY => new UInt3(0, 1, 0); + public static UInt3 UnitY => new(0, 1, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static UInt3 UnitZ => new UInt3(0, 0, 1); + public static UInt3 UnitZ => new(0, 0, 1); /// /// Creates a new instance with the specified parameters. @@ -242,32 +242,32 @@ namespace ComputeSharp /// /// Gets an value with the value for all components. /// - public UInt2 XXX => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(XXX)}"); + public UInt3 XXX => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(XXX)}"); /// /// Gets an value with the , and values. /// - public UInt2 XXY => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(XXY)}"); + public UInt3 XXY => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(XXY)}"); /// /// Gets an value with the , and values. /// - public UInt2 XXZ => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(XXZ)}"); + public UInt3 XXZ => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(XXZ)}"); /// /// Gets an value with the , and values. /// - public UInt2 XYX => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(XYX)}"); + public UInt3 XYX => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(XYX)}"); /// /// Gets an value with the , and values. /// - public UInt2 XYY => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(XYY)}"); + public UInt3 XYY => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(XYY)}"); /// /// Gets or sets an value with the , and values. /// - public UInt2 XYZ + public UInt3 XYZ { get => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(XYZ)}"); set => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(XYZ)}"); @@ -276,12 +276,12 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public UInt2 XZX => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(XZX)}"); + public UInt3 XZX => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(XZX)}"); /// /// Gets or sets an value with the , and values. /// - public UInt2 XZY + public UInt3 XZY { get => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(XZY)}"); set => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(XZY)}"); @@ -290,22 +290,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public UInt2 XZZ => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(XZZ)}"); + public UInt3 XZZ => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(XZZ)}"); /// /// Gets an value with the , and values. /// - public UInt2 YXX => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(YXX)}"); + public UInt3 YXX => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(YXX)}"); /// /// Gets an value with the , and values. /// - public UInt2 YXY => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(YXY)}"); + public UInt3 YXY => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(YXY)}"); /// /// Gets or sets an value with the , and values. /// - public UInt2 YXZ + public UInt3 YXZ { get => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(YXZ)}"); set => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(YXZ)}"); @@ -314,22 +314,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public UInt2 YYX => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(YYX)}"); + public UInt3 YYX => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(YYX)}"); /// /// Gets an value with the value for all components. /// - public UInt2 YYY => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(YYY)}"); + public UInt3 YYY => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(YYY)}"); /// /// Gets an value with the , and values. /// - public UInt2 YYZ => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(YYZ)}"); + public UInt3 YYZ => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(YYZ)}"); /// /// Gets or sets an value with the , and values. /// - public UInt2 YZX + public UInt3 YZX { get => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(YZX)}"); set => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(YZX)}"); @@ -338,22 +338,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public UInt2 YZY => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(YZY)}"); + public UInt3 YZY => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(YZY)}"); /// /// Gets an value with the , and values. /// - public UInt2 YZZ => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(YZZ)}"); + public UInt3 YZZ => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(YZZ)}"); /// /// Gets an value with the , and values. /// - public UInt2 ZXX => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(ZXX)}"); + public UInt3 ZXX => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(ZXX)}"); /// /// Gets or sets an value with the , and values. /// - public UInt2 ZXY + public UInt3 ZXY { get => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(ZXY)}"); set => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(ZXY)}"); @@ -362,12 +362,12 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public UInt2 ZXZ => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(ZXZ)}"); + public UInt3 ZXZ => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(ZXZ)}"); /// /// Gets or sets an value with the , and values. /// - public UInt2 ZYX + public UInt3 ZYX { get => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(ZYX)}"); set => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(ZYX)}"); @@ -376,57 +376,57 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public UInt2 ZYY => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(ZYY)}"); + public UInt3 ZYY => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(ZYY)}"); /// /// Gets an value with the , and values. /// - public UInt2 ZYZ => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(ZYZ)}"); + public UInt3 ZYZ => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(ZYZ)}"); /// /// Gets an value with the , and values. /// - public UInt2 ZZX => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(ZZX)}"); + public UInt3 ZZX => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(ZZX)}"); /// /// Gets an value with the , and values. /// - public UInt2 ZZY => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(ZZY)}"); + public UInt3 ZZY => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(ZZY)}"); /// /// Gets an value with the value for all components. /// - public UInt2 ZZZ => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(ZZZ)}"); + public UInt3 ZZZ => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(ZZZ)}"); /// /// Gets an value with the value for all components. /// - public UInt2 RRR => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(RRR)}"); + public UInt3 RRR => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(RRR)}"); /// /// Gets an value with the , and values. /// - public UInt2 RRG => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(RRG)}"); + public UInt3 RRG => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(RRG)}"); /// /// Gets an value with the , and values. /// - public UInt2 RRB => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(RRB)}"); + public UInt3 RRB => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(RRB)}"); /// /// Gets an value with the , and values. /// - public UInt2 RGR => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(RGR)}"); + public UInt3 RGR => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(RGR)}"); /// /// Gets an value with the , and values. /// - public UInt2 RGG => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(RGG)}"); + public UInt3 RGG => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(RGG)}"); /// /// Gets or sets an value with the , and values. /// - public UInt2 RGB + public UInt3 RGB { get => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(RGB)}"); set => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(RGB)}"); @@ -435,12 +435,12 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public UInt2 RBR => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(RBR)}"); + public UInt3 RBR => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(RBR)}"); /// /// Gets or sets an value with the , and values. /// - public UInt2 RBG + public UInt3 RBG { get => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(RBG)}"); set => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(RBG)}"); @@ -449,22 +449,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public UInt2 RBB => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(RBB)}"); + public UInt3 RBB => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(RBB)}"); /// /// Gets an value with the , and values. /// - public UInt2 GRR => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(GRR)}"); + public UInt3 GRR => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(GRR)}"); /// /// Gets an value with the , and values. /// - public UInt2 GRG => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(GRG)}"); + public UInt3 GRG => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(GRG)}"); /// /// Gets or sets an value with the , and values. /// - public UInt2 GRB + public UInt3 GRB { get => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(GRB)}"); set => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(GRB)}"); @@ -473,22 +473,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public UInt2 GGR => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(GGR)}"); + public UInt3 GGR => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(GGR)}"); /// /// Gets an value with the value for all components. /// - public UInt2 GGG => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(GGG)}"); + public UInt3 GGG => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(GGG)}"); /// /// Gets an value with the , and values. /// - public UInt2 GGB => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(GGB)}"); + public UInt3 GGB => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(GGB)}"); /// /// Gets or sets an value with the , and values. /// - public UInt2 GBR + public UInt3 GBR { get => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(GBR)}"); set => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(GBR)}"); @@ -497,22 +497,22 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public UInt2 GBG => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(GBG)}"); + public UInt3 GBG => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(GBG)}"); /// /// Gets an value with the , and values. /// - public UInt2 GBB => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(GBB)}"); + public UInt3 GBB => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(GBB)}"); /// /// Gets an value with the , and values. /// - public UInt2 BRR => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(BRR)}"); + public UInt3 BRR => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(BRR)}"); /// /// Gets or sets an value with the , and values. /// - public UInt2 BRG + public UInt3 BRG { get => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(BRG)}"); set => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(BRG)}"); @@ -521,12 +521,12 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public UInt2 BRB => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(BRB)}"); + public UInt3 BRB => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(BRB)}"); /// /// Gets or sets an value with the , and values. /// - public UInt2 BGR + public UInt3 BGR { get => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(BGR)}"); set => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(BGR)}"); @@ -535,33 +535,33 @@ namespace ComputeSharp /// /// Gets an value with the , and values. /// - public UInt2 BGG => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(BGG)}"); + public UInt3 BGG => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(BGG)}"); /// /// Gets an value with the , and values. /// - public UInt2 BGB => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(BGB)}"); + public UInt3 BGB => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(BGB)}"); /// /// Gets an value with the , and values. /// - public UInt2 BBR => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(BBR)}"); + public UInt3 BBR => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(BBR)}"); /// /// Gets an value with the , and values. /// - public UInt2 BBG => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(BBG)}"); + public UInt3 BBG => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(BBG)}"); /// /// Gets an value with the value for all components. /// - public UInt2 BBB => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(BBB)}"); + public UInt3 BBB => throw new InvalidExecutionContextException($"{nameof(UInt3)}.{nameof(BBB)}"); /// /// Creates a new value with the same value for all its components. /// /// The value to use for the components of the new instance. - public static implicit operator UInt3(uint x) => new UInt3(x, x, x); + public static implicit operator UInt3(uint x) => new(x, x, x); /// /// Sums two values. diff --git a/src/ComputeSharp.Core/Primitives/UInt/UInt4.cs b/src/ComputeSharp.Core/Primitives/UInt/UInt4.cs index 1d6f1ba9..6bfbf0fb 100644 --- a/src/ComputeSharp.Core/Primitives/UInt/UInt4.cs +++ b/src/ComputeSharp.Core/Primitives/UInt/UInt4.cs @@ -24,22 +24,22 @@ namespace ComputeSharp /// /// Gets an value with the component set to 1, and the others to 0. /// - public static UInt4 UnitX => new UInt4(1, 0, 0, 0); + public static UInt4 UnitX => new(1, 0, 0, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static UInt4 UnitY => new UInt4(0, 1, 0, 0); + public static UInt4 UnitY => new(0, 1, 0, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static UInt4 UnitZ => new UInt4(0, 0, 1, 0); + public static UInt4 UnitZ => new(0, 0, 1, 0); /// /// Gets an value with the component set to 1, and the others to 0. /// - public static UInt4 UnitW => new UInt4(0, 0, 0, 1); + public static UInt4 UnitW => new(0, 0, 0, 1); /// /// Creates a new instance with the specified parameters. @@ -3966,7 +3966,7 @@ namespace ComputeSharp /// Creates a new value with the same value for all its components. /// /// The value to use for the components of the new instance. - public static implicit operator UInt4(uint x) => new UInt4(x, x, x, x); + public static implicit operator UInt4(uint x) => new(x, x, x, x); /// /// Sums two values. From 04e3dbc316b8e7a0affac704ff5f3cc94c29b886 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 7 Dec 2020 16:43:03 +0100 Subject: [PATCH 34/36] Added resource size validation --- .../Buffers/Abstract/Buffer{T}.cs | 11 ++++++++++- .../Buffers/Abstract/Texture2D{T}.cs | 5 +++-- .../Buffers/Abstract/Texture3D{T}.cs | 7 ++++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs index a7b5c7f9..6d1cb6ba 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs @@ -7,6 +7,7 @@ using ComputeSharp.Graphics.Buffers.Enums; using ComputeSharp.Graphics.Extensions; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; +using FX = TerraFX.Interop.Windows; namespace ComputeSharp.Graphics.Buffers.Abstract { @@ -44,7 +45,15 @@ namespace ComputeSharp.Graphics.Buffers.Abstract { device.ThrowIfDisposed(); - Guard.IsGreaterThan(length, 0, nameof(length)); + if (resourceType == ResourceType.Constant) + { + Guard.IsBetweenOrEqualTo(length, 1, FX.D3D12_REQ_CONSTANT_BUFFER_ELEMENT_COUNT, nameof(length)); + } + else + { + // The maximum length is set such that the aligned buffer size can't exceed uint.MaxValue + Guard.IsBetweenOrEqualTo(length, 1, (uint.MaxValue / elementSizeInBytes) & ~255, nameof(length)); + } SizeInBytes = checked((nint)(length * elementSizeInBytes)); GraphicsDevice = device; diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index dfd28d3f..2f00b7dd 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -12,6 +12,7 @@ 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 { @@ -54,8 +55,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract { device.ThrowIfDisposed(); - Guard.IsGreaterThan(width, 0, nameof(width)); - Guard.IsGreaterThan(height, 0, nameof(height)); + 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)); GraphicsDevice = device; Width = width; diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index 837d6f55..12599cdc 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -12,6 +12,7 @@ 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 { @@ -55,9 +56,9 @@ namespace ComputeSharp.Graphics.Buffers.Abstract { device.ThrowIfDisposed(); - Guard.IsGreaterThan(width, 0, nameof(width)); - Guard.IsGreaterThan(height, 0, nameof(height)); - Guard.IsBetweenOrEqualTo(depth, 1, ushort.MaxValue, nameof(depth)); + Guard.IsBetweenOrEqualTo(width, 1, FX.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION, nameof(width)); + Guard.IsBetweenOrEqualTo(height, 1, FX.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION, nameof(height)); + Guard.IsBetweenOrEqualTo(depth, 1, FX.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION, nameof(depth)); GraphicsDevice = device; Width = width; From c262cd5519375e5b980b18c8c92dab24ae96555d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 7 Dec 2020 17:08:14 +0100 Subject: [PATCH 35/36] Added single-element indexers for texture types --- .../Buffers/ReadOnlyTexture2D{T}.cs | 8 ++++++++ .../Buffers/ReadOnlyTexture3D{T}.cs | 9 +++++++++ .../Buffers/ReadWriteBuffer{T}.cs | 2 +- .../Buffers/ReadWriteTexture2D{T}.cs | 12 ++++++++++-- .../Buffers/ReadWriteTexture3D{T}.cs | 11 ++++++++++- 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{T}.cs index c2d93771..36b930ef 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{T}.cs @@ -27,6 +27,14 @@ namespace ComputeSharp { } + /// + /// Gets a single value from the current readonly texture. + /// + /// The horizontal offset of the value to get. + /// The vertical offset of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public T this[int x, int y] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture2D)}[int,int]"); + /// /// Gets a single value from the current readonly texture. /// diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs index 26bf0c7d..306b0d19 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T}.cs @@ -28,6 +28,15 @@ namespace ComputeSharp { } + /// + /// Gets a single value from the current readonly texture. + /// + /// The horizontal offset of the value to get. + /// The vertical offset of the value to get. + /// The depthwise offset of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public T this[int x, int y, int z] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture3D)}[int,int,int]"); + /// /// Gets a single value from the current readonly texture. /// diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs index a2f2c0c2..28c65443 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs @@ -27,7 +27,7 @@ namespace ComputeSharp } /// - /// Gets or sets a single value from the current read write buffer. + /// Gets or sets a single value from the current writeable buffer. /// /// The index of the value to get or set. /// This API can only be used from a compute shader, and will always throw if used anywhere else. diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture2D{T}.cs index 925be95d..1b75ce5b 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture2D{T}.cs @@ -28,11 +28,19 @@ namespace ComputeSharp } /// - /// Gets or sets a single value from the current readonly texture. + /// Gets a single value from the current writeable texture. + /// + /// The horizontal offset of the value to get. + /// The vertical offset of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public ref T this[int x, int y] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture2D)}[int,int]"); + + /// + /// Gets or sets a single value from the current writeable texture. /// /// The coordinates of the value to get. /// This API can only be used from a compute shader, and will always throw if used anywhere else. - public ref T this[Int2 xy] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture2D)}[Int2]"); + public ref T this[Int2 xy] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture2D)}[Int2]"); /// public override string ToString() diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs index ee042fa4..8ee45602 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T}.cs @@ -29,7 +29,16 @@ namespace ComputeSharp } /// - /// Gets a single value from the current readonly texture. + /// Gets a single value from the current writeable texture. + /// + /// The horizontal offset of the value to get. + /// The vertical offset of the value to get. + /// The depthwise offset of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public ref T this[int x, int y, int z] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture3D)}[int,int,int]"); + + /// + /// Gets a single value from the current writeable texture. /// /// The coordinates of the value to get. /// This API can only be used from a compute shader, and will always throw if used anywhere else. From c208dc29b18baf5dfbcb1cb77db13d2f6c34c673 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 7 Dec 2020 17:51:50 +0100 Subject: [PATCH 36/36] Enabled shader generation for texture indexer overloads --- .../Extensions/ISymbolExtensions.cs | 20 ++++++++++++++++ .../Mappings/HlslKnownMembers.cs | 23 +++++++++++++++++++ .../Mappings/HlslKnownTypes.cs | 2 +- .../SyntaxRewriters/ShaderSourceRewriter.cs | 16 +++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs index d1581c61..e08f19e8 100644 --- a/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs @@ -74,6 +74,26 @@ namespace ComputeSharp.SourceGenerators.Extensions return symbol.ToDisplayString(FullyQualifiedWithoutGlobalAndParametersFormat); } + /// + /// Gets the full metadata name for a given instance. + /// + /// The input instance. + /// The full metadata name for . + [Pure] + public static string GetFullMetadataName(this IPropertySymbol symbol) + { + string declaringTypeName = symbol.ContainingType.GetFullMetadataName(); + + if (symbol.IsIndexer) + { + var parameters = string.Join(", ", symbol.Parameters.Select(static p => ((INamedTypeSymbol)p.Type).GetFullMetadataName())); + + return $"{declaringTypeName}.this[{parameters}]"; + } + + return $"{declaringTypeName}.{symbol.Name}"; + } + /// /// Gets a valid filename for a target symbol and generator type. /// diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs index 2ac0c8bf..7a7df32b 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs @@ -12,6 +12,17 @@ namespace ComputeSharp.SourceGenerators.Mappings /// internal static class HlslKnownMembers { + /// + /// The mapping of supported known indexers to HLSL vector type names. + /// + private static readonly IReadOnlyDictionary KnownIndexers = new Dictionary + { + [$"ComputeSharp.ReadOnlyTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2", + [$"ComputeSharp.ReadWriteTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2", + [$"ComputeSharp.ReadOnlyTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3", + [$"ComputeSharp.ReadWriteTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3" + }; + /// /// The mapping of supported known members to HLSL names. /// @@ -162,5 +173,17 @@ namespace ComputeSharp.SourceGenerators.Mappings { return KnownMembers.TryGetValue(name, out mapped); } + + /// + /// Tries to get the mapped HLSL-compatible indexer vector type name for the input indexer name. + /// + /// The input fully qualified indexer name. + /// The mapped type name, if one is found. + /// The HLSL-compatible type name that can be used in an HLSL shader for the given indexer. + [Pure] + public static bool TryGetMappedIndexerTypeName(string name, out string? mapped) + { + return KnownIndexers.TryGetValue(name, out mapped); + } } } diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs index 2d2b0f32..b4332b8e 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -41,7 +41,7 @@ namespace ComputeSharp.SourceGenerators.Mappings [typeof(Double2).FullName] = "double2", [typeof(Double3).FullName] = "double3", [typeof(Double4).FullName] = "double4", - [typeof(ThreadIds).FullName] = "uint3" + [typeof(ThreadIds).FullName] = "int3" }; /// diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 3cc8a414..0c274f7a 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -342,6 +342,22 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters return updatedNode; } + /// + public override SyntaxNode? VisitElementAccessExpression(ElementAccessExpressionSyntax node) + { + var updatedNode = (ElementAccessExpressionSyntax)base.VisitElementAccessExpression(node)!; + + if (this.semanticModel.GetOperation(node) is IPropertyReferenceOperation operation && + HlslKnownMembers.TryGetMappedIndexerTypeName(operation.Property.GetFullMetadataName(), out string? mapping)) + { + var index = InvocationExpression(IdentifierName(mapping!), ArgumentList(updatedNode.ArgumentList.Arguments)); + + return updatedNode.WithArgumentList(BracketedArgumentList(SingletonSeparatedList(Argument(index)))); + } + + return updatedNode; + } + /// public override SyntaxNode? VisitArgument(ArgumentSyntax node) {