From 6c7ea283fee2c7e1a5dbe5f5572be8a858c7446e Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Fri, 11 Mar 2022 18:35:50 -0700 Subject: [PATCH] gpu: utilize gpu.Adapter.BackendType in example Signed-off-by: Stephen Gutekanst --- gpu/examples/main.zig | 4 +-- gpu/examples/sample_utils.zig | 68 ++++++++++++++++++----------------- 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/gpu/examples/main.zig b/gpu/examples/main.zig index 77965780..401bc93c 100644 --- a/gpu/examples/main.zig +++ b/gpu/examples/main.zig @@ -23,7 +23,7 @@ pub fn main() !void { // If targeting OpenGL, we can't use the newer WGPUSurface API. Instead, we need to use the // older Dawn-specific API. https://bugs.chromium.org/p/dawn/issues/detail?id=269&q=surface&can=2 - const use_legacy_api = setup.backend_type == c.WGPUBackendType_OpenGL or setup.backend_type == c.WGPUBackendType_OpenGLES; + const use_legacy_api = setup.backend_type == .opengl or setup.backend_type == .opengles; var descriptor: gpu.SwapChain.Descriptor = undefined; if (!use_legacy_api) { window_data.swap_chain_format = .bgra8_unorm; @@ -42,7 +42,7 @@ pub fn main() !void { comptime sample_utils.detectGLFWOptions(), ); } else { - const binding = c.machUtilsCreateBinding(setup.backend_type, @ptrCast(*c.GLFWwindow, setup.window.handle), @ptrCast(c.WGPUDevice, setup.device.ptr)); + const binding = c.machUtilsCreateBinding(@enumToInt(setup.backend_type), @ptrCast(*c.GLFWwindow, setup.window.handle), @ptrCast(c.WGPUDevice, setup.device.ptr)); if (binding == null) { @panic("failed to create Dawn backend binding"); } diff --git a/gpu/examples/sample_utils.zig b/gpu/examples/sample_utils.zig index 260832ef..b570d38c 100644 --- a/gpu/examples/sample_utils.zig +++ b/gpu/examples/sample_utils.zig @@ -20,7 +20,7 @@ fn printDeviceError(error_type: c.WGPUErrorType, message: [*c]const u8, _: ?*any const Setup = struct { native_instance: gpu.NativeInstance, instance: c.WGPUInstance, - backend_type: c.WGPUBackendType, + backend_type: gpu.Adapter.BackendType, device: gpu.Device, window: glfw.Window, }; @@ -32,24 +32,24 @@ fn getEnvVarOwned(allocator: std.mem.Allocator, key: []const u8) error{ OutOfMem }; } -fn detectBackendType(allocator: std.mem.Allocator) !c.WGPUBackendType { - const WGPU_BACKEND = try getEnvVarOwned(allocator, "WGPU_BACKEND"); - if (WGPU_BACKEND) |backend| { +fn detectBackendType(allocator: std.mem.Allocator) !gpu.Adapter.BackendType { + const GPU_BACKEND = try getEnvVarOwned(allocator, "GPU_BACKEND"); + if (GPU_BACKEND) |backend| { defer allocator.free(backend); - if (std.ascii.eqlIgnoreCase(backend, "opengl")) return c.WGPUBackendType_OpenGL; - if (std.ascii.eqlIgnoreCase(backend, "opengles")) return c.WGPUBackendType_OpenGLES; - if (std.ascii.eqlIgnoreCase(backend, "d3d11")) return c.WGPUBackendType_D3D11; - if (std.ascii.eqlIgnoreCase(backend, "d3d12")) return c.WGPUBackendType_D3D12; - if (std.ascii.eqlIgnoreCase(backend, "metal")) return c.WGPUBackendType_Metal; - if (std.ascii.eqlIgnoreCase(backend, "null")) return c.WGPUBackendType_Null; - if (std.ascii.eqlIgnoreCase(backend, "vulkan")) return c.WGPUBackendType_Vulkan; + if (std.ascii.eqlIgnoreCase(backend, "opengl")) return .opengl; + if (std.ascii.eqlIgnoreCase(backend, "opengles")) return .opengles; + if (std.ascii.eqlIgnoreCase(backend, "d3d11")) return .d3d11; + if (std.ascii.eqlIgnoreCase(backend, "d3d12")) return .d3d12; + if (std.ascii.eqlIgnoreCase(backend, "metal")) return .metal; + if (std.ascii.eqlIgnoreCase(backend, "null")) return .nul; + if (std.ascii.eqlIgnoreCase(backend, "vulkan")) return .vulkan; @panic("unknown BACKEND type"); } const target = @import("builtin").target; - if (target.isDarwin()) return c.WGPUBackendType_Metal; - if (target.os.tag == .windows) return c.WGPUBackendType_D3D12; - return c.WGPUBackendType_Vulkan; + if (target.isDarwin()) return .metal; + if (target.os.tag == .windows) return .d3d12; + return .vulkan; } pub fn setup(allocator: std.mem.Allocator) !Setup { @@ -112,9 +112,9 @@ pub fn setup(allocator: std.mem.Allocator) !Setup { }; } -fn glfwWindowHintsForBackend(backend: c.WGPUBackendType) glfw.Window.Hints { +fn glfwWindowHintsForBackend(backend: gpu.Adapter.BackendType) glfw.Window.Hints { return switch (backend) { - c.WGPUBackendType_OpenGL => .{ + .opengl => .{ // Ask for OpenGL 4.4 which is what the GL backend requires for compute shaders and // texture views. .context_version_major = 4, @@ -122,7 +122,7 @@ fn glfwWindowHintsForBackend(backend: c.WGPUBackendType) glfw.Window.Hints { .opengl_forward_compat = true, .opengl_profile = .opengl_core_profile, }, - c.WGPUBackendType_OpenGLES => .{ + .opengles => .{ .context_version_major = 3, .context_version_minor = 1, .client_api = .opengl_es_api, @@ -136,21 +136,25 @@ fn glfwWindowHintsForBackend(backend: c.WGPUBackendType) glfw.Window.Hints { }; } -fn discoverAdapters(instance: c.MachDawnNativeInstance, window: glfw.Window, typ: c.WGPUBackendType) !void { - if (typ == c.WGPUBackendType_OpenGL) { - try glfw.makeContextCurrent(window); - const adapter_options = c.MachDawnNativeAdapterDiscoveryOptions_OpenGL{ - .getProc = @ptrCast(fn ([*c]const u8) callconv(.C) ?*anyopaque, glfw.getProcAddress), - }; - _ = c.machDawnNativeInstance_discoverAdapters(instance, typ, &adapter_options); - } else if (typ == c.WGPUBackendType_OpenGLES) { - try glfw.makeContextCurrent(window); - const adapter_options = c.MachDawnNativeAdapterDiscoveryOptions_OpenGLES{ - .getProc = @ptrCast(fn ([*c]const u8) callconv(.C) ?*anyopaque, glfw.getProcAddress), - }; - _ = c.machDawnNativeInstance_discoverAdapters(instance, typ, &adapter_options); - } else { - c.machDawnNativeInstance_discoverDefaultAdapters(instance); +fn discoverAdapters(instance: c.MachDawnNativeInstance, window: glfw.Window, typ: gpu.Adapter.BackendType) !void { + switch (typ) { + .opengl => { + try glfw.makeContextCurrent(window); + const adapter_options = c.MachDawnNativeAdapterDiscoveryOptions_OpenGL{ + .getProc = @ptrCast(fn ([*c]const u8) callconv(.C) ?*anyopaque, glfw.getProcAddress), + }; + _ = c.machDawnNativeInstance_discoverAdapters(instance, @enumToInt(typ), &adapter_options); + }, + .opengles => { + try glfw.makeContextCurrent(window); + const adapter_options = c.MachDawnNativeAdapterDiscoveryOptions_OpenGLES{ + .getProc = @ptrCast(fn ([*c]const u8) callconv(.C) ?*anyopaque, glfw.getProcAddress), + }; + _ = c.machDawnNativeInstance_discoverAdapters(instance, @enumToInt(typ), &adapter_options); + }, + else => { + c.machDawnNativeInstance_discoverDefaultAdapters(instance); + }, } }