From 008a83b91584b8cf406264c5378bdd7ef87683ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Fri, 4 Nov 2016 17:01:22 +0100 Subject: [PATCH] [core] add state tracking to pixel store packing alignment --- src/mbgl/gl/context.cpp | 2 ++ src/mbgl/gl/context.hpp | 2 ++ src/mbgl/gl/types.hpp | 12 ++++++++++++ src/mbgl/gl/value.cpp | 29 ++++++++++++++++++++++++++++- src/mbgl/gl/value.hpp | 16 +++++++++++++++- src/mbgl/renderer/painter_debug.cpp | 18 ++++++++---------- 6 files changed, 67 insertions(+), 12 deletions(-) diff --git a/src/mbgl/gl/context.cpp b/src/mbgl/gl/context.cpp index fc3502847..ceb9796ba 100644 --- a/src/mbgl/gl/context.cpp +++ b/src/mbgl/gl/context.cpp @@ -283,6 +283,8 @@ void Context::setDirtyState() { pointSize.setDirty(); pixelZoom.setDirty(); rasterPos.setDirty(); + pixelStorePack.setDirty(); + pixelStoreUnpack.setDirty(); #endif // MBGL_USE_GLES2 for (auto& tex : texture) { tex.setDirty(); diff --git a/src/mbgl/gl/context.hpp b/src/mbgl/gl/context.hpp index e0f9b871c..b3edd5563 100644 --- a/src/mbgl/gl/context.hpp +++ b/src/mbgl/gl/context.hpp @@ -130,6 +130,8 @@ public: #if not MBGL_USE_GLES2 State pixelZoom; State rasterPos; + State pixelStorePack; + State pixelStoreUnpack; #endif // MBGL_USE_GLES2 private: diff --git a/src/mbgl/gl/types.hpp b/src/mbgl/gl/types.hpp index 16a37d58c..6e1bc5f8e 100644 --- a/src/mbgl/gl/types.hpp +++ b/src/mbgl/gl/types.hpp @@ -62,5 +62,17 @@ enum class PrimitiveType { TriangleFan = 0x0006 }; +#if not MBGL_USE_GLES2 + +struct PixelStorageType { + int32_t alignment; +}; + +constexpr bool operator!=(const PixelStorageType& a, const PixelStorageType& b) { + return a.alignment != b.alignment; +} + +#endif // MBGL_USE_GLES2 + } // namespace gl } // namespace mbgl diff --git a/src/mbgl/gl/value.cpp b/src/mbgl/gl/value.cpp index f25008854..af4f03a13 100644 --- a/src/mbgl/gl/value.cpp +++ b/src/mbgl/gl/value.cpp @@ -389,8 +389,35 @@ RasterPos::Type RasterPos::Get() { return { pos[0], pos[1], pos[2], pos[3] }; } -#endif // MBGL_USE_GLES2 +const constexpr PixelStorePack::Type PixelStorePack::Default; +void PixelStorePack::Set(const Type& value) { + assert(value.alignment == 1 || value.alignment == 2 || value.alignment == 4 || + value.alignment == 8); + MBGL_CHECK_ERROR(glPixelStorei(GL_PACK_ALIGNMENT, value.alignment)); +} + +PixelStorePack::Type PixelStorePack::Get() { + Type value; + MBGL_CHECK_ERROR(glGetIntegerv(GL_PACK_ALIGNMENT, &value.alignment)); + return value; +} + +const constexpr PixelStoreUnpack::Type PixelStoreUnpack::Default; + +void PixelStoreUnpack::Set(const Type& value) { + assert(value.alignment == 1 || value.alignment == 2 || value.alignment == 4 || + value.alignment == 8); + MBGL_CHECK_ERROR(glPixelStorei(GL_UNPACK_ALIGNMENT, value.alignment)); +} + +PixelStoreUnpack::Type PixelStoreUnpack::Get() { + Type value; + MBGL_CHECK_ERROR(glGetIntegerv(GL_UNPACK_ALIGNMENT, &value.alignment)); + return value; +} + +#endif // MBGL_USE_GLES2 } // namespace value } // namespace gl diff --git a/src/mbgl/gl/value.hpp b/src/mbgl/gl/value.hpp index 1f01d4e71..613bbfcab 100644 --- a/src/mbgl/gl/value.hpp +++ b/src/mbgl/gl/value.hpp @@ -259,7 +259,7 @@ struct RasterPos { double z; double w; }; - static const constexpr Type Default = { 0, 0, 0, 0 }; + static const constexpr Type Default = { 0, 0, 0, 1 }; static void Set(const Type&); static Type Get(); }; @@ -268,6 +268,20 @@ constexpr bool operator!=(const RasterPos::Type& a, const RasterPos::Type& b) { return a.x != b.x || a.y != b.y || a.z != b.z || a.w != b.w; } +struct PixelStorePack { + using Type = PixelStorageType; + static const constexpr Type Default = { 4 }; + static void Set(const Type&); + static Type Get(); +}; + +struct PixelStoreUnpack { + using Type = PixelStorageType; + static const constexpr Type Default = { 4 }; + static void Set(const Type&); + static Type Get(); +}; + #endif // MBGL_USE_GLES2 } // namespace value diff --git a/src/mbgl/renderer/painter_debug.cpp b/src/mbgl/renderer/painter_debug.cpp index a502c3edd..1db2167b7 100644 --- a/src/mbgl/renderer/painter_debug.cpp +++ b/src/mbgl/renderer/painter_debug.cpp @@ -68,12 +68,10 @@ void Painter::renderClipMasks(PaintParameters&) { context.program = 0; #if not MBGL_USE_GLES2 - context.pixelZoom = { 1, 1 }; - context.rasterPos = { -1, -1, 0, 0 }; // When reading data from the framebuffer, make sure that we are storing the depth values // tightly packed into the buffer to avoid buffer overruns. Also see unpacking adjustment below. - MBGL_CHECK_ERROR(glPixelStorei(GL_PACK_ALIGNMENT, 1)); + context.pixelStorePack = { 1 }; // Read the stencil buffer const auto viewport = context.viewport.getCurrentValue(); @@ -96,8 +94,9 @@ void Painter::renderClipMasks(PaintParameters&) { *it *= factor; } - MBGL_CHECK_ERROR(glPixelStorei(GL_UNPACK_ALIGNMENT, 1)); - MBGL_CHECK_ERROR(glWindowPos2i(viewport.x, viewport.y)); + context.pixelZoom = { 1, 1 }; + context.rasterPos = { -1, -1, 0, 1 }; + context.pixelStoreUnpack = { 1 }; MBGL_CHECK_ERROR(glDrawPixels(viewport.size.width, viewport.size.height, GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels.get())); #endif // MBGL_USE_GLES2 @@ -110,12 +109,10 @@ void Painter::renderDepthBuffer(PaintParameters&) { context.program = 0; #if not MBGL_USE_GLES2 - context.pixelZoom = { 1, 1 }; - context.rasterPos = { -1, -1, 0, 0 }; // When reading data from the framebuffer, make sure that we are storing the depth values // tightly packed into the buffer to avoid buffer overruns. Also see unpacking adjustment below. - MBGL_CHECK_ERROR(glPixelStorei(GL_PACK_ALIGNMENT, 1)); + context.pixelStorePack = { 1 }; // Scales the values in the depth buffer so that they cover the entire grayscale range. This // makes it easier to spot tiny differences. @@ -137,8 +134,9 @@ void Painter::renderDepthBuffer(PaintParameters&) { pixels.get() // GLvoid * data )); - MBGL_CHECK_ERROR(glPixelStorei(GL_UNPACK_ALIGNMENT, 1)); - MBGL_CHECK_ERROR(glWindowPos2i(viewport.x, viewport.y)); + context.pixelZoom = { 1, 1 }; + context.rasterPos = { -1, -1, 0, 1 }; + context.pixelStoreUnpack = { 1 }; MBGL_CHECK_ERROR(glDrawPixels(viewport.size.width, viewport.size.height, GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels.get())); #endif // MBGL_USE_GLES2