gpu: make requestDevice callback-based, add waitForDevice helper
Previously we were attempting to turn WebGPU async functions, which are exposed by `webgpu.h` as callbacks, into Zig async functions. This in practice turns out harder than expected. For example, `Buffer.mapAsync` / `wgpuBufferMapAsync` cannot easily be exposed as a Zig async function for a few reasons: 1. The callback is merely guaranteed to be called once the buffer's content is ready to be accessed via `wgpuBufferGetMappedRange` - but there is no strict guarantee about when that is. It could be 1-3 frames later, in theory, I believe. 2. The non-deterministic timing means that one would wish to poll "has the async function returned?" but this isn't trivial without our own scheduler. 3. Zig has a fair amount of async rework in the future that is coming, and I imagine it will be one of the later things that is fully supported by the WebAssembly backend (but I am speculating) - so it seems wise to punt on this until later. Instead, we are now retaining async functions as callback-based ones, with a helper in this case to wait for the callback to be invoked. For `wgpuBufferMapAsync` we will just have the callback approach. Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
890dd57296
commit
aab99b5474
4 changed files with 74 additions and 41 deletions
|
|
@ -12,6 +12,7 @@ const Adapter = @import("Adapter.zig");
|
|||
const RequestDeviceErrorCode = Adapter.RequestDeviceErrorCode;
|
||||
const RequestDeviceError = Adapter.RequestDeviceError;
|
||||
const RequestDeviceResponse = Adapter.RequestDeviceResponse;
|
||||
const RequestDeviceCallback = Adapter.RequestDeviceCallback;
|
||||
|
||||
const Device = @import("Device.zig");
|
||||
const Surface = @import("Surface.zig");
|
||||
|
|
@ -230,7 +231,6 @@ fn wrapAdapter(adapter: c.WGPUAdapter) Adapter {
|
|||
|
||||
.ptr = adapter.?,
|
||||
.vtable = &adapter_vtable,
|
||||
.request_device_frame_size = @frameSize(adapter_vtable.requestDevice),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -246,7 +246,11 @@ const adapter_vtable = Adapter.VTable{
|
|||
}
|
||||
}).release,
|
||||
.requestDevice = (struct {
|
||||
pub fn requestDevice(ptr: *anyopaque, descriptor: *const Device.Descriptor) callconv(.Async) RequestDeviceResponse {
|
||||
pub fn requestDevice(
|
||||
ptr: *anyopaque,
|
||||
descriptor: *const Device.Descriptor,
|
||||
callback: *RequestDeviceCallback,
|
||||
) void {
|
||||
const adapter = @ptrCast(c.WGPUAdapter, @alignCast(@alignOf(c.WGPUAdapter), ptr));
|
||||
|
||||
const required_limits = if (descriptor.required_limits) |l| c.WGPURequiredLimits{
|
||||
|
|
@ -262,14 +266,13 @@ const adapter_vtable = Adapter.VTable{
|
|||
.requiredLimits = if (required_limits) |*l| l else null,
|
||||
};
|
||||
|
||||
const callback = (struct {
|
||||
pub fn callback(status: c.WGPURequestDeviceStatus, device: c.WGPUDevice, message: [*c]const u8, userdata: ?*anyopaque) callconv(.C) void {
|
||||
const _callback_response = @ptrCast(*Adapter.RequestDeviceResponse, @alignCast(@alignOf(*Adapter.RequestDeviceResponse), userdata));
|
||||
const cCallback = (struct {
|
||||
pub fn cCallback(status: c.WGPURequestDeviceStatus, device: c.WGPUDevice, message: [*c]const u8, userdata: ?*anyopaque) callconv(.C) void {
|
||||
const callback_info = @ptrCast(*RequestDeviceCallback, @alignCast(@alignOf(*RequestDeviceCallback), userdata.?));
|
||||
|
||||
// Store the response into a field on the native instance for later reading.
|
||||
_callback_response.* = if (status == c.WGPURequestDeviceStatus_Success) .{
|
||||
const response = if (status == c.WGPURequestDeviceStatus_Success) RequestDeviceResponse{
|
||||
.device = wrapDevice(device.?),
|
||||
} else .{
|
||||
} else RequestDeviceResponse{
|
||||
.err = Adapter.RequestDeviceError{
|
||||
.message = std.mem.span(message),
|
||||
.code = switch (status) {
|
||||
|
|
@ -279,18 +282,12 @@ const adapter_vtable = Adapter.VTable{
|
|||
},
|
||||
},
|
||||
};
|
||||
|
||||
callback_info.type_erased_callback(callback_info.type_erased_ctx, response);
|
||||
}
|
||||
}).callback;
|
||||
}).cCallback;
|
||||
|
||||
var callback_response: Adapter.RequestDeviceResponse = undefined;
|
||||
c.wgpuAdapterRequestDevice(adapter, &desc, callback, &callback_response);
|
||||
// TODO: Once crbug.com/dawn/1122 is fixed, we should process events here otherwise our
|
||||
// callback will not be invoked.
|
||||
// c.wgpuInstanceProcessEvents(native.instance)
|
||||
suspend {} // must suspend so that async caller can resume
|
||||
|
||||
// Return the response, asserting the callback has executed at this point.
|
||||
return callback_response;
|
||||
c.wgpuAdapterRequestDevice(adapter, &desc, cCallback, callback);
|
||||
}
|
||||
}).requestDevice,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue