Optional glbinding as OpenGL binding
authorWilly Scheibel <willyscheibel@gmx.de>
Tue, 19 Aug 2014 14:08:39 +0000 (16:08 +0200)
committerTobias Markus <tobbi@mozilla-uk.org>
Tue, 19 Aug 2014 14:10:14 +0000 (16:10 +0200)
CMakeLists.txt
src/video/gl/gl_painter.hpp
src/video/gl/gl_renderer.cpp
src/video/gl/gl_texture.cpp
src/video/glutil.hpp
src/video/texture_manager.cpp

index 607b976..8b1104b 100644 (file)
@@ -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})
index 16a197d..0345e15 100644 (file)
 #ifndef HEADER_SUPERTUX_VIDEO_GL_GL_PAINTER_HPP
 #define HEADER_SUPERTUX_VIDEO_GL_GL_PAINTER_HPP
 
+#ifdef USE_GLBINDING
+#include <glbinding/gl/gl.h>
+using namespace gl;
+#else
 #include <GL/glew.h>
+
 #include "SDL_opengl.h"
+#endif
 
 class DrawingRequest;
 
index 213c94a..4c249de 100644 (file)
 #include "video/gl/gl_texture.hpp"
 #include "video/util.hpp"
 
+#ifdef USE_GLBINDING
+#  include <glbinding/ContextInfo.h>
+#  include <glbinding/gl/extension.h>
+#  include <glbinding/callbacks.h>
+#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<int>(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<int>(GLEW_ARB_texture_non_power_of_two) << std::endl;
+#  endif
 #endif
 }
 
index b5ad44e..3a6d18f 100644 (file)
 #include "supertux/gameconfig.hpp"
 #include "video/gl/gl_texture.hpp"
 
+#ifdef USE_GLBINDING
+  #include <glbinding/ContextInfo.h>
+#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<GLint>(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<GLint>(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<GLint>(GL_LINEAR));
+  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, static_cast<GLint>(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<GLint>(GL_CLAMP_TO_EDGE));
+  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, static_cast<GLint>(GL_CLAMP_TO_EDGE));
 
   assert_gl("set texture params");
 }
index e51240d..4ead32d 100644 (file)
 #include <sstream>
 #include <stdexcept>
 
+
+#ifdef USE_GLBINDING
+
+#include <glbinding/gl/gl.h>
+using namespace gl;
+
+#else
+
 #ifndef GL_VERSION_ES_CM_1_0
 #  include <GL/glew.h>
 #endif
@@ -39,6 +47,8 @@
 #  include <GL/glext.h>
 #endif
 
+#endif
+
 static inline void check_gl_error(const char* message)
 {
   GLenum error = glGetError();
index 3a5f849..e544909 100644 (file)
@@ -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<GLint>(GL_RGBA),
                  saved_texture.width, saved_texture.height,
                  saved_texture.border, GL_RGBA,
                  GL_UNSIGNED_BYTE, saved_texture.pixels);