From 39a728d66360ff3820ace7320d0b3524e6417c4e Mon Sep 17 00:00:00 2001 From: Peter Li Date: Sat, 31 Oct 2026 15:19:52 -0700 Subject: [PATCH] SPIR-V shader record buffers and Volatile ray tracing builtins [[vk::shader_record_ext]] / [[vk::shader_record_nv]] ConstantBuffers and cbuffers become ShaderRecordBufferKHR blocks (std430, no descriptor), listed only in the interfaces of entries that use them. Builtins that change during a ray tracing invocation are Volatile from SPIR-V 1.6. Co-Authored-By: Claude Opus 5.5 --- src/front/ast.zig | 1 + src/front/parser.zig | 6 +++++- src/ir/ir.zig | 3 +++ src/ir/lower.zig | 14 +++++++++++-- src/spirv/emit.zig | 49 +++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 67 insertions(+), 6 deletions(-) diff --git a/src/front/ast.zig b/src/front/ast.zig index 2ab0d4c..f65b75f 100644 --- a/src/front/ast.zig +++ b/src/front/ast.zig @@ -259,6 +259,7 @@ pub const StructDecl = struct { }; pub const CBufferDecl = struct { + attrs: []const Attribute = &.{}, tbuffer: bool, name: []const u8, semantics: []const Semantic, diff --git a/src/front/parser.zig b/src/front/parser.zig index ad3610d..ebf4999 100644 --- a/src/front/parser.zig +++ b/src/front/parser.zig @@ -715,7 +715,11 @@ pub const Parser = struct { return p.parseStructDeclInto(out, attrs, lead_quals, template_params, ctx); } p.pos = save; - if (t.isIdent("cbuffer") or t.isIdent("tbuffer")) return out.append(p.arena, try p.parseCBuffer()); + if (t.isIdent("cbuffer") or t.isIdent("tbuffer")) { + const d = try p.parseCBuffer(); + d.kind.cbuffer.attrs = attrs; + return out.append(p.arena, d); + } if (t.isIdent("typedef")) return p.parseTypedefInto(out); if (t.isIdent("using")) return out.append(p.arena, try p.parseUsing(template_params)); if (t.isIdent("namespace")) return out.append(p.arena, try p.parseNamespace()); diff --git a/src/ir/ir.zig b/src/ir/ir.zig index a4cba46..1f5fa19 100644 --- a/src/ir/ir.zig +++ b/src/ir/ir.zig @@ -216,6 +216,9 @@ pub const Global = struct { /// A `tbuffer` block (space `cbuffer`): a texture buffer with the /// constant buffer layout. tbuffer: bool = false, + /// `[[vk::shader_record_ext]]` / `[[vk::shader_record_nv]]` buffers + /// (SPIR-V ShaderRecordBufferKHR storage). + shader_record: bool = false, }; pub const Stage = enum { diff --git a/src/ir/lower.zig b/src/ir/lower.zig index 2954ca9..54db047 100644 --- a/src/ir/lower.zig +++ b/src/ir/lower.zig @@ -233,7 +233,8 @@ const Lowerer = struct { if (sem.kind != .register) continue; binding = parseBinding(sem.values); }; - try l.m.globals.append(l.arena, .{ .name = v.name, .ty = v.type, .space = space, .binding = binding }); + const shader_record = if (v.decl) |d| isShaderRecord(d.attrs) else false; + try l.m.globals.append(l.arena, .{ .name = v.name, .ty = v.type, .space = space, .binding = binding, .shader_record = shader_record }); const i: u32 = @intCast(l.m.globals.items.len - 1); try l.globals.put(l.arena, v, i); return i; @@ -249,9 +250,11 @@ const Lowerer = struct { var name: []const u8 = "$Globals"; var binding: ir.Binding = .{}; var is_tbuffer = false; + var shader_record = false; if (v.block) |b| { const cb = b.kind.cbuffer; is_tbuffer = cb.tbuffer; + shader_record = isShaderRecord(cb.attrs); name = cb.name; for (cb.members) |m| if (m.kind == .@"var") { if (l.s.decl_vars.get(&m.kind.@"var")) |mv| try members.append(l.arena, mv); @@ -269,7 +272,7 @@ const Lowerer = struct { info.* = .{ .name = name, .complete = true }; for (members.items) |mv| try info.fields.append(l.arena, .{ .name = mv.name, .type = mv.type, .row_major = mv.row_major }); const ty = try l.s.types.intern(.{ .@"struct" = info }); - try l.m.globals.append(l.arena, .{ .name = name, .ty = ty, .space = .cbuffer, .binding = binding, .tbuffer = is_tbuffer }); + try l.m.globals.append(l.arena, .{ .name = name, .ty = ty, .space = .cbuffer, .binding = binding, .tbuffer = is_tbuffer, .shader_record = shader_record }); const gi: u32 = @intCast(l.m.globals.items.len - 1); try l.cbuffers.put(l.arena, key, gi); for (members.items, 0..) |mv, fi| try l.cbuffer_members.put(l.arena, mv, .{ .global = gi, .field = @intCast(fi) }); @@ -1985,6 +1988,13 @@ fn isMethod(f: *const semamod.Function) bool { return f.owner != null and !f.decl.quals.static; } +fn isShaderRecord(attrs: []const ast.Attribute) bool { + for (attrs) |a| { + if (std.mem.eql(u8, a.name, "vk::shader_record_ext") or std.mem.eql(u8, a.name, "vk::shader_record_nv")) return true; + } + return false; +} + /// RayQuery objects: stateful, they live in function storage. pub fn isRayQuery(t: *TypeTable, ty: TypeId) bool { return switch (t.get(ty)) { diff --git a/src/spirv/emit.zig b/src/spirv/emit.zig index 9ef5d7a..0133432 100644 --- a/src/spirv/emit.zig +++ b/src/spirv/emit.zig @@ -703,7 +703,8 @@ const Emitter = struct { if (e.fn_calls.get(fid)) |cs| try stack.appendSlice(e.arena, cs.items); } if (e.spv_minor >= 4) { - for (e.global_vars) |v| if (v != 0) try out.put(e.arena, v, {}); + // (Shader record buffers only where used: at most one each.) + for (e.global_vars, e.m.globals.items) |v, g| if (v != 0 and !g.shader_record) try out.put(e.arena, v, {}); for (e.counter_vars) |v| if (v != 0) try out.put(e.arena, v, {}); } return out.keys(); @@ -808,6 +809,22 @@ const Emitter = struct { for (m.globals.items) |g| if (g.binding.explicit) try used.put(e.arena, .{ g.binding.space, g.binding.index }, {}); var next_free: u32 = 0; for (m.globals.items, 0..) |g, gi| { + switch (g.space) { + .resource, .cbuffer => if (g.shader_record) { + // Shader record buffers: std430, no descriptor. + const st = e.t.get(if (g.space == .resource) e.t.get(g.ty).resource.elem else g.ty); + if (st != .@"struct") return e.unsupported("this shader record buffer is not supported yet", .{}); + const block = e.b.newId(); + try e.layoutStruct(block, st.@"struct", .std430, try std.fmt.allocPrint(e.arena, "type.ShaderRecord.{s}", .{g.name})); + try e.b.emit(.annotations, .decorate, &.{ block, 2 }); // Block + const var_id = e.b.newId(); + try e.b.emit(.types_globals, .variable, &.{ try e.pointerType(.shader_record_buffer_khr, block), var_id, @backingInt(StorageClass.shader_record_buffer_khr) }); + try e.b.emitWithString(.debug_names, .name, &.{var_id}, g.name, &.{}); + e.global_vars[gi] = var_id; + continue; + }, + else => {}, + } switch (g.space) { .resource => { const var_id = e.b.newId(); @@ -1614,6 +1631,11 @@ const Emitter = struct { }, i.ty), .global => |g| { e.vals[ri] = e.global_vars[g]; + if (e.m.globals.items[g].shader_record) { + try e.useVar(e.global_vars[g]); + e.ptr_layouts[ri] = .{ .rules = .std430, .row_major = false }; + return; + } e.ptr_layouts[ri] = switch (e.m.globals.items[g].space) { .cbuffer => .{ .rules = if (e.m.globals.items[g].tbuffer) .std430 else .vk_relaxed, .row_major = false }, .resource => .{ .rules = .std430, .row_major = false }, @@ -1720,14 +1742,15 @@ const Emitter = struct { try e.mesh_places.put(e.arena, ri, mp); return; } - const sc: StorageClass = if (into_cb) .uniform else if (into_buffer or (base_pl != null and base_pl.?.rules == .std430)) e.bufferSc() else if (base_pl != null) .uniform else switch (i.ptr) { + const shader_record = e.inShaderRecord(a.base); + const sc: StorageClass = if (shader_record) .shader_record_buffer_khr else if (into_cb) .uniform else if (into_buffer or (base_pl != null and base_pl.?.rules == .std430)) e.bufferSc() else if (base_pl != null) .uniform else switch (i.ptr) { .function => .function, .private => .private, .groupshared => if (e.payload_global != null and e.rootGlobal(a.base) == e.payload_global) .task_payload_workgroup else .workgroup, .cbuffer, .resource => .uniform, else => return e.unsupported("access into this storage is not supported yet", .{}), }; - var pl = if (into_cb) PtrLayout{ .rules = .vk_relaxed, .row_major = false } else if (into_tb) PtrLayout{ .rules = .std430, .row_major = false } else if (into_buffer) PtrLayout{ .rules = .std430, .row_major = false } else e.ptr_layouts[a.base.index()]; + var pl = if (shader_record) PtrLayout{ .rules = .std430, .row_major = false } else if (into_cb) PtrLayout{ .rules = .vk_relaxed, .row_major = false } else if (into_tb) PtrLayout{ .rules = .std430, .row_major = false } else if (into_buffer) PtrLayout{ .rules = .std430, .row_major = false } else e.ptr_layouts[a.base.index()]; var ops: std.ArrayList(u32) = .empty; try ops.appendSlice(e.arena, &.{ 0, 0, e.val(a.base) }); var cur = f.inst(a.base).ty; @@ -3166,6 +3189,18 @@ const Emitter = struct { } /// The global a resource value comes from (`load (global g)`). + /// Whether pointer / resource `r` reaches into a shader record buffer. + fn inShaderRecord(e: *Emitter, r: Ref) bool { + var x = r; + while (true) switch (e.f.inst(x).op) { + .access => |a| x = a.base, + .load => |p| x = p, + else => break, + }; + const g = e.globalOf(x) orelse return false; + return e.m.globals.items[g].shader_record; + } + fn globalOf(e: *Emitter, r: Ref) ?u32 { const i = e.f.inst(r); return switch (i.op) { @@ -3335,6 +3370,14 @@ const Emitter = struct { while (e.t.get(et) == .array) et = e.t.get(et).array.elem; const c = e.t.component(et).?; if (e.entry.stage == .pixel and !c.isFloat() and c != .bool) try e.b.emit(.annotations, .decorate, &.{ v, 14 }); // Flat + // Values that change during a ray tracing invocation (from SPIR-V + // 1.6 such reads need Volatile, as in dxc). + const changing = switch (builtin) { + 36, 41, 4416...4420 => e.entry.stage.isRayTracing(), + 5326 => e.entry.stage == .intersection, + else => false, + }; + if (changing and e.spv_minor >= 6) try e.b.emit(.annotations, .decorate, &.{ v, 21 }); // Volatile try e.useVar(v); try e.builtin_inputs.put(e.arena, builtin, v); return v;