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);
- }
-}
diff --git a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs
index 598568ef..2a2e09df 100644
--- a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs
+++ b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs
@@ -16,20 +16,35 @@ namespace ComputeSharp.Shaders.Renderer.Models
public IReadOnlyList FieldsList { get; set; }
///
- /// Gets or sets the number of threads in the X group
+ /// Gets or sets the total number of threads to run on the X axis
///
public int ThreadsX { get; set; }
///
- /// Gets or sets the number of threads in the Y group
+ /// Gets or sets the total number of threads to run on the Y axis
///
public int ThreadsY { get; set; }
///
- /// Gets or sets the number of threads in the Z group
+ /// 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
+ ///
+ public int NumThreadsX { get; set; }
+
+ ///
+ /// Gets or sets the number of threads in the Y group
+ ///
+ public int NumThreadsY { get; set; }
+
+ ///
+ /// Gets or sets the number of threads in the Z group
+ ///
+ 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..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,
- ThreadsX = numThreads.X,
- ThreadsY = numThreads.Y,
- ThreadsZ = 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.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', ' ');
}
}
}
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;
};