gpu: use Surface API in example

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-03-06 21:54:27 -07:00 committed by Stephen Gutekanst
parent 8f5e9f89e5
commit 9916ec043b
2 changed files with 27 additions and 39 deletions

View file

@ -2,6 +2,7 @@ const std = @import("std");
const sample_utils = @import("sample_utils.zig"); const sample_utils = @import("sample_utils.zig");
const c = @import("c.zig").c; const c = @import("c.zig").c;
const glfw = @import("glfw"); const glfw = @import("glfw");
const gpu = @import("gpu");
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
@ -37,11 +38,12 @@ pub fn main() !void {
.presentMode = c.WGPUPresentMode_Fifo, .presentMode = c.WGPUPresentMode_Fifo,
.implementation = 0, .implementation = 0,
}; };
window_data.surface = sample_utils.createSurfaceForWindow( const surface = sample_utils.createSurfaceForWindow(
setup.instance, &setup.native_instance,
setup.window, setup.window,
comptime sample_utils.detectGLFWOptions(), comptime sample_utils.detectGLFWOptions(),
); );
window_data.surface = @ptrCast(c.WGPUSurface, surface.ptr);
} else { } else {
const binding = c.machUtilsCreateBinding(setup.backend_type, @ptrCast(*c.GLFWwindow, setup.window.handle), setup.device); const binding = c.machUtilsCreateBinding(setup.backend_type, @ptrCast(*c.GLFWwindow, setup.window.handle), setup.device);
if (binding == null) { if (binding == null) {

View file

@ -160,40 +160,24 @@ pub fn detectGLFWOptions() glfw.BackendOptions {
} }
pub fn createSurfaceForWindow( pub fn createSurfaceForWindow(
instance: c.WGPUInstance, native_instance: *const gpu.NativeInstance,
window: glfw.Window, window: glfw.Window,
comptime glfw_options: glfw.BackendOptions, comptime glfw_options: glfw.BackendOptions,
) c.WGPUSurface { ) gpu.Surface {
const glfw_native = glfw.Native(glfw_options); const glfw_native = glfw.Native(glfw_options);
if (glfw_options.win32) { const descriptor = if (glfw_options.win32) gpu.Surface.Descriptor{
var desc: c.WGPUSurfaceDescriptorFromWindowsHWND = undefined; .windows_hwnd = .{
desc.chain.next = null; .label = "basic surface",
desc.chain.sType = c.WGPUSType_SurfaceDescriptorFromWindowsHWND; .hinstance = std.os.windows.kernel32.GetModuleHandleW(null),
.hwnd = glfw_native.getWin32Window(window),
desc.hinstance = std.os.windows.kernel32.GetModuleHandleW(null); },
desc.hwnd = glfw_native.getWin32Window(window); } else if (glfw_options.x11) gpu.Surface.Descriptor{
.xlib_window = .{
var descriptor: c.WGPUSurfaceDescriptor = undefined; .label = "basic surface",
descriptor.nextInChain = @ptrCast(*c.WGPUChainedStruct, &desc); .display = glfw_native.getX11Display(),
descriptor.label = "basic surface"; .window = glfw_native.getX11Window(window),
return c.wgpuInstanceCreateSurface(instance, &descriptor); },
} else if (glfw_options.x11) { } else if (glfw_options.cocoa) blk: {
var desc: c.WGPUSurfaceDescriptorFromXlibWindow = undefined;
desc.chain.next = null;
desc.chain.sType = c.WGPUSType_SurfaceDescriptorFromXlibWindow;
desc.display = glfw_native.getX11Display();
desc.window = glfw_native.getX11Window(window);
var descriptor: c.WGPUSurfaceDescriptor = undefined;
descriptor.nextInChain = @ptrCast(*c.WGPUChainedStruct, &desc);
descriptor.label = "basic surface";
return c.wgpuInstanceCreateSurface(instance, &descriptor);
} else if (glfw_options.cocoa) {
var desc: c.WGPUSurfaceDescriptorFromMetalLayer = undefined;
desc.chain.next = null;
desc.chain.sType = c.WGPUSType_SurfaceDescriptorFromMetalLayer;
const ns_window = glfw_native.getCocoaWindow(window); const ns_window = glfw_native.getCocoaWindow(window);
const ns_view = msgSend(ns_window, "contentView", .{}, *anyopaque); // [nsWindow contentView] const ns_view = msgSend(ns_window, "contentView", .{}, *anyopaque); // [nsWindow contentView]
@ -207,15 +191,17 @@ pub fn createSurfaceForWindow(
const scale_factor = msgSend(ns_window, "backingScaleFactor", .{}, f64); // [ns_window backingScaleFactor] const scale_factor = msgSend(ns_window, "backingScaleFactor", .{}, f64); // [ns_window backingScaleFactor]
msgSend(layer.?, "setContentsScale:", .{scale_factor}, void); // [layer setContentsScale:scale_factor] msgSend(layer.?, "setContentsScale:", .{scale_factor}, void); // [layer setContentsScale:scale_factor]
desc.layer = layer.?; break :blk gpu.Surface.Descriptor{
.metal_layer = .{
var descriptor: c.WGPUSurfaceDescriptor = undefined; .label = "basic surface",
descriptor.nextInChain = @ptrCast(*c.WGPUChainedStruct, &desc); .layer = layer.?,
descriptor.label = "basic surface"; },
return c.wgpuInstanceCreateSurface(instance, &descriptor); };
} else if (glfw_options.wayland) { } else if (glfw_options.wayland) {
@panic("Dawn does not yet have Wayland support, see https://bugs.chromium.org/p/dawn/issues/detail?id=1246&q=surface&can=2"); @panic("Dawn does not yet have Wayland support, see https://bugs.chromium.org/p/dawn/issues/detail?id=1246&q=surface&can=2");
} else unreachable; } else unreachable;
return native_instance.createSurface(&descriptor);
} }
// Borrowed from https://github.com/hazeycode/zig-objcrt // Borrowed from https://github.com/hazeycode/zig-objcrt