Recursive AI

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.

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

Terminal
dotnet tool install -g RecursiveAI.OneShader.Tool

Requires .NET 10 (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.

Terminal
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, 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.

C#
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"].

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

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

All oneshader recipes