diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2019-03-20 15:27:13 +0100 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2019-04-05 11:49:17 +0200 |
commit | be3b5917c169a6d5e6fcdc3cca15ca78e44dfd1d (patch) | |
tree | af780c3b78e5d2df7d8bee1f55a6f471a8047801 /src | |
parent | 839f4687aacb98440f85660120173c9b9895f46a (diff) | |
download | qtlocation-mapboxgl-be3b5917c169a6d5e6fcdc3cca15ca78e44dfd1d.tar.gz |
[core] add generic base class for renderbuffers
Diffstat (limited to 'src')
-rw-r--r-- | src/core-files.json | 3 | ||||
-rw-r--r-- | src/mbgl/gfx/context.hpp | 12 | ||||
-rw-r--r-- | src/mbgl/gfx/renderbuffer.hpp | 52 | ||||
-rw-r--r-- | src/mbgl/gl/context.cpp | 49 | ||||
-rw-r--r-- | src/mbgl/gl/context.hpp | 23 | ||||
-rw-r--r-- | src/mbgl/gl/renderbuffer.hpp | 33 | ||||
-rw-r--r-- | src/mbgl/gl/renderbuffer_resource.hpp | 19 | ||||
-rw-r--r-- | src/mbgl/renderer/layers/render_fill_extrusion_layer.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/renderer/render_static_data.hpp | 4 | ||||
-rw-r--r-- | src/mbgl/renderer/renderer_impl.cpp | 6 | ||||
-rw-r--r-- | src/mbgl/util/offscreen_texture.cpp | 6 | ||||
-rw-r--r-- | src/mbgl/util/offscreen_texture.hpp | 5 |
12 files changed, 132 insertions, 82 deletions
diff --git a/src/core-files.json b/src/core-files.json index b257666e3e..7ba2bd51e2 100644 --- a/src/core-files.json +++ b/src/core-files.json @@ -519,6 +519,7 @@ "mbgl/gfx/index_buffer.hpp": "src/mbgl/gfx/index_buffer.hpp", "mbgl/gfx/index_vector.hpp": "src/mbgl/gfx/index_vector.hpp", "mbgl/gfx/program.hpp": "src/mbgl/gfx/program.hpp", + "mbgl/gfx/renderbuffer.hpp": "src/mbgl/gfx/renderbuffer.hpp", "mbgl/gfx/stencil_mode.hpp": "src/mbgl/gfx/stencil_mode.hpp", "mbgl/gfx/texture.hpp": "src/mbgl/gfx/texture.hpp", "mbgl/gfx/types.hpp": "src/mbgl/gfx/types.hpp", @@ -540,7 +541,7 @@ "mbgl/gl/object.hpp": "src/mbgl/gl/object.hpp", "mbgl/gl/program.hpp": "src/mbgl/gl/program.hpp", "mbgl/gl/program_binary_extension.hpp": "src/mbgl/gl/program_binary_extension.hpp", - "mbgl/gl/renderbuffer.hpp": "src/mbgl/gl/renderbuffer.hpp", + "mbgl/gl/renderbuffer_resource.hpp": "src/mbgl/gl/renderbuffer_resource.hpp", "mbgl/gl/state.hpp": "src/mbgl/gl/state.hpp", "mbgl/gl/texture.hpp": "src/mbgl/gl/texture.hpp", "mbgl/gl/texture_resource.hpp": "src/mbgl/gl/texture_resource.hpp", diff --git a/src/mbgl/gfx/context.hpp b/src/mbgl/gfx/context.hpp index e898006ff5..f9b7789696 100644 --- a/src/mbgl/gfx/context.hpp +++ b/src/mbgl/gfx/context.hpp @@ -5,6 +5,7 @@ #include <mbgl/gfx/index_vector.hpp> #include <mbgl/gfx/index_buffer.hpp> #include <mbgl/gfx/texture.hpp> +#include <mbgl/gfx/renderbuffer.hpp> #include <mbgl/gfx/draw_scope.hpp> #include <mbgl/gfx/program.hpp> #include <mbgl/gfx/types.hpp> @@ -121,6 +122,17 @@ protected: TexturePixelType, TextureChannelDataType) = 0; public: + template <RenderbufferPixelType pixelType> + Renderbuffer<pixelType> + createRenderbuffer(const Size size) { + return { size, createRenderbufferResource(pixelType, size) }; + } + +protected: + virtual std::unique_ptr<RenderbufferResource> + createRenderbufferResource(RenderbufferPixelType, Size) = 0; + +public: DrawScope createDrawScope() { return { createDrawScopeResource() }; } diff --git a/src/mbgl/gfx/renderbuffer.hpp b/src/mbgl/gfx/renderbuffer.hpp new file mode 100644 index 0000000000..209b7ed3f9 --- /dev/null +++ b/src/mbgl/gfx/renderbuffer.hpp @@ -0,0 +1,52 @@ +#pragma once + +#include <mbgl/gfx/types.hpp> +#include <mbgl/util/size.hpp> + +#include <memory> +#include <cassert> + +namespace mbgl { +namespace gfx { + +class RenderbufferResource { +protected: + RenderbufferResource() = default; + +public: + virtual ~RenderbufferResource() = default; +}; + +template <RenderbufferPixelType renderbufferType> +class Renderbuffer final { +public: + Renderbuffer(const Size size_, std::unique_ptr<RenderbufferResource> resource_) + : size(size_), resource(std::move(resource_)) { + } + + void setShouldClear(bool clear) { + dirty = clear; + } + + bool needsClearing() const { + return dirty; + } + + const Size& getSize() const { + return size; + } + + template <typename T = RenderbufferResource> + T& getResource() const { + assert(resource); + return static_cast<T&>(*resource); + } + +protected: + Size size; + bool dirty = false; + std::unique_ptr<RenderbufferResource> resource; +}; + +} // namespace gfx +} // namespace mbgl diff --git a/src/mbgl/gl/context.cpp b/src/mbgl/gl/context.cpp index a1feafbedd..fcaae59804 100644 --- a/src/mbgl/gl/context.cpp +++ b/src/mbgl/gl/context.cpp @@ -3,6 +3,7 @@ #include <mbgl/gl/vertex_buffer_resource.hpp> #include <mbgl/gl/index_buffer_resource.hpp> #include <mbgl/gl/texture_resource.hpp> +#include <mbgl/gl/renderbuffer_resource.hpp> #include <mbgl/gl/draw_scope_resource.hpp> #include <mbgl/gl/texture.hpp> #include <mbgl/gl/debugging.hpp> @@ -336,7 +337,8 @@ UniqueFramebuffer Context::createFramebuffer() { return UniqueFramebuffer{ std::move(id), { this } }; } -UniqueRenderbuffer Context::createRenderbuffer(const gfx::RenderbufferPixelType type, const Size size) { +std::unique_ptr<gfx::RenderbufferResource> +Context::createRenderbufferResource(const gfx::RenderbufferPixelType type, const Size size) { RenderbufferID id = 0; MBGL_CHECK_ERROR(glGenRenderbuffers(1, &id)); UniqueRenderbuffer renderbuffer{ std::move(id), { this } }; @@ -345,9 +347,10 @@ UniqueRenderbuffer Context::createRenderbuffer(const gfx::RenderbufferPixelType MBGL_CHECK_ERROR( glRenderbufferStorage(GL_RENDERBUFFER, Enum<gfx::RenderbufferPixelType>::to(type), size.width, size.height)); bindRenderbuffer = 0; - return renderbuffer; + return std::make_unique<gl::RenderbufferResource>(std::move(renderbuffer)); } + std::unique_ptr<uint8_t[]> Context::readFramebuffer(const Size size, const gfx::TexturePixelType format, const bool flip) { const size_t stride = size.width * (format == gfx::TexturePixelType::RGBA ? 4 : 1); auto data = std::make_unique<uint8_t[]>(stride * size.height); @@ -417,48 +420,52 @@ void checkFramebuffer() { } void bindDepthStencilRenderbuffer( - const Renderbuffer<gfx::RenderbufferPixelType::DepthStencil>& depthStencil) { + const gfx::Renderbuffer<gfx::RenderbufferPixelType::DepthStencil>& depthStencil) { + auto& depthStencilResource = depthStencil.getResource<gl::RenderbufferResource>(); #ifdef GL_DEPTH_STENCIL_ATTACHMENT MBGL_CHECK_ERROR(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, - GL_RENDERBUFFER, depthStencil.renderbuffer)); + GL_RENDERBUFFER, depthStencilResource.renderbuffer)); #else MBGL_CHECK_ERROR(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, - depthStencil.renderbuffer)); + depthStencilResource.renderbuffer)); MBGL_CHECK_ERROR(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, - GL_RENDERBUFFER, depthStencil.renderbuffer)); + GL_RENDERBUFFER, depthStencilResource.renderbuffer)); #endif } } // namespace Framebuffer -Context::createFramebuffer(const Renderbuffer<gfx::RenderbufferPixelType::RGBA>& color, - const Renderbuffer<gfx::RenderbufferPixelType::DepthStencil>& depthStencil) { - if (color.size != depthStencil.size) { +Context::createFramebuffer(const gfx::Renderbuffer<gfx::RenderbufferPixelType::RGBA>& color, + const gfx::Renderbuffer<gfx::RenderbufferPixelType::DepthStencil>& depthStencil) { + if (color.getSize() != depthStencil.getSize()) { throw std::runtime_error("Renderbuffer size mismatch"); } auto fbo = createFramebuffer(); bindFramebuffer = fbo; + + auto& colorResource = color.getResource<gl::RenderbufferResource>(); MBGL_CHECK_ERROR(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, - GL_RENDERBUFFER, color.renderbuffer)); + GL_RENDERBUFFER, colorResource.renderbuffer)); bindDepthStencilRenderbuffer(depthStencil); checkFramebuffer(); - return { color.size, std::move(fbo) }; + return { color.getSize(), std::move(fbo) }; } -Framebuffer Context::createFramebuffer(const Renderbuffer<gfx::RenderbufferPixelType::RGBA>& color) { +Framebuffer Context::createFramebuffer(const gfx::Renderbuffer<gfx::RenderbufferPixelType::RGBA>& color) { auto fbo = createFramebuffer(); bindFramebuffer = fbo; + auto& colorResource = color.getResource<gl::RenderbufferResource>(); MBGL_CHECK_ERROR(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, - GL_RENDERBUFFER, color.renderbuffer)); + GL_RENDERBUFFER, colorResource.renderbuffer)); checkFramebuffer(); - return { color.size, std::move(fbo) }; + return { color.getSize(), std::move(fbo) }; } Framebuffer Context::createFramebuffer(const gfx::Texture& color, - const Renderbuffer<gfx::RenderbufferPixelType::DepthStencil>& depthStencil) { - if (color.size != depthStencil.size) { + const gfx::Renderbuffer<gfx::RenderbufferPixelType::DepthStencil>& depthStencil) { + if (color.size != depthStencil.getSize()) { throw std::runtime_error("Renderbuffer size mismatch"); } auto fbo = createFramebuffer(); @@ -483,8 +490,8 @@ Framebuffer Context::createFramebuffer(const gfx::Texture& color) { Framebuffer Context::createFramebuffer(const gfx::Texture& color, - const Renderbuffer<gfx::RenderbufferPixelType::Depth>& depthTarget) { - if (color.size != depthTarget.size) { + const gfx::Renderbuffer<gfx::RenderbufferPixelType::Depth>& depth) { + if (color.size != depth.getSize()) { throw std::runtime_error("Renderbuffer size mismatch"); } auto fbo = createFramebuffer(); @@ -492,10 +499,12 @@ Context::createFramebuffer(const gfx::Texture& color, MBGL_CHECK_ERROR(glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, reinterpret_cast<const gl::TextureResource&>(*color.resource).texture, 0)); + + auto& depthResource = depth.getResource<gl::RenderbufferResource>(); MBGL_CHECK_ERROR(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, - depthTarget.renderbuffer)); + depthResource.renderbuffer)); checkFramebuffer(); - return { depthTarget.size, std::move(fbo) }; + return { depth.getSize(), std::move(fbo) }; } std::unique_ptr<gfx::TextureResource> diff --git a/src/mbgl/gl/context.hpp b/src/mbgl/gl/context.hpp index b16a9994bf..a676c72f06 100644 --- a/src/mbgl/gl/context.hpp +++ b/src/mbgl/gl/context.hpp @@ -5,7 +5,6 @@ #include <mbgl/gl/object.hpp> #include <mbgl/gl/state.hpp> #include <mbgl/gl/value.hpp> -#include <mbgl/gl/renderbuffer.hpp> #include <mbgl/gl/framebuffer.hpp> #include <mbgl/gl/vertex_array.hpp> #include <mbgl/gl/types.hpp> @@ -60,23 +59,14 @@ public: #endif optional<std::pair<BinaryProgramFormat, std::string>> getBinaryProgram(ProgramID) const; - template <gfx::RenderbufferPixelType type> - Renderbuffer<type> createRenderbuffer(const Size size) { - static_assert(type == gfx::RenderbufferPixelType::RGBA || - type == gfx::RenderbufferPixelType::DepthStencil || - type == gfx::RenderbufferPixelType::Depth, - "invalid renderbuffer type"); - return { size, createRenderbuffer(type, size) }; - } - - Framebuffer createFramebuffer(const Renderbuffer<gfx::RenderbufferPixelType::RGBA>&, - const Renderbuffer<gfx::RenderbufferPixelType::DepthStencil>&); - Framebuffer createFramebuffer(const Renderbuffer<gfx::RenderbufferPixelType::RGBA>&); + Framebuffer createFramebuffer(const gfx::Renderbuffer<gfx::RenderbufferPixelType::RGBA>&, + const gfx::Renderbuffer<gfx::RenderbufferPixelType::DepthStencil>&); + Framebuffer createFramebuffer(const gfx::Renderbuffer<gfx::RenderbufferPixelType::RGBA>&); Framebuffer createFramebuffer(const gfx::Texture&, - const Renderbuffer<gfx::RenderbufferPixelType::DepthStencil>&); + const gfx::Renderbuffer<gfx::RenderbufferPixelType::DepthStencil>&); Framebuffer createFramebuffer(const gfx::Texture&); Framebuffer createFramebuffer(const gfx::Texture&, - const Renderbuffer<gfx::RenderbufferPixelType::Depth>&); + const gfx::Renderbuffer<gfx::RenderbufferPixelType::Depth>&); template <typename Image, gfx::TexturePixelType format = Image::channels == 4 ? gfx::TexturePixelType::RGBA @@ -213,10 +203,11 @@ private: void updateTextureResource(const gfx::TextureResource&, Size, const void* data, gfx::TexturePixelType, gfx::TextureChannelDataType) override; void updateTextureResourceSub(const gfx::TextureResource&, const uint16_t xOffset, const uint16_t yOffset, Size, const void* data, gfx::TexturePixelType, gfx::TextureChannelDataType) override; + std::unique_ptr<gfx::RenderbufferResource> createRenderbufferResource(gfx::RenderbufferPixelType, Size size) override; + std::unique_ptr<gfx::DrawScopeResource> createDrawScopeResource() override; UniqueFramebuffer createFramebuffer(); - UniqueRenderbuffer createRenderbuffer(gfx::RenderbufferPixelType, Size size); std::unique_ptr<uint8_t[]> readFramebuffer(Size, gfx::TexturePixelType, bool flip); #if not MBGL_USE_GLES2 void drawPixels(Size size, const void* data, gfx::TexturePixelType); diff --git a/src/mbgl/gl/renderbuffer.hpp b/src/mbgl/gl/renderbuffer.hpp deleted file mode 100644 index a761d17241..0000000000 --- a/src/mbgl/gl/renderbuffer.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include <mbgl/gfx/types.hpp> -#include <mbgl/gl/object.hpp> -#include <mbgl/util/size.hpp> - -namespace mbgl { -namespace gl { - -template <gfx::RenderbufferPixelType renderbufferType> -class Renderbuffer { -public: - Renderbuffer(Size size_, UniqueRenderbuffer renderbuffer_, bool dirty_ = false) - : size(std::move(size_)), renderbuffer(std::move(renderbuffer_)), dirty(dirty_) { - } - - using type = std::integral_constant<gfx::RenderbufferPixelType, renderbufferType>; - Size size; - UniqueRenderbuffer renderbuffer; - - void shouldClear(bool clear) { - dirty = clear; - } - bool needsClearing() { - return dirty; - } - -private: - bool dirty; -}; - -} // namespace gl -} // namespace mbgl diff --git a/src/mbgl/gl/renderbuffer_resource.hpp b/src/mbgl/gl/renderbuffer_resource.hpp new file mode 100644 index 0000000000..52865b42f7 --- /dev/null +++ b/src/mbgl/gl/renderbuffer_resource.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include <mbgl/gfx/renderbuffer.hpp> +#include <mbgl/gl/object.hpp> + +namespace mbgl { +namespace gl { + +class RenderbufferResource final : public gfx::RenderbufferResource { +public: + explicit RenderbufferResource(UniqueRenderbuffer renderbuffer_) + : renderbuffer(std::move(renderbuffer_)) { + } + + UniqueRenderbuffer renderbuffer; +}; + +} // namespace gl +} // namespace mbgl diff --git a/src/mbgl/renderer/layers/render_fill_extrusion_layer.cpp b/src/mbgl/renderer/layers/render_fill_extrusion_layer.cpp index 13ef84f169..c261e9048f 100644 --- a/src/mbgl/renderer/layers/render_fill_extrusion_layer.cpp +++ b/src/mbgl/renderer/layers/render_fill_extrusion_layer.cpp @@ -70,7 +70,7 @@ void RenderFillExtrusionLayer::render(PaintParameters& parameters, RenderSource* optional<float> depthClearValue = {}; if (parameters.staticData.depthRenderbuffer->needsClearing()) depthClearValue = 1.0; // Flag the depth buffer as no longer needing to be cleared for the remainder of this pass. - parameters.staticData.depthRenderbuffer->shouldClear(false); + parameters.staticData.depthRenderbuffer->setShouldClear(false); glContext.setStencilMode(gfx::StencilMode::disabled()); glContext.clear(Color{ 0.0f, 0.0f, 0.0f, 0.0f }, depthClearValue, {}); diff --git a/src/mbgl/renderer/render_static_data.hpp b/src/mbgl/renderer/render_static_data.hpp index b57139b783..f6c40163ee 100644 --- a/src/mbgl/renderer/render_static_data.hpp +++ b/src/mbgl/renderer/render_static_data.hpp @@ -2,7 +2,7 @@ #include <mbgl/gfx/vertex_buffer.hpp> #include <mbgl/gfx/index_buffer.hpp> -#include <mbgl/gl/renderbuffer.hpp> +#include <mbgl/gfx/renderbuffer.hpp> #include <mbgl/programs/background_program.hpp> #include <mbgl/programs/extrusion_texture_program.hpp> #include <mbgl/programs/programs.hpp> @@ -32,7 +32,7 @@ public: SegmentVector<RasterAttributes> rasterSegments; SegmentVector<ExtrusionTextureAttributes> extrusionTextureSegments; - optional<gl::Renderbuffer<gfx::RenderbufferPixelType::Depth>> depthRenderbuffer; + optional<gfx::Renderbuffer<gfx::RenderbufferPixelType::Depth>> depthRenderbuffer; bool has3D = false; Size backendSize; diff --git a/src/mbgl/renderer/renderer_impl.cpp b/src/mbgl/renderer/renderer_impl.cpp index ea493b49eb..00e40ae485 100644 --- a/src/mbgl/renderer/renderer_impl.cpp +++ b/src/mbgl/renderer/renderer_impl.cpp @@ -398,11 +398,11 @@ void Renderer::Impl::render(const UpdateParameters& updateParameters) { parameters.pass = RenderPass::Pass3D; if (!parameters.staticData.depthRenderbuffer || - parameters.staticData.depthRenderbuffer->size != parameters.staticData.backendSize) { + parameters.staticData.depthRenderbuffer->getSize() != parameters.staticData.backendSize) { parameters.staticData.depthRenderbuffer = - glContext.createRenderbuffer<gfx::RenderbufferPixelType::Depth>(parameters.staticData.backendSize); + parameters.context.createRenderbuffer<gfx::RenderbufferPixelType::Depth>(parameters.staticData.backendSize); } - parameters.staticData.depthRenderbuffer->shouldClear(true); + parameters.staticData.depthRenderbuffer->setShouldClear(true); uint32_t i = static_cast<uint32_t>(renderItems.size()) - 1; for (auto it = renderItems.begin(); it != renderItems.end(); ++it, --i) { diff --git a/src/mbgl/util/offscreen_texture.cpp b/src/mbgl/util/offscreen_texture.cpp index be17aa06aa..d7d5c55564 100644 --- a/src/mbgl/util/offscreen_texture.cpp +++ b/src/mbgl/util/offscreen_texture.cpp @@ -17,7 +17,7 @@ public: } Impl(gl::Context& context_, const Size size_, - gl::Renderbuffer<gfx::RenderbufferPixelType::Depth>& depth_, + gfx::Renderbuffer<gfx::RenderbufferPixelType::Depth>& depth_, const gfx::TextureChannelDataType type_) : context(context_), size(std::move(size_)), depth(&depth_), type(type_) { assert(!size.isEmpty()); @@ -58,7 +58,7 @@ private: const Size size; optional<gl::Framebuffer> framebuffer; optional<gfx::Texture> texture; - gl::Renderbuffer<gfx::RenderbufferPixelType::Depth>* depth = nullptr; + gfx::Renderbuffer<gfx::RenderbufferPixelType::Depth>* depth = nullptr; const gfx::TextureChannelDataType type; }; @@ -72,7 +72,7 @@ OffscreenTexture::OffscreenTexture(gfx::Context& context, OffscreenTexture::OffscreenTexture(gfx::Context& context, const Size size, - gl::Renderbuffer<gfx::RenderbufferPixelType::Depth>& renderbuffer, + gfx::Renderbuffer<gfx::RenderbufferPixelType::Depth>& renderbuffer, const gfx::TextureChannelDataType type) // TODO: remove cast : impl(std::make_unique<Impl>(reinterpret_cast<gl::Context&>(context), std::move(size), renderbuffer, type)) { diff --git a/src/mbgl/util/offscreen_texture.hpp b/src/mbgl/util/offscreen_texture.hpp index 1f470bd1b0..db8cfac559 100644 --- a/src/mbgl/util/offscreen_texture.hpp +++ b/src/mbgl/util/offscreen_texture.hpp @@ -1,7 +1,6 @@ #pragma once -#include <mbgl/gl/types.hpp> -#include <mbgl/gl/renderbuffer.hpp> +#include <mbgl/gfx/renderbuffer.hpp> #include <mbgl/util/image.hpp> namespace mbgl { @@ -18,7 +17,7 @@ public: gfx::TextureChannelDataType type = gfx::TextureChannelDataType::UnsignedByte); OffscreenTexture(gfx::Context&, Size size, - gl::Renderbuffer<gfx::RenderbufferPixelType::Depth>&, + gfx::Renderbuffer<gfx::RenderbufferPixelType::Depth>&, gfx::TextureChannelDataType type = gfx::TextureChannelDataType::UnsignedByte); ~OffscreenTexture(); OffscreenTexture(OffscreenTexture&&); |