diff --git a/build.zig b/build.zig index 359bb4fde..30d793ee0 100644 --- a/build.zig +++ b/build.zig @@ -16,6 +16,7 @@ const BuildConfig = build_config.BuildConfig; const WasmTarget = @import("src/os/wasm/target.zig").Target; const LibtoolStep = @import("src/build/LibtoolStep.zig"); const LipoStep = @import("src/build/LipoStep.zig"); +const MetallibStep = @import("src/build/MetallibStep.zig"); const XCFrameworkStep = @import("src/build/XCFrameworkStep.zig"); const Version = @import("src/build/Version.zig"); const Command = @import("src/Command.zig"); @@ -1072,6 +1073,7 @@ fn addDeps( // This makes things like `os/log.h` available for cross-compiling. if (step.rootModuleTarget().isDarwin()) { try @import("apple_sdk").addPaths(b, &step.root_module); + try addMetallib(b, step); } // We always need the Zig packages @@ -1257,6 +1259,34 @@ fn addDeps( return static_libs; } +/// Generate Metal shader library +fn addMetallib( + b: *std.Build, + step_: ?*std.Build.Step.Compile, +) !void { + // Our static state between runs. We memoize so we only compile + // the Metal shaders once. + const MetalState = struct { + var generated: ?std.Build.LazyPath = null; + }; + + const output = MetalState.generated orelse metal: { + const step = MetallibStep.create(b, .{ + .name = "Ghostty", + .sources = &.{b.path("src/renderer/shaders/cell.metal")}, + }); + + break :metal step.output; + }; + + if (step_) |step| { + output.addStepDependencies(&step.step); + step.root_module.addAnonymousImport("ghostty_metallib", .{ + .root_source_file = output, + }); + } +} + /// Generate help files fn addHelp( b: *std.Build, diff --git a/src/build/MetallibStep.zig b/src/build/MetallibStep.zig new file mode 100644 index 000000000..8d78d0ceb --- /dev/null +++ b/src/build/MetallibStep.zig @@ -0,0 +1,48 @@ +/// A zig build step that compiles a set of ".metal" files into a +/// ".metallib" file. +const MetallibStep = @This(); + +const std = @import("std"); +const Step = std.Build.Step; +const RunStep = std.Build.Step.Run; +const LazyPath = std.Build.LazyPath; + +pub const Options = struct { + /// The name of the xcframework to create. + name: []const u8, + + /// The Metal source files. + sources: []const LazyPath, +}; + +step: *Step, +output: LazyPath, + +pub fn create(b: *std.Build, opts: Options) *MetallibStep { + const self = b.allocator.create(MetallibStep) catch @panic("OOM"); + + const run_ir = RunStep.create( + b, + b.fmt("metal {s}", .{opts.name}), + ); + run_ir.addArgs(&.{ "xcrun", "-sdk", "macosx", "metal", "-o" }); + const output_ir = run_ir.addOutputFileArg(b.fmt("{s}.ir", .{opts.name})); + run_ir.addArgs(&.{"-c"}); + for (opts.sources) |source| run_ir.addFileArg(source); + + const run_lib = RunStep.create( + b, + b.fmt("metallib {s}", .{opts.name}), + ); + run_lib.addArgs(&.{ "xcrun", "-sdk", "macosx", "metallib", "-o" }); + const output_lib = run_lib.addOutputFileArg(b.fmt("{s}.metallib", .{opts.name})); + run_lib.addFileArg(output_ir); + run_lib.step.dependOn(&run_ir.step); + + self.* = .{ + .step = &run_lib.step, + .output = output_lib, + }; + + return self; +} diff --git a/src/renderer/metal/shaders.zig b/src/renderer/metal/shaders.zig index bbaf77dc8..721cc6e10 100644 --- a/src/renderer/metal/shaders.zig +++ b/src/renderer/metal/shaders.zig @@ -159,7 +159,7 @@ fn initLibrary(device: objc.Object) !objc.Object { const start = try std.time.Instant.now(); const data = try macos.dispatch.Data.create( - @embedFile("../shaders/Ghostty.metallib"), + @embedFile("ghostty_metallib"), macos.dispatch.queue.getMain(), macos.dispatch.Data.DESTRUCTOR_DEFAULT, );