From 6fc7cd4e9d9865bdcbd89d1422e4aa154c65c8df Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 16:42:18 +0200 Subject: [PATCH 1/4] 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 2/4] 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 3/4] 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 4/4] 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); - } -}