gpu: utilize gpu.Adapter.BackendType in example

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-03-11 18:35:50 -07:00 committed by Stephen Gutekanst
parent 40f92dd9d9
commit 6c7ea283fe
2 changed files with 38 additions and 34 deletions

View file

@ -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 // 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 // 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; var descriptor: gpu.SwapChain.Descriptor = undefined;
if (!use_legacy_api) { if (!use_legacy_api) {
window_data.swap_chain_format = .bgra8_unorm; window_data.swap_chain_format = .bgra8_unorm;
@ -42,7 +42,7 @@ pub fn main() !void {
comptime sample_utils.detectGLFWOptions(), comptime sample_utils.detectGLFWOptions(),
); );
} else { } 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) { if (binding == null) {
@panic("failed to create Dawn backend binding"); @panic("failed to create Dawn backend binding");
} }

View file

@ -20,7 +20,7 @@ fn printDeviceError(error_type: c.WGPUErrorType, message: [*c]const u8, _: ?*any
const Setup = struct { const Setup = struct {
native_instance: gpu.NativeInstance, native_instance: gpu.NativeInstance,
instance: c.WGPUInstance, instance: c.WGPUInstance,
backend_type: c.WGPUBackendType, backend_type: gpu.Adapter.BackendType,
device: gpu.Device, device: gpu.Device,
window: glfw.Window, 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 { fn detectBackendType(allocator: std.mem.Allocator) !gpu.Adapter.BackendType {
const WGPU_BACKEND = try getEnvVarOwned(allocator, "WGPU_BACKEND"); const GPU_BACKEND = try getEnvVarOwned(allocator, "GPU_BACKEND");
if (WGPU_BACKEND) |backend| { if (GPU_BACKEND) |backend| {
defer allocator.free(backend); defer allocator.free(backend);
if (std.ascii.eqlIgnoreCase(backend, "opengl")) return c.WGPUBackendType_OpenGL; if (std.ascii.eqlIgnoreCase(backend, "opengl")) return .opengl;
if (std.ascii.eqlIgnoreCase(backend, "opengles")) return c.WGPUBackendType_OpenGLES; if (std.ascii.eqlIgnoreCase(backend, "opengles")) return .opengles;
if (std.ascii.eqlIgnoreCase(backend, "d3d11")) return c.WGPUBackendType_D3D11; if (std.ascii.eqlIgnoreCase(backend, "d3d11")) return .d3d11;
if (std.ascii.eqlIgnoreCase(backend, "d3d12")) return c.WGPUBackendType_D3D12; if (std.ascii.eqlIgnoreCase(backend, "d3d12")) return .d3d12;
if (std.ascii.eqlIgnoreCase(backend, "metal")) return c.WGPUBackendType_Metal; if (std.ascii.eqlIgnoreCase(backend, "metal")) return .metal;
if (std.ascii.eqlIgnoreCase(backend, "null")) return c.WGPUBackendType_Null; if (std.ascii.eqlIgnoreCase(backend, "null")) return .nul;
if (std.ascii.eqlIgnoreCase(backend, "vulkan")) return c.WGPUBackendType_Vulkan; if (std.ascii.eqlIgnoreCase(backend, "vulkan")) return .vulkan;
@panic("unknown BACKEND type"); @panic("unknown BACKEND type");
} }
const target = @import("builtin").target; const target = @import("builtin").target;
if (target.isDarwin()) return c.WGPUBackendType_Metal; if (target.isDarwin()) return .metal;
if (target.os.tag == .windows) return c.WGPUBackendType_D3D12; if (target.os.tag == .windows) return .d3d12;
return c.WGPUBackendType_Vulkan; return .vulkan;
} }
pub fn setup(allocator: std.mem.Allocator) !Setup { 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) { return switch (backend) {
c.WGPUBackendType_OpenGL => .{ .opengl => .{
// Ask for OpenGL 4.4 which is what the GL backend requires for compute shaders and // Ask for OpenGL 4.4 which is what the GL backend requires for compute shaders and
// texture views. // texture views.
.context_version_major = 4, .context_version_major = 4,
@ -122,7 +122,7 @@ fn glfwWindowHintsForBackend(backend: c.WGPUBackendType) glfw.Window.Hints {
.opengl_forward_compat = true, .opengl_forward_compat = true,
.opengl_profile = .opengl_core_profile, .opengl_profile = .opengl_core_profile,
}, },
c.WGPUBackendType_OpenGLES => .{ .opengles => .{
.context_version_major = 3, .context_version_major = 3,
.context_version_minor = 1, .context_version_minor = 1,
.client_api = .opengl_es_api, .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 { fn discoverAdapters(instance: c.MachDawnNativeInstance, window: glfw.Window, typ: gpu.Adapter.BackendType) !void {
if (typ == c.WGPUBackendType_OpenGL) { switch (typ) {
try glfw.makeContextCurrent(window); .opengl => {
const adapter_options = c.MachDawnNativeAdapterDiscoveryOptions_OpenGL{ try glfw.makeContextCurrent(window);
.getProc = @ptrCast(fn ([*c]const u8) callconv(.C) ?*anyopaque, glfw.getProcAddress), 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) { _ = c.machDawnNativeInstance_discoverAdapters(instance, @enumToInt(typ), &adapter_options);
try glfw.makeContextCurrent(window); },
const adapter_options = c.MachDawnNativeAdapterDiscoveryOptions_OpenGLES{ .opengles => {
.getProc = @ptrCast(fn ([*c]const u8) callconv(.C) ?*anyopaque, glfw.getProcAddress), try glfw.makeContextCurrent(window);
}; const adapter_options = c.MachDawnNativeAdapterDiscoveryOptions_OpenGLES{
_ = c.machDawnNativeInstance_discoverAdapters(instance, typ, &adapter_options); .getProc = @ptrCast(fn ([*c]const u8) callconv(.C) ?*anyopaque, glfw.getProcAddress),
} else { };
c.machDawnNativeInstance_discoverDefaultAdapters(instance); _ = c.machDawnNativeInstance_discoverAdapters(instance, @enumToInt(typ), &adapter_options);
},
else => {
c.machDawnNativeInstance_discoverDefaultAdapters(instance);
},
} }
} }