Recursive AI

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.

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

Terminal
dotnet add package RecursiveAI.OneShader

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

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

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

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, 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. Choose the order of operands based on the math, and choose the packing based on the buffer layout.

All oneshader recipes