gpu: examples: use latest friendlier APIs

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-08-01 00:52:44 -07:00 committed by Stephen Gutekanst
parent ab71c7fd93
commit f856fccb76
2 changed files with 23 additions and 27 deletions

View file

@ -50,10 +50,9 @@ pub fn main() !void {
\\ } \\ }
; ;
const vs_module = setup.device.createShaderModule(&.{ const vs_module = setup.device.createShaderModule(&.{
.next_in_chain = @ptrCast(*const gpu.ChainedStruct, &gpu.ShaderModule.WGSLDescriptor{ .next_in_chain = .{ .wgsl_descriptor = &.{
.chain = .{ .next = null, .s_type = .shader_module_wgsl_descriptor },
.source = vs, .source = vs,
}), } },
.label = "my vertex shader", .label = "my vertex shader",
}); });
@ -63,10 +62,9 @@ pub fn main() !void {
\\ } \\ }
; ;
const fs_module = setup.device.createShaderModule(&.{ const fs_module = setup.device.createShaderModule(&.{
.next_in_chain = @ptrCast(*const gpu.ChainedStruct, &gpu.ShaderModule.WGSLDescriptor{ .next_in_chain = .{ .wgsl_descriptor = &.{
.chain = .{ .next = null, .s_type = .shader_module_wgsl_descriptor },
.source = fs, .source = fs,
}), } },
.label = "my fragment shader", .label = "my fragment shader",
}); });

View file

@ -7,7 +7,7 @@ const objc = @cImport({
@cInclude("objc/message.h"); @cInclude("objc/message.h");
}); });
fn printUnhandledErrorCallback(typ: gpu.ErrorType, message: [*:0]const u8, _: ?*anyopaque) callconv(.C) void { inline fn printUnhandledErrorCallback(typ: gpu.ErrorType, message: [*:0]const u8, _: void) void {
switch (typ) { switch (typ) {
.validation => std.debug.print("gpu: validation error: {s}\n", .{message}), .validation => std.debug.print("gpu: validation error: {s}\n", .{message}),
.out_of_memory => std.debug.print("gpu: out of memory: {s}\n", .{message}), .out_of_memory => std.debug.print("gpu: out of memory: {s}\n", .{message}),
@ -60,14 +60,13 @@ const RequestAdapterResponse = struct {
message: ?[*:0]const u8, message: ?[*:0]const u8,
}; };
fn requestAdapterCallback( inline fn requestAdapterCallback(
status: gpu.RequestAdapterStatus, status: gpu.RequestAdapterStatus,
adapter: *gpu.Adapter, adapter: *gpu.Adapter,
message: ?[*:0]const u8, message: ?[*:0]const u8,
userdata: ?*anyopaque, context: *?RequestAdapterResponse,
) callconv(.C) void { ) void {
const response = @ptrCast(*?RequestAdapterResponse, @alignCast(@alignOf(*?RequestAdapterResponse), userdata)); context.* = RequestAdapterResponse{
response.* = RequestAdapterResponse{
.status = status, .status = status,
.adapter = adapter, .adapter = adapter,
.message = message, .message = message,
@ -98,7 +97,7 @@ pub fn setup(allocator: std.mem.Allocator) !Setup {
.compatible_surface = surface, .compatible_surface = surface,
.power_preference = .undef, .power_preference = .undef,
.force_fallback_adapter = false, .force_fallback_adapter = false,
}, requestAdapterCallback, &response); }, &response, requestAdapterCallback);
if (response.?.status != .success) { if (response.?.status != .success) {
std.debug.print("failed to create GPU adapter: {s}\n", .{response.?.message}); std.debug.print("failed to create GPU adapter: {s}\n", .{response.?.message});
std.process.exit(1); std.process.exit(1);
@ -121,7 +120,7 @@ pub fn setup(allocator: std.mem.Allocator) !Setup {
std.process.exit(1); std.process.exit(1);
} }
device.?.setUncapturedErrorCallback(printUnhandledErrorCallback, null); device.?.setUncapturedErrorCallback({}, printUnhandledErrorCallback);
return Setup{ return Setup{
.instance = instance.?, .instance = instance.?,
.adapter = response.?.adapter, .adapter = response.?.adapter,
@ -171,14 +170,16 @@ pub fn createSurfaceForWindow(
comptime glfw_options: glfw.BackendOptions, comptime glfw_options: glfw.BackendOptions,
) *gpu.Surface { ) *gpu.Surface {
const glfw_native = glfw.Native(glfw_options); const glfw_native = glfw.Native(glfw_options);
const descriptor = if (glfw_options.win32) gpu.Surface.DescriptorFromWindowsHWND{ const extension = if (glfw_options.win32) gpu.Surface.Extension{
.chain = .{ .s_type = .surface_descriptor_from_windows_hwnd }, .from_windows_hwnd = &.{
.hinstance = std.os.windows.kernel32.GetModuleHandleW(null).?, .hinstance = std.os.windows.kernel32.GetModuleHandleW(null).?,
.hwnd = glfw_native.getWin32Window(window), .hwnd = glfw_native.getWin32Window(window),
} else if (glfw_options.x11) gpu.Surface.DescriptorFromXlibWindow{ },
.chain = .{ .s_type = .surface_descriptor_from_xlib_window }, } else if (glfw_options.x11) gpu.Surface.Extension{
.display = glfw_native.getX11Display(), .from_xlib_window = &.{
.window = glfw_native.getX11Window(window), .display = glfw_native.getX11Display(),
.window = glfw_native.getX11Window(window),
},
} else if (glfw_options.cocoa) blk: { } else if (glfw_options.cocoa) blk: {
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]
@ -193,16 +194,13 @@ 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]
break :blk gpu.Surface.DescriptorFromMetalLayer{ break :blk gpu.Surface.Extension{ .from_metal_layer = &.{ .layer = layer.? } };
.chain = .{ .s_type = .surface_descriptor_from_metal_layer },
.layer = layer.?,
};
} else if (glfw_options.wayland) { } else if (glfw_options.wayland) {
@panic("TODO: this example does not support Wayland"); @panic("TODO: this example does not support Wayland");
} else unreachable; } else unreachable;
return instance.createSurface(&gpu.Surface.Descriptor{ return instance.createSurface(&gpu.Surface.Descriptor{
.next_in_chain = @ptrCast(*const gpu.ChainedStruct, &descriptor), .next_in_chain = extension,
}); });
} }