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).
This commit is contained in:
Peter Li 2026-10-31 15:48:53 -07:00
parent bd04934bb3
commit 948a7fd864
4 changed files with 127 additions and 0 deletions

View File

@ -4259,6 +4259,25 @@ const Emitter = struct {
.min_shader_target = (kind << 16) | (6 << 4) | libinfo.minMinor(info, kind), .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(); const rdat_part = try b.finish();
defer gpa.free(rdat_part); defer gpa.free(rdat_part);
var sfi0: [8]u8 = @splat(0); var sfi0: [8]u8 = @splat(0);

View File

@ -50,6 +50,8 @@ pub const Builder = struct {
indices: std.ArrayList(u32) = .empty, indices: std.ArrayList(u32) = .empty,
resources: std.ArrayList([8]u32) = .empty, resources: std.ArrayList([8]u32) = .empty,
functions: std.ArrayList([14]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 { pub fn init(gpa: Allocator) Allocator.Error!Builder {
var b: Builder = .{ .gpa = gpa }; 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. /// The finished part.
pub fn finish(b: *Builder) Allocator.Error![]u8 { pub fn finish(b: *Builder) Allocator.Error![]u8 {
const Part = struct { ty: u32, data: []const 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 }); 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.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; var size: usize = 8 + 4 * parts.items.len;
for (parts.items) |p| size += 8 + std.mem.alignForward(usize, p.data.len, 4); 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.indices.deinit(b.gpa);
b.resources.deinit(b.gpa); b.resources.deinit(b.gpa);
b.functions.deinit(b.gpa); b.functions.deinit(b.gpa);
b.raw.deinit(b.gpa);
b.subobjects.deinit(b.gpa);
} }
}; };

View File

@ -321,6 +321,30 @@ pub const Entry = struct {
wrapper: u32 = 0, 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 { pub const Module = struct {
arena: Allocator, arena: Allocator,
types: *TypeTable, types: *TypeTable,
@ -331,6 +355,8 @@ pub const Module = struct {
entries: std.ArrayList(Entry) = .empty, entries: std.ArrayList(Entry) = .empty,
/// Libraries: functions exported by name (`export`). /// Libraries: functions exported by name (`export`).
exports: std.ArrayList(u32) = .empty, 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 { pub fn addFunction(m: *Module, f: Function) !u32 {
const p = try m.arena.create(Function); const p = try m.arena.create(Function);

View File

@ -47,6 +47,7 @@ pub fn lowerUnit(arena: Allocator, s: *Sema, opts: Options) Error!ir.Module {
} else continue; } else continue;
try l.lowerEntry(f, f.name, stage); try l.lowerEntry(f, f.name, stage);
} }
try l.subobjects();
for (s.all_functions.items) |f| { for (s.all_functions.items) |f| {
if (f.owner != null or !f.defined or !f.decl.quals.@"export" or f.decl.quals.static) continue; if (f.owner != null or !f.defined or !f.decl.quals.@"export" or f.decl.quals.static) continue;
l.cur_entry = 0; l.cur_entry = 0;
@ -131,6 +132,64 @@ const Lowerer = struct {
try l.buildEntry(f, main, .{ .entry = name, .stage = stage }); 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 { 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); _ = l.s.diags.report(.{ .severity = .@"error", .loc = loc }, "code generation: " ++ fmt, args);
} }