zdxc: partial stores to shared memory; one SPIR-V type per strided array
Writing some components of a vector or matrix in an RW structured buffer or groupshared memory read the whole value and wrote it all back, racing with other threads writing the other components. Those stores now write only the named components, as dxc does. Matrix rows and elements of buffer matrices (W[i][1][2] = x, B[i].m[r] = v, at constant or dynamic rows) are stored element by element instead of being rejected. SPIR-V arrays with the same element, length and stride are now one type, so buffers of the same element share their runtime array.
This commit is contained in:
parent
717a0a9fad
commit
3fbbd5c4b5
|
|
@ -1717,7 +1717,10 @@ 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", .{});
|
||||
if (b.mat) |mat| {
|
||||
try e.matrixPartStore(b, mat, try e.scalars(frame, s.value));
|
||||
return .none;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
|
@ -3736,6 +3739,30 @@ const Emitter = struct {
|
|||
return .{ .buf = b };
|
||||
}
|
||||
|
||||
/// Stores a row (or one component) of a matrix in a structured buffer
|
||||
/// element by element: the row need not be contiguous.
|
||||
fn matrixPartStore(e: *Emitter, b: @FieldType(Lowered, "buf"), mat: @typeInfo(@FieldType(@FieldType(Lowered, "buf"), "mat")).optional.child, vals: []const Value) Error!void {
|
||||
const m = e.t.get(mat.ty).matrix;
|
||||
const ec = try e.t.scalar(m.elem);
|
||||
const i32t = try e.dm.intType(32);
|
||||
const res = e.resources.items[b.res];
|
||||
for (vals, 0..) |v, j| {
|
||||
const c: u32 = mat.col orelse @intCast(j);
|
||||
const off0 = layout.componentOffset(e.arena, e.t, mat.ty, b.row_major, c, .dx_packed) catch return e.unsupported("buffer element types", .{});
|
||||
const const_row: ?i64 = if (m.rows == 1) 0 else e.constIntOf(mat.row);
|
||||
if (const_row) |row| {
|
||||
const off = layout.componentOffset(e.arena, e.t, mat.ty, b.row_major, @as(u32, @intCast(row)) * m.cols + c, .dx_packed) catch return e.unsupported("buffer element types", .{});
|
||||
try e.structuredStore(res, b.index, b.offset + off, b.dyn, b.row_major, ec, &.{v});
|
||||
continue;
|
||||
}
|
||||
// A dynamic row: rows are a constant byte step apart.
|
||||
const step = (layout.componentOffset(e.arena, e.t, mat.ty, b.row_major, m.cols + c, .dx_packed) catch return e.unsupported("buffer element types", .{})) - off0;
|
||||
var dyn = try e.add(i32t, .{ .binop = .{ .op = .mul, .lhs = mat.row, .rhs = try e.i32Const(step) } });
|
||||
if (b.dyn) |d| dyn = try e.add(i32t, .{ .binop = .{ .op = .add, .lhs = d, .rhs = dyn } });
|
||||
try e.structuredStore(res, b.index, b.offset + off0, dyn, b.row_major, ec, &.{v});
|
||||
}
|
||||
}
|
||||
|
||||
/// Loads a value of `ty` at a byte offset of a structured buffer
|
||||
/// element: contiguous components of one type are read together.
|
||||
fn structuredLoad(e: *Emitter, res: Resource, idx: Value, base: u32, dyn: ?Value, row_major: bool, ty: TypeId) Error![]const Value {
|
||||
|
|
|
|||
|
|
@ -1787,6 +1787,25 @@ const Lowerer = struct {
|
|||
};
|
||||
}
|
||||
|
||||
/// Whether `e` designates part of an RW structured buffer element or a
|
||||
/// groupshared variable: memory other threads may write.
|
||||
fn isSharedMemory(l: *Lowerer, e: *const ast.Expr) bool {
|
||||
const t = l.types();
|
||||
var x = e;
|
||||
while (true) switch (x.kind) {
|
||||
.member => |m| x = m.base,
|
||||
.index => |ix| {
|
||||
if (t.get(ix.base.ty) == .resource) return t.get(ix.base.ty).resource.kind == .rw_structured_buffer;
|
||||
x = ix.base;
|
||||
},
|
||||
.name => {
|
||||
const r = l.s.resolution(x) orelse return false;
|
||||
return r == .variable and r.variable.storage == .groupshared;
|
||||
},
|
||||
else => return false,
|
||||
};
|
||||
}
|
||||
|
||||
/// Pointer to the storage field holding bitfield `bf` of the struct
|
||||
/// `ptr` points to.
|
||||
fn bitStorage(l: *Lowerer, ptr: Ref, bf: Bitfield) Error!Ref {
|
||||
|
|
@ -1863,6 +1882,22 @@ const Lowerer = struct {
|
|||
.member => |m| if (t.get(m.base.ty) == .matrix) {
|
||||
const mt = t.get(m.base.ty).matrix;
|
||||
const ks = matrixSwizzle(l.arena, m.name, mt.cols) orelse return error.Unsupported;
|
||||
if (l.isSharedMemory(m.base)) {
|
||||
// Other threads may write the other elements: store
|
||||
// only the named ones (as dxc).
|
||||
const base_ptr = try l.lvalue(m.base);
|
||||
const space = l.b.f.inst(base_ptr).ptr;
|
||||
const row_ty = try t.vector(mt.elem, mt.cols);
|
||||
const ec = try t.scalar(mt.elem);
|
||||
for (ks, 0..) |k, i| {
|
||||
const part = if (ks.len == 1) value else try l.component(value, @intCast(i));
|
||||
const r = try l.b.constInt(try t.scalar(.uint), k / mt.cols);
|
||||
const c = try l.b.constInt(try t.scalar(.uint), k % mt.cols);
|
||||
const row = try l.b.add(.{ .op = .{ .access = .{ .base = base_ptr, .indices = try l.arena.dupe(Ref, &.{r}) } }, .ty = row_ty, .ptr = space });
|
||||
try l.b.store(try l.b.add(.{ .op = .{ .access = .{ .base = row, .indices = try l.arena.dupe(Ref, &.{c}) } }, .ty = ec, .ptr = space }), part);
|
||||
}
|
||||
return;
|
||||
}
|
||||
var whole = try l.rvalue(m.base);
|
||||
const row_ty = try t.vector(mt.elem, mt.cols);
|
||||
for (ks, 0..) |k, i| {
|
||||
|
|
@ -1888,6 +1923,17 @@ const Lowerer = struct {
|
|||
}
|
||||
return;
|
||||
}
|
||||
if (l.isSharedMemory(m.base)) {
|
||||
// Store only the named components (as dxc).
|
||||
const base_ptr = try l.lvalue(m.base);
|
||||
const ec = try t.scalar(t.component(m.base.ty).?);
|
||||
for (comps, 0..) |c, i| {
|
||||
const part = if (comps.len == 1) value else try l.b.add(.{ .op = .{ .extract = .{ .agg = value, .index = @intCast(i) } }, .ty = ec });
|
||||
const idx = try l.b.constInt(try t.scalar(.uint), c);
|
||||
try l.b.store(try l.b.add(.{ .op = .{ .access = .{ .base = base_ptr, .indices = try l.arena.dupe(Ref, &.{idx}) } }, .ty = ec, .ptr = l.b.f.inst(base_ptr).ptr }), part);
|
||||
}
|
||||
return;
|
||||
}
|
||||
var whole = try l.rvalue(m.base);
|
||||
const ec = try t.scalar(t.component(m.base.ty).?);
|
||||
for (comps, 0..) |c, i| {
|
||||
|
|
|
|||
|
|
@ -140,6 +140,9 @@ const Emitter = struct {
|
|||
/// spellings are one type).
|
||||
spirv_types: std.StringHashMapUnmanaged(Id) = .empty,
|
||||
/// Explicitly laid out variants of arrays and structs (buffers).
|
||||
/// Arrays with an ArrayStride by element type, length (0: runtime
|
||||
/// array) and stride: one type per layout, as in dxc.
|
||||
strided_arrays: std.AutoHashMapUnmanaged([3]u32, Id) = .empty,
|
||||
layout_types: std.AutoHashMapUnmanaged(LayoutKey, Id) = .empty,
|
||||
/// Per instruction (current function): the buffer layout its pointer
|
||||
/// result points into, if any.
|
||||
|
|
@ -479,27 +482,35 @@ const Emitter = struct {
|
|||
}
|
||||
const key: LayoutKey = .{ ty, rules, row_major };
|
||||
if (e.layout_types.get(key)) |id| return id;
|
||||
const id = e.b.newId();
|
||||
switch (e.t.get(ty)) {
|
||||
.matrix => |m| {
|
||||
// Row-major integer matrix: an array of its rows.
|
||||
const row_ty = try e.layoutTypeId(try e.t.vector(m.elem, m.cols), rules, false);
|
||||
try e.b.emit(.types_globals, .type_array, &.{ id, row_ty, try e.constUint(m.rows) });
|
||||
try e.b.emit(.annotations, .decorate, &.{ id, 6, layout.matrixStride(e.t, ty, true, rules) }); // ArrayStride
|
||||
const id = switch (e.t.get(ty)) {
|
||||
// Row-major integer matrix: an array of its rows.
|
||||
.matrix => |m| try e.stridedArray(try e.layoutTypeId(try e.t.vector(m.elem, m.cols), rules, false), m.rows, layout.matrixStride(e.t, ty, true, rules)),
|
||||
.array => |a| try e.stridedArray(try e.layoutTypeId(a.elem, rules, row_major), a.len, layout.arrayStride(e.t, a.elem, row_major, rules) catch return e.unsupported("buffer member type", .{})),
|
||||
.@"struct" => |st| blk: {
|
||||
const id = e.b.newId();
|
||||
try e.layoutStruct(id, st, rules, st.name);
|
||||
break :blk id;
|
||||
},
|
||||
.array => |a| {
|
||||
const elem = try e.layoutTypeId(a.elem, rules, row_major);
|
||||
try e.b.emit(.types_globals, .type_array, &.{ id, elem, try e.constUint(a.len) });
|
||||
const stride = layout.arrayStride(e.t, a.elem, row_major, rules) catch return e.unsupported("buffer member type", .{});
|
||||
try e.b.emit(.annotations, .decorate, &.{ id, 6, stride }); // ArrayStride
|
||||
},
|
||||
.@"struct" => |st| try e.layoutStruct(id, st, rules, st.name),
|
||||
else => return e.unsupported("buffer members of type '{s}' are not supported yet", .{try e.t.format(e.arena, ty)}),
|
||||
}
|
||||
};
|
||||
try e.layout_types.put(e.arena, key, id);
|
||||
return id;
|
||||
}
|
||||
|
||||
/// `elem[len]` (a runtime array for `len` 0) with an ArrayStride.
|
||||
fn stridedArray(e: *Emitter, elem: Id, len: u32, stride: u32) Error!Id {
|
||||
const len_id = if (len == 0) 0 else try e.constUint(len);
|
||||
const gop = try e.strided_arrays.getOrPut(e.arena, .{ elem, len_id, stride });
|
||||
if (gop.found_existing) return gop.value_ptr.*;
|
||||
const id = e.b.newId();
|
||||
if (len == 0) {
|
||||
try e.b.emit(.types_globals, .type_runtime_array, &.{ id, elem });
|
||||
} else try e.b.emit(.types_globals, .type_array, &.{ id, elem, len_id });
|
||||
try e.b.emit(.annotations, .decorate, &.{ id, 6, stride }); // ArrayStride
|
||||
gop.value_ptr.* = id;
|
||||
return id;
|
||||
}
|
||||
|
||||
/// Emits struct `id` with `st`'s fields laid out by `rules`.
|
||||
fn layoutStruct(e: *Emitter, id: Id, st: *const types.StructInfo, rules: layout.Rules, name: []const u8) Error!void {
|
||||
{
|
||||
|
|
@ -2982,9 +2993,7 @@ const Emitter = struct {
|
|||
const elem_ty = if (r.kind == .byte_address_buffer or r.kind == .rw_byte_address_buffer) try e.t.scalar(.uint) else r.elem;
|
||||
const elem = (try e.bufferElemType(ty)).?;
|
||||
const stride = layout.arrayStride(e.t, elem_ty, r.row_major, e.sbRules()) catch return e.unsupported("buffer element type '{s}'", .{try e.t.format(e.arena, elem_ty)});
|
||||
const rt = e.b.newId();
|
||||
try e.b.emit(.types_globals, .type_runtime_array, &.{ rt, elem });
|
||||
try e.b.emit(.annotations, .decorate, &.{ rt, 6, stride }); // ArrayStride
|
||||
const rt = try e.stridedArray(elem, 0, stride);
|
||||
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
|
||||
|
|
|
|||
Loading…
Reference in New Issue