From 0f08b3930ffb966b6941bf812e185c7a51b48542 Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Tue, 14 Jan 2025 12:54:52 -0700 Subject: [PATCH] win32 gui: fallback to non-debug d3d if create fails (requires sdk) --- src/win32/d3d11.zig | 52 ++++++++++++++++++++++++++++++------------ src/win32/win32ext.zig | 2 +- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/win32/d3d11.zig b/src/win32/d3d11.zig index 75a3fd4..ccb9c08 100644 --- a/src/win32/d3d11.zig +++ b/src/win32/d3d11.zig @@ -63,13 +63,14 @@ pub fn init() void { global.init_called = true; dwrite.init(); - const debug_d3d = switch (builtin.mode) { + const try_debug = switch (builtin.mode) { .Debug => true, else => false, }; - global.d3d = D3d.init(.{ .debug = debug_d3d }); - if (debug_d3d) { + global.d3d, const debug = D3d.init(.{ .debug = try_debug }); + + if (debug) { const info = win32ext.queryInterface(global.d3d.device, win32.ID3D11InfoQueue); defer _ = info.IUnknown.Release(); { @@ -400,22 +401,37 @@ const D3d = struct { context: *win32.ID3D11DeviceContext, context1: *win32.ID3D11DeviceContext1, - pub fn init(opt: struct { debug: bool }) D3d { + pub fn init(opt: struct { debug: bool }) struct { D3d, bool } { const levels = [_]win32.D3D_FEATURE_LEVEL{ .@"11_0", }; var last_hr: i32 = undefined; - for (&[_]win32.D3D_DRIVER_TYPE{ .HARDWARE, .WARP }) |driver| { + + const Config = struct { + driver: win32.D3D_DRIVER_TYPE, + debug: bool, + }; + const configs = [_]Config{ + .{ .driver = .HARDWARE, .debug = true }, + .{ .driver = .HARDWARE, .debug = false }, + .{ .driver = .SOFTWARE, .debug = true }, + .{ .driver = .SOFTWARE, .debug = false }, + }; + + for (configs) |config| { + const skip_config = config.debug and !opt.debug; + if (skip_config) continue; + var device: *win32.ID3D11Device = undefined; var context: *win32.ID3D11DeviceContext = undefined; last_hr = win32.D3D11CreateDevice( null, - driver, + config.driver, null, .{ .BGRA_SUPPORT = 1, .SINGLETHREADED = 1, - .DEBUG = if (opt.debug) 1 else 0, + .DEBUG = if (config.debug) 1 else 0, }, &levels, levels.len, @@ -424,17 +440,23 @@ const D3d = struct { null, &context, ); - if (last_hr >= 0) return .{ - .device = device, - .context = context, - .context1 = win32ext.queryInterface(context, win32.ID3D11DeviceContext1), - }; + if (last_hr >= 0) { + std.log.info("d3d11: {s} debug={}", .{ @tagName(config.driver), config.debug }); + return .{ + .{ + .device = device, + .context = context, + .context1 = win32ext.queryInterface(context, win32.ID3D11DeviceContext1), + }, + config.debug, + }; + } std.log.info( - "D3D11 {s} Driver error, hresult=0x{x}", - .{ @tagName(driver), @as(u32, @bitCast(last_hr)) }, + "D3D11 {s} Driver (with{s} debug) error, hresult=0x{x}", + .{ @tagName(config.driver), if (config.debug) "" else "out", @as(u32, @bitCast(last_hr)) }, ); } - std.debug.panic("failed to initialize Direct3D11, hresult=0x{x}", .{last_hr}); + std.debug.panic("failed to initialize Direct3D11, hresult=0x{x}", .{@as(u32, @bitCast(last_hr))}); } }; diff --git a/src/win32/win32ext.zig b/src/win32/win32ext.zig index 7e05dbb..f70d244 100644 --- a/src/win32/win32ext.zig +++ b/src/win32/win32ext.zig @@ -25,7 +25,7 @@ pub fn queryInterface(obj: anytype, comptime Interface: type) *Interface { var iface: *Interface = undefined; const hr = obj.IUnknown.QueryInterface(iid, @ptrCast(&iface)); if (hr < 0) std.debug.panic( - "QueryInferface on " ++ obj_basename ++ " as " ++ iface_basename ++ " failed, hresult={}", + "QueryInferface on " ++ obj_basename ++ " as " ++ iface_basename ++ " failed, hresult=0x{x}", .{@as(u32, @bitCast(hr))}, ); return iface;