From ac99a2cad6a4220645c0afb0b58d1d3644dd9b8c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 02:16:32 +0200 Subject: [PATCH 01/42] Removed unnecessary leftover class --- .../ShaderGenerationResult.cs | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 src/DirectX12GameEngine.Shaders/ShaderGenerationResult.cs diff --git a/src/DirectX12GameEngine.Shaders/ShaderGenerationResult.cs b/src/DirectX12GameEngine.Shaders/ShaderGenerationResult.cs deleted file mode 100644 index 84cb358a..00000000 --- a/src/DirectX12GameEngine.Shaders/ShaderGenerationResult.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Reflection; - -namespace DirectX12GameEngine.Shaders -{ - public class ShaderGenerationResult - { - public ShaderGenerationResult(string shaderSource, string computeShader) - { - ShaderSource = shaderSource; - ComputeShader = computeShader; - } - - public string ShaderSource { get; } - - public string ComputeShader { get; set; } - } -} From de864f2875284d52c62cc72fed5b284e73edf744 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 02:23:26 +0200 Subject: [PATCH 02/42] Renamed main project, minor API changes --- src/ComputeSharp.sln | 4 ++-- .../ComputeSharp.csproj} | 0 .../Extensions/PointExtensions.cs | 0 .../Extensions/RectExtensions.cs | 0 .../Extensions/SizeExtensions.cs | 0 src/DirectX12GameEngine.Graphics/Buffers/Buffer.cs | 2 +- .../DirectX12GameEngine.Graphics.csproj | 2 +- .../Helpers/MemoryHelper.cs | 2 +- .../DirectX12GameEngine.Shaders.csproj | 2 +- 9 files changed, 6 insertions(+), 6 deletions(-) rename src/{DirectX12GameEngine.Core/DirectX12GameEngine.Core.csproj => ComputeSharp/ComputeSharp.csproj} (100%) rename src/{DirectX12GameEngine.Core => ComputeSharp}/Extensions/PointExtensions.cs (100%) rename src/{DirectX12GameEngine.Core => ComputeSharp}/Extensions/RectExtensions.cs (100%) rename src/{DirectX12GameEngine.Core => ComputeSharp}/Extensions/SizeExtensions.cs (100%) rename src/{DirectX12GameEngine.Core => DirectX12GameEngine.Graphics}/Helpers/MemoryHelper.cs (97%) diff --git a/src/ComputeSharp.sln b/src/ComputeSharp.sln index e28fc3f4..08b4bd61 100644 --- a/src/ComputeSharp.sln +++ b/src/ComputeSharp.sln @@ -7,9 +7,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectX12GameEngine.Graphic EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectX12GameEngine.Shaders", "DirectX12GameEngine.Shaders\DirectX12GameEngine.Shaders.csproj", "{23AE6EED-79CD-4ADD-9A85-0148BD957D19}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectX12GameEngine.Core", "DirectX12GameEngine.Core\DirectX12GameEngine.Core.csproj", "{314EB353-E7E3-4146-8111-685CC83EC1DE}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp", "ComputeSharp\ComputeSharp.csproj", "{314EB353-E7E3-4146-8111-685CC83EC1DE}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DirectX12ComputeShaderSample", "DirectX12ComputeShaderSample\DirectX12ComputeShaderSample.csproj", "{7A48A85D-8866-4DED-A404-653ED09BF396}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectX12ComputeShaderSample", "DirectX12ComputeShaderSample\DirectX12ComputeShaderSample.csproj", "{7A48A85D-8866-4DED-A404-653ED09BF396}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/DirectX12GameEngine.Core/DirectX12GameEngine.Core.csproj b/src/ComputeSharp/ComputeSharp.csproj similarity index 100% rename from src/DirectX12GameEngine.Core/DirectX12GameEngine.Core.csproj rename to src/ComputeSharp/ComputeSharp.csproj diff --git a/src/DirectX12GameEngine.Core/Extensions/PointExtensions.cs b/src/ComputeSharp/Extensions/PointExtensions.cs similarity index 100% rename from src/DirectX12GameEngine.Core/Extensions/PointExtensions.cs rename to src/ComputeSharp/Extensions/PointExtensions.cs diff --git a/src/DirectX12GameEngine.Core/Extensions/RectExtensions.cs b/src/ComputeSharp/Extensions/RectExtensions.cs similarity index 100% rename from src/DirectX12GameEngine.Core/Extensions/RectExtensions.cs rename to src/ComputeSharp/Extensions/RectExtensions.cs diff --git a/src/DirectX12GameEngine.Core/Extensions/SizeExtensions.cs b/src/ComputeSharp/Extensions/SizeExtensions.cs similarity index 100% rename from src/DirectX12GameEngine.Core/Extensions/SizeExtensions.cs rename to src/ComputeSharp/Extensions/SizeExtensions.cs diff --git a/src/DirectX12GameEngine.Graphics/Buffers/Buffer.cs b/src/DirectX12GameEngine.Graphics/Buffers/Buffer.cs index db4052e0..2a4a4e87 100644 --- a/src/DirectX12GameEngine.Graphics/Buffers/Buffer.cs +++ b/src/DirectX12GameEngine.Graphics/Buffers/Buffer.cs @@ -1,7 +1,7 @@ using System; using System.Runtime.CompilerServices; -using DirectX12GameEngine.Core.Helpers; using DirectX12GameEngine.Graphics.Buffers.Abstract; +using DirectX12GameEngine.Graphics.Helpers; using SharpDX.Direct3D12; namespace DirectX12GameEngine.Graphics.Buffers diff --git a/src/DirectX12GameEngine.Graphics/DirectX12GameEngine.Graphics.csproj b/src/DirectX12GameEngine.Graphics/DirectX12GameEngine.Graphics.csproj index e2b2653d..5157ea8f 100644 --- a/src/DirectX12GameEngine.Graphics/DirectX12GameEngine.Graphics.csproj +++ b/src/DirectX12GameEngine.Graphics/DirectX12GameEngine.Graphics.csproj @@ -12,7 +12,7 @@ - + diff --git a/src/DirectX12GameEngine.Core/Helpers/MemoryHelper.cs b/src/DirectX12GameEngine.Graphics/Helpers/MemoryHelper.cs similarity index 97% rename from src/DirectX12GameEngine.Core/Helpers/MemoryHelper.cs rename to src/DirectX12GameEngine.Graphics/Helpers/MemoryHelper.cs index 559eabee..b7b01f42 100644 --- a/src/DirectX12GameEngine.Core/Helpers/MemoryHelper.cs +++ b/src/DirectX12GameEngine.Graphics/Helpers/MemoryHelper.cs @@ -1,7 +1,7 @@ using System; using System.Runtime.CompilerServices; -namespace DirectX12GameEngine.Core.Helpers +namespace DirectX12GameEngine.Graphics.Helpers { /// /// A with some helper methods to copy data between memory areas diff --git a/src/DirectX12GameEngine.Shaders/DirectX12GameEngine.Shaders.csproj b/src/DirectX12GameEngine.Shaders/DirectX12GameEngine.Shaders.csproj index 936e6672..9ef31fbd 100644 --- a/src/DirectX12GameEngine.Shaders/DirectX12GameEngine.Shaders.csproj +++ b/src/DirectX12GameEngine.Shaders/DirectX12GameEngine.Shaders.csproj @@ -18,7 +18,7 @@ - + From 6571390ef32333f3c900ea6623c7f4b39572587f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 02:31:36 +0200 Subject: [PATCH 03/42] Graphics project renamed, dependencies adjusted --- .../BufferDescription.cs | 0 .../BufferFlags.cs | 0 .../Buffers/Abstract/GraphicsResource.cs | 0 .../Buffers/Buffer.Constant.cs | 0 .../Buffers/Buffer.UnorderedAccess.cs | 0 .../Buffers/Buffer.cs | 0 .../CommandAllocatorPool.cs | 0 .../CommandList.cs | 0 .../CompiledCommandList.cs | 0 .../ComputeSharp.Graphics.csproj} | 8 ++++---- .../DescriptorAllocator.cs | 0 .../GraphicsDevice.cs | 0 .../GraphicsResourceUsage.cs | 0 .../Helpers/MemoryHelper.cs | 0 .../PipelineState.cs | 0 src/ComputeSharp.sln | 2 +- .../DirectX12ComputeShaderSample.csproj | 2 +- .../DirectX12GameEngine.Shaders.csproj | 3 +-- 18 files changed, 7 insertions(+), 8 deletions(-) rename src/{DirectX12GameEngine.Graphics => ComputeSharp.Graphics}/BufferDescription.cs (100%) rename src/{DirectX12GameEngine.Graphics => ComputeSharp.Graphics}/BufferFlags.cs (100%) rename src/{DirectX12GameEngine.Graphics => ComputeSharp.Graphics}/Buffers/Abstract/GraphicsResource.cs (100%) rename src/{DirectX12GameEngine.Graphics => ComputeSharp.Graphics}/Buffers/Buffer.Constant.cs (100%) rename src/{DirectX12GameEngine.Graphics => ComputeSharp.Graphics}/Buffers/Buffer.UnorderedAccess.cs (100%) rename src/{DirectX12GameEngine.Graphics => ComputeSharp.Graphics}/Buffers/Buffer.cs (100%) rename src/{DirectX12GameEngine.Graphics => ComputeSharp.Graphics}/CommandAllocatorPool.cs (100%) rename src/{DirectX12GameEngine.Graphics => ComputeSharp.Graphics}/CommandList.cs (100%) rename src/{DirectX12GameEngine.Graphics => ComputeSharp.Graphics}/CompiledCommandList.cs (100%) rename src/{DirectX12GameEngine.Graphics/DirectX12GameEngine.Graphics.csproj => ComputeSharp.Graphics/ComputeSharp.Graphics.csproj} (51%) rename src/{DirectX12GameEngine.Graphics => ComputeSharp.Graphics}/DescriptorAllocator.cs (100%) rename src/{DirectX12GameEngine.Graphics => ComputeSharp.Graphics}/GraphicsDevice.cs (100%) rename src/{DirectX12GameEngine.Graphics => ComputeSharp.Graphics}/GraphicsResourceUsage.cs (100%) rename src/{DirectX12GameEngine.Graphics => ComputeSharp.Graphics}/Helpers/MemoryHelper.cs (100%) rename src/{DirectX12GameEngine.Graphics => ComputeSharp.Graphics}/PipelineState.cs (100%) diff --git a/src/DirectX12GameEngine.Graphics/BufferDescription.cs b/src/ComputeSharp.Graphics/BufferDescription.cs similarity index 100% rename from src/DirectX12GameEngine.Graphics/BufferDescription.cs rename to src/ComputeSharp.Graphics/BufferDescription.cs diff --git a/src/DirectX12GameEngine.Graphics/BufferFlags.cs b/src/ComputeSharp.Graphics/BufferFlags.cs similarity index 100% rename from src/DirectX12GameEngine.Graphics/BufferFlags.cs rename to src/ComputeSharp.Graphics/BufferFlags.cs diff --git a/src/DirectX12GameEngine.Graphics/Buffers/Abstract/GraphicsResource.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource.cs similarity index 100% rename from src/DirectX12GameEngine.Graphics/Buffers/Abstract/GraphicsResource.cs rename to src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource.cs diff --git a/src/DirectX12GameEngine.Graphics/Buffers/Buffer.Constant.cs b/src/ComputeSharp.Graphics/Buffers/Buffer.Constant.cs similarity index 100% rename from src/DirectX12GameEngine.Graphics/Buffers/Buffer.Constant.cs rename to src/ComputeSharp.Graphics/Buffers/Buffer.Constant.cs diff --git a/src/DirectX12GameEngine.Graphics/Buffers/Buffer.UnorderedAccess.cs b/src/ComputeSharp.Graphics/Buffers/Buffer.UnorderedAccess.cs similarity index 100% rename from src/DirectX12GameEngine.Graphics/Buffers/Buffer.UnorderedAccess.cs rename to src/ComputeSharp.Graphics/Buffers/Buffer.UnorderedAccess.cs diff --git a/src/DirectX12GameEngine.Graphics/Buffers/Buffer.cs b/src/ComputeSharp.Graphics/Buffers/Buffer.cs similarity index 100% rename from src/DirectX12GameEngine.Graphics/Buffers/Buffer.cs rename to src/ComputeSharp.Graphics/Buffers/Buffer.cs diff --git a/src/DirectX12GameEngine.Graphics/CommandAllocatorPool.cs b/src/ComputeSharp.Graphics/CommandAllocatorPool.cs similarity index 100% rename from src/DirectX12GameEngine.Graphics/CommandAllocatorPool.cs rename to src/ComputeSharp.Graphics/CommandAllocatorPool.cs diff --git a/src/DirectX12GameEngine.Graphics/CommandList.cs b/src/ComputeSharp.Graphics/CommandList.cs similarity index 100% rename from src/DirectX12GameEngine.Graphics/CommandList.cs rename to src/ComputeSharp.Graphics/CommandList.cs diff --git a/src/DirectX12GameEngine.Graphics/CompiledCommandList.cs b/src/ComputeSharp.Graphics/CompiledCommandList.cs similarity index 100% rename from src/DirectX12GameEngine.Graphics/CompiledCommandList.cs rename to src/ComputeSharp.Graphics/CompiledCommandList.cs diff --git a/src/DirectX12GameEngine.Graphics/DirectX12GameEngine.Graphics.csproj b/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj similarity index 51% rename from src/DirectX12GameEngine.Graphics/DirectX12GameEngine.Graphics.csproj rename to src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj index 5157ea8f..497079a7 100644 --- a/src/DirectX12GameEngine.Graphics/DirectX12GameEngine.Graphics.csproj +++ b/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj @@ -8,11 +8,11 @@ + - - - - + + + diff --git a/src/DirectX12GameEngine.Graphics/DescriptorAllocator.cs b/src/ComputeSharp.Graphics/DescriptorAllocator.cs similarity index 100% rename from src/DirectX12GameEngine.Graphics/DescriptorAllocator.cs rename to src/ComputeSharp.Graphics/DescriptorAllocator.cs diff --git a/src/DirectX12GameEngine.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs similarity index 100% rename from src/DirectX12GameEngine.Graphics/GraphicsDevice.cs rename to src/ComputeSharp.Graphics/GraphicsDevice.cs diff --git a/src/DirectX12GameEngine.Graphics/GraphicsResourceUsage.cs b/src/ComputeSharp.Graphics/GraphicsResourceUsage.cs similarity index 100% rename from src/DirectX12GameEngine.Graphics/GraphicsResourceUsage.cs rename to src/ComputeSharp.Graphics/GraphicsResourceUsage.cs diff --git a/src/DirectX12GameEngine.Graphics/Helpers/MemoryHelper.cs b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs similarity index 100% rename from src/DirectX12GameEngine.Graphics/Helpers/MemoryHelper.cs rename to src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs diff --git a/src/DirectX12GameEngine.Graphics/PipelineState.cs b/src/ComputeSharp.Graphics/PipelineState.cs similarity index 100% rename from src/DirectX12GameEngine.Graphics/PipelineState.cs rename to src/ComputeSharp.Graphics/PipelineState.cs diff --git a/src/ComputeSharp.sln b/src/ComputeSharp.sln index 08b4bd61..a5d6dea6 100644 --- a/src/ComputeSharp.sln +++ b/src/ComputeSharp.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.29201.188 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectX12GameEngine.Graphics", "DirectX12GameEngine.Graphics\DirectX12GameEngine.Graphics.csproj", "{2499CC74-BC9A-4B30-A3B6-05F9C7BF9B34}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Graphics", "ComputeSharp.Graphics\ComputeSharp.Graphics.csproj", "{2499CC74-BC9A-4B30-A3B6-05F9C7BF9B34}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectX12GameEngine.Shaders", "DirectX12GameEngine.Shaders\DirectX12GameEngine.Shaders.csproj", "{23AE6EED-79CD-4ADD-9A85-0148BD957D19}" EndProject diff --git a/src/DirectX12ComputeShaderSample/DirectX12ComputeShaderSample.csproj b/src/DirectX12ComputeShaderSample/DirectX12ComputeShaderSample.csproj index 0823373d..bb64e8b0 100644 --- a/src/DirectX12ComputeShaderSample/DirectX12ComputeShaderSample.csproj +++ b/src/DirectX12ComputeShaderSample/DirectX12ComputeShaderSample.csproj @@ -6,7 +6,7 @@ - + diff --git a/src/DirectX12GameEngine.Shaders/DirectX12GameEngine.Shaders.csproj b/src/DirectX12GameEngine.Shaders/DirectX12GameEngine.Shaders.csproj index 9ef31fbd..1916128f 100644 --- a/src/DirectX12GameEngine.Shaders/DirectX12GameEngine.Shaders.csproj +++ b/src/DirectX12GameEngine.Shaders/DirectX12GameEngine.Shaders.csproj @@ -18,8 +18,7 @@ - - + From c178dd64e04f51a6360625a5a8dc930a3a5d6429 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 02:32:27 +0200 Subject: [PATCH 04/42] Adjusted namespaces in graphics project --- src/ComputeSharp.Graphics/BufferDescription.cs | 2 +- src/ComputeSharp.Graphics/BufferFlags.cs | 2 +- .../Buffers/Abstract/GraphicsResource.cs | 2 +- src/ComputeSharp.Graphics/Buffers/Buffer.Constant.cs | 2 +- src/ComputeSharp.Graphics/Buffers/Buffer.UnorderedAccess.cs | 2 +- src/ComputeSharp.Graphics/Buffers/Buffer.cs | 6 +++--- src/ComputeSharp.Graphics/CommandAllocatorPool.cs | 2 +- src/ComputeSharp.Graphics/CommandList.cs | 4 ++-- src/ComputeSharp.Graphics/CompiledCommandList.cs | 2 +- src/ComputeSharp.Graphics/DescriptorAllocator.cs | 2 +- src/ComputeSharp.Graphics/GraphicsDevice.cs | 2 +- src/ComputeSharp.Graphics/GraphicsResourceUsage.cs | 2 +- src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs | 2 +- src/ComputeSharp.Graphics/PipelineState.cs | 2 +- src/DirectX12ComputeShaderSample/Gpu.cs | 6 +++--- src/DirectX12ComputeShaderSample/Program.cs | 4 ++-- src/DirectX12GameEngine.Shaders/ShaderLoader.cs | 2 +- src/DirectX12GameEngine.Shaders/ShaderResource.cs | 2 +- 18 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/ComputeSharp.Graphics/BufferDescription.cs b/src/ComputeSharp.Graphics/BufferDescription.cs index 89159df8..efc94f66 100644 --- a/src/ComputeSharp.Graphics/BufferDescription.cs +++ b/src/ComputeSharp.Graphics/BufferDescription.cs @@ -1,4 +1,4 @@ -namespace DirectX12GameEngine.Graphics +namespace ComputeSharp.Graphics { public struct BufferDescription { diff --git a/src/ComputeSharp.Graphics/BufferFlags.cs b/src/ComputeSharp.Graphics/BufferFlags.cs index 1c0a26b5..ce2b64ed 100644 --- a/src/ComputeSharp.Graphics/BufferFlags.cs +++ b/src/ComputeSharp.Graphics/BufferFlags.cs @@ -1,6 +1,6 @@ using System; -namespace DirectX12GameEngine.Graphics +namespace ComputeSharp.Graphics { [Flags] public enum BufferFlags diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource.cs index 1ffa6721..e8318b60 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource.cs @@ -1,7 +1,7 @@ using System; using SharpDX.Direct3D12; -namespace DirectX12GameEngine.Graphics.Buffers.Abstract +namespace ComputeSharp.Graphics.Buffers.Abstract { /// /// An abstract that represents a graphics resource object diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer.Constant.cs b/src/ComputeSharp.Graphics/Buffers/Buffer.Constant.cs index 7745734e..b1081bc6 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer.Constant.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer.Constant.cs @@ -1,6 +1,6 @@ using System; -namespace DirectX12GameEngine.Graphics.Buffers +namespace ComputeSharp.Graphics.Buffers { public partial class Buffer { diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer.UnorderedAccess.cs b/src/ComputeSharp.Graphics/Buffers/Buffer.UnorderedAccess.cs index 20ec576d..44d2aca3 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer.UnorderedAccess.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer.UnorderedAccess.cs @@ -1,6 +1,6 @@ using System; -namespace DirectX12GameEngine.Graphics.Buffers +namespace ComputeSharp.Graphics.Buffers { public partial class Buffer { diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer.cs b/src/ComputeSharp.Graphics/Buffers/Buffer.cs index 2a4a4e87..9b8c2728 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer.cs @@ -1,10 +1,10 @@ using System; using System.Runtime.CompilerServices; -using DirectX12GameEngine.Graphics.Buffers.Abstract; -using DirectX12GameEngine.Graphics.Helpers; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Helpers; using SharpDX.Direct3D12; -namespace DirectX12GameEngine.Graphics.Buffers +namespace ComputeSharp.Graphics.Buffers { public partial class Buffer : GraphicsResource { diff --git a/src/ComputeSharp.Graphics/CommandAllocatorPool.cs b/src/ComputeSharp.Graphics/CommandAllocatorPool.cs index 1c130675..a14dccd0 100644 --- a/src/ComputeSharp.Graphics/CommandAllocatorPool.cs +++ b/src/ComputeSharp.Graphics/CommandAllocatorPool.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using SharpDX.Direct3D12; -namespace DirectX12GameEngine.Graphics +namespace ComputeSharp.Graphics { internal sealed class CommandAllocatorPool : IDisposable { diff --git a/src/ComputeSharp.Graphics/CommandList.cs b/src/ComputeSharp.Graphics/CommandList.cs index 341aa967..99702716 100644 --- a/src/ComputeSharp.Graphics/CommandList.cs +++ b/src/ComputeSharp.Graphics/CommandList.cs @@ -1,8 +1,8 @@ using System; -using DirectX12GameEngine.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Buffers.Abstract; using SharpDX.Direct3D12; -namespace DirectX12GameEngine.Graphics +namespace ComputeSharp.Graphics { public sealed class CommandList : IDisposable { diff --git a/src/ComputeSharp.Graphics/CompiledCommandList.cs b/src/ComputeSharp.Graphics/CompiledCommandList.cs index a4bc4f07..57e2a7ff 100644 --- a/src/ComputeSharp.Graphics/CompiledCommandList.cs +++ b/src/ComputeSharp.Graphics/CompiledCommandList.cs @@ -1,6 +1,6 @@ using SharpDX.Direct3D12; -namespace DirectX12GameEngine.Graphics +namespace ComputeSharp.Graphics { public sealed class CompiledCommandList { diff --git a/src/ComputeSharp.Graphics/DescriptorAllocator.cs b/src/ComputeSharp.Graphics/DescriptorAllocator.cs index ff795e41..5a041bf2 100644 --- a/src/ComputeSharp.Graphics/DescriptorAllocator.cs +++ b/src/ComputeSharp.Graphics/DescriptorAllocator.cs @@ -1,7 +1,7 @@ using System; using SharpDX.Direct3D12; -namespace DirectX12GameEngine.Graphics +namespace ComputeSharp.Graphics { internal sealed class DescriptorAllocator : IDisposable { diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs index 42be36df..893b4a2c 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs @@ -3,7 +3,7 @@ using System.Threading; using SharpDX.Direct3D; using SharpDX.Direct3D12; -namespace DirectX12GameEngine.Graphics +namespace ComputeSharp.Graphics { public sealed class GraphicsDevice : IDisposable { diff --git a/src/ComputeSharp.Graphics/GraphicsResourceUsage.cs b/src/ComputeSharp.Graphics/GraphicsResourceUsage.cs index b4299070..64448684 100644 --- a/src/ComputeSharp.Graphics/GraphicsResourceUsage.cs +++ b/src/ComputeSharp.Graphics/GraphicsResourceUsage.cs @@ -1,4 +1,4 @@ -namespace DirectX12GameEngine.Graphics +namespace ComputeSharp.Graphics { public enum GraphicsHeapType { diff --git a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs index b7b01f42..6e3a516b 100644 --- a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs @@ -1,7 +1,7 @@ using System; using System.Runtime.CompilerServices; -namespace DirectX12GameEngine.Graphics.Helpers +namespace ComputeSharp.Graphics.Helpers { /// /// A with some helper methods to copy data between memory areas diff --git a/src/ComputeSharp.Graphics/PipelineState.cs b/src/ComputeSharp.Graphics/PipelineState.cs index c74c8967..c7dcdc5e 100644 --- a/src/ComputeSharp.Graphics/PipelineState.cs +++ b/src/ComputeSharp.Graphics/PipelineState.cs @@ -1,6 +1,6 @@ using SharpDX.Direct3D12; -namespace DirectX12GameEngine.Graphics +namespace ComputeSharp.Graphics { /// /// A representing a custom pipeline state for a compute operation diff --git a/src/DirectX12ComputeShaderSample/Gpu.cs b/src/DirectX12ComputeShaderSample/Gpu.cs index 65f9f084..81a27c03 100644 --- a/src/DirectX12ComputeShaderSample/Gpu.cs +++ b/src/DirectX12ComputeShaderSample/Gpu.cs @@ -1,13 +1,13 @@ using System; using System.Linq; -using DirectX12GameEngine.Graphics; +using ComputeSharp.Graphics; using DirectX12GameEngine.Shaders; using DirectX12GameEngine.Shaders.Primitives; using DirectX12GameEngine.Shaders.Renderer; using DirectX12GameEngine.Shaders.Renderer.Models; using SharpDX.Direct3D12; -using CommandList = DirectX12GameEngine.Graphics.CommandList; -using PipelineState = DirectX12GameEngine.Graphics.PipelineState; +using CommandList = ComputeSharp.Graphics.CommandList; +using PipelineState = ComputeSharp.Graphics.PipelineState; using ShaderBytecode = SharpDX.Direct3D12.ShaderBytecode; namespace DirectX12ComputeShaderSample diff --git a/src/DirectX12ComputeShaderSample/Program.cs b/src/DirectX12ComputeShaderSample/Program.cs index 48be6ecd..8f96bd63 100644 --- a/src/DirectX12ComputeShaderSample/Program.cs +++ b/src/DirectX12ComputeShaderSample/Program.cs @@ -1,9 +1,9 @@ using DirectX12GameEngine.Shaders; using System; using System.Linq; -using DirectX12GameEngine.Graphics.Buffers; +using ComputeSharp.Graphics.Buffers; using DirectX12GameEngine.Shaders.Primitives; -using Buffer = DirectX12GameEngine.Graphics.Buffers.Buffer; +using Buffer = ComputeSharp.Graphics.Buffers.Buffer; namespace DirectX12ComputeShaderSample { diff --git a/src/DirectX12GameEngine.Shaders/ShaderLoader.cs b/src/DirectX12GameEngine.Shaders/ShaderLoader.cs index 4f49fe48..2fac6a03 100644 --- a/src/DirectX12GameEngine.Shaders/ShaderLoader.cs +++ b/src/DirectX12GameEngine.Shaders/ShaderLoader.cs @@ -4,7 +4,7 @@ using System.Diagnostics.Contracts; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; -using DirectX12GameEngine.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Buffers.Abstract; using DirectX12GameEngine.Shaders.Mappings; using DirectX12GameEngine.Shaders.Primitives; using DirectX12GameEngine.Shaders.Renderer.Models.Abstract; diff --git a/src/DirectX12GameEngine.Shaders/ShaderResource.cs b/src/DirectX12GameEngine.Shaders/ShaderResource.cs index 2cf469ec..6d90e986 100644 --- a/src/DirectX12GameEngine.Shaders/ShaderResource.cs +++ b/src/DirectX12GameEngine.Shaders/ShaderResource.cs @@ -1,6 +1,6 @@ using System; using System.Numerics; -using DirectX12GameEngine.Graphics.Buffers; +using ComputeSharp.Graphics.Buffers; using Buffer = System.Buffer; namespace DirectX12GameEngine.Shaders From 031dd213b48e4a74915c56eabdb6fb0c1e51c524 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 02:35:38 +0200 Subject: [PATCH 05/42] Shaders project renamed --- .../Assets/ShaderTemplate.mustache | 0 .../ComputeSharp.Shaders.csproj} | 0 .../Mappings/HlslKnownMethods.cs | 0 .../Mappings/HlslKnownTypes.cs | 0 .../MethodDecompiler.cs | 0 .../Primitives/ThreadIds.cs | 0 .../Renderer/Models/Fields/Abstract/FieldInfoBase.cs | 0 .../Renderer/Models/Fields/ConstantBufferFieldInfo.cs | 0 .../Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs | 0 .../Renderer/Models/Fields/StaticScalarFieldInfo.cs | 0 .../Renderer/Models/ShaderInfo.cs | 0 .../Renderer/ShaderRenderer.cs | 0 .../ShaderCompiler.cs | 0 .../ShaderLoader.cs | 0 .../ShaderResource.cs | 0 .../ShaderSyntaxRewriter.cs | 0 .../SyntaxNodeExtensions.cs | 0 src/ComputeSharp.sln | 2 +- .../DirectX12ComputeShaderSample.csproj | 2 +- 19 files changed, 2 insertions(+), 2 deletions(-) rename src/{DirectX12GameEngine.Shaders => ComputeSharp.Shaders}/Assets/ShaderTemplate.mustache (100%) rename src/{DirectX12GameEngine.Shaders/DirectX12GameEngine.Shaders.csproj => ComputeSharp.Shaders/ComputeSharp.Shaders.csproj} (100%) rename src/{DirectX12GameEngine.Shaders => ComputeSharp.Shaders}/Mappings/HlslKnownMethods.cs (100%) rename src/{DirectX12GameEngine.Shaders => ComputeSharp.Shaders}/Mappings/HlslKnownTypes.cs (100%) rename src/{DirectX12GameEngine.Shaders => ComputeSharp.Shaders}/MethodDecompiler.cs (100%) rename src/{DirectX12GameEngine.Shaders => ComputeSharp.Shaders}/Primitives/ThreadIds.cs (100%) rename src/{DirectX12GameEngine.Shaders => ComputeSharp.Shaders}/Renderer/Models/Fields/Abstract/FieldInfoBase.cs (100%) rename src/{DirectX12GameEngine.Shaders => ComputeSharp.Shaders}/Renderer/Models/Fields/ConstantBufferFieldInfo.cs (100%) rename src/{DirectX12GameEngine.Shaders => ComputeSharp.Shaders}/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs (100%) rename src/{DirectX12GameEngine.Shaders => ComputeSharp.Shaders}/Renderer/Models/Fields/StaticScalarFieldInfo.cs (100%) rename src/{DirectX12GameEngine.Shaders => ComputeSharp.Shaders}/Renderer/Models/ShaderInfo.cs (100%) rename src/{DirectX12GameEngine.Shaders => ComputeSharp.Shaders}/Renderer/ShaderRenderer.cs (100%) rename src/{DirectX12GameEngine.Shaders => ComputeSharp.Shaders}/ShaderCompiler.cs (100%) rename src/{DirectX12GameEngine.Shaders => ComputeSharp.Shaders}/ShaderLoader.cs (100%) rename src/{DirectX12GameEngine.Shaders => ComputeSharp.Shaders}/ShaderResource.cs (100%) rename src/{DirectX12GameEngine.Shaders => ComputeSharp.Shaders}/ShaderSyntaxRewriter.cs (100%) rename src/{DirectX12GameEngine.Shaders => ComputeSharp.Shaders}/SyntaxNodeExtensions.cs (100%) diff --git a/src/DirectX12GameEngine.Shaders/Assets/ShaderTemplate.mustache b/src/ComputeSharp.Shaders/Assets/ShaderTemplate.mustache similarity index 100% rename from src/DirectX12GameEngine.Shaders/Assets/ShaderTemplate.mustache rename to src/ComputeSharp.Shaders/Assets/ShaderTemplate.mustache diff --git a/src/DirectX12GameEngine.Shaders/DirectX12GameEngine.Shaders.csproj b/src/ComputeSharp.Shaders/ComputeSharp.Shaders.csproj similarity index 100% rename from src/DirectX12GameEngine.Shaders/DirectX12GameEngine.Shaders.csproj rename to src/ComputeSharp.Shaders/ComputeSharp.Shaders.csproj diff --git a/src/DirectX12GameEngine.Shaders/Mappings/HlslKnownMethods.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs similarity index 100% rename from src/DirectX12GameEngine.Shaders/Mappings/HlslKnownMethods.cs rename to src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs diff --git a/src/DirectX12GameEngine.Shaders/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs similarity index 100% rename from src/DirectX12GameEngine.Shaders/Mappings/HlslKnownTypes.cs rename to src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs diff --git a/src/DirectX12GameEngine.Shaders/MethodDecompiler.cs b/src/ComputeSharp.Shaders/MethodDecompiler.cs similarity index 100% rename from src/DirectX12GameEngine.Shaders/MethodDecompiler.cs rename to src/ComputeSharp.Shaders/MethodDecompiler.cs diff --git a/src/DirectX12GameEngine.Shaders/Primitives/ThreadIds.cs b/src/ComputeSharp.Shaders/Primitives/ThreadIds.cs similarity index 100% rename from src/DirectX12GameEngine.Shaders/Primitives/ThreadIds.cs rename to src/ComputeSharp.Shaders/Primitives/ThreadIds.cs diff --git a/src/DirectX12GameEngine.Shaders/Renderer/Models/Fields/Abstract/FieldInfoBase.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/FieldInfoBase.cs similarity index 100% rename from src/DirectX12GameEngine.Shaders/Renderer/Models/Fields/Abstract/FieldInfoBase.cs rename to src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/FieldInfoBase.cs diff --git a/src/DirectX12GameEngine.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs similarity index 100% rename from src/DirectX12GameEngine.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs rename to src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs diff --git a/src/DirectX12GameEngine.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs similarity index 100% rename from src/DirectX12GameEngine.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs rename to src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs diff --git a/src/DirectX12GameEngine.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs similarity index 100% rename from src/DirectX12GameEngine.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs rename to src/ComputeSharp.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs diff --git a/src/DirectX12GameEngine.Shaders/Renderer/Models/ShaderInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs similarity index 100% rename from src/DirectX12GameEngine.Shaders/Renderer/Models/ShaderInfo.cs rename to src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs diff --git a/src/DirectX12GameEngine.Shaders/Renderer/ShaderRenderer.cs b/src/ComputeSharp.Shaders/Renderer/ShaderRenderer.cs similarity index 100% rename from src/DirectX12GameEngine.Shaders/Renderer/ShaderRenderer.cs rename to src/ComputeSharp.Shaders/Renderer/ShaderRenderer.cs diff --git a/src/DirectX12GameEngine.Shaders/ShaderCompiler.cs b/src/ComputeSharp.Shaders/ShaderCompiler.cs similarity index 100% rename from src/DirectX12GameEngine.Shaders/ShaderCompiler.cs rename to src/ComputeSharp.Shaders/ShaderCompiler.cs diff --git a/src/DirectX12GameEngine.Shaders/ShaderLoader.cs b/src/ComputeSharp.Shaders/ShaderLoader.cs similarity index 100% rename from src/DirectX12GameEngine.Shaders/ShaderLoader.cs rename to src/ComputeSharp.Shaders/ShaderLoader.cs diff --git a/src/DirectX12GameEngine.Shaders/ShaderResource.cs b/src/ComputeSharp.Shaders/ShaderResource.cs similarity index 100% rename from src/DirectX12GameEngine.Shaders/ShaderResource.cs rename to src/ComputeSharp.Shaders/ShaderResource.cs diff --git a/src/DirectX12GameEngine.Shaders/ShaderSyntaxRewriter.cs b/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs similarity index 100% rename from src/DirectX12GameEngine.Shaders/ShaderSyntaxRewriter.cs rename to src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs diff --git a/src/DirectX12GameEngine.Shaders/SyntaxNodeExtensions.cs b/src/ComputeSharp.Shaders/SyntaxNodeExtensions.cs similarity index 100% rename from src/DirectX12GameEngine.Shaders/SyntaxNodeExtensions.cs rename to src/ComputeSharp.Shaders/SyntaxNodeExtensions.cs diff --git a/src/ComputeSharp.sln b/src/ComputeSharp.sln index a5d6dea6..a8415a79 100644 --- a/src/ComputeSharp.sln +++ b/src/ComputeSharp.sln @@ -5,7 +5,7 @@ VisualStudioVersion = 16.0.29201.188 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Graphics", "ComputeSharp.Graphics\ComputeSharp.Graphics.csproj", "{2499CC74-BC9A-4B30-A3B6-05F9C7BF9B34}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectX12GameEngine.Shaders", "DirectX12GameEngine.Shaders\DirectX12GameEngine.Shaders.csproj", "{23AE6EED-79CD-4ADD-9A85-0148BD957D19}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Shaders", "ComputeSharp.Shaders\ComputeSharp.Shaders.csproj", "{23AE6EED-79CD-4ADD-9A85-0148BD957D19}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp", "ComputeSharp\ComputeSharp.csproj", "{314EB353-E7E3-4146-8111-685CC83EC1DE}" EndProject diff --git a/src/DirectX12ComputeShaderSample/DirectX12ComputeShaderSample.csproj b/src/DirectX12ComputeShaderSample/DirectX12ComputeShaderSample.csproj index bb64e8b0..a9e9535e 100644 --- a/src/DirectX12ComputeShaderSample/DirectX12ComputeShaderSample.csproj +++ b/src/DirectX12ComputeShaderSample/DirectX12ComputeShaderSample.csproj @@ -7,7 +7,7 @@ - + From e381de2c7ce6882ac7354a5a7a2f8b031b140653 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 02:37:33 +0200 Subject: [PATCH 06/42] Adjusted namespaces in shaders project --- src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs | 2 +- src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs | 3 +-- src/ComputeSharp.Shaders/MethodDecompiler.cs | 2 +- src/ComputeSharp.Shaders/Primitives/ThreadIds.cs | 2 +- .../Renderer/Models/Fields/Abstract/FieldInfoBase.cs | 2 +- .../Renderer/Models/Fields/ConstantBufferFieldInfo.cs | 4 ++-- .../Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs | 4 ++-- .../Renderer/Models/Fields/StaticScalarFieldInfo.cs | 4 ++-- src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs | 4 ++-- src/ComputeSharp.Shaders/Renderer/ShaderRenderer.cs | 4 ++-- src/ComputeSharp.Shaders/ShaderCompiler.cs | 2 +- src/ComputeSharp.Shaders/ShaderLoader.cs | 9 ++++----- src/ComputeSharp.Shaders/ShaderResource.cs | 4 +--- src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs | 2 +- src/ComputeSharp.Shaders/SyntaxNodeExtensions.cs | 4 ++-- src/DirectX12ComputeShaderSample/Gpu.cs | 8 ++++---- src/DirectX12ComputeShaderSample/Program.cs | 6 +++--- 17 files changed, 31 insertions(+), 35 deletions(-) diff --git a/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs index 6e33493c..f7d7fe7d 100644 --- a/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs +++ b/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Diagnostics.Contracts; using Microsoft.CodeAnalysis; -namespace DirectX12GameEngine.Shaders.Mappings +namespace ComputeSharp.Shaders.Mappings { /// /// A that contains and maps known HLSL method names to common .NET method diff --git a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs index c6038358..d1712543 100644 --- a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs @@ -3,9 +3,8 @@ using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Numerics; using System.Text.RegularExpressions; -using DirectX12GameEngine.Shaders.Primitives; -namespace DirectX12GameEngine.Shaders.Mappings +namespace ComputeSharp.Shaders.Mappings { /// /// A that contains and maps known HLSL type names to common .NET types diff --git a/src/ComputeSharp.Shaders/MethodDecompiler.cs b/src/ComputeSharp.Shaders/MethodDecompiler.cs index 031f2e21..0c09ad67 100644 --- a/src/ComputeSharp.Shaders/MethodDecompiler.cs +++ b/src/ComputeSharp.Shaders/MethodDecompiler.cs @@ -12,7 +12,7 @@ using ICSharpCode.Decompiler.Metadata; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; -namespace DirectX12GameEngine.Shaders +namespace ComputeSharp.Shaders { /// /// A that is able to decompile arbitrary methods diff --git a/src/ComputeSharp.Shaders/Primitives/ThreadIds.cs b/src/ComputeSharp.Shaders/Primitives/ThreadIds.cs index 08cf64b3..219f4ef9 100644 --- a/src/ComputeSharp.Shaders/Primitives/ThreadIds.cs +++ b/src/ComputeSharp.Shaders/Primitives/ThreadIds.cs @@ -1,4 +1,4 @@ -namespace DirectX12GameEngine.Shaders.Primitives +namespace ComputeSharp { /// /// A that indicates the ids of a given GPU thread running a compute shader diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/FieldInfoBase.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/FieldInfoBase.cs index ead23005..ff7cc533 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/FieldInfoBase.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/FieldInfoBase.cs @@ -1,4 +1,4 @@ -namespace DirectX12GameEngine.Shaders.Renderer.Models.Abstract +namespace ComputeSharp.Shaders.Renderer.Models.Fields.Abstract { /// /// A that contains info on a shader field diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs index aa453dab..1f8c3160 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs @@ -1,6 +1,6 @@ -using DirectX12GameEngine.Shaders.Renderer.Models.Abstract; +using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; -namespace DirectX12GameEngine.Shaders.Renderer.Models.Fields +namespace ComputeSharp.Shaders.Renderer.Models.Fields { /// /// A that contains info on a constant buffer field diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs index abfb06c9..e7280bc2 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs @@ -1,6 +1,6 @@ -using DirectX12GameEngine.Shaders.Renderer.Models.Abstract; +using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; -namespace DirectX12GameEngine.Shaders.Renderer.Models.Fields +namespace ComputeSharp.Shaders.Renderer.Models.Fields { /// /// A that contains info on a read write buffer field diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs index ff41f95e..ab6ca039 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs @@ -1,6 +1,6 @@ -using DirectX12GameEngine.Shaders.Renderer.Models.Abstract; +using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; -namespace DirectX12GameEngine.Shaders.Renderer.Models.Fields +namespace ComputeSharp.Shaders.Renderer.Models.Fields { /// /// A that contains info on a static scalar field diff --git a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs index 64ba1e8d..598568ef 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs @@ -1,9 +1,9 @@ using System.Collections.Generic; -using DirectX12GameEngine.Shaders.Renderer.Models.Abstract; +using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; #pragma warning disable CS8618 // Non-nullable field is uninitialized -namespace DirectX12GameEngine.Shaders.Renderer.Models +namespace ComputeSharp.Shaders.Renderer.Models { /// /// A that contains info to render a shader source to compile diff --git a/src/ComputeSharp.Shaders/Renderer/ShaderRenderer.cs b/src/ComputeSharp.Shaders/Renderer/ShaderRenderer.cs index 5533346f..e5b40be2 100644 --- a/src/ComputeSharp.Shaders/Renderer/ShaderRenderer.cs +++ b/src/ComputeSharp.Shaders/Renderer/ShaderRenderer.cs @@ -2,9 +2,9 @@ using System.Diagnostics.Contracts; using System.IO; using System.Reflection; -using DirectX12GameEngine.Shaders.Renderer.Models; +using ComputeSharp.Shaders.Renderer.Models; -namespace DirectX12GameEngine.Shaders.Renderer +namespace ComputeSharp.Shaders.Renderer { /// /// A that uses the mustache templating system to render HLSL shaders diff --git a/src/ComputeSharp.Shaders/ShaderCompiler.cs b/src/ComputeSharp.Shaders/ShaderCompiler.cs index ea64c675..fc21b010 100644 --- a/src/ComputeSharp.Shaders/ShaderCompiler.cs +++ b/src/ComputeSharp.Shaders/ShaderCompiler.cs @@ -3,7 +3,7 @@ using System.Diagnostics.Contracts; using DotNetDxc; using SharpDX.Direct3D12; -namespace DirectX12GameEngine.Shaders +namespace ComputeSharp.Shaders { /// /// A that uses the APIs to compile compute shaders diff --git a/src/ComputeSharp.Shaders/ShaderLoader.cs b/src/ComputeSharp.Shaders/ShaderLoader.cs index 2fac6a03..6688be7a 100644 --- a/src/ComputeSharp.Shaders/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/ShaderLoader.cs @@ -5,17 +5,16 @@ using System.Linq; using System.Reflection; using System.Text.RegularExpressions; using ComputeSharp.Graphics.Buffers.Abstract; -using DirectX12GameEngine.Shaders.Mappings; -using DirectX12GameEngine.Shaders.Primitives; -using DirectX12GameEngine.Shaders.Renderer.Models.Abstract; -using DirectX12GameEngine.Shaders.Renderer.Models.Fields; +using ComputeSharp.Shaders.Mappings; +using ComputeSharp.Shaders.Renderer.Models.Fields; +using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using SharpDX.Direct3D12; #pragma warning disable CS8618 // Non-nullable field is uninitialized -namespace DirectX12GameEngine.Shaders +namespace ComputeSharp.Shaders { /// /// A responsible for loading and processing instances diff --git a/src/ComputeSharp.Shaders/ShaderResource.cs b/src/ComputeSharp.Shaders/ShaderResource.cs index 6d90e986..5ca82346 100644 --- a/src/ComputeSharp.Shaders/ShaderResource.cs +++ b/src/ComputeSharp.Shaders/ShaderResource.cs @@ -1,9 +1,7 @@ using System; -using System.Numerics; using ComputeSharp.Graphics.Buffers; -using Buffer = System.Buffer; -namespace DirectX12GameEngine.Shaders +namespace ComputeSharp.Shaders { public abstract class ShaderResource { diff --git a/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs b/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs index f2e9c018..65063848 100644 --- a/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs +++ b/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs @@ -4,7 +4,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; -namespace DirectX12GameEngine.Shaders +namespace ComputeSharp.Shaders { public class ShaderSyntaxRewriter : CSharpSyntaxRewriter { diff --git a/src/ComputeSharp.Shaders/SyntaxNodeExtensions.cs b/src/ComputeSharp.Shaders/SyntaxNodeExtensions.cs index b2e0f0fb..7660be33 100644 --- a/src/ComputeSharp.Shaders/SyntaxNodeExtensions.cs +++ b/src/ComputeSharp.Shaders/SyntaxNodeExtensions.cs @@ -1,10 +1,10 @@ using System.Linq; -using DirectX12GameEngine.Shaders.Mappings; +using ComputeSharp.Shaders.Mappings; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; -namespace DirectX12GameEngine.Shaders +namespace ComputeSharp.Shaders { public static class SyntaxNodeExtensions { diff --git a/src/DirectX12ComputeShaderSample/Gpu.cs b/src/DirectX12ComputeShaderSample/Gpu.cs index 81a27c03..970ae4b7 100644 --- a/src/DirectX12ComputeShaderSample/Gpu.cs +++ b/src/DirectX12ComputeShaderSample/Gpu.cs @@ -1,10 +1,10 @@ using System; using System.Linq; +using ComputeSharp; using ComputeSharp.Graphics; -using DirectX12GameEngine.Shaders; -using DirectX12GameEngine.Shaders.Primitives; -using DirectX12GameEngine.Shaders.Renderer; -using DirectX12GameEngine.Shaders.Renderer.Models; +using ComputeSharp.Shaders; +using ComputeSharp.Shaders.Renderer; +using ComputeSharp.Shaders.Renderer.Models; using SharpDX.Direct3D12; using CommandList = ComputeSharp.Graphics.CommandList; using PipelineState = ComputeSharp.Graphics.PipelineState; diff --git a/src/DirectX12ComputeShaderSample/Program.cs b/src/DirectX12ComputeShaderSample/Program.cs index 8f96bd63..3b280ca1 100644 --- a/src/DirectX12ComputeShaderSample/Program.cs +++ b/src/DirectX12ComputeShaderSample/Program.cs @@ -1,8 +1,8 @@ -using DirectX12GameEngine.Shaders; -using System; +using System; using System.Linq; +using ComputeSharp; using ComputeSharp.Graphics.Buffers; -using DirectX12GameEngine.Shaders.Primitives; +using ComputeSharp.Shaders; using Buffer = ComputeSharp.Graphics.Buffers.Buffer; namespace DirectX12ComputeShaderSample From e370068ff6e7118a1e373575bd0c9d06717e13d9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 02:41:44 +0200 Subject: [PATCH 07/42] Fixed refactoring glitch --- src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs index f7d7fe7d..143e087e 100644 --- a/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs +++ b/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs @@ -30,9 +30,9 @@ namespace ComputeSharp.Shaders.Mappings ["System.MathF.PI"] = "3.14159274f", // ThreadIds - ["DirectX12GameEngine.Shaders.Primitives.ThreadIds.X"] = ".x", - ["DirectX12GameEngine.Shaders.Primitives.ThreadIds.Y"] = ".y", - ["DirectX12GameEngine.Shaders.Primitives.ThreadIds.Z"] = ".z", + ["ComputeSharp.ThreadIds.X"] = ".x", + ["ComputeSharp.ThreadIds.Y"] = ".y", + ["ComputeSharp.ThreadIds.Z"] = ".z", // Vector3 ["System.Numerics.Vector3.X"] = ".x", From 0896d368e44d524c8b5c5fc417f2e1c493da115b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 02:43:03 +0200 Subject: [PATCH 08/42] Minor code refactoring --- src/ComputeSharp.Shaders/ComputeSharp.Shaders.csproj | 6 +----- src/ComputeSharp.Shaders/Renderer/ShaderRenderer.cs | 2 +- .../{Assets => Renderer/Templates}/ShaderTemplate.mustache | 0 3 files changed, 2 insertions(+), 6 deletions(-) rename src/ComputeSharp.Shaders/{Assets => Renderer/Templates}/ShaderTemplate.mustache (100%) diff --git a/src/ComputeSharp.Shaders/ComputeSharp.Shaders.csproj b/src/ComputeSharp.Shaders/ComputeSharp.Shaders.csproj index 1916128f..086f0e66 100644 --- a/src/ComputeSharp.Shaders/ComputeSharp.Shaders.csproj +++ b/src/ComputeSharp.Shaders/ComputeSharp.Shaders.csproj @@ -35,11 +35,7 @@ - - - - - + PreserveNewest diff --git a/src/ComputeSharp.Shaders/Renderer/ShaderRenderer.cs b/src/ComputeSharp.Shaders/Renderer/ShaderRenderer.cs index e5b40be2..f9aeb680 100644 --- a/src/ComputeSharp.Shaders/Renderer/ShaderRenderer.cs +++ b/src/ComputeSharp.Shaders/Renderer/ShaderRenderer.cs @@ -24,7 +24,7 @@ namespace ComputeSharp.Shaders.Renderer string assemblyPath = Assembly.GetExecutingAssembly().Location, assemblyDirectory = Path.GetDirectoryName(assemblyPath) ?? throw new InvalidOperationException("Can't find the assembly directory"), - templatePath = Path.Combine(assemblyDirectory, "Assets", "ShaderTemplate.mustache"); + templatePath = Path.Combine(assemblyDirectory, "Renderer", "Templates", "ShaderTemplate.mustache"); Template = File.ReadAllText(templatePath); } diff --git a/src/ComputeSharp.Shaders/Assets/ShaderTemplate.mustache b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache similarity index 100% rename from src/ComputeSharp.Shaders/Assets/ShaderTemplate.mustache rename to src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache From bc593c655d0d8d2668d6c670853e7e2ddde007dc Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 11:13:00 +0200 Subject: [PATCH 09/42] Minor bug fixes --- .../Buffers/Abstract/GraphicsResource.cs | 4 ++-- src/ComputeSharp.Graphics/CommandList.cs | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource.cs index e8318b60..46f42eec 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource.cs @@ -35,12 +35,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// /// Gets the instance for the current resource /// - protected internal CpuDescriptorHandle NativeCpuDescriptorHandle { get; protected set; } + protected internal CpuDescriptorHandle? NativeCpuDescriptorHandle { get; protected set; } /// /// Gets the instance for the current resource /// - protected internal GpuDescriptorHandle NativeGpuDescriptorHandle { get; protected set; } + protected internal GpuDescriptorHandle? NativeGpuDescriptorHandle { get; protected set; } /// /// Maps the current resource to a specified subresource diff --git a/src/ComputeSharp.Graphics/CommandList.cs b/src/ComputeSharp.Graphics/CommandList.cs index 99702716..355f1981 100644 --- a/src/ComputeSharp.Graphics/CommandList.cs +++ b/src/ComputeSharp.Graphics/CommandList.cs @@ -110,7 +110,8 @@ namespace ComputeSharp.Graphics public void SetGraphicsRootDescriptorTable(int rootParameterIndex, GraphicsResource resource) { - SetGraphicsRootDescriptorTable(rootParameterIndex, resource.NativeGpuDescriptorHandle); + if (resource.NativeGpuDescriptorHandle == null) throw new InvalidOperationException("Invalid graphics resource GPU descriptor"); + SetGraphicsRootDescriptorTable(rootParameterIndex, resource.NativeGpuDescriptorHandle.Value); } public void SetGraphicsRootDescriptorTable(int rootParameterIndex, GpuDescriptorHandle baseDescriptor) @@ -130,7 +131,8 @@ namespace ComputeSharp.Graphics public void SetComputeRootDescriptorTable(int rootParameterIndex, GraphicsResource resource) { - SetComputeRootDescriptorTable(rootParameterIndex, resource.NativeGpuDescriptorHandle); + if (resource.NativeGpuDescriptorHandle == null) throw new InvalidOperationException("Invalid graphics resource GPU descriptor"); + SetComputeRootDescriptorTable(rootParameterIndex, resource.NativeGpuDescriptorHandle.Value); } public void SetComputeRootDescriptorTable(int rootParameterIndex, GpuDescriptorHandle baseDescriptor) From b7e05ec85bad13df76a218d8e87499f77464bef8 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 11:47:26 +0200 Subject: [PATCH 10/42] Added initial Buffer2 implementation --- src/ComputeSharp.Graphics/Buffers/Buffer2.cs | 109 +++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/ComputeSharp.Graphics/Buffers/Buffer2.cs diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer2.cs b/src/ComputeSharp.Graphics/Buffers/Buffer2.cs new file mode 100644 index 00000000..df75a923 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/Buffer2.cs @@ -0,0 +1,109 @@ +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using ComputeSharp.Graphics.Buffers.Abstract; +using SharpDX.Direct3D12; + +namespace ComputeSharp.Graphics.Buffers +{ + /// + /// A representing a typed buffer stored on GPU memory + /// + /// The type of items stored on the buffer + public sealed class Buffer2 : GraphicsResource where T : unmanaged + { + /// + /// Creates a new instance with the specified parameters + /// + /// The associated with the current instance + /// The number of items to store in the current buffer + /// The heap type for the current buffer + public Buffer2(GraphicsDevice device, int size, HeapType heapType) : base(device) + { + Size = size; + ElementSizeInBytes = Unsafe.SizeOf(); + SizeInBytes = Size * ElementSizeInBytes; + HeapType = heapType; + + ResourceFlags flags = heapType == HeapType.Default ? ResourceFlags.AllowUnorderedAccess : ResourceFlags.None; + ResourceDescription description = ResourceDescription.Buffer(ElementSizeInBytes, flags); + ResourceStates resourceStates = heapType switch + { + HeapType.Upload => ResourceStates.GenericRead, + HeapType.Readback => ResourceStates.CopyDestination, + _ => ResourceStates.CopyDestination + }; + + NativeResource = GraphicsDevice.NativeDevice.CreateCommittedResource(new HeapProperties(heapType), HeapFlags.None, description, resourceStates); + + (NativeCpuDescriptorHandle, NativeGpuDescriptorHandle) = heapType switch + { + HeapType.Default => CreateUnorderedAccessView(), + HeapType.Upload => CreateConstantBufferView(), + _ => default + }; + } + + /// + /// Creates the descriptors for a constant buffer + /// + /// The CPU and GPU handles for a constant buffer + [Pure] + private (CpuDescriptorHandle, GpuDescriptorHandle) CreateConstantBufferView() + { + (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1); + + int constantBufferSize = (SizeInBytes + 255) & ~255; + + ConstantBufferViewDescription description = new ConstantBufferViewDescription + { + BufferLocation = NativeResource!.GPUVirtualAddress, + SizeInBytes = constantBufferSize + }; + + GraphicsDevice.NativeDevice.CreateConstantBufferView(description, cpuHandle); + + return (cpuHandle, gpuHandle); + } + + /// + /// Creates the descriptors for a read write buffer + /// + /// The CPU and GPU handles for a read write buffer + [Pure] + private (CpuDescriptorHandle, GpuDescriptorHandle) CreateUnorderedAccessView() + { + (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1); + + UnorderedAccessViewDescription description = new UnorderedAccessViewDescription + { + Format = SharpDX.DXGI.Format.R32_Float, + Dimension = UnorderedAccessViewDimension.Buffer, + Buffer = { ElementCount = Size } + }; + + GraphicsDevice.NativeDevice.CreateUnorderedAccessView(NativeResource, null, description, cpuHandle); + + return (cpuHandle, gpuHandle); + } + + /// + /// Gets the size of the current buffer, as in the number of values it contains + /// + public int Size { get; } + + /// + /// Gets the size in bytes of the current buffer + /// + public int SizeInBytes { get; } + + /// + /// Gets the size in bytes of each value contained in the buffer + /// + public int ElementSizeInBytes { get; } + + /// + /// Gets the heap type being targeted by the current buffer + /// + public HeapType HeapType { get; } + } +} From 0a389d81ecaca9b449fa79c1b9947502e46e6bd2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 11:57:41 +0200 Subject: [PATCH 11/42] Added APIs to read and write data to Buffer2 --- src/ComputeSharp.Graphics/Buffers/Buffer2.cs | 54 +++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer2.cs b/src/ComputeSharp.Graphics/Buffers/Buffer2.cs index df75a923..740de6b8 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer2.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer2.cs @@ -1,6 +1,8 @@ -using System.Diagnostics.Contracts; +using System; +using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Helpers; using SharpDX.Direct3D12; namespace ComputeSharp.Graphics.Buffers @@ -105,5 +107,55 @@ namespace ComputeSharp.Graphics.Buffers /// Gets the heap type being targeted by the current buffer /// public HeapType HeapType { get; } + + /// + /// Reads the contents of the current instance and writes them into a target + /// + /// The input to write data to + public void GetData(Span span) + { + // Create a temporary readback buffer if the current buffer type is read write (can't be read from directly) + if (HeapType == HeapType.Default) + { + using Buffer2 readbackBaffer = new Buffer2(GraphicsDevice, Size, HeapType.Readback); + using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); + + copyCommandList.CopyBufferRegion(this, 0, readbackBaffer, 0, SizeInBytes); + copyCommandList.Flush(true); + + readbackBaffer.GetData(span); + } + else + { + // Map and read the memory back directly + Map(0); + MemoryHelper.Copy(MappedResource, span); + Unmap(0); + } + } + + /// + /// Writes the contents of a given to the current instance + /// + /// The input to read data from + public void SetData(Span span) + { + // Create a temporary upload buffer if the current buffer type is read write (can't be written to directly) + if (HeapType == HeapType.Default) + { + using Buffer2 uploadBuffer = new Buffer2(GraphicsDevice, Size, HeapType.Upload); + using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); + + copyCommandList.CopyBufferRegion(uploadBuffer, 0, this, 0, SizeInBytes); + copyCommandList.Flush(true); + } + else + { + // Map and copy the memory directly + Map(0); + MemoryHelper.Copy(span, MappedResource); + Unmap(0); + } + } } } From bde2f35fe6079c0eee8c1d94cfb162fb4f4335c6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 12:01:29 +0200 Subject: [PATCH 12/42] Added Bufffer2 indexer --- src/ComputeSharp.Graphics/Buffers/Buffer2.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer2.cs b/src/ComputeSharp.Graphics/Buffers/Buffer2.cs index 740de6b8..bf4b2d5b 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer2.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer2.cs @@ -108,6 +108,17 @@ namespace ComputeSharp.Graphics.Buffers /// public HeapType HeapType { get; } + /// + /// Gets or sets a single value from the current 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 + public T this[int i] + { + get => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); + set => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); + } + /// /// Reads the contents of the current instance and writes them into a target /// From 79c4df4a096c9584d36f4d5279c49934d4549383 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 12:04:16 +0200 Subject: [PATCH 13/42] Added Buffer2.GetData() overload --- src/ComputeSharp.Graphics/Buffers/Buffer2.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer2.cs b/src/ComputeSharp.Graphics/Buffers/Buffer2.cs index bf4b2d5b..26f4d91d 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer2.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer2.cs @@ -119,6 +119,19 @@ namespace ComputeSharp.Graphics.Buffers set => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); } + /// + /// 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[Size]; + GetData(data); + + return data; + } + /// /// Reads the contents of the current instance and writes them into a target /// From 207a26006a223a093f57fed7e653e550094492f0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 12:48:42 +0200 Subject: [PATCH 14/42] Added BufferExtensions class --- src/ComputeSharp.Graphics/Buffers/Buffer2.cs | 2 +- .../Buffers/BufferExtensions.cs | 61 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer2.cs b/src/ComputeSharp.Graphics/Buffers/Buffer2.cs index 26f4d91d..24d8873d 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer2.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer2.cs @@ -19,7 +19,7 @@ namespace ComputeSharp.Graphics.Buffers /// The associated with the current instance /// The number of items to store in the current buffer /// The heap type for the current buffer - public Buffer2(GraphicsDevice device, int size, HeapType heapType) : base(device) + internal Buffer2(GraphicsDevice device, int size, HeapType heapType) : base(device) { Size = size; ElementSizeInBytes = Unsafe.SizeOf(); diff --git a/src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs b/src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs new file mode 100644 index 00000000..318698bb --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs @@ -0,0 +1,61 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using SharpDX.Direct3D12; + +namespace ComputeSharp.Graphics.Buffers +{ + /// + /// A with extension methods for the type to allocate buffers + /// + public static class BufferExtensions + { + /// + /// 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 size of the buffer to allocate + /// A zeroed read write instance of size + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Buffer2 AllocateReadWriteBuffer(this GraphicsDevice device, int size) where T : unmanaged + { + return new Buffer2(device, size, HeapType.Default); + } + + /// + /// 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] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Buffer2 AllocateReadWriteBuffer(this GraphicsDevice device, Span span) where T : unmanaged + { + Buffer2 buffer = new Buffer2(device, span.Length, HeapType.Default); + buffer.SetData(span); + + return buffer; + } + + /// + /// Allocates a new constant 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 constant instance with the contents of the input + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Buffer2 AllocateConstantBuffer(this GraphicsDevice device, Span span) where T : unmanaged + { + Buffer2 buffer = new Buffer2(device, span.Length, HeapType.Upload); + buffer.SetData(span); + + return buffer; + } + } +} From c12dffdba0ab479539b1c502e5d398a217c82550 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 12:56:41 +0200 Subject: [PATCH 15/42] Fixed Buffer2 indexer argument type to conform to HLSL --- src/ComputeSharp.Graphics/Buffers/Buffer2.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer2.cs b/src/ComputeSharp.Graphics/Buffers/Buffer2.cs index 24d8873d..1abed2c6 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer2.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer2.cs @@ -113,7 +113,7 @@ namespace ComputeSharp.Graphics.Buffers /// /// 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 - public T this[int i] + public T this[uint i] { get => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); set => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); From 0ce0c94611b53f986e10aa47232396f79db41391 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 13:15:39 +0200 Subject: [PATCH 16/42] Bug fixes in the Buffer2 type --- src/ComputeSharp.Graphics/Buffers/Buffer2.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer2.cs b/src/ComputeSharp.Graphics/Buffers/Buffer2.cs index 1abed2c6..919a7c6e 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer2.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer2.cs @@ -27,12 +27,12 @@ namespace ComputeSharp.Graphics.Buffers HeapType = heapType; ResourceFlags flags = heapType == HeapType.Default ? ResourceFlags.AllowUnorderedAccess : ResourceFlags.None; - ResourceDescription description = ResourceDescription.Buffer(ElementSizeInBytes, flags); + ResourceDescription description = ResourceDescription.Buffer(SizeInBytes, flags); ResourceStates resourceStates = heapType switch { HeapType.Upload => ResourceStates.GenericRead, HeapType.Readback => ResourceStates.CopyDestination, - _ => ResourceStates.CopyDestination + _ => ResourceStates.Common }; NativeResource = GraphicsDevice.NativeDevice.CreateCommittedResource(new HeapProperties(heapType), HeapFlags.None, description, resourceStates); From 9e253144b44ddaf98da2c3fc95cd3ff67d08418c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 13:26:48 +0200 Subject: [PATCH 17/42] Fixed bug in SetData API for the Buffer2 type --- src/ComputeSharp.Graphics/Buffers/Buffer2.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer2.cs b/src/ComputeSharp.Graphics/Buffers/Buffer2.cs index 919a7c6e..1a17cf3a 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer2.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer2.cs @@ -170,6 +170,7 @@ namespace ComputeSharp.Graphics.Buffers using Buffer2 uploadBuffer = new Buffer2(GraphicsDevice, Size, HeapType.Upload); using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); + uploadBuffer.SetData(span); copyCommandList.CopyBufferRegion(uploadBuffer, 0, this, 0, SizeInBytes); copyCommandList.Flush(true); } From 08ed370fd83f91f58b9621bcd80df8196be00d0c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 13:32:30 +0200 Subject: [PATCH 18/42] Implemented handling of captured buffers in shaders --- .../Mappings/HlslKnownTypes.cs | 11 ++++---- src/ComputeSharp.Shaders/ShaderLoader.cs | 8 +++--- src/ComputeSharp.Shaders/ShaderResource.cs | 26 ------------------- src/DirectX12ComputeShaderSample/Program.cs | 8 ++---- 4 files changed, 12 insertions(+), 41 deletions(-) delete mode 100644 src/ComputeSharp.Shaders/ShaderResource.cs diff --git a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs index d1712543..17eeb5ba 100644 --- a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Numerics; using System.Text.RegularExpressions; +using ComputeSharp.Graphics.Buffers; namespace ComputeSharp.Shaders.Mappings { @@ -26,7 +27,7 @@ namespace ComputeSharp.Shaders.Mappings [typeof(Vector4).FullName] = "float4", [typeof(Matrix4x4).FullName] = "float4x4", [typeof(ThreadIds).FullName] = "uint3", - [typeof(RWBufferResource<>).FullName] = "RWBuffer" + [typeof(Buffer2<>).FullName] = "RWBuffer" }; /// @@ -53,13 +54,13 @@ namespace ComputeSharp.Shaders.Mappings type == typeof(Matrix4x4); /// - /// Checks whether or not the input type is a value + /// Checks whether or not the input type is a value /// /// The input instance to check - /// A indicating whether the input is in fact a instance + /// A indicating whether the input is in fact a instance [Pure] - public static bool IsReadWriteBufferType(Type type) => type.IsGenericType && - type.GetGenericTypeDefinition() == typeof(RWBufferResource<>); + public static bool IsBufferType(Type type) => type.IsGenericType && + type.GetGenericTypeDefinition() == typeof(Buffer2<>); /// /// Checks whether or not a given is an HLSL known type diff --git a/src/ComputeSharp.Shaders/ShaderLoader.cs b/src/ComputeSharp.Shaders/ShaderLoader.cs index 6688be7a..05d6d235 100644 --- a/src/ComputeSharp.Shaders/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/ShaderLoader.cs @@ -4,6 +4,7 @@ using System.Diagnostics.Contracts; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; +using ComputeSharp.Graphics.Buffers; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Shaders.Mappings; using ComputeSharp.Shaders.Renderer.Models.Fields; @@ -115,16 +116,15 @@ namespace ComputeSharp.Shaders FieldInfoBase processedFieldInfo; // Read write buffer - if (HlslKnownTypes.IsReadWriteBufferType(fieldType)) + if (HlslKnownTypes.IsBufferType(fieldType) && + (HeapType)fieldType.GetProperty(nameof(Buffer2.HeapType)).GetValue(fieldValue) == HeapType.Default) { // Root parameter for a read write buffer DescriptorRange range = new DescriptorRange(DescriptorRangeType.UnorderedAccessView, 1, readWriteBuffersCount); descriptorRanges.Add(range); // Reference to the underlying buffer - object readWriteBuffer = fieldInfo.GetValue(ShaderInstance); - GraphicsResource resource = (GraphicsResource)fieldType.GetProperty(nameof(RWBufferResource.Buffer)).GetValue(readWriteBuffer); - readWriteBuffers.Add(resource); + readWriteBuffers.Add((GraphicsResource)fieldValue); string typeName = HlslKnownTypes.GetMappedName(fieldType); processedFieldInfo = new ReadWriteBufferFieldInfo(typeName, fieldName, readWriteBuffersCount++); diff --git a/src/ComputeSharp.Shaders/ShaderResource.cs b/src/ComputeSharp.Shaders/ShaderResource.cs deleted file mode 100644 index 5ca82346..00000000 --- a/src/ComputeSharp.Shaders/ShaderResource.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using ComputeSharp.Graphics.Buffers; - -namespace ComputeSharp.Shaders -{ - public abstract class ShaderResource - { - } - - public sealed class RWBufferResource : ShaderResource where T : unmanaged - { - internal RWBufferResource(Buffer buffer) => Buffer = buffer; - - public Buffer Buffer { get; } - - public T this[uint index] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - } - - public static class RWBufferResourceExtensions - { - public static RWBufferResource GetGpuResource(this Buffer buffer) where T : unmanaged - { - return new RWBufferResource(buffer); - } - } -} diff --git a/src/DirectX12ComputeShaderSample/Program.cs b/src/DirectX12ComputeShaderSample/Program.cs index 3b280ca1..9bee1a7d 100644 --- a/src/DirectX12ComputeShaderSample/Program.cs +++ b/src/DirectX12ComputeShaderSample/Program.cs @@ -20,16 +20,12 @@ namespace DirectX12ComputeShaderSample array[i] = i + 1; } - using Buffer gpuBuffer = Buffer.UnorderedAccess.New(Gpu.Default, array.AsSpan()); - - // Variables for closure - uint size = (uint)width; - var data = gpuBuffer.GetGpuResource(); + using Buffer2 gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array.AsSpan()); // Shader body Action action = id => { - data[id.X + id.Y * size] *= 2; + gpuBuffer[id.X + id.Y * (uint)width] *= 2; }; // Run the shader From d75e84c61881db0fb2be3637c70c78a596ca60ae Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 13:34:58 +0200 Subject: [PATCH 19/42] Removed unnecessary types --- .../BufferDescription.cs | 18 -- src/ComputeSharp.Graphics/BufferFlags.cs | 19 -- .../Buffers/Buffer.Constant.cs | 25 -- .../Buffers/Buffer.UnorderedAccess.cs | 25 -- src/ComputeSharp.Graphics/Buffers/Buffer.cs | 232 ------------------ src/DirectX12ComputeShaderSample/Program.cs | 2 - 6 files changed, 321 deletions(-) delete mode 100644 src/ComputeSharp.Graphics/BufferDescription.cs delete mode 100644 src/ComputeSharp.Graphics/BufferFlags.cs delete mode 100644 src/ComputeSharp.Graphics/Buffers/Buffer.Constant.cs delete mode 100644 src/ComputeSharp.Graphics/Buffers/Buffer.UnorderedAccess.cs delete mode 100644 src/ComputeSharp.Graphics/Buffers/Buffer.cs diff --git a/src/ComputeSharp.Graphics/BufferDescription.cs b/src/ComputeSharp.Graphics/BufferDescription.cs deleted file mode 100644 index efc94f66..00000000 --- a/src/ComputeSharp.Graphics/BufferDescription.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace ComputeSharp.Graphics -{ - public struct BufferDescription - { - public BufferDescription(int sizeInBytes, BufferFlags bufferFlags, GraphicsHeapType heapType) - { - SizeInBytes = sizeInBytes; - Flags = bufferFlags; - HeapType = heapType; - } - - public int SizeInBytes; - - public BufferFlags Flags; - - public GraphicsHeapType HeapType; - } -} diff --git a/src/ComputeSharp.Graphics/BufferFlags.cs b/src/ComputeSharp.Graphics/BufferFlags.cs deleted file mode 100644 index ce2b64ed..00000000 --- a/src/ComputeSharp.Graphics/BufferFlags.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; - -namespace ComputeSharp.Graphics -{ - [Flags] - public enum BufferFlags - { - None = 0, - ConstantBuffer = 1, - IndexBuffer = 2, - VertexBuffer = 4, - RenderTarget = 8, - ShaderResource = 16, - UnorderedAccess = 32, - StructuredBuffer = 64, - RawBuffer = 512, - ArgumentBuffer = 1024 - } -} diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer.Constant.cs b/src/ComputeSharp.Graphics/Buffers/Buffer.Constant.cs deleted file mode 100644 index b1081bc6..00000000 --- a/src/ComputeSharp.Graphics/Buffers/Buffer.Constant.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; - -namespace ComputeSharp.Graphics.Buffers -{ - public partial class Buffer - { - public static class Constant - { - public static unsafe Buffer New(GraphicsDevice device, int size, GraphicsHeapType heapType = GraphicsHeapType.Upload) - { - return Buffer.New(device, size, BufferFlags.ConstantBuffer, heapType); - } - - public static unsafe Buffer New(GraphicsDevice device, in T data, GraphicsHeapType heapType = GraphicsHeapType.Upload) where T : unmanaged - { - return Buffer.New(device, data, BufferFlags.ConstantBuffer, heapType); - } - - public static unsafe Buffer New(GraphicsDevice device, Span data, GraphicsHeapType heapType = GraphicsHeapType.Upload) where T : unmanaged - { - return Buffer.New(device, data, BufferFlags.ConstantBuffer, heapType); - } - } - } -} diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer.UnorderedAccess.cs b/src/ComputeSharp.Graphics/Buffers/Buffer.UnorderedAccess.cs deleted file mode 100644 index 44d2aca3..00000000 --- a/src/ComputeSharp.Graphics/Buffers/Buffer.UnorderedAccess.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; - -namespace ComputeSharp.Graphics.Buffers -{ - public partial class Buffer - { - public static class UnorderedAccess - { - public static unsafe Buffers.Buffer New(GraphicsDevice device, int size, GraphicsHeapType heapType = GraphicsHeapType.Default) - { - return Buffers.Buffer.New(device, size, BufferFlags.UnorderedAccess, heapType); - } - - public static unsafe Buffer New(GraphicsDevice device, in T data, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged - { - return Buffers.Buffer.New(device, data, BufferFlags.UnorderedAccess, heapType); - } - - public static unsafe Buffer New(GraphicsDevice device, Span data, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged - { - return Buffers.Buffer.New(device, data, BufferFlags.UnorderedAccess, heapType); - } - } - } -} \ No newline at end of file diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer.cs b/src/ComputeSharp.Graphics/Buffers/Buffer.cs deleted file mode 100644 index 9b8c2728..00000000 --- a/src/ComputeSharp.Graphics/Buffers/Buffer.cs +++ /dev/null @@ -1,232 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using ComputeSharp.Graphics.Buffers.Abstract; -using ComputeSharp.Graphics.Helpers; -using SharpDX.Direct3D12; - -namespace ComputeSharp.Graphics.Buffers -{ - public partial class Buffer : GraphicsResource - { - protected Buffer(GraphicsDevice device) : base(device) - { - } - - public BufferDescription Description { get; private set; } - - public int SizeInBytes => Description.SizeInBytes; - - public BufferFlags Flags => Description.Flags; - - public GraphicsHeapType Usage => Description.HeapType; - - public int ElementCount { get; protected set; } - - public T[] GetData() where T : unmanaged - { - T[] data = new T[SizeInBytes / Unsafe.SizeOf()]; - GetData(data.AsSpan()); - - return data; - } - - public unsafe void GetData(ref T data, int offsetInBytes = 0) where T : unmanaged - { - fixed (T* pointer = &data) - { - GetData(new Span(pointer, 1), offsetInBytes); - } - } - - public void GetData(Span data, int offsetInBytes = 0) where T : unmanaged - { - if (Usage == GraphicsHeapType.Default) - { - using Buffer readbackBaffer = New(GraphicsDevice, data.Length, BufferFlags.None, GraphicsHeapType.Readback); - using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); - - copyCommandList.CopyBufferRegion(this, offsetInBytes, readbackBaffer, 0, data.Length * Unsafe.SizeOf()); - copyCommandList.Flush(true); - - readbackBaffer.GetData(data); - } - else - { - Map(0); - MemoryHelper.Copy(MappedResource + offsetInBytes, data); - Unmap(0); - } - } - - public unsafe void SetData(in T data, int offsetInBytes = 0) where T : unmanaged - { - fixed (T* pointer = &data) - { - SetData(new Span(pointer, 1), offsetInBytes); - } - } - - public void SetData(Span data, int offsetInBytes = 0) where T : unmanaged - { - if (Usage == GraphicsHeapType.Default) - { - using Buffer uploadBuffer = New(GraphicsDevice, data, BufferFlags.None, GraphicsHeapType.Upload); - using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); - - copyCommandList.CopyBufferRegion(uploadBuffer, 0, this, offsetInBytes, data.Length * Unsafe.SizeOf()); - copyCommandList.Flush(true); - } - else - { - Map(0); - MemoryHelper.Copy(data, MappedResource + offsetInBytes); - Unmap(0); - } - } - - public Buffers.Buffer ToReadback() - { - BufferDescription description = Description; - description.HeapType = GraphicsHeapType.Readback; - description.Flags = BufferFlags.None; - - return New(GraphicsDevice, description); - } - - public static Buffers.Buffer New(GraphicsDevice device, BufferDescription description) - { - return new Buffers.Buffer(device).InitializeFrom(description); - } - - public static Buffers.Buffer New(GraphicsDevice device, int size, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) - { - return New(device, new BufferDescription(size, bufferFlags, heapType)); - } - - public static Buffer New(GraphicsDevice device, int elementCount, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged - { - int bufferSize = elementCount * Unsafe.SizeOf(); - - return new Buffer(device, bufferSize, bufferFlags, heapType); - } - - public static unsafe Buffer New(GraphicsDevice device, in T data, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged - { - fixed (T* pointer = &data) - { - return New(device, new Span(pointer, 1), bufferFlags, heapType); - } - } - - public static Buffer New(GraphicsDevice device, Span data, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged - { - Buffer buffer = New(device, data.Length, bufferFlags, heapType); - buffer.SetData(data); - - return buffer; - } - - protected internal Buffers.Buffer InitializeFrom(BufferDescription description) - { - ResourceStates resourceStates = ResourceStates.Common; - - if (description.HeapType == GraphicsHeapType.Upload) - { - resourceStates = ResourceStates.GenericRead; - } - else if (description.HeapType == GraphicsHeapType.Readback) - { - resourceStates = ResourceStates.CopyDestination; - } - - NativeResource ??= GraphicsDevice.NativeDevice.CreateCommittedResource( - new HeapProperties((HeapType)description.HeapType), HeapFlags.None, - ConvertToNativeDescription(description), resourceStates); - - Description = description; - - (NativeCpuDescriptorHandle, NativeGpuDescriptorHandle) = description.Flags switch - { - BufferFlags.ConstantBuffer => CreateConstantBufferView(), - BufferFlags.ShaderResource => CreateShaderResourceView(), - BufferFlags.UnorderedAccess => CreateUnorderedAccessView(), - _ => default - }; - - return this; - } - - private static ResourceDescription ConvertToNativeDescription(BufferDescription description) - { - int size = description.SizeInBytes; - ResourceFlags flags = ResourceFlags.None; - - if (description.Flags.HasFlag(BufferFlags.UnorderedAccess)) - { - flags |= ResourceFlags.AllowUnorderedAccess; - //flags |= ResourceFlags.AllowSimultaneousAccess; - } - - return ResourceDescription.Buffer(size, flags); - } - - private (CpuDescriptorHandle, GpuDescriptorHandle) CreateConstantBufferView() - { - (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1); - - int constantBufferSize = (SizeInBytes + 255) & ~255; - - ConstantBufferViewDescription cbvDescription = new ConstantBufferViewDescription - { - BufferLocation = NativeResource.GPUVirtualAddress, - SizeInBytes = constantBufferSize - }; - - GraphicsDevice.NativeDevice.CreateConstantBufferView(cbvDescription, cpuHandle); - - return (cpuHandle, gpuHandle); - } - - private (CpuDescriptorHandle, GpuDescriptorHandle) CreateShaderResourceView() - { - (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1); - GraphicsDevice.NativeDevice.CreateShaderResourceView(NativeResource, null, cpuHandle); - - return (cpuHandle, gpuHandle); - } - - private (CpuDescriptorHandle, GpuDescriptorHandle) CreateUnorderedAccessView() - { - (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1); - - var description = new UnorderedAccessViewDescription - { - Format = SharpDX.DXGI.Format.R32_Float, - Dimension = UnorderedAccessViewDimension.Buffer, - Buffer = - { - ElementCount = ElementCount, - } - }; - - GraphicsDevice.NativeDevice.CreateUnorderedAccessView(NativeResource, null, description, cpuHandle); - - return (cpuHandle, gpuHandle); - } - } - - public class Buffer : Buffers.Buffer where T : unmanaged - { - protected internal Buffer(GraphicsDevice device, int size, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) : base(device) - { - ElementSize = Unsafe.SizeOf(); - ElementCount = size / ElementSize; - - InitializeFrom(new BufferDescription(size, bufferFlags, heapType)); - } - - public int ElementSize { get; } - - public T[] GetData() => GetData(); - } -} diff --git a/src/DirectX12ComputeShaderSample/Program.cs b/src/DirectX12ComputeShaderSample/Program.cs index 9bee1a7d..347d721b 100644 --- a/src/DirectX12ComputeShaderSample/Program.cs +++ b/src/DirectX12ComputeShaderSample/Program.cs @@ -2,8 +2,6 @@ using System.Linq; using ComputeSharp; using ComputeSharp.Graphics.Buffers; -using ComputeSharp.Shaders; -using Buffer = ComputeSharp.Graphics.Buffers.Buffer; namespace DirectX12ComputeShaderSample { From acf2aa9fce1c1ed0fe6f36cd0c801fe63b6f8729 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 13:37:58 +0200 Subject: [PATCH 20/42] Renamed Buffer2 type --- .../Buffers/{Buffer2.cs => Buffer.cs} | 16 ++++++++-------- .../Buffers/BufferExtensions.cs | 18 +++++++++--------- .../Mappings/HlslKnownTypes.cs | 8 ++++---- src/ComputeSharp.Shaders/ShaderLoader.cs | 2 +- src/DirectX12ComputeShaderSample/Program.cs | 2 +- 5 files changed, 23 insertions(+), 23 deletions(-) rename src/ComputeSharp.Graphics/Buffers/{Buffer2.cs => Buffer.cs} (90%) diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer2.cs b/src/ComputeSharp.Graphics/Buffers/Buffer.cs similarity index 90% rename from src/ComputeSharp.Graphics/Buffers/Buffer2.cs rename to src/ComputeSharp.Graphics/Buffers/Buffer.cs index 1a17cf3a..1cbc6ad9 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer2.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer.cs @@ -11,15 +11,15 @@ namespace ComputeSharp.Graphics.Buffers /// A representing a typed buffer stored on GPU memory /// /// The type of items stored on the buffer - public sealed class Buffer2 : GraphicsResource where T : unmanaged + public sealed class Buffer : GraphicsResource where T : unmanaged { /// - /// Creates a new instance with the specified parameters + /// Creates a new instance with the specified parameters /// /// The associated with the current instance /// The number of items to store in the current buffer /// The heap type for the current buffer - internal Buffer2(GraphicsDevice device, int size, HeapType heapType) : base(device) + internal Buffer(GraphicsDevice device, int size, HeapType heapType) : base(device) { Size = size; ElementSizeInBytes = Unsafe.SizeOf(); @@ -120,7 +120,7 @@ namespace ComputeSharp.Graphics.Buffers } /// - /// Reads the contents of the current instance and returns an array + /// Reads the contents of the current instance and returns an array /// /// A array with the contents of the current buffer [Pure] @@ -133,7 +133,7 @@ namespace ComputeSharp.Graphics.Buffers } /// - /// Reads the contents of the current instance and writes them into a target + /// Reads the contents of the current instance and writes them into a target /// /// The input to write data to public void GetData(Span span) @@ -141,7 +141,7 @@ namespace ComputeSharp.Graphics.Buffers // Create a temporary readback buffer if the current buffer type is read write (can't be read from directly) if (HeapType == HeapType.Default) { - using Buffer2 readbackBaffer = new Buffer2(GraphicsDevice, Size, HeapType.Readback); + using Buffer readbackBaffer = new Buffer(GraphicsDevice, Size, HeapType.Readback); using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); copyCommandList.CopyBufferRegion(this, 0, readbackBaffer, 0, SizeInBytes); @@ -159,7 +159,7 @@ namespace ComputeSharp.Graphics.Buffers } /// - /// Writes the contents of a given to the current instance + /// Writes the contents of a given to the current instance /// /// The input to read data from public void SetData(Span span) @@ -167,7 +167,7 @@ namespace ComputeSharp.Graphics.Buffers // Create a temporary upload buffer if the current buffer type is read write (can't be written to directly) if (HeapType == HeapType.Default) { - using Buffer2 uploadBuffer = new Buffer2(GraphicsDevice, Size, HeapType.Upload); + using Buffer uploadBuffer = new Buffer(GraphicsDevice, Size, HeapType.Upload); using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); uploadBuffer.SetData(span); diff --git a/src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs b/src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs index 318698bb..e424961e 100644 --- a/src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs +++ b/src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs @@ -16,12 +16,12 @@ namespace ComputeSharp.Graphics.Buffers /// The type of items to store in the buffer /// The instance to use to allocate the buffer /// The size of the buffer to allocate - /// A zeroed read write instance of size + /// A zeroed read write instance of size [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Buffer2 AllocateReadWriteBuffer(this GraphicsDevice device, int size) where T : unmanaged + public static Buffer AllocateReadWriteBuffer(this GraphicsDevice device, int size) where T : unmanaged { - return new Buffer2(device, size, HeapType.Default); + return new Buffer(device, size, HeapType.Default); } /// @@ -30,12 +30,12 @@ namespace ComputeSharp.Graphics.Buffers /// 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 + /// A read write instance with the contents of the input [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Buffer2 AllocateReadWriteBuffer(this GraphicsDevice device, Span span) where T : unmanaged + public static Buffer AllocateReadWriteBuffer(this GraphicsDevice device, Span span) where T : unmanaged { - Buffer2 buffer = new Buffer2(device, span.Length, HeapType.Default); + Buffer buffer = new Buffer(device, span.Length, HeapType.Default); buffer.SetData(span); return buffer; @@ -47,12 +47,12 @@ namespace ComputeSharp.Graphics.Buffers /// 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 constant instance with the contents of the input + /// A constant instance with the contents of the input [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Buffer2 AllocateConstantBuffer(this GraphicsDevice device, Span span) where T : unmanaged + public static Buffer AllocateConstantBuffer(this GraphicsDevice device, Span span) where T : unmanaged { - Buffer2 buffer = new Buffer2(device, span.Length, HeapType.Upload); + Buffer buffer = new Buffer(device, span.Length, HeapType.Upload); buffer.SetData(span); return buffer; diff --git a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs index 17eeb5ba..e1847415 100644 --- a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs @@ -27,7 +27,7 @@ namespace ComputeSharp.Shaders.Mappings [typeof(Vector4).FullName] = "float4", [typeof(Matrix4x4).FullName] = "float4x4", [typeof(ThreadIds).FullName] = "uint3", - [typeof(Buffer2<>).FullName] = "RWBuffer" + [typeof(Buffer<>).FullName] = "RWBuffer" }; /// @@ -54,13 +54,13 @@ namespace ComputeSharp.Shaders.Mappings type == typeof(Matrix4x4); /// - /// Checks whether or not the input type is a value + /// Checks whether or not the input type is a value /// /// The input instance to check - /// A indicating whether the input is in fact a instance + /// A indicating whether the input is in fact a instance [Pure] public static bool IsBufferType(Type type) => type.IsGenericType && - type.GetGenericTypeDefinition() == typeof(Buffer2<>); + type.GetGenericTypeDefinition() == typeof(Buffer<>); /// /// Checks whether or not a given is an HLSL known type diff --git a/src/ComputeSharp.Shaders/ShaderLoader.cs b/src/ComputeSharp.Shaders/ShaderLoader.cs index 05d6d235..a0f362db 100644 --- a/src/ComputeSharp.Shaders/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/ShaderLoader.cs @@ -117,7 +117,7 @@ namespace ComputeSharp.Shaders // Read write buffer if (HlslKnownTypes.IsBufferType(fieldType) && - (HeapType)fieldType.GetProperty(nameof(Buffer2.HeapType)).GetValue(fieldValue) == HeapType.Default) + (HeapType)fieldType.GetProperty(nameof(Buffer.HeapType)).GetValue(fieldValue) == HeapType.Default) { // Root parameter for a read write buffer DescriptorRange range = new DescriptorRange(DescriptorRangeType.UnorderedAccessView, 1, readWriteBuffersCount); diff --git a/src/DirectX12ComputeShaderSample/Program.cs b/src/DirectX12ComputeShaderSample/Program.cs index 347d721b..78958d04 100644 --- a/src/DirectX12ComputeShaderSample/Program.cs +++ b/src/DirectX12ComputeShaderSample/Program.cs @@ -18,7 +18,7 @@ namespace DirectX12ComputeShaderSample array[i] = i + 1; } - using Buffer2 gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array.AsSpan()); + using Buffer gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array.AsSpan()); // Shader body Action action = id => From 13ee3e942209d4c23f23a68cd8bdf4b4c3fd01d5 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 13:48:35 +0200 Subject: [PATCH 21/42] Added specific writeable and readonly buffer types --- src/ComputeSharp.Graphics/Buffers/Buffer.cs | 15 ++-------- .../Buffers/BufferExtensions.cs | 18 +++++------ .../Buffers/ReadOnlyBuffer{T}.cs | 26 ++++++++++++++++ .../Buffers/ReadWriteBuffer{T}.cs | 30 +++++++++++++++++++ .../Mappings/HlslKnownTypes.cs | 10 +++---- src/ComputeSharp.Shaders/ShaderLoader.cs | 4 +-- src/DirectX12ComputeShaderSample/Program.cs | 2 +- 7 files changed, 74 insertions(+), 31 deletions(-) create mode 100644 src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs create mode 100644 src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer.cs b/src/ComputeSharp.Graphics/Buffers/Buffer.cs index 1cbc6ad9..703bc324 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer.cs @@ -11,7 +11,7 @@ namespace ComputeSharp.Graphics.Buffers /// A representing a typed buffer stored on GPU memory /// /// The type of items stored on the buffer - public sealed class Buffer : GraphicsResource where T : unmanaged + public class Buffer : GraphicsResource where T : unmanaged { /// /// Creates a new instance with the specified parameters @@ -19,7 +19,7 @@ namespace ComputeSharp.Graphics.Buffers /// The associated with the current instance /// The number of items to store in the current buffer /// The heap type for the current buffer - internal Buffer(GraphicsDevice device, int size, HeapType heapType) : base(device) + protected internal Buffer(GraphicsDevice device, int size, HeapType heapType) : base(device) { Size = size; ElementSizeInBytes = Unsafe.SizeOf(); @@ -108,17 +108,6 @@ namespace ComputeSharp.Graphics.Buffers /// public HeapType HeapType { get; } - /// - /// Gets or sets a single value from the current 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 - public T this[uint i] - { - get => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); - set => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); - } - /// /// Reads the contents of the current instance and returns an array /// diff --git a/src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs b/src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs index e424961e..ae6e41c5 100644 --- a/src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs +++ b/src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs @@ -16,12 +16,12 @@ namespace ComputeSharp.Graphics.Buffers /// The type of items to store in the buffer /// The instance to use to allocate the buffer /// The size of the buffer to allocate - /// A zeroed read write instance of size + /// A zeroed instance of size [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Buffer AllocateReadWriteBuffer(this GraphicsDevice device, int size) where T : unmanaged + public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, int size) where T : unmanaged { - return new Buffer(device, size, HeapType.Default); + return new ReadWriteBuffer(device, size); } /// @@ -30,12 +30,12 @@ namespace ComputeSharp.Graphics.Buffers /// 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 + /// A read write instance with the contents of the input [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Buffer AllocateReadWriteBuffer(this GraphicsDevice device, Span span) where T : unmanaged + public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, Span span) where T : unmanaged { - Buffer buffer = new Buffer(device, span.Length, HeapType.Default); + ReadWriteBuffer buffer = new ReadWriteBuffer(device, span.Length); buffer.SetData(span); return buffer; @@ -47,12 +47,12 @@ namespace ComputeSharp.Graphics.Buffers /// 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 constant instance with the contents of the input + /// A constant instance with the contents of the input [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Buffer AllocateConstantBuffer(this GraphicsDevice device, Span span) where T : unmanaged + public static ReadOnlyBuffer AllocateConstantBuffer(this GraphicsDevice device, Span span) where T : unmanaged { - Buffer buffer = new Buffer(device, span.Length, HeapType.Upload); + ReadOnlyBuffer buffer = new ReadOnlyBuffer(device, span.Length); buffer.SetData(span); return buffer; diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs new file mode 100644 index 00000000..c86d4084 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs @@ -0,0 +1,26 @@ +using System; +using SharpDX.Direct3D12; + +namespace ComputeSharp.Graphics.Buffers +{ + /// + /// A representing a typed read write buffer stored on GPU memory + /// + /// The type of items stored on the buffer + public sealed class ReadOnlyBuffer : Buffer where T : unmanaged + { + /// + /// Creates a new instance with the specified parameters + /// + /// The associated with the current instance + /// The number of items to store in the current buffer + internal ReadOnlyBuffer(GraphicsDevice device, int size) : base(device, size, HeapType.Upload) { } + + /// + /// Gets a single value from the current read write buffer + /// + /// The index 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[uint i] => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); + } +} diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs new file mode 100644 index 00000000..ded6d1e9 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs @@ -0,0 +1,30 @@ +using System; +using SharpDX.Direct3D12; + +namespace ComputeSharp.Graphics.Buffers +{ + /// + /// A representing a typed read write buffer stored on GPU memory + /// + /// The type of items stored on the buffer + public sealed class ReadWriteBuffer : Buffer where T : unmanaged + { + /// + /// Creates a new instance with the specified parameters + /// + /// The associated with the current instance + /// The number of items to store in the current buffer + internal ReadWriteBuffer(GraphicsDevice device, int size) : base(device, size, HeapType.Default) { } + + /// + /// Gets or sets a single value from the current read write 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 + public T this[uint i] + { + get => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); + set => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); + } + } +} diff --git a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs index e1847415..32bf790d 100644 --- a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs @@ -27,7 +27,7 @@ namespace ComputeSharp.Shaders.Mappings [typeof(Vector4).FullName] = "float4", [typeof(Matrix4x4).FullName] = "float4x4", [typeof(ThreadIds).FullName] = "uint3", - [typeof(Buffer<>).FullName] = "RWBuffer" + [typeof(ReadWriteBuffer<>).FullName] = "RWBuffer" }; /// @@ -54,13 +54,13 @@ namespace ComputeSharp.Shaders.Mappings type == typeof(Matrix4x4); /// - /// Checks whether or not the input type is a value + /// Checks whether or not the input type is a value /// /// The input instance to check - /// A indicating whether the input is in fact a instance + /// A indicating whether the input is in fact a instance [Pure] - public static bool IsBufferType(Type type) => type.IsGenericType && - type.GetGenericTypeDefinition() == typeof(Buffer<>); + public static bool IsReadWriteBufferType(Type type) => type.IsGenericType && + type.GetGenericTypeDefinition() == typeof(ReadWriteBuffer<>); /// /// Checks whether or not a given is an HLSL known type diff --git a/src/ComputeSharp.Shaders/ShaderLoader.cs b/src/ComputeSharp.Shaders/ShaderLoader.cs index a0f362db..aabd5bb8 100644 --- a/src/ComputeSharp.Shaders/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/ShaderLoader.cs @@ -4,7 +4,6 @@ using System.Diagnostics.Contracts; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; -using ComputeSharp.Graphics.Buffers; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Shaders.Mappings; using ComputeSharp.Shaders.Renderer.Models.Fields; @@ -116,8 +115,7 @@ namespace ComputeSharp.Shaders FieldInfoBase processedFieldInfo; // Read write buffer - if (HlslKnownTypes.IsBufferType(fieldType) && - (HeapType)fieldType.GetProperty(nameof(Buffer.HeapType)).GetValue(fieldValue) == HeapType.Default) + if (HlslKnownTypes.IsReadWriteBufferType(fieldType)) { // Root parameter for a read write buffer DescriptorRange range = new DescriptorRange(DescriptorRangeType.UnorderedAccessView, 1, readWriteBuffersCount); diff --git a/src/DirectX12ComputeShaderSample/Program.cs b/src/DirectX12ComputeShaderSample/Program.cs index 78958d04..ed6ba932 100644 --- a/src/DirectX12ComputeShaderSample/Program.cs +++ b/src/DirectX12ComputeShaderSample/Program.cs @@ -18,7 +18,7 @@ namespace DirectX12ComputeShaderSample array[i] = i + 1; } - using Buffer gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array.AsSpan()); + using ReadWriteBuffer gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array.AsSpan()); // Shader body Action action = id => From d015e97ca573bb498a30d45a2659ee942ae308f8 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 13:49:16 +0200 Subject: [PATCH 22/42] Minor code refactoring --- .../Buffers/{ => Extensions}/BufferExtensions.cs | 3 +-- src/DirectX12ComputeShaderSample/Program.cs | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) rename src/ComputeSharp.Graphics/Buffers/{ => Extensions}/BufferExtensions.cs (97%) diff --git a/src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs b/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs similarity index 97% rename from src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs rename to src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs index ae6e41c5..70dacfb5 100644 --- a/src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs +++ b/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs @@ -1,9 +1,8 @@ using System; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; -using SharpDX.Direct3D12; -namespace ComputeSharp.Graphics.Buffers +namespace ComputeSharp.Graphics.Buffers.Extensions { /// /// A with extension methods for the type to allocate buffers diff --git a/src/DirectX12ComputeShaderSample/Program.cs b/src/DirectX12ComputeShaderSample/Program.cs index ed6ba932..813bd5b1 100644 --- a/src/DirectX12ComputeShaderSample/Program.cs +++ b/src/DirectX12ComputeShaderSample/Program.cs @@ -2,6 +2,7 @@ using System.Linq; using ComputeSharp; using ComputeSharp.Graphics.Buffers; +using ComputeSharp.Graphics.Buffers.Extensions; namespace DirectX12ComputeShaderSample { From 19dc0172f4690095b39cfb7fdb6f71ad502b6205 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 13:59:56 +0200 Subject: [PATCH 23/42] Added new buffer extension APIs --- .../Buffers/Extensions/BufferExtensions.cs | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs b/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs index 70dacfb5..14767a10 100644 --- a/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs +++ b/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; namespace ComputeSharp.Graphics.Buffers.Extensions { @@ -23,6 +24,38 @@ namespace ComputeSharp.Graphics.Buffers.Extensions return new ReadWriteBuffer(device, size); } + /// + /// 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] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + 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 2D array with the data to copy on the allocated buffer + /// A read write instance with the contents of the input 2D array + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, T[,] array) where T : unmanaged + { + fixed (T* p = array) + { + Span span = new Span(p, array.Length); + return device.AllocateReadWriteBuffer(span); + } + } + /// /// Allocates a new read write buffer with the specified parameters /// @@ -40,6 +73,38 @@ namespace ComputeSharp.Graphics.Buffers.Extensions return buffer; } + /// + /// Allocates a new constant 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 constant instance with the contents of the input array + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadOnlyBuffer AllocateConstantBuffer(this GraphicsDevice device, T[] array) where T : unmanaged + { + return device.AllocateConstantBuffer(array.AsSpan()); + } + + /// + /// Allocates a new constant buffer with the specified parameters + /// + /// The type of items to store in the buffer + /// The instance to use to allocate the buffer + /// The input 2D array with the data to copy on the allocated buffer + /// A constant instance with the contents of the input 2D array + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe ReadOnlyBuffer AllocateConstantBuffer(this GraphicsDevice device, T[,] array) where T : unmanaged + { + fixed (T* p = array) + { + Span span = new Span(p, array.Length); + return device.AllocateConstantBuffer(span); + } + } + /// /// Allocates a new constant buffer with the specified parameters /// From 0db59766c34f7c5e5caa9cb246f1e31112580ab5 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 14:09:44 +0200 Subject: [PATCH 24/42] Code refactoring --- src/ComputeSharp.Shaders/ShaderLoader.cs | 2 +- .../ShaderSyntaxRewriter.cs | 72 +++++++------------ 2 files changed, 27 insertions(+), 47 deletions(-) diff --git a/src/ComputeSharp.Shaders/ShaderLoader.cs b/src/ComputeSharp.Shaders/ShaderLoader.cs index aabd5bb8..46b721e1 100644 --- a/src/ComputeSharp.Shaders/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/ShaderLoader.cs @@ -151,7 +151,7 @@ namespace ComputeSharp.Shaders MethodDecompiler.Instance.GetSyntaxTree(Action.Method, out SyntaxNode root, out SemanticModel semanticModel); // Rewrite the shader method (eg. to fix the type declarations) - ShaderSyntaxRewriter syntaxRewriter = new ShaderSyntaxRewriter(semanticModel, true); + ShaderSyntaxRewriter syntaxRewriter = new ShaderSyntaxRewriter(semanticModel); root = syntaxRewriter.Visit(root); // Get the thread ids identifier name and shader method body diff --git a/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs b/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs index 65063848..1f81ab64 100644 --- a/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs +++ b/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs @@ -1,33 +1,30 @@ -using System; -using System.Linq; -using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; namespace ComputeSharp.Shaders { + /// + /// A custom that processes C# methods to convert to HLSL + /// public class ShaderSyntaxRewriter : CSharpSyntaxRewriter { - private readonly int depth; - private readonly bool isTopLevel; - private readonly SemanticModel semanticModel; + /// + /// The instance to use to rewrite the decompiled code + /// + private readonly SemanticModel SemanticModel; - public ShaderSyntaxRewriter(SemanticModel semanticModel, bool isTopLevel = false, int depth = 0) - { - this.semanticModel = semanticModel; - this.isTopLevel = isTopLevel; - this.depth = depth; - } + /// + /// Creates a new instance with the specified parameters + /// + /// + public ShaderSyntaxRewriter(SemanticModel semanticModel) => SemanticModel = semanticModel; + /// public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node) { node = (MethodDeclarationSyntax)base.VisitMethodDeclaration(node); - if (isTopLevel && depth > 0) - { - node = node.ReplaceToken(node.Identifier, SyntaxFactory.Identifier($"Base_{depth}_{node.Identifier.ValueText}")); - } - SyntaxTokenList modifiers = new SyntaxTokenList(); if (node.Modifiers.Any(SyntaxKind.StaticKeyword)) @@ -38,6 +35,7 @@ namespace ComputeSharp.Shaders return node.ReplaceType(node.ReturnType).WithModifiers(modifiers); } + /// public override SyntaxNode VisitParameter(ParameterSyntax node) { node = (ParameterSyntax)base.VisitParameter(node); @@ -47,18 +45,23 @@ namespace ComputeSharp.Shaders return node; } + /// public override SyntaxNode VisitCastExpression(CastExpressionSyntax node) { node = (CastExpressionSyntax)base.VisitCastExpression(node); + return node.ReplaceType(node.Type); } + /// public override SyntaxNode VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node) { node = (LocalDeclarationStatementSyntax)base.VisitLocalDeclarationStatement(node); + return node.ReplaceType(node.Declaration.Type); } + /// public override SyntaxNode VisitObjectCreationExpression(ObjectCreationExpressionSyntax node) { node = (ObjectCreationExpressionSyntax)base.VisitObjectCreationExpression(node); @@ -68,48 +71,25 @@ namespace ComputeSharp.Shaders { return SyntaxFactory.CastExpression(node.Type, SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(0))); } - else - { - return SyntaxFactory.InvocationExpression(node.Type, node.ArgumentList); - } + + return SyntaxFactory.InvocationExpression(node.Type, node.ArgumentList); } + /// public override SyntaxNode VisitDefaultExpression(DefaultExpressionSyntax node) { node = (DefaultExpressionSyntax)base.VisitDefaultExpression(node); node = node.ReplaceType(node.Type); + return SyntaxFactory.CastExpression(node.Type, SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(0))); } + /// public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyntax node) { node = (MemberAccessExpressionSyntax)base.VisitMemberAccessExpression(node); - if (node.Expression is BaseExpressionSyntax) - { - if (isTopLevel) - { - return SyntaxFactory.IdentifierName($"Base_{depth + 1}_{node.Name}"); - } - else - { - SymbolInfo memberSymbolInfo = semanticModel.GetSymbolInfo(node.Name); - ISymbol? memberSymbol = memberSymbolInfo.Symbol ?? memberSymbolInfo.CandidateSymbols.FirstOrDefault(); - - if (memberSymbol != null) - { - return SyntaxFactory.IdentifierName($"{memberSymbol.ContainingType.Name}::{node.Name}"); - } - else - { - throw new InvalidOperationException(); - } - } - } - else - { - return node.ReplaceMember(semanticModel); - } + return node.ReplaceMember(SemanticModel); } } } From 4db7880ab7dd841083aa1e07c398f320824d84c2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 14:20:21 +0200 Subject: [PATCH 25/42] More minor code refactoring --- .../Extensions/{ => Windows.Foundation}/PointExtensions.cs | 0 .../Extensions/{ => Windows.Foundation}/RectExtensions.cs | 0 .../Extensions/{ => Windows.Foundation}/SizeExtensions.cs | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename src/ComputeSharp/Extensions/{ => Windows.Foundation}/PointExtensions.cs (100%) rename src/ComputeSharp/Extensions/{ => Windows.Foundation}/RectExtensions.cs (100%) rename src/ComputeSharp/Extensions/{ => Windows.Foundation}/SizeExtensions.cs (100%) diff --git a/src/ComputeSharp/Extensions/PointExtensions.cs b/src/ComputeSharp/Extensions/Windows.Foundation/PointExtensions.cs similarity index 100% rename from src/ComputeSharp/Extensions/PointExtensions.cs rename to src/ComputeSharp/Extensions/Windows.Foundation/PointExtensions.cs diff --git a/src/ComputeSharp/Extensions/RectExtensions.cs b/src/ComputeSharp/Extensions/Windows.Foundation/RectExtensions.cs similarity index 100% rename from src/ComputeSharp/Extensions/RectExtensions.cs rename to src/ComputeSharp/Extensions/Windows.Foundation/RectExtensions.cs diff --git a/src/ComputeSharp/Extensions/SizeExtensions.cs b/src/ComputeSharp/Extensions/Windows.Foundation/SizeExtensions.cs similarity index 100% rename from src/ComputeSharp/Extensions/SizeExtensions.cs rename to src/ComputeSharp/Extensions/Windows.Foundation/SizeExtensions.cs From 9b69b65ad752409b0cd4063dd29acbd0563b802c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 14:22:29 +0200 Subject: [PATCH 26/42] Moved Gpu class to the main ComputeSharp project --- src/ComputeSharp/ComputeSharp.csproj | 7 ++++++- .../Gpu.cs | 9 +++++++-- .../DirectX12ComputeShaderSample.csproj | 3 +-- 3 files changed, 14 insertions(+), 5 deletions(-) rename src/{DirectX12ComputeShaderSample => ComputeSharp}/Gpu.cs (88%) diff --git a/src/ComputeSharp/ComputeSharp.csproj b/src/ComputeSharp/ComputeSharp.csproj index 50891f1b..57aeb7e6 100644 --- a/src/ComputeSharp/ComputeSharp.csproj +++ b/src/ComputeSharp/ComputeSharp.csproj @@ -2,7 +2,8 @@ netstandard2.0 - true + 8.0 + enable @@ -12,4 +13,8 @@ + + + + diff --git a/src/DirectX12ComputeShaderSample/Gpu.cs b/src/ComputeSharp/Gpu.cs similarity index 88% rename from src/DirectX12ComputeShaderSample/Gpu.cs rename to src/ComputeSharp/Gpu.cs index 970ae4b7..2992cc39 100644 --- a/src/DirectX12ComputeShaderSample/Gpu.cs +++ b/src/ComputeSharp/Gpu.cs @@ -1,6 +1,5 @@ using System; using System.Linq; -using ComputeSharp; using ComputeSharp.Graphics; using ComputeSharp.Shaders; using ComputeSharp.Shaders.Renderer; @@ -10,12 +9,18 @@ using CommandList = ComputeSharp.Graphics.CommandList; using PipelineState = ComputeSharp.Graphics.PipelineState; using ShaderBytecode = SharpDX.Direct3D12.ShaderBytecode; -namespace DirectX12ComputeShaderSample +namespace ComputeSharp { + /// + /// A that acts as an entry-point for all the library APIs, exposing the available GPU devices + /// public static class Gpu { private static GraphicsDevice _Default; + /// + /// Gets the default instance for the current machine + /// public static GraphicsDevice Default => _Default ??= new GraphicsDevice(true); } diff --git a/src/DirectX12ComputeShaderSample/DirectX12ComputeShaderSample.csproj b/src/DirectX12ComputeShaderSample/DirectX12ComputeShaderSample.csproj index a9e9535e..33fc9464 100644 --- a/src/DirectX12ComputeShaderSample/DirectX12ComputeShaderSample.csproj +++ b/src/DirectX12ComputeShaderSample/DirectX12ComputeShaderSample.csproj @@ -6,8 +6,7 @@ - - + From 39c4e28d5b21524efd5f0a844ecf2223d2d3da46 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 14:33:33 +0200 Subject: [PATCH 27/42] More code refactoring --- .../Extensions/SyntaxNodeExtensions.cs | 67 +++++++++++++++++++ .../ShaderSyntaxRewriter.cs | 3 +- .../SyntaxNodeExtensions.cs | 45 ------------- 3 files changed, 69 insertions(+), 46 deletions(-) create mode 100644 src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs delete mode 100644 src/ComputeSharp.Shaders/SyntaxNodeExtensions.cs diff --git a/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs new file mode 100644 index 00000000..d7739008 --- /dev/null +++ b/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs @@ -0,0 +1,67 @@ +using System.Diagnostics.Contracts; +using System.Linq; +using ComputeSharp.Shaders.Mappings; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; + +namespace ComputeSharp.Shaders.Extensions +{ + /// + /// A with some extension methods for C# syntax nodes + /// + public static class SyntaxNodeExtensions + { + /// + /// Checks a value and replaces the value type to be HLSL compatible, if needed + /// + /// The type of the input instance + /// The input to check and modify if needed + /// The to use for the input node + /// A instance that represents a type compatible with HLSL + [Pure] + public static TRoot ReplaceType(this TRoot node, TypeSyntax type) where TRoot : SyntaxNode + { + string value = HlslKnownTypes.GetMappedName(type.ToString()); + + // If the HLSL mapped full type name equals the original type, just return the input node + if (value == type.ToString()) return node; + + // Process and return the type name + TypeSyntax newType = SyntaxFactory.ParseTypeName(value).WithLeadingTrivia(type.GetLeadingTrivia()).WithTrailingTrivia(type.GetTrailingTrivia()); + return node.ReplaceNode(type, newType); + } + + /// + /// Checks a instance and replaces it to be HLSL compatible, if needed + /// + /// The input to check and modify if needed + /// The to use to load symbols for the input node + /// A instance that is compatible with HLSL + [Pure] + public static SyntaxNode ReplaceMember(this MemberAccessExpressionSyntax node, SemanticModel semanticModel) + { + SymbolInfo containingMemberSymbolInfo = semanticModel.GetSymbolInfo(node.Expression); + SymbolInfo memberSymbolInfo = semanticModel.GetSymbolInfo(node.Name); + + ISymbol? memberSymbol = memberSymbolInfo.Symbol ?? memberSymbolInfo.CandidateSymbols.FirstOrDefault(); + + // If the input member has no symbol or is not a field, property or method, just return it + if (memberSymbol is null || + memberSymbol.Kind != SymbolKind.Field && + memberSymbol.Kind != SymbolKind.Property && + memberSymbol.Kind != SymbolKind.Method) + { + return node; + } + + // Process the input node if it's a known method invocation + if (HlslKnownMethods.TryGetMappedName(containingMemberSymbolInfo.Symbol, memberSymbol) is string value) + { + return SyntaxFactory.IdentifierName(value).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); + } + + return node; + } + } +} diff --git a/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs b/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs index 1f81ab64..17419fc0 100644 --- a/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs +++ b/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs @@ -1,4 +1,5 @@ -using Microsoft.CodeAnalysis; +using ComputeSharp.Shaders.Extensions; +using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; diff --git a/src/ComputeSharp.Shaders/SyntaxNodeExtensions.cs b/src/ComputeSharp.Shaders/SyntaxNodeExtensions.cs deleted file mode 100644 index 7660be33..00000000 --- a/src/ComputeSharp.Shaders/SyntaxNodeExtensions.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System.Linq; -using ComputeSharp.Shaders.Mappings; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; - -namespace ComputeSharp.Shaders -{ - public static class SyntaxNodeExtensions - { - public static TRoot ReplaceType(this TRoot node, TypeSyntax type) where TRoot : SyntaxNode - { - string value = HlslKnownTypes.GetMappedName(type.ToString()); - - if (value == type.ToString()) - { - return node; - } - else - { - return node.ReplaceNode(type, SyntaxFactory.ParseTypeName(value).WithLeadingTrivia(type.GetLeadingTrivia()).WithTrailingTrivia(type.GetTrailingTrivia())); - } - } - - public static SyntaxNode ReplaceMember(this MemberAccessExpressionSyntax node, SemanticModel semanticModel) - { - SymbolInfo containingMemberSymbolInfo = semanticModel.GetSymbolInfo(node.Expression); - SymbolInfo memberSymbolInfo = semanticModel.GetSymbolInfo(node.Name); - - ISymbol? memberSymbol = memberSymbolInfo.Symbol ?? memberSymbolInfo.CandidateSymbols.FirstOrDefault(); - - if (memberSymbol is null || (memberSymbol.Kind != SymbolKind.Field && memberSymbol.Kind != SymbolKind.Property && memberSymbol.Kind != SymbolKind.Method)) - { - return node; - } - - if (HlslKnownMethods.TryGetMappedName(containingMemberSymbolInfo.Symbol, memberSymbol) is string value) - { - return SyntaxFactory.IdentifierName(value).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); - } - - return node; - } - } -} From ce660d8aa710ab77f21276abace67f9a650b82bb Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 14:54:22 +0200 Subject: [PATCH 28/42] Added NumThreads struct --- .../Primitives/NumThreads.cs | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/ComputeSharp.Shaders/Primitives/NumThreads.cs diff --git a/src/ComputeSharp.Shaders/Primitives/NumThreads.cs b/src/ComputeSharp.Shaders/Primitives/NumThreads.cs new file mode 100644 index 00000000..06a6e852 --- /dev/null +++ b/src/ComputeSharp.Shaders/Primitives/NumThreads.cs @@ -0,0 +1,64 @@ +using System.Runtime.CompilerServices; + +namespace ComputeSharp +{ + /// + /// A that indicates the number of threads to use to run a given compute shader + /// + public readonly struct NumThreads + { + /// + /// The X id of the current thread + /// + public readonly int X; + + /// + /// The Y id of the current thread + /// + public readonly int Y; + + /// + /// The Z id of the current thread + /// + public readonly int Z; + + /// + /// Creates a new instance with the specified parameters + /// + /// The number of threads in the X axis + /// The number of threads in the Y axis + public NumThreads(int x, int y) + { + X = x; + Y = y; + Z = 1; + } + + /// + /// Creates a new instance with the specified parameters + /// + /// The number of threads in the X axis + /// The number of threads in the Y axis + /// The number of threads in the Z axis + public NumThreads(int x, int y, int z) + { + X = x; + Y = y; + Z = z; + } + + /// + /// Creates a new instance from a value tuple + /// + /// The tuple with the thread X and Y counts + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator NumThreads((int X, int Y) xy) => new NumThreads(xy.X, xy.Y); + + /// + /// Creates a new instance from a value tuple + /// + /// The tuple with the thread X, Y and Z counts + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator NumThreads((int X, int Y, int Z) xyz) => new NumThreads(xyz.X, xyz.Y, xyz.Z); + } +} From 0339cfb6d647e8b21afab3281f4604a7caccf46f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 14:55:46 +0200 Subject: [PATCH 29/42] Minor code refactoring --- src/ComputeSharp.Shaders/{ => Translation}/MethodDecompiler.cs | 2 +- src/ComputeSharp.Shaders/{ => Translation}/ShaderCompiler.cs | 2 +- src/ComputeSharp.Shaders/{ => Translation}/ShaderLoader.cs | 2 +- .../{ => Translation}/ShaderSyntaxRewriter.cs | 2 +- src/ComputeSharp/Gpu.cs | 1 + 5 files changed, 5 insertions(+), 4 deletions(-) rename src/ComputeSharp.Shaders/{ => Translation}/MethodDecompiler.cs (99%) rename src/ComputeSharp.Shaders/{ => Translation}/ShaderCompiler.cs (98%) rename src/ComputeSharp.Shaders/{ => Translation}/ShaderLoader.cs (99%) rename src/ComputeSharp.Shaders/{ => Translation}/ShaderSyntaxRewriter.cs (98%) diff --git a/src/ComputeSharp.Shaders/MethodDecompiler.cs b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs similarity index 99% rename from src/ComputeSharp.Shaders/MethodDecompiler.cs rename to src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs index 0c09ad67..b2352443 100644 --- a/src/ComputeSharp.Shaders/MethodDecompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs @@ -12,7 +12,7 @@ using ICSharpCode.Decompiler.Metadata; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; -namespace ComputeSharp.Shaders +namespace ComputeSharp.Shaders.Translation { /// /// A that is able to decompile arbitrary methods diff --git a/src/ComputeSharp.Shaders/ShaderCompiler.cs b/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs similarity index 98% rename from src/ComputeSharp.Shaders/ShaderCompiler.cs rename to src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs index fc21b010..712fbd85 100644 --- a/src/ComputeSharp.Shaders/ShaderCompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs @@ -3,7 +3,7 @@ using System.Diagnostics.Contracts; using DotNetDxc; using SharpDX.Direct3D12; -namespace ComputeSharp.Shaders +namespace ComputeSharp.Shaders.Translation { /// /// A that uses the APIs to compile compute shaders diff --git a/src/ComputeSharp.Shaders/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs similarity index 99% rename from src/ComputeSharp.Shaders/ShaderLoader.cs rename to src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 46b721e1..d6fd5fde 100644 --- a/src/ComputeSharp.Shaders/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -14,7 +14,7 @@ using SharpDX.Direct3D12; #pragma warning disable CS8618 // Non-nullable field is uninitialized -namespace ComputeSharp.Shaders +namespace ComputeSharp.Shaders.Translation { /// /// A responsible for loading and processing instances diff --git a/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs b/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs similarity index 98% rename from src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs rename to src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs index 17419fc0..63b4a0e9 100644 --- a/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs @@ -3,7 +3,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; -namespace ComputeSharp.Shaders +namespace ComputeSharp.Shaders.Translation { /// /// A custom that processes C# methods to convert to HLSL diff --git a/src/ComputeSharp/Gpu.cs b/src/ComputeSharp/Gpu.cs index 2992cc39..8fc43d6b 100644 --- a/src/ComputeSharp/Gpu.cs +++ b/src/ComputeSharp/Gpu.cs @@ -4,6 +4,7 @@ using ComputeSharp.Graphics; using ComputeSharp.Shaders; using ComputeSharp.Shaders.Renderer; using ComputeSharp.Shaders.Renderer.Models; +using ComputeSharp.Shaders.Translation; using SharpDX.Direct3D12; using CommandList = ComputeSharp.Graphics.CommandList; using PipelineState = ComputeSharp.Graphics.PipelineState; From d890f785508c1aa95c532cfd44e0f6c868e68c02 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 15:00:46 +0200 Subject: [PATCH 30/42] Added ShaderRunner class --- src/ComputeSharp.Shaders/ShaderRunner.cs | 65 ++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/ComputeSharp.Shaders/ShaderRunner.cs diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs new file mode 100644 index 00000000..e16a67c7 --- /dev/null +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -0,0 +1,65 @@ +using System; +using System.Linq; +using ComputeSharp.Graphics; +using ComputeSharp.Shaders.Renderer; +using ComputeSharp.Shaders.Renderer.Models; +using ComputeSharp.Shaders.Translation; +using SharpDX.Direct3D12; +using CommandList = ComputeSharp.Graphics.CommandList; +using PipelineState = ComputeSharp.Graphics.PipelineState; + +namespace ComputeSharp.Shaders +{ + /// + /// A responsible for performing all the necessary operations to compile and run a compute shader + /// + public static class ShaderRunner + { + /// + /// Compiles and runs the input shader on a target instance, with the specified parameters + /// + /// The to use to run the shader + /// The value that indicates the number of threads to run in each thread group + /// The value that indicates the number of thread groups to dispatch + /// The input representing the compute shader to run + public static void Run(GraphicsDevice device, NumThreads numThreads, NumThreads numGroups, Action action) + { + // Load the input shader + ShaderLoader shaderLoader = ShaderLoader.Load(action); + + // Render the loaded shader + ShaderInfo shaderInfo = new ShaderInfo + { + FieldsList = shaderLoader.FieldsInfo, + ThreadsX = numThreads.X, + ThreadsY = numThreads.Y, + ThreadsZ = numThreads.Z, + ThreadsIdsVariableName = shaderLoader.ThreadsIdsVariableName, + ShaderBody = shaderLoader.MethodBody + }; + string shaderSource = ShaderRenderer.Instance.Render(shaderInfo); + + // Compile the loaded shader to HLSL bytecode + ShaderBytecode shaderBytecode = ShaderCompiler.Instance.CompileShader(shaderSource); + + // Create the root signature for the pipeline and the pipeline state + RootSignatureDescription rootSignatureDescription = new RootSignatureDescription(RootSignatureFlags.None, shaderLoader.RootParameters); + RootSignature rootSignature = device.CreateRootSignature(rootSignatureDescription); + PipelineState pipelineState = new PipelineState(device, rootSignature, shaderBytecode); + + // Create the commands list + using CommandList commandList = new CommandList(device, CommandListType.Compute); + commandList.SetPipelineState(pipelineState); + + // Load the captured buffers + foreach (var buffer in shaderLoader.ReadWriteBuffers.Select((r, i) => (Resource: r, Index: i))) + { + commandList.SetComputeRootDescriptorTable(buffer.Index, buffer.Resource); + } + + // Dispatch and wait for completion + commandList.Dispatch(numGroups.X, numGroups.Y, numGroups.Z); + commandList.Flush(true); + } + } +} From 4c533ad2f63a6a8517c213b054a212d2750dc978 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 15:15:37 +0200 Subject: [PATCH 31/42] Code refactoring to the Gpu class, moved to extension class --- .../Extensions/GraphicsDeviceExtensions.cs | 28 ++++++++++ src/ComputeSharp/Gpu.cs | 55 +------------------ 2 files changed, 29 insertions(+), 54 deletions(-) create mode 100644 src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs diff --git a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs new file mode 100644 index 00000000..bf61fc4e --- /dev/null +++ b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs @@ -0,0 +1,28 @@ +using System; +using ComputeSharp.Graphics; +using ComputeSharp.Shaders; + +namespace ComputeSharp +{ + /// + /// A that contains extension methods for the type, used to run compute shaders + /// + public static class GraphicsDeviceExtensions + { + /// + /// The default number of threads in a thead froup + /// + public const int DefaultThreadGroupCount = 64; + + /// + /// Compiles and runs the input shader on a target instance, with the specified parameters + /// + /// The to use to run the shader + /// The number of threads to run on the X axis + /// The input representing the compute shader to run + public static void For(this GraphicsDevice device, int x, Action action) + { + ShaderRunner.Run(device, (x, 1), (1, 1), action); + } + } +} diff --git a/src/ComputeSharp/Gpu.cs b/src/ComputeSharp/Gpu.cs index 8fc43d6b..7af82c16 100644 --- a/src/ComputeSharp/Gpu.cs +++ b/src/ComputeSharp/Gpu.cs @@ -1,14 +1,4 @@ -using System; -using System.Linq; -using ComputeSharp.Graphics; -using ComputeSharp.Shaders; -using ComputeSharp.Shaders.Renderer; -using ComputeSharp.Shaders.Renderer.Models; -using ComputeSharp.Shaders.Translation; -using SharpDX.Direct3D12; -using CommandList = ComputeSharp.Graphics.CommandList; -using PipelineState = ComputeSharp.Graphics.PipelineState; -using ShaderBytecode = SharpDX.Direct3D12.ShaderBytecode; +using ComputeSharp.Graphics; namespace ComputeSharp { @@ -24,47 +14,4 @@ namespace ComputeSharp /// public static GraphicsDevice Default => _Default ??= new GraphicsDevice(true); } - - public static class GraphicsDeviceExtensions - { - public static void For(this GraphicsDevice device, int n, Action action) - { - // Load the input shader - ShaderLoader shaderLoader = ShaderLoader.Load(action); - - // Render the loaded shader - ShaderInfo shaderInfo = new ShaderInfo - { - FieldsList = shaderLoader.FieldsInfo, - ThreadsX = n, - ThreadsY = 1, - ThreadsZ = 1, - ThreadsIdsVariableName = shaderLoader.ThreadsIdsVariableName, - ShaderBody = shaderLoader.MethodBody - }; - string shaderSource = ShaderRenderer.Instance.Render(shaderInfo); - - // Compile the loaded shader to HLSL bytecode - ShaderBytecode shaderBytecode = ShaderCompiler.Instance.CompileShader(shaderSource); - - // Create the root signature for the pipeline and the pipeline state - RootSignatureDescription rootSignatureDescription = new RootSignatureDescription(RootSignatureFlags.None, shaderLoader.RootParameters); - RootSignature rootSignature = device.CreateRootSignature(rootSignatureDescription); - PipelineState pipelineState = new PipelineState(device, rootSignature, shaderBytecode); - - // Create the commands list - using CommandList commandList = new CommandList(device, CommandListType.Compute); - commandList.SetPipelineState(pipelineState); - - // Load the captured buffers - foreach (var buffer in shaderLoader.ReadWriteBuffers.Select((r, i) => (Resource: r, Index: i))) - { - commandList.SetComputeRootDescriptorTable(buffer.Index, buffer.Resource); - } - - // Dispatch and wait for completion - commandList.Dispatch(1, 1, 1); - commandList.Flush(true); - } - } } From e639320b5cfc4c6e6832acb72a5b602302e1779a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 15:20:41 +0200 Subject: [PATCH 32/42] Added DebuggerDisplay attribute to NumThreads struct --- src/ComputeSharp.Shaders/Primitives/NumThreads.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.Shaders/Primitives/NumThreads.cs b/src/ComputeSharp.Shaders/Primitives/NumThreads.cs index 06a6e852..ffad8e7e 100644 --- a/src/ComputeSharp.Shaders/Primitives/NumThreads.cs +++ b/src/ComputeSharp.Shaders/Primitives/NumThreads.cs @@ -1,10 +1,12 @@ -using System.Runtime.CompilerServices; +using System.Diagnostics; +using System.Runtime.CompilerServices; namespace ComputeSharp { /// /// A that indicates the number of threads to use to run a given compute shader /// + [DebuggerDisplay("(X: {X}, Y: {Y}, Z: {Z})")] public readonly struct NumThreads { /// From 8d79cb2aaee37dfb731df258e9a66bf8a344ae51 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 15:25:17 +0200 Subject: [PATCH 33/42] Added more public APIs, fixed code sample --- .../Extensions/GraphicsDeviceExtensions.cs | 50 ++++++++++++++++++- src/DirectX12ComputeShaderSample/Program.cs | 4 +- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs index bf61fc4e..2c72d15f 100644 --- a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs +++ b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs @@ -22,7 +22,55 @@ namespace ComputeSharp /// The input representing the compute shader to run public static void For(this GraphicsDevice device, int x, Action action) { - ShaderRunner.Run(device, (x, 1), (1, 1), action); + int groupsX = (x - 1) / DefaultThreadGroupCount + 1; + + ShaderRunner.Run(device, (DefaultThreadGroupCount, 1), (groupsX, 1), action); + } + + /// + /// Compiles and runs the input shader on a target instance, with the specified parameters + /// + /// The to use to run the shader + /// The number of threads to run on the X axis + /// The number of threads to run on the Y axis + /// The input representing the compute shader to run + public static void For(this GraphicsDevice device, int x, int y, Action action) + { + int + groupsX = (x - 1) / DefaultThreadGroupCount + 1, + groupsY = (y - 1) / DefaultThreadGroupCount + 1; + + ShaderRunner.Run(device, (DefaultThreadGroupCount, DefaultThreadGroupCount), (groupsX, groupsY), action); + } + + /// + /// Compiles and runs the input shader on a target instance, with the specified parameters + /// + /// The to use to run the shader + /// The number of threads to run on the X axis + /// The number of threads to run on the Y axis + /// The number of threads to run on the Z axis + /// The input representing the compute shader to run + public static void For(this GraphicsDevice device, int x, int y, int z, Action action) + { + int + groupsX = (x - 1) / DefaultThreadGroupCount + 1, + groupsY = (y - 1) / DefaultThreadGroupCount + 1, + groupsZ = (z - 1) / DefaultThreadGroupCount + 1; + + ShaderRunner.Run(device, (DefaultThreadGroupCount, DefaultThreadGroupCount, DefaultThreadGroupCount), (groupsX, groupsY, groupsZ), action); + } + + /// + /// Compiles and runs the input shader on a target instance, with the specified parameters + /// + /// The to use to run the shader + /// The value that indicates the number of threads to run in each thread group + /// The value that indicates the number of thread groups to dispatch + /// The input representing the compute shader to run + public static void For(this GraphicsDevice device, NumThreads numThreads, NumThreads numGroups, Action action) + { + ShaderRunner.Run(device, numThreads, numGroups, action); } } } diff --git a/src/DirectX12ComputeShaderSample/Program.cs b/src/DirectX12ComputeShaderSample/Program.cs index 813bd5b1..59e3cc8f 100644 --- a/src/DirectX12ComputeShaderSample/Program.cs +++ b/src/DirectX12ComputeShaderSample/Program.cs @@ -24,7 +24,9 @@ namespace DirectX12ComputeShaderSample // Shader body Action action = id => { - gpuBuffer[id.X + id.Y * (uint)width] *= 2; + uint offset = id.X + id.Y * (uint)width; + if (offset > 99) return; + gpuBuffer[offset] *= 2; }; // Run the shader From 13a011f31a35d8cd720fcdc5e93f4dd2a253561c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 15:29:33 +0200 Subject: [PATCH 34/42] Removed unnecessary GetMethodHandle method --- .../Translation/MethodDecompiler.cs | 53 +------------------ 1 file changed, 2 insertions(+), 51 deletions(-) diff --git a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs index b2352443..20c2a95a 100644 --- a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs @@ -108,8 +108,8 @@ namespace ComputeSharp.Shaders.Translation lock (Lock) { // Get the handle of the input method - GetMethodHandle(methodInfo, out string assemblyPath, out EntityHandle methodHandle); - + string assemblyPath = methodInfo.DeclaringType?.Assembly.Location ?? throw new InvalidOperationException(); + EntityHandle methodHandle = MetadataTokenHelpers.TryAsEntityHandle(methodInfo.MetadataToken) ?? throw new InvalidOperationException(); // Get or create a decompiler for the target assembly, and decompile the method if (!Decompilers.TryGetValue(assemblyPath, out CSharpDecompiler decompiler)) { @@ -129,55 +129,6 @@ namespace ComputeSharp.Shaders.Translation } } - /// - /// Loads the assembly path and method handle for the input instance - /// - /// The input to inspect - /// The path of the assembly containing the input method - /// The handle for the input method - private void GetMethodHandle(MethodInfo methodInfo, out string assemblyPath, out EntityHandle methodHandle) - { - // Try to get the method handle from the assembly of the declaring type - Type declaringType = methodInfo.DeclaringType ?? throw new InvalidOperationException("Invalid declaring type"); - string path = declaringType.Assembly.Location; - if (!string.IsNullOrEmpty(path)) - { - assemblyPath = path; - methodHandle = MetadataTokenHelpers.TryAsEntityHandle(methodInfo.MetadataToken) ?? throw new InvalidOperationException(); - } - else - { - // Try to get the PEFile and type definition for the method declaring type - if (!TypeDefinitions.TryGetValue(methodInfo.DeclaringType, out (PEFile PEFile, TypeDefinition TypeDefinition) tuple)) - { - foreach (PEFile peFile in PEFiles) - { - // If not available, browse through the loaded PEFiles to find the type definition - TypeDefinitionHandle typeDefinitionHandle = peFile.Metadata.TypeDefinitions.FirstOrDefault(t => t.GetFullTypeName(peFile.Metadata).ToString() == methodInfo.DeclaringType.FullName); - - if (!typeDefinitionHandle.IsNil) - { - tuple = (peFile, peFile.Metadata.GetTypeDefinition(typeDefinitionHandle)); - TypeDefinitions.Add(methodInfo.DeclaringType, tuple); - - break; - } - } - - if (tuple.PEFile is null) throw new InvalidOperationException($"Missing PEFile for declaring type {methodInfo.DeclaringType.FullName}"); - } - - PEFile peFileForMethod = tuple.PEFile; - TypeDefinition typeDefinition = tuple.TypeDefinition; - - // Retrieve the assembly path and method handle from the loaded PEFile and metadata - assemblyPath = peFileForMethod.FileName; - methodHandle = typeDefinition.GetMethods() - .Where(m => peFileForMethod.Metadata.StringComparer.Equals(peFileForMethod.Metadata.GetMethodDefinition(m).Name, methodInfo.Name)) - .First(m => peFileForMethod.Metadata.GetMethodDefinition(m).GetParameters().Count == methodInfo.GetParameters().Length); - } - } - /// /// Creates a new instance targeting a given assembly /// From 3939c0eac169cf59f0fbf566df4ad666d52506dd Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 15:54:25 +0200 Subject: [PATCH 35/42] Added automatic threads number and group count optimization --- src/ComputeSharp.Graphics/GraphicsDevice.cs | 6 ++++ .../Extensions/GraphicsDeviceExtensions.cs | 32 ++++++------------- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs index 893b4a2c..8aeb3983 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs @@ -15,6 +15,7 @@ namespace ComputeSharp.Graphics if (enableDebugLayer) DebugInterface.Get().EnableDebugLayer(); #endif NativeDevice = new Device(null, FeatureLevel); + WavefrontSize = NativeDevice.D3D12Options1.WaveLaneCountMin; NativeComputeCommandQueue = NativeDevice.CreateCommandQueue(new CommandQueueDescription(SharpDX.Direct3D12.CommandListType.Compute)); NativeCopyCommandQueue = NativeDevice.CreateCommandQueue(new CommandQueueDescription(SharpDX.Direct3D12.CommandListType.Copy)); @@ -40,6 +41,11 @@ namespace ComputeSharp.Graphics CopyCommandList.Close(); } + /// + /// Gets the number of lanes in a SIMD wave on the current device (also known as "wavefront size" or "warp width") + /// + public int WavefrontSize { get; } + public CommandList CommandList { get; } public CommandList CopyCommandList { get; } diff --git a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs index 2c72d15f..ff704cfa 100644 --- a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs +++ b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs @@ -9,23 +9,13 @@ namespace ComputeSharp /// public static class GraphicsDeviceExtensions { - /// - /// The default number of threads in a thead froup - /// - public const int DefaultThreadGroupCount = 64; - /// /// Compiles and runs the input shader on a target instance, with the specified parameters /// /// The to use to run the shader /// The number of threads to run on the X axis /// The input representing the compute shader to run - public static void For(this GraphicsDevice device, int x, Action action) - { - int groupsX = (x - 1) / DefaultThreadGroupCount + 1; - - ShaderRunner.Run(device, (DefaultThreadGroupCount, 1), (groupsX, 1), action); - } + public static void For(this GraphicsDevice device, int x, Action action) => device.For(x, 1, 1, action); /// /// Compiles and runs the input shader on a target instance, with the specified parameters @@ -34,14 +24,7 @@ namespace ComputeSharp /// The number of threads to run on the X axis /// The number of threads to run on the Y axis /// The input representing the compute shader to run - public static void For(this GraphicsDevice device, int x, int y, Action action) - { - int - groupsX = (x - 1) / DefaultThreadGroupCount + 1, - groupsY = (y - 1) / DefaultThreadGroupCount + 1; - - ShaderRunner.Run(device, (DefaultThreadGroupCount, DefaultThreadGroupCount), (groupsX, groupsY), action); - } + public static void For(this GraphicsDevice device, int x, int y, Action action) => device.For(x, y, 1, action); /// /// Compiles and runs the input shader on a target instance, with the specified parameters @@ -54,11 +37,14 @@ namespace ComputeSharp public static void For(this GraphicsDevice device, int x, int y, int z, Action action) { int - groupsX = (x - 1) / DefaultThreadGroupCount + 1, - groupsY = (y - 1) / DefaultThreadGroupCount + 1, - groupsZ = (z - 1) / DefaultThreadGroupCount + 1; + threadsX = device.WavefrontSize, + threadsY = y > 1 ? device.WavefrontSize : 1, + threadsZ = z > 1 ? device.WavefrontSize : 1, + groupsX = (x - 1) / device.WavefrontSize + 1, + groupsY = (y - 1) / device.WavefrontSize + 1, + groupsZ = (z - 1) / device.WavefrontSize + 1; - ShaderRunner.Run(device, (DefaultThreadGroupCount, DefaultThreadGroupCount, DefaultThreadGroupCount), (groupsX, groupsY, groupsZ), action); + ShaderRunner.Run(device, (threadsX, threadsY, threadsZ), (groupsX, groupsY, groupsZ), action); } /// From 6fc7cd4e9d9865bdcbd89d1422e4aa154c65c8df Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 16:42:18 +0200 Subject: [PATCH 36/42] Updated mustached template to support automatic bounds check --- src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs | 6 +++--- .../Renderer/Templates/ShaderTemplate.mustache | 7 ++++++- src/ComputeSharp.Shaders/ShaderRunner.cs | 6 +++--- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs index 598568ef..eb7bf1fe 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs @@ -18,17 +18,17 @@ namespace ComputeSharp.Shaders.Renderer.Models /// /// Gets or sets the number of threads in the X group /// - public int ThreadsX { get; set; } + public int NumThreadsX { get; set; } /// /// Gets or sets the number of threads in the Y group /// - public int ThreadsY { get; set; } + public int NumThreadsY { get; set; } /// /// Gets or sets the number of threads in the Z group /// - public int ThreadsZ { get; set; } + public int NumThreadsZ { get; set; } /// /// Gets or sets the name for the threads id variable used in the shader diff --git a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache index 3ab5de4a..eee11e3e 100644 --- a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache +++ b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache @@ -18,6 +18,11 @@ cbuffer {{FieldName}}Buffer : register(b{{ConstantBufferIndex}}) {{/FieldsList}} {{!Shader entry point}} [Shader("compute")] -[NumThreads({{ThreadsX}}, {{ThreadsY}}, {{ThreadsZ}})] +[NumThreads({{NumThreadsX}}, {{NumThreadsY}}, {{NumThreadsZ}})] void CSMain(uint3 {{ThreadsIdsVariableName}} : SV_DispatchThreadId) +{ +if ({{ThreadsIdsVariableName}}.x < {{ThreadsX}} && + {{ThreadsIdsVariableName}}.y < {{ThreadsY}} && + {{ThreadsIdsVariableName}}.z < {{ThreadsZ}}) {{&ShaderBody}} +} diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs index e16a67c7..7886a08a 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -31,9 +31,9 @@ namespace ComputeSharp.Shaders ShaderInfo shaderInfo = new ShaderInfo { FieldsList = shaderLoader.FieldsInfo, - ThreadsX = numThreads.X, - ThreadsY = numThreads.Y, - ThreadsZ = numThreads.Z, + NumThreadsX = numThreads.X, + NumThreadsY = numThreads.Y, + NumThreadsZ = numThreads.Z, ThreadsIdsVariableName = shaderLoader.ThreadsIdsVariableName, ShaderBody = shaderLoader.MethodBody }; From 09f5e8e6e5c3d85f93bae57aafc44226a21330bb Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 16:42:27 +0200 Subject: [PATCH 37/42] Minor shader source formatting tweak --- src/ComputeSharp.Shaders/Translation/ShaderLoader.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index d6fd5fde..acdeac27 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -161,6 +161,7 @@ namespace ComputeSharp.Shaders.Translation // Additional preprocessing MethodBody = Regex.Replace(MethodBody, @"\d+[fFdD]", m => m.Value.Replace("f", "")); + MethodBody = MethodBody.TrimEnd('\n', '\r', ' '); } } } From 13fc51df4a9ee6c2d18d1d7e1efdd18992c04a72 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 16:47:32 +0200 Subject: [PATCH 38/42] APIs refactored, implemented automatic bounds checking --- .../Renderer/Models/ShaderInfo.cs | 15 ++++++++++ src/ComputeSharp.Shaders/ShaderRunner.cs | 27 ++++++++++++----- .../Extensions/GraphicsDeviceExtensions.cs | 29 ++----------------- src/DirectX12ComputeShaderSample/Program.cs | 1 - 4 files changed, 38 insertions(+), 34 deletions(-) diff --git a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs index eb7bf1fe..2a2e09df 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs @@ -15,6 +15,21 @@ namespace ComputeSharp.Shaders.Renderer.Models /// public IReadOnlyList FieldsList { get; set; } + /// + /// Gets or sets the total number of threads to run on the X axis + /// + public int ThreadsX { get; set; } + + /// + /// Gets or sets the total number of threads to run on the Y axis + /// + public int ThreadsY { get; set; } + + /// + /// Gets or sets the total number of threads to run on the Z axis + /// + public int ThreadsZ { get; set; } + /// /// Gets or sets the number of threads in the X group /// diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs index 7886a08a..1b5cbe0e 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -19,11 +19,21 @@ namespace ComputeSharp.Shaders /// Compiles and runs the input shader on a target instance, with the specified parameters /// /// The to use to run the shader - /// The value that indicates the number of threads to run in each thread group - /// The value that indicates the number of thread groups to dispatch + /// The number of threads to run on the X axis + /// The number of threads to run on the Y axis + /// The number of threads to run on the Z axis /// The input representing the compute shader to run - public static void Run(GraphicsDevice device, NumThreads numThreads, NumThreads numGroups, Action action) + public static void Run(GraphicsDevice device, int x, int y, int z, Action action) { + // Calculate the optimized thread num and group values + int + threadsX = device.WavefrontSize, + threadsY = y > 1 ? device.WavefrontSize : 1, + threadsZ = z > 1 ? device.WavefrontSize : 1, + groupsX = (x - 1) / device.WavefrontSize + 1, + groupsY = (y - 1) / device.WavefrontSize + 1, + groupsZ = (z - 1) / device.WavefrontSize + 1; + // Load the input shader ShaderLoader shaderLoader = ShaderLoader.Load(action); @@ -31,9 +41,12 @@ namespace ComputeSharp.Shaders ShaderInfo shaderInfo = new ShaderInfo { FieldsList = shaderLoader.FieldsInfo, - NumThreadsX = numThreads.X, - NumThreadsY = numThreads.Y, - NumThreadsZ = numThreads.Z, + ThreadsX = x, + ThreadsY = y, + ThreadsZ = z, + NumThreadsX = threadsX, + NumThreadsY = threadsY, + NumThreadsZ = threadsZ, ThreadsIdsVariableName = shaderLoader.ThreadsIdsVariableName, ShaderBody = shaderLoader.MethodBody }; @@ -58,7 +71,7 @@ namespace ComputeSharp.Shaders } // Dispatch and wait for completion - commandList.Dispatch(numGroups.X, numGroups.Y, numGroups.Z); + commandList.Dispatch(groupsX, groupsY, groupsZ); commandList.Flush(true); } } diff --git a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs index ff704cfa..f9bdc634 100644 --- a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs +++ b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs @@ -15,7 +15,7 @@ namespace ComputeSharp /// The to use to run the shader /// The number of threads to run on the X axis /// The input representing the compute shader to run - public static void For(this GraphicsDevice device, int x, Action action) => device.For(x, 1, 1, action); + public static void For(this GraphicsDevice device, int x, Action action) => ShaderRunner.Run(device, x, 1, 1, action); /// /// Compiles and runs the input shader on a target instance, with the specified parameters @@ -24,7 +24,7 @@ namespace ComputeSharp /// The number of threads to run on the X axis /// The number of threads to run on the Y axis /// The input representing the compute shader to run - public static void For(this GraphicsDevice device, int x, int y, Action action) => device.For(x, y, 1, action); + public static void For(this GraphicsDevice device, int x, int y, Action action) => ShaderRunner.Run(device, x, y, 1, action); /// /// Compiles and runs the input shader on a target instance, with the specified parameters @@ -34,29 +34,6 @@ namespace ComputeSharp /// The number of threads to run on the Y axis /// The number of threads to run on the Z axis /// The input representing the compute shader to run - public static void For(this GraphicsDevice device, int x, int y, int z, Action action) - { - int - threadsX = device.WavefrontSize, - threadsY = y > 1 ? device.WavefrontSize : 1, - threadsZ = z > 1 ? device.WavefrontSize : 1, - groupsX = (x - 1) / device.WavefrontSize + 1, - groupsY = (y - 1) / device.WavefrontSize + 1, - groupsZ = (z - 1) / device.WavefrontSize + 1; - - ShaderRunner.Run(device, (threadsX, threadsY, threadsZ), (groupsX, groupsY, groupsZ), action); - } - - /// - /// Compiles and runs the input shader on a target instance, with the specified parameters - /// - /// The to use to run the shader - /// The value that indicates the number of threads to run in each thread group - /// The value that indicates the number of thread groups to dispatch - /// The input representing the compute shader to run - public static void For(this GraphicsDevice device, NumThreads numThreads, NumThreads numGroups, Action action) - { - ShaderRunner.Run(device, numThreads, numGroups, action); - } + public static void For(this GraphicsDevice device, int x, int y, int z, Action action) => ShaderRunner.Run(device, x, y, z, action); } } diff --git a/src/DirectX12ComputeShaderSample/Program.cs b/src/DirectX12ComputeShaderSample/Program.cs index 59e3cc8f..84cb626a 100644 --- a/src/DirectX12ComputeShaderSample/Program.cs +++ b/src/DirectX12ComputeShaderSample/Program.cs @@ -25,7 +25,6 @@ namespace DirectX12ComputeShaderSample Action action = id => { uint offset = id.X + id.Y * (uint)width; - if (offset > 99) return; gpuBuffer[offset] *= 2; }; From f249ab33051a8a4e907a69d812f102d39a0b52e6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 16:48:13 +0200 Subject: [PATCH 39/42] Removed unnecessary NumThreads struct --- .../Primitives/NumThreads.cs | 66 ------------------- 1 file changed, 66 deletions(-) delete mode 100644 src/ComputeSharp.Shaders/Primitives/NumThreads.cs diff --git a/src/ComputeSharp.Shaders/Primitives/NumThreads.cs b/src/ComputeSharp.Shaders/Primitives/NumThreads.cs deleted file mode 100644 index ffad8e7e..00000000 --- a/src/ComputeSharp.Shaders/Primitives/NumThreads.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System.Diagnostics; -using System.Runtime.CompilerServices; - -namespace ComputeSharp -{ - /// - /// A that indicates the number of threads to use to run a given compute shader - /// - [DebuggerDisplay("(X: {X}, Y: {Y}, Z: {Z})")] - public readonly struct NumThreads - { - /// - /// The X id of the current thread - /// - public readonly int X; - - /// - /// The Y id of the current thread - /// - public readonly int Y; - - /// - /// The Z id of the current thread - /// - public readonly int Z; - - /// - /// Creates a new instance with the specified parameters - /// - /// The number of threads in the X axis - /// The number of threads in the Y axis - public NumThreads(int x, int y) - { - X = x; - Y = y; - Z = 1; - } - - /// - /// Creates a new instance with the specified parameters - /// - /// The number of threads in the X axis - /// The number of threads in the Y axis - /// The number of threads in the Z axis - public NumThreads(int x, int y, int z) - { - X = x; - Y = y; - Z = z; - } - - /// - /// Creates a new instance from a value tuple - /// - /// The tuple with the thread X and Y counts - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator NumThreads((int X, int Y) xy) => new NumThreads(xy.X, xy.Y); - - /// - /// Creates a new instance from a value tuple - /// - /// The tuple with the thread X, Y and Z counts - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator NumThreads((int X, int Y, int Z) xyz) => new NumThreads(xyz.X, xyz.Y, xyz.Z); - } -} From 5809e582074852e57994dc35697af0a2c7292cbf Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 16:58:27 +0200 Subject: [PATCH 40/42] Added solution items folder --- src/ComputeSharp.sln | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ComputeSharp.sln b/src/ComputeSharp.sln index a8415a79..6d17bb29 100644 --- a/src/ComputeSharp.sln +++ b/src/ComputeSharp.sln @@ -11,6 +11,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp", "ComputeShar EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectX12ComputeShaderSample", "DirectX12ComputeShaderSample\DirectX12ComputeShaderSample.csproj", "{7A48A85D-8866-4DED-A404-653ED09BF396}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_SolutionItems", "_SolutionItems", "{1A9C4108-F61A-4ABE-B5CF-F524521EAE2D}" + ProjectSection(SolutionItems) = preProject + ..\.gitignore = ..\.gitignore + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU From 026beaad9108c14fe3f9a0cc818c05da685974b4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 17:04:36 +0200 Subject: [PATCH 41/42] Moved the sample project to separate subfolder --- .../NetCoreComputeShaderSample.csproj | 2 +- .../NetCoreComputeShaderSample}/Program.cs | 18 +++++++++--------- src/ComputeSharp.sln | 7 ++++++- 3 files changed, 16 insertions(+), 11 deletions(-) rename src/DirectX12ComputeShaderSample/DirectX12ComputeShaderSample.csproj => samples/NetCoreComputeShaderSample/NetCoreComputeShaderSample.csproj (72%) rename {src/DirectX12ComputeShaderSample => samples/NetCoreComputeShaderSample}/Program.cs (87%) diff --git a/src/DirectX12ComputeShaderSample/DirectX12ComputeShaderSample.csproj b/samples/NetCoreComputeShaderSample/NetCoreComputeShaderSample.csproj similarity index 72% rename from src/DirectX12ComputeShaderSample/DirectX12ComputeShaderSample.csproj rename to samples/NetCoreComputeShaderSample/NetCoreComputeShaderSample.csproj index 33fc9464..bfb20336 100644 --- a/src/DirectX12ComputeShaderSample/DirectX12ComputeShaderSample.csproj +++ b/samples/NetCoreComputeShaderSample/NetCoreComputeShaderSample.csproj @@ -6,7 +6,7 @@ - + diff --git a/src/DirectX12ComputeShaderSample/Program.cs b/samples/NetCoreComputeShaderSample/Program.cs similarity index 87% rename from src/DirectX12ComputeShaderSample/Program.cs rename to samples/NetCoreComputeShaderSample/Program.cs index 84cb626a..8a862cca 100644 --- a/src/DirectX12ComputeShaderSample/Program.cs +++ b/samples/NetCoreComputeShaderSample/Program.cs @@ -4,7 +4,7 @@ using ComputeSharp; using ComputeSharp.Graphics.Buffers; using ComputeSharp.Graphics.Buffers.Extensions; -namespace DirectX12ComputeShaderSample +namespace NetCoreComputeShaderSample { public class Program { @@ -19,7 +19,11 @@ namespace DirectX12ComputeShaderSample array[i] = i + 1; } - using ReadWriteBuffer gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array.AsSpan()); + // Print the initial matrix + Console.WriteLine("Before:"); + PrintMatrix(array, width, height); + + using ReadWriteBuffer gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array); // Shader body Action action = id => @@ -31,14 +35,10 @@ namespace DirectX12ComputeShaderSample // Run the shader Gpu.Default.For(100, action); - // Print matrix + // Get the data back + gpuBuffer.GetData(array); - Console.WriteLine("Before:"); - PrintMatrix(array, width, height); - - gpuBuffer.GetData(array.AsSpan()); - - Console.WriteLine(); + // Print the updated matrix Console.WriteLine("After:"); PrintMatrix(array, width, height); } diff --git a/src/ComputeSharp.sln b/src/ComputeSharp.sln index 6d17bb29..56d82d29 100644 --- a/src/ComputeSharp.sln +++ b/src/ComputeSharp.sln @@ -9,13 +9,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Shaders", "Com EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp", "ComputeSharp\ComputeSharp.csproj", "{314EB353-E7E3-4146-8111-685CC83EC1DE}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirectX12ComputeShaderSample", "DirectX12ComputeShaderSample\DirectX12ComputeShaderSample.csproj", "{7A48A85D-8866-4DED-A404-653ED09BF396}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreComputeShaderSample", "..\samples\NetCoreComputeShaderSample\NetCoreComputeShaderSample.csproj", "{7A48A85D-8866-4DED-A404-653ED09BF396}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_SolutionItems", "_SolutionItems", "{1A9C4108-F61A-4ABE-B5CF-F524521EAE2D}" ProjectSection(SolutionItems) = preProject ..\.gitignore = ..\.gitignore EndProjectSection EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -60,6 +62,9 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {7A48A85D-8866-4DED-A404-653ED09BF396} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {4664C5E3-0340-4E22-BCFD-98AAEDF5F2DC} EndGlobalSection From d12b687bebf8c8b66497f8590cbc2dd5808dc6a1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 17:05:58 +0200 Subject: [PATCH 42/42] Minor code refactoring --- samples/NetCoreComputeShaderSample/Program.cs | 1 - .../Buffers/Extensions/BufferExtensions.cs | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/NetCoreComputeShaderSample/Program.cs b/samples/NetCoreComputeShaderSample/Program.cs index 8a862cca..de9ba68f 100644 --- a/samples/NetCoreComputeShaderSample/Program.cs +++ b/samples/NetCoreComputeShaderSample/Program.cs @@ -2,7 +2,6 @@ using System.Linq; using ComputeSharp; using ComputeSharp.Graphics.Buffers; -using ComputeSharp.Graphics.Buffers.Extensions; namespace NetCoreComputeShaderSample { diff --git a/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs b/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs index 14767a10..28dc83ff 100644 --- a/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs +++ b/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs @@ -1,9 +1,10 @@ using System; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; +using ComputeSharp.Graphics; +using ComputeSharp.Graphics.Buffers; -namespace ComputeSharp.Graphics.Buffers.Extensions +namespace ComputeSharp { /// /// A with extension methods for the type to allocate buffers