From 285505e36bf67ace5940889e241ef21bd4449b65 Mon Sep 17 00:00:00 2001 From: peterino2 Date: Tue, 3 Nov 2026 13:20:03 -0800 Subject: [PATCH] dxil: write [WaveSize] to the entry, PSV and RDAT [WaveSize] was checked but not written: the entry properties had no wave size, so a shader written for one wave size could run with another. DXC writes tag 11 { size } before SM 6.8 and tag 23 { min, max, preferred } from 6.8; PSV's and RDAT's expected wave lane counts take the minimum and the maximum (or the minimum again). Unreal: 44 of the 2,080 verify compiles use WaveSize and change; all 11,488 validate (validator 1.8). --- src/dxil/emit.zig | 20 ++++++++ src/dxil/shader.zig | 8 ++-- src/ir/ir.zig | 3 ++ src/ir/lower.zig | 114 ++++++++++++++++++++++---------------------- 4 files changed, 86 insertions(+), 59 deletions(-) diff --git a/src/dxil/emit.zig b/src/dxil/emit.zig index b5aca32..e7b7675 100644 --- a/src/dxil/emit.zig +++ b/src/dxil/emit.zig @@ -6783,6 +6783,9 @@ const Emitter = struct { .feature_info2 = @truncate(libinfo.featureInfo(info.flags) >> 32), .shader_stage_flag = if (lf.stage == .library) info.mask & 0xffff else info.mask & (@as(u32, 1) << @intCast(kind)), .min_shader_target = (kind << 16) | (6 << 4) | libinfo.minMinor(info, if (lf.stage == .library) null else kind), + // (RDAT: 0 where none is specified.) + .min_wave_lanes = @intCast(lf.entry.wave_size[0]), + .max_wave_lanes = @intCast(if (lf.entry.wave_size[1] != 0) lf.entry.wave_size[1] else lf.entry.wave_size[0]), .extended_properties = props, }); } @@ -6838,6 +6841,14 @@ const Emitter = struct { }); } + /// The wave lane counts PSV and RDAT give: WaveSize's minimum, and its + /// maximum (a range) or the minimum again; with none, 0 and maxInt. + fn waveLanes(entry: ir.Entry) [2]u32 { + const ws = entry.wave_size; + if (ws[0] == 0) return .{ 0, std.math.maxInt(u32) }; + return .{ ws[0], if (ws[1] != 0) ws[1] else ws[0] }; + } + /// Entry properties of the stage: thread group size and the GS / HS / /// DS / MS / AS state. fn stageProps(e: *Emitter, entry: ir.Entry, props: *std.ArrayList(?module.MdId)) Error!void { @@ -6849,6 +6860,14 @@ const Emitter = struct { }.f; const threads = try dm.mdNode(&.{ try mdI32(dm, entry.num_threads[0]), try mdI32(dm, entry.num_threads[1]), try mdI32(dm, entry.num_threads[2]) }); if (entry.stage == .compute) try props.appendSlice(e.arena, &.{ try mdI32(dm, 4), threads }); + // WaveSize: a value (tag 11) before SM 6.8, a range with a + // preferred size after (tag 23; zeros where not given). + if (entry.wave_size[0] != 0) { + const ws = entry.wave_size; + if (e.profile.minor >= 8) { + try props.appendSlice(e.arena, &.{ try mdI32(dm, 23), try dm.mdNode(&.{ try mdI32(dm, ws[0]), try mdI32(dm, ws[1]), try mdI32(dm, ws[2]) }) }); + } else try props.appendSlice(e.arena, &.{ try mdI32(dm, 11), try dm.mdNode(&.{try mdI32(dm, ws[0])}) }); + } const payload_size: u32 = if (entry.mesh.payload) |p| try e.payloadSize(p) else 0; if (entry.stage == .mesh) { // MS state: thread group, max vertices, max primitives, @@ -7064,6 +7083,7 @@ const Emitter = struct { .entry = entry.name, .stage_info = stage_info, .num_threads = if (threaded) entry.num_threads else .{ 0, 0, 0 }, + .wave_lanes = waveLanes(entry), .mesh_topology = if (entry.stage == .mesh) @backingInt(entry.mesh.topology) else 0, .uses_view_id = e.uses_view_id, .resources = binds.items, diff --git a/src/dxil/shader.zig b/src/dxil/shader.zig index 6ede345..258502c 100644 --- a/src/dxil/shader.zig +++ b/src/dxil/shader.zig @@ -143,6 +143,8 @@ pub const PsvInfo = struct { /// The stage-specific PSVRuntimeInfo0 union. stage_info: [16]u8 = @splat(0), num_threads: [3]u32 = .{ 0, 0, 0 }, + /// Minimum and maximum expected wave lane counts (WaveSize). + wave_lanes: [2]u32 = .{ 0, 0xFFFFFFFF }, resources: []const ResourceBind = &.{}, inputs: []const signature.Element = &.{}, outputs: []const signature.Element = &.{}, @@ -172,8 +174,8 @@ pub fn psv(gpa: Allocator, info: PsvInfo) ![]u8 { try w(&buf, gpa, 60); // sizeof(PSVRuntimeInfo4) // PSVRuntimeInfo0 try buf.appendSlice(gpa, &info.stage_info); - try w(&buf, gpa, 0); // MinimumExpectedWaveLaneCount - try w(&buf, gpa, 0xFFFFFFFF); // MaximumExpectedWaveLaneCount + try w(&buf, gpa, info.wave_lanes[0]); // MinimumExpectedWaveLaneCount + try w(&buf, gpa, info.wave_lanes[1]); // MaximumExpectedWaveLaneCount // PSVRuntimeInfo1: stage, UsesViewID, MaxVertexCount, element counts, // vector counts. // (The MaxVertexCount union holds SigPatchConstOrPrimVectors for hull @@ -221,7 +223,7 @@ pub fn psv(gpa: Allocator, info: PsvInfo) ![]u8 { try w(&records, gpa, idx_off); var cols_and_start: u8 = @intCast(e.cols); if (e.start_row != null) cols_and_start |= 0x40 | @as(u8, @intCast(e.start_col << 4)); - try records.appendSlice(gpa, &.{ @intCast(e.rows), @intCast(e.start_row orelse 0), cols_and_start, @intFromEnum(e.kind), @intCast(e.comp.sigCompType()), @intFromEnum(e.interp), @intCast((e.stream & 3) << 4), 0 }); + try records.appendSlice(gpa, &.{ @intCast(e.rows), @intCast(e.start_row orelse 0), cols_and_start, @backingInt(e.kind), @intCast(e.comp.sigCompType()), @backingInt(e.interp), @intCast((e.stream & 3) << 4), 0 }); }; const entry_off: u32 = @intCast(strings.items.len); try strings.appendSlice(gpa, info.entry); diff --git a/src/ir/ir.zig b/src/ir/ir.zig index 019fc35..57da522 100644 --- a/src/ir/ir.zig +++ b/src/ir/ir.zig @@ -436,6 +436,9 @@ pub const Entry = struct { name: []const u8, stage: Stage, num_threads: [3]u32 = .{ 1, 1, 1 }, + /// `[WaveSize(min[, max[, preferred]])]`: zeros where not given (no + /// attribute: min 0). + wave_size: [3]u32 = .{ 0, 0, 0 }, geometry: GeometryInfo = .{}, tess: TessInfo = .{}, mesh: MeshInfo = .{}, diff --git a/src/ir/lower.zig b/src/ir/lower.zig index 93d4910..6eae6a7 100644 --- a/src/ir/lower.zig +++ b/src/ir/lower.zig @@ -73,11 +73,11 @@ pub fn lowerUnit(arena: Allocator, s: *Sema, opts: Options) Error!ir.Module { /// The stage a `[shader("...")]` attribute names. fn stageNamed(name: []const u8) ?ir.Stage { const names = [_]struct { []const u8, ir.Stage }{ - .{ "vertex", .vertex }, .{ "pixel", .pixel }, .{ "geometry", .geometry }, - .{ "hull", .hull }, .{ "domain", .domain }, .{ "compute", .compute }, - .{ "mesh", .mesh }, .{ "amplification", .amplification }, .{ "raygeneration", .raygeneration }, - .{ "intersection", .intersection }, .{ "anyhit", .anyhit }, .{ "closesthit", .closesthit }, - .{ "miss", .miss }, .{ "callable", .callable }, + .{ "vertex", .vertex }, .{ "pixel", .pixel }, .{ "geometry", .geometry }, + .{ "hull", .hull }, .{ "domain", .domain }, .{ "compute", .compute }, + .{ "mesh", .mesh }, .{ "amplification", .amplification }, .{ "raygeneration", .raygeneration }, + .{ "intersection", .intersection }, .{ "anyhit", .anyhit }, .{ "closesthit", .closesthit }, + .{ "miss", .miss }, .{ "callable", .callable }, }; for (names) |t| if (std.ascii.eqlIgnoreCase(name, t[0])) return t[1]; return null; @@ -318,10 +318,10 @@ const Lowerer = struct { fn subobjects(l: *Lowerer) Error!void { const Kind = ir.Subobject.Kind; const kinds = [_]struct { []const u8, Kind }{ - .{ "StateObjectConfig", .state_object_config }, .{ "GlobalRootSignature", .global_root_signature }, - .{ "LocalRootSignature", .local_root_signature }, .{ "SubobjectToExportsAssociation", .subobject_to_exports_association }, - .{ "RaytracingShaderConfig", .raytracing_shader_config }, .{ "RaytracingPipelineConfig", .raytracing_pipeline_config }, - .{ "TriangleHitGroup", .hit_group }, .{ "ProceduralPrimitiveHitGroup", .hit_group }, + .{ "StateObjectConfig", .state_object_config }, .{ "GlobalRootSignature", .global_root_signature }, + .{ "LocalRootSignature", .local_root_signature }, .{ "SubobjectToExportsAssociation", .subobject_to_exports_association }, + .{ "RaytracingShaderConfig", .raytracing_shader_config }, .{ "RaytracingPipelineConfig", .raytracing_pipeline_config }, + .{ "TriangleHitGroup", .hit_group }, .{ "ProceduralPrimitiveHitGroup", .hit_group }, .{ "RaytracingPipelineConfig1", .raytracing_pipeline_config1 }, }; // (Builtin objects are static globals.) @@ -1412,49 +1412,49 @@ const Lowerer = struct { /// A call to `f` with `base_expr` as the object of a method (null: /// the current `this`, or no object). fn methodCall(l: *Lowerer, f: *const semamod.Function, fi: u32, base_expr: ?*ast.Expr, args: []const *ast.Expr, ret_ty: TypeId) Error!Ref { - var object: ?Ref = null; - var write_back: ?Ref = null; - const owner_ty = if (isMethod(f)) try l.s.types.intern(.{ .@"struct" = f.owner.? }) else TypeTable.void_type; - if (base_expr) |base| { - const direct = if (isAddressable(l.s, base)) try l.lvalue(base) else null; - const plain = if (direct) |p| l.b.f.inst(p).op == .local_var or l.b.f.inst(p).op == .param else false; - if (direct != null and plain and base.ty == owner_ty) { - object = direct; - } else { - // Through a function-local copy of the method's own - // struct (a base prefix of derived objects), written - // back afterwards to writable storage. - const tmp = try l.b.add(.{ .op = .local_var, .ty = owner_ty, .ptr = .function }); - var whole = if (direct) |p| try l.b.load(p) else try l.rvalue(base); - var whole_ty = base.ty; - if (l.s.types.get(base.ty) == .resource) { - // A method of ConstantBuffer's S: on a copy of S. - whole_ty = l.s.types.get(base.ty).resource.elem; - whole = try l.convert(whole, whole_ty); - } - try l.b.store(tmp, try l.basePrefix(whole, whole_ty, owner_ty)); - object = tmp; - if (direct) |p| switch (l.b.f.inst(p).ptr) { - .function, .private, .groupshared => write_back = p, - else => {}, - }; + var object: ?Ref = null; + var write_back: ?Ref = null; + const owner_ty = if (isMethod(f)) try l.s.types.intern(.{ .@"struct" = f.owner.? }) else TypeTable.void_type; + if (base_expr) |base| { + const direct = if (isAddressable(l.s, base)) try l.lvalue(base) else null; + const plain = if (direct) |p| l.b.f.inst(p).op == .local_var or l.b.f.inst(p).op == .param else false; + if (direct != null and plain and base.ty == owner_ty) { + object = direct; + } else { + // Through a function-local copy of the method's own + // struct (a base prefix of derived objects), written + // back afterwards to writable storage. + const tmp = try l.b.add(.{ .op = .local_var, .ty = owner_ty, .ptr = .function }); + var whole = if (direct) |p| try l.b.load(p) else try l.rvalue(base); + var whole_ty = base.ty; + if (l.s.types.get(base.ty) == .resource) { + // A method of ConstantBuffer's S: on a copy of S. + whole_ty = l.s.types.get(base.ty).resource.elem; + whole = try l.convert(whole, whole_ty); + } + try l.b.store(tmp, try l.basePrefix(whole, whole_ty, owner_ty)); + object = tmp; + if (direct) |p| switch (l.b.f.inst(p).ptr) { + .function, .private, .groupshared => write_back = p, + else => {}, + }; + } + } + const res = try l.userCall(f, fi, args, ret_ty, object); + if (write_back) |p| { + const updated = try l.b.load(object.?); + if (base_expr.?.ty == owner_ty) { + try l.b.store(p, updated); + } else { + // Only the base's fields. + for (l.s.types.get(owner_ty).@"struct".fields.items, 0..) |fld, k| { + const idx = try l.b.constInt(try l.types().scalar(.uint), @intCast(k)); + const fp = try l.b.add(.{ .op = .{ .access = .{ .base = p, .indices = try l.arena.dupe(Ref, &.{idx}) } }, .ty = fld.type, .ptr = l.b.f.inst(p).ptr }); + try l.b.store(fp, try l.b.add(.{ .op = .{ .extract = .{ .agg = updated, .index = @intCast(k) } }, .ty = fld.type })); } } - const res = try l.userCall(f, fi, args, ret_ty, object); - if (write_back) |p| { - const updated = try l.b.load(object.?); - if (base_expr.?.ty == owner_ty) { - try l.b.store(p, updated); - } else { - // Only the base's fields. - for (l.s.types.get(owner_ty).@"struct".fields.items, 0..) |fld, k| { - const idx = try l.b.constInt(try l.types().scalar(.uint), @intCast(k)); - const fp = try l.b.add(.{ .op = .{ .access = .{ .base = p, .indices = try l.arena.dupe(Ref, &.{idx}) } }, .ty = fld.type, .ptr = l.b.f.inst(p).ptr }); - try l.b.store(fp, try l.b.add(.{ .op = .{ .extract = .{ .agg = updated, .index = @intCast(k) } }, .ty = fld.type })); - } - } - } - return res; + } + return res; } /// An overloaded operator: a method call on the first operand. @@ -2429,6 +2429,9 @@ const Lowerer = struct { if (std.ascii.eqlIgnoreCase(a.name, "numthreads") and a.args.len == 3) { for (a.args, 0..) |arg, i| entry.num_threads[i] = @intCast(@max(1, l.s.evalInt(arg) orelse 1)); } + if (std.ascii.eqlIgnoreCase(a.name, "wavesize") and a.args.len >= 1 and a.args.len <= 3) { + for (a.args, 0..) |arg, i| entry.wave_size[i] = @intCast(@max(0, l.s.evalInt(arg) orelse 0)); + } if (std.ascii.eqlIgnoreCase(a.name, "maxvertexcount") and a.args.len == 1) entry.geometry.max_vertex_count = @intCast(@max(1, l.s.evalInt(a.args[0]) orelse 1)); if (std.ascii.eqlIgnoreCase(a.name, "instance") and a.args.len == 1) entry.geometry.instance_count = @intCast(@max(1, l.s.evalInt(a.args[0]) orelse 1)); if (std.ascii.eqlIgnoreCase(a.name, "RootSignature") and a.args.len == 1 and a.args[0].kind == .string_lit) entry.root_signature = try stringContents(l.arena, a.args[0].kind.string_lit); @@ -2708,11 +2711,11 @@ fn packOffset(v: *const Variable) ?u32 { fn imageFormatNamed(name: ?[]const u8) ?u32 { const n = name orelse return null; const names = [_][]const u8{ - "unknown", "rgba32f", "rgba16f", "r32f", "rgba8", "rgba8snorm", "rg32f", "rg16f", - "r11g11b10f", "r16f", "rgba16", "rgb10a2", "rg16", "rg8", "r16", "r8", - "rgba16snorm", "rg16snorm", "rg8snorm", "r16snorm", "r8snorm", "rgba32i", "rgba16i", "rgba8i", - "r32i", "rg32i", "rg16i", "rg8i", "r16i", "r8i", "rgba32ui", "rgba16ui", - "rgba8ui", "r32ui", "rgb10a2ui", "rg32ui", "rg16ui", "rg8ui", "r16ui", "r8ui", + "unknown", "rgba32f", "rgba16f", "r32f", "rgba8", "rgba8snorm", "rg32f", "rg16f", + "r11g11b10f", "r16f", "rgba16", "rgb10a2", "rg16", "rg8", "r16", "r8", + "rgba16snorm", "rg16snorm", "rg8snorm", "r16snorm", "r8snorm", "rgba32i", "rgba16i", "rgba8i", + "r32i", "rg32i", "rg16i", "rg8i", "r16i", "r8i", "rgba32ui", "rgba16ui", + "rgba8ui", "r32ui", "rgb10a2ui", "rg32ui", "rg16ui", "rg8ui", "r16ui", "r8ui", "r64ui", "r64i", }; for (names, 0..) |x, k| if (std.ascii.eqlIgnoreCase(x, n)) return @intCast(k); @@ -2748,7 +2751,6 @@ pub fn isObject(t: *TypeTable, ty: TypeId) bool { }; } - fn semanticOf(sems: []const ast.Semantic) ?[]const u8 { for (sems) |sem| if (sem.kind == .semantic) return sem.values[0]; return null;