From c02578721a2aa695a7c80802ab8903d4c3f75e41 Mon Sep 17 00:00:00 2001 From: Austin Rude Date: Thu, 12 Jan 2023 15:21:25 -0700 Subject: [PATCH] gpu: Update example to latest zig/glfw Update the mach/gpu only example to work with the latest zig and recent glfw error handling changes. --- libs/gpu/build.zig | 4 ++-- libs/gpu/examples/main.zig | 4 ++-- libs/gpu/examples/sample_utils.zig | 31 +++++++++++++++++++++--------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/libs/gpu/build.zig b/libs/gpu/build.zig index 3c987a30..41034166 100644 --- a/libs/gpu/build.zig +++ b/libs/gpu/build.zig @@ -28,8 +28,8 @@ pub fn build(b: *std.build.Builder) !void { const example = b.addExecutable("gpu-hello-triangle", "examples/main.zig"); example.setBuildMode(mode); example.setTarget(target); - example.addPackage(gpu.pkg(b)); - example.addPackage(glfw.pkg(b)); + example.addPackage(gpu.pkg); + example.addPackage(glfw.pkg); try gpu.link(b, example, .{ .gpu_dawn_options = gpu_dawn_options }); example.install(); diff --git a/libs/gpu/examples/main.zig b/libs/gpu/examples/main.zig index 23e5ecbe..5e79a9a3 100644 --- a/libs/gpu/examples/main.zig +++ b/libs/gpu/examples/main.zig @@ -11,7 +11,7 @@ pub fn main() !void { gpu.Impl.init(); const setup = try sample_utils.setup(allocator); - const framebuffer_size = try setup.window.getFramebufferSize(); + const framebuffer_size = setup.window.getFramebufferSize(); const window_data = try allocator.create(WindowData); window_data.* = .{ @@ -133,7 +133,7 @@ fn frame(params: FrameParams) !void { const pool = try sample_utils.AutoReleasePool.init(); defer sample_utils.AutoReleasePool.release(pool); - try glfw.pollEvents(); + glfw.pollEvents(); const pl = params.window.getUserPointer(WindowData).?; if (pl.swap_chain == null or !std.meta.eql(pl.current_desc, pl.target_desc)) { pl.swap_chain = params.device.createSwapChain(pl.surface, &pl.target_desc); diff --git a/libs/gpu/examples/sample_utils.zig b/libs/gpu/examples/sample_utils.zig index 84a39ffb..9cb4fd58 100644 --- a/libs/gpu/examples/sample_utils.zig +++ b/libs/gpu/examples/sample_utils.zig @@ -73,14 +73,22 @@ inline fn requestAdapterCallback( pub fn setup(allocator: std.mem.Allocator) !Setup { const backend_type = try detectBackendType(allocator); - try glfw.init(.{}); + glfw.setErrorCallback(errorCallback); + if (!glfw.init(.{})) { + std.log.err("failed to initialize GLFW: {?s}", .{glfw.getErrorString()}); + std.process.exit(1); + } // Create the test window and discover adapters using it (esp. for OpenGL) var hints = glfwWindowHintsForBackend(backend_type); hints.cocoa_retina_framebuffer = true; - const window = try glfw.Window.create(640, 480, "mach/gpu window", null, null, hints); - if (backend_type == .opengl) try glfw.makeContextCurrent(window); - if (backend_type == .opengles) try glfw.makeContextCurrent(window); + const window = glfw.Window.create(640, 480, "mach/gpu window", null, null, hints) orelse { + std.log.err("failed to create GLFW window: {?s}", .{glfw.getErrorString()}); + std.process.exit(1); + }; + + if (backend_type == .opengl) glfw.makeContextCurrent(window); + if (backend_type == .opengles) glfw.makeContextCurrent(window); const instance = gpu.createInstance(null); if (instance == null) { @@ -229,16 +237,21 @@ pub const AutoReleasePool = if (!@import("builtin").target.isDarwin()) opaque { } }; +/// Default GLFW error handling callback +fn errorCallback(error_code: glfw.ErrorCode, description: [:0]const u8) void { + std.log.err("glfw: {}: {s}\n", .{ error_code, description }); +} + // Borrowed from https://github.com/hazeycode/zig-objcrt pub fn msgSend(obj: anytype, sel_name: [:0]const u8, args: anytype, comptime ReturnType: type) ReturnType { const args_meta = @typeInfo(@TypeOf(args)).Struct.fields; const FnType = switch (args_meta.len) { 0 => *const fn (@TypeOf(obj), objc.SEL) callconv(.C) ReturnType, - 1 => *const fn (@TypeOf(obj), objc.SEL, args_meta[0].field_type) callconv(.C) ReturnType, - 2 => *const fn (@TypeOf(obj), objc.SEL, args_meta[0].field_type, args_meta[1].field_type) callconv(.C) ReturnType, - 3 => *const fn (@TypeOf(obj), objc.SEL, args_meta[0].field_type, args_meta[1].field_type, args_meta[2].field_type) callconv(.C) ReturnType, - 4 => *const fn (@TypeOf(obj), objc.SEL, args_meta[0].field_type, args_meta[1].field_type, args_meta[2].field_type, args_meta[3].field_type) callconv(.C) ReturnType, + 1 => *const fn (@TypeOf(obj), objc.SEL, args_meta[0].type) callconv(.C) ReturnType, + 2 => *const fn (@TypeOf(obj), objc.SEL, args_meta[0].type, args_meta[1].type) callconv(.C) ReturnType, + 3 => *const fn (@TypeOf(obj), objc.SEL, args_meta[0].type, args_meta[1].type, args_meta[2].type) callconv(.C) ReturnType, + 4 => *const fn (@TypeOf(obj), objc.SEL, args_meta[0].type, args_meta[1].type, args_meta[2].type, args_meta[3].type) callconv(.C) ReturnType, else => @compileError("Unsupported number of args"), }; @@ -246,5 +259,5 @@ pub fn msgSend(obj: anytype, sel_name: [:0]const u8, args: anytype, comptime Ret var func = @ptrCast(FnType, &objc.objc_msgSend); const sel = objc.sel_getUid(@ptrCast([*c]const u8, sel_name)); - return @call(.{}, func, .{ obj, sel } ++ args); + return @call(.auto, func, .{ obj, sel } ++ args); }