Upstream-status: diff --git a/.gitmodules b/.gitmodules index ce80999..dd529f0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -5,9 +5,6 @@ path = src/utf8 url = https://github.com/sheredom/utf8.h.git branch = master -[submodule "src/glad"] - path = src/glad - url = https://github.com/Dav1dde/glad.git [submodule "src/imgui"] path = src/imgui url = https://github.com/ocornut/imgui diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index af8fdd4..b359dde 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,4 @@ ## COMMON ## -option(ENABLE_GL1 "Build OpenGL 1 renderer." ON) option(ENABLE_GL3 "Build OpenGL 3 renderer." ON) option(ENABLE_GLES2 "Configure OpenGL 3 renderer to be OpenGL ES 2.0 compatible." OFF) @@ -52,21 +51,6 @@ if(NOT SQLITE3_FOUND) add_subdirectory(sqlite3) endif() -## glad ## -execute_process( - COMMAND git submodule update --init src/glad - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} -) -set(GLAD_SOURCES_DIR "${PROJECT_SOURCE_DIR}/src/glad") -add_subdirectory(glad/cmake glad_cmake) - -if(ENABLE_GL1 OR ENABLE_GL3) -glad_add_library(glad_gl REPRODUCIBLE API gl:compatibility=3.2 EXTENSIONS NONE) -endif() -if(ENABLE_GLES2) -glad_add_library(glad_gles REPRODUCIBLE API gles2=2.0 EXTENSIONS GL_OES_element_index_uint GL_EXT_texture_format_BGRA8888) -endif() - ## SDL2 ## find_package(SDL2 REQUIRED CONFIG) add_definitions(-DENABLE_SDL2) @@ -89,27 +73,34 @@ if(NOT TARGET SDL2::SDL2) endif() endif() +## OpenGL ## +add_library(opengl_target INTERFACE IMPORTED) +if(ENABLE_GL3) + find_package(OpenGL REQUIRED) + set_target_properties(opengl_target PROPERTIES + INTERFACE_COMPILE_DEFINITIONS ENABLE_GL3 + ) + if(ENABLE_GLES2) + set_target_properties(opengl_target PROPERTIES + INTERFACE_LINK_LIBRARIES OpenGL::GLES2 + INTERFACE_COMPILE_DEFINITIONS ENABLE_GLES2 + ) + else() + set_target_properties(opengl_target PROPERTIES + INTERFACE_LINK_LIBRARIES OpenGL::GL + ) + endif() +endif() + ## imgui ## # note: in the future there may be integrated CMake support into imgui # see: https://github.com/ocornut/imgui/pull/1713 -# for now do it manually, after glad and SDL2 because we use the includes for the sdl_opengl examples +# for now do it manually, after SDL2 because we use the includes for the sdl_opengl examples execute_process( COMMAND git submodule update --init src/imgui WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} ) -# workaround for OpenGL include for OpenGL2, need to be glad rather than gl itself -file(READ "${CMAKE_CURRENT_SOURCE_DIR}/imgui/backends/imgui_impl_opengl2.cpp" input) -string(REPLACE "OpenGL/gl.h" "glad/gl.h" input "${input}") -string(REPLACE "GL/gl.h" "glad/gl.h" input "${input}") -file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/imgui/backends/imgui_impl_opengl2.cpp" "${input}") - -# workaround for OpenGLES includes, need to be glad rather than gles itself -file(READ "${CMAKE_CURRENT_SOURCE_DIR}/imgui/backends/imgui_impl_opengl3.cpp" input) -string(REPLACE "GLES2/gl2.h" "glad/gles.h" input "${input}") -string(REPLACE "GLES3/gl3.h" "glad/gles.h" input "${input}") -file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/imgui/backends/imgui_impl_opengl3.cpp" "${input}") - set(SOURCES imgui/imgui.cpp imgui/imgui_draw.cpp @@ -119,11 +110,6 @@ set(SOURCES imgui/backends/imgui_impl_sdl2.cpp) -if(ENABLE_GL1) - LIST(APPEND SOURCES - imgui/backends/imgui_impl_opengl2.cpp - ) -endif() if(ENABLE_GL3) LIST(APPEND SOURCES imgui/backends/imgui_impl_opengl3.cpp @@ -132,7 +118,6 @@ endif() add_library(imgui STATIC ${SOURCES}) target_compile_definitions(imgui PUBLIC "-DImDrawIdx=unsigned int") # short is not enough for us -target_compile_definitions(imgui PUBLIC "-DIMGUI_IMPL_OPENGL_LOADER_GLAD") # We use glad # Configure GL3 renderer to be GLES2 compatible if GLES2 is enabled if(ENABLE_GLES2) target_compile_definitions(imgui PUBLIC "-DIMGUI_IMPL_OPENGL_ES2") @@ -142,15 +127,7 @@ target_include_directories(imgui PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/imgui/examples ${CMAKE_CURRENT_SOURCE_DIR}/imgui/misc/cpp ) -if(ENABLE_GLES2) -target_link_libraries(imgui - glad_gles -) -elseif(ENABLE_GL1 OR ENABLE_GL3) - target_link_libraries(imgui - glad_gl - ) -endif() + if(MINGW) target_link_libraries(imgui SDL2::SDL2-static diff --git a/src/openboardview/CMakeLists.txt b/src/openboardview/CMakeLists.txt index 7f8fbc0..470a145 100644 --- a/src/openboardview/CMakeLists.txt +++ b/src/openboardview/CMakeLists.txt @@ -1,13 +1,3 @@ -if(ENABLE_GL1) - add_definitions(-DENABLE_GL1) -endif() -if(ENABLE_GL3) - add_definitions(-DENABLE_GL3) -endif() -if(ENABLE_GLES2) - add_definitions(-DENABLE_GLES2) -endif() - # Platform-specific configuration if(WIN32) add_definitions(-DUNICODE) @@ -15,8 +5,6 @@ if(WIN32) else() if(APPLE) find_library(COCOA_LIBRARY Cocoa) - find_package(OpenGL REQUIRED) - set(OPENGL_INCLUDE_DIR ${OPENGL_INCLUDE_DIR}/Headers) else(APPLE) find_package(PkgConfig REQUIRED) @@ -113,11 +101,6 @@ set(SOURCES main_opengl.cpp ) -if(ENABLE_GL1) - LIST(APPEND SOURCES - Renderers/ImGuiRendererSDLGL1.cpp - ) -endif() if(ENABLE_GL3) LIST(APPEND SOURCES Renderers/ImGuiRendererSDLGL3.cpp @@ -191,9 +174,8 @@ target_include_directories(${PROJECT_NAME_LOWER} PRIVATE ${CMAKE_CURRENT_BINARY_DIR} # for build-generated ${IMGUI_INCLUDE_DIRS} ${UTF8_INCLUDE_DIR} - ${GLAD_INCLUDE_DIRS} ${GTK_INCLUDE_DIRS} - ${OPENGL_INCLUDE_DIR} + opengl_target ${ZLIB_INCLUDE_DIRS} ${FONTCONFIG_INCLUDE_DIRS} ) @@ -202,7 +184,7 @@ target_link_libraries(${PROJECT_NAME_LOWER} imgui SQLite::SQLite3 mpc - ${GLAD_LIBRARIES} + opengl_target ${COCOA_LIBRARY} ${ZLIB_LIBRARIES} ${FILESYSTEM_LIBRARIES} diff --git a/src/openboardview/Renderers/ImGuiRendererSDL.cpp b/src/openboardview/Renderers/ImGuiRendererSDL.cpp index 73f8fe8..1de05f1 100644 --- a/src/openboardview/Renderers/ImGuiRendererSDL.cpp +++ b/src/openboardview/Renderers/ImGuiRendererSDL.cpp @@ -3,16 +3,19 @@ #include #include -#ifdef ENABLE_GLES2 -#include -#else -#include -#endif - #define STB_IMAGE_IMPLEMENTATION #include #include "filesystem_impl.h" + +#ifdef ENABLE_GLES2 +#include +#else +#define GL_GLEXT_PROTOTYPES 1 +#include +#include +#endif +#include "backends/imgui_impl_opengl3.h" #include "backends/imgui_impl_sdl2.h" #include "../utils.h" @@ -71,13 +74,9 @@ bool ImGuiRendererSDL::init() { return false; } -#ifdef ENABLE_GLES2 - this->version = gladLoadGLES2(reinterpret_cast(SDL_GL_GetProcAddress)); -#else - this->version = gladLoadGL(reinterpret_cast(SDL_GL_GetProcAddress)); -#endif + SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &this->version); if (!version) { - SDL_LogError(SDL_LOG_CATEGORY_ERROR, "%s: glad failed to load OpenGL\n", this->name().c_str()); + SDL_LogError(SDL_LOG_CATEGORY_ERROR, "%s: imgui failed to load OpenGL\n", this->name().c_str()); shutdown(); return false; } @@ -104,7 +103,7 @@ bool ImGuiRendererSDL::init() { SDL_LogWarn(SDL_LOG_CATEGORY_RENDER, "%s: Unable to enable VSync: %s\n", this->name().c_str(), SDL_GetError()); // check if we got the correct OpenGL context version - if (!this->checkGLVersion(this->version)) { + if (!this->checkGLVersion()) { shutdown(); return false; } diff --git a/src/openboardview/Renderers/ImGuiRendererSDL.h b/src/openboardview/Renderers/ImGuiRendererSDL.h index bb058ef..d1d4f33 100644 --- a/src/openboardview/Renderers/ImGuiRendererSDL.h +++ b/src/openboardview/Renderers/ImGuiRendererSDL.h @@ -3,12 +3,9 @@ #include -// SDL, glad -#ifdef ENABLE_GLES2 -#include -#else -#include -#endif +#include "backends/imgui_impl_opengl3.h" +#include "backends/imgui_impl_sdl2.h" + #include #include @@ -23,7 +20,7 @@ public: virtual ~ImGuiRendererSDL(); virtual std::string name(); - virtual bool checkGLVersion(int version) = 0; + virtual bool checkGLVersion() = 0; virtual bool init(); virtual void processEvent(SDL_Event &event); virtual void initFrame(); diff --git a/src/openboardview/Renderers/ImGuiRendererSDLGL1.cpp b/src/openboardview/Renderers/ImGuiRendererSDLGL1.cpp deleted file mode 100644 index 7e75f6d..0000000 --- a/src/openboardview/Renderers/ImGuiRendererSDLGL1.cpp +++ /dev/null @@ -1,104 +0,0 @@ -#include "ImGuiRendererSDLGL1.h" - -#include -#include "backends/imgui_impl_opengl2.h" - -std::string ImGuiRendererSDLGL1::name() { - return "ImGuiRendererSDLGL1"; -} - -bool ImGuiRendererSDLGL1::checkGLVersion(int version) { - int major = GLAD_VERSION_MAJOR(version); - int minor = GLAD_VERSION_MINOR(version); - - if (major < 1 || (major == 1 && minor < 1)) { - SDL_LogError(SDL_LOG_CATEGORY_RENDER, - "Minimal OpenGL version required is %d.%d. Got %d.%d.", - 1, - 1, - major, - minor); - return false; - } - return true; -} - -void ImGuiRendererSDLGL1::setGLVersion() { - ImGuiRendererSDL::setGLVersion(); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); -} - -bool ImGuiRendererSDLGL1::init() { - return ImGuiRendererSDL::init() && ImGui_ImplOpenGL2_Init(); -} - -void ImGuiRendererSDLGL1::initFrame() { - ImGui_ImplOpenGL2_NewFrame(); - ImGuiRendererSDL::initFrame(); -} - -void ImGuiRendererSDLGL1::renderDrawData() { - ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData()); -} - -void ImGuiRendererSDLGL1::shutdown() { - if (ImGui::GetIO().BackendRendererUserData != nullptr) { - ImGui_ImplOpenGL2_Shutdown(); - } - ImGuiRendererSDL::shutdown(); -} - -std::string ImGuiRendererSDLGL1::createTexture(ImTextureID* out_texture, uint8_t* data, int w, int h, char fmt) { - GLuint tex; - GLenum format; - - if (w > glMaxTextureSize) { - return "width of " + std::to_string(w) + "px is too large for this GPU. Maximum allowed: " + std::to_string(glMaxTextureSize); - } - - if (h > glMaxTextureSize) { - return "height of " + std::to_string(h) + "px is too large for this GPU. Maximum allowed: " + std::to_string(glMaxTextureSize); - } - - switch (fmt) { - default: - case 0: - format = GL_BGRA; - break; - case 1: - format = GL_RGBA; - break; - case 2: - format = GL_RGB; - break; - } - - glGenTextures(1, &tex); - glBindTexture(GL_TEXTURE_2D, tex); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - - int major = GLAD_VERSION_MAJOR(version); - int minor = GLAD_VERSION_MINOR(version); - if (major == 2 || (major == 1 && minor >= 4)) { - glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); - } - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, format, GL_UNSIGNED_BYTE, data); - - GLenum code = glGetError(); - if (code == GL_OUT_OF_MEMORY) { - return "image too large to fit in the GPU memory."; - } - if (code != GL_NO_ERROR) { - return "error " + std::to_string(code) + " when loading the image into GPU memory."; - } - - glBindTexture(GL_TEXTURE_2D, 0); - - *out_texture = tex; - - return {}; -} diff --git a/src/openboardview/Renderers/ImGuiRendererSDLGL1.h b/src/openboardview/Renderers/ImGuiRendererSDLGL1.h deleted file mode 100644 index 0f66c16..0000000 --- a/src/openboardview/Renderers/ImGuiRendererSDLGL1.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef _IMGUIRENDERERSDLGL1_H_ -#define _IMGUIRENDERERSDLGL1_H_ - -#include - -#include "ImGuiRendererSDL.h" - -class ImGuiRendererSDLGL1 : public ImGuiRendererSDL { - using ImGuiRendererSDL::ImGuiRendererSDL; - -public: - std::string name(); - bool checkGLVersion(int version); - void setGLVersion(); - bool init(); - void initFrame(); - void renderDrawData(); - void shutdown(); - std::string createTexture(ImTextureID* out_texture, uint8_t* data, int w, int h, char fmt); -}; - -#endif diff --git a/src/openboardview/Renderers/ImGuiRendererSDLGL3.cpp b/src/openboardview/Renderers/ImGuiRendererSDLGL3.cpp index 1810e7a..2c5d79b 100644 --- a/src/openboardview/Renderers/ImGuiRendererSDLGL3.cpp +++ b/src/openboardview/Renderers/ImGuiRendererSDLGL3.cpp @@ -1,20 +1,38 @@ #include "ImGuiRendererSDLGL3.h" - #ifdef ENABLE_GLES2 -#include +#include #else -#include +#include +#include // for extension typedefs like PFNGLGENERATEMIPMAPPROC #endif #include "backends/imgui_impl_opengl3.h" +#include "backends/imgui_impl_sdl2.h" + +#ifdef ENABLE_GLES2 +#define GLEXT_MIPMAP_FUNC "glGenerateMipmapOES" +#else +#define GLEXT_MIPMAP_FUNC "glGenerateMipmap" +#endif + +#ifndef PFNGLGENERATEMIPMAPPROC // fallback for GLES2 +typedef void (APIENTRYP PFNGLGENERATEMIPMAPPROC) (GLenum target); +#endif + +static inline void portable_glGenerateMipmap(GLenum target) { + static auto pfn = (PFNGLGENERATEMIPMAPPROC)SDL_GL_GetProcAddress(GLEXT_MIPMAP_FUNC); + if (pfn) pfn(target); +} std::string ImGuiRendererSDLGL3::name() { return "ImGuiRendererSDLGL3"; } -bool ImGuiRendererSDLGL3::checkGLVersion(int version) { - int major = GLAD_VERSION_MAJOR(version); - int minor = GLAD_VERSION_MINOR(version); +bool ImGuiRendererSDLGL3::checkGLVersion() { + int major, minor; + + SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major); + SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor); if (major < GL_VERSION_MAJOR || (major == GL_VERSION_MAJOR && minor < GL_VERSION_MINOR)) { SDL_LogError(SDL_LOG_CATEGORY_RENDER, @@ -129,7 +147,7 @@ std::string ImGuiRendererSDLGL3::createTexture(ImTextureID* out_texture, uint8_t return "error " + std::to_string(code) + " when loading the image into GPU memory."; } - glGenerateMipmap(GL_TEXTURE_2D); + portable_glGenerateMipmap(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); *out_texture = tex; diff --git a/src/openboardview/Renderers/ImGuiRendererSDLGL3.h b/src/openboardview/Renderers/ImGuiRendererSDLGL3.h index af84011..2e47e0b 100644 --- a/src/openboardview/Renderers/ImGuiRendererSDLGL3.h +++ b/src/openboardview/Renderers/ImGuiRendererSDLGL3.h @@ -21,7 +21,7 @@ class ImGuiRendererSDLGL3 : public ImGuiRendererSDL { public: std::string name(); - bool checkGLVersion(int version); + bool checkGLVersion(); void setGLVersion(); bool init(); void initFrame(); diff --git a/src/openboardview/Renderers/Renderers.cpp b/src/openboardview/Renderers/Renderers.cpp index 1fb549d..94d5dd6 100644 --- a/src/openboardview/Renderers/Renderers.cpp +++ b/src/openboardview/Renderers/Renderers.cpp @@ -22,10 +22,6 @@ namespace Renderers { std::unique_ptr newInstance(Renderer r) { switch (r) { -#ifdef ENABLE_GL1 - case Renderer::OPENGL1: - return std::unique_ptr(new ImGuiRendererSDLGL1()); -#endif #ifdef ENABLE_GL3 case Renderer::OPENGL3: return std::unique_ptr(new ImGuiRendererSDLGL3()); diff --git a/src/openboardview/Renderers/Renderers.h b/src/openboardview/Renderers/Renderers.h index f88c15b..f1d86cf 100644 --- a/src/openboardview/Renderers/Renderers.h +++ b/src/openboardview/Renderers/Renderers.h @@ -1,7 +1,6 @@ #ifndef _RENDERERS_H_ #define _RENDERERS_H_ -#include "ImGuiRendererSDLGL1.h" #include "ImGuiRendererSDLGL3.h" #include diff --git a/src/openboardview/confparse.cpp b/src/openboardview/confparse.cpp index 4f7fd26..c6000c2 100644 --- a/src/openboardview/confparse.cpp +++ b/src/openboardview/confparse.cpp @@ -18,7 +18,6 @@ char default_conf[] = " configuration\r\n\ #\r\n\ # Renderer options\r\n\ -# 1 = OpenGL1\r\n\ # 2 = OpenGL3\r\n\ # 3 = OpenGLES2\r\n\ renderer=2\r\n\ diff --git a/src/openboardview/main_opengl.cpp b/src/openboardview/main_opengl.cpp index 366cdef..14dc5a8 100644 --- a/src/openboardview/main_opengl.cpp +++ b/src/openboardview/main_opengl.cpp @@ -76,7 +76,7 @@ char help[] = -y : Set window height\n\ -z : Set font size\n\ -p : Set the dpi\n\ - -r : Set the renderer [ OPENGL1 = 1; OPENGL3 = 2; OPENGLES2 = 3 ]\n\ + -r : Set the renderer [ OPENGL3 = 2; OPENGLES2 = 3 ]\n\ -d : Debug mode\n\ "; @@ -317,7 +317,7 @@ int main(int argc, char **argv) { // Setup renderer bool initialized = Renderers::initBestRenderer(g.renderer); if (!initialized) { - SDL_LogError(SDL_LOG_CATEGORY_RENDER, "%s", "No renderer not available. Exiting."); + SDL_LogError(SDL_LOG_CATEGORY_RENDER, "%s", "No renderer available. Exiting."); cleanupAndExit(1); }