mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-08-02 14:57:31 +03:00
Merge pull request #2056 from ghostty-org/metal
metal: precompile shaders as part of the build
This commit is contained in:
30
build.zig
30
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,
|
||||
|
8
pkg/macos/dispatch.zig
Normal file
8
pkg/macos/dispatch.zig
Normal 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
3
pkg/macos/dispatch/c.zig
Normal file
@ -0,0 +1,3 @@
|
||||
pub usingnamespace @cImport({
|
||||
@cInclude("dispatch/dispatch.h");
|
||||
});
|
31
pkg/macos/dispatch/data.zig
Normal file
31
pkg/macos/dispatch/data.zig
Normal 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;
|
8
pkg/macos/dispatch/queue.zig
Normal file
8
pkg/macos/dispatch/queue.zig
Normal 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().?;
|
||||
}
|
@ -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");
|
||||
|
48
src/build/MetallibStep.zig
Normal file
48
src/build/MetallibStep.zig
Normal 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;
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <metal_stdlib>
|
||||
|
||||
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:
|
||||
|
Reference in New Issue
Block a user