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; };