From 07c6c57ec2a81870f3ec2cb34143d215a4c7cb0b Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Tue, 30 Jun 2020 18:15:44 +1000 Subject: [PATCH] Fix core-initiated D3D9/D3D11 driver switches Currently, RetroArch only does this for GL and Vulkan hardware render contexts. Requesting a D3D11 context would result in the frontend creating a GL context instead (or whatever was set in frontend config), and then eventually crashing when it tried to render the UI. --- retroarch.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/retroarch.c b/retroarch.c index 149fe9a6bb..81257662ce 100644 --- a/retroarch.c +++ b/retroarch.c @@ -30151,6 +30151,20 @@ static bool hw_render_context_is_vulkan(enum retro_hw_context_type type) } #endif +#ifdef HAVE_D3D9 +static bool hw_render_context_is_d3d9(const struct retro_hw_render_callback* hwr) +{ + return hwr->context_type == RETRO_HW_CONTEXT_DIRECT3D && hwr->version_major == 9; +} +#endif + +#ifdef HAVE_D3D11 +static bool hw_render_context_is_d3d11(const struct retro_hw_render_callback* hwr) +{ + return hwr->context_type == RETRO_HW_CONTEXT_DIRECT3D && hwr->version_major == 11; +} +#endif + #if defined(HAVE_OPENGL) || defined(HAVE_OPENGL_CORE) static bool hw_render_context_is_gl(enum retro_hw_context_type type) { @@ -31603,6 +31617,42 @@ static bool video_driver_find_driver(struct rarch_state *p_rarch) } #endif +#if defined(HAVE_D3D9) + if (hwr && hw_render_context_is_d3d9(hwr)) + { + RARCH_LOG("[Video]: Using HW render, D3D9 driver forced.\n"); + if (!string_is_equal(settings->arrays.video_driver, "d3d9")) + { + RARCH_LOG("[Video]: \"%s\" saved as cached driver.\n", settings->arrays.video_driver); + strlcpy(p_rarch->cached_video_driver, + settings->arrays.video_driver, + sizeof(p_rarch->cached_video_driver)); + configuration_set_string(settings, + settings->arrays.video_driver, + "d3d9"); + } + p_rarch->current_video = &video_d3d9; + } +#endif + +#if defined(HAVE_D3D11) + if (hwr && hw_render_context_is_d3d11(hwr)) + { + RARCH_LOG("[Video]: Using HW render, D3D11 driver forced.\n"); + if (!string_is_equal(settings->arrays.video_driver, "d3d11")) + { + RARCH_LOG("[Video]: \"%s\" saved as cached driver.\n", settings->arrays.video_driver); + strlcpy(p_rarch->cached_video_driver, + settings->arrays.video_driver, + sizeof(p_rarch->cached_video_driver)); + configuration_set_string(settings, + settings->arrays.video_driver, + "d3d11"); + } + p_rarch->current_video = &video_d3d11; + } +#endif + #if defined(HAVE_OPENGL) || defined(HAVE_OPENGL_CORE) if (hwr && hw_render_context_is_gl(hwr->context_type)) {