Compile the matrix multiply-add in Tile.hlsl from the zip for Vulkan. No GPU is needed to compile it.
dotnet add package RecursiveAI.OneShaderRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneShader; var result = ShaderCompiler.CompileFile( "Tile.hlsl", new() { Enable16BitTypes = true }); if (!result.Success) return 1; File.WriteAllBytes("Tile.spv", result.Blobs.Single().ToArray()); var entries = result.Reflection!.EntryPoints; var entry = entries.Single(); Console.WriteLine(entry.RequiredFeatures); return 0;
The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.
SPIR-V is the default target. The snippet writes Tile.spv and prints:
Float16, StorageBuffer16, CooperativeMatrix, VulkanMemoryModel, FullSubgroupsHere is Tile.hlsl. The two inputs hold row-major 16 × 16 tiles of float16_t; the output holds a tile of 32-bit floats. MulAdd adds the product to a zero accumulator.
StructuredBuffer<float16_t> A : register(t0); StructuredBuffer<float16_t> B : register(t1); RWStructuredBuffer<float> Output : register(u2); typedef CooperativeMatrix<float16_t, 16, 16, MatrixUse::A> TileA; typedef CooperativeMatrix<float16_t, 16, 16, MatrixUse::B> TileB; typedef CooperativeMatrix<float, 16, 16, MatrixUse::Accumulator> TileC; [shader("compute")] [numthreads(32, 1, 1)] void Main() { TileA left; TileB right; left.Load(A, 0, 16, MatrixLayout::RowMajor); right.Load(B, 0, 16, MatrixLayout::RowMajor); TileC product = TileC::MulAdd(left, right, TileC::Zero()); product.Store(Output, 0, 16, MatrixLayout::RowMajor); }
The full program
using OneShader; var result = ShaderCompiler.CompileFile( "Tile.hlsl", new CompileOptions { Targets = [CompileTarget.SpirV], Enable16BitTypes = true }); foreach (var diagnostic in result.Diagnostics) Console.Error.WriteLine(diagnostic); if (!result.Success) return 1; var spirv = result.Blobs.Single(); File.WriteAllBytes("Tile.spv", spirv.ToArray()); var entry = result.Reflection!.EntryPoints.Single(); Console.WriteLine($"Tile.spv: {spirv.Data.Length} bytes"); Console.WriteLine($"{spirv.EntryPoint}: {entry.RequiredFeatures}"); return 0;
Tile.spv: 1440 bytes Main: Float16, StorageBuffer16, CooperativeMatrix, VulkanMemoryModel, FullSubgroups
If compilation fails, the program prints diagnostics and writes nothing. Successful compilation doesn’t tell you which cooperative matrix shapes a particular Vulkan device supports.
Variations
Add the product to an existing tile. In Tile.hlsl, replace the last two statements with the following to load Output as the initial accumulator instead of starting from zero. A host dispatching this version must initialize that buffer first.
TileC current; current.Load(Output, 0, 16, MatrixLayout::RowMajor); TileC sum = TileC::MulAdd(left, right, current); sum.Store(Output, 0, 16, MatrixLayout::RowMajor);
Related recipes
The short answer
Declare the operand and accumulator tiles with CooperativeMatrix and call TileC::MulAdd(a, b, c) to compute a × b + c. Compile for SPIR-V in OneShader and read Reflection.EntryPoints[i].RequiredFeatures before planning Vulkan device and pipeline creation. The compiler writes the module without requiring a Vulkan device.
How does a Vulkan cooperative matrix multiply-add work?
Each subgroup shares the storage and work for the tile: a single invocation doesn’t hold a full copy. MatrixUse::A is the left operand (M × K), MatrixUse::B is the right operand (K × N), and MatrixUse::Accumulator is the tile you add to and return (M × N). This shader uses 16 for M, N and K. Its loads and store are row-major with a stride of 16 elements, not 16 bytes.
Every invocation in the subgroup must reach the cooperative operations together. Don’t put the loads, multiply-add or store behind a branch that only some lanes take. The compiler emits OpCooperativeMatrixMulAddKHR and changes the module to use the Vulkan memory model. The SPIR-V extension defines the instruction and how it uses the subgroup.
What Vulkan features does cooperative matrix SPIR-V need?
For this example, the compiler reports these requirements for Main:
| Reflection flag | What the host needs |
|---|---|
CooperativeMatrix | VK_KHR_cooperative_matrix and the cooperativeMatrix device feature. |
VulkanMemoryModel | The vulkanMemoryModel device feature. |
Float16 | The shaderFloat16 device feature. |
StorageBuffer16 | The storageBuffer16BitAccess device feature for the half-precision input buffers. |
FullSubgroups | A pipeline stage created with VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT. This is a pipeline flag, not a device feature. |
The OneShader SPIR-V backend guide explains why that stage flag matters: the generated module uses SPIR-V 1.5, and the local workgroup size must be a multiple of the selected subgroup size. This shader has 32 invocations and one output tile, so use one 32-lane subgroup per workgroup and dispatch only one workgroup as written. Otherwise more than one subgroup or workgroup could write the same tile.
Extension support isn’t enough to pick a tile. Query vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR for a subgroup-scope 16 × 16 × 16 combination with float16 A and B and float32 accumulator and result. The compiler doesn’t check those properties against the device. If the combination is unavailable, pick another supported tile and element combination or use a different shader.