Skip to content

Commit

Permalink
gpu-dawn: use cross-platform process.getEnvVarOwned
Browse files Browse the repository at this point in the history
Helps #86

Signed-off-by: Stephen Gutekanst <[email protected]>
  • Loading branch information
slimsag committed Feb 21, 2022
1 parent 918bc61 commit e451e4a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gpu-dawn/src/dawn/hello_triangle.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 13 additions & 4 deletions gpu-dawn/src/dawn/sample_utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(.{});
Expand Down

0 comments on commit e451e4a

Please sign in to comment.