diff --git a/src/dxcapi.zig b/src/dxcapi.zig index b068937..7cbd414 100644 --- a/src/dxcapi.zig +++ b/src/dxcapi.zig @@ -112,6 +112,8 @@ const IID_IDxcIncludeHandler = GUID.parse("7f61fc7d-950d-467f-b3e3-3c02fb49187c" const IID_IDxcOperationResult = GUID.parse("cedb484a-d4e9-445a-b991-ca21ca157dc2"); const IID_IDxcResult = GUID.parse("58346cda-dde7-4497-9461-6f87af5e0659"); const IID_IDxcCompiler3 = GUID.parse("228b4687-5a6a-4730-900c-9702b2203f54"); +const IID_IDxcCompiler = GUID.parse("8c210bf3-011f-4422-8d70-6f9acb8db617"); +const IID_IDxcCompiler2 = GUID.parse("a005a9d9-b8bb-4594-b5c9-0e633bec4d37"); const IID_IDxcUtils = GUID.parse("4605c4cb-2019-492a-ada4-65f20bb7d67f"); const IID_IDxcVersionInfo = GUID.parse("b04f5b50-2059-4f12-a8ff-a1e0cde1cc7e"); @@ -739,6 +741,18 @@ const Compiler3Vtbl = extern struct { Disassemble: *const fn (This, ?*const Buffer, *const GUID, ?Out) callconv(cc) HRESULT, }; +const DxcDefine = extern struct { Name: ?[*:0]const WCHAR, Value: ?[*:0]const WCHAR }; + +/// IDxcCompiler and IDxcCompiler2 (hassle-rs and other older clients): +/// entry point, profile and defines as parameters, the source as a blob. +const Compiler2Vtbl = extern struct { + unknown: UnknownVtbl, + Compile: *const fn (This, ?*anyopaque, ?[*:0]const WCHAR, ?[*:0]const WCHAR, ?[*:0]const WCHAR, ?[*]const ?[*:0]const WCHAR, u32, ?[*]const DxcDefine, u32, ?*anyopaque, ?Out) callconv(cc) HRESULT, + Preprocess: *const fn (This, ?*anyopaque, ?[*:0]const WCHAR, ?[*]const ?[*:0]const WCHAR, u32, ?[*]const DxcDefine, u32, ?*anyopaque, ?Out) callconv(cc) HRESULT, + Disassemble: *const fn (This, ?*anyopaque, ?Out) callconv(cc) HRESULT, + CompileWithDebug: *const fn (This, ?*anyopaque, ?[*:0]const WCHAR, ?[*:0]const WCHAR, ?[*:0]const WCHAR, ?[*]const ?[*:0]const WCHAR, u32, ?[*]const DxcDefine, u32, ?*anyopaque, ?Out, ?*?[*:0]WCHAR, ?Out) callconv(cc) HRESULT, +}; + const VersionInfoVtbl = extern struct { unknown: UnknownVtbl, GetVersion: *const fn (This, ?*u32, ?*u32) callconv(cc) HRESULT, @@ -748,8 +762,17 @@ const VersionInfoVtbl = extern struct { const Compiler = struct { compiler: Iface(Compiler3Vtbl) = .{ .vtbl = &compiler_vtbl }, version: Iface(VersionInfoVtbl) = .{ .vtbl = &version_vtbl }, + legacy: Iface(Compiler2Vtbl) = .{ .vtbl = &legacy_vtbl }, rc: RefCount = .{}, + const legacy_vtbl: Compiler2Vtbl = .{ + .unknown = .{ .QueryInterface = legacyQueryInterface, .AddRef = legacyAddRef, .Release = legacyRelease }, + .Compile = legacyCompile, + .Preprocess = legacyPreprocess, + .Disassemble = legacyDisassemble, + .CompileWithDebug = legacyCompileWithDebug, + }; + const compiler_vtbl: Compiler3Vtbl = .{ .unknown = .{ .QueryInterface = queryInterface, .AddRef = addRef, .Release = release }, .Compile = compileFn, @@ -770,6 +793,8 @@ const Compiler = struct { &c.compiler else if (iid.eql(IID_IDxcVersionInfo)) &c.version + else if (iid.eql(IID_IDxcCompiler) or iid.eql(IID_IDxcCompiler2)) + &c.legacy else null; if (p == null) { @@ -834,6 +859,85 @@ const Compiler = struct { _ = put(out, null); return E_NOTIMPL; } + + fn legacyOf(this: This) *Compiler { + return parent(Compiler, "legacy", this); + } + fn legacyQueryInterface(this: This, iid: *const GUID, out: ?Out) callconv(cc) HRESULT { + return legacyOf(this).query(iid, out); + } + fn legacyAddRef(this: This) callconv(cc) ULONG { + return legacyOf(this).rc.addRef(); + } + fn legacyRelease(this: This) callconv(cc) ULONG { + return release(&legacyOf(this).compiler); + } + + fn legacyCompile(_: This, source: ?*anyopaque, name: ?[*:0]const WCHAR, entry: ?[*:0]const WCHAR, profile: ?[*:0]const WCHAR, args: ?[*]const ?[*:0]const WCHAR, count: u32, defines: ?[*]const DxcDefine, define_count: u32, include: ?*anyopaque, out: ?Out) callconv(cc) HRESULT { + const p = profile orelse { + _ = put(out, null); + return E_INVALIDARG; + }; + return legacyRun(source, name, .{ .compile = .{ .entry = entry, .profile = p } }, args, count, defines, define_count, include, out); + } + fn legacyCompileWithDebug(this: This, source: ?*anyopaque, name: ?[*:0]const WCHAR, entry: ?[*:0]const WCHAR, profile: ?[*:0]const WCHAR, args: ?[*]const ?[*:0]const WCHAR, count: u32, defines: ?[*]const DxcDefine, define_count: u32, include: ?*anyopaque, out: ?Out, debug_name: ?*?[*:0]WCHAR, debug_blob: ?Out) callconv(cc) HRESULT { + // No separate debug blob (zdxc writes no PDBs). + if (debug_name) |d| d.* = null; + if (debug_blob) |d| d.* = null; + return legacyCompile(this, source, name, entry, profile, args, count, defines, define_count, include, out); + } + fn legacyPreprocess(_: This, source: ?*anyopaque, name: ?[*:0]const WCHAR, args: ?[*]const ?[*:0]const WCHAR, count: u32, defines: ?[*]const DxcDefine, define_count: u32, include: ?*anyopaque, out: ?Out) callconv(cc) HRESULT { + return legacyRun(source, name, .preprocess, args, count, defines, define_count, include, out); + } + fn legacyDisassemble(_: This, _: ?*anyopaque, out: ?Out) callconv(cc) HRESULT { + _ = put(out, null); + return E_NOTIMPL; + } + + const LegacyMode = union(enum) { + compile: struct { entry: ?[*:0]const WCHAR, profile: [*:0]const WCHAR }, + preprocess, + }; + + /// The legacy calls as IDxcCompiler3 arguments: `-E entry -T profile` + /// (or `-P`), the arguments, then `-D name=value` per define. + fn legacyRun(source: ?*anyopaque, name: ?[*:0]const WCHAR, mode: LegacyMode, args: ?[*]const ?[*:0]const WCHAR, count: u32, defines: ?[*]const DxcDefine, define_count: u32, include: ?*anyopaque, out: ?Out) HRESULT { + const o = out orelse return E_POINTER; + o.* = null; + const blob = source orelse return E_INVALIDARG; + if ((count > 0 and args == null) or (define_count > 0 and defines == null)) return E_INVALIDARG; + const r = runLegacy(blob, name, mode, if (args) |a| a[0..count] else &.{}, if (defines) |d| d[0..define_count] else &.{}, include) catch |e| return switch (e) { + error.OutOfMemory => E_OUTOFMEMORY, + error.InvalidArgument => E_INVALIDARG, + }; + o.* = &r.iface; + return S_OK; + } + + fn runLegacy(blob: *anyopaque, name: ?[*:0]const WCHAR, mode: LegacyMode, wargs: []const ?[*:0]const WCHAR, defines: []const DxcDefine, include: ?*anyopaque) error{ OutOfMemory, InvalidArgument }!*Result { + if (thread_arena == null) thread_arena = .init(gpa); + const arena = &thread_arena.?; + defer _ = arena.reset(.{ .retain_with_limit = retain_limit }); + const a = arena.allocator(); + var args: std.ArrayList([]const u8) = .empty; + switch (mode) { + .compile => |c| { + if (c.entry) |e| try args.appendSlice(a, &.{ "-E", try fromWide(a, e) }); + try args.appendSlice(a, &.{ "-T", try fromWide(a, c.profile) }); + }, + .preprocess => try args.append(a, "-P"), + } + for (wargs) |w| try args.append(a, try fromWide(a, w orelse return error.InvalidArgument)); + for (defines) |d| { + const n = try fromWide(a, d.Name orelse continue); + try args.appendSlice(a, &.{ "-D", if (d.Value) |v| try std.fmt.allocPrint(a, "{s}={s}", .{ n, try fromWide(a, v) }) else n }); + } + const text = Utils.blobText(a, blob) catch |e| return switch (e) { + error.OutOfMemory => error.OutOfMemory, + else => error.InvalidArgument, + }; + return runText(a, args.items, text, if (name) |n| try fromWide(a, n) else null, include); + } }; /// Each thread's compile arena, kept between calls. Freeing it per call @@ -851,11 +955,17 @@ fn run(source: *const Buffer, wargs: []const ?[*:0]const WCHAR, include: ?*anyop const args = try a.alloc([]const u8, wargs.len); for (args, wargs) |*d, w| d.* = try fromWide(a, w orelse return error.InvalidArgument); const bytes: []const u8 = if (source.ptr) |p| p[0..source.size] else &.{}; + return runText(a, args, try decodeText(a, bytes, source.encoding), null, include); +} + +/// A compile of `text` (in the caller's thread arena). +fn runText(a: std.mem.Allocator, args: []const []const u8, text: []const u8, source_name: ?[]const u8, include: ?*anyopaque) error{ OutOfMemory, InvalidArgument }!*Result { var client: ClientFs = undefined; var threaded: std.Io.Threaded = .init_single_threaded; var req: compile.Request = .{ .args = args, - .source = try decodeText(a, bytes, source.encoding), + .source = text, + .source_name = source_name, .io = threaded.io(), .fs = NoFs.fileSystem(), .compat_notes = false, diff --git a/src/spirv/emit.zig b/src/spirv/emit.zig index 41e117b..a2dadbf 100644 --- a/src/spirv/emit.zig +++ b/src/spirv/emit.zig @@ -203,6 +203,9 @@ const Emitter = struct { spec_ids: std.AutoHashMapUnmanaged(u32, Id) = .empty, ext_imports: std.StringHashMapUnmanaged(Id) = .empty, string_ids: std.StringHashMapUnmanaged(Id) = .empty, + /// Reads of resource locals nothing assigns: the resource of the same + /// type each stands in with where it is used. + undef_resources: std.AutoHashMapUnmanaged(u32, Id) = .empty, /// Flattened resource arrays: a variable per element. flat_vars: std.AutoHashMapUnmanaged(u32, []const Id) = .empty, flat_iface: std.ArrayList(Id) = .empty, @@ -845,6 +848,14 @@ const Emitter = struct { return id; } + /// A declared resource global of type `ty`. + fn anyGlobalOf(e: *Emitter, ty: TypeId) ?u32 { + for (e.m.globals.items, 0..) |g, gi| { + if (g.ty == ty and g.space == .resource and e.global_vars[gi] != 0) return @intCast(gi); + } + return null; + } + /// An OpString of `str` (made once). fn stringId(e: *Emitter, str: []const u8) Error!Id { if (e.string_ids.get(str)) |id| return id; @@ -2384,7 +2395,9 @@ const Emitter = struct { } fn val(e: *Emitter, r: Ref) Id { - return e.vals[r.index()]; + const v = e.vals[r.index()]; + if (v == 0) if (e.undef_resources.get(r.index())) |stand_in| return stand_in; + return v; } fn compOf(e: *Emitter, ty: TypeId) Scalar { @@ -2449,7 +2462,28 @@ const Emitter = struct { try e.indexed_loads.put(e.arena, @intCast(ri), .{ .global = il.global, .indices = idx, .types = il.index_types, .nonuniform = il.nonuniform }); return; } - if (e.val(p) == 0 and f.inst(p).op == .local_var) return e.unsupported("a resource local read before it is assigned", .{}); + if (e.val(p) == 0 and f.inst(p).op == .local_var) { + // A resource local nothing assigns (a struct copied + // with a resource member left unset, read where dxc's + // legalization folds the read away): its value is + // undefined, so any resource of its type will do. + // An OpUndef pointer would break logical addressing. + // Copies (`res = this`) pass the undefined value on + // without assigning it; the first real use takes the + // stand-in. + const only_copies = for (f.insts.items) |x| { + if (x.op != .store or x.op.store.ptr != p) continue; + const v = f.inst(x.op.store.value); + if (v.op != .load or f.inst(v.op.load).op != .local_var) break false; + } else true; + if (only_copies) if (e.anyGlobalOf(i.ty)) |g| { + if (e.spv_minor >= 4) try e.useVar(e.global_vars[g]); + e.vals[ri] = 0; + try e.undef_resources.put(e.arena, @intCast(ri), e.global_vars[g]); + return; + }; + return e.unsupported("a resource local read before it is assigned", .{}); + } e.vals[ri] = e.val(p); return; } @@ -2486,6 +2520,8 @@ const Emitter = struct { } return; } + // Copying an undefined resource assigns nothing. + if (target.op == .local_var and e.undef_resources.contains(s.value.index())) 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", .{}); @@ -4289,12 +4325,20 @@ const Emitter = struct { } fn globalOf(e: *Emitter, r: Ref) ?u32 { + return e.globalOfDepth(r, 0); + } + + /// (Copies between resource locals can form cycles: give up after + /// `max_depth` steps.) + fn globalOfDepth(e: *Emitter, r: Ref, depth: u32) ?u32 { + const max_depth = 256; + if (depth > max_depth) return null; const i = e.f.inst(r); return switch (i.op) { .global => |g| g, - .load => |p| e.globalOf(p), + .load => |p| e.globalOfDepth(p, depth + 1), .local_var => for (e.f.insts.items) |inst| { - if (inst.op == .store and inst.op.store.ptr == r) break e.globalOf(inst.op.store.value); + if (inst.op == .store and inst.op.store.ptr == r) break e.globalOfDepth(inst.op.store.value, depth + 1); } else null, // A specialized parameter: the global it is bound to. .param => |pi| blk: {