gpu: implement Device.setLostCallback

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-03-18 18:46:00 -07:00 committed by Stephen Gutekanst
parent 672b3a5601
commit a0d28a74b0
2 changed files with 53 additions and 7 deletions

View file

@ -75,9 +75,7 @@ pub const VTable = struct {
loseForTesting: fn (ptr: *anyopaque) void, loseForTesting: fn (ptr: *anyopaque) void,
popErrorScope: fn (ptr: *anyopaque, callback: *ErrorCallback) bool, popErrorScope: fn (ptr: *anyopaque, callback: *ErrorCallback) bool,
pushErrorScope: fn (ptr: *anyopaque, filter: ErrorFilter) void, pushErrorScope: fn (ptr: *anyopaque, filter: ErrorFilter) void,
// TODO: callback setLostCallback: fn (ptr: *anyopaque, callback: *LostCallback) void,
// setDeviceLostCallback: fn (ptr: *anyopaque, callback: DeviceLostCallback) void,
// WGPU_EXPORT void wgpuDeviceSetDeviceLostCallback(WGPUDevice device, WGPUDeviceLostCallback callback, void * userdata);
// TODO: callback // TODO: callback
// setLoggingCallback: fn (ptr: *anyopaque, callback: LoggingCallback) void, // setLoggingCallback: fn (ptr: *anyopaque, callback: LoggingCallback) void,
// WGPU_EXPORT void wgpuDeviceSetLoggingCallback(WGPUDevice device, WGPULoggingCallback callback, void * userdata); // WGPU_EXPORT void wgpuDeviceSetLoggingCallback(WGPUDevice device, WGPULoggingCallback callback, void * userdata);
@ -113,6 +111,32 @@ pub inline fn pushErrorScope(device: Device, filter: ErrorFilter) void {
device.vtable.pushErrorScope(device.ptr, filter); device.vtable.pushErrorScope(device.ptr, filter);
} }
pub inline fn setLostCallback(device: Device, callback: *LostCallback) void {
device.vtable.setLostCallback(device.ptr, callback);
}
pub const LostCallback = struct {
type_erased_ctx: *anyopaque,
type_erased_callback: fn (ctx: *anyopaque, reason: LostReason, message: [*:0]const u8) callconv(.Inline) void,
pub fn init(
comptime Context: type,
ctx: *Context,
comptime callback: fn (ctx: *Context, reason: LostReason, message: [*:0]const u8) void,
) LostCallback {
const erased = (struct {
pub inline fn erased(type_erased_ctx: *anyopaque, reason: LostReason, message: [*:0]const u8) void {
callback(@ptrCast(*Context, @alignCast(@alignOf(*Context), type_erased_ctx)), reason, message);
}
}).erased;
return .{
.type_erased_ctx = ctx,
.type_erased_callback = erased,
};
}
};
pub inline fn createBindGroup(device: Device, descriptor: *const BindGroup.Descriptor) BindGroup { pub inline fn createBindGroup(device: Device, descriptor: *const BindGroup.Descriptor) BindGroup {
return device.vtable.createBindGroup(device.ptr, descriptor); return device.vtable.createBindGroup(device.ptr, descriptor);
} }
@ -223,6 +247,7 @@ test {
_ = injectError; _ = injectError;
_ = loseForTesting; _ = loseForTesting;
_ = popErrorScope; _ = popErrorScope;
_ = setLostCallback;
_ = createBindGroup; _ = createBindGroup;
_ = pushErrorScope; _ = pushErrorScope;
_ = createBindGroupLayout; _ = createBindGroupLayout;

View file

@ -335,10 +335,7 @@ const device_vtable = Device.VTable{
} }
}).loseForTesting, }).loseForTesting,
.popErrorScope = (struct { .popErrorScope = (struct {
pub fn popErrorScope( pub fn popErrorScope(ptr: *anyopaque, callback: *ErrorCallback) bool {
ptr: *anyopaque,
callback: *ErrorCallback,
) bool {
const cCallback = (struct { const cCallback = (struct {
pub fn cCallback( pub fn cCallback(
typ: c.WGPUErrorType, typ: c.WGPUErrorType,
@ -410,6 +407,30 @@ const device_vtable = Device.VTable{
c.wgpuDevicePushErrorScope(@ptrCast(c.WGPUDevice, ptr), @enumToInt(filter)); c.wgpuDevicePushErrorScope(@ptrCast(c.WGPUDevice, ptr), @enumToInt(filter));
} }
}).pushErrorScope, }).pushErrorScope,
.setLostCallback = (struct {
pub fn setLostCallback(ptr: *anyopaque, callback: *Device.LostCallback) void {
const cCallback = (struct {
pub fn cCallback(
reason: c.WGPUDeviceLostReason,
message: [*c]const u8,
userdata: ?*anyopaque,
) callconv(.C) void {
const callback_info = @ptrCast(*Device.LostCallback, @alignCast(@alignOf(*Device.LostCallback), userdata));
callback_info.type_erased_callback(
callback_info.type_erased_ctx,
@intToEnum(Device.LostReason, reason),
std.mem.span(message),
);
}
}).cCallback;
c.wgpuDeviceSetDeviceLostCallback(
@ptrCast(c.WGPUDevice, ptr),
cCallback,
callback,
);
}
}).setLostCallback,
.createBindGroupLayout = (struct { .createBindGroupLayout = (struct {
pub fn createBindGroupLayout(ptr: *anyopaque, descriptor: *const BindGroupLayout.Descriptor) BindGroupLayout { pub fn createBindGroupLayout(ptr: *anyopaque, descriptor: *const BindGroupLayout.Descriptor) BindGroupLayout {
const desc = c.WGPUBindGroupLayoutDescriptor{ const desc = c.WGPUBindGroupLayoutDescriptor{