From 49a6612fea2065e73865d36fdeaac711e46e5b5d Mon Sep 17 00:00:00 2001 From: Michael Martin Date: Sat, 11 Jul 2020 15:34:21 -0700 Subject: [PATCH] Rework SDL2 render driver selection. The "direct3d" DX9 driver is no longer skipped by default. There is a new --renderer=VALUE option to attempt to use the named SDL2 render driver if present and compatible. --- sc2/src/libs/graphics/gfx_common.h | 3 ++- sc2/src/libs/graphics/sdl/pure.c | 12 +++++++----- sc2/src/libs/graphics/sdl/pure.h | 2 +- sc2/src/libs/graphics/sdl/sdl2_pure.c | 22 ++++++++++++---------- sc2/src/libs/graphics/sdl/sdl_common.c | 6 +++--- sc2/src/uqm.c | 14 ++++++++++++-- 6 files changed, 37 insertions(+), 22 deletions(-) diff --git a/sc2/src/libs/graphics/gfx_common.h b/sc2/src/libs/graphics/gfx_common.h index c4db5cd48..e9e421aba 100644 --- a/sc2/src/libs/graphics/gfx_common.h +++ b/sc2/src/libs/graphics/gfx_common.h @@ -63,7 +63,8 @@ extern int GfxFlags; // The following functions are driver-defined void TFB_PreInit (void); -int TFB_InitGraphics (int driver, int flags, int width, int height); +int TFB_InitGraphics (int driver, int flags, const char *renderer, + int width, int height); int TFB_ReInitGraphics (int driver, int flags, int width, int height); void TFB_UninitGraphics (void); void TFB_ProcessEvents (void); diff --git a/sc2/src/libs/graphics/sdl/pure.c b/sc2/src/libs/graphics/sdl/pure.c index 07631a299..df4a3293f 100644 --- a/sc2/src/libs/graphics/sdl/pure.c +++ b/sc2/src/libs/graphics/sdl/pure.c @@ -237,7 +237,7 @@ TFB_Pure_ConfigureVideo (int driver, int flags, int width, int height, int toggl } int -TFB_Pure_InitGraphics (int driver, int flags, int width, int height) +TFB_Pure_InitGraphics (int driver, int flags, const char *renderer, int width, int height) { char VideoName[256]; @@ -245,10 +245,12 @@ TFB_Pure_InitGraphics (int driver, int flags, int width, int height) SDL_VideoDriverName (VideoName, sizeof (VideoName)); log_add (log_Info, "SDL driver used: %s", VideoName); - // Set the environment variable SDL_VIDEODRIVER to override - // For Linux: x11 (default), dga, fbcon, directfb, svgalib, - // ggi, aalib - // For Windows: directx (default), windib + (void) renderer; + // The "renderer" argument is ignored by SDL1. To control how SDL1 + // gets its pixmap, set the environment variable SDL_VIDEODRIVER. + // For Linux: x11 (default), dga, fbcon, directfb, svgalib, + // ggi, aalib + // For Windows: directx (default), windib log_add (log_Info, "SDL initialized."); log_add (log_Info, "Initializing Screen."); diff --git a/sc2/src/libs/graphics/sdl/pure.h b/sc2/src/libs/graphics/sdl/pure.h index 8e2632a0a..50cf06f4b 100644 --- a/sc2/src/libs/graphics/sdl/pure.h +++ b/sc2/src/libs/graphics/sdl/pure.h @@ -21,7 +21,7 @@ #include "libs/graphics/sdl/sdl_common.h" -int TFB_Pure_InitGraphics (int driver, int flags, int width, int height); +int TFB_Pure_InitGraphics (int driver, int flags, const char *renderer, int width, int height); void TFB_Pure_UninitGraphics (void); int TFB_Pure_ConfigureVideo (int driver, int flags, int width, int height, int togglefullscreen); void Scale_PerfTest (void); diff --git a/sc2/src/libs/graphics/sdl/sdl2_pure.c b/sc2/src/libs/graphics/sdl/sdl2_pure.c index be1f89a1f..bf958bc87 100644 --- a/sc2/src/libs/graphics/sdl/sdl2_pure.c +++ b/sc2/src/libs/graphics/sdl/sdl2_pure.c @@ -34,6 +34,7 @@ static TFB_SDL2_SCREENINFO SDL2_Screens[TFB_GFX_NUMSCREENS]; static SDL_Window *window = NULL; static SDL_Renderer *renderer = NULL; +static const char *rendererBackend = NULL; static int ScreenFilterMode; @@ -99,26 +100,26 @@ static int FindBestRenderDriver (void) { int i, n; + if (!rendererBackend) { + /* If the user has no preference, just let SDL2 choose */ + return -1; + } n = SDL_GetNumRenderDrivers (); + log_add (log_Info, "Searching for render driver \"%s\".", rendererBackend); + for (i = 0; i < n; i++) { SDL_RendererInfo info; if (SDL_GetRenderDriverInfo (i, &info) < 0) { continue; } - if (!strcmp(info.name, "direct3d")) { - /* The 32-bit Windows 8+ Intel Integrated Graphics - * driver has a common bug that causes the D3D9 - * display to crash with a divide-by-zero error. - * If we have any alternatives, we will ask for - * them instead. */ - continue; - } - if (info.flags & SDL_RENDERER_ACCELERATED) { + if (!strcmp(info.name, rendererBackend)) { return i; } + log_add (log_Info, "Skipping render driver \"%s\"", info.name); } /* We did not find any accelerated drivers that weren't D3D9. * Return -1 to ask SDL2 to do its best. */ + log_add (log_Info, "Render driver \"%s\" not available, using system default", rendererBackend); return -1; } @@ -265,7 +266,7 @@ TFB_Pure_ConfigureVideo (int driver, int flags, int width, int height, int toggl } int -TFB_Pure_InitGraphics (int driver, int flags, int width, int height) +TFB_Pure_InitGraphics (int driver, int flags, const char *renderer, int width, int height) { log_add (log_Info, "Initializing SDL."); log_add (log_Info, "SDL initialized."); @@ -273,6 +274,7 @@ TFB_Pure_InitGraphics (int driver, int flags, int width, int height) ScreenWidth = 320; ScreenHeight = 240; + rendererBackend = renderer; if (TFB_Pure_ConfigureVideo (driver, flags, width, height, 0)) { diff --git a/sc2/src/libs/graphics/sdl/sdl_common.c b/sc2/src/libs/graphics/sdl/sdl_common.c index 3f57084a6..b699bf825 100644 --- a/sc2/src/libs/graphics/sdl/sdl_common.c +++ b/sc2/src/libs/graphics/sdl/sdl_common.c @@ -51,7 +51,7 @@ volatile int QuitPosted = 0; volatile int GameActive = 1; // Track the SDL_ACTIVEEVENT state SDL_APPACTIVE int -TFB_InitGraphics (int driver, int flags, int width, int height) +TFB_InitGraphics (int driver, int flags, const char *renderer, int width, int height) { int result, i; char caption[200]; @@ -72,12 +72,12 @@ TFB_InitGraphics (int driver, int flags, int width, int height) driver = TFB_GFXDRIVER_SDL_PURE; log_add (log_Warning, "OpenGL support not compiled in," " so using pure SDL driver"); - result = TFB_Pure_InitGraphics (driver, flags, width, height); + result = TFB_Pure_InitGraphics (driver, flags, renderer, width, height); #endif } else { - result = TFB_Pure_InitGraphics (driver, flags, width, height); + result = TFB_Pure_InitGraphics (driver, flags, renderer, width, height); } #if SDL_MAJOR_VERSION == 1 diff --git a/sc2/src/uqm.c b/sc2/src/uqm.c index 4924f931c..43c25d8ed 100644 --- a/sc2/src/uqm.c +++ b/sc2/src/uqm.c @@ -101,6 +101,8 @@ struct options_struct const char *addonDir; const char **addons; int numAddons; + + const char *graphicsBackend; // Commandline and user config options DECL_CONFIG_OPTION(bool, opengl); @@ -236,6 +238,7 @@ main (int argc, char *argv[]) /* .addonDir = */ NULL, /* .addons = */ NULL, /* .numAddons = */ 0, + /* .graphicsBackend = */ NULL, INIT_CONFIG_OPTION( opengl, false ), INIT_CONFIG_OPTION2( resolution, 640, 480 ), @@ -419,8 +422,8 @@ main (int argc, char *argv[]) gfxFlags |= TFB_GFXFLAGS_SCANLINES; if (options.showFps.value) gfxFlags |= TFB_GFXFLAGS_SHOWFPS; - TFB_InitGraphics (gfxDriver, gfxFlags, options.resolution.width, - options.resolution.height); + TFB_InitGraphics (gfxDriver, gfxFlags, options.graphicsBackend, + options.resolution.width, options.resolution.height); if (options.gamma.set && setGammaCorrection (options.gamma.value)) optGamma = options.gamma.value; else @@ -709,6 +712,7 @@ enum ADDONDIR_OPT, ACCEL_OPT, SAFEMODE_OPT, + RENDERER_OPT, #ifdef NETPLAY NETHOST1_OPT, NETPORT1_OPT, @@ -756,6 +760,7 @@ static struct option longOptions[] = {"addondir", 1, NULL, ADDONDIR_OPT}, {"accel", 1, NULL, ACCEL_OPT}, {"safe", 0, NULL, SAFEMODE_OPT}, + {"renderer", 1, NULL, RENDERER_OPT}, #ifdef NETPLAY {"nethost1", 1, NULL, NETHOST1_OPT}, {"netport1", 1, NULL, NETPORT1_OPT}, @@ -1050,6 +1055,9 @@ parseOptions (int argc, char *argv[], struct options_struct *options) case SAFEMODE_OPT: setBoolOption (&options->safeMode, true); break; + case RENDERER_OPT: + options->graphicsBackend = optarg; + break; #ifdef NETPLAY case NETHOST1_OPT: netplayOptions.peer[0].isServer = false; @@ -1208,6 +1216,8 @@ usage (FILE *out, const struct options_struct *defaults) "may be specified multiple times)"); log_add (log_User, " --addondir=ADDONDIR (directory where addons " "reside)"); + log_add (log_User, " --renderer=name (Select named rendering engine " + "if possible)"); log_add (log_User, " --sound=DRIVER (openal, mixsdl, none; default " "mixsdl)"); log_add (log_User, " --stereosfx (enables positional sound effects, "