From 02e2730fa20aaabbe24929a4c82f6ae124d694cb Mon Sep 17 00:00:00 2001 From: Peter Li Date: Sat, 31 Oct 2026 15:48:53 -0700 Subject: [PATCH] DXIL library subobjects State object config, exports associations, shader / pipeline configs and hit groups declared in a library become RDAT subobject records (root signature subobjects still need a root signature compiler). Co-Authored-By: Claude Opus 5.5 --- src/dxil/emit.zig | 19 +++++++++++++ src/dxil/rdat.zig | 23 +++++++++++++++ src/ir/ir.zig | 26 +++++++++++++++++ src/ir/lower.zig | 59 +++++++++++++++++++++++++++++++++++++++ test/dxil/subobjects.hlsl | 14 ++++++++++ 5 files changed, 141 insertions(+) create mode 100644 test/dxil/subobjects.hlsl diff --git a/src/dxil/emit.zig b/src/dxil/emit.zig index e8d2fcc..2f9e0d6 100644 --- a/src/dxil/emit.zig +++ b/src/dxil/emit.zig @@ -4259,6 +4259,25 @@ const Emitter = struct { .min_shader_target = (kind << 16) | (6 << 4) | libinfo.minMinor(info, kind), }); } + for (e.m.subobjects.items) |so| { + var rec: [6]u32 = .{ @intFromEnum(so.kind), try b.string(so.name), 0, 0, 0, 0 }; + switch (so.kind) { + .state_object_config, .raytracing_pipeline_config => rec[2] = so.ints[0], + .raytracing_shader_config, .raytracing_pipeline_config1 => rec[2..4].* = so.ints, + .subobject_to_exports_association => { + rec[2] = try b.string(so.strings[0]); + const idx = try e.arena.alloc(u32, so.exports.len); + for (idx, so.exports) |*x, name| x.* = try b.string(name); + rec[3] = try b.array(idx); + }, + .hit_group => { + rec[2] = so.ints[0]; + for (so.strings, 0..) |x, k| rec[3 + k] = try b.string(x); + }, + .global_root_signature, .local_root_signature => return e.unsupported("root signature subobjects are not supported yet", .{}), + } + try b.addSubobject(rec); + } const rdat_part = try b.finish(); defer gpa.free(rdat_part); var sfi0: [8]u8 = @splat(0); diff --git a/src/dxil/rdat.zig b/src/dxil/rdat.zig index 3715cbf..7d759d6 100644 --- a/src/dxil/rdat.zig +++ b/src/dxil/rdat.zig @@ -50,6 +50,8 @@ pub const Builder = struct { indices: std.ArrayList(u32) = .empty, resources: std.ArrayList([8]u32) = .empty, functions: std.ArrayList([14]u32) = .empty, + raw: std.ArrayList(u8) = .empty, + subobjects: std.ArrayList([6]u32) = .empty, pub fn init(gpa: Allocator) Allocator.Error!Builder { var b: Builder = .{ .gpa = gpa }; @@ -96,6 +98,19 @@ pub const Builder = struct { }); } + /// RuntimeDataSubobjectInfo: kind, name, then four kind-specific + /// words. + pub fn addSubobject(b: *Builder, rec: [6]u32) Allocator.Error!void { + try b.subobjects.append(b.gpa, rec); + } + + /// Offset of `bytes` in the raw bytes part. + pub fn bytes(b: *Builder, data: []const u8) Allocator.Error!u32 { + const off: u32 = @intCast(b.raw.items.len); + try b.raw.appendSlice(b.gpa, data); + return off; + } + /// The finished part. pub fn finish(b: *Builder) Allocator.Error![]u8 { const Part = struct { ty: u32, data: []const u8 }; @@ -118,6 +133,12 @@ pub const Builder = struct { try parts.append(b.gpa, .{ .ty = 4, .data = t }); } if (b.indices.items.len > 0) try parts.append(b.gpa, .{ .ty = 2, .data = std.mem.sliceAsBytes(b.indices.items) }); + if (b.raw.items.len > 0) try parts.append(b.gpa, .{ .ty = 5, .data = b.raw.items }); + if (b.subobjects.items.len > 0) { + const t = try table(b.gpa, 6, std.mem.sliceAsBytes(b.subobjects.items), b.subobjects.items.len); + try tables.append(b.gpa, t); + try parts.append(b.gpa, .{ .ty = 6, .data = t }); + } var size: usize = 8 + 4 * parts.items.len; for (parts.items) |p| size += 8 + std.mem.alignForward(usize, p.data.len, 4); @@ -151,6 +172,8 @@ pub const Builder = struct { b.indices.deinit(b.gpa); b.resources.deinit(b.gpa); b.functions.deinit(b.gpa); + b.raw.deinit(b.gpa); + b.subobjects.deinit(b.gpa); } }; diff --git a/src/ir/ir.zig b/src/ir/ir.zig index 1f5fa19..2f1c822 100644 --- a/src/ir/ir.zig +++ b/src/ir/ir.zig @@ -321,6 +321,30 @@ pub const Entry = struct { wrapper: u32 = 0, }; +/// A ray tracing state object subobject (DXIL::SubobjectKind values). +pub const Subobject = struct { + name: []const u8, + kind: Kind, + /// Flags, sizes, depths and hit group type, by kind. + ints: [2]u32 = .{ 0, 0 }, + /// Hit group shaders (any hit, closest hit, intersection); the + /// associated subobject; root signature text. + strings: [3][]const u8 = .{ "", "", "" }, + /// Associations: the exports. + exports: []const []const u8 = &.{}, + + pub const Kind = enum(u32) { + state_object_config = 0, + global_root_signature = 1, + local_root_signature = 2, + subobject_to_exports_association = 8, + raytracing_shader_config = 9, + raytracing_pipeline_config = 10, + hit_group = 11, + raytracing_pipeline_config1 = 12, + }; +}; + pub const Module = struct { arena: Allocator, types: *TypeTable, @@ -331,6 +355,8 @@ pub const Module = struct { entries: std.ArrayList(Entry) = .empty, /// Libraries: functions exported by name (`export`). exports: std.ArrayList(u32) = .empty, + /// Libraries: state object subobjects, in declaration order. + subobjects: std.ArrayList(Subobject) = .empty, pub fn addFunction(m: *Module, f: Function) !u32 { const p = try m.arena.create(Function); diff --git a/src/ir/lower.zig b/src/ir/lower.zig index ef990d4..1208a8e 100644 --- a/src/ir/lower.zig +++ b/src/ir/lower.zig @@ -47,6 +47,7 @@ pub fn lowerUnit(arena: Allocator, s: *Sema, opts: Options) Error!ir.Module { } else continue; try l.lowerEntry(f, f.name, stage); } + try l.subobjects(); for (s.all_functions.items) |f| { if (f.owner != null or !f.defined or !f.decl.quals.@"export" or f.decl.quals.static) continue; l.cur_entry = 0; @@ -131,6 +132,64 @@ const Lowerer = struct { try l.buildEntry(f, main, .{ .entry = name, .stage = stage }); } + /// The library's subobject declarations (`TriangleHitGroup hg = {...};`). + 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 }, + .{ "RaytracingPipelineConfig1", .raytracing_pipeline_config1 }, + }; + // (Builtin objects are static globals.) + for (l.s.static_globals.items) |v| { + const r = switch (l.s.types.get(v.type)) { + .resource => |r| r, + else => continue, + }; + if (r.kind != .other) continue; + const kind = for (kinds) |k| { + if (std.mem.eql(u8, r.name, k[0])) break k[1]; + } else continue; + const init_e = v.init orelse continue; + const items: []const *ast.Expr = if (init_e.kind == .init_list) init_e.kind.init_list else &.{init_e}; + var so: ir.Subobject = .{ .name = v.name, .kind = kind }; + var strs: std.ArrayList([]const u8) = .empty; + var ints: std.ArrayList(u32) = .empty; + for (items) |it| switch (it.kind) { + .string_lit => |lit| try strs.append(l.arena, lit[1 .. lit.len - 1]), + else => try ints.append(l.arena, @truncate(@as(u64, @bitCast(l.s.evalInt(it) orelse 0)))), + }; + for (ints.items[0..@min(2, ints.items.len)], 0..) |x, k| so.ints[k] = x; + switch (kind) { + .hit_group => { + // Triangle groups: any hit, closest hit; procedural ones + // add the intersection shader. + so.ints[0] = @intFromBool(std.mem.eql(u8, r.name, "ProceduralPrimitiveHitGroup")); + for (strs.items[0..@min(3, strs.items.len)], 0..) |x, k| so.strings[k] = x; + }, + .subobject_to_exports_association => { + if (strs.items.len > 0) so.strings[0] = strs.items[0]; + var exports: std.ArrayList([]const u8) = .empty; + if (strs.items.len > 1) { + var it = std.mem.splitScalar(u8, strs.items[1], ';'); + while (it.next()) |x| { + const t = std.mem.trim(u8, x, " \t"); + if (t.len > 0) try exports.append(l.arena, t); + } + } + so.exports = exports.items; + }, + .global_root_signature, .local_root_signature => if (strs.items.len > 0) { + so.strings[0] = strs.items[0]; + }, + else => {}, + } + try l.m.subobjects.append(l.arena, so); + } + } + fn unsupported(l: *Lowerer, loc: ast.Loc, comptime fmt: []const u8, args: anytype) void { _ = l.s.diags.report(.{ .severity = .@"error", .loc = loc }, "code generation: " ++ fmt, args); } diff --git a/test/dxil/subobjects.hlsl b/test/dxil/subobjects.hlsl new file mode 100644 index 0000000..47896c7 --- /dev/null +++ b/test/dxil/subobjects.hlsl @@ -0,0 +1,14 @@ +// PROFILE: lib_6_5 +// State object subobjects go to the RDAT subobject table (checked against +// dxc's RDAT). +// ZCHECK: define void @"\01?b@@YAXUP@@@Z" +StateObjectConfig soc = { STATE_OBJECT_FLAGS_ALLOW_LOCAL_DEPENDENCIES_ON_EXTERNAL_DEFINITONS }; +SubobjectToExportsAssociation sea = { "rsc", "a;b;foo;c" }; +SubobjectToExportsAssociation sea2 = { "rsc", ";" }; +RaytracingShaderConfig rsc = { 128, 64 }; +RaytracingPipelineConfig rpc = { 512 }; +TriangleHitGroup trHitGt = { "a", "b" }; +ProceduralPrimitiveHitGroup ppHitGt = { "a", "b", "c"}; +RaytracingPipelineConfig1 rpc1 = { 32, RAYTRACING_PIPELINE_FLAG_SKIP_TRIANGLES }; +struct P { float4 c; }; +[shader("miss")] void b(inout P p) { p.c = 1; }