From 64e023c992f7e5f4c7b81fdc2aec79a68183d3ab Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Sun, 6 Mar 2022 20:28:10 -0700 Subject: [PATCH] gpu: prepare to implement Surface Signed-off-by: Stephen Gutekanst --- gpu/src/Interface.zig | 7 +++++- gpu/src/NativeInstance.zig | 27 ++++++++++++++++------ gpu/src/Surface.zig | 46 ++++++++++++++++++++++++++++++++++++++ gpu/src/main.zig | 2 ++ 4 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 gpu/src/Surface.zig diff --git a/gpu/src/Interface.zig b/gpu/src/Interface.zig index e4eff7fe..22553e36 100644 --- a/gpu/src/Interface.zig +++ b/gpu/src/Interface.zig @@ -8,5 +8,10 @@ vtable: *const VTable, pub const VTable = struct { // TODO(gpu): make these *const fn once stage2 is released. - free: fn (ptr: *anyopaque, buf: []u8, buf_align: u29, ret_addr: usize) void, + deinit: fn (ptr: *anyopaque) void, }; + +// 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 a02ae409..da38fe87 100644 --- a/gpu/src/NativeInstance.zig +++ b/gpu/src/NativeInstance.zig @@ -1,18 +1,29 @@ //! A native webgpu.h implementation of the gpu.Interface const c = @import("c.zig").c; const Interface = @import("Interface.zig"); +const Surface = @import("Surface.zig"); const NativeInstance = @This(); /// The WGPUInstance that is wrapped by this native instance. instance: c.WGPUInstance, -/// Returns the gpu.Interface for interacting with this native instance. -pub fn interface(native: NativeInstance) Interface { - _ = native; - @panic("not implemented"); +vtable: Interface.VTable, +/// Wraps a native WGPUInstance to provide an implementation of the gpu.Interface. +pub fn wrap(instance: c.WGPUInstance) NativeInstance { + return .{ .instance = instance }; +} + +/// Returns the gpu.Interface for interacting with this native instance. +pub fn interface(native: *const NativeInstance) Interface { + return .{ + .ptr = native, + .vtable = native.vtable, + }; // TODO: implement Interface + // WGPU_EXPORT void wgpuInstanceReference(WGPUInstance instance); + // WGPU_EXPORT void wgpuInstanceRelease(WGPUInstance instance); // TODO: implement Device interface @@ -25,7 +36,9 @@ pub fn interface(native: NativeInstance) Interface { // WGPU_EXPORT void wgpuAdapterGetProperties(WGPUAdapter adapter, WGPUAdapterProperties * properties); } -/// Wraps a native WGPUInstance to provide an implementation of the gpu.Interface. -pub fn wrap(instance: c.WGPUInstance) NativeInstance { - return .{ .instance = instance }; +pub fn createSurface(native: *const NativeInstance, descriptor: *const Surface.Descriptor) Surface { + _ = native; + _ = descriptor; + // TODO: + // WGPU_EXPORT WGPUSurface wgpuInstanceCreateSurface(WGPUInstance instance, WGPUSurfaceDescriptor const * descriptor); } diff --git a/gpu/src/Surface.zig b/gpu/src/Surface.zig new file mode 100644 index 00000000..262b7e73 --- /dev/null +++ b/gpu/src/Surface.zig @@ -0,0 +1,46 @@ +//! A native WebGPU surface + +// typedef enum WGPUSType { +// WGPUSType_Invalid = 0x00000000, +// WGPUSType_SurfaceDescriptorFromMetalLayer = 0x00000001, +// WGPUSType_SurfaceDescriptorFromWindowsHWND = 0x00000002, +// WGPUSType_SurfaceDescriptorFromXlibWindow = 0x00000003, +// WGPUSType_SurfaceDescriptorFromCanvasHTMLSelector = 0x00000004, + +const Descriptor = struct {}; +// typedef struct WGPUSurfaceDescriptor { +// WGPUChainedStruct const * nextInChain; +// char const * label; +// } WGPUSurfaceDescriptor; + +// typedef struct WGPUSurfaceDescriptorFromCanvasHTMLSelector { +// WGPUChainedStruct chain; +// char const * selector; +// } WGPUSurfaceDescriptorFromCanvasHTMLSelector; + +// typedef struct WGPUSurfaceDescriptorFromMetalLayer { +// WGPUChainedStruct chain; +// void * layer; +// } WGPUSurfaceDescriptorFromMetalLayer; + +// typedef struct WGPUSurfaceDescriptorFromWindowsCoreWindow { +// WGPUChainedStruct chain; +// void * coreWindow; +// } WGPUSurfaceDescriptorFromWindowsCoreWindow; + +// typedef struct WGPUSurfaceDescriptorFromWindowsHWND { +// WGPUChainedStruct chain; +// void * hinstance; +// void * hwnd; +// } WGPUSurfaceDescriptorFromWindowsHWND; + +// typedef struct WGPUSurfaceDescriptorFromWindowsSwapChainPanel { +// WGPUChainedStruct chain; +// void * swapChainPanel; +// } WGPUSurfaceDescriptorFromWindowsSwapChainPanel; + +// typedef struct WGPUSurfaceDescriptorFromXlibWindow { +// WGPUChainedStruct chain; +// void * display; +// uint32_t window; +// } WGPUSurfaceDescriptorFromXlibWindow; diff --git a/gpu/src/main.zig b/gpu/src/main.zig index 7a42857c..8c7342d4 100644 --- a/gpu/src/main.zig +++ b/gpu/src/main.zig @@ -21,6 +21,7 @@ const NativeInstance = @import("NativeInstance.zig"); const Adapter = @import("Adapter.zig"); const Device = @import("Device.zig"); +const Surface = @import("Surface.zig"); const FeatureName = @import("feature_name.zig").FeatureName; const SupportedLimits = @import("supported_limits.zig").SupportedLimits; @@ -31,6 +32,7 @@ test "syntax" { _ = Adapter; _ = Device; + _ = Surface; _ = FeatureName; _ = SupportedLimits;