win32 gui: fallback to non-debug d3d if create fails (requires sdk)

This commit is contained in:
Jonathan Marler 2025-01-14 12:54:52 -07:00 committed by CJ van den Berg
parent 35ca71e032
commit 0f08b3930f
2 changed files with 38 additions and 16 deletions

View file

@ -63,13 +63,14 @@ pub fn init() void {
global.init_called = true; global.init_called = true;
dwrite.init(); dwrite.init();
const debug_d3d = switch (builtin.mode) { const try_debug = switch (builtin.mode) {
.Debug => true, .Debug => true,
else => false, 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); const info = win32ext.queryInterface(global.d3d.device, win32.ID3D11InfoQueue);
defer _ = info.IUnknown.Release(); defer _ = info.IUnknown.Release();
{ {
@ -400,22 +401,37 @@ const D3d = struct {
context: *win32.ID3D11DeviceContext, context: *win32.ID3D11DeviceContext,
context1: *win32.ID3D11DeviceContext1, 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{ const levels = [_]win32.D3D_FEATURE_LEVEL{
.@"11_0", .@"11_0",
}; };
var last_hr: i32 = undefined; 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 device: *win32.ID3D11Device = undefined;
var context: *win32.ID3D11DeviceContext = undefined; var context: *win32.ID3D11DeviceContext = undefined;
last_hr = win32.D3D11CreateDevice( last_hr = win32.D3D11CreateDevice(
null, null,
driver, config.driver,
null, null,
.{ .{
.BGRA_SUPPORT = 1, .BGRA_SUPPORT = 1,
.SINGLETHREADED = 1, .SINGLETHREADED = 1,
.DEBUG = if (opt.debug) 1 else 0, .DEBUG = if (config.debug) 1 else 0,
}, },
&levels, &levels,
levels.len, levels.len,
@ -424,17 +440,23 @@ const D3d = struct {
null, null,
&context, &context,
); );
if (last_hr >= 0) return .{ if (last_hr >= 0) {
std.log.info("d3d11: {s} debug={}", .{ @tagName(config.driver), config.debug });
return .{
.{
.device = device, .device = device,
.context = context, .context = context,
.context1 = win32ext.queryInterface(context, win32.ID3D11DeviceContext1), .context1 = win32ext.queryInterface(context, win32.ID3D11DeviceContext1),
},
config.debug,
}; };
}
std.log.info( std.log.info(
"D3D11 {s} Driver error, hresult=0x{x}", "D3D11 {s} Driver (with{s} debug) error, hresult=0x{x}",
.{ @tagName(driver), @as(u32, @bitCast(last_hr)) }, .{ @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))});
} }
}; };

View file

@ -25,7 +25,7 @@ pub fn queryInterface(obj: anytype, comptime Interface: type) *Interface {
var iface: *Interface = undefined; var iface: *Interface = undefined;
const hr = obj.IUnknown.QueryInterface(iid, @ptrCast(&iface)); const hr = obj.IUnknown.QueryInterface(iid, @ptrCast(&iface));
if (hr < 0) std.debug.panic( 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))}, .{@as(u32, @bitCast(hr))},
); );
return iface; return iface;