From 030d6d5a62ecd12a570d58334e6cfb4747b53fdf Mon Sep 17 00:00:00 2001 From: Andrea Odetti Date: Tue, 19 Oct 2021 09:38:36 +0100 Subject: [PATCH] Allow to use OpenGL (not GLES). GLES is still the default, except on Mac OS X. Signed-off-by: Andrea Odetti --- source/frontends/sdl/CMakeLists.txt | 27 +++++++++--- source/frontends/sdl/imgui/gles.h | 44 ++++++++++++++++++-- source/frontends/sdl/imgui/image.cpp | 4 +- source/frontends/sdl/imgui/sdlimguiframe.cpp | 37 ++++++++++++---- 4 files changed, 92 insertions(+), 20 deletions(-) diff --git a/source/frontends/sdl/CMakeLists.txt b/source/frontends/sdl/CMakeLists.txt index 3b0ed505..c4567774 100644 --- a/source/frontends/sdl/CMakeLists.txt +++ b/source/frontends/sdl/CMakeLists.txt @@ -1,12 +1,30 @@ include(FindPkgConfig) -find_package(SDL2 REQUIRED) -pkg_search_module(SDL2_IMAGE REQUIRED SDL2_image) -pkg_search_module(GLES2 REQUIRED glesv2) +add_executable(sa2) + set(IMGUI_PATH "imgui/imgui") set(IMGUI_CLUB_PATH "imgui/imgui_club") -add_executable(sa2) +find_package(SDL2 REQUIRED) +pkg_search_module(SDL2_IMAGE REQUIRED SDL2_image) + +# the default is to use GLES +set (SA2_USE_OPENGL 0) + +if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + set (SA2_USE_OPENGL 1) +endif() + + +if (${SA2_USE_OPENGL}) + pkg_search_module(GLES2 REQUIRED opengl) +else() + pkg_search_module(GLES2 REQUIRED glesv2) + target_compile_definitions(sa2 PRIVATE + IMGUI_IMPL_OPENGL_ES2 + ) +endif() + set(SOURCE_FILES main.cpp @@ -86,7 +104,6 @@ target_include_directories(sa2 PRIVATE ) target_compile_definitions(sa2 PRIVATE - IMGUI_IMPL_OPENGL_ES2 IMGUI_USER_CONFIG="frontends/sdl/imgui/imconfig.h" ) diff --git a/source/frontends/sdl/imgui/gles.h b/source/frontends/sdl/imgui/gles.h index 80bef59e..12a1e9b7 100644 --- a/source/frontends/sdl/imgui/gles.h +++ b/source/frontends/sdl/imgui/gles.h @@ -7,7 +7,16 @@ // "Supported versions are: 1.00 ES" #include -#define SDL_CONTEXT_MAJOR 2 +#define SA2_CONTEXT_FLAGS 0 +#define SA2_CONTEXT_PROFILE_MASK SDL_GL_CONTEXT_PROFILE_ES +#define SA2_CONTEXT_MAJOR_VERSION 2 +#define SA2_CONTEXT_MINOR_VERSION 0 + + // this is defined in gl2ext.h and nowhere in gl3.h + #define SA2_IMAGE_FORMAT GL_BGRA_EXT + +// this is used in all cases for GL_BGRA_EXT +#include #elif defined(IMGUI_IMPL_OPENGL_ES3) @@ -20,13 +29,40 @@ // "Supported versions are: 1.00 ES, 3.00 ES, and 3.10 ES" #include -#define SDL_CONTEXT_MAJOR 3 +#define SA2_CONTEXT_FLAGS 0 +#define SA2_CONTEXT_PROFILE_MASK SDL_GL_CONTEXT_PROFILE_ES +#define SA2_CONTEXT_MAJOR_VERSION 3 +#define SA2_CONTEXT_MINOR_VERSION 0 + +// this is defined in gl2ext.h and nowhere in gl3.h + #define SA2_IMAGE_FORMAT GL_BGRA_EXT + // "310 es" is accepted on a Pi4, but the imgui shaders do not compile +// this is used in all cases for GL_BGRA_EXT +#include + +#elif defined(__APPLE__) + +#include +#define SA2_CONTEXT_FLAGS SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG +#define SA2_CONTEXT_PROFILE_MASK SDL_GL_CONTEXT_PROFILE_CORE +#define SA2_CONTEXT_MAJOR_VERSION 3 +#define SA2_CONTEXT_MINOR_VERSION 2 + +#define SA2_IMAGE_FORMAT GL_RGBA + +#else + +#include +#define SA2_CONTEXT_FLAGS 0 +#define SA2_CONTEXT_PROFILE_MASK SDL_GL_CONTEXT_PROFILE_CORE +#define SA2_CONTEXT_MAJOR_VERSION 3 +#define SA2_CONTEXT_MINOR_VERSION 2 + +#define SA2_IMAGE_FORMAT GL_RGBA #endif -// this is used in all cases for GL_BGRA_EXT -#include #include "imgui.h" #include "imgui_impl_sdl.h" diff --git a/source/frontends/sdl/imgui/image.cpp b/source/frontends/sdl/imgui/image.cpp index fff0e5d2..af9fb0a7 100644 --- a/source/frontends/sdl/imgui/image.cpp +++ b/source/frontends/sdl/imgui/image.cpp @@ -31,7 +31,7 @@ namespace sa2 { glBindTexture(GL_TEXTURE_2D, texture); - const GLenum format = GL_BGRA_EXT; // this is defined in gl2ext.h and nowhere in gl3.h + const GLenum format = SA2_IMAGE_FORMAT; const GLenum type = GL_UNSIGNED_BYTE; glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, nullptr); @@ -48,7 +48,7 @@ namespace sa2 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - const GLenum format = GL_BGRA_EXT; // this is defined in gl2ext.h and nowhere in gl3.h + const GLenum format = SA2_IMAGE_FORMAT; const GLenum type = GL_UNSIGNED_BYTE; glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data); // reset to default state diff --git a/source/frontends/sdl/imgui/sdlimguiframe.cpp b/source/frontends/sdl/imgui/sdlimguiframe.cpp index fdb7a5bb..d0462ab2 100644 --- a/source/frontends/sdl/imgui/sdlimguiframe.cpp +++ b/source/frontends/sdl/imgui/sdlimguiframe.cpp @@ -12,16 +12,35 @@ #include +namespace +{ + + std::string safeGlGetString(GLenum name) + { + const char * str = reinterpret_cast(glGetString(name)); + if (str) + { + return str; + } + else + { + return ""; + } + } + +} + + namespace sa2 { SDLImGuiFrame::SDLImGuiFrame(const common2::EmulatorOptions & options) : SDLFrame(options) { - 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, SDL_CONTEXT_MAJOR); // from local gles.h - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SA2_CONTEXT_FLAGS); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SA2_CONTEXT_PROFILE_MASK); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, SA2_CONTEXT_MAJOR_VERSION); // from local gles.h + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, SA2_CONTEXT_MINOR_VERSION); // Create window with graphics context SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); @@ -49,11 +68,11 @@ namespace sa2 // Setup Platform/Renderer backends std::cerr << "IMGUI_VERSION: " << IMGUI_VERSION << std::endl; - std::cerr << "GL_VENDOR: " << glGetString(GL_VENDOR) << std::endl; - std::cerr << "GL_RENDERER: " << glGetString(GL_RENDERER) << std::endl; - std::cerr << "GL_VERSION: " << glGetString(GL_VERSION) << std::endl; - std::cerr << "GL_SHADING_LANGUAGE_VERSION: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl; - // const char* runtime_gl_extensions = (const char*)glGetString(GL_EXTENSIONS); + std::cerr << "GL_VENDOR: " << safeGlGetString(GL_VENDOR) << std::endl; + std::cerr << "GL_RENDERER: " << safeGlGetString(GL_RENDERER) << std::endl; + std::cerr << "GL_VERSION: " << safeGlGetString(GL_VERSION) << std::endl; + std::cerr << "GL_SHADING_LANGUAGE_VERSION: " << safeGlGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl; + // const char* runtime_gl_extensions = (const char*)safeGlGetString(GL_EXTENSIONS); // std::cerr << "GL_EXTENSIONS: " << runtime_gl_extensions << std::endl; // Setup Dear ImGui context