From 63386e4a22e1da4bb87824b3b9dd0b8c351a3b32 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 25 Aug 2023 08:12:31 -0700 Subject: [PATCH] build: can select renderer with -Drenderer Note that not all renderers work in all environments. --- build.zig | 9 +++++++++ src/build_config.zig | 7 +++++++ src/renderer.zig | 34 ++++++++++++++++++++++++++++------ 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/build.zig b/build.zig index 5546b1117..231595afb 100644 --- a/build.zig +++ b/build.zig @@ -6,6 +6,7 @@ const RunStep = std.build.RunStep; const apprt = @import("src/apprt.zig"); const font = @import("src/font/main.zig"); +const renderer = @import("src/renderer.zig"); const terminfo = @import("src/terminfo/main.zig"); const WasmTarget = @import("src/os/wasm/target.zig").Target; const LibtoolStep = @import("src/build/LibtoolStep.zig"); @@ -52,6 +53,7 @@ const app_version = std.SemanticVersion{ .major = 0, .minor = 1, .patch = 0 }; var tracy: bool = false; var flatpak: bool = false; var app_runtime: apprt.Runtime = .none; +var renderer_impl: renderer.Impl = .opengl; var font_backend: font.Backend = .freetype; pub fn build(b: *std.Build) !void { @@ -98,6 +100,12 @@ pub fn build(b: *std.Build) !void { "The app runtime to use. Not all values supported on all platforms.", ) orelse apprt.Runtime.default(target); + renderer_impl = b.option( + renderer.Impl, + "renderer", + "The app runtime to use. Not all values supported on all platforms.", + ) orelse renderer.Impl.default(target, wasm_target); + const static = b.option( bool, "static", @@ -203,6 +211,7 @@ pub fn build(b: *std.Build) !void { exe_options.addOption(bool, "flatpak", flatpak); exe_options.addOption(apprt.Runtime, "app_runtime", app_runtime); exe_options.addOption(font.Backend, "font_backend", font_backend); + exe_options.addOption(renderer.Impl, "renderer", renderer_impl); // Exe if (exe_) |exe| { diff --git a/src/build_config.zig b/src/build_config.zig index b086ae0fa..867d1da51 100644 --- a/src/build_config.zig +++ b/src/build_config.zig @@ -8,6 +8,7 @@ const options = @import("build_options"); const assert = std.debug.assert; const apprt = @import("apprt.zig"); const font = @import("font/main.zig"); +const rendererpkg = @import("renderer.zig"); /// The semantic version of this build. pub const version = options.app_version; @@ -29,6 +30,12 @@ pub const font_backend: font.Backend = std.meta.stringToEnum( @tagName(options.font_backend), ).?; +/// The renderer implementation to use. +pub const renderer: rendererpkg.Impl = std.meta.stringToEnum( + rendererpkg.Impl, + @tagName(options.renderer), +).?; + /// We want to integrate with Flatpak APIs. pub const flatpak = options.flatpak; diff --git a/src/renderer.zig b/src/renderer.zig index e28f024aa..b2a75ae36 100644 --- a/src/renderer.zig +++ b/src/renderer.zig @@ -7,7 +7,10 @@ //! APIs. The renderers in this package assume that the renderer is already //! setup (OpenGL has a context, Vulkan has a surface, etc.) +const std = @import("std"); const builtin = @import("builtin"); +const build_config = @import("build_config.zig"); +const WasmTarget = @import("os/wasm/target.zig").Target; pub usingnamespace @import("renderer/cursor.zig"); pub usingnamespace @import("renderer/message.zig"); @@ -19,14 +22,33 @@ pub const Options = @import("renderer/Options.zig"); pub const Thread = @import("renderer/Thread.zig"); pub const State = @import("renderer/State.zig"); +/// Possible implementations, used for build options. +pub const Impl = enum { + opengl, + metal, + webgl, + + pub fn default( + target: std.zig.CrossTarget, + wasm_target: WasmTarget, + ) Impl { + if (target.getCpuArch() == .wasm32) { + return switch (wasm_target) { + .browser => .webgl, + }; + } + + if (target.isDarwin()) return .metal; + return .opengl; + } +}; + /// The implementation to use for the renderer. This is comptime chosen /// so that every build has exactly one renderer implementation. -const wasm_target = @import("os/wasm/target.zig"); -pub const Renderer = if (wasm_target.target) |target| switch (target) { - .browser => WebGL, -} else switch (builtin.os.tag) { - .macos => Metal, - else => OpenGL, +pub const Renderer = switch (build_config.renderer) { + .metal => Metal, + .opengl => OpenGL, + .webgl => WebGL, }; test {