From 3dcb1c2e4b441768e3a0e7d34e6a4b1ef76762ca Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Sun, 20 Feb 2022 15:52:57 -0700 Subject: [PATCH] gpu-dawn: use cross-platform process.getEnvVarOwned Helps hexops/mach#86 Signed-off-by: Stephen Gutekanst --- gpu-dawn/src/dawn/hello_triangle.zig | 2 +- gpu-dawn/src/dawn/sample_utils.zig | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/gpu-dawn/src/dawn/hello_triangle.zig b/gpu-dawn/src/dawn/hello_triangle.zig index 74b5237003..4a48886419 100644 --- a/gpu-dawn/src/dawn/hello_triangle.zig +++ b/gpu-dawn/src/dawn/hello_triangle.zig @@ -15,7 +15,7 @@ pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var allocator = gpa.allocator(); - const setup = try sample_utils.setup(); + const setup = try sample_utils.setup(allocator); const queue = c.wgpuDeviceGetQueue(setup.device); var descriptor = std.mem.zeroes(c.WGPUSwapChainDescriptor); diff --git a/gpu-dawn/src/dawn/sample_utils.zig b/gpu-dawn/src/dawn/sample_utils.zig index 73aad500f8..8cf58753af 100644 --- a/gpu-dawn/src/dawn/sample_utils.zig +++ b/gpu-dawn/src/dawn/sample_utils.zig @@ -51,8 +51,17 @@ const Setup = struct { window: glfw.Window, }; -fn detectBackendType() c.WGPUBackendType { - if (std.os.getenv("WGPU_BACKEND")) |backend| { +fn getEnvVarOwned(allocator: std.mem.Allocator, key: []const u8) error{ OutOfMemory, InvalidUtf8 }!?[]u8 { + return std.process.getEnvVarOwned(allocator, key) catch |err| switch (err) { + error.EnvironmentVariableNotFound => @as(?[]u8, null), + else => |e| e, + }; +} + +fn detectBackendType(allocator: std.mem.Allocator) !c.WGPUBackendType { + const WGPU_BACKEND = try getEnvVarOwned(allocator, "WGPU_BACKEND"); + if (WGPU_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; @@ -82,8 +91,8 @@ fn backendTypeString(t: c.WGPUBackendType) []const u8 { }; } -pub fn setup() !Setup { - const backend_type = detectBackendType(); +pub fn setup(allocator: std.mem.Allocator) !Setup { + const backend_type = try detectBackendType(allocator); const cmd_buf_type = CmdBufType.none; try glfw.init(.{});