gpu-dawn: use cross-platform process.getEnvVarOwned

Helps hexops/mach#86

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-02-20 15:52:57 -07:00 committed by Stephen Gutekanst
parent 918bc61187
commit e451e4acb8
2 changed files with 14 additions and 5 deletions

View file

@ -15,7 +15,7 @@ pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var allocator = gpa.allocator(); var allocator = gpa.allocator();
const setup = try sample_utils.setup(); const setup = try sample_utils.setup(allocator);
const queue = c.wgpuDeviceGetQueue(setup.device); const queue = c.wgpuDeviceGetQueue(setup.device);
var descriptor = std.mem.zeroes(c.WGPUSwapChainDescriptor); var descriptor = std.mem.zeroes(c.WGPUSwapChainDescriptor);

View file

@ -51,8 +51,17 @@ const Setup = struct {
window: glfw.Window, window: glfw.Window,
}; };
fn detectBackendType() c.WGPUBackendType { fn getEnvVarOwned(allocator: std.mem.Allocator, key: []const u8) error{ OutOfMemory, InvalidUtf8 }!?[]u8 {
if (std.os.getenv("WGPU_BACKEND")) |backend| { 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, "opengl")) return c.WGPUBackendType_OpenGL;
if (std.ascii.eqlIgnoreCase(backend, "opengles")) return c.WGPUBackendType_OpenGLES; 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, "d3d11")) return c.WGPUBackendType_D3D11;
@ -82,8 +91,8 @@ fn backendTypeString(t: c.WGPUBackendType) []const u8 {
}; };
} }
pub fn setup() !Setup { pub fn setup(allocator: std.mem.Allocator) !Setup {
const backend_type = detectBackendType(); const backend_type = try detectBackendType(allocator);
const cmd_buf_type = CmdBufType.none; const cmd_buf_type = CmdBufType.none;
try glfw.init(.{}); try glfw.init(.{});