From 52520d04ef66375bfd861842a1ff9552dadfae9b Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Tue, 16 Aug 2022 20:12:25 -0700 Subject: [PATCH] gpu: improve type naming for next_in_chain extension types Signed-off-by: Stephen Gutekanst --- gpu/README.md | 2 +- gpu/examples/sample_utils.zig | 6 +++--- gpu/src/shader_module.zig | 14 +++++++------- gpu/src/surface.zig | 26 +++++++++++++------------- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/gpu/README.md b/gpu/README.md index 5fdfcb54..7eebd593 100644 --- a/gpu/README.md +++ b/gpu/README.md @@ -218,7 +218,7 @@ Here `gpu.Surface.Descriptor` is a concrete type. The `next_in_chain` field is s Complexity aside, `next_in_chain` is not type safe! It cannot be, because such an extension could be implementation-specific. To make this safer, we instead change the `next_in_chain` field type to be a union, where one option is the type-unsafe `generic` pointer, and the other options are known extensions: ```zig -pub const Extension = extern union { +pub const NextInChain = extern union { generic: ?*const ChainedStruct, from_windows_hwnd: *const DescriptorFromWindowsHWND, // ... diff --git a/gpu/examples/sample_utils.zig b/gpu/examples/sample_utils.zig index 270c563d..19dd2e72 100644 --- a/gpu/examples/sample_utils.zig +++ b/gpu/examples/sample_utils.zig @@ -170,12 +170,12 @@ pub fn createSurfaceForWindow( comptime glfw_options: glfw.BackendOptions, ) *gpu.Surface { const glfw_native = glfw.Native(glfw_options); - const extension = if (glfw_options.win32) gpu.Surface.Extension{ + const extension = if (glfw_options.win32) gpu.Surface.Descriptor.NextInChain{ .from_windows_hwnd = &.{ .hinstance = std.os.windows.kernel32.GetModuleHandleW(null).?, .hwnd = glfw_native.getWin32Window(window), }, - } else if (glfw_options.x11) gpu.Surface.Extension{ + } else if (glfw_options.x11) gpu.Surface.Descriptor.NextInChain{ .from_xlib_window = &.{ .display = glfw_native.getX11Display(), .window = glfw_native.getX11Window(window), @@ -194,7 +194,7 @@ pub fn createSurfaceForWindow( const scale_factor = msgSend(ns_window, "backingScaleFactor", .{}, f64); // [ns_window backingScaleFactor] msgSend(layer.?, "setContentsScale:", .{scale_factor}, void); // [layer setContentsScale:scale_factor] - break :blk gpu.Surface.Extension{ .from_metal_layer = &.{ .layer = layer.? } }; + break :blk gpu.Surface.Descriptor.NextInChain{ .from_metal_layer = &.{ .layer = layer.? } }; } else if (glfw_options.wayland) { @panic("TODO: this example does not support Wayland"); } else unreachable; diff --git a/gpu/src/shader_module.zig b/gpu/src/shader_module.zig index b2fd7561..8dc1b259 100644 --- a/gpu/src/shader_module.zig +++ b/gpu/src/shader_module.zig @@ -5,14 +5,14 @@ const CompilationInfo = @import("types.zig").CompilationInfo; const Impl = @import("interface.zig").Impl; pub const ShaderModule = opaque { - pub const Extension = extern union { - generic: ?*const ChainedStruct, - spirv_descriptor: ?*const SPIRVDescriptor, - wgsl_descriptor: ?*const WGSLDescriptor, - }; - pub const Descriptor = extern struct { - next_in_chain: Extension = .{ .generic = null }, + pub const NextInChain = extern union { + generic: ?*const ChainedStruct, + spirv_descriptor: ?*const SPIRVDescriptor, + wgsl_descriptor: ?*const WGSLDescriptor, + }; + + next_in_chain: NextInChain = .{ .generic = null }, label: ?[*:0]const u8 = null, }; diff --git a/gpu/src/surface.zig b/gpu/src/surface.zig index 8452dab1..ad8d4b1a 100644 --- a/gpu/src/surface.zig +++ b/gpu/src/surface.zig @@ -2,20 +2,20 @@ const ChainedStruct = @import("types.zig").ChainedStruct; const Impl = @import("interface.zig").Impl; pub const Surface = opaque { - pub const Extension = extern union { - generic: ?*const ChainedStruct, - from_android_native_window: *const DescriptorFromAndroidNativeWindow, - from_canvas_html_selector: *const DescriptorFromCanvasHTMLSelector, - from_metal_layer: *const DescriptorFromMetalLayer, - from_wayland_surface: *const DescriptorFromWaylandSurface, - from_windows_core_window: *const DescriptorFromWindowsCoreWindow, - from_windows_hwnd: *const DescriptorFromWindowsHWND, - from_windows_swap_chain_panel: *const DescriptorFromWindowsSwapChainPanel, - from_xlib_window: *const DescriptorFromXlibWindow, - }; - pub const Descriptor = extern struct { - next_in_chain: Extension = .{ .generic = null }, + pub const NextInChain = extern union { + generic: ?*const ChainedStruct, + from_android_native_window: *const DescriptorFromAndroidNativeWindow, + from_canvas_html_selector: *const DescriptorFromCanvasHTMLSelector, + from_metal_layer: *const DescriptorFromMetalLayer, + from_wayland_surface: *const DescriptorFromWaylandSurface, + from_windows_core_window: *const DescriptorFromWindowsCoreWindow, + from_windows_hwnd: *const DescriptorFromWindowsHWND, + from_windows_swap_chain_panel: *const DescriptorFromWindowsSwapChainPanel, + from_xlib_window: *const DescriptorFromXlibWindow, + }; + + next_in_chain: NextInChain = .{ .generic = null }, label: ?[*:0]const u8 = null, };