From a05f060153497e1be6515120b65f580f4551f9e0 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Thu, 1 Jun 2017 21:32:26 +0300 Subject: [core] Added Backend::{assume,set}ScissorTest --- include/mbgl/map/backend.hpp | 2 ++ platform/default/mbgl/gl/offscreen_view.cpp | 1 + src/mbgl/gl/context.cpp | 4 ++-- src/mbgl/gl/context.hpp | 1 + src/mbgl/gl/value.cpp | 12 ++++++++++++ src/mbgl/gl/value.hpp | 7 +++++++ src/mbgl/map/backend.cpp | 10 ++++++++++ src/mbgl/util/offscreen_texture.cpp | 1 + test/util/offscreen_texture.test.cpp | 10 ++++++++++ 9 files changed, 46 insertions(+), 2 deletions(-) diff --git a/include/mbgl/map/backend.hpp b/include/mbgl/map/backend.hpp index 23a75b82ae..2e73ad994c 100644 --- a/include/mbgl/map/backend.hpp +++ b/include/mbgl/map/backend.hpp @@ -62,6 +62,7 @@ protected: // It sets the internal assumed state to the supplied values. void assumeFramebufferBinding(gl::FramebufferID fbo); void assumeViewport(int32_t x, int32_t y, const Size&); + void assumeScissorTest(bool); // Returns true when assumed framebuffer binding hasn't changed from the implicit binding. bool implicitFramebufferBound(); @@ -70,6 +71,7 @@ protected: // supplied values. void setFramebufferBinding(gl::FramebufferID fbo); void setViewport(int32_t x, int32_t y, const Size&); + void setScissorTest(bool); protected: std::unique_ptr context; diff --git a/platform/default/mbgl/gl/offscreen_view.cpp b/platform/default/mbgl/gl/offscreen_view.cpp index 5424e03c96..e7cf7cffe5 100644 --- a/platform/default/mbgl/gl/offscreen_view.cpp +++ b/platform/default/mbgl/gl/offscreen_view.cpp @@ -24,6 +24,7 @@ public: context.bindFramebuffer = framebuffer->framebuffer; } + context.scissorTest = false; context.viewport = { 0, 0, size }; } diff --git a/src/mbgl/gl/context.cpp b/src/mbgl/gl/context.cpp index 6a68c3ff69..2a36100dfd 100644 --- a/src/mbgl/gl/context.cpp +++ b/src/mbgl/gl/context.cpp @@ -468,8 +468,8 @@ void Context::reset() { } void Context::setDirtyState() { - // Note: does not set viewport/bindFramebuffer to dirty since they are handled separately in - // the view object. + // Note: does not set viewport/scissorTest/bindFramebuffer to dirty + // since they are handled separately in the view object. stencilFunc.setDirty(); stencilMask.setDirty(); stencilTest.setDirty(); diff --git a/src/mbgl/gl/context.hpp b/src/mbgl/gl/context.hpp index 56c0618989..9086b8c955 100644 --- a/src/mbgl/gl/context.hpp +++ b/src/mbgl/gl/context.hpp @@ -199,6 +199,7 @@ public: State activeTexture; State bindFramebuffer; State viewport; + State scissorTest; std::array, 2> texture; State vertexArrayObject { *this }; State program; diff --git a/src/mbgl/gl/value.cpp b/src/mbgl/gl/value.cpp index c081c941f5..32c632d101 100644 --- a/src/mbgl/gl/value.cpp +++ b/src/mbgl/gl/value.cpp @@ -267,6 +267,18 @@ Viewport::Type Viewport::Get() { { static_cast(viewport[2]), static_cast(viewport[3]) } }; } +const constexpr ScissorTest::Type ScissorTest::Default; + +void ScissorTest::Set(const Type& value) { + MBGL_CHECK_ERROR(value ? glEnable(GL_SCISSOR_TEST) : glDisable(GL_SCISSOR_TEST)); +} + +ScissorTest::Type ScissorTest::Get() { + Type scissorTest; + MBGL_CHECK_ERROR(scissorTest = glIsEnabled(GL_SCISSOR_TEST)); + return scissorTest; +} + const constexpr BindFramebuffer::Type BindFramebuffer::Default; void BindFramebuffer::Set(const Type& value) { diff --git a/src/mbgl/gl/value.hpp b/src/mbgl/gl/value.hpp index aa5cca6fec..b8656db5b0 100644 --- a/src/mbgl/gl/value.hpp +++ b/src/mbgl/gl/value.hpp @@ -182,6 +182,13 @@ struct Viewport { static Type Get(); }; +struct ScissorTest { + using Type = bool; + static const constexpr Type Default = false; + static void Set(const Type&); + static Type Get(); +}; + constexpr bool operator!=(const Viewport::Type& a, const Viewport::Type& b) { return a.x != b.x || a.y != b.y || a.size != b.size; } diff --git a/src/mbgl/map/backend.cpp b/src/mbgl/map/backend.cpp index 4c84453c1f..83c2fed00b 100644 --- a/src/mbgl/map/backend.cpp +++ b/src/mbgl/map/backend.cpp @@ -38,6 +38,11 @@ void Backend::assumeViewport(int32_t x, int32_t y, const Size& size) { assert(gl::value::Viewport::Get() == getContext().viewport.getCurrentValue()); } +void Backend::assumeScissorTest(bool enabled) { + getContext().scissorTest.setCurrentValue(enabled); + assert(gl::value::ScissorTest::Get() == getContext().scissorTest.getCurrentValue()); +} + bool Backend::implicitFramebufferBound() { return getContext().bindFramebuffer.getCurrentValue() == ImplicitFramebufferBinding; } @@ -54,6 +59,11 @@ void Backend::setViewport(int32_t x, int32_t y, const Size& size) { assert(gl::value::Viewport::Get() == getContext().viewport.getCurrentValue()); } +void Backend::setScissorTest(bool enabled) { + getContext().scissorTest = enabled; + assert(gl::value::ScissorTest::Get() == getContext().scissorTest.getCurrentValue()); +} + Backend::~Backend() = default; } // namespace mbgl diff --git a/src/mbgl/util/offscreen_texture.cpp b/src/mbgl/util/offscreen_texture.cpp index fe24774b7c..77a1416e48 100644 --- a/src/mbgl/util/offscreen_texture.cpp +++ b/src/mbgl/util/offscreen_texture.cpp @@ -33,6 +33,7 @@ public: } context.activeTexture = 0; + context.scissorTest = false; context.viewport = { 0, 0, size }; } diff --git a/test/util/offscreen_texture.test.cpp b/test/util/offscreen_texture.test.cpp index feaabf2630..0149b1a8dd 100644 --- a/test/util/offscreen_texture.test.cpp +++ b/test/util/offscreen_texture.test.cpp @@ -14,6 +14,11 @@ TEST(OffscreenTexture, EmptyRed) { HeadlessBackend backend { test::sharedDisplay() }; BackendScope scope { backend }; OffscreenView view(backend.getContext(), { 512, 256 }); + + // Scissor test shouldn't leak after OffscreenView::bind(). + MBGL_CHECK_ERROR(glScissor(64, 64, 128, 128)); + backend.getContext().scissorTest.setCurrentValue(true); + view.bind(); MBGL_CHECK_ERROR(glClearColor(1.0f, 0.0f, 0.0f, 1.0f)); @@ -128,6 +133,11 @@ void main() { // Then, create a texture, bind it, and render yellow to that texture. This should not // affect the originally bound FBO. OffscreenTexture texture(context, { 128, 128 }); + + // Scissor test shouldn't leak after OffscreenTexture::bind(). + MBGL_CHECK_ERROR(glScissor(32, 32, 64, 64)); + context.scissorTest.setCurrentValue(true); + texture.bind(); context.clear(Color(), {}, {}); -- cgit v1.2.1