diff --git a/include/mbgl/gl/gl_helper.hpp b/include/mbgl/gl/gl_helper.hpp index e1fb8c27c..4f3990a43 100644 --- a/include/mbgl/gl/gl_helper.hpp +++ b/include/mbgl/gl/gl_helper.hpp @@ -1,88 +1,21 @@ #ifndef MBGL_GL_GL_HELPER #define MBGL_GL_GL_HELPER -#include - -#include - -namespace { - -template -class Preserve { -public: - Preserve() : data(Create()) {} - ~Preserve() { Destroy(data); } - -private: - const T data; -}; - -inline bool getBlend() { - return glIsEnabled(GL_BLEND); -} - -inline void setBlend(const bool& enabled) { - enabled ? MBGL_CHECK_ERROR(glEnable(GL_BLEND)) : MBGL_CHECK_ERROR(glDisable(GL_BLEND)); -} - -inline std::array getClearColor() { - std::array color; - MBGL_CHECK_ERROR(glGetFloatv(GL_COLOR_CLEAR_VALUE, color.data())); - return color; -} - -inline void setClearColor(const std::array& color) { - MBGL_CHECK_ERROR(glClearColor(color[0], color[1], color[2], color[3])); -} - - -inline std::array getBlendFunc() { - GLint func[2]; - glGetIntegerv(GL_BLEND_SRC_ALPHA, &func[0]); - glGetIntegerv(GL_BLEND_DST_ALPHA, &func[1]); - return {{ static_cast(func[0]), static_cast(func[1]) }}; -} - -inline void setBlendFunc(const std::array& func) { - MBGL_CHECK_ERROR(glBlendFunc(func[0], func[1])); -} - -#ifndef GL_ES_VERSION_2_0 -inline std::array getPixelZoom() { - std::array zoom; - glGetDoublev(GL_ZOOM_X, &zoom[0]); - glGetDoublev(GL_ZOOM_Y, &zoom[1]); - return zoom; -} - -inline void setPixelZoom(const std::array& func) { - MBGL_CHECK_ERROR(glPixelZoom(func[0], func[1])); -} - - -inline std::array getRasterPos() { - std::array pos; - MBGL_CHECK_ERROR(glGetDoublev(GL_CURRENT_RASTER_POSITION, pos.data())); - return pos; -} - -inline void setRasterPos(const std::array& pos) { - MBGL_CHECK_ERROR(glRasterPos4d(pos[0], pos[1], pos[2], pos[3])); -} -#endif -} // end anonymous namespace - namespace mbgl { namespace gl { -using PreserveBlend = Preserve; -using PreserveClearColor = Preserve, getClearColor, setClearColor>; -using PreserveBlendFunc = Preserve, getBlendFunc, setBlendFunc>; +template +class Preserve { +public: + inline Preserve() : data(T::Get()) { + } + inline ~Preserve() { + T::Set(data); + } -#ifndef GL_ES_VERSION_2_0 -using PreservePixelZoom = Preserve, getPixelZoom, setPixelZoom>; -using PreserveRasterPos = Preserve, getRasterPos, setRasterPos>; -#endif +private: + const typename T::Type data; +}; } // namespace gl } // namespace mbgl diff --git a/src/mbgl/renderer/gl_config.hpp b/include/mbgl/gl/gl_values.hpp similarity index 78% rename from src/mbgl/renderer/gl_config.hpp rename to include/mbgl/gl/gl_values.hpp index 4ab1dbb26..a5d413f5a 100644 --- a/src/mbgl/renderer/gl_config.hpp +++ b/include/mbgl/gl/gl_values.hpp @@ -1,5 +1,5 @@ -#ifndef MBGL_RENDERER_GL_CONFIG -#define MBGL_RENDERER_GL_CONFIG +#ifndef MBGL_GL_GL_VALUES +#define MBGL_GL_GL_VALUES #include #include @@ -10,32 +10,6 @@ namespace mbgl { namespace gl { -template -class Value { -public: - inline void operator=(const typename T::Type& value) { - if (dirty || current != value) { - dirty = false; - current = value; - T::Set(current); - } - } - - inline void reset() { - dirty = true; - current = T::Default; - T::Set(current); - } - - inline void setDirty() { - dirty = true; - } - -private: - typename T::Type current = T::Default; - bool dirty = false; -}; - struct ClearDepth { using Type = GLfloat; static const Type Default; @@ -266,64 +240,37 @@ struct LineWidth { } }; -class Config { -public: - void reset() { - stencilFunc.reset(); - stencilMask.reset(); - stencilTest.reset(); - stencilOp.reset(); - depthRange.reset(); - depthMask.reset(); - depthTest.reset(); - depthFunc.reset(); - blend.reset(); - blendFunc.reset(); - colorMask.reset(); - clearDepth.reset(); - clearColor.reset(); - clearStencil.reset(); - program.reset(); - lineWidth.reset(); - } +#ifndef GL_ES_VERSION_2_0 - void setDirty() { - stencilFunc.setDirty(); - stencilMask.setDirty(); - stencilTest.setDirty(); - stencilOp.setDirty(); - depthRange.setDirty(); - depthMask.setDirty(); - depthTest.setDirty(); - depthFunc.setDirty(); - blend.setDirty(); - blendFunc.setDirty(); - colorMask.setDirty(); - clearDepth.setDirty(); - clearColor.setDirty(); - clearStencil.setDirty(); - program.setDirty(); - lineWidth.setDirty(); +struct PixelZoom { + struct Type { GLfloat xfactor; GLfloat yfactor; }; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glPixelZoom(value.xfactor, value.yfactor)); + } + inline static Type Get() { + Type value; + MBGL_CHECK_ERROR(glGetFloatv(GL_ZOOM_X, &value.xfactor)); + MBGL_CHECK_ERROR(glGetFloatv(GL_ZOOM_Y, &value.yfactor)); + return value; } - - Value stencilFunc; - Value stencilMask; - Value stencilTest; - Value stencilOp; - Value depthRange; - Value depthMask; - Value depthTest; - Value depthFunc; - Value blend; - Value blendFunc; - Value colorMask; - Value clearDepth; - Value clearColor; - Value clearStencil; - Value program; - Value lineWidth; }; +struct RasterPos { + using Type = std::array; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glRasterPos4d(value[0], value[1], value[2], value[3])); + } + inline static Type Get() { + Type pos; + MBGL_CHECK_ERROR(glGetDoublev(GL_CURRENT_RASTER_POSITION, pos.data())); + return pos; + } +}; + +#endif + } // namespace gl } // namespace mbgl diff --git a/include/mbgl/platform/default/glfw_view.hpp b/include/mbgl/platform/default/glfw_view.hpp index 0e46f7d87..2f7440420 100644 --- a/include/mbgl/platform/default/glfw_view.hpp +++ b/include/mbgl/platform/default/glfw_view.hpp @@ -6,6 +6,7 @@ #ifdef MBGL_USE_GLES2 #define GLFW_INCLUDE_ES2 #endif +#define GL_GLEXT_PROTOTYPES #include #include diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp index 375b9fd62..3173bfdde 100644 --- a/platform/default/glfw_view.cpp +++ b/platform/default/glfw_view.cpp @@ -1,8 +1,9 @@ +#include #include #include #include -#include #include +#include #include #include #include @@ -496,8 +497,8 @@ void showDebugImage(std::string name, const char *data, size_t width, size_t hei float scale = static_cast(fbWidth) / static_cast(width); { - gl::PreservePixelZoom pixelZoom; - gl::PreserveRasterPos rasterPos; + gl::Preserve pixelZoom; + gl::Preserve rasterPos; MBGL_CHECK_ERROR(glPixelZoom(scale, -scale)); MBGL_CHECK_ERROR(glRasterPos2f(-1.0f, 1.0f)); @@ -533,11 +534,11 @@ void showColorDebugImage(std::string name, const char *data, size_t logicalWidth float yScale = static_cast(fbHeight) / static_cast(height); { - gl::PreserveClearColor clearColor; - gl::PreserveBlend blend; - gl::PreserveBlendFunc blendFunc; - gl::PreservePixelZoom pixelZoom; - gl::PreserveRasterPos rasterPos; + gl::Preserve clearColor; + gl::Preserve blend; + gl::Preserve blendFunc; + gl::Preserve pixelZoom; + gl::Preserve rasterPos; MBGL_CHECK_ERROR(glClearColor(0.8, 0.8, 0.8, 1)); MBGL_CHECK_ERROR(glClear(GL_COLOR_BUFFER_BIT)); diff --git a/src/mbgl/renderer/gl_config.cpp b/src/mbgl/gl/gl_config.cpp similarity index 100% rename from src/mbgl/renderer/gl_config.cpp rename to src/mbgl/gl/gl_config.cpp diff --git a/src/mbgl/gl/gl_config.hpp b/src/mbgl/gl/gl_config.hpp new file mode 100644 index 000000000..af373fc3f --- /dev/null +++ b/src/mbgl/gl/gl_config.hpp @@ -0,0 +1,100 @@ +#ifndef MBGL_GL_GL_CONFIG +#define MBGL_GL_GL_CONFIG + +#include +#include +#include + +#include + +namespace mbgl { +namespace gl { + +template +class Value { +public: + inline void operator=(const typename T::Type& value) { + if (dirty || current != value) { + dirty = false; + current = value; + T::Set(current); + } + } + + inline void reset() { + dirty = true; + current = T::Default; + T::Set(current); + } + + inline void setDirty() { + dirty = true; + } + +private: + typename T::Type current = T::Default; + bool dirty = false; +}; + +class Config { +public: + void reset() { + stencilFunc.reset(); + stencilMask.reset(); + stencilTest.reset(); + stencilOp.reset(); + depthRange.reset(); + depthMask.reset(); + depthTest.reset(); + depthFunc.reset(); + blend.reset(); + blendFunc.reset(); + colorMask.reset(); + clearDepth.reset(); + clearColor.reset(); + clearStencil.reset(); + program.reset(); + lineWidth.reset(); + } + + void setDirty() { + stencilFunc.setDirty(); + stencilMask.setDirty(); + stencilTest.setDirty(); + stencilOp.setDirty(); + depthRange.setDirty(); + depthMask.setDirty(); + depthTest.setDirty(); + depthFunc.setDirty(); + blend.setDirty(); + blendFunc.setDirty(); + colorMask.setDirty(); + clearDepth.setDirty(); + clearColor.setDirty(); + clearStencil.setDirty(); + program.setDirty(); + lineWidth.setDirty(); + } + + Value stencilFunc; + Value stencilMask; + Value stencilTest; + Value stencilOp; + Value depthRange; + Value depthMask; + Value depthTest; + Value depthFunc; + Value blend; + Value blendFunc; + Value colorMask; + Value clearDepth; + Value clearColor; + Value clearStencil; + Value program; + Value lineWidth; +}; + +} // namespace gl +} // namespace mbgl + +#endif // MBGL_RENDERER_GL_CONFIG diff --git a/src/mbgl/renderer/painter.hpp b/src/mbgl/renderer/painter.hpp index 394097883..e2949681c 100644 --- a/src/mbgl/renderer/painter.hpp +++ b/src/mbgl/renderer/painter.hpp @@ -10,7 +10,7 @@ #include #include -#include +#include #include