diff --git a/src/dxil/emit.zig b/src/dxil/emit.zig index 3a85e1c..67359e0 100644 --- a/src/dxil/emit.zig +++ b/src/dxil/emit.zig @@ -45,6 +45,9 @@ const Lowered = union(enum) { /// A local holding a resource (index into Emitter.res_locals): an /// alias of the one resource stored into it. res_local: u32, + /// An array of resources (or a sub-array), with the flattened index so + /// far. + res_array: struct { res: u32, index: ?Value = null, ty: TypeId }, /// A place in a constant buffer: byte offset plus a dynamic register /// offset. cbuf: struct { res: u32, offset: u32 = 0, dyn_regs: ?Value = null, row_major: bool = false }, @@ -77,6 +80,11 @@ const Resource = struct { elem: TypeId, /// SamplerComparisonState. comparison: bool = false, + /// Registers in the range (arrays of resources); maxInt for unsized. + count: u32 = 1, + /// An array element's handle made at its use: a copy of the array's + /// entry, skipped by metadata. + view: bool = false, const Class = enum(u8) { srv = 0, uav = 1, cbuffer = 2, sampler = 3 }; }; @@ -111,7 +119,9 @@ const Emitter = struct { resources: std.ArrayList(Resource) = .empty, resource_of_global: std.AutoHashMapUnmanaged(u32, u32) = .empty, private_globals: std.AutoHashMapUnmanaged(u32, Ptr) = .empty, - res_locals: std.ArrayList(?u32) = .empty, + res_locals: std.ArrayList(?Lowered) = .empty, + /// Indices marked with NonUniformResourceIndex. + nonuniform: std.AutoHashMapUnmanaged(Value, void) = .empty, uses_double: bool = false, uses_int64: bool = false, /// 16-bit min-precision values cross the signature. @@ -299,14 +309,22 @@ const Emitter = struct { }; for (e.m.globals.items, 0..) |g, gi| { if (g.space != .resource) continue; - if (e.t.get(g.ty) == .sampler) { + // Arrays of resources are register ranges. + var gty = g.ty; + var count: u32 = 1; + while (e.t.get(gty) == .array) { + const arr = e.t.get(gty).array; + count = if (arr.len == 0 or count == std.math.maxInt(u32)) std.math.maxInt(u32) else count * arr.len; + gty = arr.elem; + } + if (e.t.get(gty) == .sampler) { if (g.binding.explicit and g.binding.class != 's') return e.unsupported("binding '{s}' to the wrong register class", .{g.name}); - try e.resources.append(e.arena, .{ .global = @intCast(gi), .class = .sampler, .id = counts[3], .kind = 14, .elem = g.ty, .comparison = e.t.get(g.ty).sampler.comparison }); + try e.resources.append(e.arena, .{ .global = @intCast(gi), .class = .sampler, .id = counts[3], .kind = 14, .elem = gty, .comparison = e.t.get(gty).sampler.comparison, .count = count }); counts[3] += 1; try e.resource_of_global.put(e.arena, @intCast(gi), @intCast(e.resources.items.len - 1)); continue; } - const r = switch (e.t.get(g.ty)) { + const r = switch (e.t.get(gty)) { .resource => |r| r, else => return e.unsupported("global '{s}' is not supported yet", .{g.name}), }; @@ -329,22 +347,33 @@ const Emitter = struct { if (g.binding.explicit and g.binding.class != @as(u8, if (class == .uav) 'u' else 't')) { return e.unsupported("binding '{s}' to the wrong register class", .{g.name}); } - try e.resources.append(e.arena, .{ .global = @intCast(gi), .class = class, .id = counts[@backingInt(class)], .kind = kind, .elem = r.elem }); + try e.resources.append(e.arena, .{ .global = @intCast(gi), .class = class, .id = counts[@backingInt(class)], .kind = kind, .elem = r.elem, .count = count }); counts[@backingInt(class)] += 1; try e.resource_of_global.put(e.arena, @intCast(gi), @intCast(e.resources.items.len - 1)); } // Unbound resources take the next free register of their class. var used: std.AutoHashMapUnmanaged(struct { u8, u32, u32 }, void) = .empty; + // (Unsized ranges are taken to hold 64 registers here.) + const span = struct { + fn f(n: u32) u32 { + return if (n == std.math.maxInt(u32)) 64 else n; + } + }.f; for (e.resources.items) |res| { const b = e.m.globals.items[res.global].binding; - if (b.explicit) try used.put(e.arena, .{ @backingInt(res.class), b.space, b.index }, {}); + if (b.explicit) for (0..span(res.count)) |k| try used.put(e.arena, .{ @backingInt(res.class), b.space, b.index + @as(u32, @intCast(k)) }, {}); } for (e.resources.items) |res| { const g = &e.m.globals.items[res.global]; if (g.binding.explicit) continue; var i: u32 = 0; - while (used.contains(.{ @backingInt(res.class), g.binding.space, i })) i += 1; - try used.put(e.arena, .{ @backingInt(res.class), g.binding.space, i }, {}); + while (true) : (i += 1) { + const free = for (0..span(res.count)) |k| { + if (used.contains(.{ @backingInt(res.class), g.binding.space, i + @as(u32, @intCast(k)) })) break false; + } else true; + if (free) break; + } + for (0..span(res.count)) |k| try used.put(e.arena, .{ @backingInt(res.class), g.binding.space, i + @as(u32, @intCast(k)) }, {}); // Globals are const in the module; record the chosen binding. @constCast(g).binding.index = i; } @@ -355,41 +384,53 @@ const Emitter = struct { } fn createHandles(e: *Emitter) Error!void { - if (e.resources.items.len == 0) return; + for (e.resources.items) |*res| { + // Array elements get their handles where they are used. + if (res.count != 1) continue; + res.handle = try e.makeHandle(res.*, try e.i32Const(e.binding(res.*).index), false, true); + } + } + + /// A handle to register `index` of `res`'s range (in the prologue, or + /// at the current point). + fn makeHandle(e: *Emitter, res: Resource, index: Value, nonuniform: bool, prologue: bool) Error!Value { const i32t = try e.dm.intType(32); const i1t = try e.dm.intType(1); const i8t = try e.dm.intType(8); + const b = e.binding(res); + const nu = try e.dm.constant(.{ .ty = i1t, .kind = .{ .int = @intFromBool(nonuniform) } }); + const emitCall = struct { + fn f(em: *Emitter, pro: bool, callee: u32, args: []const Value) Error!Value { + const op: @FieldType(module.Inst, "op") = .{ .call = .{ .callee = callee, .args = try em.arena.dupe(Value, args) } }; + return if (pro) em.addPrologue(em.handle_ty, op) else em.add(em.handle_ty, op); + } + }.f; if (self_sm66(e)) { const bind_ty = try e.dm.getType(.{ .@"struct" = .{ .name = "dx.types.ResBind", .elems = &.{ i32t, i32t, i32t, i8t } } }); const props_ty = try e.dm.getType(.{ .@"struct" = .{ .name = "dx.types.ResourceProperties", .elems = &.{ i32t, i32t } } }); const create = try e.dxOp("dx.op.createHandleFromBinding", e.handle_ty, &.{ i32t, bind_ty, i32t, i1t }, .nounwind_readnone); const annotate = try e.dxOp("dx.op.annotateHandle", e.handle_ty, &.{ i32t, e.handle_ty, props_ty }, .nounwind_readnone); - for (e.resources.items) |*res| { - const b = e.binding(res.*); - const lb = try e.dm.constant(.{ .ty = i32t, .kind = .{ .int = b.index } }); - const space = try e.dm.constant(.{ .ty = i32t, .kind = .{ .int = b.space } }); - const cls = try e.dm.constant(.{ .ty = i8t, .kind = .{ .int = @backingInt(res.class) } }); - const bind = try e.dm.constant(.{ .ty = bind_ty, .kind = .{ .aggregate = try e.arena.dupe(module.ConstId, &.{ lb.constant, lb.constant, space.constant, cls.constant }) } }); - const raw = try e.addPrologue(e.handle_ty, .{ .call = .{ .callee = create, .args = try e.arena.dupe(Value, &.{ try e.i32Const(217), bind, try e.i32Const(b.index), try e.dm.constant(.{ .ty = i1t, .kind = .{ .int = 0 } }) }) } }); - const p = try e.resourceProperties(res.*); - const p0 = try e.dm.constant(.{ .ty = i32t, .kind = .{ .int = p[0] } }); - const p1 = try e.dm.constant(.{ .ty = i32t, .kind = .{ .int = p[1] } }); - const props = try e.dm.constant(.{ .ty = props_ty, .kind = .{ .aggregate = try e.arena.dupe(module.ConstId, &.{ p0.constant, p1.constant }) } }); - res.handle = try e.addPrologue(e.handle_ty, .{ .call = .{ .callee = annotate, .args = try e.arena.dupe(Value, &.{ try e.i32Const(216), raw, props }) } }); - } - } else { - const create = try e.dxOp("dx.op.createHandle", e.handle_ty, &.{ i32t, i8t, i32t, i32t, i1t }, .nounwind_readonly); - for (e.resources.items) |*res| { - const b = e.binding(res.*); - res.handle = try e.addPrologue(e.handle_ty, .{ .call = .{ .callee = create, .args = try e.arena.dupe(Value, &.{ - try e.i32Const(57), - try e.dm.constant(.{ .ty = i8t, .kind = .{ .int = @backingInt(res.class) } }), - try e.i32Const(res.id), - try e.i32Const(b.index), - try e.dm.constant(.{ .ty = i1t, .kind = .{ .int = 0 } }), - }) } }); - } + const ub: i64 = if (res.count == std.math.maxInt(u32)) -1 else b.index + res.count - 1; + const lb_c = try e.dm.constant(.{ .ty = i32t, .kind = .{ .int = b.index } }); + const ub_c = try e.dm.constant(.{ .ty = i32t, .kind = .{ .int = ub } }); + const space = try e.dm.constant(.{ .ty = i32t, .kind = .{ .int = b.space } }); + const cls = try e.dm.constant(.{ .ty = i8t, .kind = .{ .int = @backingInt(res.class) } }); + const bind = try e.dm.constant(.{ .ty = bind_ty, .kind = .{ .aggregate = try e.arena.dupe(module.ConstId, &.{ lb_c.constant, ub_c.constant, space.constant, cls.constant }) } }); + const raw = try emitCall(e, prologue, create, &.{ try e.i32Const(217), bind, index, nu }); + const p = try e.resourceProperties(res); + const p0 = try e.dm.constant(.{ .ty = i32t, .kind = .{ .int = p[0] } }); + const p1 = try e.dm.constant(.{ .ty = i32t, .kind = .{ .int = p[1] } }); + const props = try e.dm.constant(.{ .ty = props_ty, .kind = .{ .aggregate = try e.arena.dupe(module.ConstId, &.{ p0.constant, p1.constant }) } }); + return emitCall(e, prologue, annotate, &.{ try e.i32Const(216), raw, props }); } + const create = try e.dxOp("dx.op.createHandle", e.handle_ty, &.{ i32t, i8t, i32t, i32t, i1t }, .nounwind_readonly); + return emitCall(e, prologue, create, &.{ + try e.i32Const(57), + try e.dm.constant(.{ .ty = i8t, .kind = .{ .int = @backingInt(res.class) } }), + try e.i32Const(res.id), + index, + nu, + }); } fn self_sm66(e: *Emitter) bool { @@ -650,6 +691,7 @@ const Emitter = struct { } const ri = e.resource_of_global.get(g) orelse return e.unsupported("global '{s}' is not supported yet", .{e.m.globals.items[g].name}); if (e.resources.items[ri].class == .cbuffer) return .{ .cbuf = .{ .res = ri } }; + if (e.resources.items[ri].count != 1) return .{ .res_array = .{ .res = ri, .ty = e.m.globals.items[g].ty } }; return .{ .handle = ri }; }, .local_var => { @@ -661,7 +703,8 @@ const Emitter = struct { }, .load => |p| switch (frame.vals[p.index()]) { .handle => |h| return .{ .handle = h }, - .res_local => |k| return .{ .handle = e.res_locals.items[k] orelse return e.unsupported("a resource local read before it is assigned", .{}) }, + .res_local => |k| return e.res_locals.items[k] orelse return e.unsupported("a resource local read before it is assigned", .{}), + .res_array => |ra| return .{ .res_array = ra }, .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) }, .ptr => |ptr| return e.loadPtr(ptr, i.ty), @@ -670,13 +713,11 @@ const Emitter = struct { .store => |s| { if (frame.vals[s.ptr.index()] == .res_local) { const slot = &e.res_locals.items[frame.vals[s.ptr.index()].res_local]; - const h = switch (frame.vals[s.value.index()]) { - .handle => |h| h, - else => return e.unsupported("resources not tied to a global are not supported yet", .{}), - }; + const h = frame.vals[s.value.index()]; + if (h != .handle and h != .res_array) return e.unsupported("resources not tied to a global are not supported yet", .{}); // Selecting between resources at run time has no // handle-based lowering here; one assignment is fine. - if (slot.*) |old| if (old != h) return e.unsupported("assigning different resources to one variable is not supported yet", .{}); + if (slot.*) |old| if (!std.meta.eql(old, h)) return e.unsupported("assigning different resources to one variable is not supported yet", .{}); slot.* = h; return .none; } @@ -693,6 +734,7 @@ const Emitter = struct { return .none; }, .access => |a| { + if (frame.vals[a.base.index()] == .res_array) return e.arrayElement(frame, frame.vals[a.base.index()].res_array, a.indices); if (frame.vals[a.base.index()] == .cbuf) return e.cbufferAccess(frame, frame.vals[a.base.index()].cbuf, f.inst(a.base).ty, a.indices); if (frame.vals[a.base.index()] == .handle) { const ri = frame.vals[a.base.index()].handle; @@ -1214,6 +1256,10 @@ const Emitter = struct { return .none; } if (try e.mathIntrinsic(name, a[0..args.len], ac, rc, out)) return .{ .scalars = out }; + if (eq(u8, name, "NonUniformResourceIndex")) { + try e.nonuniform.put(e.arena, a[0][0], {}); + return .{ .scalars = a[0] }; + } const Deriv = struct { name: []const u8, op: i64 }; const derivs = [_]Deriv{ .{ .name = "ddx", .op = 83 }, .{ .name = "ddx_coarse", .op = 83 }, .{ .name = "ddy", .op = 84 }, @@ -1525,8 +1571,60 @@ const Emitter = struct { // ------------------------------------------------------------------ // Resources and stage inputs + /// `array[i]...` on a resource array: a sub-array, or the element's + /// handle (created here, at register lower bound + flattened index). + fn arrayElement(e: *Emitter, frame: *Frame, start: @FieldType(Lowered, "res_array"), indices: []const Ref) Error!Lowered { + var ra = start; + const i32t = try e.dm.intType(32); + var nonuniform = false; + for (indices) |x| { + const arr = switch (e.t.get(ra.ty)) { + .array => |arr| arr, + else => return e.unsupported("indexing this resource is not supported yet", .{}), + }; + var inner: u32 = 1; + var et = arr.elem; + while (e.t.get(et) == .array) : (et = e.t.get(et).array.elem) inner *= e.t.get(et).array.len; + const xv = (try e.scalars(frame, x))[0]; + if (e.nonuniform.contains(xv)) nonuniform = true; + const scaled = try e.intArith(.mul, xv, try e.i32Const(inner)); + ra.index = if (ra.index) |old| try e.intArith(.add, old, scaled) else scaled; + ra.ty = arr.elem; + } + _ = i32t; + if (e.t.get(ra.ty) == .array) return .{ .res_array = ra }; + const res = e.resources.items[ra.res]; + const lb = e.binding(res).index; + const idx = try e.intArith(.add, ra.index orelse try e.i32Const(0), try e.i32Const(lb)); + var view = res; + view.view = true; + view.handle = try e.makeHandle(res, idx, nonuniform, false); + try e.resources.append(e.arena, view); + return .{ .handle = @intCast(e.resources.items.len - 1) }; + } + + fn constIntOf(e: *Emitter, v: Value) ?i64 { + if (v != .constant) return null; + return switch (e.dm.constants.items[v.constant].kind) { + .int => |x| x, + else => null, + }; + } + + /// i32 `a op b`, folded when both are constants (and identities). + fn intArith(e: *Emitter, op: module.BinOp, a: Value, b: Value) Error!Value { + const x = e.constIntOf(a); + const y = e.constIntOf(b); + if (x != null and y != null) return e.i32Const(@as(i32, @truncate(if (op == .add) x.? + y.? else x.? * y.?))); + if (op == .add and y == 0) return a; + if (op == .mul and y == 1) return a; + return e.add(try e.dm.intType(32), .{ .binop = .{ .op = op, .lhs = a, .rhs = b } }); + } + fn resourceKind(e: *Emitter, res: Resource) types.ResourceKind { - return e.t.get(e.m.globals.items[res.global].ty).resource.kind; + var ty = e.m.globals.items[res.global].ty; + while (e.t.get(ty) == .array) ty = e.t.get(ty).array.elem; + return e.t.get(ty).resource.kind; } /// The scalar a texture's ResRet carries for elements of `elem`. @@ -2244,8 +2342,9 @@ const Emitter = struct { if (e.uses_min_precision) flags |= 1 << 5; var uavs: u32 = 0; for (e.resources.items) |r| { + if (r.view) continue; if (r.kind == 11 or r.kind == 12) flags |= 1 << 4; - if (r.class == .uav) uavs += 1; + if (r.class == .uav) uavs += @min(r.count, 9); } if (uavs > 8) flags |= 1 << 15; if (e.uses_typed_uav_load) flags |= 1 << 13; @@ -2307,7 +2406,16 @@ const Emitter = struct { // Resource lists by class. var lists: [4]std.ArrayList(?module.MdId) = .{ .empty, .empty, .empty, .empty }; for (e.resources.items) |r| { + if (r.view) continue; const g = e.m.globals.items[r.global]; + // Ranges: the pointer is to an array; unsized ones have size -1. + const range: i64 = if (r.count == std.math.maxInt(u32)) -1 else r.count; + const rangeTy = struct { + fn f(m: *module.Module, n: u32, t: DType) !DType { + if (n == 1) return t; + return m.getType(.{ .array = .{ .len = if (n == std.math.maxInt(u32)) 0 else n, .elem = t } }); + } + }.f; if (r.class == .cbuffer) { const f32t = try dm.getType(.float); const cb_ty = try dm.getType(.{ .@"struct" = .{ .name = g.name, .elems = &.{f32t} } }); @@ -2318,13 +2426,15 @@ const Emitter = struct { } if (r.class == .sampler) { const name = if (r.comparison) "struct.SamplerComparisonState" else "struct.SamplerState"; - const st = try dm.getType(.{ .@"struct" = .{ .name = name, .elems = &.{try dm.intType(32)} } }); + const st = try rangeTy(dm, r.count, try dm.getType(.{ .@"struct" = .{ .name = name, .elems = &.{try dm.intType(32)} } })); const undef_s = try dm.constant(.{ .ty = try dm.ptrType(st), .kind = .undef }); const b = g.binding; - try lists[3].append(e.arena, try dm.mdNode(&.{ try mdI32(dm, r.id), try dm.mdValue(try dm.ptrType(st), undef_s), try dm.mdString(""), try mdI32(dm, b.space), try mdI32(dm, b.index), try mdI32(dm, 1), try mdI32(dm, @intFromBool(r.comparison)), null })); + try lists[3].append(e.arena, try dm.mdNode(&.{ try mdI32(dm, r.id), try dm.mdValue(try dm.ptrType(st), undef_s), try dm.mdString(""), try mdI32(dm, b.space), try mdI32(dm, b.index), try mdI32(dm, range), try mdI32(dm, @intFromBool(r.comparison)), null })); continue; } - const rt = e.t.get(g.ty).resource; + var rty = g.ty; + while (e.t.get(rty) == .array) rty = e.t.get(rty).array.elem; + const rt = e.t.get(rty).resource; const class_name = try std.fmt.allocPrint(e.arena, "class.{s}<{s}>", .{ rt.name, try e.t.format(e.arena, r.elem) }); // The class type only names the resource; aggregates get a // placeholder element. @@ -2332,11 +2442,11 @@ const Emitter = struct { // Typed resources carry their component count in the element // (the validator reads it from here). if (r.kind >= 1 and r.kind <= 10 and e.t.get(r.elem) == .vector) elem_dt = try dm.getType(.{ .vector = .{ .len = e.t.get(r.elem).vector.len, .elem = elem_dt } }); - const class_ty = try dm.getType(.{ .@"struct" = .{ .name = class_name, .elems = &.{elem_dt} } }); + const class_ty = try rangeTy(dm, r.count, try dm.getType(.{ .@"struct" = .{ .name = class_name, .elems = &.{elem_dt} } })); const undef_ptr = try dm.constant(.{ .ty = try dm.ptrType(class_ty), .kind = .undef }); const b = g.binding; var fields: std.ArrayList(?module.MdId) = .empty; - try fields.appendSlice(e.arena, &.{ try mdI32(dm, r.id), try dm.mdValue(try dm.ptrType(class_ty), undef_ptr), try dm.mdString(""), try mdI32(dm, b.space), try mdI32(dm, b.index), try mdI32(dm, 1), try mdI32(dm, r.kind) }); + try fields.appendSlice(e.arena, &.{ try mdI32(dm, r.id), try dm.mdValue(try dm.ptrType(class_ty), undef_ptr), try dm.mdString(""), try mdI32(dm, b.space), try mdI32(dm, b.index), try mdI32(dm, range), try mdI32(dm, r.kind) }); if (r.class == .uav) { try fields.appendSlice(e.arena, &.{ try mdBool(dm, i1t, false), try mdBool(dm, i1t, false), try mdBool(dm, i1t, false) }); } else try fields.append(e.arena, try mdI32(dm, 0)); // sample count @@ -2416,7 +2526,7 @@ const Emitter = struct { defer gpa.free(dxil_part); var binds: std.ArrayList(shader.ResourceBind) = .empty; // PSV lists CBVs, samplers, SRVs, then UAVs. - for ([_]Resource.Class{ .cbuffer, .sampler, .srv, .uav }) |cls| for (e.resources.items) |r| if (r.class == cls) { + for ([_]Resource.Class{ .cbuffer, .sampler, .srv, .uav }) |cls| for (e.resources.items) |r| if (r.class == cls and !r.view) { const b = e.binding(r); const res_type: u32 = switch (r.class) { .srv => switch (r.kind) { @@ -2432,7 +2542,8 @@ const Emitter = struct { .cbuffer => 2, .sampler => 1, }; - try binds.append(e.arena, .{ .res_type = res_type, .space = b.space, .lower = b.index, .upper = b.index, .kind = r.kind }); + const upper: u32 = if (r.count == std.math.maxInt(u32)) std.math.maxInt(u32) else b.index + r.count - 1; + try binds.append(e.arena, .{ .res_type = res_type, .space = b.space, .lower = b.index, .upper = upper, .kind = r.kind }); }; var stage_info: [16]u8 = @splat(0); switch (entry.stage) { diff --git a/src/ir/lower.zig b/src/ir/lower.zig index deb71c3..b3389e3 100644 --- a/src/ir/lower.zig +++ b/src/ir/lower.zig @@ -125,7 +125,7 @@ const Lowerer = struct { if (l.globals.get(v)) |i| return i; const space: ir.Space = switch (v.storage) { .groupshared => .groupshared, - .global_static, .constant => if (l.s.types.get(v.type) == .resource or l.s.types.get(v.type) == .sampler) .resource else .private, + .global_static, .constant => if (isObject(&l.s.types, v.type)) .resource else .private, .global_uniform, .cbuffer => unreachable, // see cbufferMember else => unreachable, }; @@ -164,7 +164,7 @@ const Lowerer = struct { }; } else { for (l.s.uniform_globals.items) |g| { - if (l.s.types.get(g.type) == .resource or l.s.types.get(g.type) == .sampler) continue; + if (isObject(&l.s.types, g.type)) continue; try members.append(l.arena, g); } } @@ -879,7 +879,7 @@ const Lowerer = struct { switch (v.storage) { .local, .param, .local_static => return l.fail(e, "this variable"), .global_uniform, .cbuffer => { - if (l.s.types.get(v.type) == .resource or l.s.types.get(v.type) == .sampler) { + if (isObject(&l.s.types, v.type)) { l.unsupported(e.loc, "resource '{s}' declared as a uniform is not supported yet", .{v.name}); return error.Unsupported; } @@ -1072,7 +1072,7 @@ const Lowerer = struct { for (l.s.static_globals.items) |v| { const init_e = v.init orelse continue; if (!l.globals.contains(v) or inits.contains(v)) continue; - if (l.s.types.get(v.type) == .resource or l.s.types.get(v.type) == .sampler) continue; + if (isObject(&l.s.types, v.type)) continue; const fi = try l.m.addFunction(.{ .name = try std.fmt.allocPrint(l.arena, "init.{s}", .{v.name}), .params = &.{}, .ret = v.type }); try inits.put(l.arena, v, fi); l.b = .{ .m = &l.m, .f = l.m.functions.items[fi] }; @@ -1140,6 +1140,15 @@ pub fn saturatingInt(f: f64) i64 { return @intFromFloat(@trunc(f)); } +/// Resources, samplers and arrays of them. +pub fn isObject(t: *TypeTable, ty: TypeId) bool { + return switch (t.get(ty)) { + .resource, .sampler => true, + .array => |a| isObject(t, a.elem), + else => false, + }; +} + fn paramAttrs(q: ast.Qualifiers) Lowerer.SigAttrs { return .{ .interp = semamod.interpOf(q), .row_major = q.row_major }; } diff --git a/src/spirv/builder.zig b/src/spirv/builder.zig index 2d36517..97fbf09 100644 --- a/src/spirv/builder.zig +++ b/src/spirv/builder.zig @@ -54,6 +54,7 @@ pub const Op = enum(u16) { composite_construct = 80, composite_extract = 81, composite_insert = 82, + copy_object = 83, transpose = 84, array_length = 68, sampled_image = 86, diff --git a/src/spirv/emit.zig b/src/spirv/emit.zig index 28131ac..8bccbf0 100644 --- a/src/spirv/emit.zig +++ b/src/spirv/emit.zig @@ -45,6 +45,13 @@ const Capability = enum(u32) { image_query = 50, storage_image_read_without_format = 55, storage_image_write_without_format = 56, + shader_non_uniform = 5301, + runtime_descriptor_array = 5302, + uniform_texel_buffer_array_non_uniform_indexing = 5311, + storage_texel_buffer_array_non_uniform_indexing = 5312, + sampled_image_array_non_uniform_indexing = 5307, + storage_buffer_array_non_uniform_indexing = 5308, + storage_image_array_non_uniform_indexing = 5309, }; pub fn emit(gpa: Allocator, m: *const ir.Module, diags: *diag.Diagnostics) Error![]u8 { @@ -86,6 +93,11 @@ const Emitter = struct { image_types: std.AutoHashMapUnmanaged([6]u32, Id) = .empty, sampled_image_types: std.AutoHashMapUnmanaged(Id, Id) = .empty, sampler_type: Id = 0, + resource_arrays: std.AutoHashMapUnmanaged([2]u32, Id) = .empty, + extensions: std.ArrayList([]const u8) = .empty, + /// Values selected with NonUniformResourceIndex (and resources reached + /// through them). + nonuniform_ids: std.AutoHashMapUnmanaged(Id, void) = .empty, variants: std.ArrayList(struct { fi: u32, bindings: []const Id, id: Id }) = .empty, /// Entry inputs / outputs: interface variable and its value type. inputs: []Interface = &.{}, @@ -484,16 +496,9 @@ const Emitter = struct { for (m.globals.items, 0..) |g, gi| { switch (g.space) { .resource => { - const r = if (e.t.get(g.ty) == .sampler) @FieldType(types.Type, "resource"){ .kind = .other, .elem = g.ty, .name = "SamplerState" } else switch (e.t.get(g.ty)) { - .resource => |r| r, - else => return e.unsupported("global '{s}' of type '{s}' is not supported yet", .{ g.name, try e.t.format(e.arena, g.ty) }), - }; const var_id = e.b.newId(); - const sc: StorageClass = switch (r.kind) { - .structured_buffer, .rw_structured_buffer, .byte_address_buffer, .rw_byte_address_buffer => .uniform, - else => .uniform_constant, - }; - try e.b.emit(.types_globals, .variable, &.{ try e.resourcePtrType(g.ty), var_id, @backingInt(sc) }); + const p = try e.resourcePointee(g.ty); + try e.b.emit(.types_globals, .variable, &.{ try e.pointerType(p.sc, p.ty), var_id, @backingInt(p.sc) }); var binding = g.binding.index; if (!g.binding.explicit) { while (used.contains(.{ g.binding.space, next_free })) next_free += 1; @@ -884,6 +889,7 @@ const Emitter = struct { try e.b.emit(.functions, .store, &.{ e.val(s.ptr), e.val(s.value) }); }, .access => |a| { + if (e.t.get(f.inst(a.base).ty) == .array and isResourceType(e.t, f.inst(a.base).ty)) return e.resourceArrayAccess(ri, a, i.ty); // Buffers reached through locals or parameters are still // Uniform std430 storage. const into_buffer = e.t.get(f.inst(a.base).ty) == .resource; @@ -935,6 +941,8 @@ const Emitter = struct { e.vals[ri] = e.b.newId(); ops.items[1] = e.vals[ri]; try e.b.emit(.functions, .access_chain, ops.items); + // Elements of a non-uniformly selected buffer. + if (e.nonuniform_ids.contains(e.val(a.base))) try e.markNonUniform(e.vals[ri], f.inst(a.base).ty); }, .binary => |x| { if (e.isTrueMatrix(i.ty)) { @@ -1213,17 +1221,84 @@ const Emitter = struct { /// The pointer type of a resource variable of HLSL type `ty` (also how /// resources are passed to functions). fn resourcePtrType(e: *Emitter, ty: TypeId) Error!Id { + const p = try e.resourcePointee(ty); + return e.pointerType(p.sc, p.ty); + } + + const Pointee = struct { sc: StorageClass, ty: Id }; + + /// Storage class and type of a resource variable of HLSL type `ty` + /// (arrays of resources are arrays of these). + fn resourcePointee(e: *Emitter, ty: TypeId) Error!Pointee { switch (e.t.get(ty)) { - .sampler => return e.pointerType(.uniform_constant, try e.samplerType()), + .sampler => return .{ .sc = .uniform_constant, .ty = try e.samplerType() }, .resource => |r| switch (r.kind) { - .structured_buffer, .rw_structured_buffer, .byte_address_buffer, .rw_byte_address_buffer => return e.pointerType(.uniform, try e.bufferBlockType(ty)), - else => if (texture.shape(r.kind)) |sh| return e.pointerType(.uniform_constant, try e.imageType(sh, r.elem)), + .structured_buffer, .rw_structured_buffer, .byte_address_buffer, .rw_byte_address_buffer => return .{ .sc = .uniform, .ty = try e.bufferBlockType(ty) }, + else => if (texture.shape(r.kind)) |sh| return .{ .sc = .uniform_constant, .ty = try e.imageType(sh, r.elem) }, + }, + .array => |arr| { + const inner = try e.resourcePointee(arr.elem); + const key: [2]u32 = .{ inner.ty, arr.len }; + if (e.resource_arrays.get(key)) |id| return .{ .sc = inner.sc, .ty = id }; + const id = e.b.newId(); + if (arr.len == 0) { + try e.cap(.runtime_descriptor_array); + try e.extension("SPV_EXT_descriptor_indexing"); + try e.b.emit(.types_globals, .type_runtime_array, &.{ id, inner.ty }); + } else try e.b.emit(.types_globals, .type_array, &.{ id, inner.ty, try e.constUint(arr.len) }); + try e.resource_arrays.put(e.arena, key, id); + return .{ .sc = inner.sc, .ty = id }; }, else => {}, } return e.unsupported("resource type '{s}' is not supported yet", .{try e.t.format(e.arena, ty)}); } + fn extension(e: *Emitter, name: []const u8) Error!void { + for (e.extensions.items) |x| if (std.mem.eql(u8, x, name)) return; + try e.extensions.append(e.arena, name); + try e.b.emitWithString(.extensions, .extension, &.{}, name, &.{}); + } + + /// `array[i]...` into an array of resources: an access chain to the + /// element (or sub-array) variable. + fn resourceArrayAccess(e: *Emitter, ri: usize, a: @FieldType(ir.Op, "access"), ty: TypeId) Error!void { + const p = try e.resourcePointee(ty); + var ops: std.ArrayList(u32) = .empty; + const id = e.b.newId(); + var nonuniform = false; + try ops.appendSlice(e.arena, &.{ try e.pointerType(p.sc, p.ty), id, e.val(a.base) }); + for (a.indices) |x| { + if (e.nonuniform_ids.contains(e.val(x))) nonuniform = true; + try ops.append(e.arena, e.val(x)); + } + try e.b.emit(.functions, .access_chain, ops.items); + e.vals[ri] = id; + if (nonuniform) try e.markNonUniform(id, ty); + } + + /// Decorates a non-uniformly selected resource value (and requires + /// the matching capability). + fn markNonUniform(e: *Emitter, id: Id, ty: TypeId) Error!void { + try e.cap(.shader_non_uniform); + try e.extension("SPV_EXT_descriptor_indexing"); + var et = ty; + while (e.t.get(et) == .array) et = e.t.get(et).array.elem; + const c: Capability = switch (e.t.get(et)) { + .sampler => .sampled_image_array_non_uniform_indexing, + .resource => |r| switch (r.kind) { + .structured_buffer, .rw_structured_buffer, .byte_address_buffer, .rw_byte_address_buffer => .storage_buffer_array_non_uniform_indexing, + .buffer => .uniform_texel_buffer_array_non_uniform_indexing, + .rw_buffer => .storage_texel_buffer_array_non_uniform_indexing, + else => if (r.kind.isWritable()) .storage_image_array_non_uniform_indexing else .sampled_image_array_non_uniform_indexing, + }, + else => .shader_non_uniform, + }; + try e.cap(c); + try e.b.emit(.annotations, .decorate, &.{ id, 5300 }); // NonUniform + try e.nonuniform_ids.put(e.arena, id, {}); + } + // ------------------------------------------------------------------ // Images and samplers @@ -1318,7 +1393,9 @@ const Emitter = struct { } fn loadImage(e: *Emitter, r: Ref) Error!Id { - return e.opRaw(.load, try e.imageTypeOf(r), &.{e.val(r)}); + const id = try e.opRaw(.load, try e.imageTypeOf(r), &.{e.val(r)}); + if (e.nonuniform_ids.contains(e.val(r))) try e.markNonUniform(id, e.f.inst(r).ty); + return id; } fn resourceInfo(e: *Emitter, r: Ref) @FieldType(types.Type, "resource") { @@ -1481,7 +1558,9 @@ const Emitter = struct { } if (call.op == .sample_position) return e.unsupported("GetSamplePosition is not supported yet", .{}); const smp = try e.opRaw(.load, try e.samplerType(), &.{e.val(args[call.sampler.?])}); + if (e.nonuniform_ids.contains(e.val(args[call.sampler.?]))) try e.markNonUniform(smp, e.f.inst(args[call.sampler.?]).ty); const si = try e.opRaw(.sampled_image, try e.sampledImageType(try e.imageTypeOf(res)), &.{ img, smp }); + if (e.nonuniform_ids.contains(img) or e.nonuniform_ids.contains(smp)) try e.markNonUniform(si, e.f.inst(res).ty); if (call.op == .calc_lod or call.op == .calc_lod_unclamped) { try e.cap(.image_query); const q = try e.opRaw(.image_query_lod, try e.typeId(try e.t.vector(.float, 2)), &.{ si, coord }); @@ -1673,6 +1752,15 @@ const Emitter = struct { } fn intrinsic(e: *Emitter, name: []const u8, ty: TypeId, args: []const Ref) Error!Id { + if (std.mem.eql(u8, name, "NonUniformResourceIndex")) { + // A decorated copy marks the index (as dxc). + const id = try e.op1(.copy_object, ty, &.{e.val(args[0])}); + try e.cap(.shader_non_uniform); + try e.extension("SPV_EXT_descriptor_indexing"); + try e.b.emit(.annotations, .decorate, &.{ id, 5300 }); // NonUniform + try e.nonuniform_ids.put(e.arena, id, {}); + return id; + } const ids = try e.arena.alloc(u32, args.len); const tys = try e.arena.alloc(TypeId, args.len); for (args, ids, tys) |a, *id, *t| { @@ -1868,6 +1956,7 @@ fn dfs(a: Allocator, f: *const ir.Function, b: u32, seen: []bool, post: *std.Arr fn isResourceType(t: *TypeTable, ty: TypeId) bool { return switch (t.get(ty)) { .resource, .sampler => true, + .array => |a| isResourceType(t, a.elem), else => false, }; } diff --git a/test/dxil/resource_arrays.hlsl b/test/dxil/resource_arrays.hlsl new file mode 100644 index 0000000..e11e5fd --- /dev/null +++ b/test/dxil/resource_arrays.hlsl @@ -0,0 +1,14 @@ +// PROFILE: ps_6_0 +// Resource arrays are register ranges; elements get handles where used. +// ZCHECK: createHandle(i32 57, i8 0, i32 0, i32 3, i1 false) +// ZCHECK: createHandle(i32 57, i8 0, i32 0, i32 {{.*}}, i1 true) +// ZCHECK: ]* undef, !"", i32 0, i32 1, i32 4, i32 2, i32 0, +// ZCHECK: [2 x %struct.SamplerState]* undef +Texture2D ts[4] : register(t1); +SamplerState ss[2] : register(s0); +RWStructuredBuffer bufs[3] : register(u0); +float4 main(float2 uv : UV, uint i : I) : SV_Target { + float4 c = ts[2].Sample(ss[1], uv) + ts[NonUniformResourceIndex(i)].Sample(ss[0], uv); + bufs[1][0] = c.x; + return c + bufs[i][3]; +} diff --git a/test/spirv/resource_arrays.hlsl b/test/spirv/resource_arrays.hlsl new file mode 100644 index 0000000..e21fa61 --- /dev/null +++ b/test/spirv/resource_arrays.hlsl @@ -0,0 +1,15 @@ +// PROFILE: ps_6_0 +// Resource arrays: array-typed variables, access chains, NonUniform as dxc. +// ZCHECK: OpCapability SampledImageArrayNonUniformIndexing +// ZCHECK: OpExtension "SPV_EXT_descriptor_indexing" +// ZCHECK: OpTypeArray {{.*}} %uint_4 +// ZCHECK: OpAccessChain %_ptr_Uniform_float {{.*}} %int_0 %uint_3 +// ZCHECK: NonUniform +Texture2D ts[4] : register(t1); +SamplerState ss[2] : register(s0); +RWStructuredBuffer bufs[3] : register(u0); +float4 main(float2 uv : UV, uint i : I) : SV_Target { + float4 c = ts[2].Sample(ss[1], uv) + ts[NonUniformResourceIndex(i)].Sample(ss[0], uv); + bufs[1][0] = c.x; + return c + bufs[i][3]; +}