gpu: correctly allow unsetting Device.setLostCallback

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-08-16 21:13:36 -07:00
parent b28a01d2e8
commit 612c4420d6
3 changed files with 17 additions and 13 deletions

View file

@ -260,19 +260,22 @@ pub const Device = opaque {
Impl.devicePushErrorScope(device, filter);
}
// TODO: presumably callback should be nullable for unsetting
pub inline fn setDeviceLostCallback(
device: *Device,
context: anytype,
comptime callback: fn (ctx: @TypeOf(context), reason: LostReason, message: [*:0]const u8) callconv(.Inline) void,
comptime callback: ?fn (ctx: @TypeOf(context), reason: LostReason, message: [*:0]const u8) callconv(.Inline) void,
) void {
const Context = @TypeOf(context);
const Helper = struct {
pub fn callback(reason: LostReason, message: [*:0]const u8, userdata: ?*anyopaque) callconv(.C) void {
callback(if (Context == void) {} else @ptrCast(Context, @alignCast(@alignOf(std.meta.Child(Context)), userdata)), reason, message);
}
};
Impl.deviceSetDeviceLostCallback(device, Helper.callback, if (Context == void) null else context);
if (callback) |cb| {
const Context = @TypeOf(context);
const Helper = struct {
pub fn callback(reason: LostReason, message: [*:0]const u8, userdata: ?*anyopaque) callconv(.C) void {
cb(if (Context == void) {} else @ptrCast(Context, @alignCast(@alignOf(std.meta.Child(Context)), userdata)), reason, message);
}
};
Impl.deviceSetDeviceLostCallback(device, Helper.callback, if (Context == void) null else context);
} else {
Impl.deviceSetDeviceLostCallback(device, null, null);
}
}
pub inline fn setLabel(device: *Device, label: [*:0]const u8) void {