diff --git a/gpu/src/Interface.zig b/gpu/src/Interface.zig index 22553e36..4c67a081 100644 --- a/gpu/src/Interface.zig +++ b/gpu/src/Interface.zig @@ -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); diff --git a/gpu/src/NativeInstance.zig b/gpu/src/NativeInstance.zig index 75c43db7..15c73230 100644 --- a/gpu/src/NativeInstance.zig +++ b/gpu/src/NativeInstance.zig @@ -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: