From 8844c5445a897c7facc655663060cbeb8cc5a464 Mon Sep 17 00:00:00 2001 From: Peter Li Date: Sat, 31 Oct 2026 12:58:48 -0700 Subject: [PATCH] Add structured buffers of matrices SPIR-V: MatrixStride / RowMajor on the block's array member (std430 strides, as dxc). DXIL: rows and row components read through the whole matrix (a column-major row is not contiguous). Co-Authored-By: Claude Opus 5.5 --- src/dxil/emit.zig | 50 +++++++++++++++++++++++++++++++-- src/spirv/emit.zig | 8 +++++- test/dxil/buffer_matrices.hlsl | 11 ++++++++ test/spirv/buffer_matrices.hlsl | 13 +++++++++ 4 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 test/dxil/buffer_matrices.hlsl create mode 100644 test/spirv/buffer_matrices.hlsl diff --git a/src/dxil/emit.zig b/src/dxil/emit.zig index c80fd9f..fef8a96 100644 --- a/src/dxil/emit.zig +++ b/src/dxil/emit.zig @@ -61,7 +61,16 @@ const Lowered = union(enum) { typed: struct { base: Value, dty: DType, addr_space: u32, ty: TypeId, path: []const Value = &.{} }, /// A place inside a structured buffer element: element index plus byte /// offset (constant and dynamic). - buf: struct { res: u32, index: Value, offset: u32 = 0, dyn: ?Value = null, row_major: bool = false }, + buf: struct { + res: u32, + index: Value, + offset: u32 = 0, + dyn: ?Value = null, + row_major: bool = false, + /// A row (and component) of the matrix at the place: read from + /// the whole matrix (a row need not be contiguous). + mat: ?struct { ty: TypeId, row: Value, col: ?u32 = null } = null, + }, }; /// Pointer into a flattened local: alloca `base` of `[n x scalar]`, at @@ -915,7 +924,31 @@ const Emitter = struct { return .{ .scalars = try e.textureLoad(res, mip, tx.coords, null, i.ty) }; }, .cbuf => |c| return e.cbufferLoad(c.res, c.offset, c.dyn_regs, c.row_major, i.ty), - .buf => |b| return .{ .scalars = try e.structuredLoad(e.resources.items[b.res], b.index, b.offset, b.dyn, b.row_major, i.ty) }, + .buf => |b| { + const mat = b.mat orelse return .{ .scalars = try e.structuredLoad(e.resources.items[b.res], b.index, b.offset, b.dyn, b.row_major, i.ty) }; + const m = e.t.get(mat.ty).matrix; + const whole = try e.structuredLoad(e.resources.items[b.res], b.index, b.offset, b.dyn, b.row_major, mat.ty); + const cols: []const u32 = if (mat.col) |c| &.{c} else blk: { + const all = try e.arena.alloc(u32, m.cols); + for (all, 0..) |*x, k| x.* = @intCast(k); + break :blk all; + }; + const out = try e.arena.alloc(Value, cols.len); + for (out, cols) |*o, c| { + if (e.constIntOf(mat.row)) |row| { + o.* = whole[@as(u32, @intCast(row)) * m.cols + c]; + continue; + } + // A dynamic row: select among the rows. + var v = whole[c]; + for (1..m.rows) |row| { + const is_r = try e.add(try e.dm.intType(1), .{ .cmp = .{ .pred = 32, .lhs = mat.row, .rhs = try e.i32Const(@intCast(row)) } }); + v = try e.select(is_r, whole[row * m.cols + c], v, try e.valueType(v)); + } + o.* = v; + } + return .{ .scalars = out }; + }, .ptr => |ptr| return e.loadPtr(ptr, i.ty), .typed => |tp| return e.typedLoad(tp, i.ty), .mesh => return e.unsupported("reading mesh shader outputs is not supported", .{}), @@ -942,6 +975,7 @@ const Emitter = struct { } if (frame.vals[s.ptr.index()] == .buf) { const b = frame.vals[s.ptr.index()].buf; + if (b.mat != null) return e.unsupported("writing part of a matrix in a buffer is not supported yet", .{}); try e.structuredStore(e.resources.items[b.res], b.index, b.offset, b.dyn, b.row_major, f.inst(s.value).ty, try e.scalars(frame, s.value)); return .none; } @@ -2444,6 +2478,13 @@ const Emitter = struct { cur = st.fields.items[k].type; }, .array, .vector => { + if (b.mat) |*mt| { + // A component of a matrix row. + if (mt.col != null or xi.op != .constant) return e.unsupported("indexing this buffer member is not supported yet", .{}); + mt.col = @intCast(xi.op.constant.int); + cur = try e.t.scalar(e.t.get(cur).vector.elem); + continue; + } const elem = if (e.t.get(cur) == .array) e.t.get(cur).array.elem else try e.t.scalar(e.t.get(cur).vector.elem); const stride_b = layout.arrayStride(e.t, elem, b.row_major, .dx_packed) catch return e.unsupported("buffer element types", .{}); if (xi.op == .constant) { @@ -2455,6 +2496,11 @@ const Emitter = struct { } cur = elem; }, + .matrix => |m| { + if (b.mat != null) return e.unsupported("indexing this buffer member is not supported yet", .{}); + b.mat = .{ .ty = cur, .row = (try e.scalars(frame, x))[0] }; + cur = try e.t.vector(m.elem, m.cols); + }, else => return e.unsupported("indexing this buffer member is not supported yet", .{}), } } diff --git a/src/spirv/emit.zig b/src/spirv/emit.zig index 6041791..3fa3af6 100644 --- a/src/spirv/emit.zig +++ b/src/spirv/emit.zig @@ -790,6 +790,7 @@ const Emitter = struct { return switch (e.t.get(ty)) { .scalar => |s| storageScalar(s).name(), .vector => |v| std.fmt.allocPrint(e.arena, "v{d}{s}", .{ v.len, storageScalar(v.elem).name() }), + .matrix => |m| std.fmt.allocPrint(e.arena, "mat{d}v{d}{s}", .{ m.rows, m.cols, storageScalar(m.elem).name() }), else => e.t.format(e.arena, ty), }; } @@ -1917,7 +1918,6 @@ const Emitter = struct { .structured_buffer, .rw_structured_buffer, .append_structured_buffer, .consume_structured_buffer => r.elem, else => return null, }; - if (e.isTrueMatrix(elem_ty)) return e.unsupported("buffers of matrices are not supported yet", .{}); return try e.layoutTypeId(elem_ty, .std430, false); } @@ -1935,6 +1935,12 @@ const Emitter = struct { const st = e.b.newId(); try e.b.emit(.types_globals, .type_struct, &.{ st, rt }); try e.b.emit(.annotations, .member_decorate, &.{ st, 0, 35, 0 }); // Offset 0 + if (e.isTrueMatrix(elem_ty) and storageScalar(e.t.get(elem_ty).matrix.elem).isFloat()) { + // Buffers of (column-major) matrices: HLSL rows are SPIR-V + // columns. + try e.b.emit(.annotations, .member_decorate, &.{ st, 0, 7, layout.matrixStride(e.t, elem_ty, false, .std430) }); // MatrixStride + try e.b.emit(.annotations, .member_decorate, &.{ st, 0, 4 }); // RowMajor + } if (!r.kind.isWritable() and r.kind != .consume_structured_buffer) try e.b.emit(.annotations, .member_decorate, &.{ st, 0, 24 }); // NonWritable try e.b.emit(.annotations, .decorate, &.{ st, e.bufferBlockDecoration() }); try e.b.emitWithString(.debug_names, .name, &.{st}, try std.fmt.allocPrint(e.arena, "type.{s}.{s}", .{ r.name, try e.typeLabel(elem_ty) }), &.{}); diff --git a/test/dxil/buffer_matrices.hlsl b/test/dxil/buffer_matrices.hlsl new file mode 100644 index 0000000..61b45b3 --- /dev/null +++ b/test/dxil/buffer_matrices.hlsl @@ -0,0 +1,11 @@ +// PROFILE: cs_6_0 +// Structured buffers of matrices: whole loads and stores, rows (constant +// and dynamic) and row components. +StructuredBuffer m : register(t0); +RWStructuredBuffer r : register(u1); +RWStructuredBuffer o : register(u0); +[numthreads(1,1,1)] void main(uint i : SV_GroupIndex) { + o[i] = float4(m[i][1], 1) + r[i][0].x + float4(m[0][i % 4], m[1][2].y); + r[i] = r[i] * 2; + o[i + 1] = mul(float4(1, 2, 3, 4), m[i]).xyzz; +} diff --git a/test/spirv/buffer_matrices.hlsl b/test/spirv/buffer_matrices.hlsl new file mode 100644 index 0000000..df045a3 --- /dev/null +++ b/test/spirv/buffer_matrices.hlsl @@ -0,0 +1,13 @@ +// PROFILE: cs_6_0 +// Structured buffers of matrices: whole loads and stores, rows (constant +// and dynamic) and row components. +// ZCHECK: OpMemberDecorate %type_StructuredBuffer_mat4v3float 0 MatrixStride 16 +// ZCHECK: OpMemberDecorate %type_RWStructuredBuffer_mat2v3float 0 MatrixStride 8 +StructuredBuffer m : register(t0); +RWStructuredBuffer r : register(u1); +RWStructuredBuffer o : register(u0); +[numthreads(1,1,1)] void main(uint i : SV_GroupIndex) { + o[i] = float4(m[i][1], 1) + r[i][0].x + float4(m[0][i % 4], m[1][2].y); + r[i] = r[i] * 2; + o[i + 1] = mul(float4(1, 2, 3, 4), m[i]).xyzz; +}