From 8df0c70c69857ce6ce65a859eb1391ed0e694805 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Fri, 18 Mar 2022 18:21:54 -0700 Subject: [PATCH] gpu: implement Device.popErrorScope Signed-off-by: Stephen Gutekanst --- gpu/src/Device.zig | 10 +++++++--- gpu/src/NativeInstance.zig | 28 ++++++++++++++++++++++++++++ gpu/src/structs.zig | 24 ++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/gpu/src/Device.zig b/gpu/src/Device.zig index a83521e1..d4af19fe 100644 --- a/gpu/src/Device.zig +++ b/gpu/src/Device.zig @@ -10,6 +10,7 @@ const Feature = @import("enums.zig").Feature; const ErrorType = @import("enums.zig").ErrorType; const ErrorFilter = @import("enums.zig").ErrorFilter; const Limits = @import("data.zig").Limits; +const ErrorCallback = @import("structs.zig").ErrorCallback; const Queue = @import("Queue.zig"); const ShaderModule = @import("ShaderModule.zig"); const Surface = @import("Surface.zig"); @@ -72,9 +73,7 @@ pub const VTable = struct { // WGPU_EXPORT bool wgpuDeviceHasFeature(WGPUDevice device, WGPUFeature feature); injectError: fn (ptr: *anyopaque, type: ErrorType, message: [*:0]const u8) void, loseForTesting: fn (ptr: *anyopaque) void, - // TODO: callback - // popErrorScope: fn (ptr: *anyopaque, callback: ErrorCallback) bool, - // WGPU_EXPORT bool wgpuDevicePopErrorScope(WGPUDevice device, WGPUErrorCallback callback, void * userdata); + popErrorScope: fn (ptr: *anyopaque, callback: *ErrorCallback) bool, pushErrorScope: fn (ptr: *anyopaque, filter: ErrorFilter) void, // TODO: callback // setDeviceLostCallback: fn (ptr: *anyopaque, callback: DeviceLostCallback) void, @@ -108,6 +107,10 @@ pub inline fn loseForTesting(device: Device) void { device.vtable.loseForTesting(device.ptr); } +pub inline fn popErrorScope(device: Device, callback: *ErrorCallback) bool { + return device.vtable.popErrorScope(device.ptr, callback); +} + pub inline fn pushErrorScope(device: Device, filter: ErrorFilter) void { device.vtable.pushErrorScope(device.ptr, filter); } @@ -217,6 +220,7 @@ test { _ = getQueue; _ = injectError; _ = loseForTesting; + _ = popErrorScope; _ = createBindGroup; _ = pushErrorScope; _ = createBindGroupLayout; diff --git a/gpu/src/NativeInstance.zig b/gpu/src/NativeInstance.zig index 5c10be7f..792a645a 100644 --- a/gpu/src/NativeInstance.zig +++ b/gpu/src/NativeInstance.zig @@ -49,6 +49,7 @@ const ErrorFilter = @import("enums.zig").ErrorFilter; const ImageCopyBuffer = @import("structs.zig").ImageCopyBuffer; const ImageCopyTexture = @import("structs.zig").ImageCopyTexture; +const ErrorCallback = @import("structs.zig").ErrorCallback; const NativeInstance = @This(); @@ -333,6 +334,33 @@ const device_vtable = Device.VTable{ c.wgpuDeviceLoseForTesting(@ptrCast(c.WGPUDevice, ptr)); } }).loseForTesting, + .popErrorScope = (struct { + pub fn popErrorScope( + ptr: *anyopaque, + callback: *ErrorCallback, + ) bool { + const cCallback = (struct { + pub fn cCallback( + typ: c.WGPUErrorType, + message: [*c]const u8, + userdata: ?*anyopaque, + ) callconv(.C) void { + const callback_info = @ptrCast(*ErrorCallback, @alignCast(@alignOf(*ErrorCallback), userdata)); + callback_info.type_erased_callback( + callback_info.type_erased_ctx, + @intToEnum(ErrorType, typ), + std.mem.span(message), + ); + } + }).cCallback; + + return c.wgpuDevicePopErrorScope( + @ptrCast(c.WGPUDevice, ptr), + cCallback, + callback, + ); + } + }).popErrorScope, .createBindGroup = (struct { pub fn createBindGroup(ptr: *anyopaque, descriptor: *const BindGroup.Descriptor) BindGroup { var few_entries: [16]c.WGPUBindGroupEntry = undefined; diff --git a/gpu/src/structs.zig b/gpu/src/structs.zig index 454cb69e..67eb1d4e 100644 --- a/gpu/src/structs.zig +++ b/gpu/src/structs.zig @@ -21,6 +21,7 @@ const RenderPassTimestampLocation = @import("enums.zig").RenderPassTimestampLoca const LoadOp = @import("enums.zig").LoadOp; const StoreOp = @import("enums.zig").StoreOp; const ColorWriteMask = @import("enums.zig").ColorWriteMask; +const ErrorType = @import("enums.zig").ErrorType; pub const MultisampleState = struct { count: u32, @@ -136,6 +137,28 @@ pub const ImageCopyTexture = struct { aspect: Texture.Aspect, }; +pub const ErrorCallback = struct { + type_erased_ctx: *anyopaque, + type_erased_callback: fn (ctx: *anyopaque, typ: ErrorType, message: [*:0]const u8) callconv(.Inline) void, + + pub fn init( + comptime Context: type, + ctx: *Context, + comptime callback: fn (ctx: *Context, typ: ErrorType, message: [*:0]const u8) void, + ) ErrorCallback { + const erased = (struct { + pub inline fn erased(type_erased_ctx: *anyopaque, typ: ErrorType, message: [*:0]const u8) void { + callback(@ptrCast(*Context, @alignCast(@alignOf(*Context), type_erased_ctx)), typ, message); + } + }).erased; + + return .{ + .type_erased_ctx = ctx, + .type_erased_callback = erased, + }; + } +}; + test { _ = MultisampleState; _ = PrimitiveState; @@ -151,4 +174,5 @@ test { _ = FragmentState; _ = ImageCopyBuffer; _ = ImageCopyTexture; + _ = ErrorCallback; }