---
title: "Export HLSL reflection as JSON"
url: https://recursiveai.org/oneshader/cookbook/export-hlsl-reflection-json/
description: "Export HLSL shader reflection as JSON with the OneShader command line. Get resource bindings and compute workgroup sizes alongside the compiled shader."
version: "1.7.1"
---

# Export HLSL reflection as JSON

**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.Tool) · [GitHub](https://github.com/recursive-ai-oss/oneshader-csharp) · [Docs](https://recursiveai.org/oneshader/docs/) · [Download example (.zip)](https://recursiveai.org/oneshader/cookbook/export-hlsl-reflection-json/export-hlsl-reflection-json.zip)

Write shader bindings and entry points to JSON without a GPU.

```shell
dotnet tool install -g RecursiveAI.OneShader.Tool
```

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

Use this `fill.hlsl` from the zip:

```hlsl
RWStructuredBuffer<uint> Output : register(u0);

[numthreads(8, 1, 1)]
void main(uint3 id : SV_DispatchThreadID)
{
    Output[id.x] = id.x;
}
```

SPIR-V is the default target, so `-Fo fill.spv` names the compiled output.

```shell
oneshader -T cs_6_0 fill.hlsl \
  -Fo fill.spv --reflect fill.json
```

Writes `fill.spv` and `fill.json` without printing anything on success. Here’s `fill.json`:

```json
{
  "schemaVersion": 1,
  "source": "fill.hlsl",
  "targets": [
    "SpirV"
  ],
  "resources": [
    {
      "name": "Output",
      "kind": "StorageBuffer",
      "hlslType": "RWStructuredBuffer\u003Cuint\u003E",
      "resourceType": "RWStructuredBuffer",
      "count": 1,
      "stages": [
        "Compute"
      ],
      "slots": [
        {
          "target": "SpirV",
          "set": 0,
          "binding": 0
        }
      ],
      "counterSlots": [],
      "layout": {
        "kind": "Scalar",
        "size": 4,
        "alignment": 4,
        "scalar": "UInt"
      }
    }
  ],
  "pushConstants": [],
  "entryPoints": [
    {
      "name": "main",
      "stage": "Compute",
      "workgroupSize": [
        8,
        1,
        1
      ],
      "inputs": [
        {
          "name": "id",
          "semantic": "SV_DispatchThreadID",
          "semanticIndex": 0,
          "builtIn": "GlobalInvocationId",
          "scalar": "UInt",
          "components": 3,
          "offset": 0,
          "interpolation": "Default"
        }
      ],
      "outputs": [],
      "usedResources": [
        "Output"
      ]
    }
  ]
}
```

## The full program

Unzip [the example](https://recursiveai.org/oneshader/cookbook/export-hlsl-reflection-json/export-hlsl-reflection-json.zip), then `dotnet run`.

The zip restores the command-line tool from its pinned manifest, calls it from C#, and reads the exported JSON. You don’t need the global install for the zip.

```csharp
using System.Diagnostics;
using System.Text.Json;

RunDotnet("tool", "restore");
RunDotnet("oneshader", "-T", "cs_6_0", "fill.hlsl",
    "-Fo", "fill.spv", "--reflect", "fill.json");

using var reflection = JsonDocument.Parse(File.ReadAllText("fill.json"));
var root = reflection.RootElement;
if (root.GetProperty("schemaVersion").GetInt32() != 1)
    throw new NotSupportedException("Expected reflection schema version 1.");

var resource = root.GetProperty("resources")[0];
var slot = resource.GetProperty("slots")[0];
Console.WriteLine($"{resource.GetProperty("name")}: set {slot.GetProperty("set")}, " +
    $"binding {slot.GetProperty("binding")}");

var entry = root.GetProperty("entryPoints")[0];
var size = entry.GetProperty("workgroupSize").EnumerateArray();
Console.WriteLine($"{entry.GetProperty("name")}: {string.Join(" x ", size)}");

static void RunDotnet(params string[] args)
{
    var command = new ProcessStartInfo("dotnet");
    foreach (string arg in args) command.ArgumentList.Add(arg);
    using var process = Process.Start(command)!;
    process.WaitForExit();
    if (process.ExitCode != 0)
        throw new InvalidOperationException($"dotnet {args[0]} exited with {process.ExitCode}.");
}
```

After the tool restore messages, it prints:

```text
Output: set 0, binding 0
main: 8 x 1 x 1
```

If compilation fails or the CLI can’t write an output, it reports the error on standard error and exits with code `1`.

## Variations

**Export reflection for WebGPU.** Choose WGSL and give it its own output files. The resulting JSON has `"targets": ["Wgsl"]`.

```shell
oneshader -T cs_6_0 fill.hlsl --target wgsl \
  -Fo fill.wgsl --reflect fill-wgsl.json
```

## Related recipes

- [Compile HLSL to SPIR-V](https://recursiveai.org/oneshader/cookbook/hlsl-to-spirv/)
- [Read SPIR-V reflection in C#](https://recursiveai.org/oneshader/cookbook/spirv-reflection-csharp/)

---

## The short answer

Pass `--reflect fill.json` to the OneShader CLI when you compile HLSL. Use `-Fo` to put the compiled shader in its own file, leaving the JSON as a separate file your build scripts can read. No Vulkan device is needed for this export.

## What does HLSL shader reflection JSON contain?

Reflection describes the boundary between a shader and the code that runs it. The [CLI’s JSON schema](https://recursiveai.org/oneshader/docs/command-line/#reflection-json-schema-version-1) lists the shader’s declared resources, their types and counts, and their target-specific slots. Each entry point also lists the resources it uses. In `fill.json`, `Output` is a storage buffer with a SPIR-V slot at set `0`, binding `0`. The stage is `Compute`, and `workgroupSize` reports the `[numthreads]` declaration, not the number of groups the host will dispatch.

For a buffer with structured data, look at its `layout`, which describes the byte sizes, offsets, and strides. Those fields tell you how the shader expects the data to be laid out; the resource name alone doesn’t tell you how to pack it. If you’ve compiled more than one target, look for the slot with the target you’re binding rather than using the first slot in the list.

### Comparing reflection between builds

With the same compile, the CLI writes the same bytes: it uses a fixed order and LF line endings, and does not include machine paths. That makes the file useful when you want to see whether changing HLSL moves a binding or changes a buffer layout. Keep the shader and its matching JSON together so a host does not use yesterday’s layout with today’s shader.

A reader can check `schemaVersion` before taking fields from the file. The CLI documentation says changes that break readers raise that number. Check it separately from the NuGet package version; the package version identifies the executable, while the schema version identifies the JSON contract.
