From f266dcdda36d5ef50bac644be34dbb4d4fffa7d1 Mon Sep 17 00:00:00 2001 From: Willy Scheibel Date: Tue, 19 Aug 2014 16:08:39 +0200 Subject: [PATCH] Optional glbinding as OpenGL binding --- CMakeLists.txt | 29 +++++++++++++++++++++++------ src/video/gl/gl_painter.hpp | 6 ++++++ src/video/gl/gl_renderer.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ src/video/gl/gl_texture.cpp | 32 ++++++++++++++++++++++---------- src/video/glutil.hpp | 10 ++++++++++ src/video/texture_manager.cpp | 6 +++--- 6 files changed, 106 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 607b9763a..8b1104b2a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,6 +71,8 @@ PKG_SEARCH_MODULE(SDL2IMAGE REQUIRED SDL2_image>=2.0.0) SET(HAVE_SDL TRUE) OPTION(ENABLE_OPENGL "Enable OpenGL support" ON) +OPTION(GLBINDING_ENABLED "Use glbinding instead of GLEW" OFF) +OPTION(GLBINDING_DEBUG_OUTPUT "Enable glbinding debug output for each called OpenGL function" ON) IF(ENABLE_OPENGL) FIND_PACKAGE(OpenGL) IF(OPENGL_FOUND) @@ -78,10 +80,21 @@ IF(ENABLE_OPENGL) SET(HAVE_OPENGL TRUE) ENDIF(OPENGL_FOUND) - FIND_PACKAGE(GLEW) - IF(GLEW_FOUND) - INCLUDE_DIRECTORIES(${GLEW_INCLUDE_DIR}) - ENDIF(GLEW_FOUND) + IF(GLBINDING_ENABLED) + FIND_PACKAGE(GLBINDING REQUIRED) + IF(GLBINDING_FOUND) + INCLUDE_DIRECTORIES(${GLBINDING_INCLUDES}) + ADD_DEFINITIONS(-DUSE_GLBINDING) + IF (GLBINDING_DEBUG_OUTPUT) + ADD_DEFINITIONS(-DUSE_GLBINDING_DEBUG_OUTPUT) + ENDIF() + ENDIF() + ELSE() + FIND_PACKAGE(GLEW REQUIRED) + IF(GLEW_FOUND) + INCLUDE_DIRECTORIES(${GLEW_INCLUDE_DIR}) + ENDIF(GLEW_FOUND) + ENDIF() ENDIF(ENABLE_OPENGL) FIND_PACKAGE(OpenAL REQUIRED) @@ -166,7 +179,7 @@ include_directories (${CMAKE_CURRENT_SOURCE_DIR}/external/obstack/) IF(MSVC) include_directories (${CMAKE_CURRENT_SOURCE_DIR}/mk/msvc) -ADD_DEFINITIONS(-D_USE_MATH_DEFINES -DNOMINMAX) + ADD_DEFINITIONS(-D_USE_MATH_DEFINES -DNOMINMAX) ENDIF(MSVC) ## Build list of sources for supertux binary @@ -363,7 +376,11 @@ TARGET_LINK_LIBRARIES(supertux2 ${OGGVORBIS_LIBRARIES}) TARGET_LINK_LIBRARIES(supertux2 ${PHYSFS_LIBRARY}) IF(HAVE_OPENGL) TARGET_LINK_LIBRARIES(supertux2 ${OPENGL_LIBRARY}) - TARGET_LINK_LIBRARIES(supertux2 ${GLEW_LIBRARY}) + IF(GLBINDING_FOUND) + TARGET_LINK_LIBRARIES(supertux2 ${GLBINDING_LIBRARIES}) + ELSE() + TARGET_LINK_LIBRARIES(supertux2 ${GLEW_LIBRARY}) + ENDIF() ENDIF(HAVE_OPENGL) IF(HAVE_LIBCURL) TARGET_LINK_LIBRARIES(supertux2 ${CURL_LIBRARY}) diff --git a/src/video/gl/gl_painter.hpp b/src/video/gl/gl_painter.hpp index 16a197dd0..0345e15e0 100644 --- a/src/video/gl/gl_painter.hpp +++ b/src/video/gl/gl_painter.hpp @@ -17,8 +17,14 @@ #ifndef HEADER_SUPERTUX_VIDEO_GL_GL_PAINTER_HPP #define HEADER_SUPERTUX_VIDEO_GL_GL_PAINTER_HPP +#ifdef USE_GLBINDING +#include +using namespace gl; +#else #include + #include "SDL_opengl.h" +#endif class DrawingRequest; diff --git a/src/video/gl/gl_renderer.cpp b/src/video/gl/gl_renderer.cpp index 213c94a1b..4c249dea4 100644 --- a/src/video/gl/gl_renderer.cpp +++ b/src/video/gl/gl_renderer.cpp @@ -30,6 +30,12 @@ #include "video/gl/gl_texture.hpp" #include "video/util.hpp" +#ifdef USE_GLBINDING +# include +# include +# include +#endif + #define LIGHTMAP_DIV 5 #ifdef GL_VERSION_ES_CM_1_0 @@ -67,6 +73,40 @@ GLRenderer::GLRenderer() : apply_video_mode(); +#ifdef USE_GLBINDING + + glbinding::Binding::initialize(); + +#ifdef USE_GLBINDING_DEBUG_OUTPUT + glbinding::setCallbackMask(glbinding::CallbackMask::After | glbinding::CallbackMask::ParametersAndReturnValue); + + glbinding::setAfterCallback([](const glbinding::FunctionCall & call) { + std::cout << call.function.name() << "("; + + for (unsigned i = 0; i < call.parameters.size(); ++i) + { + std::cout << call.parameters[i]->asString(); + if (i < call.parameters.size() - 1) + std::cout << ", "; + } + + std::cout << ")"; + + if (call.returnValue) + { + std::cout << " -> " << call.returnValue->asString(); + } + + std::cout << std::endl; + }); +#endif + + static auto extensions = glbinding::ContextInfo::extensions(); + log_info << "Using glbinding 1.0.0 " << std::endl; + log_info << "ARB_texture_non_power_of_two: " << static_cast(extensions.find(GLextension::GL_ARB_texture_non_power_of_two) != extensions.end()) << std::endl; + +#endif + // setup opengl state and transform glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); @@ -80,6 +120,7 @@ GLRenderer::GLRenderer() : apply_config(); #ifndef GL_VERSION_ES_CM_1_0 + #ifndef USE_GLBINDING GLenum err = glewInit(); if (GLEW_OK != err) { @@ -89,6 +130,7 @@ GLRenderer::GLRenderer() : } log_info << "Using GLEW " << glewGetString(GLEW_VERSION) << std::endl; log_info << "GLEW_ARB_texture_non_power_of_two: " << static_cast(GLEW_ARB_texture_non_power_of_two) << std::endl; +# endif #endif } diff --git a/src/video/gl/gl_texture.cpp b/src/video/gl/gl_texture.cpp index b5ad44e6a..3a6d18f08 100644 --- a/src/video/gl/gl_texture.cpp +++ b/src/video/gl/gl_texture.cpp @@ -17,6 +17,10 @@ #include "supertux/gameconfig.hpp" #include "video/gl/gl_texture.hpp" +#ifdef USE_GLBINDING + #include +#endif + namespace { inline bool is_power_of_2(int v) @@ -56,9 +60,8 @@ GLTexture::GLTexture(unsigned int width, unsigned int height) : try { glBindTexture(GL_TEXTURE_2D, m_handle); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, - m_texture_width, m_texture_height, - 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); + glTexImage2D(GL_TEXTURE_2D, 0, static_cast(GL_RGBA), m_texture_width, + m_texture_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); set_texture_params(); } catch(...) { @@ -78,11 +81,20 @@ GLTexture::GLTexture(SDL_Surface* image) : m_texture_width = next_power_of_two(image->w); m_texture_height = next_power_of_two(image->h); #else +# ifdef USE_GLBINDING + static auto extensions = glbinding::ContextInfo::extensions(); + if (extensions.find(GLextension::GL_ARB_texture_non_power_of_two) != extensions.end()) + { + m_texture_width = image->w; + m_texture_height = image->h; + } +# else if (GLEW_ARB_texture_non_power_of_two) { m_texture_width = image->w; m_texture_height = image->h; } +# endif else { m_texture_width = next_power_of_two(image->w); @@ -126,12 +138,12 @@ GLTexture::GLTexture(SDL_Surface* image) : glBindTexture(GL_TEXTURE_2D, m_handle); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); -#ifdef GL_UNPACK_ROW_LENGTH +#if defined(GL_UNPACK_ROW_LENGTH) || defined(USE_GLBINDING) glPixelStorei(GL_UNPACK_ROW_LENGTH, convert->pitch/convert->format->BytesPerPixel); #else /* OpenGL ES doesn't support UNPACK_ROW_LENGTH, let's hope SDL didn't add * padding bytes, otherwise we need some extra code here... */ - assert(convert->pitch == texture_width * convert->format->BytesPerPixel); + assert(convert->pitch == m_texture_width * convert->format->BytesPerPixel); #endif if(SDL_MUSTLOCK(convert)) @@ -139,7 +151,7 @@ GLTexture::GLTexture(SDL_Surface* image) : SDL_LockSurface(convert); } - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, + glTexImage2D(GL_TEXTURE_2D, 0, static_cast(GL_RGBA), m_texture_width, m_texture_height, 0, sdl_format, GL_UNSIGNED_BYTE, convert->pixels); @@ -173,11 +185,11 @@ GLTexture::~GLTexture() void GLTexture::set_texture_params() { - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, static_cast(GL_LINEAR)); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, static_cast(GL_LINEAR)); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, static_cast(GL_CLAMP_TO_EDGE)); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, static_cast(GL_CLAMP_TO_EDGE)); assert_gl("set texture params"); } diff --git a/src/video/glutil.hpp b/src/video/glutil.hpp index e51240dfa..4ead32d62 100644 --- a/src/video/glutil.hpp +++ b/src/video/glutil.hpp @@ -24,6 +24,14 @@ #include #include + +#ifdef USE_GLBINDING + +#include +using namespace gl; + +#else + #ifndef GL_VERSION_ES_CM_1_0 # include #endif @@ -39,6 +47,8 @@ # include #endif +#endif + static inline void check_gl_error(const char* message) { GLenum error = glGetError(); diff --git a/src/video/texture_manager.cpp b/src/video/texture_manager.cpp index 3a5f84970..e54490919 100644 --- a/src/video/texture_manager.cpp +++ b/src/video/texture_manager.cpp @@ -240,7 +240,7 @@ TextureManager::create_dummy_texture() void TextureManager::save_textures() { -#ifdef GL_PACK_ROW_LENGTH +#if defined(GL_PACK_ROW_LENGTH) || defined(USE_GLBINDING) /* all this stuff is not support by OpenGL ES */ glPixelStorei(GL_PACK_ROW_LENGTH, 0); glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0); @@ -305,7 +305,7 @@ TextureManager::save_texture(GLTexture* texture) void TextureManager::reload_textures() { -#ifdef GL_UNPACK_ROW_LENGTH +#if defined(GL_UNPACK_ROW_LENGTH) || defined(USE_GLBINDING) /* OpenGL ES doesn't support these */ glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0); @@ -324,7 +324,7 @@ TextureManager::reload_textures() assert_gl("creating texture handle"); glBindTexture(GL_TEXTURE_2D, handle); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, + glTexImage2D(GL_TEXTURE_2D, 0, static_cast(GL_RGBA), saved_texture.width, saved_texture.height, saved_texture.border, GL_RGBA, GL_UNSIGNED_BYTE, saved_texture.pixels); -- 2.11.0