gpu: improve type naming for next_in_chain extension types

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-08-16 20:12:25 -07:00
parent 26755e0837
commit 52520d04ef
4 changed files with 24 additions and 24 deletions

View file

@ -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,
// ...

View file

@ -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;

View file

@ -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,
};

View file

@ -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,
};