core: cleanup sysgpu feature flag logic

* `@import("mach").core.gpu` has been renamed to `@import("mach").gpu`
* `pub const SYSGPUInterface` now has a default value (i.e. you do not need to write it in your main.zig, if you were.)
* You can now check `if (comptime mach.use_sysgpu)` for any conditional code you might have that should only run with sysgpu.

This (old):

```
pub const mach_core_options = core.ComptimeOptions{
    .use_wgpu = false,
    .use_sysgpu = true,
};
```

Has been replaced by this:

```
pub const use_sysgpu = true;
```

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-04-19 20:48:33 -07:00
parent 6a2358baf8
commit 7a1efdaa69
32 changed files with 85 additions and 132 deletions

View file

@ -34,8 +34,8 @@ should_exit: bool = false,
fn init(core: *Mod) !void { fn init(core: *Mod) !void {
// Initialize GPU implementation // Initialize GPU implementation
if (comptime mach.core.options.use_wgpu) try mach.core.wgpu.Impl.init(mach.core.allocator, .{}); if (comptime !mach.use_sysgpu) try mach.wgpu.Impl.init(mach.core.allocator, .{});
if (comptime mach.core.options.use_sysgpu) try mach.core.sysgpu.Impl.init(mach.core.allocator, .{}); if (comptime mach.use_sysgpu) try mach.sysgpu.Impl.init(mach.core.allocator, .{});
mach.core.allocator = gpa.allocator(); // TODO: banish this global allocator mach.core.allocator = gpa.allocator(); // TODO: banish this global allocator
try mach.core.init(.{}); try mach.core.init(.{});

View file

@ -2,6 +2,7 @@ const std = @import("std");
const mach = @import("mach"); const mach = @import("mach");
const core = mach.core; const core = mach.core;
const gpu = mach.gpu;
const zm = @import("zmath"); const zm = @import("zmath");
const zigimg = @import("zigimg"); const zigimg = @import("zigimg");

View file

@ -4,7 +4,7 @@ const std = @import("std");
const mach = @import("mach"); const mach = @import("mach");
const core = mach.core; const core = mach.core;
const gpu = core.gpu; const gpu = mach.gpu;
title_timer: core.Timer, title_timer: core.Timer,
timer: core.Timer, timer: core.Timer,
@ -18,10 +18,8 @@ frame_counter: usize,
pub const App = @This(); pub const App = @This();
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};

View file

@ -2,16 +2,14 @@ const std = @import("std");
const mach = @import("mach"); const mach = @import("mach");
const core = mach.core; const core = mach.core;
const gpu = core.gpu; const gpu = mach.gpu;
const renderer = @import("renderer.zig"); const renderer = @import("renderer.zig");
pub const App = @This(); pub const App = @This();
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};

View file

@ -27,10 +27,8 @@ depth_texture_view: *gpu.TextureView,
pub const App = @This(); pub const App = @This();
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
pub fn init(app: *App) !void { pub fn init(app: *App) !void {
try core.init(.{}); try core.init(.{});

View file

@ -11,10 +11,8 @@ const VertexWriter = @import("vertex_writer.zig").VertexWriter;
pub const App = @This(); pub const App = @This();
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
const Vec2 = [2]f32; const Vec2 = [2]f32;
const Vec3 = [3]f32; const Vec3 = [3]f32;

View file

@ -20,10 +20,8 @@ const vertices = @import("cube_mesh.zig").vertices;
pub const App = @This(); pub const App = @This();
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
const UniformBufferObject = struct { const UniformBufferObject = struct {
mat: zm.Mat, mat: zm.Mat,

View file

@ -19,10 +19,8 @@ const Quat = zm.Quat;
pub const App = @This(); pub const App = @This();
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};

View file

@ -22,10 +22,8 @@ img_size: gpu.Extent3D,
pub const App = @This(); pub const App = @This();
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
// Constants from the blur.wgsl shader // Constants from the blur.wgsl shader
const tile_dimension: u32 = 128; const tile_dimension: u32 = 128;

View file

@ -7,10 +7,8 @@ const gpu = mach.gpu;
const zigimg = @import("zigimg"); const zigimg = @import("zigimg");
const assets = @import("assets"); const assets = @import("assets");
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
title_timer: core.Timer, title_timer: core.Timer,
pipeline: *gpu.RenderPipeline, pipeline: *gpu.RenderPipeline,

View file

@ -24,10 +24,8 @@ bind_group: *gpu.BindGroup,
pub const App = @This(); pub const App = @This();
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
pub fn init(app: *App) !void { pub fn init(app: *App) !void {
try core.init(.{}); try core.init(.{});

View file

@ -6,10 +6,8 @@ const gpu = mach.gpu;
pub const App = @This(); pub const App = @This();
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};

View file

@ -11,10 +11,8 @@ const VertexWriter = @import("vertex_writer.zig").VertexWriter;
pub const App = @This(); pub const App = @This();
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
const Vec4 = [4]f32; const Vec4 = [4]f32;
const Vec3 = [3]f32; const Vec3 = [3]f32;

View file

@ -13,10 +13,8 @@ const quad = @import("quad_mesh.zig").quad;
pub const App = @This(); pub const App = @This();
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
const pixel_size = 8; const pixel_size = 8;

View file

@ -8,10 +8,9 @@ const renderer = @import("renderer.zig");
pub const App = @This(); pub const App = @This();
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
title_timer: core.Timer, title_timer: core.Timer,

View file

@ -16,10 +16,8 @@ const vertices = [_]Vertex{
}; };
const index_data = [_]u32{ 0, 1, 2, 2, 3, 0 }; const index_data = [_]u32{ 0, 1, 2, 2, 3, 0 };
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
pub const App = @This(); pub const App = @This();

View file

@ -10,10 +10,8 @@ const vertices = @import("cube_mesh.zig").vertices;
pub const App = @This(); pub const App = @This();
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const UniformBufferObject = struct { const UniformBufferObject = struct {

View file

@ -11,10 +11,8 @@ const json = std.json;
pub const App = @This(); pub const App = @This();
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
const speed = 2.0 * 100.0; // pixels per second const speed = 2.0 * 100.0; // pixels per second

View file

@ -12,10 +12,8 @@ const assets = @import("assets");
pub const App = @This(); pub const App = @This();
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
const UniformBufferObject = struct { const UniformBufferObject = struct {
mat: zm.Mat, mat: zm.Mat,

View file

@ -22,10 +22,8 @@ const vertices = [_]Vertex{
}; };
const index_data = [_]u32{ 0, 1, 2, 2, 3, 0 }; const index_data = [_]u32{ 0, 1, 2, 2, 3, 0 };
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};

View file

@ -6,10 +6,8 @@ const gpu = mach.gpu;
pub const App = @This(); pub const App = @This();
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
title_timer: core.Timer, title_timer: core.Timer,
pipeline: *gpu.RenderPipeline, pipeline: *gpu.RenderPipeline,

View file

@ -6,10 +6,8 @@ const gpu = mach.gpu;
pub const App = @This(); pub const App = @This();
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
title_timer: core.Timer, title_timer: core.Timer,
pipeline: *gpu.RenderPipeline, pipeline: *gpu.RenderPipeline,

View file

@ -2,7 +2,7 @@ const std = @import("std");
const mach = @import("mach"); const mach = @import("mach");
const core = mach.core; const core = mach.core;
const gpu = core.gpu; const gpu = mach.gpu;
const zm = @import("zmath"); const zm = @import("zmath");
const Vertex = @import("cube_mesh.zig").Vertex; const Vertex = @import("cube_mesh.zig").Vertex;
@ -24,10 +24,8 @@ bind_group2: *gpu.BindGroup,
pub const App = @This(); pub const App = @This();
pub const mach_core_options = core.ComptimeOptions{ // Use experimental sysgpu graphics API
.use_wgpu = false, pub const use_sysgpu = true;
.use_sysgpu = true,
};
pub fn init(app: *App) !void { pub fn init(app: *App) !void {
try core.init(.{}); try core.init(.{});

View file

@ -44,30 +44,7 @@ fn ErrorSet(comptime F: type) type {
return @typeInfo(@typeInfo(F).Fn.return_type.?).ErrorUnion.error_set; return @typeInfo(@typeInfo(F).Fn.return_type.?).ErrorUnion.error_set;
} }
/// Comptime options that you can configure in your main file by writing e.g.: const gpu = if (mach.use_sysgpu) sysgpu.sysgpu else @import("../gpu/main.zig");
///
/// ```
/// pub const mach_core_options = core.ComptimeOptions{
/// .use_wgpu = true,
/// .use_sysgpu = true,
/// };
/// ```
pub const ComptimeOptions = struct {
/// Whether to use
use_wgpu: bool = true,
/// Whether or not to use the experimental sysgpu graphics API.
use_sysgpu: bool = false,
};
pub const options = if (@hasDecl(@import("root"), "mach_core_options"))
@import("root").mach_core_options
else
ComptimeOptions{};
pub const wgpu = @import("../gpu/main.zig");
pub const gpu = if (options.use_sysgpu) sysgpu.sysgpu else wgpu;
pub fn AppInterface(comptime app_entry: anytype) void { pub fn AppInterface(comptime app_entry: anytype) void {
if (!@hasDecl(app_entry, "App")) { if (!@hasDecl(app_entry, "App")) {

View file

@ -3,7 +3,7 @@ const builtin = @import("builtin");
const mach = @import("../../main.zig"); const mach = @import("../../main.zig");
const gamemode = mach.gamemode; const gamemode = mach.gamemode;
const mach_core = mach.core; const mach_core = mach.core;
const gpu = mach_core.gpu; const gpu = mach.gpu;
pub inline fn printUnhandledErrorCallback(_: void, ty: gpu.ErrorType, message: [*:0]const u8) void { pub inline fn printUnhandledErrorCallback(_: void, ty: gpu.ErrorType, message: [*:0]const u8) void {
switch (ty) { switch (ty) {

View file

@ -1,8 +1,9 @@
const builtin = @import("builtin"); const builtin = @import("builtin");
const std = @import("std"); const std = @import("std");
const glfw = @import("mach-glfw"); const glfw = @import("mach-glfw");
const mach = @import("../../../main.zig");
const mach_core = @import("../../main.zig"); const mach_core = @import("../../main.zig");
const gpu = mach_core.gpu; const gpu = mach.gpu;
const objc = @import("objc.zig"); const objc = @import("objc.zig");
const Options = @import("../../main.zig").Options; const Options = @import("../../main.zig").Options;
const Event = @import("../../main.zig").Event; const Event = @import("../../main.zig").Event;
@ -136,11 +137,11 @@ pub fn init(
input: *Frequency, input: *Frequency,
options: Options, options: Options,
) !void { ) !void {
if (!@import("builtin").is_test and mach_core.options.use_wgpu) _ = mach_core.wgpu.Export(blk: { if (!@import("builtin").is_test and !mach.use_sysgpu) _ = mach.wgpu.Export(blk: {
if (@hasDecl(@import("root"), "GPUInterface")) break :blk @import("root").GPUInterface; if (@hasDecl(@import("root"), "GPUInterface")) break :blk @import("root").GPUInterface;
break :blk mach_core.wgpu.dawn.Interface; break :blk mach.wgpu.dawn.Interface;
}); });
if (!@import("builtin").is_test and mach_core.options.use_sysgpu) _ = mach_core.sysgpu.sysgpu.Export(@import("root").SYSGPUInterface); if (!@import("builtin").is_test and mach.use_sysgpu) _ = mach.sysgpu.sysgpu.Export(@import("root").SYSGPUInterface);
const backend_type = try detectBackendType(allocator); const backend_type = try detectBackendType(allocator);

View file

@ -8,14 +8,16 @@ pub usingnamespace @import("app");
const App = @import("app").App; const App = @import("app").App;
const std = @import("std"); const std = @import("std");
const core = @import("mach").core;
const mach = @import("mach");
const core = mach.core;
pub usingnamespace if (!@hasDecl(App, "GPUInterface")) struct { pub usingnamespace if (!@hasDecl(App, "GPUInterface")) struct {
pub const GPUInterface = core.wgpu.dawn.Interface; pub const GPUInterface = mach.wgpu.dawn.Interface;
} else struct {}; } else struct {};
pub usingnamespace if (!@hasDecl(App, "SYSGPUInterface")) extern struct { pub usingnamespace if (!@hasDecl(App, "SYSGPUInterface")) extern struct {
pub const SYSGPUInterface = core.sysgpu.Impl; pub const SYSGPUInterface = mach.sysgpu.Impl;
} else struct {}; } else struct {};
pub fn main() !void { pub fn main() !void {
@ -29,8 +31,8 @@ pub fn main() !void {
core.allocator = gpa.allocator(); core.allocator = gpa.allocator();
// Initialize GPU implementation // Initialize GPU implementation
if (comptime core.options.use_wgpu) try core.wgpu.Impl.init(core.allocator, .{}); if (comptime !mach.use_sysgpu) try mach.wgpu.Impl.init(core.allocator, .{});
if (comptime core.options.use_sysgpu) try core.sysgpu.Impl.init(core.allocator, .{}); if (comptime mach.use_sysgpu) try mach.sysgpu.Impl.init(core.allocator, .{});
var app: App = undefined; var app: App = undefined;
try app.init(); try app.init();

View file

@ -1,8 +1,9 @@
const std = @import("std"); const std = @import("std");
const js = @import("js.zig"); const js = @import("js.zig");
const Timer = @import("Timer.zig"); const Timer = @import("Timer.zig");
const mach = @import("../../../main.zig");
const mach_core = @import("../../main.zig"); const mach_core = @import("../../main.zig");
const gpu = mach_core.gpu; const gpu = mach.gpu;
const Options = @import("../../main.zig").Options; const Options = @import("../../main.zig").Options;
const Event = @import("../../main.zig").Event; const Event = @import("../../main.zig").Event;
const KeyEvent = @import("../../main.zig").KeyEvent; const KeyEvent = @import("../../main.zig").KeyEvent;

View file

@ -8,8 +8,10 @@ pub usingnamespace @import("app");
const App = @import("app").App; const App = @import("app").App;
const std = @import("std"); const std = @import("std");
const core = @import("mach").core;
const gpu = core.gpu; const mach = @import("mach");
const core = mach.core;
const gpu = mach.gpu;
pub const GPUInterface = gpu.StubInterface; pub const GPUInterface = gpu.StubInterface;

View file

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const mach = @import("../../../main.zig");
const mach_core = @import("../../main.zig"); const mach_core = @import("../../main.zig");
const gpu = mach_core.gpu; const gpu = mach.gpu;
const Options = @import("../../main.zig").Options; const Options = @import("../../main.zig").Options;
const Event = @import("../../main.zig").Event; const Event = @import("../../main.zig").Event;
const KeyEvent = @import("../../main.zig").KeyEvent; const KeyEvent = @import("../../main.zig").KeyEvent;

View file

@ -1,8 +1,9 @@
const builtin = @import("builtin"); const builtin = @import("builtin");
const std = @import("std"); const std = @import("std");
const glfw = @import("mach-glfw"); const glfw = @import("mach-glfw");
const mach = @import("../../../main.zig");
const mach_core = @import("../../main.zig"); const mach_core = @import("../../main.zig");
const gpu = mach_core.gpu; const gpu = mach.gpu;
const unicode = @import("unicode.zig"); const unicode = @import("unicode.zig");
const Options = @import("../../main.zig").Options; const Options = @import("../../main.zig").Options;
const Event = @import("../../main.zig").Event; const Event = @import("../../main.zig").Event;
@ -299,11 +300,11 @@ pub fn init(
input: *Frequency, input: *Frequency,
options: Options, options: Options,
) !void { ) !void {
if (!@import("builtin").is_test and mach_core.options.use_wgpu) _ = mach_core.wgpu.Export(blk: { if (!@import("builtin").is_test and !mach.use_sysgpu) _ = mach.wgpu.Export(blk: {
if (@hasDecl(@import("root"), "GPUInterface")) break :blk @import("root").GPUInterface; if (@hasDecl(@import("root"), "GPUInterface")) break :blk @import("root").GPUInterface;
break :blk mach_core.wgpu.dawn.Interface; break :blk mach.wgpu.dawn.Interface;
}); });
if (!@import("builtin").is_test and mach_core.options.use_sysgpu) _ = mach_core.sysgpu.sysgpu.Export(@import("root").SYSGPUInterface); if (!@import("builtin").is_test and mach.use_sysgpu) _ = mach.sysgpu.sysgpu.Export(@import("root").SYSGPUInterface);
const libx11 = try LibX11.load(); const libx11 = try LibX11.load();
const libxcursor: ?LibXCursor = LibXCursor.load() catch |err| switch (err) { const libxcursor: ?LibXCursor = LibXCursor.load() catch |err| switch (err) {

View file

@ -4,7 +4,7 @@ const builtin = @import("builtin");
// Core // Core
pub const core = if (build_options.want_core) @import("core/main.zig") else struct {}; pub const core = if (build_options.want_core) @import("core/main.zig") else struct {};
pub const Timer = if (build_options.want_core) core.Timer else struct {}; pub const Timer = if (build_options.want_core) core.Timer else struct {};
pub const gpu = if (build_options.want_core) core.gpu else struct {}; pub const wgpu = if (build_options.want_core) @import("gpu/main.zig") else struct {};
pub const sysjs = if (build_options.want_core) @import("mach-sysjs") else struct {}; pub const sysjs = if (build_options.want_core) @import("mach-sysjs") else struct {};
pub const Core = if (build_options.want_core) @import("Core.zig") else struct {}; pub const Core = if (build_options.want_core) @import("Core.zig") else struct {};
@ -32,6 +32,14 @@ pub const Mod = ModSet(modules).Mod;
pub const EntityID = @import("module/main.zig").EntityID; // TODO: rename to just Entity? pub const EntityID = @import("module/main.zig").EntityID; // TODO: rename to just Entity?
pub const Archetype = @import("module/main.zig").Archetype; pub const Archetype = @import("module/main.zig").Archetype;
/// To use experimental sysgpu graphics API, you can write this in your main.zig:
///
/// ```
/// pub const use_sysgpu = true;
/// ```
pub const use_sysgpu = if (@hasDecl(@import("root"), "use_sysgpu")) @import("root").use_sysgpu else false;
pub const gpu = if (use_sysgpu) sysgpu.sysgpu else wgpu;
test { test {
const std = @import("std"); const std = @import("std");
// TODO: refactor code so we can use this here: // TODO: refactor code so we can use this here: