{mach,gpu}: add autorelease pool
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
f366244a9a
commit
68d5f6fcb1
4 changed files with 55 additions and 1 deletions
|
|
@ -142,6 +142,9 @@ const FrameParams = struct {
|
||||||
};
|
};
|
||||||
|
|
||||||
fn frame(params: FrameParams) !void {
|
fn frame(params: FrameParams) !void {
|
||||||
|
const pool = try sample_utils.AutoReleasePool.init();
|
||||||
|
defer sample_utils.AutoReleasePool.release(pool);
|
||||||
|
|
||||||
try glfw.pollEvents();
|
try glfw.pollEvents();
|
||||||
const pl = params.window.getUserPointer(WindowData).?;
|
const pl = params.window.getUserPointer(WindowData).?;
|
||||||
if (pl.swap_chain == null or !std.meta.eql(pl.current_desc, pl.target_desc)) {
|
if (pl.swap_chain == null or !std.meta.eql(pl.current_desc, pl.target_desc)) {
|
||||||
|
|
|
||||||
|
|
@ -99,7 +99,7 @@ pub fn setup(allocator: std.mem.Allocator) !Setup {
|
||||||
.force_fallback_adapter = false,
|
.force_fallback_adapter = false,
|
||||||
}, &response, requestAdapterCallback);
|
}, &response, requestAdapterCallback);
|
||||||
if (response.?.status != .success) {
|
if (response.?.status != .success) {
|
||||||
std.debug.print("failed to create GPU adapter: {s}\n", .{response.?.message});
|
std.debug.print("failed to create GPU adapter: {s}\n", .{response.?.message.?});
|
||||||
std.process.exit(1);
|
std.process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -204,6 +204,29 @@ pub fn createSurfaceForWindow(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const AutoReleasePool = opaque {
|
||||||
|
pub fn init() error{OutOfMemory}!?*AutoReleasePool {
|
||||||
|
if (!@import("builtin").target.isDarwin()) return null;
|
||||||
|
|
||||||
|
// pool = [NSAutoreleasePool alloc];
|
||||||
|
var pool = msgSend(objc.objc_getClass("NSAutoreleasePool"), "alloc", .{}, ?*AutoReleasePool);
|
||||||
|
if (pool == null) return error.OutOfMemory;
|
||||||
|
|
||||||
|
// pool = [pool init];
|
||||||
|
pool = msgSend(pool, "init", .{}, ?*AutoReleasePool);
|
||||||
|
if (pool == null) unreachable;
|
||||||
|
|
||||||
|
return pool;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn release(pool: ?*AutoReleasePool) void {
|
||||||
|
if (!@import("builtin").target.isDarwin()) return;
|
||||||
|
|
||||||
|
// [pool release];
|
||||||
|
msgSend(pool, "release", .{}, void);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Borrowed from https://github.com/hazeycode/zig-objcrt
|
// Borrowed from https://github.com/hazeycode/zig-objcrt
|
||||||
pub fn msgSend(obj: anytype, sel_name: [:0]const u8, args: anytype, comptime ReturnType: type) ReturnType {
|
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 args_meta = @typeInfo(@TypeOf(args)).Struct.fields;
|
||||||
|
|
|
||||||
|
|
@ -593,6 +593,11 @@ pub fn main() !void {
|
||||||
defer app.deinit(core);
|
defer app.deinit(core);
|
||||||
|
|
||||||
while (!core.internal.window.shouldClose()) {
|
while (!core.internal.window.shouldClose()) {
|
||||||
|
// On Darwin targets, Dawn requires an NSAutoreleasePool per frame to release
|
||||||
|
// some resources. See Dawn's CHelloWorld example.
|
||||||
|
const pool = try util.AutoReleasePool.init();
|
||||||
|
defer util.AutoReleasePool.release(pool);
|
||||||
|
|
||||||
try coreUpdate(core, null);
|
try coreUpdate(core, null);
|
||||||
|
|
||||||
try app.update(core);
|
try app.update(core);
|
||||||
|
|
|
||||||
|
|
@ -137,6 +137,29 @@ pub fn createSurfaceForWindow(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const AutoReleasePool = opaque {
|
||||||
|
pub fn init() error{OutOfMemory}!?*AutoReleasePool {
|
||||||
|
if (!@import("builtin").target.isDarwin()) return null;
|
||||||
|
|
||||||
|
// pool = [NSAutoreleasePool alloc];
|
||||||
|
var pool = msgSend(objc.objc_getClass("NSAutoreleasePool"), "alloc", .{}, ?*AutoReleasePool);
|
||||||
|
if (pool == null) return error.OutOfMemory;
|
||||||
|
|
||||||
|
// pool = [pool init];
|
||||||
|
pool = msgSend(pool, "init", .{}, ?*AutoReleasePool);
|
||||||
|
if (pool == null) unreachable;
|
||||||
|
|
||||||
|
return pool;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn release(pool: ?*AutoReleasePool) void {
|
||||||
|
if (!@import("builtin").target.isDarwin()) return;
|
||||||
|
|
||||||
|
// [pool release];
|
||||||
|
msgSend(pool, "release", .{}, void);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Borrowed from https://github.com/hazeycode/zig-objcrt
|
// Borrowed from https://github.com/hazeycode/zig-objcrt
|
||||||
fn msgSend(obj: anytype, sel_name: [:0]const u8, args: anytype, comptime ReturnType: type) ReturnType {
|
fn msgSend(obj: anytype, sel_name: [:0]const u8, args: anytype, comptime ReturnType: type) ReturnType {
|
||||||
const args_meta = @typeInfo(@TypeOf(args)).Struct.fields;
|
const args_meta = @typeInfo(@TypeOf(args)).Struct.fields;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue