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.
This commit is contained in:
parent
340e886e78
commit
c02578721a
3 changed files with 26 additions and 13 deletions
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue