From e74987b07fac1c2e39ced47e2401436fb01b2a1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Thu, 4 Feb 2016 16:59:59 +0100 Subject: [core] move GL value objects from gl_config.hpp to gl_values.hpp Also removes duplicate code from gl_helper.hpp by reusing the GL values that we already have anyway. --- include/mbgl/gl/gl_helper.hpp | 85 +-------- include/mbgl/gl/gl_values.hpp | 277 ++++++++++++++++++++++++++++ include/mbgl/platform/default/glfw_view.hpp | 1 + 3 files changed, 287 insertions(+), 76 deletions(-) create mode 100644 include/mbgl/gl/gl_values.hpp (limited to 'include') diff --git a/include/mbgl/gl/gl_helper.hpp b/include/mbgl/gl/gl_helper.hpp index e1fb8c27c5..4f3990a434 100644 --- a/include/mbgl/gl/gl_helper.hpp +++ b/include/mbgl/gl/gl_helper.hpp @@ -1,89 +1,22 @@ #ifndef MBGL_GL_GL_HELPER #define MBGL_GL_GL_HELPER -#include - -#include - -namespace { +namespace mbgl { +namespace gl { -template +template class Preserve { public: - Preserve() : data(Create()) {} - ~Preserve() { Destroy(data); } + inline Preserve() : data(T::Get()) { + } + inline ~Preserve() { + T::Set(data); + } private: - const T data; + const typename T::Type 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>; - -#ifndef GL_ES_VERSION_2_0 -using PreservePixelZoom = Preserve, getPixelZoom, setPixelZoom>; -using PreserveRasterPos = Preserve, getRasterPos, setRasterPos>; -#endif - } // namespace gl } // namespace mbgl diff --git a/include/mbgl/gl/gl_values.hpp b/include/mbgl/gl/gl_values.hpp new file mode 100644 index 0000000000..a5d413f5af --- /dev/null +++ b/include/mbgl/gl/gl_values.hpp @@ -0,0 +1,277 @@ +#ifndef MBGL_GL_GL_VALUES +#define MBGL_GL_GL_VALUES + +#include +#include +#include + +#include + +namespace mbgl { +namespace gl { + +struct ClearDepth { + using Type = GLfloat; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glClearDepth(value)); + } + inline static Type Get() { + Type clearDepth; + MBGL_CHECK_ERROR(glGetFloatv(GL_DEPTH_CLEAR_VALUE, &clearDepth)); + return clearDepth; + } +}; + +struct ClearColor { + struct Type { GLfloat r, g, b, a; }; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glClearColor(value.r, value.g, value.b, value.a)); + } + inline static Type Get() { + GLfloat floats[4]; + MBGL_CHECK_ERROR(glGetFloatv(GL_COLOR_CLEAR_VALUE, floats)); + return { floats[0], floats[1], floats[2], floats[3] }; + } +}; + +inline bool operator!=(const ClearColor::Type& a, const ClearColor::Type& b) { + return a.r != b.r || a.g != b.g || a.b != b.b || a.a != b.a; +} + +struct ClearStencil { + using Type = GLint; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glClearStencil(value)); + } + inline static Type Get() { + Type clearStencil; + MBGL_CHECK_ERROR(glGetIntegerv(GL_STENCIL_CLEAR_VALUE, &clearStencil)); + return clearStencil; + } +}; + +struct StencilMask { + using Type = GLuint; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glStencilMask(value)); + } + inline static Type Get() { + GLint stencilMask; + MBGL_CHECK_ERROR(glGetIntegerv(GL_STENCIL_WRITEMASK, &stencilMask)); + return stencilMask; + } +}; + +struct DepthMask { + using Type = GLboolean; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glDepthMask(value)); + } + inline static Type Get() { + Type depthMask; + MBGL_CHECK_ERROR(glGetBooleanv(GL_DEPTH_WRITEMASK, &depthMask)); + return depthMask; + } +}; + +struct ColorMask { + struct Type { bool r, g, b, a; }; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glColorMask(value.r, value.g, value.b, value.a)); + } + inline static Type Get() { + GLboolean bools[4]; + MBGL_CHECK_ERROR(glGetBooleanv(GL_COLOR_WRITEMASK, bools)); + return { static_cast(bools[0]), static_cast(bools[1]), + static_cast(bools[2]), static_cast(bools[3]) }; + } +}; + +inline bool operator!=(const ColorMask::Type& a, const ColorMask::Type& b) { + return a.r != b.r || a.g != b.g || a.b != b.b || a.a != b.a; +} + +struct StencilFunc { + struct Type { GLenum func; GLint ref; GLuint mask; }; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glStencilFunc(value.func, value.ref, value.mask)); + } + inline static Type Get() { + GLint func, ref, mask; + MBGL_CHECK_ERROR(glGetIntegerv(GL_STENCIL_FUNC, &func)); + MBGL_CHECK_ERROR(glGetIntegerv(GL_STENCIL_REF, &ref)); + MBGL_CHECK_ERROR(glGetIntegerv(GL_STENCIL_VALUE_MASK, &mask)); + return { static_cast(func), ref, static_cast(mask) }; + } +}; + +inline bool operator!=(const StencilFunc::Type& a, const StencilFunc::Type& b) { + return a.func != b.func || a.ref != b.ref || a.mask != b.mask; +} + +struct StencilTest { + using Type = bool; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(value ? glEnable(GL_STENCIL_TEST) : glDisable(GL_STENCIL_TEST)); + } + inline static Type Get() { + Type stencilTest; + MBGL_CHECK_ERROR(stencilTest = glIsEnabled(GL_STENCIL_TEST)); + return stencilTest; + } +}; + +struct StencilOp { + struct Type { GLenum sfail, dpfail, dppass; }; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glStencilOp(value.sfail, value.dpfail, value.dppass)); + } + inline static Type Get() { + GLint sfail, dpfail, dppass; + MBGL_CHECK_ERROR(glGetIntegerv(GL_STENCIL_FAIL, &sfail)); + MBGL_CHECK_ERROR(glGetIntegerv(GL_STENCIL_PASS_DEPTH_FAIL, &dpfail)); + MBGL_CHECK_ERROR(glGetIntegerv(GL_STENCIL_PASS_DEPTH_PASS, &dppass)); + return { static_cast(sfail), static_cast(dpfail), static_cast(dppass) }; + } +}; + +struct DepthRange { + struct Type { GLfloat near, far; }; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glDepthRange(value.near, value.far)); + } + inline static Type Get() { + GLfloat floats[2]; + MBGL_CHECK_ERROR(glGetFloatv(GL_DEPTH_RANGE, floats)); + return { floats[0], floats[1] }; + } +}; + +inline bool operator!=(const DepthRange::Type& a, const DepthRange::Type& b) { + return a.near != b.near || a.far != b.far; +} + +struct DepthTest { + using Type = bool; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(value ? glEnable(GL_DEPTH_TEST) : glDisable(GL_DEPTH_TEST)); + } + inline static Type Get() { + Type depthTest; + MBGL_CHECK_ERROR(depthTest = glIsEnabled(GL_DEPTH_TEST)); + return depthTest; + } +}; + +struct DepthFunc { + using Type = GLenum; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glDepthFunc(value)); + } + inline static Type Get() { + GLint depthFunc; + MBGL_CHECK_ERROR(glGetIntegerv(GL_DEPTH_FUNC, &depthFunc)); + return depthFunc; + } +}; + +struct Blend { + using Type = bool; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(value ? glEnable(GL_BLEND) : glDisable(GL_BLEND)); + } + inline static Type Get() { + Type blend; + MBGL_CHECK_ERROR(blend = glIsEnabled(GL_BLEND)); + return blend; + } +}; + +struct BlendFunc { + struct Type { GLenum sfactor, dfactor; }; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glBlendFunc(value.sfactor, value.dfactor)); + } + inline static Type Get() { + GLint sfactor, dfactor; + MBGL_CHECK_ERROR(glGetIntegerv(GL_BLEND_SRC_ALPHA, &sfactor)); + MBGL_CHECK_ERROR(glGetIntegerv(GL_BLEND_DST_ALPHA, &dfactor)); + return { static_cast(sfactor), static_cast(dfactor) }; + } +}; + +struct Program { + using Type = GLuint; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glUseProgram(value)); + } + inline static Type Get() { + GLint program; + MBGL_CHECK_ERROR(glGetIntegerv(GL_CURRENT_PROGRAM, &program)); + return program; + } +}; + +struct LineWidth { + using Type = GLfloat; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glLineWidth(value)); + } + inline static Type Get() { + Type lineWidth; + MBGL_CHECK_ERROR(glGetFloatv(GL_LINE_WIDTH, &lineWidth)); + return lineWidth; + } +}; + +#ifndef GL_ES_VERSION_2_0 + +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; + } +}; + +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 + +#endif // MBGL_RENDERER_GL_CONFIG diff --git a/include/mbgl/platform/default/glfw_view.hpp b/include/mbgl/platform/default/glfw_view.hpp index 0e46f7d87e..2f74404201 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 -- cgit v1.2.1