Skip to content

Commit

Permalink
refactor opengl_setup_sdl/glfw.cpp (use OpenGlOptions)
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Apr 24, 2024
1 parent f8b0b99 commit 8998e33
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,72 @@
namespace HelloImGui { namespace BackendApi
{

void OpenGlSetupGlfw::SelectOpenGlVersion()
static void ApplyOpenGlOptions(OpenGlOptions& openGlOptions)
{
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, openGlOptions.MajorVersion);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, openGlOptions.MinorVersion);
if (openGlOptions.UseCoreProfile)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
if (openGlOptions.UseForwardCompat)
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
}

static OpenGlOptions MakeOpenGlOptions()
{
int major = 3;
int minor = 3;
bool useCoreProfile = true;
bool useForwardCompat = true;
auto* runnerParams = HelloImGui::GetRunnerParams();
if (runnerParams->rendererBackendOptions.openGlOptions.has_value())
return runnerParams->rendererBackendOptions.openGlOptions.value();

OpenGlOptions openGlOptions;

//
// Compute MajorVersion, MinorVersion, UseCoreProfile, UseForwardCompat
//
openGlOptions.MajorVersion = 3;
openGlOptions.MinorVersion = 3;
openGlOptions.UseCoreProfile = true;
openGlOptions.UseForwardCompat = true;

#if defined(IMGUI_IMPL_OPENGL_ES3)
{
BACKEND_THROW("OpenGlSetupGlfw::SelectOpenGlVersion needs implementation for GLES !");
// SDL_GL_SetAttribute(SDL_GL_CONTEXT_EGL, 3);
// SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
// SDL_GL_CONTEXT_PROFILE_ES);
// SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
// SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
}
#elif defined(__APPLE__)
{
// 3.2+ only
major = 3;
minor = 2;
useCoreProfile = true;
useForwardCompat = true;
openGlOptions.MajorVersion = 3;
openGlOptions.MinorVersion = 2;
openGlOptions.UseCoreProfile = true;
openGlOptions.UseForwardCompat = true;
}
#else
{
major = 3;
minor = 3;
useCoreProfile = true; // 3.2+ only
useForwardCompat = true; // 3.0+ only
openGlOptions.MajorVersion = 3;
openGlOptions.MinorVersion = 3;
openGlOptions.UseCoreProfile = true; // 3.2+ only
openGlOptions.UseForwardCompat = true; // 3.0+ only
}
#endif

auto* runnerParams = HelloImGui::GetRunnerParams();
if (runnerParams->rendererBackendOptions.openGlOptions.has_value())
{
auto& openGlOptions = runnerParams->rendererBackendOptions.openGlOptions.value();
major = openGlOptions.MajorVersion;
minor = openGlOptions.MinorVersion;
useCoreProfile = openGlOptions.UseCoreProfile;
useForwardCompat = openGlOptions.UseForwardCompat;
}
//
// Compute GlslVersion
//
#if defined(IMGUI_IMPL_OPENGL_ES3)
openGlOptions.GlslVersion = "#version 300es";
#elif defined(__APPLE__)
openGlOptions.GlslVersion = "#version 150";
#else
// GLSL 130
openGlOptions.GlslVersion = "#version 130";
#endif

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, major);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minor);
if (useCoreProfile)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
if (useForwardCompat)
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
return openGlOptions;
}

void OpenGlSetupGlfw::SelectOpenGlVersion()
{
OpenGlOptions openGlOptions = MakeOpenGlOptions();
ApplyOpenGlOptions(openGlOptions);
}

void OpenGlSetupGlfw::InitGlLoader()
Expand All @@ -75,33 +92,17 @@ namespace HelloImGui { namespace BackendApi
bool err = false; // If you use IMGUI_IMPL_OPENGL_LOADER_CUSTOM, your loader is likely to requires
// some form of initialization.
#endif
if (err)
{
BACKEND_THROW("RunnerGlfwOpenGl3::Impl_InitGlLoader(): Failed to initialize OpenGL loader!");
}
if (err)
{
BACKEND_THROW("RunnerGlfwOpenGl3::Impl_InitGlLoader(): Failed to initialize OpenGL loader!");
}
#endif // #ifndef __EMSCRIPTEN__
}

std::string OpenGlSetupGlfw::GlslVersion()
{
std::string glsl_version;
#if defined(IMGUI_IMPL_OPENGL_ES3)
glsl_version = "#version 300es";
#elif defined(__APPLE__)
glsl_version = "#version 150";
#else
// GLSL 130
glsl_version = "#version 130";
#endif

auto runnerParams = HelloImGui::GetRunnerParams();
if (runnerParams->rendererBackendOptions.openGlOptions.has_value())
{
auto& openGlOptions = runnerParams->rendererBackendOptions.openGlOptions.value();
glsl_version = openGlOptions.GlslVersion;
}

return glsl_version.c_str();
OpenGlOptions openGlOptions = MakeOpenGlOptions();
return openGlOptions.GlslVersion;
}
}} // namespace HelloImGui { namespace BackendApi

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,108 +10,143 @@

namespace HelloImGui { namespace BackendApi
{
void OpenGlSetupSdl::SelectOpenGlVersion()
static void ApplyOpenGlOptions(OpenGlOptions& openGlOptions)
{
int major = 3;
int minor = 3;
bool useCoreProfile = false;
bool useForwardCompat = false;
#ifndef __EMSCRIPTEN__
// Gl version not set in Emscripten, for legacy reasons that are not clear
// (was not used in HelloImGui before April 2024)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, openGlOptions.MajorVersion);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, openGlOptions.MinorVersion);
#endif
if (openGlOptions.UseCoreProfile)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
if (openGlOptions.UseForwardCompat)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
}

#if defined(__EMSCRIPTEN__)
/*
static OpenGlOptions MakeOpenGlOptions()
{
auto* runnerParams = HelloImGui::GetRunnerParams();
if (runnerParams->rendererBackendOptions.openGlOptions.has_value())
return runnerParams->rendererBackendOptions.openGlOptions.value();

OpenGlOptions openGlOptions;

//
// Compute MajorVersion, MinorVersion, UseCoreProfile, UseForwardCompat
//
openGlOptions.MajorVersion = 3;
openGlOptions.MinorVersion = 3;
openGlOptions.UseCoreProfile = false;
openGlOptions.UseForwardCompat = false;

#if defined(__EMSCRIPTEN__)
// Nothing!
#elif defined(IMGUI_IMPL_OPENGL_ES3)
{
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
}*/
#elif defined(IMGUI_IMPL_OPENGL_ES3)
openGlOptions.MajorVersion = 3;
openGlOptions.MinorVersion = 0;
}
#elif defined(IMGUI_IMPL_OPENGL_ES2)
{
openGlOptions.MajorVersion = 2;
openGlOptions.MinorVersion = 0;
}
#elif defined(__APPLE__)
{
openGlOptions.MajorVersion = 3;
openGlOptions.MinorVersion = 2;
openGlOptions.UseCoreProfile = true;
openGlOptions.UseForwardCompat = true; // Always required on Mac
}
#else
{
openGlOptions.MajorVersion = 3;
openGlOptions.MinorVersion = 3;
openGlOptions.UseCoreProfile = true;
}
#endif

//
// Compute GlslVersion
//
#if defined(IMGUI_IMPL_OPENGL_ES3)
openGlOptions.GlslVersion = "#version 300 es";
#elif defined(IMGUI_IMPL_OPENGL_ES2)
openGlOptions.GlslVersion = "#version 200 es";
#elif defined(__APPLE__)
openGlOptions.GlslVersion = "#version 150";
#else
openGlOptions.GlslVersion = "#version 130";
#endif

return openGlOptions;
}

static void AdditionalOpenGlPreSetup()
{
#if defined(__EMSCRIPTEN__)
// Nothing!
#elif defined(IMGUI_IMPL_OPENGL_ES3)
{
SDL_GL_SetAttribute(SDL_GL_CONTEXT_EGL, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
major = 3;
minor = 0;
}
#elif defined(IMGUI_IMPL_OPENGL_ES2)
#elif defined(IMGUI_IMPL_OPENGL_ES2)
{
SDL_GL_SetAttribute(SDL_GL_CONTEXT_EGL, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
major = 2;
minor = 0;
}
#elif defined(__APPLE__)
{
useForwardCompat= true; // Always required on Mac
useCoreProfile = true;
major = 3;
minor = 2;
}
#else
{
#elif defined(__APPLE__)
// Nothing!
#else
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
useCoreProfile = true;
major = 3;
minor = 3;
}
#endif

#ifndef __EMSCRIPTEN__
// Gl version not set in Emscripten, for legacy reasons that are not clear
// (was not used in HelloImGui before April 2024)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
#endif
if (useCoreProfile)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
if (useForwardCompat)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
#endif

}

static void AdditionalOpenGlPostSetup()
{
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
}


void OpenGlSetupSdl::SelectOpenGlVersion()
{
AdditionalOpenGlPreSetup();

OpenGlOptions openGlOptions = MakeOpenGlOptions();
ApplyOpenGlOptions(openGlOptions);

AdditionalOpenGlPostSetup();
}

void OpenGlSetupSdl::InitGlLoader()
{
// Initialize OpenGL loader
#if defined(IMGUI_IMPL_OPENGL_ES3) || defined(IMGUI_IMPL_OPENGL_ES2) || defined(__EMSCRIPTEN__)
; // nothing to do
#elif defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
bool err = gl3wInit() != 0;
if (err)
BACKEND_THROW("Failed to initialize OpenGL loader!");
#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
bool err = glewInit() != GLEW_OK;
if (err)
BACKEND_THROW("Failed to initialize OpenGL loader!");
#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
bool err = gladLoadGLLoader(reinterpret_cast<GLADloadproc>(SDL_GL_GetProcAddress)) == 0;
if (err)
BACKEND_THROW("Failed to initialize OpenGL loader!");
#endif
#if defined(IMGUI_IMPL_OPENGL_ES3) || defined(IMGUI_IMPL_OPENGL_ES2) || defined(__EMSCRIPTEN__)
; // nothing to do
#elif defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
bool err = gl3wInit() != 0;
if (err)
BACKEND_THROW("Failed to initialize OpenGL loader!");
#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
bool err = glewInit() != GLEW_OK;
if (err)
BACKEND_THROW("Failed to initialize OpenGL loader!");
#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
bool err = gladLoadGLLoader(reinterpret_cast<GLADloadproc>(SDL_GL_GetProcAddress)) == 0;
if (err)
BACKEND_THROW("Failed to initialize OpenGL loader!");
#endif
}

std::string OpenGlSetupSdl::GlslVersion()
{
std::string glsl_version;
#if defined(IMGUI_IMPL_OPENGL_ES3)
glsl_version = "#version 300 es";
#elif defined(IMGUI_IMPL_OPENGL_ES2)
glsl_version = "#version 200 es";
#elif defined(__APPLE__)
glsl_version = "#version 150";
#else
glsl_version = "#version 130";
#endif

auto runnerParams = HelloImGui::GetRunnerParams();
if (runnerParams->rendererBackendOptions.openGlOptions.has_value())
{
auto& openGlOptions = runnerParams->rendererBackendOptions.openGlOptions.value();
glsl_version = openGlOptions.GlslVersion;
}

return glsl_version.c_str();
OpenGlOptions openGlOptions = MakeOpenGlOptions();
return openGlOptions.GlslVersion;
}
}} // namespace HelloImGui { namespace BackendApi

Expand Down

0 comments on commit 8998e33

Please sign in to comment.