diff --git a/src/dxil/emit.zig b/src/dxil/emit.zig index 6d30ed5..eca8cee 100644 --- a/src/dxil/emit.zig +++ b/src/dxil/emit.zig @@ -131,6 +131,7 @@ const Emitter = struct { m: *const ir.Module, t: *TypeTable, profile: shader.Profile, + entry: ir.Entry = undefined, diags: *diag.Diagnostics, main_fn: u32 = 0, @@ -334,7 +335,9 @@ const Emitter = struct { fn run(e: *Emitter, gpa: Allocator) Error![]u8 { const m = e.m; - const entry = m.entry orelse return e.unsupported("no entry point", .{}); + if (m.entries.items.len != 1 or e.profile.kind == .library) return e.unsupported("library targets are not supported yet", .{}); + const entry = m.entries.items[0]; + e.entry = entry; switch (entry.stage) { .compute, .vertex, .pixel, .geometry, .hull, .domain, .mesh, .amplification => {}, else => return e.unsupported("{s} shaders are not supported yet", .{@tagName(entry.stage)}), @@ -552,12 +555,12 @@ const Emitter = struct { /// Derivatives (and implicit-LOD sampling): pixel shaders, and from SM /// 6.6 compute, mesh and amplification shaders. fn checkDerivatives(e: *Emitter) Error!void { - switch (e.m.entry.?.stage) { + switch (e.entry.stage) { .pixel => {}, .compute, .mesh, .amplification => if (e.profile.minor < 6) return e.unsupported("derivatives outside pixel shaders need shader model 6.6", .{}), - else => return e.unsupported("derivatives are not available in {s} shaders", .{@tagName(e.m.entry.?.stage)}), + else => return e.unsupported("derivatives are not available in {s} shaders", .{@tagName(e.entry.stage)}), } - if (e.m.entry.?.stage == .mesh or e.m.entry.?.stage == .amplification) e.uses_mesh_derivatives = true; + if (e.entry.stage == .mesh or e.entry.stage == .amplification) e.uses_mesh_derivatives = true; } /// The ResourceProperties constant annotating `res`'s handles. @@ -722,7 +725,7 @@ const Emitter = struct { }; @memset(frame.vals, .none); @memset(frame.blocks, null); - const is_entry = e.cur == 0 and e.blocks.items[0].items.len == 0 and (fi == e.m.entry.?.wrapper or fi == e.m.entry.?.patch_wrapper); + const is_entry = e.cur == 0 and e.blocks.items[0].items.len == 0 and (fi == e.entry.wrapper or fi == e.entry.patch_wrapper); if (!is_entry) frame.cont = try e.newBlock(); // The callee's entry continues the current block. frame.blocks[0] = e.cur; @@ -854,6 +857,7 @@ const Emitter = struct { switch (i.op) { .constant => |c| return e.list(&.{try e.constant(i.ty, c)}), .param => |p| return frame.params[p], + .ray_incoming => return e.unsupported("ray tracing shaders are not supported yet", .{}), .heap_resource => |h| { if (e.t.get(i.ty) != .resource and e.t.get(i.ty) != .sampler) return .none; if (e.profile.minor < 6) return e.unsupported("descriptor heaps need shader model 6.6", .{}); @@ -3181,7 +3185,7 @@ const Emitter = struct { } fn inputAt(e: *Emitter, k: u32, ty: TypeId, vertex: ?u32) Error!Lowered { - const entry = e.m.entry.?; + const entry = e.entry; if (entry.stage == .compute or entry.stage == .mesh or entry.stage == .amplification) return e.computeInput(k, ty); const el = entry.inputs.items[k]; const se = if (e.in_elem[k]) |ei| &e.sig_in[ei] else return e.systemValueInput(el.semantic, ty); @@ -3204,7 +3208,7 @@ const Emitter = struct { } fn output(e: *Emitter, frame: *Frame, k: u32, value: Ref) Error!void { - const el = e.m.entry.?.outputs.items[k]; + const el = e.entry.outputs.items[k]; const se = if (e.out_elem[k]) |ei| &e.sig_out[ei] else return e.unsupported("output '{s}' is not supported yet", .{el.semantic}); const vals = try e.scalars(frame, value); const i32t = try e.dm.intType(32); @@ -3231,7 +3235,7 @@ const Emitter = struct { /// A domain shader's patch constant (or a system value read with its /// own intrinsic). fn patchInput(e: *Emitter, k: u32, ty: TypeId) Error!Lowered { - const el = e.m.entry.?.patch_constants.items[k]; + const el = e.entry.patch_constants.items[k]; const se = if (e.pc_elem[k]) |ei| &e.sig_pc[ei] else return e.systemValueInput(el.semantic, ty); const i32t = try e.dm.intType(32); const i8t = try e.dm.intType(8); @@ -3251,7 +3255,7 @@ const Emitter = struct { /// A patch constant written by the hull shader's patch constant /// function. fn patchOutput(e: *Emitter, frame: *Frame, k: u32, value: Ref) Error!void { - const el = e.m.entry.?.patch_constants.items[k]; + const el = e.entry.patch_constants.items[k]; const se = if (e.pc_elem[k]) |ei| &e.sig_pc[ei] else return e.unsupported("patch constant '{s}' is not supported yet", .{el.semantic}); const vals = try e.scalars(frame, value); const i32t = try e.dm.intType(32); @@ -3270,7 +3274,7 @@ const Emitter = struct { /// An output control point of the hull shader, read by its patch /// constant function. fn outputControlPoint(e: *Emitter, k: u32, ty: TypeId, point: u32) Error!Lowered { - const el = e.m.entry.?.outputs.items[k]; + const el = e.entry.outputs.items[k]; const se = if (e.out_elem[k]) |ei| &e.sig_out[ei] else return e.unsupported("output '{s}' is not supported yet", .{el.semantic}); const i32t = try e.dm.intType(32); const i8t = try e.dm.intType(8); @@ -3462,7 +3466,7 @@ const Emitter = struct { _ = try e.callOp(fn_i, void_ty, &.{ try e.i32Const(169), index, vals[0], vals[1], v2 }); return; } - const entry = e.m.entry.?; + const entry = e.entry; const els = if (m.kind == .vertices) entry.outputs.items else entry.patch_constants.items; const map = if (m.kind == .vertices) e.out_elem else e.pc_elem; const sig = if (m.kind == .vertices) e.sig_out else e.sig_pc; @@ -3491,7 +3495,7 @@ const Emitter = struct { } fn computeInput(e: *Emitter, k: u32, ty: TypeId) Error!Lowered { - const el = e.m.entry.?.inputs.items[k]; + const el = e.entry.inputs.items[k]; const eqi = std.ascii.eqlIgnoreCase; const i32t = try e.dm.intType(32); const n = try e.flatCount(ty); @@ -3537,7 +3541,7 @@ const Emitter = struct { if (e.uses_typed_uav_load) flags |= 1 << 13; // Resources may not alias (DXIL 1.7+, when UAVs are used). if (uavs > 0 and e.profile.minor >= 7) flags |= 1 << 33; - const stage = e.m.entry.?.stage; + const stage = e.entry.stage; if (uavs > 0 and (stage == .vertex or stage == .geometry or stage == .hull or stage == .domain)) flags |= 1 << 16; for (e.sig_out) |el| switch (el.kind) { .render_target_array_index, .viewport_array_index => if (stage == .vertex) { diff --git a/src/front/preproc.zig b/src/front/preproc.zig index e3d6aed..210f86f 100644 --- a/src/front/preproc.zig +++ b/src/front/preproc.zig @@ -27,6 +27,8 @@ pub const Options = struct { major: u32 = 6, minor: u32 = 0, spirv: bool = false, + /// SPIR-V 1.x of the target environment. + spirv_minor: u32 = 0, enable_16bit: bool = false, }; @@ -241,7 +243,7 @@ pub const Preprocessor = struct { ); try w.print("#define __SHADER_TARGET_STAGE {d}\n#define __SHADER_TARGET_MAJOR {d}\n#define __SHADER_TARGET_MINOR {d}\n", .{ o.stage, o.major, o.minor }); if (o.enable_16bit) try w.writeAll("#define __HLSL_ENABLE_16_BIT 1\n"); - if (o.spirv) try w.writeAll("#define __spirv__ 1\n#define __SPIRV_MAJOR_VERSION__ 1\n#define __SPIRV_MINOR_VERSION__ 0\n"); + if (o.spirv) try w.print("#define __spirv__ 1\n#define __SPIRV_MAJOR_VERSION__ 1\n#define __SPIRV_MINOR_VERSION__ {d}\n", .{o.spirv_minor}); for (o.defines) |d| try w.print("#define {s} {s}\n", .{ d.name, d.value orelse "1" }); const id = try pp.sm.addFile("", text.written()); diff --git a/src/ir/ir.zig b/src/ir/ir.zig index ab36daf..a4cba46 100644 --- a/src/ir/ir.zig +++ b/src/ir/ir.zig @@ -38,7 +38,11 @@ pub const BlockId = enum(u32) { }; /// Where a pointer points. -pub const Space = enum { none, function, private, groupshared, resource, input, output, cbuffer, mesh }; +pub const Space = enum { none, function, private, groupshared, resource, input, output, cbuffer, mesh, ray }; + +/// Ray tracing shader storage an entry receives: the incoming payload, +/// the hit attributes and the incoming callable data. +pub const RayIncoming = enum { payload, attributes, callable }; pub const BinaryOp = enum { add, @@ -122,6 +126,9 @@ pub const Op = union(enum) { /// `ResourceDescriptorHeap[index]` / `SamplerDescriptorHeap[index]` /// as a resource of the instruction's type. heap_resource: struct { index: Ref, sampler: bool }, + /// Ray tracing entries: pointer (`ray` space) to the storage of an + /// incoming value of the instruction's type. + ray_incoming: RayIncoming, patch_output: struct { element: u32, value: Ref }, output_vertex: struct { element: u32, vertex: u32 }, /// Geometry shaders: emit the vertex written to the outputs of a stream, @@ -178,6 +185,9 @@ pub const Function = struct { name: []const u8, params: []const Param, ret: TypeId, + /// The entry (index into `Module.entries`) the function was lowered + /// for first. + entry: u32 = 0, insts: std.ArrayList(Inst) = .empty, blocks: std.ArrayList(Block) = .empty, @@ -208,7 +218,27 @@ pub const Global = struct { tbuffer: bool = false, }; -pub const Stage = enum { vertex, pixel, geometry, hull, domain, compute, mesh, amplification, library }; +pub const Stage = enum { + vertex, + pixel, + geometry, + hull, + domain, + compute, + mesh, + amplification, + library, + raygeneration, + intersection, + anyhit, + closesthit, + miss, + callable, + + pub fn isRayTracing(s: Stage) bool { + return @backingInt(s) >= @backingInt(Stage.raygeneration); + } +}; /// One entry-point input or output (a semantic on a parameter, field or /// the return value). @@ -294,7 +324,10 @@ pub const Module = struct { globals: std.ArrayList(Global) = .empty, /// By pointer: lowering appends callees while building a caller. functions: std.ArrayList(*Function) = .empty, - entry: ?Entry = null, + /// Entry points (one, or a library's shaders in declaration order). + entries: std.ArrayList(Entry) = .empty, + /// Libraries: functions exported by name (`export`). + exports: std.ArrayList(u32) = .empty, pub fn addFunction(m: *Module, f: Function) !u32 { const p = try m.arena.create(Function); @@ -448,6 +481,7 @@ fn dumpInst(m: *const Module, f: *const Function, r: Ref, w: *std.Io.Writer) !vo .patch_input => |k| try w.print("patch input {d}", .{k}), .mesh_output => |k| try w.print("mesh output {s}", .{@tagName(k)}), .mesh_payload => try w.print("mesh payload", .{}), + .ray_incoming => |k| try w.print("ray incoming {s}", .{@tagName(k)}), .heap_resource => |h| try w.print("{s}[%{d}]", .{ if (h.sampler) "SamplerDescriptorHeap" else "ResourceDescriptorHeap", h.index.index() }), .patch_output => |x| try w.print("patch output {d} = %{d}", .{ x.element, x.value.index() }), .output_vertex => |x| try w.print("output {d} vertex {d}", .{ x.element, x.vertex }), diff --git a/src/ir/lower.zig b/src/ir/lower.zig index aba9cf0..2954ca9 100644 --- a/src/ir/lower.zig +++ b/src/ir/lower.zig @@ -33,19 +33,56 @@ pub const Error = Allocator.Error || error{Unsupported}; /// not handle yet fail with `error.Unsupported` after reporting why. pub fn lowerUnit(arena: Allocator, s: *Sema, opts: Options) Error!ir.Module { var l: Lowerer = .{ .arena = arena, .s = s, .m = .{ .arena = arena, .types = &s.types } }; + if (opts.stage == .library) { + // Every function with a shader attribute, in declaration order. + for (s.all_functions.items) |f| { + if (f.owner != null or !f.defined) continue; + const stage = for (f.decl.attrs) |a| { + if (!std.ascii.eqlIgnoreCase(a.name, "shader") or a.args.len != 1 or a.args[0].kind != .string_lit) continue; + const lit = a.args[0].kind.string_lit; + break stageNamed(lit[1 .. lit.len - 1]) orelse { + l.unsupported(a.loc, "{s} shaders are not supported yet", .{lit}); + return error.Unsupported; + }; + } else continue; + try l.lowerEntry(f, f.name, stage); + } + 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; + const fi = try l.functionIndex(f); + try l.drain(); + try l.m.exports.append(l.arena, fi); + } + if (l.m.entries.items.len == 0 and l.m.exports.items.len == 0) { + l.unsupported(.none, "libraries without shader entry points or exported functions", .{}); + return error.Unsupported; + } + return l.m; + } const entry_fn = for (s.all_functions.items) |f| { if (f.owner == null and f.defined and std.mem.eql(u8, f.name, opts.entry)) break f; } else { l.unsupported(.none, "missing entry point definition", .{}); return error.Unsupported; }; - if (opts.stage == .geometry) try l.registerStreams(entry_fn); - const main = try l.functionIndex(entry_fn); - try l.drain(); - try l.buildEntry(entry_fn, main, opts); + try l.lowerEntry(entry_fn, opts.entry, opts.stage); return l.m; } +/// 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 }, + }; + for (names) |t| if (std.ascii.eqlIgnoreCase(name, t[0])) return t[1]; + return null; +} + const Stream = struct { param: u32, ty: TypeId, first: u32, kind: @FieldType(types.Type, "stream") }; const Lowerer = struct { @@ -79,6 +116,24 @@ const Lowerer = struct { /// The current method's object pointer. this_ptr: Ref = .none, ret_ty: TypeId = TypeTable.void_type, + /// The entry being lowered (index into the module's entries). + cur_entry: u32 = 0, + /// Initializer functions of static globals (made once). + inits: std.AutoHashMapUnmanaged(*const Variable, u32) = .empty, + + /// Lowers one entry point and what it reaches (functions shared with + /// earlier entries are already lowered). + fn lowerEntry(l: *Lowerer, f: *const semamod.Function, name: []const u8, stage: ir.Stage) Error!void { + l.cur_entry = @intCast(l.m.entries.items.len); + l.early_outputs = .empty; + l.streams = .empty; + l.entry_fn = null; + l.mesh_payload = null; + if (stage == .geometry) try l.registerStreams(f); + const main = try l.functionIndex(f); + try l.drain(); + try l.buildEntry(f, main, .{ .entry = name, .stage = stage }); + } 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); @@ -102,7 +157,7 @@ const Lowerer = struct { if (l.s.types.get(p.type) == .stream) continue; try params.append(l.arena, .{ .name = p.name, .ty = l.irType(p.type), .by_ref = p.dir != .in }); } - const i = try l.m.addFunction(.{ .name = f.name, .params = params.items, .ret = f.ret }); + const i = try l.m.addFunction(.{ .name = f.name, .params = params.items, .ret = f.ret, .entry = l.cur_entry }); try l.funcs.put(l.arena, f, i); try l.pending.append(l.arena, f); return i; @@ -400,7 +455,7 @@ const Lowerer = struct { try l.vars.put(l.arena, variable, try l.b.add(.{ .op = .{ .global = gi }, .ty = glob.ty, .ptr = glob.space })); return; } - if (v.init == null and isObject(&l.s.types, variable.type)) { + if (v.init == null and isObject(&l.s.types, variable.type) and !isRayQuery(&l.s.types, variable.type)) { l.unsupported(d.loc, "uninitialized local resource '{s}' is not supported yet", .{variable.name}); return error.Unsupported; } @@ -878,6 +933,10 @@ const Lowerer = struct { fn construct(l: *Lowerer, e: *ast.Expr, ty: TypeId, args: []const *ast.Expr) Error!Ref { const t = l.types(); + if (args.len == 0 and isRayQuery(t, ty)) { + // A new query object. + return l.b.load(try l.b.add(.{ .op = .local_var, .ty = ty, .ptr = .function })); + } var comps: std.ArrayList(Ref) = .empty; for (args) |a| try l.flatten(try l.rvalue(a), &comps); if (comps.items.len == 0) return l.fail(e, "this constructor"); @@ -1473,7 +1532,7 @@ const Lowerer = struct { /// each into its own function (an initializer may use earlier statics, /// which then need initializing too). Returns them by global. fn staticInits(l: *Lowerer) Error!std.AutoHashMapUnmanaged(*const Variable, u32) { - var inits: std.AutoHashMapUnmanaged(*const Variable, u32) = .empty; + const inits = &l.inits; var changed = true; while (changed) { changed = false; @@ -1481,7 +1540,7 @@ const Lowerer = struct { const init_e = v.init orelse continue; if (!l.globals.contains(v) or inits.contains(v)) continue; if (isObject(&l.s.types, v.type)) continue; - const fi = try l.m.addFunction(.{ .name = try std.fmt.allocPrint(l.arena, "init.{s}", .{v.name}), .params = &.{}, .ret = v.type }); + const fi = try l.m.addFunction(.{ .name = try std.fmt.allocPrint(l.arena, "init.{s}", .{v.name}), .params = &.{}, .ret = v.type, .entry = l.cur_entry }); try inits.put(l.arena, v, fi); l.b = .{ .m = &l.m, .f = l.m.functions.items[fi] }; l.vars = .empty; @@ -1494,7 +1553,7 @@ const Lowerer = struct { changed = true; } } - return inits; + return inits.*; } /// One leaf at a time of a signature value of type `ty`: registers @@ -1722,7 +1781,7 @@ const Lowerer = struct { }; const pci = try l.functionIndex(pcf); try l.drain(); - const wrapper = try l.m.addFunction(.{ .name = try std.fmt.allocPrint(l.arena, "{s}.wrapper", .{name}), .params = &.{}, .ret = TypeTable.void_type }); + const wrapper = try l.m.addFunction(.{ .name = try std.fmt.allocPrint(l.arena, "{s}.wrapper", .{name}), .params = &.{}, .ret = TypeTable.void_type, .entry = l.cur_entry }); l.b = .{ .m = &l.m, .f = l.m.functions.items[wrapper] }; l.b.setBlock(try l.b.newBlock()); const args = try l.arena.alloc(Ref, pcf.params.len); @@ -1773,7 +1832,7 @@ const Lowerer = struct { .line => .line, .triangle => .triangle, }; - const wrapper = try l.m.addFunction(.{ .name = opts.entry, .params = &.{}, .ret = TypeTable.void_type }); + const wrapper = try l.m.addFunction(.{ .name = opts.entry, .params = &.{}, .ret = TypeTable.void_type, .entry = l.cur_entry }); l.b = .{ .m = &l.m, .f = l.m.functions.items[wrapper] }; l.b.setBlock(try l.b.newBlock()); for (l.s.static_globals.items) |v| if (inits.get(v)) |fi| { @@ -1784,6 +1843,8 @@ const Lowerer = struct { }; const args = try l.arena.alloc(Ref, f.params.len); const locals = try l.arena.alloc(Ref, f.params.len); + const ray_srcs = try l.arena.alloc(Ref, f.params.len); + @memset(ray_srcs, .none); var n_args: usize = 0; if (opts.stage == .mesh) for (f.decl.attrs) |a| { if (std.ascii.eqlIgnoreCase(a.name, "outputtopology") and a.args.len == 1 and a.args[0].kind == .string_lit) { @@ -1861,6 +1922,15 @@ const Lowerer = struct { args[i] = try l.b.load(locals[i]); continue; } + if (opts.stage.isRayTracing()) { + // Payload, hit attributes or callable data: copied in from + // (and inout ones back to) the shader's incoming storage. + const kind: ir.RayIncoming = if (opts.stage == .callable) .callable else if (i == 0) .payload else .attributes; + ray_srcs[i] = try l.b.add(.{ .op = .{ .ray_incoming = kind }, .ty = p.type, .ptr = .ray }); + try l.b.store(locals[i], try l.b.load(ray_srcs[i])); + args[i] = if (p.dir == .in) try l.b.load(locals[i]) else locals[i]; + continue; + } if (p.dir != .out) { const v = try l.entryInput(&entry, p.type, semanticOf(pd.semantics), paramAttrs(pd.quals), pd.name_loc, pd.name); try l.b.store(locals[i], v); @@ -1876,6 +1946,10 @@ const Lowerer = struct { if (p.dir == .in or l.s.types.get(p.type) == .stream) continue; if (l.b.f.inst(locals[i]).ptr == .mesh) continue; const v = try l.b.load(locals[i]); + if (ray_srcs[i] != .none) { + try l.b.store(ray_srcs[i], v); + continue; + } try l.entryOutput(&entry, p.type, semanticOf(pd.semantics), paramAttrs(pd.quals), v, pd.name_loc, pd.name); } l.b.terminate(.{ .ret = .none }); @@ -1892,7 +1966,7 @@ const Lowerer = struct { } // The HLSL entry keeps its name with a prefix (as dxc's SPIR-V). l.m.functions.items[main].name = try std.fmt.allocPrint(l.arena, "src.{s}", .{opts.entry}); - l.m.entry = entry; + try l.m.entries.append(l.arena, entry); } }; @@ -1911,6 +1985,14 @@ fn isMethod(f: *const semamod.Function) bool { return f.owner != null and !f.decl.quals.static; } +/// RayQuery objects: stateful, they live in function storage. +pub fn isRayQuery(t: *TypeTable, ty: TypeId) bool { + return switch (t.get(ty)) { + .resource => |r| r.kind == .other and std.mem.eql(u8, r.name, "RayQuery"), + else => false, + }; +} + /// Resources, samplers and arrays of them. pub fn isObject(t: *TypeTable, ty: TypeId) bool { return switch (t.get(ty)) { diff --git a/src/ir/opt.zig b/src/ir/opt.zig index d7203ab..9011c37 100644 --- a/src/ir/opt.zig +++ b/src/ir/opt.zig @@ -181,7 +181,7 @@ fn mapRefs(a: Allocator, xs: []const Ref, ctx: anytype) Allocator.Error![]const /// `op` with every operand passed through `ctx.map`. fn mapOp(a: Allocator, op: Op, ctx: anytype) Allocator.Error!Op { return switch (op) { - .constant, .param, .global, .local_var, .input, .input_vertex, .emit, .cut, .patch_input, .output_vertex, .mesh_output, .mesh_payload => op, + .constant, .param, .global, .local_var, .input, .input_vertex, .emit, .cut, .patch_input, .output_vertex, .mesh_output, .mesh_payload, .ray_incoming => op, .load => |p| .{ .load = ctx.map(p) }, .store => |x| .{ .store = .{ .ptr = ctx.map(x.ptr), .value = ctx.map(x.value) } }, .access => |x| .{ .access = .{ .base = ctx.map(x.base), .indices = try mapRefs(a, x.indices, ctx) } }, diff --git a/src/main.zig b/src/main.zig index 540d5aa..0af923b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -236,6 +236,7 @@ fn run(cli: Cli, arena: std.mem.Allocator, args: []const [:0]const u8, stdout: * .major = if (profile) |p| p.major else 6, .minor = if (profile) |p| p.minor else 0, .spirv = opts.spirv, + .spirv_minor = if (zdxc.spirv.emit.TargetEnv.parse(opts.target_env)) |t| t.minor else 0, .enable_16bit = opts.enable_16bit, }); var out: Io.Writer.Allocating = .init(arena); @@ -258,7 +259,7 @@ fn run(cli: Cli, arena: std.mem.Allocator, args: []const [:0]const u8, stdout: * .root = opts.entry, }); try sema.checkUnit(tu); - if (!opts.sema and !diags.hasErrors() and profile != null and !is_lib) { + if (!opts.sema and !diags.hasErrors() and profile != null) { const stage: zdxc.ir.ir.Stage = switch (profile.?.kind) { .vertex => .vertex, .pixel => .pixel, diff --git a/src/sema/sema.zig b/src/sema/sema.zig index bc4bfeb..beee73c 100644 --- a/src/sema/sema.zig +++ b/src/sema/sema.zig @@ -1063,6 +1063,11 @@ pub const Sema = struct { // Any other builtin object (RayQuery, node records, vk:: types...). if (isOtherBuiltinObject(n)) { var elem: TypeId = TypeTable.void_type; + if (eql(n, "RayQuery")) { + const flags: u32 = if (args.len > 0) @truncate(@as(u64, @bitCast(s.constArg(args[0]) orelse 0))) else 0; + const flags2: u32 = if (args.len > 1) @truncate(@as(u64, @bitCast(s.constArg(args[1]) orelse 0))) else 0; + return s.types.intern(.{ .resource = .{ .kind = .other, .elem = elem, .name = n, .flags = flags, .flags2 = flags2 } }); + } if (args.len > 0) elem = try s.typeArg(args[0]) orelse TypeTable.void_type; return s.types.intern(.{ .resource = .{ .kind = .other, .elem = elem, .name = n } }); } @@ -2270,7 +2275,10 @@ pub const Sema = struct { return .{ .ty = v.type, .lvalue = true, .read_only = v.read_only }; }, .functions => |set| return .{ .ty = TypeTable.error_type, .callee = .{ .functions = set } }, - .enumerator => |en| return rvalue(en.type), + .enumerator => |en| { + s.record(e, .{ .enumerator = en.value }); + return rvalue(en.type); + }, .type => |t| return .{ .ty = TypeTable.error_type, .callee = .{ .type = t } }, .namespace => { s.err(e.loc, "unexpected namespace name '{s}'", .{n.last()}); @@ -3555,7 +3563,7 @@ fn methodNamespace(t: types.Type) ?[]const u8 { .sampled_texture_cube => "VkSampledTextureCUBEMethods", .sampled_texture_cube_array => "VkSampledTextureCUBEArrayMethods", .constant_buffer, .texture_buffer, .raytracing_acceleration_structure => null, - .other => null, + .other => if (std.mem.eql(u8, r.name, "RayQuery")) "RayQueryMethods" else null, }, else => null, }; diff --git a/src/sema/types.zig b/src/sema/types.zig index ea6667c..6b9b832 100644 --- a/src/sema/types.zig +++ b/src/sema/types.zig @@ -415,7 +415,8 @@ pub const Type = union(enum) { @"struct": *StructInfo, @"enum": *EnumInfo, /// `elem` is the template argument (or the void type when absent). - resource: struct { kind: ResourceKind, elem: TypeId, name: []const u8 }, + /// `flags`: RayQuery's constant ray flags (template arguments). + resource: struct { kind: ResourceKind, elem: TypeId, name: []const u8, flags: u32 = 0, flags2: u32 = 0 }, sampler: struct { comparison: bool }, string, /// PointStream / LineStream / TriangleStream. @@ -467,7 +468,7 @@ pub const TypeTable = struct { .array => |a| .{ .tag = 4, .a = a.elem, .b = a.len }, .@"struct" => |s| .{ .tag = 5, .a = @intFromPtr(s), .b = 0 }, .@"enum" => |e| .{ .tag = 6, .a = @intFromPtr(e), .b = 0 }, - .resource => |r| .{ .tag = 7, .a = (@as(u64, @backingInt(r.kind)) << 32) | r.elem, .b = std.hash.Wyhash.hash(0, r.name) }, + .resource => |r| .{ .tag = 7, .a = (@as(u64, @backingInt(r.kind)) << 32) | r.elem, .b = std.hash.Wyhash.hash(r.flags | (@as(u64, r.flags2) << 32), r.name) }, .sampler => |s| .{ .tag = 8, .a = @intFromBool(s.comparison), .b = 0 }, .string => .{ .tag = 9, .a = 0, .b = 0 }, .stream => |s| .{ .tag = 10, .a = @backingInt(s.kind), .b = s.elem }, diff --git a/src/spirv/builder.zig b/src/spirv/builder.zig index de0b0ce..440cc75 100644 --- a/src/spirv/builder.zig +++ b/src/spirv/builder.zig @@ -180,6 +180,38 @@ pub const Op = enum(u16) { @"unreachable" = 255, group_non_uniform_elect = 333, emit_mesh_tasks_ext = 5294, + trace_ray_khr = 4445, + execute_callable_khr = 4446, + convert_u_to_acceleration_structure_khr = 4447, + ignore_intersection_khr = 4448, + terminate_ray_khr = 4449, + type_ray_query_khr = 4472, + ray_query_initialize_khr = 4473, + ray_query_terminate_khr = 4474, + ray_query_generate_intersection_khr = 4475, + ray_query_confirm_intersection_khr = 4476, + ray_query_proceed_khr = 4477, + ray_query_get_intersection_type_khr = 4479, + report_intersection_khr = 5334, + ray_query_get_intersection_triangle_vertex_positions_khr = 5340, + type_acceleration_structure_khr = 5341, + ray_query_get_ray_t_min_khr = 6016, + ray_query_get_ray_flags_khr = 6017, + ray_query_get_intersection_t_khr = 6018, + ray_query_get_intersection_instance_custom_index_khr = 6019, + ray_query_get_intersection_instance_id_khr = 6020, + ray_query_get_intersection_instance_shader_binding_table_record_offset_khr = 6021, + ray_query_get_intersection_geometry_index_khr = 6022, + ray_query_get_intersection_primitive_index_khr = 6023, + ray_query_get_intersection_barycentrics_khr = 6024, + ray_query_get_intersection_front_face_khr = 6025, + ray_query_get_intersection_candidate_aabb_opaque_khr = 6026, + ray_query_get_intersection_object_ray_direction_khr = 6027, + ray_query_get_intersection_object_ray_origin_khr = 6028, + ray_query_get_world_ray_direction_khr = 6029, + ray_query_get_world_ray_origin_khr = 6030, + ray_query_get_intersection_object_to_world_khr = 6031, + ray_query_get_intersection_world_to_object_khr = 6032, set_mesh_outputs_ext = 5295, group_non_uniform_all = 334, group_non_uniform_any = 335, diff --git a/src/spirv/emit.zig b/src/spirv/emit.zig index 59bd2a4..9ef5d7a 100644 --- a/src/spirv/emit.zig +++ b/src/spirv/emit.zig @@ -26,10 +26,11 @@ pub const Error = Allocator.Error || error{Unsupported}; /// Marks a binding that is a recipe (see Emitter.resourceBinding). const recipe_tag: u32 = 0x8000_0000; -const StorageClass = enum(u32) { uniform_constant = 0, input = 1, uniform = 2, output = 3, workgroup = 4, private = 6, function = 7, image = 11, storage_buffer = 12, task_payload_workgroup = 5402 }; +const StorageClass = enum(u32) { uniform_constant = 0, input = 1, uniform = 2, output = 3, workgroup = 4, private = 6, function = 7, image = 11, storage_buffer = 12, task_payload_workgroup = 5402, callable_data_khr = 5328, incoming_callable_data_khr = 5329, ray_payload_khr = 5338, hit_attribute_khr = 5339, incoming_ray_payload_khr = 5342, shader_record_buffer_khr = 5343 }; const Capability = enum(u32) { shader = 1, + linkage = 5, geometry = 2, tessellation = 3, float16 = 9, @@ -68,6 +69,12 @@ const Capability = enum(u32) { storage_image_write_without_format = 56, group_non_uniform = 61, mesh_shading_ext = 5283, + ray_tracing_khr = 4479, + ray_query_khr = 4472, + ray_traversal_primitive_culling_khr = 4478, + ray_tracing_position_fetch_khr = 5336, + ray_query_position_fetch_khr = 5391, + ray_tracing_opacity_micromap_ext = 5381, group_non_uniform_vote = 62, group_non_uniform_arithmetic = 63, group_non_uniform_ballot = 64, @@ -87,13 +94,15 @@ const Capability = enum(u32) { pub const TargetEnv = struct { /// SPIR-V minor version (1.x). minor: u8, + /// A universal (not Vulkan) environment: linkage is allowed. + universal: bool = false, pub fn parse(s: []const u8) ?TargetEnv { const table = [_]struct { []const u8, u8 }{ .{ "vulkan1.0", 0 }, .{ "vulkan1.1", 3 }, .{ "vulkan1.1spirv1.3", 3 }, .{ "vulkan1.1spirv1.4", 4 }, .{ "vulkan1.2", 5 }, .{ "vulkan1.3", 6 }, .{ "vulkan1.4", 6 }, .{ "universal1.5", 5 }, }; - for (table) |t| if (std.mem.eql(u8, s, t[0])) return .{ .minor = t[1] }; + for (table) |t| if (std.mem.eql(u8, s, t[0])) return .{ .minor = t[1], .universal = std.mem.startsWith(u8, s, "universal") }; return null; } }; @@ -101,7 +110,7 @@ pub const TargetEnv = struct { pub fn emit(gpa: Allocator, m: *const ir.Module, diags: *diag.Diagnostics, env: TargetEnv) Error![]u8 { var arena_state = std.heap.ArenaAllocator.init(gpa); defer arena_state.deinit(); - var e: Emitter = .{ .arena = arena_state.allocator(), .b = .init(gpa), .m = m, .t = m.types, .diags = diags, .spv_minor = env.minor }; + var e: Emitter = .{ .arena = arena_state.allocator(), .b = .init(gpa), .m = m, .t = m.types, .diags = diags, .spv_minor = env.minor, .universal = env.universal }; defer e.b.deinit(); try e.module(); return e.b.finish(0x00010000 | (@as(u32, env.minor) << 8)); @@ -115,6 +124,7 @@ const Emitter = struct { diags: *diag.Diagnostics, /// SPIR-V 1.x. spv_minor: u8 = 0, + universal: bool = false, value_types: std.AutoHashMapUnmanaged(TypeId, Id) = .empty, /// Explicitly laid out variants of arrays and structs (buffers). @@ -144,12 +154,12 @@ const Emitter = struct { /// on the heap's binding. heap_vars: std.AutoHashMapUnmanaged(struct { bool, Id }, Id) = .empty, heap_bindings: [2]u32 = .{ 0, 0 }, - /// Compute derivatives' execution mode (0: none). - derivative_mode: u32 = 0, /// Image types by (sampled type, dim, arrayed, ms, sampled, format). image_types: std.AutoHashMapUnmanaged([6]u32, Id) = .empty, sampled_image_types: std.AutoHashMapUnmanaged(Id, Id) = .empty, sampler_type: Id = 0, + accel_type: Id = 0, + ray_query_type: Id = 0, counter_vars: []Id = &.{}, builtin_inputs: std.AutoHashMapUnmanaged(u32, Id) = .empty, /// Per instruction (current function): texel places (`tex[i]`). @@ -165,22 +175,27 @@ const Emitter = struct { cur_bindings: []const Id = &.{}, /// Resource array elements bound to specialized parameters. recipes: std.ArrayList(struct { var_id: Id, ptr_ty: Id, indices: []const Id }) = .empty, - /// Entry inputs / outputs: interface variable and its value type. - inputs: []Interface = &.{}, - outputs: []Interface = &.{}, - patches: []Interface = &.{}, - /// Mesh shaders: the primitive indices builtin and the payload - /// variable (both made on first use); amplification shaders: the - /// groupshared variable sent by DispatchMesh. - indices_var: Id = 0, - payload_var: Id = 0, payload_global: ?u32 = null, /// Per instruction (current function): places in mesh output arrays. mesh_places: std.AutoHashMapUnmanaged(u32, MeshPlace) = .empty, /// Parameters receiving mesh output arrays: not real parameters (the /// outputs are module variables); by function and parameter index. mesh_params: std.AutoHashMapUnmanaged(struct { *const ir.Function, u32 }, ir.MeshOutput) = .empty, - interface_ids: std.ArrayList(Id) = .empty, + /// Per entry: its interface state; `st` and `entry` are the current + /// entry's (the one the function being emitted was lowered for). + states: []EntryState = &.{}, + st: *EntryState = undefined, + entry: *const ir.Entry = undefined, + /// Interface variables each function uses directly, and the functions + /// it calls (by function id), for the entries' interface lists. + fn_uses: std.AutoHashMapUnmanaged(Id, std.ArrayList(Id)) = .empty, + fn_calls: std.AutoHashMapUnmanaged(Id, std.ArrayList(Id)) = .empty, + cur_fn: Id = 0, + /// Ray tracing storage by (storage class, value type). + ray_vars: std.AutoHashMapUnmanaged(struct { StorageClass, TypeId }, Id) = .empty, + /// Next Location per ray tracing storage class (RayPayloadKHR, + /// CallableDataKHR). + ray_locations: std.AutoHashMapUnmanaged(StorageClass, u32) = .empty, // Per function. vals: []Id = &.{}, @@ -195,6 +210,21 @@ const Emitter = struct { elem_array: bool = false, base: u32 = 0, }; + const EntryState = struct { + /// Entry inputs / outputs: interface variable and its value type. + inputs: []Interface = &.{}, + outputs: []Interface = &.{}, + patches: []Interface = &.{}, + /// Interface variables made for the entry itself. + iface: std.ArrayList(Id) = .empty, + /// Compute derivatives' execution mode (0: none). + derivative_mode: u32 = 0, + /// Mesh shaders: the primitive indices builtin and the payload + /// variable (both made on first use); amplification shaders: the + /// groupshared variable sent by DispatchMesh. + indices_var: Id = 0, + payload_var: Id = 0, + }; const LayoutKey = struct { TypeId, layout.Rules, bool }; const MeshPlace = struct { kind: ir.MeshOutput, index: Ref = .none, offset: u32 = 0, ty: TypeId }; const PtrLayout = struct { rules: layout.Rules, row_major: bool }; @@ -564,37 +594,122 @@ const Emitter = struct { fn module(e: *Emitter) Error!void { const m = e.m; - const entry = m.entry orelse return e.unsupported("no entry point", .{}); + if (m.entries.items.len == 0 and m.exports.items.len == 0) return e.unsupported("no entry point", .{}); try e.b.emit(.debug_names, .source, &.{ 5, 600 }); // HLSL e.void_id = try e.typeId(TypeTable.void_type); try e.cap(.shader); - if (entry.stage == .geometry) try e.cap(.geometry); - if (entry.stage == .mesh or entry.stage == .amplification) { - if (e.spv_minor < 4) return e.unsupported("mesh and amplification shaders need SPIR-V 1.4 or later (-fspv-target-env=vulkan1.2 or later)", .{}); - try e.cap(.mesh_shading_ext); - try e.extension("SPV_EXT_mesh_shader"); - e.payload_global = e.dispatchPayloadGlobal(); - try e.findMeshParams(); + e.states = try e.arena.alloc(EntryState, @max(1, m.entries.items.len)); + for (e.states) |*st| st.* = .{}; + e.st = &e.states[0]; + if (m.entries.items.len > 0) { + e.entry = &m.entries.items[0]; + } else { + // Exports only: functions see no entry interface. + const none = try e.arena.create(ir.Entry); + none.* = .{ .func = 0, .name = "", .stage = .library }; + e.entry = none; + } + // Vulkan has no linkage: exports are plain functions there. + const link = m.exports.items.len > 0 and e.universal; + if (m.entries.items.len == 0 and !link) return e.unsupported("libraries without entry points need a universal target environment (-fspv-target-env=universal1.5)", .{}); + if (link) try e.cap(.linkage); + for (m.entries.items) |*entry| { + if (entry.stage == .geometry) try e.cap(.geometry); + if (entry.stage == .mesh or entry.stage == .amplification) { + if (e.spv_minor < 4) return e.unsupported("mesh and amplification shaders need SPIR-V 1.4 or later (-fspv-target-env=vulkan1.2 or later)", .{}); + if (m.entries.items.len > 1) return e.unsupported("mesh and amplification shaders in libraries are not supported yet", .{}); + try e.cap(.mesh_shading_ext); + try e.extension("SPV_EXT_mesh_shader"); + e.payload_global = e.dispatchPayloadGlobal(); + try e.findMeshParams(); + } + if (entry.stage.isRayTracing()) { + if (e.spv_minor < 4) return e.unsupported("ray tracing shaders need SPIR-V 1.4 or later (-fspv-target-env=vulkan1.2 or later)", .{}); + try e.cap(.ray_tracing_khr); + try e.extension("SPV_KHR_ray_tracing"); + } } e.func_ids = try e.arena.alloc(Id, m.functions.items.len); for (e.func_ids) |*id| id.* = e.b.newId(); + if (link) for (m.exports.items) |fi| { + if (e.hasResourceParams(m.functions.items[fi])) return e.unsupported("exported functions with resource parameters are not supported yet", .{}); + try e.b.emitWithString(.annotations, .decorate, &.{ e.func_ids[fi], 41 }, m.functions.items[fi].name, &.{0}); // LinkageAttributes Export + }; try e.emitGlobals(); - try e.emitInterface(entry); + for (m.entries.items, 0..) |entry, k| { + e.setEntry(@intCast(k)); + try e.emitInterface(entry); + } for (m.functions.items, e.func_ids) |f, id| { + e.setEntry(f.entry); if (!e.hasResourceParams(f)) try e.emitFunction(f, id, &.{}); } var vi: usize = 0; while (vi < e.variants.items.len) : (vi += 1) { const v = e.variants.items[vi]; + e.setEntry(m.functions.items[v.fi].entry); try e.emitFunction(m.functions.items[v.fi], v.id, v.bindings); } for (e.capabilities.keys()) |c| try e.b.emit(.capabilities, .capability, &.{@backingInt(c)}); if (e.glsl != 0) try e.b.emitWithString(.ext_inst_imports, .ext_inst_import, &.{e.glsl}, "GLSL.std.450", &.{}); try e.b.emit(.memory_model, .memory_model, &.{ 0, 1 }); // Logical GLSL450 + for (m.entries.items, 0..) |*entry, k| { + e.setEntry(@intCast(k)); + try e.entryPoint(entry); + } + for (m.functions.items, e.func_ids) |f, id| { + if (!e.hasResourceParams(f)) try e.b.emitWithString(.debug_names, .name, &.{id}, f.name, &.{}); + } + } + + fn recordCall(e: *Emitter, callee: Id) Error!void { + const gop = try e.fn_calls.getOrPut(e.arena, e.cur_fn); + if (!gop.found_existing) gop.value_ptr.* = .empty; + try gop.value_ptr.append(e.arena, callee); + } + + fn setEntry(e: *Emitter, k: u32) void { + e.st = &e.states[k]; + if (k < e.m.entries.items.len) e.entry = &e.m.entries.items[k]; + } + + /// Records that the current function (or, outside functions, the + /// current entry) uses interface variable `v`. + fn useVar(e: *Emitter, v: Id) Error!void { + const list = if (e.cur_fn == 0) &e.st.iface else blk: { + const gop = try e.fn_uses.getOrPut(e.arena, e.cur_fn); + if (!gop.found_existing) gop.value_ptr.* = .empty; + break :blk gop.value_ptr; + }; + for (list.items) |x| if (x == v) return; + try list.append(e.arena, v); + } + + /// The entry's interface: its own variables and those of every + /// function it reaches (from SPIR-V 1.4 every global it may use). + fn entryInterface(e: *Emitter, wrapper: Id) Error![]const Id { + var out: std.AutoArrayHashMapUnmanaged(Id, void) = .empty; + for (e.st.iface.items) |v| try out.put(e.arena, v, {}); + var seen: std.AutoHashMapUnmanaged(Id, void) = .empty; + var stack: std.ArrayList(Id) = .empty; + try stack.append(e.arena, wrapper); + while (stack.pop()) |fid| { + if ((try seen.getOrPut(e.arena, fid)).found_existing) continue; + if (e.fn_uses.get(fid)) |us| for (us.items) |v| try out.put(e.arena, v, {}); + 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, {}); + for (e.counter_vars) |v| if (v != 0) try out.put(e.arena, v, {}); + } + return out.keys(); + } + + fn entryPoint(e: *Emitter, entry: *const ir.Entry) Error!void { const model: u32 = switch (entry.stage) { .vertex => 0, .hull => 1, @@ -604,15 +719,16 @@ const Emitter = struct { .compute => 5, .amplification => 5364, // TaskEXT .mesh => 5365, // MeshEXT + .raygeneration => 5313, // RayGenerationKHR + .intersection => 5314, + .anyhit => 5315, + .closesthit => 5316, + .miss => 5317, + .callable => 5318, .library => return e.unsupported("library targets are not supported", .{}), }; const wrapper = e.func_ids[entry.wrapper]; - // From SPIR-V 1.4 the interface lists every global the entry uses. - if (e.spv_minor >= 4) { - for (e.global_vars) |v| if (v != 0) try e.interface_ids.append(e.arena, v); - for (e.counter_vars) |v| if (v != 0) try e.interface_ids.append(e.arena, v); - } - try e.b.emitWithString(.entry_points, .entry_point, &.{ model, wrapper }, entry.name, e.interface_ids.items); + try e.b.emitWithString(.entry_points, .entry_point, &.{ model, wrapper }, entry.name, try e.entryInterface(wrapper)); switch (entry.stage) { .compute => try e.b.emit(.execution_modes, .execution_mode, &.{ wrapper, 17, entry.num_threads[0], entry.num_threads[1], entry.num_threads[2] }), .geometry => { @@ -672,10 +788,7 @@ const Emitter = struct { }, else => {}, } - if (e.derivative_mode != 0) try e.b.emit(.execution_modes, .execution_mode, &.{ wrapper, e.derivative_mode }); - for (m.functions.items, e.func_ids) |f, id| { - if (!e.hasResourceParams(f)) try e.b.emitWithString(.debug_names, .name, &.{id}, f.name, &.{}); - } + if (e.st.derivative_mode != 0) try e.b.emit(.execution_modes, .execution_mode, &.{ wrapper, e.st.derivative_mode }); } fn domainMode(d: ir.Domain) u32 { @@ -1034,15 +1147,15 @@ const Emitter = struct { } fn emitInterface(e: *Emitter, entry: ir.Entry) Error!void { - e.inputs = try e.arena.alloc(Interface, entry.inputs.items.len); - e.outputs = try e.arena.alloc(Interface, entry.outputs.items.len); - e.patches = try e.arena.alloc(Interface, entry.patch_constants.items.len); + e.st.inputs = try e.arena.alloc(Interface, entry.inputs.items.len); + e.st.outputs = try e.arena.alloc(Interface, entry.outputs.items.len); + e.st.patches = try e.arena.alloc(Interface, entry.patch_constants.items.len); if (entry.stage == .hull or entry.stage == .domain) try e.cap(.tessellation); // Variables needing a Location, assigned once all exist. const Located = struct { var_id: Id, key: []const u8, count: u32, input: bool, target: ?u32 }; var located: std.ArrayList(Located) = .empty; const lists = [_][]const ir.SigElement{ entry.inputs.items, entry.outputs.items, entry.patch_constants.items }; - const ifaces = [_][]Interface{ e.inputs, e.outputs, e.patches }; + const ifaces = [_][]Interface{ e.st.inputs, e.st.outputs, e.st.patches }; for (lists, ifaces, 0..) |els, out, li| { const patch = li == 2; const input = if (patch) entry.stage == .domain else li == 0; @@ -1079,7 +1192,7 @@ const Emitter = struct { try e.b.emit(.types_globals, .variable, &.{ try e.pointerType(sc, try e.typeId(arr)), v, @backingInt(sc) }); try e.b.emit(.annotations, .decorate, &.{ v, 11, if (kind == 0) 3 else 4 }); // BuiltIn ClipDistance / CullDistance try e.b.emitWithString(.debug_names, .name, &.{v}, if (kind == 0) "gl_ClipDistance" else "gl_CullDistance", &.{}); - try e.interface_ids.append(e.arena, v); + try e.useVar(v); clip_vars[kind] = v; } for (els, out, 0..) |el, *iface, ek| { @@ -1143,7 +1256,7 @@ const Emitter = struct { const name = try std.fmt.allocPrint(e.arena, "{s}.var.{s}{d}", .{ if (input) "in" else "out", el.semantic, el.semantic_index }); try e.b.emitWithString(.debug_names, .name, &.{var_id}, name, &.{}); iface.* = .{ .var_id = var_id, .ty = vty, .elem_array = bi != null and e.t.get(bi.?.ty) == .array }; - try e.interface_ids.append(e.arena, var_id); + try e.useVar(var_id); } } // Locations in declaration order, except hull outputs and domain @@ -1250,6 +1363,8 @@ const Emitter = struct { fn emitFunction(e: *Emitter, f: *const ir.Function, fn_id: Id, bindings: []const Id) Error!void { e.f = f; + e.cur_fn = fn_id; + defer e.cur_fn = 0; e.cur_bindings = bindings; e.texel_places = .empty; e.mesh_places = .empty; @@ -1262,6 +1377,10 @@ const Emitter = struct { for (f.params, 0..) |p, k| { if (bindings.len > 0 and bindings[k] != 0) continue; if (e.mesh_params.contains(.{ f, @intCast(k) })) continue; + if (isRayQuery(e.t, p.ty)) { + try params.append(e.arena, try e.pointerType(.function, try e.rayQueryType())); + continue; + } const t = try e.typeId(p.ty); try params.append(e.arena, if (p.by_ref) try e.pointerType(.function, t) else t); } @@ -1304,6 +1423,13 @@ const Emitter = struct { if (oi == 0) { // OpVariables must open the first block. for (f.insts.items, 0..) |inst, i| if (inst.op == .local_var) { + if (isRayQuery(e.t, inst.ty)) { + try e.cap(.ray_query_khr); + try e.extension("SPV_KHR_ray_query"); + e.vals[i] = e.b.newId(); + try e.b.emit(.functions, .variable, &.{ try e.pointerType(.function, try e.rayQueryType()), e.vals[i], @backingInt(StorageClass.function) }); + continue; + } // Resource locals alias the one resource stored in them. if (isResourceType(e.t, inst.ty)) continue; const ptr = try e.pointerType(.function, try e.typeId(inst.ty)); @@ -1363,7 +1489,7 @@ const Emitter = struct { /// For the hull shader's entry wrapper: the patch constant wrapper it /// calls before returning. fn hullEpilogue(e: *Emitter, fn_id: Id) ?Id { - const entry = e.m.entry.?; + const entry = e.entry; if (entry.stage != .hull or fn_id != e.func_ids[entry.wrapper]) return null; return e.func_ids[entry.patch_wrapper orelse return null]; } @@ -1481,6 +1607,11 @@ const Emitter = struct { const ri = r.index(); switch (i.op) { .constant, .param, .local_var => {}, + .ray_incoming => |k| e.vals[ri] = try e.rayVar(switch (k) { + .payload => .incoming_ray_payload_khr, + .attributes => .hit_attribute_khr, + .callable => .incoming_callable_data_khr, + }, i.ty), .global => |g| { e.vals[ri] = e.global_vars[g]; e.ptr_layouts[ri] = switch (e.m.globals.items[g].space) { @@ -1519,6 +1650,11 @@ const Emitter = struct { return e.imageWrite(tp.res, e.val(tp.coord), e.val(s.value)); } const target = f.inst(s.ptr); + if (target.op == .local_var and isRayQuery(e.t, target.ty)) { + // A copy of a RayQuery is the same query object. + e.vals[s.ptr.index()] = e.val(s.value); + return; + } if (target.op == .local_var and isResourceType(e.t, target.ty)) { const old = e.vals[s.ptr.index()]; if (old != 0 and old != e.val(s.value)) return e.unsupported("assigning different resources to one variable is not supported yet", .{}); @@ -1814,11 +1950,14 @@ const Emitter = struct { if (e.hasResourceParams(callee)) { const bindings = try e.arena.alloc(Id, c.args.len); for (callee.params, c.args, bindings) |p, a, *bd| bd.* = if (isResourceType(e.t, p.ty)) try e.resourceBinding(a) else 0; - try ops.append(e.arena, try e.variantFor(c.func, bindings)); + const vid = try e.variantFor(c.func, bindings); + try e.recordCall(vid); + try ops.append(e.arena, vid); for (callee.params, c.args) |p, a| if (!isResourceType(e.t, p.ty)) try ops.append(e.arena, e.val(a)); e.vals[ri] = try e.op1(.function_call, i.ty, ops.items); return; } + try e.recordCall(e.func_ids[c.func]); try ops.append(e.arena, e.func_ids[c.func]); for (c.args, 0..) |a, k| { if (e.mesh_params.contains(.{ callee, @intCast(k) })) continue; @@ -1838,6 +1977,10 @@ const Emitter = struct { try e.b.emit(.functions, .label, &.{e.b.newId()}); return; } + if (try e.rayIntrinsic(c.name, i.ty, c.args)) |id| { + e.vals[ri] = id; + return; + } e.vals[ri] = try e.intrinsic(c.name, i.ty, c.args); }, .res_load => |x| { @@ -1859,13 +2002,13 @@ const Emitter = struct { try e.b.emit(.functions, .store, &.{ ptr, v }); }, .method => |x| e.vals[ri] = try e.method(x.name, x.res, x.args, i.ty), - .input => |k| e.vals[ri] = try e.loadInterface(e.inputs[k], .input, i.ty), + .input => |k| e.vals[ri] = try e.loadInterface(e.st.inputs[k], .input, i.ty), .input_vertex => |x| { - const iface = e.inputs[x.element]; + const iface = e.st.inputs[x.element]; const ptr = try e.opRaw(.access_chain, try e.pointerType(.input, try e.typeId(iface.ty)), &.{ iface.var_id, try e.constUint(x.vertex) }); e.vals[ri] = try e.reshape(try e.op1(.load, iface.ty, &.{ptr}), iface.ty, i.ty); }, - .patch_input => |k| e.vals[ri] = try e.loadInterface(e.patches[k], .input, i.ty), + .patch_input => |k| e.vals[ri] = try e.loadInterface(e.st.patches[k], .input, i.ty), .mesh_output => try e.mesh_places.put(e.arena, ri, .{ .kind = i.op.mesh_output, .ty = i.ty }), .heap_resource => |h| { if (e.t.get(i.ty) != .resource and e.t.get(i.ty) != .sampler) return; @@ -1881,7 +2024,7 @@ const Emitter = struct { try e.b.emit(.annotations, .decorate, &.{ v, 34, 0 }); // DescriptorSet try e.b.emit(.annotations, .decorate, &.{ v, 33, e.heap_bindings[@intFromBool(h.sampler)] }); // Binding try e.b.emitWithString(.debug_names, .name, &.{v}, if (h.sampler) "SamplerDescriptorHeap" else "ResourceDescriptorHeap", &.{}); - if (e.spv_minor >= 4) try e.interface_ids.append(e.arena, v); + if (e.spv_minor >= 4) try e.useVar(v); try e.heap_vars.put(e.arena, key, v); break :blk v; }; @@ -1890,31 +2033,31 @@ const Emitter = struct { if (e.nonuniform_ids.contains(e.val(h.index))) try e.markNonUniform(id, i.ty); }, .mesh_payload => { - if (e.payload_var == 0) { - e.payload_var = e.b.newId(); - try e.b.emit(.types_globals, .variable, &.{ try e.pointerType(.task_payload_workgroup, try e.typeId(i.ty)), e.payload_var, @backingInt(StorageClass.task_payload_workgroup) }); - try e.b.emitWithString(.debug_names, .name, &.{e.payload_var}, "in.var.payload", &.{}); - try e.interface_ids.append(e.arena, e.payload_var); + if (e.st.payload_var == 0) { + e.st.payload_var = e.b.newId(); + try e.b.emit(.types_globals, .variable, &.{ try e.pointerType(.task_payload_workgroup, try e.typeId(i.ty)), e.st.payload_var, @backingInt(StorageClass.task_payload_workgroup) }); + try e.b.emitWithString(.debug_names, .name, &.{e.st.payload_var}, "in.var.payload", &.{}); + try e.useVar(e.st.payload_var); } - e.vals[ri] = try e.op1(.load, i.ty, &.{e.payload_var}); + e.vals[ri] = try e.op1(.load, i.ty, &.{e.st.payload_var}); }, - .patch_output => |o| try e.storeInterface(e.patches[o.element], f.inst(o.value).ty, e.val(o.value)), + .patch_output => |o| try e.storeInterface(e.st.patches[o.element], f.inst(o.value).ty, e.val(o.value)), .output_vertex => |x| { - const iface = e.outputs[x.element]; + const iface = e.st.outputs[x.element]; const ptr = try e.opRaw(.access_chain, try e.pointerType(.output, try e.typeId(iface.ty)), &.{ iface.var_id, try e.constUint(x.vertex) }); e.vals[ri] = try e.reshape(try e.op1(.load, iface.ty, &.{ptr}), iface.ty, i.ty); }, .emit, .cut => |stream| { - const multi = e.m.entry.?.geometry.streams[1] != null; + const multi = e.entry.geometry.streams[1] != null; if (multi) { try e.b.emit(.functions, if (i.op == .emit) .emit_stream_vertex else .end_stream_primitive, &.{try e.constUint(stream)}); } else try e.b.emit(.functions, if (i.op == .emit) .emit_vertex else .end_primitive, &.{}); }, .output => |o| { - const iface = e.outputs[o.element]; + const iface = e.st.outputs[o.element]; if (iface.elem_array) return e.storeInterface(iface, f.inst(o.value).ty, e.val(o.value)); const v = try e.reshape(e.val(o.value), f.inst(o.value).ty, iface.ty); - if (e.m.entry.?.outputs.items[o.element].per_vertex) { + if (e.entry.outputs.items[o.element].per_vertex) { // Hull output control points: this invocation's element. const id = try e.op1(.load, try e.t.scalar(.uint), &.{try e.builtinInput(8, try e.t.scalar(.uint))}); const ptr = try e.opRaw(.access_chain, try e.pointerType(.output, try e.typeId(iface.ty)), &.{ iface.var_id, id }); @@ -2006,33 +2149,33 @@ const Emitter = struct { /// each attribute's array) or a primitive's indices. fn meshStore(e: *Emitter, mp: MeshPlace, v: Id, ty: TypeId) Error!void { if (mp.index == .none) return e.unsupported("storing a whole mesh output array is not supported yet", .{}); - const entry = e.m.entry.?; + const entry = e.entry; const index = e.val(mp.index); if (mp.kind == .indices) { const lines = entry.mesh.topology == .line; const vty = try e.t.vector(.uint, if (lines) 2 else 3); - if (e.indices_var == 0) { - e.indices_var = e.b.newId(); - try e.b.emit(.types_globals, .variable, &.{ try e.pointerType(.output, try e.typeId(try e.t.array(vty, entry.mesh.max_primitives))), e.indices_var, @backingInt(StorageClass.output) }); - try e.b.emit(.annotations, .decorate, &.{ e.indices_var, 11, if (lines) 5295 else 5296 }); // BuiltIn Primitive{Line,Triangle}IndicesEXT - try e.interface_ids.append(e.arena, e.indices_var); + if (e.st.indices_var == 0) { + e.st.indices_var = e.b.newId(); + try e.b.emit(.types_globals, .variable, &.{ try e.pointerType(.output, try e.typeId(try e.t.array(vty, entry.mesh.max_primitives))), e.st.indices_var, @backingInt(StorageClass.output) }); + try e.b.emit(.annotations, .decorate, &.{ e.st.indices_var, 11, if (lines) 5295 else 5296 }); // BuiltIn Primitive{Line,Triangle}IndicesEXT + try e.useVar(e.st.indices_var); } if (mp.offset == 0 and e.t.componentCount(ty) == e.t.componentCount(vty)) { - const ptr = try e.opRaw(.access_chain, try e.pointerType(.output, try e.typeId(vty)), &.{ e.indices_var, index }); + const ptr = try e.opRaw(.access_chain, try e.pointerType(.output, try e.typeId(vty)), &.{ e.st.indices_var, index }); try e.b.emit(.functions, .store, &.{ ptr, try e.reshape(v, ty, vty) }); return; } const ut = try e.t.scalar(.uint); for (0..e.t.componentCount(ty).?) |c| { const comp = try e.flatComponent(v, ty, @intCast(c)); - const ptr = try e.opRaw(.access_chain, try e.pointerType(.output, try e.typeId(ut)), &.{ e.indices_var, index, try e.constUint(mp.offset + @as(u32, @intCast(c))) }); + const ptr = try e.opRaw(.access_chain, try e.pointerType(.output, try e.typeId(ut)), &.{ e.st.indices_var, index, try e.constUint(mp.offset + @as(u32, @intCast(c))) }); try e.b.emit(.functions, .store, &.{ ptr, try e.reshape(comp[0], comp[1], ut) }); } return; } const n = e.t.componentCount(ty) orelse return e.unsupported("mesh outputs of type '{s}' are not supported yet", .{try e.t.format(e.arena, ty)}); const els = if (mp.kind == .vertices) entry.outputs.items else entry.patch_constants.items; - const ifaces = if (mp.kind == .vertices) e.outputs else e.patches; + const ifaces = if (mp.kind == .vertices) e.st.outputs else e.st.patches; for (0..n) |c| { const comp = try e.flatComponent(v, ty, @intCast(c)); var flat = mp.offset + @as(u32, @intCast(c)); @@ -2073,21 +2216,21 @@ const Emitter = struct { /// Derivatives outside pixel shaders: compute-like stages, over quads /// of the thread group (or linearly, as dxc). fn checkDerivatives(e: *Emitter) Error!void { - const entry = e.m.entry.?; + const entry = e.entry; switch (entry.stage) { .pixel => return, .compute, .mesh, .amplification => {}, else => return e.unsupported("derivatives are not available in {s} shaders", .{@tagName(entry.stage)}), } - if (e.derivative_mode != 0) return; + if (e.st.derivative_mode != 0) return; try e.extension("SPV_KHR_compute_shader_derivatives"); const t = entry.num_threads; if (t[0] % 2 == 0 and t[1] % 2 == 0) { try e.cap(.compute_derivative_group_quads); - e.derivative_mode = 5289; // DerivativeGroupQuadsKHR + e.st.derivative_mode = 5289; // DerivativeGroupQuadsKHR } else { try e.cap(.compute_derivative_group_linear); - e.derivative_mode = 5290; // DerivativeGroupLinearKHR + e.st.derivative_mode = 5290; // DerivativeGroupLinearKHR } } @@ -2216,6 +2359,7 @@ const Emitter = struct { switch (e.t.get(ty)) { .sampler => return .{ .sc = .uniform_constant, .ty = try e.samplerType() }, .resource => |r| switch (r.kind) { + .raytracing_acceleration_structure => return .{ .sc = .uniform_constant, .ty = try e.accelType() }, .structured_buffer, .rw_structured_buffer, .append_structured_buffer, .consume_structured_buffer, .byte_address_buffer, .rw_byte_address_buffer => return .{ .sc = e.bufferSc(), .ty = try e.bufferBlockType(ty) }, .constant_buffer => if (e.t.get(r.elem) == .@"struct") return .{ .sc = .uniform, .ty = try e.constantBufferBlock(r.elem) }, .texture_buffer => if (e.t.get(r.elem) == .@"struct") return .{ .sc = e.bufferSc(), .ty = try e.textureBufferBlock(r.elem) }, @@ -2293,6 +2437,165 @@ const Emitter = struct { // ------------------------------------------------------------------ // Images and samplers + fn accelType(e: *Emitter) Error!Id { + if (e.accel_type == 0) { + e.accel_type = e.b.newId(); + try e.b.emit(.types_globals, .type_acceleration_structure_khr, &.{e.accel_type}); + } + return e.accel_type; + } + + /// The acceleration structure a resource value designates. + fn loadAccel(e: *Emitter, r: Ref) Error!Id { + return e.opRaw(.load, try e.accelType(), &.{e.val(r)}); + } + + /// RayDesc's members: Origin, TMin, Direction, TMax. + fn rayDesc(e: *Emitter, r: Ref) Error![4]Id { + const f3 = try e.t.vector(.float, 3); + const f1 = try e.t.scalar(.float); + const v = e.val(r); + return .{ + try e.op1(.composite_extract, f3, &.{ v, 0 }), try e.op1(.composite_extract, f1, &.{ v, 1 }), + try e.op1(.composite_extract, f3, &.{ v, 2 }), try e.op1(.composite_extract, f1, &.{ v, 3 }), + }; + } + + /// Ray tracing intrinsics (SPV_KHR_ray_tracing): TraceRay and friends, + /// and the system values as builtins. Null for other names. + fn rayIntrinsic(e: *Emitter, name: []const u8, ty: TypeId, args: []const Ref) Error!?Id { + const eq = std.mem.eql; + if (eq(u8, name, "TraceRay") or eq(u8, name, "CallShader")) { + // Through the payload / callable data variable, copied in and + // back out. + const trace = eq(u8, name, "TraceRay"); + const data = args[args.len - 1]; + const dty = e.f.inst(data).ty; + const v = try e.rayVar(if (trace) .ray_payload_khr else .callable_data_khr, dty); + try e.b.emit(.functions, .store, &.{ v, try e.op1(.load, dty, &.{e.val(data)}) }); + if (trace) { + const d = try e.rayDesc(args[6]); + try e.b.emit(.functions, .trace_ray_khr, &.{ try e.loadAccel(args[0]), e.val(args[1]), e.val(args[2]), e.val(args[3]), e.val(args[4]), e.val(args[5]), d[0], d[1], d[2], d[3], v }); + } else try e.b.emit(.functions, .execute_callable_khr, &.{ e.val(args[0]), v }); + try e.b.emit(.functions, .store, &.{ e.val(data), try e.op1(.load, dty, &.{v}) }); + return 0; + } + if (eq(u8, name, "ReportHit")) { + const aty = e.f.inst(args[2]).ty; + const v = try e.rayVar(.hit_attribute_khr, aty); + try e.b.emit(.functions, .store, &.{ v, e.val(args[2]) }); + return try e.op1(.report_intersection_khr, ty, &.{ e.val(args[0]), e.val(args[1]) }); + } + if (eq(u8, name, "IgnoreHit") or eq(u8, name, "AcceptHitAndEndSearch")) { + try e.b.emit(.functions, if (eq(u8, name, "IgnoreHit")) .ignore_intersection_khr else .terminate_ray_khr, &.{}); + // A terminator: what follows is unreachable. + try e.b.emit(.functions, .label, &.{e.b.newId()}); + return 0; + } + const Sys = struct { []const u8, u32, enum { plain, transpose } }; + const table = [_]Sys{ + .{ "DispatchRaysIndex", 5319, .plain }, .{ "DispatchRaysDimensions", 5320, .plain }, .{ "WorldRayOrigin", 5321, .plain }, + .{ "WorldRayDirection", 5322, .plain }, .{ "ObjectRayOrigin", 5323, .plain }, .{ "ObjectRayDirection", 5324, .plain }, + .{ "RayTMin", 5325, .plain }, .{ "RayTCurrent", 5326, .plain }, .{ "InstanceID", 5327, .plain }, + .{ "InstanceIndex", 6, .plain }, .{ "PrimitiveIndex", 7, .plain }, .{ "HitKind", 5333, .plain }, + .{ "RayFlags", 5351, .plain }, .{ "GeometryIndex", 5352, .plain }, .{ "ObjectToWorld4x3", 5330, .plain }, + .{ "WorldToObject4x3", 5331, .plain }, .{ "ObjectToWorld3x4", 5330, .transpose }, .{ "WorldToObject3x4", 5331, .transpose }, + .{ "ObjectToWorld", 5330, .transpose }, .{ "WorldToObject", 5331, .transpose }, + }; + for (table) |x| { + if (!eq(u8, name, x[0])) continue; + if (x[2] == .plain) { + const vty = try e.builtinValueType(x[1], ty); + return try e.reshape(try e.op1(.load, vty, &.{try e.builtinInput(x[1], vty)}), vty, ty); + } + const m = e.t.get(ty).matrix; + const mty = try e.t.matrix(m.elem, m.cols, m.rows); + return try e.op1(.transpose, ty, &.{try e.op1(.load, mty, &.{try e.builtinInput(x[1], mty)})}); + } + return null; + } + + /// The SPIR-V type of a builtin read as an HLSL value of type `ty`. + fn builtinValueType(e: *Emitter, builtin: u32, ty: TypeId) Error!TypeId { + return switch (builtin) { + 5319, 5320 => e.t.vector(.uint, 3), + 5321...5324 => e.t.vector(.float, 3), + 5325, 5326 => e.t.scalar(.float), + 6, 7, 5327, 5333, 5351, 5352 => e.t.scalar(.uint), + else => ty, + }; + } + + fn rayQueryType(e: *Emitter) Error!Id { + if (e.ray_query_type == 0) { + e.ray_query_type = e.b.newId(); + try e.b.emit(.types_globals, .type_ray_query_khr, &.{e.ray_query_type}); + } + return e.ray_query_type; + } + + /// RayQuery methods (SPV_KHR_ray_query). + fn rayQueryMethod(e: *Emitter, name: []const u8, res: Ref, args: []const Ref, ty: TypeId) Error!Id { + const eq = std.mem.eql; + const q = e.val(res); + if (eq(u8, name, "TraceRayInline")) { + // The type's constant flags join the call's. + var flags = e.val(args[1]); + const tf = e.t.get(e.f.inst(res).ty).resource.flags; + if (tf != 0) flags = try e.op1(.bitwise_or, try e.t.scalar(.uint), &.{ flags, try e.constUint(tf) }); + const d = try e.rayDesc(args[3]); + try e.b.emit(.functions, .ray_query_initialize_khr, &.{ q, try e.loadAccel(args[0]), flags, e.val(args[2]), d[0], d[1], d[2], d[3] }); + return 0; + } + if (eq(u8, name, "Proceed")) return e.op1(.ray_query_proceed_khr, ty, &.{q}); + if (eq(u8, name, "Abort")) { + try e.b.emit(.functions, .ray_query_terminate_khr, &.{q}); + return 0; + } + if (eq(u8, name, "CommitNonOpaqueTriangleHit")) { + try e.b.emit(.functions, .ray_query_confirm_intersection_khr, &.{q}); + return 0; + } + if (eq(u8, name, "CommitProceduralPrimitiveHit")) { + try e.b.emit(.functions, .ray_query_generate_intersection_khr, &.{ q, e.val(args[0]) }); + return 0; + } + if (eq(u8, name, "RayFlags")) return e.op1(.ray_query_get_ray_flags_khr, ty, &.{q}); + if (eq(u8, name, "WorldRayOrigin")) return e.op1(.ray_query_get_world_ray_origin_khr, ty, &.{q}); + if (eq(u8, name, "WorldRayDirection")) return e.op1(.ray_query_get_world_ray_direction_khr, ty, &.{q}); + if (eq(u8, name, "RayTMin")) return e.op1(.ray_query_get_ray_t_min_khr, ty, &.{q}); + if (eq(u8, name, "CommittedStatus")) return e.op1(.ray_query_get_intersection_type_khr, ty, &.{ q, try e.constUint(1) }); + if (eq(u8, name, "CandidateType")) return e.op1(.ray_query_get_intersection_type_khr, ty, &.{ q, try e.constUint(0) }); + if (eq(u8, name, "CandidateProceduralPrimitiveNonOpaque")) { + const opaque_ = try e.op1(.ray_query_get_intersection_candidate_aabb_opaque_khr, ty, &.{q}); + return e.op1(.logical_not, ty, &.{opaque_}); + } + // Candidate* / Committed* getters. + const committed = std.mem.startsWith(u8, name, "Committed"); + if (!committed and !std.mem.startsWith(u8, name, "Candidate")) return e.unsupported("RayQuery method '{s}' is not supported yet", .{name}); + const rest = name[if (committed) 9 else 9..]; + const which = try e.constUint(@intFromBool(committed)); + const Get = struct { []const u8, Op, bool }; + const table = [_]Get{ + .{ "ObjectToWorld4x3", .ray_query_get_intersection_object_to_world_khr, false }, .{ "ObjectToWorld3x4", .ray_query_get_intersection_object_to_world_khr, true }, + .{ "WorldToObject4x3", .ray_query_get_intersection_world_to_object_khr, false }, .{ "WorldToObject3x4", .ray_query_get_intersection_world_to_object_khr, true }, + .{ "TriangleFrontFace", .ray_query_get_intersection_front_face_khr, false }, .{ "TriangleBarycentrics", .ray_query_get_intersection_barycentrics_khr, false }, + .{ "TriangleRayT", .ray_query_get_intersection_t_khr, false }, .{ "RayT", .ray_query_get_intersection_t_khr, false }, + .{ "InstanceIndex", .ray_query_get_intersection_instance_id_khr, false }, .{ "InstanceID", .ray_query_get_intersection_instance_custom_index_khr, false }, + .{ "GeometryIndex", .ray_query_get_intersection_geometry_index_khr, false }, .{ "PrimitiveIndex", .ray_query_get_intersection_primitive_index_khr, false }, + .{ "ObjectRayOrigin", .ray_query_get_intersection_object_ray_origin_khr, false }, .{ "ObjectRayDirection", .ray_query_get_intersection_object_ray_direction_khr, false }, + .{ "InstanceContributionToHitGroupIndex", .ray_query_get_intersection_instance_shader_binding_table_record_offset_khr, false }, + }; + for (table) |g| { + if (!eq(u8, rest, g[0])) continue; + if (!g[2]) return e.op1(g[1], ty, &.{ q, which }); + const m = e.t.get(ty).matrix; + const raw = try e.op1(g[1], try e.t.matrix(m.elem, m.cols, m.rows), &.{ q, which }); + return e.op1(.transpose, ty, &.{raw}); + } + return e.unsupported("RayQuery method '{s}' is not supported yet", .{name}); + } + fn samplerType(e: *Emitter) Error!Id { if (e.sampler_type == 0) { e.sampler_type = e.b.newId(); @@ -2468,6 +2771,7 @@ const Emitter = struct { fn method(e: *Emitter, name: []const u8, res: Ref, args: []const Ref, ty: TypeId) Error!Id { const eq = std.mem.eql; if (e.t.get(e.f.inst(res).ty) != .resource) return e.unsupported("method '{s}' is not supported yet", .{name}); + if (isRayQuery(e.t, e.f.inst(res).ty)) return e.rayQueryMethod(name, res, args, ty); const r = e.resourceInfo(res); if (eq(u8, name, "GetDimensions")) { try e.getDimensions(res, r, args); @@ -2989,17 +3293,49 @@ const Emitter = struct { return e.glsl; } + /// The module variable of ray tracing storage class `sc` holding values + /// of type `ty` (one per pair; outgoing payloads and callable data get + /// distinct Locations). + fn rayVar(e: *Emitter, sc: StorageClass, ty: TypeId) Error!Id { + const v = e.ray_vars.get(.{ sc, ty }) orelse blk: { + const v = e.b.newId(); + try e.b.emit(.types_globals, .variable, &.{ try e.pointerType(sc, try e.typeId(ty)), v, @backingInt(sc) }); + if (sc == .ray_payload_khr or sc == .callable_data_khr) { + const gop = try e.ray_locations.getOrPut(e.arena, sc); + if (!gop.found_existing) gop.value_ptr.* = 0; + try e.b.emit(.annotations, .decorate, &.{ v, 30, gop.value_ptr.* }); // Location + gop.value_ptr.* += 1; + } + const name = switch (sc) { + .incoming_ray_payload_khr => "payload", + .hit_attribute_khr => "attributes", + .incoming_callable_data_khr => "callable.data", + .ray_payload_khr => "ray.payload", + .callable_data_khr => "callable.param", + else => "ray.var", + }; + try e.b.emitWithString(.debug_names, .name, &.{v}, name, &.{}); + try e.ray_vars.put(e.arena, .{ sc, ty }, v); + break :blk v; + }; + try e.useVar(v); + return v; + } + /// An Input variable for a builtin the code reads (created once). fn builtinInput(e: *Emitter, builtin: u32, ty: TypeId) Error!Id { - if (e.builtin_inputs.get(builtin)) |v| return v; + if (e.builtin_inputs.get(builtin)) |v| { + try e.useVar(v); + return v; + } const v = e.b.newId(); try e.b.emit(.types_globals, .variable, &.{ try e.pointerType(.input, try e.typeId(ty)), v, @backingInt(StorageClass.input) }); try e.b.emit(.annotations, .decorate, &.{ v, 11, builtin }); // BuiltIn var et = ty; while (e.t.get(et) == .array) et = e.t.get(et).array.elem; const c = e.t.component(et).?; - if (e.m.entry.?.stage == .pixel and !c.isFloat() and c != .bool) try e.b.emit(.annotations, .decorate, &.{ v, 14 }); // Flat - try e.interface_ids.append(e.arena, v); + if (e.entry.stage == .pixel and !c.isFloat() and c != .bool) try e.b.emit(.annotations, .decorate, &.{ v, 14 }); // Flat + try e.useVar(v); try e.builtin_inputs.put(e.arena, builtin, v); return v; } @@ -3037,10 +3373,9 @@ const Emitter = struct { const xty = e.f.inst(args[0]).ty; const n = e.t.componentCount(xty).?; if (n == 1) return try e.op1(.group_non_uniform_all_equal, bool_ty, &.{ scope, e.val(args[0]) }); - const ct = try e.t.scalar(e.t.component(xty).?); const parts = try e.arena.alloc(Id, n); - for (parts, 0..) |*p, k| p.* = try e.op1(.group_non_uniform_all_equal, bool_ty, &.{ scope, try e.op1(.composite_extract, ct, &.{ e.val(args[0]), @intCast(k) }) }); - return try e.op1(.composite_construct, ty, parts); + for (parts, 0..) |*p, k| p.* = try e.op1(.group_non_uniform_all_equal, bool_ty, &.{ scope, (try e.flatComponent(e.val(args[0]), xty, @intCast(k)))[0] }); + return try e.composeFlat(ty, parts); } if (eq(u8, name, "WaveReadLaneFirst")) { try e.cap(.group_non_uniform_ballot); @@ -3531,9 +3866,19 @@ fn dfs(a: Allocator, f: *const ir.Function, b: u32, seen: []bool, post: *std.Arr try post.append(a, b); } +fn isRayQuery(t: *TypeTable, ty: TypeId) bool { + return switch (t.get(ty)) { + .resource => |r| r.kind == .other and std.mem.eql(u8, r.name, "RayQuery"), + else => false, + }; +} + +/// Resources and samplers (not RayQuery objects, which are function +/// storage passed by pointer). fn isResourceType(t: *TypeTable, ty: TypeId) bool { return switch (t.get(ty)) { - .resource, .sampler => true, + .resource => !isRayQuery(t, ty), + .sampler => true, .array => |a| isResourceType(t, a.elem), else => false, }; diff --git a/test/oracle/codegen.sh b/test/oracle/codegen.sh index 8a3775d..10422f5 100644 --- a/test/oracle/codegen.sh +++ b/test/oracle/codegen.sh @@ -20,7 +20,6 @@ valid=0; invalid=0; unsupported=0; other=0; crashed=0 : > "$TMP/invalid"; : > "$TMP/reasons"; : > "$TMP/valid"; : > "$TMP/crashed" while read -r f; do flags=$(sh test/oracle/run_flags.sh "$f") || continue - case "$flags" in *lib_6*) continue ;; esac is_spirv=0 case "$flags" in *-spirv*) is_spirv=1 ;; esac if [ "$BACKEND" = spirv ]; then diff --git a/test/spirv/rayquery.hlsl b/test/spirv/rayquery.hlsl new file mode 100644 index 0000000..ec80088 --- /dev/null +++ b/test/spirv/rayquery.hlsl @@ -0,0 +1,45 @@ +// PROFILE: cs_6_5 +// ENV: vulkan1.2 +// FLAGS: -E main +// RayQuery: a function-storage query object, its type flags joined to the +// TraceRayInline flags, passed to functions by pointer. +// ZCHECK: OpCapability RayQueryKHR +// ZCHECK: OpExtension "SPV_KHR_ray_query" +// ZCHECK: OpTypeRayQueryKHR +// ZCHECK: OpBitwiseOr %uint {{.*}} %uint_1 +// ZCHECK: OpRayQueryInitializeKHR +// ZCHECK: OpRayQueryProceedKHR +// ZCHECK: OpRayQueryGetIntersectionTypeKHR %uint {{.*}} %uint_0 +// ZCHECK: OpRayQueryConfirmIntersectionKHR +// ZCHECK: OpRayQueryGenerateIntersectionKHR +// ZCHECK: OpRayQueryGetIntersectionTypeKHR %uint {{.*}} %uint_1 +// ZCHECK: OpRayQueryGetIntersectionBarycentricsKHR %v2float {{.*}} %uint_1 +// ZCHECK: OpRayQueryGetIntersectionObjectToWorldKHR %mat4v3float +// ZCHECK: OpRayQueryTerminateKHR +RaytracingAccelerationStructure scene : register(t0); +RWStructuredBuffer output : register(u0); + +void start(RayQuery q, RayDesc ray) { + q.TraceRayInline(scene, RAY_FLAG_NONE, 0xFF, ray); +} + +[numthreads(8, 1, 1)] +void main(uint3 id : SV_DispatchThreadID) { + RayDesc ray = { float3(id), 0.0, float3(0, 0, 1), 100.0 }; + RayQuery q; + start(q, ray); + while (q.Proceed()) { + if (q.CandidateType() == CANDIDATE_NON_OPAQUE_TRIANGLE) + q.CommitNonOpaqueTriangleHit(); + else + q.CommitProceduralPrimitiveHit(q.RayTMin()); + } + float4 r = 0; + if (q.CommittedStatus() == COMMITTED_TRIANGLE_HIT) { + r.xy = q.CommittedTriangleBarycentrics(); + r.z = q.CommittedRayT() + q.CommittedInstanceIndex(); + r.w = q.CommittedObjectToWorld4x3()[3][0]; + } + q.Abort(); + output[id.x] = r; +} diff --git a/test/spirv/raytracing.hlsl b/test/spirv/raytracing.hlsl new file mode 100644 index 0000000..0124d16 --- /dev/null +++ b/test/spirv/raytracing.hlsl @@ -0,0 +1,77 @@ +// PROFILE: lib_6_3 +// ENV: vulkan1.2 +// Ray tracing shaders in one library: each entry gets its own interface +// (one incoming payload / hit attribute variable each), TraceRay and +// CallShader go through located payload / callable data variables. +// ZCHECK: OpCapability RayTracingKHR +// ZCHECK: OpExtension "SPV_KHR_ray_tracing" +// ZCHECK: OpEntryPoint RayGenerationKHR %rgen "rgen" +// ZCHECK: OpEntryPoint ClosestHitKHR %chit "chit" +// ZCHECK: OpEntryPoint MissKHR %miss "miss" +// ZCHECK: OpEntryPoint AnyHitKHR %ahit "ahit" +// ZCHECK: OpEntryPoint IntersectionKHR %isect "isect" +// ZCHECK: OpEntryPoint CallableKHR %call "call" +// ZCHECK: OpDecorate %ray_payload Location 0 +// ZCHECK: OpDecorate {{.*}} BuiltIn LaunchIdKHR +// ZCHECK: OpDecorate {{.*}} BuiltIn ObjectToWorldKHR +// ZCHECK: OpTypeAccelerationStructureKHR +// ZCHECK: OpTraceRayKHR +// ZCHECK: OpExecuteCallableKHR +// ZCHECK: OpReportIntersectionKHR +// ZCHECK: OpIgnoreIntersectionKHR +// ZCHECK: OpTerminateRayKHR +// ZCHECK: OpTranspose %mat3v4float +struct Payload { float4 color; uint depth; }; +struct Attr { float2 bary; }; +struct CallData { float3 v; }; + +RaytracingAccelerationStructure scene : register(t0); +RWTexture2D output : register(u0); + +[shader("raygeneration")] +void rgen() { + uint2 id = DispatchRaysIndex().xy; + float2 uv = (float2(id) + 0.5) / float2(DispatchRaysDimensions().xy); + RayDesc ray; + ray.Origin = float3(uv, 0); + ray.Direction = float3(0, 0, 1); + ray.TMin = 0.001; + ray.TMax = 1000; + Payload p = { float4(0, 0, 0, 0), 0 }; + TraceRay(scene, RAY_FLAG_FORCE_OPAQUE, 0xFF, 0, 1, 0, ray, p); + CallData c = { p.color.rgb }; + CallShader(0, c); + output[id] = float4(c.v, 1); +} + +[shader("closesthit")] +void chit(inout Payload p, in BuiltInTriangleIntersectionAttributes a) { + float3x4 m = ObjectToWorld3x4(); + p.color = float4(a.barycentrics, RayTCurrent(), m[0][3]) + WorldRayDirection().xyzz; + p.depth = InstanceID() + PrimitiveIndex() + HitKind() + GeometryIndex(); +} + +[shader("miss")] +void miss(inout Payload p) { + p.color = float4(WorldRayOrigin(), RayTMin()); +} + +[shader("anyhit")] +void ahit(inout Payload p, in Attr a) { + if (a.bary.x > 0.5) + IgnoreHit(); + if (RayFlags() & RAY_FLAG_CULL_BACK_FACING_TRIANGLES) + AcceptHitAndEndSearch(); + p.depth += 1; +} + +[shader("intersection")] +void isect() { + Attr a = { ObjectRayOrigin().xy }; + ReportHit(RayTCurrent() * 0.5, 0, a); +} + +[shader("callable")] +void call(inout CallData c) { + c.v *= 2; +}