diff --git a/build.zig b/build.zig index f2c1d35a8..9cac8d810 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"); @@ -1076,6 +1077,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 @@ -1262,6 +1264,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/pkg/macos/dispatch.zig b/pkg/macos/dispatch.zig new file mode 100644 index 000000000..9c66b529f --- /dev/null +++ b/pkg/macos/dispatch.zig @@ -0,0 +1,8 @@ +pub const c = @import("dispatch/c.zig"); +pub const data = @import("dispatch/data.zig"); +pub const queue = @import("dispatch/queue.zig"); +pub const Data = data.Data; + +test { + @import("std").testing.refAllDecls(@This()); +} diff --git a/pkg/macos/dispatch/c.zig b/pkg/macos/dispatch/c.zig new file mode 100644 index 000000000..527469765 --- /dev/null +++ b/pkg/macos/dispatch/c.zig @@ -0,0 +1,3 @@ +pub usingnamespace @cImport({ + @cInclude("dispatch/dispatch.h"); +}); diff --git a/pkg/macos/dispatch/data.zig b/pkg/macos/dispatch/data.zig new file mode 100644 index 000000000..223eabeb1 --- /dev/null +++ b/pkg/macos/dispatch/data.zig @@ -0,0 +1,31 @@ +const std = @import("std"); +const foundation = @import("../foundation.zig"); +const c = @import("c.zig"); + +pub const Data = opaque { + pub const DESTRUCTOR_DEFAULT = c.DISPATCH_DATA_DESTRUCTOR_DEFAULT; + + pub fn create( + data: []const u8, + queue: ?*anyopaque, + destructor: ?*anyopaque, + ) !*Data { + return dispatch_data_create( + data.ptr, + data.len, + queue, + destructor, + ) orelse return error.OutOfMemory; + } + + pub fn release(data: *Data) void { + foundation.c.CFRelease(data); + } +}; + +extern "c" fn dispatch_data_create( + data: [*]const u8, + len: usize, + queue: ?*anyopaque, + destructor: ?*anyopaque, +) ?*Data; diff --git a/pkg/macos/dispatch/queue.zig b/pkg/macos/dispatch/queue.zig new file mode 100644 index 000000000..72b6bbaec --- /dev/null +++ b/pkg/macos/dispatch/queue.zig @@ -0,0 +1,8 @@ +const std = @import("std"); +const c = @import("c.zig"); + +pub const Queue = *anyopaque; // dispatch_queue_t + +pub fn getMain() Queue { + return c.dispatch_get_main_queue().?; +} diff --git a/pkg/macos/main.zig b/pkg/macos/main.zig index 2b05f11ae..20274e9c0 100644 --- a/pkg/macos/main.zig +++ b/pkg/macos/main.zig @@ -1,5 +1,6 @@ pub const foundation = @import("foundation.zig"); pub const animation = @import("animation.zig"); +pub const dispatch = @import("dispatch.zig"); pub const graphics = @import("graphics.zig"); pub const os = @import("os.zig"); pub const text = @import("text.zig"); 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 0652a3c98..721cc6e10 100644 --- a/src/renderer/metal/shaders.zig +++ b/src/renderer/metal/shaders.zig @@ -156,27 +156,29 @@ pub const PostUniforms = extern struct { /// Initialize the MTLLibrary. A MTLLibrary is a collection of shaders. fn initLibrary(device: objc.Object) !objc.Object { - // Hardcoded since this file isn't meant to be reusable. - const data = @embedFile("../shaders/cell.metal"); - const source = try macos.foundation.String.createWithBytes( - data, - .utf8, - false, + const start = try std.time.Instant.now(); + + const data = try macos.dispatch.Data.create( + @embedFile("ghostty_metallib"), + macos.dispatch.queue.getMain(), + macos.dispatch.Data.DESTRUCTOR_DEFAULT, ); - defer source.release(); + defer data.release(); var err: ?*anyopaque = null; const library = device.msgSend( objc.Object, - objc.sel("newLibraryWithSource:options:error:"), + objc.sel("newLibraryWithData:error:"), .{ - source, - @as(?*anyopaque, null), + data, &err, }, ); try checkError(err); + const end = try std.time.Instant.now(); + log.debug("shader library loaded time={}us", .{end.since(start) / std.time.ns_per_us}); + return library; } diff --git a/src/renderer/shaders/cell.metal b/src/renderer/shaders/cell.metal index c382f87a3..9719dea81 100644 --- a/src/renderer/shaders/cell.metal +++ b/src/renderer/shaders/cell.metal @@ -1,3 +1,5 @@ +#include + using namespace metal; struct Uniforms { @@ -291,6 +293,7 @@ fragment float4 cell_text_fragment(CellTextVertexOut in [[stage_in]], constexpr sampler textureSampler(address::clamp_to_edge, filter::linear); switch (in.mode) { + default: case MODE_TEXT_CURSOR: case MODE_TEXT_CONSTRAINED: case MODE_TEXT_POWERLINE: