---
title: "Make HLSL default to row-major matrices in C#"
url: https://recursiveai.org/oneshader/cookbook/set-default-hlsl-matrix-packing/
description: "Set HLSL’s row-major default in C#. Set DefaultMatrixPacking without editing shader declarations and check buffer offsets in reflection."
version: "1.7.1"
---

# Make HLSL default to row-major matrices in C#

**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/set-default-hlsl-matrix-packing/set-default-hlsl-matrix-packing.zip)

HLSL packs matrices column-major by default. Set the default to row-major for the whole SPIR-V compile without editing each declaration.

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

```csharp
using OneShader;
using OneShader.Types;
var options = new CompileOptions {
    DefaultMatrixPacking =
        MatrixPacking.RowMajor
};
var result = ShaderCompiler.CompileFile(
    "Transform.hlsl", options);
if (!result.Success)
    throw new Exception(string.Join(
        '\n', result.Diagnostics));
var layout = result.Reflection!
    .Resources.Single().Layout!;
var matrix = layout.Members[0].Type;
Console.WriteLine(matrix.Packing);
```

Unzip [the example](https://recursiveai.org/oneshader/cookbook/set-default-hlsl-matrix-packing/set-default-hlsl-matrix-packing.zip), then `dotnet run`.

The first matrix in the buffer, `Transform`, now packs by row:

```text
RowMajor
```

The zip includes `Transform.hlsl`. If you’re pasting the snippet, save this input beside `Program.cs`. `Transform` has no qualifier; `Fixed` explicitly stays column-major:

```hlsl
cbuffer Params : register(b0) {
    float2x3 Transform;
    column_major float2x3 Fixed;
    float Bias;
};
[shader("pixel")]
float4 PSMain() : SV_Target {
    return float4(Transform[0][1],
                  Fixed[0][1], Bias, 1);
}
```

## The full program

Compile the same source twice and read the constant-buffer layout from the compiler’s reflection metadata. The two layouts show why the CPU code that fills `Params` needs to agree with the compile option.

```csharp
using OneShader;
using OneShader.Types;

Inspect(MatrixPacking.RowMajor);
Inspect(MatrixPacking.ColumnMajor);

static void Inspect(MatrixPacking packing)
{
    var result = ShaderCompiler.CompileFile("Transform.hlsl", new CompileOptions
    {
        DefaultMatrixPacking = packing,
    });
    if (!result.Success)
        throw new Exception(string.Join('\n', result.Diagnostics));

    var layout = result.Reflection!.Resources.Single(r => r.Name == "Params").Layout!;
    var transform = layout.Members.Single(m => m.Name == "Transform");
    var fixedMatrix = layout.Members.Single(m => m.Name == "Fixed");
    var bias = layout.Members.Single(m => m.Name == "Bias");
    Console.WriteLine($"Default: {packing}");
    Console.WriteLine($"Transform: {transform.Type.Packing}, {transform.Type.Size} bytes");
    Console.WriteLine($"Fixed: {fixedMatrix.Type.Packing}, {fixedMatrix.Type.Size} bytes");
    Console.WriteLine($"Bias: byte offset {bias.Offset}");
}
```

```text
Default: RowMajor
Transform: RowMajor, 32 bytes
Fixed: ColumnMajor, 48 bytes
Bias: byte offset 80
Default: ColumnMajor
Transform: ColumnMajor, 48 bytes
Fixed: ColumnMajor, 48 bytes
Bias: byte offset 96
```

If the compiler reports an error, the program throws with the diagnostics instead of reading a layout.

## Variations

**Set the default in the shader source instead.** If the setting belongs with the HLSL rather than the C# call, add this line before `cbuffer Params` in `Transform.hlsl`:

```hlsl
#pragma pack_matrix(row_major)
```

Run the full program again. Both compile options now report `Transform: RowMajor, 32 bytes` and `Bias: byte offset 80`; the explicit `column_major` on `Fixed` still wins.

## Related recipes

- [Match HLSL constant buffers with C# structs](https://recursiveai.org/oneshader/cookbook/cbuffer-packing-csharp-structs/)
- [Read shader reflection from C#](https://recursiveai.org/oneshader/cookbook/spirv-reflection-csharp/)

## The short answer

Pass `new CompileOptions { DefaultMatrixPacking = MatrixPacking.RowMajor }` to `ShaderCompiler.CompileFile` or `ShaderCompiler.Compile`. The option changes the starting default for unqualified declarations; it does not rewrite `Transform.hlsl` or override a matrix declared `column_major`. Check the reflected layout before copying CPU data into a shader buffer.

## Is HLSL row- or column-major by default?

HLSL’s default matrix packing is [column-major](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-appendix-pre-pragma-pack-matrix), and OneShader’s `DefaultMatrixPacking` starts with `MatrixPacking.ColumnMajor`. Use the C# option when the same convention should apply throughout a compile without editing the file. Use a declaration qualifier when a particular buffer field must keep its own layout. A `pack_matrix` pragma changes the default from that point in the source; the variation shows how it takes precedence over the C# option for an unqualified field.

## What is the difference between row-major and column-major matrix packing?

A `float2x3` has two rows and three columns either way. Row-major packs it in two groups of three values; column-major packs it in three groups of two. In this constant buffer, the groups start 16 bytes apart, so `Transform` occupies 32 bytes in the first compile and 48 in the second. The explicit `Fixed` matrix takes 48 bytes in both. The next field, `Bias`, moves with the size of `Transform`. The stride in this example is a constant-buffer layout rule, not a claim about all matrices in all buffers.

## When should I use row-major instead of column-major in HLSL?

Use the convention your CPU buffer writer expects. Check each reflected member’s offset and its type’s `Packing` and `MatrixStride`. If the shader and the writer disagree, they can read the wrong matrix elements or even the wrong following field. A flat, tightly packed array of six floats is not the padded layout in either run above.

Packing changes where the bytes go, not the meaning of `Transform[0][1]`. It also does not change the rules for [multiplying matrices and vectors in HLSL](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-per-component-math). Choose the order of operands based on the math, and choose the packing based on the buffer layout.
