---
title: "Read HLSL workgroup size in C#"
url: https://recursiveai.org/oneshader/cookbook/read-generated-compute-workgroup-size/
description: "Read HLSL compute workgroup dimensions from generated C# bindings, round up the dispatch counts, and run a Vulkan compute shader with OneShader."
version: "1.7.1"
---

# Read HLSL workgroup size in C#

**OneShader** is a free, open-source HLSL compiler library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneShader.Build) · [GitHub](https://github.com/recursive-ai-oss/oneshader-csharp) · [Docs](https://recursiveai.org/oneshader/docs/) · [Download example (.zip)](https://recursiveai.org/oneshader/cookbook/read-generated-compute-workgroup-size/read-generated-compute-workgroup-size.zip)

Read the shader’s `[numthreads]` values from generated C# constants.

```shell
dotnet add package RecursiveAI.OneShader.Build
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using static Example.Shaders.Fill.CSMain;
Console.WriteLine($"{ThreadGroupSizeX} x " +
  $"{ThreadGroupSizeY} x {ThreadGroupSizeZ}");
```

Unzip [the example](https://recursiveai.org/oneshader/cookbook/read-generated-compute-workgroup-size/read-generated-compute-workgroup-size.zip), 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:

```xml
<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.

```hlsl
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:

```shell
dotnet add package RecursiveAI.OneGpu.Vulkan
```

Divide 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.

```csharp
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);
```

```text
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#:

```hlsl
[numthreads(4, 2, 1)]
```

```text
4 x 2 x 1
Groups: 5 x 5 x 1
Last cell: 190
```

## Related recipes

- [Generate C# bindings from HLSL](https://recursiveai.org/oneshader/cookbook/generate-csharp-shader-bindings/)
- [Set an HLSL preprocessor macro in a C# project](https://recursiveai.org/oneshader/cookbook/define-hlsl-build-macro-in-csproj/)

---

## 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](https://recursiveai.org/oneshader/docs/generated-bindings/#what-is-generated) lists the members available on each entry point.

## What does `numthreads` mean in an HLSL compute shader?

[`numthreads(X, Y, Z)`](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-attributes-numthreads) 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`](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/sv-dispatchthreadid), the thread’s global coordinate across the dispatch, for both the bounds check and the buffer index.
