---
title: "Compile HLSL packed 8-bit dot products to SPIR-V"
url: https://recursiveai.org/oneshader/cookbook/compile-hlsl-packed-dot-products/
description: "Compile dot4add_u8packed and dot4add_i8packed from HLSL to SPIR-V in C#. Write a shader file and check the Vulkan integer dot product feature."
version: "1.7.1"
---

# Compile HLSL packed 8-bit dot products to SPIR-V

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

`dot4add_u8packed` multiplies four pairs of packed bytes, sums them and adds an accumulator. Running it requires Vulkan integer dot product support.

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

Save the `dot.hlsl` file shown below beside `Program.cs` first. It’s also in the zip.

```csharp
using OneShader;

var result = ShaderCompiler.CompileFile(
    "dot.hlsl"); // SPIR-V by default
if (!result.Success)
    throw new InvalidOperationException(
        string.Join("\n", result.Diagnostics));
File.WriteAllBytes("dot.spv",
    result.Blobs[0].ToArray());
Console.WriteLine("Wrote dot.spv");
Console.WriteLine(result.Reflection!
    .EntryPoints[0].RequiredFeatures);
```

`dotnet run` writes the SPIR-V binary and prints the required Vulkan feature:

```text
Wrote dot.spv
IntegerDotProduct
```

If the shader doesn’t compile, the program reports the compiler diagnostics and doesn’t write a new binary.

Paste the code above into Program.cs of a new `dotnet new console` project and `dotnet run`, or unzip [the example](https://recursiveai.org/oneshader/cookbook/compile-hlsl-packed-dot-products/compile-hlsl-packed-dot-products.zip) and `dotnet run`.

`dot.hlsl`:

```hlsl
RWStructuredBuffer<uint> Scores;
[shader("compute")]
[numthreads(1, 1, 1)]
void Main(uint i : SV_DispatchThreadID)
{
    Scores[i] = dot4add_u8packed(
        Scores[i], 0x01020304u, 7u);
}
```

## Variations

**Use signed bytes.** Replace `dot.hlsl` with the following to use signed lanes and a signed accumulator. The same C# code writes `dot.spv` and prints `IntegerDotProduct`.

```hlsl
RWStructuredBuffer<int> Scores;
[shader("compute")]
[numthreads(1, 1, 1)]
void Main(uint i : SV_DispatchThreadID)
{
    Scores[i] = dot4add_i8packed(
        asuint(Scores[i]), 0x01020304u, 7);
}
```

## Related recipes

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

## The short answer

Put `dot4add_u8packed` or `dot4add_i8packed` in the HLSL and call `ShaderCompiler.CompileFile` from C#. OneShader produces SPIR-V by default. Save the compiled entry point’s bytes, then check its reflected `RequiredFeatures` before using the shader. The example has one entry point and prints `IntegerDotProduct`, which is `shaderIntegerDotProduct` on the Vulkan device.

## What is the difference between dot4add_u8packed and dot4add_i8packed?

Both take two packed 32-bit words. Each word supplies four byte-sized lanes, so the intrinsic multiplies corresponding lanes, sums those four products and adds the third argument. The unsigned form reads bytes as 0 through 255; the signed form reads the same bits as signed bytes, from −128 through 127. For example, `0xFF` in a lane means 255 in the unsigned form and −1 in the signed form. The signed variant uses `asuint` to pass the input’s bits, not to convert each lane into an unsigned magnitude.

The literal `0x01020304u` in the example supplies weights 4, 3, 2 and 1 from least to most significant byte. Arrange the values and weights in corresponding lanes, and choose signedness to match how the input bytes were encoded. The accumulator does not saturate: addition wraps at 32 bits. OneShader emits `OpUDot` or `OpSDot` with `PackedVectorFormat4x8Bit`, then `OpIAdd`, rather than an accumulating saturating instruction.

## What Vulkan feature does a packed integer dot product need?

The SPIR-V declares `DotProduct` and `DotProductInput4x8BitPacked` and names the `SPV_KHR_integer_dot_product` extension. In Vulkan, query and enable [`shaderIntegerDotProduct`](https://docs.vulkan.org/refpages/latest/refpages/source/VkPhysicalDeviceShaderIntegerDotProductFeatures.html) before using this shader. This is a device feature, not something that the .NET compiler enables for you. It is also not a promise that the operation is accelerated: Vulkan reports separate [properties for signed and unsigned packed acceleration](https://docs.vulkan.org/refpages/latest/refpages/source/VkPhysicalDeviceShaderIntegerDotProductProperties.html).

The byte lanes here are packed in `uint` words, not stored as four standalone shader 8-bit scalars. You don’t need to enable 16-bit types or change how you upload those 32-bit words. If a target device lacks integer dot products, use a different shader that unpacks and multiplies the lanes explicitly instead of loading this binary.
