---
title: "Compile HLSL InterlockedAdd64 to SPIR-V"
url: https://recursiveai.org/oneshader/cookbook/hlsl-64-bit-byte-address-buffer-atomics-spirv/
description: "Add to a 64-bit RWByteAddressBuffer counter with HLSL InterlockedAdd64. Compile to SPIR-V and check the two Vulkan device features the shader needs."
version: "1.7.3"
---

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

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneShader) · [GitHub](https://github.com/recursive-ai-oss/oneshader-csharp) · [Docs](https://recursiveai.org/oneshader/docs/) · [Download example (.zip)](https://recursiveai.org/oneshader/cookbook/hlsl-64-bit-byte-address-buffer-atomics-spirv/hlsl-64-bit-byte-address-buffer-atomics-spirv.zip)

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

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

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (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](https://recursiveai.org/oneshader/cookbook/hlsl-64-bit-byte-address-buffer-atomics-spirv/hlsl-64-bit-byte-address-buffer-atomics-spirv.zip) 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);
}
```

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

## Related recipes

- [Compile a 32-bit HLSL atomic add](https://recursiveai.org/oneshader/cookbook/hlsl-interlockedadd/)
- [Read SPIR-V reflection in C#](https://recursiveai.org/oneshader/cookbook/spirv-reflection-csharp/)

## 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](https://microsoft.github.io/DirectX-Specs/d3d/HLSL_SM_6_6_Int64_and_Float_Atomics.html#interlockedadd), 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`](https://docs.vulkan.org/refpages/latest/refpages/source/VkPhysicalDeviceFeatures.html) and [`shaderBufferInt64Atomics`](https://docs.vulkan.org/refpages/latest/refpages/source/VkPhysicalDeviceShaderAtomicInt64Features.html) 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.
