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 <noreply@anthropic.com>
This commit is contained in:
parent
f572308b2b
commit
5ac6c4c066
|
|
@ -259,6 +259,7 @@ pub const StructDecl = struct {
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const CBufferDecl = struct {
|
pub const CBufferDecl = struct {
|
||||||
|
attrs: []const Attribute = &.{},
|
||||||
tbuffer: bool,
|
tbuffer: bool,
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
semantics: []const Semantic,
|
semantics: []const Semantic,
|
||||||
|
|
|
||||||
|
|
@ -715,7 +715,11 @@ pub const Parser = struct {
|
||||||
return p.parseStructDeclInto(out, attrs, lead_quals, template_params, ctx);
|
return p.parseStructDeclInto(out, attrs, lead_quals, template_params, ctx);
|
||||||
}
|
}
|
||||||
p.pos = save;
|
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("typedef")) return p.parseTypedefInto(out);
|
||||||
if (t.isIdent("using")) return out.append(p.arena, try p.parseUsing(template_params));
|
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());
|
if (t.isIdent("namespace")) return out.append(p.arena, try p.parseNamespace());
|
||||||
|
|
|
||||||
|
|
@ -216,6 +216,9 @@ pub const Global = struct {
|
||||||
/// A `tbuffer` block (space `cbuffer`): a texture buffer with the
|
/// A `tbuffer` block (space `cbuffer`): a texture buffer with the
|
||||||
/// constant buffer layout.
|
/// constant buffer layout.
|
||||||
tbuffer: bool = false,
|
tbuffer: bool = false,
|
||||||
|
/// `[[vk::shader_record_ext]]` / `[[vk::shader_record_nv]]` buffers
|
||||||
|
/// (SPIR-V ShaderRecordBufferKHR storage).
|
||||||
|
shader_record: bool = false,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Stage = enum {
|
pub const Stage = enum {
|
||||||
|
|
|
||||||
|
|
@ -233,7 +233,8 @@ const Lowerer = struct {
|
||||||
if (sem.kind != .register) continue;
|
if (sem.kind != .register) continue;
|
||||||
binding = parseBinding(sem.values);
|
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);
|
const i: u32 = @intCast(l.m.globals.items.len - 1);
|
||||||
try l.globals.put(l.arena, v, i);
|
try l.globals.put(l.arena, v, i);
|
||||||
return i;
|
return i;
|
||||||
|
|
@ -249,9 +250,11 @@ const Lowerer = struct {
|
||||||
var name: []const u8 = "$Globals";
|
var name: []const u8 = "$Globals";
|
||||||
var binding: ir.Binding = .{};
|
var binding: ir.Binding = .{};
|
||||||
var is_tbuffer = false;
|
var is_tbuffer = false;
|
||||||
|
var shader_record = false;
|
||||||
if (v.block) |b| {
|
if (v.block) |b| {
|
||||||
const cb = b.kind.cbuffer;
|
const cb = b.kind.cbuffer;
|
||||||
is_tbuffer = cb.tbuffer;
|
is_tbuffer = cb.tbuffer;
|
||||||
|
shader_record = isShaderRecord(cb.attrs);
|
||||||
name = cb.name;
|
name = cb.name;
|
||||||
for (cb.members) |m| if (m.kind == .@"var") {
|
for (cb.members) |m| if (m.kind == .@"var") {
|
||||||
if (l.s.decl_vars.get(&m.kind.@"var")) |mv| try members.append(l.arena, mv);
|
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 };
|
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 });
|
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 });
|
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);
|
const gi: u32 = @intCast(l.m.globals.items.len - 1);
|
||||||
try l.cbuffers.put(l.arena, key, gi);
|
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) });
|
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;
|
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.
|
/// RayQuery objects: stateful, they live in function storage.
|
||||||
pub fn isRayQuery(t: *TypeTable, ty: TypeId) bool {
|
pub fn isRayQuery(t: *TypeTable, ty: TypeId) bool {
|
||||||
return switch (t.get(ty)) {
|
return switch (t.get(ty)) {
|
||||||
|
|
|
||||||
|
|
@ -703,7 +703,8 @@ const Emitter = struct {
|
||||||
if (e.fn_calls.get(fid)) |cs| try stack.appendSlice(e.arena, cs.items);
|
if (e.fn_calls.get(fid)) |cs| try stack.appendSlice(e.arena, cs.items);
|
||||||
}
|
}
|
||||||
if (e.spv_minor >= 4) {
|
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, {});
|
for (e.counter_vars) |v| if (v != 0) try out.put(e.arena, v, {});
|
||||||
}
|
}
|
||||||
return out.keys();
|
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 }, {});
|
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;
|
var next_free: u32 = 0;
|
||||||
for (m.globals.items, 0..) |g, gi| {
|
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) {
|
switch (g.space) {
|
||||||
.resource => {
|
.resource => {
|
||||||
const var_id = e.b.newId();
|
const var_id = e.b.newId();
|
||||||
|
|
@ -1614,6 +1631,11 @@ const Emitter = struct {
|
||||||
}, i.ty),
|
}, i.ty),
|
||||||
.global => |g| {
|
.global => |g| {
|
||||||
e.vals[ri] = e.global_vars[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) {
|
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 },
|
.cbuffer => .{ .rules = if (e.m.globals.items[g].tbuffer) .std430 else .vk_relaxed, .row_major = false },
|
||||||
.resource => .{ .rules = .std430, .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);
|
try e.mesh_places.put(e.arena, ri, mp);
|
||||||
return;
|
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,
|
.function => .function,
|
||||||
.private => .private,
|
.private => .private,
|
||||||
.groupshared => if (e.payload_global != null and e.rootGlobal(a.base) == e.payload_global) .task_payload_workgroup else .workgroup,
|
.groupshared => if (e.payload_global != null and e.rootGlobal(a.base) == e.payload_global) .task_payload_workgroup else .workgroup,
|
||||||
.cbuffer, .resource => .uniform,
|
.cbuffer, .resource => .uniform,
|
||||||
else => return e.unsupported("access into this storage is not supported yet", .{}),
|
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;
|
var ops: std.ArrayList(u32) = .empty;
|
||||||
try ops.appendSlice(e.arena, &.{ 0, 0, e.val(a.base) });
|
try ops.appendSlice(e.arena, &.{ 0, 0, e.val(a.base) });
|
||||||
var cur = f.inst(a.base).ty;
|
var cur = f.inst(a.base).ty;
|
||||||
|
|
@ -3166,6 +3189,18 @@ const Emitter = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The global a resource value comes from (`load (global g)`).
|
/// 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 {
|
fn globalOf(e: *Emitter, r: Ref) ?u32 {
|
||||||
const i = e.f.inst(r);
|
const i = e.f.inst(r);
|
||||||
return switch (i.op) {
|
return switch (i.op) {
|
||||||
|
|
@ -3335,6 +3370,14 @@ const Emitter = struct {
|
||||||
while (e.t.get(et) == .array) et = e.t.get(et).array.elem;
|
while (e.t.get(et) == .array) et = e.t.get(et).array.elem;
|
||||||
const c = e.t.component(et).?;
|
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
|
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.useVar(v);
|
||||||
try e.builtin_inputs.put(e.arena, builtin, v);
|
try e.builtin_inputs.put(e.arena, builtin, v);
|
||||||
return v;
|
return v;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue