diff --git a/src/dxil/emit.zig b/src/dxil/emit.zig index 5eb05c5..6d30ed5 100644 --- a/src/dxil/emit.zig +++ b/src/dxil/emit.zig @@ -166,6 +166,7 @@ const Emitter = struct { uses_resource_heap: bool = false, uses_extended_command_info: bool = false, uses_tiled_resources: bool = false, + uses_mesh_derivatives: bool = false, /// The ResRet of the last texture operation (for status arguments). last_resret: Value = undefined, /// Status values, forwarded through locals within a block: the @@ -548,6 +549,17 @@ const Emitter = struct { }); } + /// Derivatives (and implicit-LOD sampling): pixel shaders, and from SM + /// 6.6 compute, mesh and amplification shaders. + fn checkDerivatives(e: *Emitter) Error!void { + switch (e.m.entry.?.stage) { + .pixel => {}, + .compute, .mesh, .amplification => if (e.profile.minor < 6) return e.unsupported("derivatives outside pixel shaders need shader model 6.6", .{}), + else => return e.unsupported("derivatives are not available in {s} shaders", .{@tagName(e.m.entry.?.stage)}), + } + if (e.m.entry.?.stage == .mesh or e.m.entry.?.stage == .amplification) e.uses_mesh_derivatives = true; + } + /// The ResourceProperties constant annotating `res`'s handles. fn propertiesConst(e: *Emitter, res: Resource) Error!Value { const i32t = try e.dm.intType(32); @@ -1694,7 +1706,7 @@ const Emitter = struct { const is_deriv = eq(u8, name, "fwidth") or for (derivs) |d| { if (eq(u8, name, d.name)) break true; } else false; - if (is_deriv and e.m.entry.?.stage != .pixel) return e.unsupported("derivatives outside pixel shaders are not supported yet", .{}); + if (is_deriv) try e.checkDerivatives(); for (derivs) |d| if (eq(u8, name, d.name)) { for (out, 0..) |*o, k| o.* = try e.unaryOp(d.op, "unary", a[0][k], at); return .{ .scalars = out }; @@ -2436,7 +2448,19 @@ const Emitter = struct { } const sh = texture.shape(kind) orelse return e.unsupported("method '{s}' is not supported yet", .{name}); const call = texture.decode(name, sh, arg_refs.len) orelse return e.unsupported("method '{s}' is not supported yet", .{name}); - if (call.offsets4) return e.unsupported("Gather with four offsets is not supported yet", .{}); + if (call.offsets4) { + // Four offsets: a gather per offset, component k of the k-th. + if (call.status != null) return e.unsupported("Gather with four offsets and a status is not supported yet", .{}); + const out = try e.arena.alloc(Value, try e.flatCount(ty)); + for (0..4) |k| { + var one = call; + one.offsets4 = false; + one.offset = call.offset.? + @as(u8, @intCast(k)); + const r = try e.textureCall(frame, res, sh, one, name, arg_refs, ty); + if (k < out.len) out[k] = r.scalars[k]; + } + return .{ .scalars = out }; + } if (call.status) |k| { // The operation, then its status. var plain = call; @@ -2452,7 +2476,7 @@ const Emitter = struct { const i32t = try e.dm.intType(32); const f32t = try e.dm.getType(.float); const undef_i32 = try e.dm.constant(.{ .ty = i32t, .kind = .undef }); - if (call.op.implicitLod() and e.m.entry.?.stage != .pixel) return e.unsupported("'{s}' outside pixel shaders is not supported yet", .{name}); + if (call.op.implicitLod()) try e.checkDerivatives(); const arg = struct { fn f(em: *Emitter, fr: *Frame, refs: []const Ref, idx: ?u8) Error!?[]const Value { return if (idx) |k| try em.scalars(fr, refs[k]) else null; @@ -3530,6 +3554,7 @@ const Emitter = struct { if (e.uses_resource_heap) flags |= 1 << 30; if (e.uses_barycentrics) flags |= 1 << 22; if (e.uses_tiled_resources) flags |= 1 << 12; + if (e.uses_mesh_derivatives) flags |= 1 << 29; if (e.uses_extended_command_info) flags |= 1 << 38; if (e.uses_sampler_heap) flags |= 1 << 31; if (e.uses_wave) flags |= 1 << 19; @@ -3545,7 +3570,7 @@ const Emitter = struct { .{ 1 << 21, 0x10000 }, .{ 1 << 24, 0x80000 }, .{ 1 << 19, 0x4000 }, .{ 1 << 13, 0x800 }, .{ 1 << 30, 0x2000000 }, .{ 1 << 31, 0x4000000 }, .{ 1 << 22, 0x20000 }, .{ 1 << 38, 0x100000000 }, - .{ 1 << 12, 0x100 }, + .{ 1 << 12, 0x100 }, .{ 1 << 29, 0x1000000 }, }; var f: u64 = 0; for (map) |m| if (flags & m[0] != 0) { diff --git a/src/spirv/emit.zig b/src/spirv/emit.zig index f969908..5ea24c2 100644 --- a/src/spirv/emit.zig +++ b/src/spirv/emit.zig @@ -50,6 +50,8 @@ const Capability = enum(u32) { shader_viewport_index_layer_ext = 5254, fragment_fully_covered_ext = 5265, fragment_barycentric_khr = 5284, + compute_derivative_group_quads = 5288, + compute_derivative_group_linear = 5350, geometry_streams = 54, multi_viewport = 57, min_lod = 42, @@ -140,6 +142,8 @@ const Emitter = struct { /// on the heap's binding. heap_vars: std.AutoHashMapUnmanaged(struct { bool, Id }, Id) = .empty, heap_bindings: [2]u32 = .{ 0, 0 }, + /// Compute derivatives' execution mode (0: none). + derivative_mode: u32 = 0, /// Image types by (sampled type, dim, arrayed, ms, sampled, format). image_types: std.AutoHashMapUnmanaged([6]u32, Id) = .empty, sampled_image_types: std.AutoHashMapUnmanaged(Id, Id) = .empty, @@ -666,6 +670,7 @@ const Emitter = struct { }, else => {}, } + if (e.derivative_mode != 0) try e.b.emit(.execution_modes, .execution_mode, &.{ wrapper, e.derivative_mode }); for (m.functions.items, e.func_ids) |f, id| { if (!e.hasResourceParams(f)) try e.b.emitWithString(.debug_names, .name, &.{id}, f.name, &.{}); } @@ -1977,6 +1982,27 @@ const Emitter = struct { } } + /// Derivatives outside pixel shaders: compute-like stages, over quads + /// of the thread group (or linearly, as dxc). + fn checkDerivatives(e: *Emitter) Error!void { + const entry = e.m.entry.?; + switch (entry.stage) { + .pixel => return, + .compute, .mesh, .amplification => {}, + else => return e.unsupported("derivatives are not available in {s} shaders", .{@tagName(entry.stage)}), + } + if (e.derivative_mode != 0) return; + try e.extension("SPV_KHR_compute_shader_derivatives"); + const t = entry.num_threads; + if (t[0] % 2 == 0 and t[1] % 2 == 0) { + try e.cap(.compute_derivative_group_quads); + e.derivative_mode = 5289; // DerivativeGroupQuadsKHR + } else { + try e.cap(.compute_derivative_group_linear); + e.derivative_mode = 5290; // DerivativeGroupLinearKHR + } + } + /// Reads an interface variable as a value of type `ty`. fn loadInterface(e: *Emitter, iface: Interface, sc: StorageClass, ty: TypeId) Error!Id { if (!iface.elem_array) return e.reshape(try e.op1(.load, iface.ty, &.{iface.var_id}), iface.ty, ty); @@ -2371,8 +2397,8 @@ const Emitter = struct { const sh = texture.shape(r.kind) orelse return e.unsupported("method '{s}' is not supported yet", .{name}); const call = texture.decode(name, sh, args.len) orelse return e.unsupported("method '{s}' is not supported yet", .{name}); const status: ?Ref = if (call.status) |k| args[k] else null; - if (call.offsets4) return e.unsupported("Gather with four offsets is not supported yet", .{}); - if (call.op.implicitLod() and e.m.entry.?.stage != .pixel) return e.unsupported("'{s}' outside pixel shaders is not supported yet", .{name}); + + if (call.op.implicitLod()) try e.checkDerivatives(); const s = try e.texelScalar(r.elem); const img = try e.loadImage(res); const coord = e.val(args[call.coord]); @@ -2414,7 +2440,23 @@ const Emitter = struct { mask |= 0x2; try extra.append(e.arena, lod_from_coord.?); } - if (call.offset) |k| { + if (call.offsets4) { + // Four offsets: ConstOffsets, an array of constants. + const k = call.offset.?; + const ot = try e.t.vector(.int, 2); + const arr_ty = try e.t.array(ot, 4); + var ops: std.ArrayList(u32) = .empty; + const id = e.b.newId(); + try ops.appendSlice(e.arena, &.{ try e.typeId(arr_ty), id }); + for (0..4) |j| { + const c = (try e.constValue(args[k + j])) orelse return e.unsupported("Gather with four offsets that are not constants is not supported yet", .{}); + try ops.append(e.arena, c); + } + try e.b.emit(.types_globals, .constant_composite, ops.items); + try e.cap(.image_gather_extended); + mask |= 0x20; + try extra.append(e.arena, id); + } else if (call.offset) |k| { if (try e.constValue(args[k])) |c| { mask |= 0x8; try extra.append(e.arena, c); @@ -3157,7 +3199,7 @@ const Emitter = struct { const deriv = for ([_][]const u8{ "ddx", "ddy", "ddx_coarse", "ddy_coarse", "ddx_fine", "ddy_fine", "fwidth" }) |d| { if (eqn(u8, name, d)) break true; } else false; - if (deriv and e.m.entry.?.stage != .pixel) return e.unsupported("derivatives outside pixel shaders are not supported yet", .{}); + if (deriv) try e.checkDerivatives(); if (eqn(u8, name, "ddx_fine") or eqn(u8, name, "ddy_fine")) { try e.cap(.derivative_control); return e.op1(if (eqn(u8, name, "ddx_fine")) .dpdx_fine else .dpdy_fine, ty, ids);