Recursive AI

Compile HLSL InterlockedAdd64 to SPIR-V

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

Use InterlockedAdd64 when several shader invocations update the same 64-bit counter. This example targets Vulkan.

Terminal
dotnet add package RecursiveAI.OneShader

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

HLSL
// Counter: RWByteAddressBuffer at u0
uint64_t previous;
Counter.InterlockedAdd64(0, 1ull, previous);

Put these lines in the compute shader below. The C# example compiles Counter.hlsl to counter.spv.

The example is the full program below: unzip it and dotnet run.

Full program

Save this input as Counter.hlsl beside Program.cs, or use the copy in the zip.

HLSL
RWByteAddressBuffer Counter : register(u0);
 
[shader("compute")]
[numthreads(1, 1, 1)]
void CSMain()
{
    uint64_t previous;
    Counter.InterlockedAdd64(0, 1ull, previous);
}
C#
using OneShader;
 
var result = ShaderCompiler.CompileFile("Counter.hlsl",
    new CompileOptions { Targets = [CompileTarget.SpirV] });
if (!result.Success)
    throw new InvalidOperationException(
        string.Join(Environment.NewLine, result.Diagnostics));
 
var shader = result.Find("CSMain", CompileTarget.SpirV)!;
File.WriteAllBytes("counter.spv", shader.ToArray());
Console.WriteLine("Wrote counter.spv");
var entry = result.Reflection!.EntryPoints.Single();
Console.WriteLine($"Required: {entry.RequiredFeatures}");
Text
Wrote counter.spv
Required: Int64, Int64Atomics

If compilation fails, the program prints the compiler diagnostics in the exception instead of writing a new SPIR-V file.

Variations

Add to the next counter. If the buffer contains two 64-bit counters, change the shader body to this to increment the second. The argument is a byte offset, not a counter index.

HLSL
uint64_t previous;
Counter.InterlockedAdd64(8, 1ull, previous);

The short answer

Declare RWByteAddressBuffer, call InterlockedAdd64 with an 8-byte-aligned offset and a 64-bit addend, then compile with OneShader 1.7.3 or later for SPIR-V. The shader adds directly to the buffer. The optional output argument receives the old value, not the sum.

How does InterlockedAdd64 address an RWByteAddressBuffer?

A raw buffer has no element type in its HLSL declaration. The 64 in the method name specifies the width of the operation; 1ull is an unsigned 64-bit addend. In the Shader Model 6.6 signature, the first parameter is a byte offset, and signed and unsigned overloads use int64_t and uint64_t respectively. Use offsets 0, 8, 16, and so on for a packed table of counters. Offset 4 is not aligned for this operation.

In Vulkan, bind a storage buffer at set 0, binding 0. The descriptor range must cover the counter and the application must initialize it before dispatch. OneShader emits a 64-bit view that aliases the raw buffer at the same binding; you don’t bind a second descriptor for the same counter. If you read back the counter on the host, synchronize the dispatch’s write with that read. The atomic add protects the update itself; it doesn’t make the whole dispatch or other buffer writes visible to your application.

What Vulkan features do 64-bit buffer atomics need?

This shader’s SPIR-V declares Int64 and Int64Atomics. The example prints those same names from the entry point’s reflection. Query and enable shaderInt64 and shaderBufferInt64Atomics when creating the logical device. shaderSharedInt64Atomics covers workgroup shared memory, not this storage buffer. The compiler can produce the binary on a machine without either feature; success at compile time is not a device check.

If a device supports only 32-bit atomics, use a separate shader and a counter sized for that range. Two independent 32-bit adds don’t implement one atomic 64-bit update. This InterlockedAdd64 example isn’t a fallback for Metal or WebGPU: OneShader rejects this method on those targets.

All oneshader recipes