diff --git a/cmake/core-files.cmake b/cmake/core-files.cmake index a885e182a..a809eb2a6 100644 --- a/cmake/core-files.cmake +++ b/cmake/core-files.cmake @@ -78,14 +78,15 @@ set(MBGL_CORE_FILES # gl include/mbgl/gl/gl.hpp include/mbgl/gl/gl_helper.hpp - include/mbgl/gl/gl_values.hpp src/mbgl/gl/debugging.cpp src/mbgl/gl/debugging.hpp src/mbgl/gl/gl.cpp src/mbgl/gl/gl_config.hpp - src/mbgl/gl/gl_values.cpp src/mbgl/gl/object_store.cpp src/mbgl/gl/object_store.hpp + src/mbgl/gl/state.hpp + src/mbgl/gl/value.cpp + src/mbgl/gl/value.hpp # layout src/mbgl/layout/clip_lines.cpp diff --git a/include/mbgl/gl/gl_helper.hpp b/include/mbgl/gl/gl_helper.hpp deleted file mode 100644 index 9d46d018c..000000000 --- a/include/mbgl/gl/gl_helper.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -namespace mbgl { -namespace gl { - -template -class Preserve { -public: - Preserve() : data(T::Get()) { - } - ~Preserve() { - T::Set(data); - } - -private: - const typename T::Type data; -}; - -} // namespace gl -} // namespace mbgl diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp index 6995c6e9b..17b233a41 100644 --- a/platform/default/glfw_view.cpp +++ b/platform/default/glfw_view.cpp @@ -3,14 +3,15 @@ #include #include #include -#include -#include #include #include #include #include #include +#include +#include + #include #include @@ -533,8 +534,8 @@ void showDebugImage(std::string name, const char *data, size_t width, size_t hei float scale = static_cast(fbWidth) / static_cast(width); { - gl::Preserve pixelZoom; - gl::Preserve rasterPos; + gl::PreserveState pixelZoom; + gl::PreserveState rasterPos; MBGL_CHECK_ERROR(glPixelZoom(scale, -scale)); MBGL_CHECK_ERROR(glRasterPos2f(-1.0f, 1.0f)); @@ -570,11 +571,11 @@ void showColorDebugImage(std::string name, const char *data, size_t logicalWidth float yScale = static_cast(fbHeight) / static_cast(height); { - gl::Preserve clearColor; - gl::Preserve blend; - gl::Preserve blendFunc; - gl::Preserve pixelZoom; - gl::Preserve rasterPos; + gl::PreserveState clearColor; + gl::PreserveState blend; + gl::PreserveState blendFunc; + gl::PreserveState pixelZoom; + gl::PreserveState 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/gl/gl_config.hpp b/src/mbgl/gl/gl_config.hpp index 225f4f8de..fed79594e 100644 --- a/src/mbgl/gl/gl_config.hpp +++ b/src/mbgl/gl/gl_config.hpp @@ -1,72 +1,14 @@ #pragma once -#include -#include -#include - -#include +#include +#include namespace mbgl { namespace gl { - -template -struct DefaultValue { - static typename T::Type Get() { - return T::Get(); - } -}; -template -struct DefaultValue { - static typename T::Type Get() { - return T::Default; - } -}; - -template -class Value { -public: - void operator=(const typename T::Type& value) { - if (*this != value) { - dirty = false; - current = value; - T::Set(current); - } - } - - bool operator!=(const typename T::Type& value) const { - return dirty || current != value; - } - - void reset() { - *this = defaultValue; - } - - void setDirty() { - dirty = true; - } - - typename T::Type getCurrent() const { - return current; - } - - bool getDirty() const { - return dirty; - } - - void setDefaultValue(const typename T::Type& value) { - defaultValue = value; - } - -private: - typename T::Type defaultValue = DefaultValue::Get(); - typename T::Type current = defaultValue; - bool dirty = false; -}; - class Config { public: - void reset() { + void resetState() { stencilFunc.reset(); stencilMask.reset(); stencilTest.reset(); @@ -91,12 +33,15 @@ public: pixelZoom.reset(); rasterPos.reset(); #endif // GL_ES_VERSION_2_0 + for (auto& tex : texture) { + tex.reset(); + } vertexBuffer.reset(); elementBuffer.reset(); vertexArrayObject.reset(); } - void setDirty() { + void setDirtyState() { stencilFunc.setDirty(); stencilMask.setDirty(); stencilTest.setDirty(); @@ -121,39 +66,42 @@ public: pixelZoom.setDirty(); rasterPos.setDirty(); #endif // GL_ES_VERSION_2_0 + for (auto& tex : texture) { + tex.setDirty(); + } vertexBuffer.setDirty(); elementBuffer.setDirty(); vertexArrayObject.setDirty(); } - Value stencilFunc; - Value stencilMask; - Value stencilTest; - Value stencilOp; - Value depthRange; - Value depthMask; - Value depthTest; - Value depthFunc; - Value blend; - Value blendFunc; - Value blendColor; - Value colorMask; - Value clearDepth; - Value clearColor; - Value clearStencil; - Value program; - Value lineWidth; - Value activeTexture; - Value bindFramebuffer; - Value viewport; + State stencilFunc; + State stencilMask; + State stencilTest; + State stencilOp; + State depthRange; + State depthMask; + State depthTest; + State depthFunc; + State blend; + State blendFunc; + State blendColor; + State colorMask; + State clearDepth; + State clearColor; + State clearStencil; + State program; + State lineWidth; + State activeTexture; + State bindFramebuffer; + State viewport; #ifndef GL_ES_VERSION_2_0 - Value pixelZoom; - Value rasterPos; + State pixelZoom; + State rasterPos; #endif // GL_ES_VERSION_2_0 - std::array, 2> texture; - Value> vertexBuffer; - Value> elementBuffer; - Value vertexArrayObject; + std::array, 2> texture; + State> vertexBuffer; + State> elementBuffer; + State vertexArrayObject; }; } // namespace gl diff --git a/src/mbgl/gl/state.hpp b/src/mbgl/gl/state.hpp new file mode 100644 index 000000000..274ae5e2f --- /dev/null +++ b/src/mbgl/gl/state.hpp @@ -0,0 +1,90 @@ +#pragma once + +namespace mbgl { +namespace gl { + +// Helper struct that allows obtaining the default value of a Value class +template +struct DefaultValue { + static typename T::Type Get() { + return T::Get(); + } +}; +template +struct DefaultValue { + static typename T::Type Get() { + return T::Default; + } +}; + +// Wraps a piece of OpenGL state and remember its value to avoid redundant state calls. +// Wrapped types need to implement to the Value class interface: +// +// class Value { +// using Type = ...; +// static const constexpr Type Default = ...; +// static void Set(const Type& value); +// static Type Get(); +// }; +// +// The Get() function is optional, but if it is omitted, you must provide a Default. +// Default is also optional, but if it is omitted, you must provide a Get() function. +// If both are present, DefaultValue::Get() will use the Default member. +template +class State { +public: + void operator=(const typename T::Type& value) { + if (*this != value) { + dirty = false; + currentValue = value; + T::Set(currentValue); + } + } + + bool operator!=(const typename T::Type& value) const { + return dirty || currentValue != value; + } + + void reset() { + *this = defaultValue; + } + + void setDirty() { + dirty = true; + } + + typename T::Type getCurrentValue() const { + return currentValue; + } + + bool isDirty() const { + return dirty; + } + + void setDefaultValue(const typename T::Type& value) { + defaultValue = value; + } + +private: + typename T::Type defaultValue = DefaultValue::Get(); + typename T::Type currentValue = defaultValue; + bool dirty = false; +}; + +// Helper struct that stores the current state and restores it upon destruction. You should not use +// this code normally, except for debugging purposes. +template +class PreserveState { +public: + PreserveState() : value(T::Get()) { + } + ~PreserveState() { + T::Set(value); + } + +private: + const typename T::Type value; +}; + +} // namespace gl +} // namespace mbgl diff --git a/src/mbgl/gl/gl_values.cpp b/src/mbgl/gl/value.cpp similarity index 95% rename from src/mbgl/gl/gl_values.cpp rename to src/mbgl/gl/value.cpp index c987c65aa..7df3f4e3b 100644 --- a/src/mbgl/gl/gl_values.cpp +++ b/src/mbgl/gl/value.cpp @@ -1,7 +1,8 @@ -#include +#include namespace mbgl { namespace gl { +namespace value { const constexpr StencilFunc::Type StencilFunc::Default; const constexpr StencilMask::Type StencilMask::Default; @@ -29,5 +30,6 @@ const constexpr PixelZoom::Type PixelZoom::Default; const constexpr RasterPos::Type RasterPos::Default; #endif // GL_ES_VERSION_2_0 +} // namespace value } // namespace gl } // namespace mbgl diff --git a/include/mbgl/gl/gl_values.hpp b/src/mbgl/gl/value.hpp similarity index 99% rename from include/mbgl/gl/gl_values.hpp rename to src/mbgl/gl/value.hpp index fa3b87c43..6436228ed 100644 --- a/include/mbgl/gl/gl_values.hpp +++ b/src/mbgl/gl/value.hpp @@ -10,6 +10,7 @@ namespace mbgl { namespace gl { +namespace value { struct ClearDepth { using Type = GLfloat; @@ -376,5 +377,6 @@ struct BindVAO { }; +} // namespace value } // namespace gl } // namespace mbgl diff --git a/src/mbgl/renderer/painter.cpp b/src/mbgl/renderer/painter.cpp index 438e4b5e4..2b3189cc5 100644 --- a/src/mbgl/renderer/painter.cpp +++ b/src/mbgl/renderer/painter.cpp @@ -48,8 +48,8 @@ Painter::Painter(const TransformState& state_, #endif // Reset GL values - config.setDirty(); - config.reset(); + config.setDirtyState(); + config.resetState(); } Painter::~Painter() = default; @@ -225,7 +225,7 @@ void Painter::render(const Style& style, const FrameData& frame_, SpriteAtlas& a } if (frame.contextMode == GLContextMode::Shared) { - config.setDirty(); + config.setDirtyState(); } } @@ -276,7 +276,7 @@ void Painter::renderPass(PaintParameters& parameters, config.stencilTest = GL_FALSE; setDepthSublayer(0); layer.as()->impl->render(state); - config.setDirty(); + config.setDirtyState(); config.bindFramebuffer.reset(); config.viewport.reset(); } else { diff --git a/src/mbgl/renderer/painter_debug.cpp b/src/mbgl/renderer/painter_debug.cpp index 8c15070cd..adecc2051 100644 --- a/src/mbgl/renderer/painter_debug.cpp +++ b/src/mbgl/renderer/painter_debug.cpp @@ -6,8 +6,6 @@ #include #include #include -#include -#include #include namespace mbgl { diff --git a/test/gl/object.cpp b/test/gl/object.cpp index 5733a15e4..68cd2ddea 100644 --- a/test/gl/object.cpp +++ b/test/gl/object.cpp @@ -4,7 +4,6 @@ #include #include -#include #include #include @@ -26,11 +25,11 @@ struct MockGLObject { const bool MockGLObject::Default = false; -TEST(GLObject, Preserve) { +TEST(GLObject, PreserveState) { getFlag = false; setFlag = false; - auto object = std::make_unique>(); + auto object = std::make_unique>(); EXPECT_TRUE(getFlag); EXPECT_FALSE(setFlag); @@ -43,28 +42,28 @@ TEST(GLObject, Preserve) { TEST(GLObject, Value) { setFlag = false; - auto object = std::make_unique>(); - EXPECT_EQ(object->getCurrent(), false); - EXPECT_FALSE(object->getDirty()); + auto object = std::make_unique>(); + EXPECT_EQ(object->getCurrentValue(), false); + EXPECT_FALSE(object->isDirty()); EXPECT_FALSE(setFlag); object->setDirty(); - EXPECT_TRUE(object->getDirty()); + EXPECT_TRUE(object->isDirty()); *object = false; - EXPECT_EQ(object->getCurrent(), false); - EXPECT_FALSE(object->getDirty()); + EXPECT_EQ(object->getCurrentValue(), false); + EXPECT_FALSE(object->isDirty()); EXPECT_TRUE(setFlag); setFlag = false; *object = true; - EXPECT_EQ(object->getCurrent(), true); - EXPECT_FALSE(object->getDirty()); + EXPECT_EQ(object->getCurrentValue(), true); + EXPECT_FALSE(object->isDirty()); EXPECT_TRUE(setFlag); object->reset(); - EXPECT_EQ(object->getCurrent(), false); - EXPECT_FALSE(object->getDirty()); + EXPECT_EQ(object->getCurrentValue(), false); + EXPECT_FALSE(object->isDirty()); EXPECT_TRUE(setFlag); } diff --git a/test/util/offscreen_texture.cpp b/test/util/offscreen_texture.cpp index 74a616134..b4d98078b 100644 --- a/test/util/offscreen_texture.cpp +++ b/test/util/offscreen_texture.cpp @@ -130,7 +130,7 @@ void main() { test::checkImage("test/fixtures/offscreen_texture/render-to-texture", image, 0, 0); // Now reset the FBO back to normal and retrieve the original (restored) framebuffer. - config.reset(); + config.resetState(); image = view.readStillImage(); test::checkImage("test/fixtures/offscreen_texture/render-to-fbo", image, 0, 0);