From 6a3ab36e521edfc6879b388037aadf9b832ec69e Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Wed, 29 Jan 2025 23:03:05 -0500 Subject: [PATCH] SDL3: Cleaned up GraphicsAdapter fetch routine --- src/FNAPlatform/SDL3_FNAPlatform.cs | 61 ++++++++++++----------------- 1 file changed, 24 insertions(+), 37 deletions(-) diff --git a/src/FNAPlatform/SDL3_FNAPlatform.cs b/src/FNAPlatform/SDL3_FNAPlatform.cs index 4e4e5bfc..7ea6ef34 100644 --- a/src/FNAPlatform/SDL3_FNAPlatform.cs +++ b/src/FNAPlatform/SDL3_FNAPlatform.cs @@ -821,13 +821,7 @@ public static GraphicsAdapter RegisterGame(Game game) // Store this for internal event filter work activeGames.Add(game); - // Which display did we end up on? - uint displayId = SDL.SDL_GetDisplayForWindow( - game.Window.Handle - ); - int displayIndex; - TryFetchDisplayIndex(displayId, out displayIndex); - return GraphicsAdapter.Adapters[displayIndex]; + return FetchDisplayAdapter(game.Window.Handle); } public static void UnregisterGame(Game game) @@ -1016,20 +1010,11 @@ ref bool textInputSuppress * display, a GraphicsDevice Reset occurs. * -flibit */ - uint newId = SDL.SDL_GetDisplayForWindow( - game.Window.Handle - ); - int newIndex; - if ( !TryFetchDisplayIndex(newId, out newIndex) || - newIndex >= GraphicsAdapter.Adapters.Count ) - { - GraphicsAdapter.AdaptersChanged(); // quickfix for this event coming in before the display reattach event. (must be fixed in sdl) - TryFetchDisplayIndex(newId, out newIndex); - } + GraphicsAdapter next = FetchDisplayAdapter(game.Window.Handle); - if (GraphicsAdapter.Adapters[newIndex] != currentAdapter) + if (next != currentAdapter) { - currentAdapter = GraphicsAdapter.Adapters[newIndex]; + currentAdapter = next; game.GraphicsDevice.Reset( game.GraphicsDevice.PresentationParameters, currentAdapter @@ -1053,18 +1038,7 @@ ref bool textInputSuppress { GraphicsAdapter.AdaptersChanged(); - uint displayId = SDL.SDL_GetDisplayForWindow( - game.Window.Handle - ); - int displayIndex; - if (TryFetchDisplayIndex(displayId, out displayIndex)) - { - currentAdapter = GraphicsAdapter.Adapters[displayIndex]; - } - else - { - currentAdapter = GraphicsAdapter.DefaultAdapter; - } + currentAdapter = FetchDisplayAdapter(game.Window.Handle); // Orientation Change if (evt.type == (uint) SDL.SDL_EventType.SDL_EVENT_DISPLAY_ORIENTATION) @@ -1223,19 +1197,32 @@ private static void RunEmscriptenMainLoop() // FIXME SDL3: This is really sloppy -flibit private static uint[] displayIds; - private static bool TryFetchDisplayIndex(uint id, out int index) + private static GraphicsAdapter FetchDisplayAdapter(IntPtr window, bool retry = true) { + uint displayId = SDL.SDL_GetDisplayForWindow(window); + + int index = -1; for (int i = 0; i < displayIds.Length; i += 1) { - if (id == displayIds[i]) + if (displayId == displayIds[i]) { index = i; - return true; + break; + } + } + + if (index < 0 || index > GraphicsAdapter.Adapters.Count) + { + FNALoggerEXT.LogWarn("SDL3 Window ID and Display ID desync'd"); + if (retry) + { + GraphicsAdapter.AdaptersChanged(); + return FetchDisplayAdapter(window, false); } + FNALoggerEXT.LogWarn("SDL3 Window ID and Display ID desync'd really badly"); + return GraphicsAdapter.DefaultAdapter; } - FNALoggerEXT.LogWarn("SDL3 Window ID and display ID desync'd"); - index = -1; - return false; + return GraphicsAdapter.Adapters[index]; } public static GraphicsAdapter[] GetGraphicsAdapters()