Merge pull request #2056 from ghostty-org/metal

metal: precompile shaders as part of the build
This commit is contained in:
Mitchell Hashimoto
2024-08-06 15:44:35 -07:00
committed by GitHub
9 changed files with 144 additions and 10 deletions

View File

@ -16,6 +16,7 @@ const BuildConfig = build_config.BuildConfig;
const WasmTarget = @import("src/os/wasm/target.zig").Target; const WasmTarget = @import("src/os/wasm/target.zig").Target;
const LibtoolStep = @import("src/build/LibtoolStep.zig"); const LibtoolStep = @import("src/build/LibtoolStep.zig");
const LipoStep = @import("src/build/LipoStep.zig"); const LipoStep = @import("src/build/LipoStep.zig");
const MetallibStep = @import("src/build/MetallibStep.zig");
const XCFrameworkStep = @import("src/build/XCFrameworkStep.zig"); const XCFrameworkStep = @import("src/build/XCFrameworkStep.zig");
const Version = @import("src/build/Version.zig"); const Version = @import("src/build/Version.zig");
const Command = @import("src/Command.zig"); const Command = @import("src/Command.zig");
@ -1076,6 +1077,7 @@ fn addDeps(
// This makes things like `os/log.h` available for cross-compiling. // This makes things like `os/log.h` available for cross-compiling.
if (step.rootModuleTarget().isDarwin()) { if (step.rootModuleTarget().isDarwin()) {
try @import("apple_sdk").addPaths(b, &step.root_module); try @import("apple_sdk").addPaths(b, &step.root_module);
try addMetallib(b, step);
} }
// We always need the Zig packages // We always need the Zig packages
@ -1262,6 +1264,34 @@ fn addDeps(
return static_libs; 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 /// Generate help files
fn addHelp( fn addHelp(
b: *std.Build, b: *std.Build,

8
pkg/macos/dispatch.zig Normal file
View File

@ -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());
}

3
pkg/macos/dispatch/c.zig Normal file
View File

@ -0,0 +1,3 @@
pub usingnamespace @cImport({
@cInclude("dispatch/dispatch.h");
});

View File

@ -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;

View File

@ -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().?;
}

View File

@ -1,5 +1,6 @@
pub const foundation = @import("foundation.zig"); pub const foundation = @import("foundation.zig");
pub const animation = @import("animation.zig"); pub const animation = @import("animation.zig");
pub const dispatch = @import("dispatch.zig");
pub const graphics = @import("graphics.zig"); pub const graphics = @import("graphics.zig");
pub const os = @import("os.zig"); pub const os = @import("os.zig");
pub const text = @import("text.zig"); pub const text = @import("text.zig");

View File

@ -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;
}

View File

@ -156,27 +156,29 @@ pub const PostUniforms = extern struct {
/// Initialize the MTLLibrary. A MTLLibrary is a collection of shaders. /// Initialize the MTLLibrary. A MTLLibrary is a collection of shaders.
fn initLibrary(device: objc.Object) !objc.Object { fn initLibrary(device: objc.Object) !objc.Object {
// Hardcoded since this file isn't meant to be reusable. const start = try std.time.Instant.now();
const data = @embedFile("../shaders/cell.metal");
const source = try macos.foundation.String.createWithBytes( const data = try macos.dispatch.Data.create(
data, @embedFile("ghostty_metallib"),
.utf8, macos.dispatch.queue.getMain(),
false, macos.dispatch.Data.DESTRUCTOR_DEFAULT,
); );
defer source.release(); defer data.release();
var err: ?*anyopaque = null; var err: ?*anyopaque = null;
const library = device.msgSend( const library = device.msgSend(
objc.Object, objc.Object,
objc.sel("newLibraryWithSource:options:error:"), objc.sel("newLibraryWithData:error:"),
.{ .{
source, data,
@as(?*anyopaque, null),
&err, &err,
}, },
); );
try checkError(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; return library;
} }

View File

@ -1,3 +1,5 @@
#include <metal_stdlib>
using namespace metal; using namespace metal;
struct Uniforms { struct Uniforms {
@ -291,6 +293,7 @@ fragment float4 cell_text_fragment(CellTextVertexOut in [[stage_in]],
constexpr sampler textureSampler(address::clamp_to_edge, filter::linear); constexpr sampler textureSampler(address::clamp_to_edge, filter::linear);
switch (in.mode) { switch (in.mode) {
default:
case MODE_TEXT_CURSOR: case MODE_TEXT_CURSOR:
case MODE_TEXT_CONSTRAINED: case MODE_TEXT_CONSTRAINED:
case MODE_TEXT_POWERLINE: case MODE_TEXT_POWERLINE: