gpu: implement Interface.reference, Interface.release

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-03-06 22:22:42 -07:00 committed by Stephen Gutekanst
parent 8d31fae6ee
commit 3f19563354
2 changed files with 30 additions and 16 deletions

View file

@ -2,16 +2,24 @@
//!
//! Like std.mem.Allocator, but representing a WebGPU implementation.
// The type erased pointer to the Device implementation
const Interface = @This();
/// The type erased pointer to the Interface implementation
ptr: *anyopaque,
vtable: *const VTable,
pub const VTable = struct {
// TODO(gpu): make these *const fn once stage2 is released.
deinit: fn (ptr: *anyopaque) void,
reference: fn (ptr: *anyopaque) void,
release: fn (ptr: *anyopaque) void,
};
pub inline fn reference(interface: Interface) void {
interface.vtable.reference(interface.ptr);
}
pub inline fn release(interface: Interface) void {
interface.vtable.release(interface.ptr);
}
// TODO:
// WGPU_EXPORT void wgpuInstanceRequestAdapter(WGPUInstance instance, WGPURequestAdapterOptions const * options, WGPURequestAdapterCallback callback, void * userdata);
// WGPU_EXPORT void wgpuInstanceReference(WGPUInstance instance);
// WGPU_EXPORT void wgpuInstanceRelease(WGPUInstance instance);

View file

@ -8,26 +8,32 @@ const NativeInstance = @This();
/// The WGPUInstance that is wrapped by this native instance.
instance: c.WGPUInstance,
vtable: Interface.VTable,
/// Wraps a native WGPUInstance to provide an implementation of the gpu.Interface.
pub fn wrap(instance: *anyopaque) NativeInstance {
return .{
.instance = @ptrCast(c.WGPUInstance, instance),
.vtable = undefined, // TODO
};
return .{ .instance = @ptrCast(c.WGPUInstance, instance) };
}
const interface_vtable = Interface.VTable{
.reference = (struct {
pub fn reference(ptr: *anyopaque) void {
const native = @ptrCast(*NativeInstance, @alignCast(@alignOf(*NativeInstance), ptr));
c.wgpuInstanceReference(native.instance);
}
}).reference,
.release = (struct {
pub fn release(ptr: *anyopaque) void {
const native = @ptrCast(*NativeInstance, @alignCast(@alignOf(*NativeInstance), ptr));
c.wgpuInstanceRelease(native.instance);
}
}).release,
};
/// Returns the gpu.Interface for interacting with this native instance.
pub fn interface(native: *NativeInstance) Interface {
return .{
.ptr = native,
.vtable = &native.vtable,
.vtable = &interface_vtable,
};
// TODO: implement Interface
// WGPU_EXPORT void wgpuInstanceReference(WGPUInstance instance);
// WGPU_EXPORT void wgpuInstanceRelease(WGPUInstance instance);
// TODO: implement Device interface
// TODO: implement Adapter interface: