From 7081d5e154e208322b95f8efab74ec6e014a263d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Tue, 2 Apr 2019 22:35:46 +0200 Subject: [core] move depth/stencil buffer debug viz to gfx::Context --- include/mbgl/map/mode.hpp | 3 -- src/mbgl/gfx/context.hpp | 6 ++++ src/mbgl/gl/context.cpp | 57 +++++++++++++++++++++++++++++++++++++ src/mbgl/gl/context.hpp | 6 ++++ src/mbgl/renderer/renderer_impl.cpp | 51 +++------------------------------ 5 files changed, 73 insertions(+), 50 deletions(-) diff --git a/include/mbgl/map/mode.hpp b/include/mbgl/map/mode.hpp index 4ee289d855..11dd8583a4 100644 --- a/include/mbgl/map/mode.hpp +++ b/include/mbgl/map/mode.hpp @@ -37,11 +37,8 @@ enum class MapDebugOptions : EnumType { Timestamps = 1 << 3, Collision = 1 << 4, Overdraw = 1 << 5, -// FIXME: https://github.com/mapbox/mapbox-gl-native/issues/5117 -#if not MBGL_USE_GLES2 StencilClip = 1 << 6, DepthBuffer = 1 << 7, -#endif // MBGL_USE_GLES2 }; MBGL_CONSTEXPR MapDebugOptions operator|(MapDebugOptions lhs, MapDebugOptions rhs) { diff --git a/src/mbgl/gfx/context.hpp b/src/mbgl/gfx/context.hpp index c956debcb0..140d3b2356 100644 --- a/src/mbgl/gfx/context.hpp +++ b/src/mbgl/gfx/context.hpp @@ -162,6 +162,12 @@ private: public: virtual std::unique_ptr createCommandEncoder() = 0; + +#if not defined(NDEBUG) +public: + virtual void visualizeStencilBuffer() = 0; + virtual void visualizeDepthBuffer(float depthRangeSize) = 0; +#endif }; } // namespace gfx diff --git a/src/mbgl/gl/context.cpp b/src/mbgl/gl/context.cpp index 29675b0446..e91c922a50 100644 --- a/src/mbgl/gl/context.cpp +++ b/src/mbgl/gl/context.cpp @@ -703,6 +703,9 @@ void Context::setColorMode(const gfx::ColorMode& color) { std::unique_ptr Context::createCommandEncoder() { backend.updateAssumedState(); + if (backend.contextIsShared()) { + setDirtyState(); + } return std::make_unique(*this); } @@ -811,5 +814,59 @@ void Context::performCleanup() { } } +#if not defined(NDEBUG) +void Context::visualizeStencilBuffer() { +#if not MBGL_USE_GLES2 + setStencilMode(gfx::StencilMode::disabled()); + setDepthMode(gfx::DepthMode::disabled()); + setColorMode(gfx::ColorMode::unblended()); + program = 0; + + // Reset the value in case someone else changed it, or it's dirty. + pixelTransferStencil = gl::value::PixelTransferStencil::Default; + + // Read the stencil buffer + const auto viewportValue = viewport.getCurrentValue(); + auto image = readFramebuffer(viewportValue.size, false); + + // Scale the Stencil buffer to cover the entire color space. + auto it = image.data.get(); + auto end = it + viewportValue.size.width * viewportValue.size.height; + const auto factor = 255.0f / *std::max_element(it, end); + for (; it != end; ++it) { + *it *= factor; + } + + pixelZoom = { 1, 1 }; + rasterPos = { -1, -1, 0, 1 }; + drawPixels(image); +#endif +} + +void Context::visualizeDepthBuffer(const float depthRangeSize) { + (void)depthRangeSize; +#if not MBGL_USE_GLES2 + setStencilMode(gfx::StencilMode::disabled()); + setDepthMode(gfx::DepthMode::disabled()); + setColorMode(gfx::ColorMode::unblended()); + program = 0; + + // Scales the values in the depth buffer so that they cover the entire grayscale range. This + // makes it easier to spot tiny differences. + const float base = 1.0f / (1.0f - depthRangeSize); + pixelTransferDepth = { base, 1.0f - base }; + + // Read the stencil buffer + auto viewportValue = viewport.getCurrentValue(); + auto image = readFramebuffer(viewportValue.size, false); + + pixelZoom = { 1, 1 }; + rasterPos = { -1, -1, 0, 1 }; + drawPixels(image); +#endif +} + +#endif + } // namespace gl } // namespace mbgl diff --git a/src/mbgl/gl/context.hpp b/src/mbgl/gl/context.hpp index 062cbd7b8a..ae935e32cb 100644 --- a/src/mbgl/gl/context.hpp +++ b/src/mbgl/gl/context.hpp @@ -241,6 +241,12 @@ private: public: // For testing bool disableVAOExtension = false; + +#if not defined(NDEBUG) +public: + void visualizeStencilBuffer() override; + void visualizeDepthBuffer(float depthRangeSize) override; +#endif }; } // namespace gl diff --git a/src/mbgl/renderer/renderer_impl.cpp b/src/mbgl/renderer/renderer_impl.cpp index 345bd2f259..6d4c7b6320 100644 --- a/src/mbgl/renderer/renderer_impl.cpp +++ b/src/mbgl/renderer/renderer_impl.cpp @@ -369,10 +369,6 @@ void Renderer::Impl::render(const UpdateParameters& updateParameters) { // TODO: remove cast gl::Context& glContext = static_cast(parameters.context); - if (backend.contextIsShared()) { - glContext.setDirtyState(); - } - // - UPLOAD PASS ------------------------------------------------------------------------------- // Uploads all required buffers and images before we do any actual rendering. { @@ -477,33 +473,10 @@ void Renderer::Impl::render(const UpdateParameters& updateParameters) { } } -#if not MBGL_USE_GLES2 and not defined(NDEBUG) +#if not defined(NDEBUG) // Render tile clip boundaries, using stencil buffer to calculate fill color. if (parameters.debugOptions & MapDebugOptions::StencilClip) { - glContext.setStencilMode(gfx::StencilMode::disabled()); - glContext.setDepthMode(gfx::DepthMode::disabled()); - glContext.setColorMode(gfx::ColorMode::unblended()); - glContext.program = 0; - - // Reset the value in case someone else changed it, or it's dirty. - glContext.pixelTransferStencil = gl::value::PixelTransferStencil::Default; - - // Read the stencil buffer - const auto viewport = glContext.viewport.getCurrentValue(); - auto image = glContext.readFramebuffer(viewport.size, false); - - // Scale the Stencil buffer to cover the entire color space. - auto it = image.data.get(); - auto end = it + viewport.size.width * viewport.size.height; - const auto factor = 255.0f / *std::max_element(it, end); - for (; it != end; ++it) { - *it *= factor; - } - - glContext.pixelZoom = { 1, 1 }; - glContext.rasterPos = { -1, -1, 0, 1 }; - glContext.drawPixels(image); - + parameters.context.visualizeStencilBuffer(); return; } #endif @@ -560,26 +533,10 @@ void Renderer::Impl::render(const UpdateParameters& updateParameters) { } } -#if not MBGL_USE_GLES2 and not defined(NDEBUG) +#if not defined(NDEBUG) // Render the depth buffer. if (parameters.debugOptions & MapDebugOptions::DepthBuffer) { - glContext.setStencilMode(gfx::StencilMode::disabled()); - glContext.setDepthMode(gfx::DepthMode::disabled()); - glContext.setColorMode(gfx::ColorMode::unblended()); - glContext.program = 0; - - // Scales the values in the depth buffer so that they cover the entire grayscale range. This - // makes it easier to spot tiny differences. - const float base = 1.0f / (1.0f - parameters.depthRangeSize); - glContext.pixelTransferDepth = { base, 1.0f - base }; - - // Read the stencil buffer - auto viewport = glContext.viewport.getCurrentValue(); - auto image = glContext.readFramebuffer(viewport.size, false); - - glContext.pixelZoom = { 1, 1 }; - glContext.rasterPos = { -1, -1, 0, 1 }; - glContext.drawPixels(image); + parameters.context.visualizeDepthBuffer(parameters.depthRangeSize); } #endif -- cgit v1.2.1