Read the shader’s [numthreads] values from generated C# constants.
dotnet add package RecursiveAI.OneShader.BuildRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using static Example.Shaders.Fill.CSMain; Console.WriteLine($"{ThreadGroupSizeX} x " + $"{ThreadGroupSizeY} x {ThreadGroupSizeZ}");
Unzip the example, then dotnet run.
Prints 8 x 4 x 1 for Fill.hlsl, shown below and included in the zip. ThreadGroupSizeX, ThreadGroupSizeY, and ThreadGroupSizeZ are uint constants on the generated compute entry-point class, Fill.CSMain.
OneShader.Build compiles .hlsl files in the project by default. To generate Example.Shaders.Fill.CSMain, put Fill.hlsl beside your .csproj and add these lines inside its <PropertyGroup> and <ItemGroup>, respectively:
<RootNamespace>Example</RootNamespace> <PackageReference Include="RecursiveAI.OneShader.Build" Version="1.7.1" />
Input: Fill.hlsl. This compute shader fills a 19 × 10 grid. Threads beyond its edges skip the write.
RWStructuredBuffer<uint> Output : register(u0); [shader("compute")] [numthreads(8, 4, 1)] void CSMain(uint3 id : SV_DispatchThreadID) { if (id.x >= 19 || id.y >= 10) return; uint index = id.y * 19 + id.x; Output[index] = index + 1; }
The full program
The dispatch needs a Vulkan 1.3 loader and driver. OneGpu, Recursive AI’s GPU library, runs the shader; you don’t need it to read the sizes. To run the shader in your own project, add its Vulkan package:
dotnet add package RecursiveAI.OneGpu.VulkanDivide each grid dimension by the corresponding generated size and round up. The program passes the three group counts to Dispatch, then reads the last grid cell from the GPU.
using Example.Shaders; using RecursiveAI.OneGpu; using RecursiveAI.OneGpu.Vulkan; uint x = Fill.CSMain.ThreadGroupSizeX; uint y = Fill.CSMain.ThreadGroupSizeY; uint z = Fill.CSMain.ThreadGroupSizeZ; Console.WriteLine($"{x} x {y} x {z}"); const int width = 19, height = 10, depth = 1; uint groupsX = RoundUp(width, x); uint groupsY = RoundUp(height, y); uint groupsZ = RoundUp(depth, z); Console.WriteLine($"Groups: {groupsX} x {groupsY} x {groupsZ}"); using var device = VulkanGpu.CreateDevice(); using var output = device.CreateBuffer<uint>(width * height); using var kernel = device.CreateComputeKernel( new ShaderSource(ShaderStage.Compute, Fill.CSMain.EntryPoint) { SpirV = Fill.CSMain.SpirV, Layout = Fill.CSMain.Layout }); using var group = kernel.CreateBindGroup(device, 0, [BindGroupEntry.ForBuffer(0, output.Buffer)]); var commands = device.BeginCommands(); var pass = commands.BeginComputePass(); pass.SetPipeline(kernel.Pipeline); pass.SetBindGroup(0, group); pass.Dispatch(groupsX, groupsY, groupsZ); pass.EndPass(); device.Submit(commands); var values = new uint[width * height]; await output.ReadAsync(values); Console.WriteLine($"Last cell: {values[^1]}"); static uint RoundUp(uint count, uint size) => count / size + (count % size == 0 ? 0u : 1u);
8 x 4 x 1 Groups: 3 x 3 x 1 Last cell: 190
If Fill isn’t found by the C# compiler, check that the project references the Build package, the file is included, and the namespace matches the project’s RootNamespace.
Variations
Change the workgroup size in HLSL. Replace the attribute in Fill.hlsl and rerun without editing C#:
[numthreads(4, 2, 1)]4 x 2 x 1 Groups: 5 x 5 x 1 Last cell: 190
Related recipes
The short answer
Read the workgroup dimensions from the generated C# bindings for the same entry point whose SPIR-V you pass to the compute pipeline. The generated bindings guide lists the members available on each entry point.
What does numthreads mean in an HLSL compute shader?
numthreads(X, Y, Z) describes the shape of one group. An 8 × 4 × 1 group has 32 threads. It says nothing about how many groups the CPU will launch. For a flat array, you can use a group that only extends on X and launch one group on Y and Z. For a grid, use separate X and Y sizes so each thread can use its X and Y coordinates directly.
How do you calculate the dispatch group count from numthreads?
Round up the grid size divided by the threads per group independently on each axis. In the example, 19 columns need three groups of eight along X, and ten rows need three groups of four along Y. That launches a 24 × 12 grid for a 19 × 10 data set. Truncating would miss the last three columns and last two rows; rounding up without the bounds check would let extra threads write outside the intended grid. The shader uses SV_DispatchThreadID, the thread’s global coordinate across the dispatch, for both the bounds check and the buffer index.