From 3e4d1bcb621095fb7bb920328c1ee4efacd3abde Mon Sep 17 00:00:00 2001 From: Pascal Thomet Date: Thu, 25 Apr 2024 12:54:16 +0200 Subject: [PATCH] OpenGL setup: try standard settings, then more conservative. --- .../opengl_setup_helper/opengl_setup_glfw.cpp | 44 ++++++++++++++ .../opengl_setup_helper/opengl_setup_sdl.cpp | 57 ++++++++++++++++++- 2 files changed, 99 insertions(+), 2 deletions(-) diff --git a/src/hello_imgui/internal/backend_impls/opengl_setup_helper/opengl_setup_glfw.cpp b/src/hello_imgui/internal/backend_impls/opengl_setup_helper/opengl_setup_glfw.cpp index ad245cc1..2c3f5edf 100644 --- a/src/hello_imgui/internal/backend_impls/opengl_setup_helper/opengl_setup_glfw.cpp +++ b/src/hello_imgui/internal/backend_impls/opengl_setup_helper/opengl_setup_glfw.cpp @@ -9,6 +9,7 @@ namespace HelloImGui { namespace BackendApi { + static bool gUseConservativeSettings = false; static void ApplyOpenGlOptions(OpenGlOptions& openGlOptions) { @@ -22,6 +23,10 @@ namespace HelloImGui { namespace BackendApi static OpenGlOptions MakeOpenGlOptions() { + // Notes: + // - SelectOpenGlVersion will first try non-conservative settings, and if it fails, try conservative settings + // - if runnerParams->rendererBackendOptions.openGlOptions.has_value(), we always use it + auto* runnerParams = HelloImGui::GetRunnerParams(); if (runnerParams->rendererBackendOptions.openGlOptions.has_value()) return runnerParams->rendererBackendOptions.openGlOptions.value(); @@ -69,13 +74,52 @@ namespace HelloImGui { namespace BackendApi openGlOptions.GlslVersion = "#version 130"; #endif + // + // Conservative settings based on user feedback + // + if (gUseConservativeSettings) + { + #ifdef _WIN32 + // cf https://github.com/pthom/imgui_bundle/issues/206#issuecomment-2074578423 + openGlOptions.MajorVersion = 3; + openGlOptions.MinorVersion = 1; + openGlOptions.UseCoreProfile = false; + openGlOptions.UseForwardCompat = true; + openGlOptions.GlslVersion = "#version 130"; + #endif + } + return openGlOptions; } + static bool CanCreateWindowWithCurrentOpenGlSettings() + { + bool success = false; + GLFWwindow *dummyWindow = glfwCreateWindow(20, 20, "test", NULL, NULL); + if (dummyWindow) + { + glfwDestroyWindow(dummyWindow); + success = true; + } + return success; + } + void OpenGlSetupGlfw::SelectOpenGlVersion() { OpenGlOptions openGlOptions = MakeOpenGlOptions(); ApplyOpenGlOptions(openGlOptions); + bool canCreateWindowWithStandardOpenGlSettings = CanCreateWindowWithCurrentOpenGlSettings(); + + if (!canCreateWindowWithStandardOpenGlSettings) + { + printf("Can't create window with standard OpenGL settings. Trying more conservative settings.\n"); + gUseConservativeSettings = true; + OpenGlOptions openGlOptions = MakeOpenGlOptions(); + ApplyOpenGlOptions(openGlOptions); + + bool canCreateWindowWithConservativeOpenGlSettings = CanCreateWindowWithCurrentOpenGlSettings(); + IM_ASSERT(canCreateWindowWithConservativeOpenGlSettings && "OpenGlSetupGlfw::SelectOpenGlVersion(): Can't create window with conservative OpenGL settings."); + } } void OpenGlSetupGlfw::InitGlLoader() diff --git a/src/hello_imgui/internal/backend_impls/opengl_setup_helper/opengl_setup_sdl.cpp b/src/hello_imgui/internal/backend_impls/opengl_setup_helper/opengl_setup_sdl.cpp index 67f97f89..502e612a 100644 --- a/src/hello_imgui/internal/backend_impls/opengl_setup_helper/opengl_setup_sdl.cpp +++ b/src/hello_imgui/internal/backend_impls/opengl_setup_helper/opengl_setup_sdl.cpp @@ -10,6 +10,8 @@ namespace HelloImGui { namespace BackendApi { + static bool gUseConservativeSettings = false; + static void ApplyOpenGlOptions(OpenGlOptions& openGlOptions) { #ifndef __EMSCRIPTEN__ @@ -26,6 +28,10 @@ namespace HelloImGui { namespace BackendApi static OpenGlOptions MakeOpenGlOptions() { + // Notes: + // - SelectOpenGlVersion will first try non-conservative settings, and if it fails, try conservative settings + // - if runnerParams->rendererBackendOptions.openGlOptions.has_value(), we always use it + auto* runnerParams = HelloImGui::GetRunnerParams(); if (runnerParams->rendererBackendOptions.openGlOptions.has_value()) return runnerParams->rendererBackendOptions.openGlOptions.value(); @@ -80,9 +86,41 @@ namespace HelloImGui { namespace BackendApi openGlOptions.GlslVersion = "#version 130"; #endif + // + // Conservative settings based on user feedback + // + if (gUseConservativeSettings) + { + #ifdef _WIN32 + // cf https://github.com/pthom/imgui_bundle/issues/206#issuecomment-2074578423 + openGlOptions.MajorVersion = 3; + openGlOptions.MinorVersion = 1; + openGlOptions.UseCoreProfile = false; + openGlOptions.UseForwardCompat = true; + openGlOptions.GlslVersion = "#version 130"; + #endif + } + return openGlOptions; } + static bool CanCreateWindowWithCurrentOpenGlSettings() + { + bool success = false; + SDL_Window *dummyWindow = SDL_CreateWindow("test", 0, 0, 20, 20, SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN); + if (dummyWindow) + { + SDL_GLContext dummyContext = SDL_GL_CreateContext(dummyWindow); + if (dummyContext) + { + SDL_GL_DeleteContext(dummyContext); + success = true; + } + SDL_DestroyWindow(dummyWindow); + } + return success; + } + static void AdditionalOpenGlPreSetup() { #if defined(__EMSCRIPTEN__) @@ -116,11 +154,26 @@ namespace HelloImGui { namespace BackendApi void OpenGlSetupSdl::SelectOpenGlVersion() { AdditionalOpenGlPreSetup(); - OpenGlOptions openGlOptions = MakeOpenGlOptions(); ApplyOpenGlOptions(openGlOptions); - AdditionalOpenGlPostSetup(); + + bool canCreateWindowWithStandardOpenGlSettings = CanCreateWindowWithCurrentOpenGlSettings(); + + if (!canCreateWindowWithStandardOpenGlSettings) + { + printf("Can't create window with standard OpenGL settings. Trying more conservative settings.\n"); + gUseConservativeSettings = true; + + AdditionalOpenGlPreSetup(); + OpenGlOptions openGlOptions = MakeOpenGlOptions(); + ApplyOpenGlOptions(openGlOptions); + AdditionalOpenGlPostSetup(); + + bool canCreateWindowWithConservativeOpenGlSettings = CanCreateWindowWithCurrentOpenGlSettings(); + IM_ASSERT(canCreateWindowWithConservativeOpenGlSettings && "OpenGlSetupSdl::SelectOpenGlVersion(): Can't create window with conservative OpenGL settings."); + } + } void OpenGlSetupSdl::InitGlLoader()