Skip to content

Commit

Permalink
OpenGL setup: try standard settings, then more conservative.
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Apr 25, 2024
1 parent 0cd80a2 commit 3e4d1bc
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace HelloImGui { namespace BackendApi
{
static bool gUseConservativeSettings = false;

static void ApplyOpenGlOptions(OpenGlOptions& openGlOptions)
{
Expand All @@ -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();
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace HelloImGui { namespace BackendApi
{
static bool gUseConservativeSettings = false;

static void ApplyOpenGlOptions(OpenGlOptions& openGlOptions)
{
#ifndef __EMSCRIPTEN__
Expand All @@ -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();
Expand Down Expand Up @@ -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__)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 3e4d1bc

Please sign in to comment.