[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.
This commit is contained in:
Konstantin Käfer
2016-02-04 16:59:59 +01:00
parent 5c3b01ffcf
commit e74987b07f
7 changed files with 151 additions and 169 deletions

View File

@@ -1,88 +1,21 @@
#ifndef MBGL_GL_GL_HELPER
#define MBGL_GL_GL_HELPER
#include <mbgl/gl/gl.hpp>
#include <array>
namespace {
template <typename T, T (*Create)(), void (*Destroy)(const T&)>
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<float, 4> getClearColor() {
std::array<float, 4> color;
MBGL_CHECK_ERROR(glGetFloatv(GL_COLOR_CLEAR_VALUE, color.data()));
return color;
}
inline void setClearColor(const std::array<float, 4>& color) {
MBGL_CHECK_ERROR(glClearColor(color[0], color[1], color[2], color[3]));
}
inline std::array<GLenum, 2> getBlendFunc() {
GLint func[2];
glGetIntegerv(GL_BLEND_SRC_ALPHA, &func[0]);
glGetIntegerv(GL_BLEND_DST_ALPHA, &func[1]);
return {{ static_cast<GLenum>(func[0]), static_cast<GLenum>(func[1]) }};
}
inline void setBlendFunc(const std::array<GLenum, 2>& func) {
MBGL_CHECK_ERROR(glBlendFunc(func[0], func[1]));
}
#ifndef GL_ES_VERSION_2_0
inline std::array<double, 2> getPixelZoom() {
std::array<double, 2> zoom;
glGetDoublev(GL_ZOOM_X, &zoom[0]);
glGetDoublev(GL_ZOOM_Y, &zoom[1]);
return zoom;
}
inline void setPixelZoom(const std::array<double, 2>& func) {
MBGL_CHECK_ERROR(glPixelZoom(func[0], func[1]));
}
inline std::array<double, 4> getRasterPos() {
std::array<double, 4> pos;
MBGL_CHECK_ERROR(glGetDoublev(GL_CURRENT_RASTER_POSITION, pos.data()));
return pos;
}
inline void setRasterPos(const std::array<double, 4>& pos) {
MBGL_CHECK_ERROR(glRasterPos4d(pos[0], pos[1], pos[2], pos[3]));
}
#endif
} // end anonymous namespace
namespace mbgl {
namespace gl {
using PreserveBlend = Preserve<bool, getBlend, setBlend>;
using PreserveClearColor = Preserve<std::array<float, 4>, getClearColor, setClearColor>;
using PreserveBlendFunc = Preserve<std::array<GLenum, 2>, getBlendFunc, setBlendFunc>;
template <typename T>
class Preserve {
public:
inline Preserve() : data(T::Get()) {
}
inline ~Preserve() {
T::Set(data);
}
#ifndef GL_ES_VERSION_2_0
using PreservePixelZoom = Preserve<std::array<double, 2>, getPixelZoom, setPixelZoom>;
using PreserveRasterPos = Preserve<std::array<double, 4>, getRasterPos, setRasterPos>;
#endif
private:
const typename T::Type data;
};
} // namespace gl
} // namespace mbgl

View File

@@ -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 <cstdint>
#include <tuple>
@@ -10,32 +10,6 @@
namespace mbgl {
namespace gl {
template <typename T>
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> stencilFunc;
Value<StencilMask> stencilMask;
Value<StencilTest> stencilTest;
Value<StencilOp> stencilOp;
Value<DepthRange> depthRange;
Value<DepthMask> depthMask;
Value<DepthTest> depthTest;
Value<DepthFunc> depthFunc;
Value<Blend> blend;
Value<BlendFunc> blendFunc;
Value<ColorMask> colorMask;
Value<ClearDepth> clearDepth;
Value<ClearColor> clearColor;
Value<ClearStencil> clearStencil;
Value<Program> program;
Value<LineWidth> lineWidth;
};
struct RasterPos {
using Type = std::array<GLdouble, 4>;
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

View File

@@ -6,6 +6,7 @@
#ifdef MBGL_USE_GLES2
#define GLFW_INCLUDE_ES2
#endif
#define GL_GLEXT_PROTOTYPES
#include <GLFW/glfw3.h>
#include <atomic>

View File

@@ -1,8 +1,9 @@
#include <mbgl/platform/default/glfw_view.hpp>
#include <mbgl/annotation/point_annotation.hpp>
#include <mbgl/annotation/shape_annotation.hpp>
#include <mbgl/sprite/sprite_image.hpp>
#include <mbgl/platform/default/glfw_view.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/gl/gl_values.hpp>
#include <mbgl/gl/gl_helper.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/util/string.hpp>
@@ -496,8 +497,8 @@ void showDebugImage(std::string name, const char *data, size_t width, size_t hei
float scale = static_cast<float>(fbWidth) / static_cast<float>(width);
{
gl::PreservePixelZoom pixelZoom;
gl::PreserveRasterPos rasterPos;
gl::Preserve<gl::PixelZoom> pixelZoom;
gl::Preserve<gl::RasterPos> 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<float>(fbHeight) / static_cast<float>(height);
{
gl::PreserveClearColor clearColor;
gl::PreserveBlend blend;
gl::PreserveBlendFunc blendFunc;
gl::PreservePixelZoom pixelZoom;
gl::PreserveRasterPos rasterPos;
gl::Preserve<gl::ClearColor> clearColor;
gl::Preserve<gl::Blend> blend;
gl::Preserve<gl::BlendFunc> blendFunc;
gl::Preserve<gl::PixelZoom> pixelZoom;
gl::Preserve<gl::RasterPos> rasterPos;
MBGL_CHECK_ERROR(glClearColor(0.8, 0.8, 0.8, 1));
MBGL_CHECK_ERROR(glClear(GL_COLOR_BUFFER_BIT));

100
src/mbgl/gl/gl_config.hpp Normal file
View File

@@ -0,0 +1,100 @@
#ifndef MBGL_GL_GL_CONFIG
#define MBGL_GL_GL_CONFIG
#include <cstdint>
#include <tuple>
#include <array>
#include <mbgl/gl/gl_values.hpp>
namespace mbgl {
namespace gl {
template <typename T>
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> stencilFunc;
Value<StencilMask> stencilMask;
Value<StencilTest> stencilTest;
Value<StencilOp> stencilOp;
Value<DepthRange> depthRange;
Value<DepthMask> depthMask;
Value<DepthTest> depthTest;
Value<DepthFunc> depthFunc;
Value<Blend> blend;
Value<BlendFunc> blendFunc;
Value<ColorMask> colorMask;
Value<ClearDepth> clearDepth;
Value<ClearColor> clearColor;
Value<ClearStencil> clearStencil;
Value<Program> program;
Value<LineWidth> lineWidth;
};
} // namespace gl
} // namespace mbgl
#endif // MBGL_RENDERER_GL_CONFIG

View File

@@ -10,7 +10,7 @@
#include <mbgl/geometry/vao.hpp>
#include <mbgl/geometry/static_vertex_buffer.hpp>
#include <mbgl/renderer/gl_config.hpp>
#include <mbgl/gl/gl_config.hpp>
#include <mbgl/style/types.hpp>