From da3b7749b05af842797e83d3b3815ae3d8ab79bf Mon Sep 17 00:00:00 2001 From: Mikhail Pozdnyakov Date: Wed, 3 Jul 2019 15:55:47 +0300 Subject: [core] RenderSource creates render items --- src/mbgl/renderer/layers/render_symbol_layer.cpp | 2 +- src/mbgl/renderer/paint_parameters.cpp | 4 +- src/mbgl/renderer/paint_parameters.hpp | 5 +- src/mbgl/renderer/render_layer.cpp | 4 +- src/mbgl/renderer/render_layer.hpp | 5 +- src/mbgl/renderer/render_orchestrator.cpp | 36 +++++------ src/mbgl/renderer/render_source.hpp | 11 ++-- src/mbgl/renderer/render_tile.cpp | 4 +- src/mbgl/renderer/render_tile.hpp | 4 +- src/mbgl/renderer/sources/render_image_source.cpp | 79 ++++++++++++----------- src/mbgl/renderer/sources/render_image_source.hpp | 28 +++++--- src/mbgl/renderer/sources/render_tile_source.cpp | 57 +++++++++++----- src/mbgl/renderer/sources/render_tile_source.hpp | 7 +- 13 files changed, 140 insertions(+), 106 deletions(-) (limited to 'src/mbgl/renderer') diff --git a/src/mbgl/renderer/layers/render_symbol_layer.cpp b/src/mbgl/renderer/layers/render_symbol_layer.cpp index e254466e0b..60b0c8e2d8 100644 --- a/src/mbgl/renderer/layers/render_symbol_layer.cpp +++ b/src/mbgl/renderer/layers/render_symbol_layer.cpp @@ -508,7 +508,7 @@ void RenderSymbolLayer::prepare(const LayerPrepareParameters& params) { std::sort(renderTiles.begin(), renderTiles.end(), comp); placementData.clear(); - for (RenderTile& renderTile : renderTiles) { + for (const RenderTile& renderTile : renderTiles) { auto* bucket = static_cast(renderTile.getBucket(*baseImpl)); if (bucket && bucket->bucketLeaderID == getID()) { // Only place this layer if it's the "group leader" for the bucket diff --git a/src/mbgl/renderer/paint_parameters.cpp b/src/mbgl/renderer/paint_parameters.cpp index 5034850264..0bb66972a8 100644 --- a/src/mbgl/renderer/paint_parameters.cpp +++ b/src/mbgl/renderer/paint_parameters.cpp @@ -90,7 +90,7 @@ void PaintParameters::clearStencil() { namespace { // Detects a difference in keys of renderTiles and tileClippingMaskIDs -bool tileIDsIdentical(const std::vector>& renderTiles, +bool tileIDsIdentical(const RenderTiles& renderTiles, const std::map& tileClippingMaskIDs) { assert(std::is_sorted(renderTiles.begin(), renderTiles.end(), [](const RenderTile& a, const RenderTile& b) { return a.id < b.id; })); @@ -103,7 +103,7 @@ bool tileIDsIdentical(const std::vector>& ren } // namespace -void PaintParameters::renderTileClippingMasks(const std::vector>& renderTiles) { +void PaintParameters::renderTileClippingMasks(const RenderTiles& renderTiles) { if (renderTiles.empty() || tileIDsIdentical(renderTiles, tileClippingMaskIDs)) { // The current stencil mask is for this source already; no need to draw another one. return; diff --git a/src/mbgl/renderer/paint_parameters.hpp b/src/mbgl/renderer/paint_parameters.hpp index d78125da54..a0d26df7c5 100644 --- a/src/mbgl/renderer/paint_parameters.hpp +++ b/src/mbgl/renderer/paint_parameters.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -23,8 +24,6 @@ class ImageManager; class LineAtlas; class PatternAtlas; class UnwrappedTileID; -class RenderSource; -class RenderTile; namespace gfx { class Context; @@ -89,7 +88,7 @@ public: // Stencil handling public: - void renderTileClippingMasks(const std::vector>&); + void renderTileClippingMasks(const RenderTiles&); gfx::StencilMode stencilModeForClipping(const UnwrappedTileID&) const; gfx::StencilMode stencilModeFor3D(); diff --git a/src/mbgl/renderer/render_layer.cpp b/src/mbgl/renderer/render_layer.cpp index 2f57d9ed9c..821b10050f 100644 --- a/src/mbgl/renderer/render_layer.cpp +++ b/src/mbgl/renderer/render_layer.cpp @@ -54,10 +54,10 @@ optional RenderLayer::getSolidBackground() const { return nullopt; } -RenderLayer::RenderTiles RenderLayer::filterRenderTiles(RenderTiles tiles) const { +RenderTiles RenderLayer::filterRenderTiles(RenderTiles tiles) const { RenderTiles filtered; - for (RenderTile& tile : tiles) { + for (const RenderTile& tile : tiles) { if (tile.holdForFade()) { continue; } diff --git a/src/mbgl/renderer/render_layer.hpp b/src/mbgl/renderer/render_layer.hpp index 7f73826c34..11c7587893 100644 --- a/src/mbgl/renderer/render_layer.hpp +++ b/src/mbgl/renderer/render_layer.hpp @@ -30,7 +30,7 @@ public: class LayerPlacementData { public: std::reference_wrapper bucket; - std::reference_wrapper tile; + std::reference_wrapper tile; std::shared_ptr featureIndex; }; @@ -43,6 +43,8 @@ public: const TransformState& state; }; +using RenderTiles = std::vector>; + class RenderLayer { protected: RenderLayer(Immutable); @@ -115,7 +117,6 @@ protected: void checkRenderability(const PaintParameters&, uint32_t activeBindingCount); protected: - using RenderTiles = std::vector>; // Stores current set of tiles to be rendered for this layer. RenderTiles renderTiles; diff --git a/src/mbgl/renderer/render_orchestrator.cpp b/src/mbgl/renderer/render_orchestrator.cpp index b980efcff8..fafbc8b635 100644 --- a/src/mbgl/renderer/render_orchestrator.cpp +++ b/src/mbgl/renderer/render_orchestrator.cpp @@ -55,25 +55,11 @@ private: uint32_t index; }; -class SourceRenderItem final : public RenderItem { -public: - explicit SourceRenderItem(RenderSource& source_) - : source(source_) {} - -private: - bool hasRenderPass(RenderPass) const override { return false; } - void upload(gfx::UploadPass& pass) const override { source.get().upload(pass); } - void render(PaintParameters& parameters) const override { source.get().finishRender(parameters); } - const std::string& getName() const override { return source.get().baseImpl->id; } - - std::reference_wrapper source; -}; - class RenderTreeImpl final : public RenderTree { public: RenderTreeImpl(std::unique_ptr parameters_, std::set layerRenderItems_, - std::vector sourceRenderItems_, + std::vector> sourceRenderItems_, LineAtlas& lineAtlas_, PatternAtlas& patternAtlas_) : RenderTree(std::move(parameters_)), @@ -87,13 +73,16 @@ public: return { layerRenderItems.begin(), layerRenderItems.end() }; } RenderItems getSourceRenderItems() const override { - return { sourceRenderItems.begin(), sourceRenderItems.end() }; + RenderItems result; + result.reserve(sourceRenderItems.size()); + for (const auto& item : sourceRenderItems) result.emplace_back(*item); + return result; } LineAtlas& getLineAtlas() const override { return lineAtlas; } PatternAtlas& getPatternAtlas() const override { return patternAtlas; } std::set layerRenderItems; - std::vector sourceRenderItems; + std::vector> sourceRenderItems; std::reference_wrapper lineAtlas; std::reference_wrapper patternAtlas; }; @@ -339,12 +328,10 @@ std::unique_ptr RenderOrchestrator::createRenderTree(const UpdatePar return nullptr; } - std::vector sourceRenderItems; - // Update all matrices and generate data that we should upload to the GPU. + // Prepare. Update all matrices and generate data that we should upload to the GPU. for (const auto& entry : renderSources) { if (entry.second->isEnabled()) { entry.second->prepare({renderTreeParameters->transformParams, updateParameters.debugOptions, *imageManager}); - sourceRenderItems.emplace_back(*entry.second); } } @@ -355,7 +342,7 @@ std::unique_ptr RenderOrchestrator::createRenderTree(const UpdatePar layersNeedPlacement.emplace_back(renderLayer); } } - + // Symbol placement. { if (!isMapModeContinuous) { // TODO: Think about right way for symbol index to handle still rendering @@ -406,6 +393,13 @@ std::unique_ptr RenderOrchestrator::createRenderTree(const UpdatePar imageManager->reduceMemoryUseIfCacheSizeExceedsLimit(); } + std::vector> sourceRenderItems; + for (const auto& entry : renderSources) { + if (entry.second->isEnabled()) { + sourceRenderItems.emplace_back(entry.second->createRenderItem()); + } + } + return std::make_unique( std::move(renderTreeParameters), std::move(layerRenderItems), diff --git a/src/mbgl/renderer/render_source.hpp b/src/mbgl/renderer/render_source.hpp index d9af901664..fc770f83cf 100644 --- a/src/mbgl/renderer/render_source.hpp +++ b/src/mbgl/renderer/render_source.hpp @@ -30,6 +30,7 @@ class CollisionIndex; class TransformParameters; class ImageManager; class ImageSourceRenderData; +class RenderItem; namespace gfx { class UploadPass; @@ -42,6 +43,8 @@ public: const ImageManager& imageManager; }; +using RenderTiles = std::vector>; + class RenderSource : protected TileObserver { public: static std::unique_ptr create(Immutable); @@ -55,14 +58,14 @@ public: bool needsRendering, bool needsRelayout, const TileParameters&) = 0; - - virtual void upload(gfx::UploadPass&) = 0; + // Note: calling of this method might reset the contained render data. + virtual std::unique_ptr createRenderItem() = 0; + // Creates the render data to be passed to the render item. virtual void prepare(const SourcePrepareParameters&) = 0; - virtual void finishRender(PaintParameters&) = 0; virtual void updateFadingTiles() = 0; virtual bool hasFadingTiles() const = 0; // Returns a list of RenderTiles, sorted by tile id. - virtual std::vector> getRenderTiles() = 0; + virtual RenderTiles getRenderTiles() = 0; virtual const ImageSourceRenderData* getImageRenderData() const { return nullptr; } virtual const Tile* getRenderedTile(const UnwrappedTileID&) const { return nullptr; } diff --git a/src/mbgl/renderer/render_tile.cpp b/src/mbgl/renderer/render_tile.cpp index 3c61e7d7a3..fbb2ae81de 100644 --- a/src/mbgl/renderer/render_tile.cpp +++ b/src/mbgl/renderer/render_tile.cpp @@ -91,7 +91,7 @@ const gfx::Texture& RenderTile::getIconAtlasTexture() const { return renderData->getIconAtlasTexture(); } -void RenderTile::upload(gfx::UploadPass& uploadPass) { +void RenderTile::upload(gfx::UploadPass& uploadPass) const { assert(renderData); renderData->upload(uploadPass); @@ -129,7 +129,7 @@ void RenderTile::prepare(const SourcePrepareParameters& parameters) { matrix::multiply(nearClippedMatrix, transform.nearClippedProjMatrix, nearClippedMatrix); } -void RenderTile::finishRender(PaintParameters& parameters) { +void RenderTile::finishRender(PaintParameters& parameters) const { if (!needsRendering || parameters.debugOptions == MapDebugOptions::NoDebug) return; diff --git a/src/mbgl/renderer/render_tile.hpp b/src/mbgl/renderer/render_tile.hpp index a532bf8dd8..34d027cae5 100644 --- a/src/mbgl/renderer/render_tile.hpp +++ b/src/mbgl/renderer/render_tile.hpp @@ -59,9 +59,9 @@ public: const gfx::Texture& getGlyphAtlasTexture() const; const gfx::Texture& getIconAtlasTexture() const; - void upload(gfx::UploadPass&); + void upload(gfx::UploadPass&) const; void prepare(const SourcePrepareParameters&); - void finishRender(PaintParameters&); + void finishRender(PaintParameters&) const; mat4 translateVtxMatrix(const mat4& tileMatrix, const std::array& translation, diff --git a/src/mbgl/renderer/sources/render_image_source.cpp b/src/mbgl/renderer/sources/render_image_source.cpp index 04edced011..7e793c6a4a 100644 --- a/src/mbgl/renderer/sources/render_image_source.cpp +++ b/src/mbgl/renderer/sources/render_image_source.cpp @@ -17,55 +17,25 @@ namespace mbgl { using namespace style; -RenderImageSource::RenderImageSource(Immutable impl_) - : RenderSource(impl_) { -} - -RenderImageSource::~RenderImageSource() = default; - -const style::ImageSource::Impl& RenderImageSource::impl() const { - return static_cast(*baseImpl); -} +ImageSourceRenderData::~ImageSourceRenderData() = default; -bool RenderImageSource::isLoaded() const { - return !!bucket; -} - -void RenderImageSource::upload(gfx::UploadPass& uploadPass) { - if (bucket->needsUpload()) { +void ImageSourceRenderData::upload(gfx::UploadPass& uploadPass) const { + if (bucket && bucket->needsUpload()) { bucket->upload(uploadPass); } } -void RenderImageSource::prepare(const SourcePrepareParameters& parameters) { - if (!isLoaded()) { - return; - } - - std::vector matrices{tileIds.size(), mat4()}; - const auto& transformParams = parameters.transform; - for (size_t i = 0u; i < tileIds.size(); ++i) { - mat4& matrix = matrices[i]; - matrix::identity(matrix); - transformParams.state.matrixFor(matrix, tileIds[i]); - matrix::multiply(matrix, transformParams.alignedProjMatrix, matrix); - } - - renderData = std::make_unique(bucket, std::move(matrices)); -} - -void RenderImageSource::finishRender(PaintParameters& parameters) { - if (!isLoaded() || !(parameters.debugOptions & MapDebugOptions::TileBorders)) { +void ImageSourceRenderData::render(PaintParameters& parameters) const { + if (!bucket || !(parameters.debugOptions & MapDebugOptions::TileBorders)) { return; } - assert(renderData); static const style::Properties<>::PossiblyEvaluated properties {}; static const DebugProgram::Binders paintAttributeData(properties, 0); auto& programInstance = parameters.programs.debug; - for (auto matrix : renderData->matrices) { + for (auto matrix : matrices) { programInstance.draw( parameters.context, *parameters.renderPass, @@ -96,6 +66,43 @@ void RenderImageSource::finishRender(PaintParameters& parameters) { } } +RenderImageSource::RenderImageSource(Immutable impl_) + : RenderSource(std::move(impl_)) { +} + +RenderImageSource::~RenderImageSource() = default; + +const style::ImageSource::Impl& RenderImageSource::impl() const { + return static_cast(*baseImpl); +} + +bool RenderImageSource::isLoaded() const { + return !!bucket; +} + +std::unique_ptr RenderImageSource::createRenderItem() { + assert(renderData); + return std::move(renderData); +} + +void RenderImageSource::prepare(const SourcePrepareParameters& parameters) { + assert(!renderData); + if (!isLoaded()) { + renderData = std::make_unique(bucket, std::vector{}, baseImpl->id); + return; + } + + std::vector matrices{tileIds.size(), mat4()}; + const auto& transformParams = parameters.transform; + for (size_t i = 0u; i < tileIds.size(); ++i) { + mat4& matrix = matrices[i]; + matrix::identity(matrix); + transformParams.state.matrixFor(matrix, tileIds[i]); + matrix::multiply(matrix, transformParams.alignedProjMatrix, matrix); + } + renderData = std::make_unique(bucket, std::move(matrices), baseImpl->id); +} + std::unordered_map> RenderImageSource::queryRenderedFeatures(const ScreenLineString&, const TransformState&, diff --git a/src/mbgl/renderer/sources/render_image_source.hpp b/src/mbgl/renderer/sources/render_image_source.hpp index 804daeef87..d21f4fae10 100644 --- a/src/mbgl/renderer/sources/render_image_source.hpp +++ b/src/mbgl/renderer/sources/render_image_source.hpp @@ -9,14 +9,25 @@ namespace mbgl { class RasterBucket; -class ImageSourceRenderData { +class ImageSourceRenderData final : public RenderItem { public: ImageSourceRenderData(std::shared_ptr bucket_, - std::vector matrices_) + std::vector matrices_, + std::string name_) : bucket(std::move(bucket_)), - matrices(std::move(matrices_)) {} - std::shared_ptr bucket; - std::vector matrices; + matrices(std::move(matrices_)), + name(std::move(name_)) {} + ~ImageSourceRenderData() override; + const std::shared_ptr bucket; + const std::vector matrices; + +private: + void upload(gfx::UploadPass&) const override; + void render(PaintParameters&) const override; + bool hasRenderPass(RenderPass) const override { return false; } + const std::string& getName() const override { return name; } + + std::string name; }; class RenderImageSource final : public RenderSource { @@ -26,9 +37,8 @@ public: bool isLoaded() const final; - void upload(gfx::UploadPass&) final; + std::unique_ptr createRenderItem() override; void prepare(const SourcePrepareParameters&) final; - void finishRender(PaintParameters&) final; void updateFadingTiles() final {} bool hasFadingTiles() const final { return false; } @@ -38,9 +48,7 @@ public: bool needsRelayout, const TileParameters&) final; - std::vector> getRenderTiles() final { - return {}; - } + RenderTiles getRenderTiles() override { return {}; } const ImageSourceRenderData* getImageRenderData() const override { return renderData.get(); diff --git a/src/mbgl/renderer/sources/render_tile_source.cpp b/src/mbgl/renderer/sources/render_tile_source.cpp index 3af3b92e77..99bdfde485 100644 --- a/src/mbgl/renderer/sources/render_tile_source.cpp +++ b/src/mbgl/renderer/sources/render_tile_source.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -10,8 +11,37 @@ namespace mbgl { using namespace style; +class TileSourceRenderItem : public RenderItem { +public: + TileSourceRenderItem(Immutable> renderTiles_, std::string name_) + : renderTiles(std::move(renderTiles_)) + , name(std::move(name_)) {} + +private: + void upload(gfx::UploadPass&) const override; + void render(PaintParameters&) const override; + bool hasRenderPass(RenderPass) const override { return false; } + const std::string& getName() const override { return name; } + + Immutable> renderTiles; + std::string name; +}; + +void TileSourceRenderItem::upload(gfx::UploadPass& parameters) const { + for (auto& tile : *renderTiles) { + tile.upload(parameters); + } +} + +void TileSourceRenderItem::render(PaintParameters& parameters) const { + for (auto& tile : *renderTiles) { + tile.finishRender(parameters); + } +} + RenderTileSource::RenderTileSource(Immutable impl_) - : RenderSource(std::move(impl_)) { + : RenderSource(std::move(impl_)) + , renderTiles(makeMutable>()) { tilePyramid.setObserver(this); } @@ -21,25 +51,18 @@ bool RenderTileSource::isLoaded() const { return tilePyramid.isLoaded(); } -void RenderTileSource::upload(gfx::UploadPass& parameters) { - for (auto& tile : renderTiles) { - tile.upload(parameters); - } +std::unique_ptr RenderTileSource::createRenderItem() { + return std::make_unique(renderTiles, baseImpl->id); } void RenderTileSource::prepare(const SourcePrepareParameters& parameters) { - renderTiles.clear(); - renderTiles.reserve(tilePyramid.getRenderedTiles().size()); + auto tiles = makeMutable>(); + tiles->reserve(tilePyramid.getRenderedTiles().size()); for (auto& entry : tilePyramid.getRenderedTiles()) { - renderTiles.emplace_back(entry.first, entry.second); - renderTiles.back().prepare(parameters); - } -} - -void RenderTileSource::finishRender(PaintParameters& parameters) { - for (auto& tile : renderTiles) { - tile.finishRender(parameters); + tiles->emplace_back(entry.first, entry.second); + tiles->back().prepare(parameters); } + renderTiles = std::move(tiles); } void RenderTileSource::updateFadingTiles() { @@ -50,8 +73,8 @@ bool RenderTileSource::hasFadingTiles() const { return tilePyramid.hasFadingTiles(); } -std::vector> RenderTileSource::getRenderTiles() { - return { renderTiles.begin(), renderTiles.end() }; +RenderTiles RenderTileSource::getRenderTiles() { + return { renderTiles->begin(), renderTiles->end() }; } const Tile* RenderTileSource::getRenderedTile(const UnwrappedTileID& tileID) const { diff --git a/src/mbgl/renderer/sources/render_tile_source.hpp b/src/mbgl/renderer/sources/render_tile_source.hpp index 91b453968d..e3148b45b9 100644 --- a/src/mbgl/renderer/sources/render_tile_source.hpp +++ b/src/mbgl/renderer/sources/render_tile_source.hpp @@ -16,13 +16,12 @@ public: bool isLoaded() const override; - void upload(gfx::UploadPass&) override; + std::unique_ptr createRenderItem() override; void prepare(const SourcePrepareParameters&) override; - void finishRender(PaintParameters&) override; void updateFadingTiles() override; bool hasFadingTiles() const override; - std::vector> getRenderTiles() override; + RenderTiles getRenderTiles() override; const Tile* getRenderedTile(const UnwrappedTileID&) const override; std::unordered_map> @@ -40,7 +39,7 @@ public: protected: TilePyramid tilePyramid; - std::vector renderTiles; + Immutable> renderTiles; }; } // namespace mbgl -- cgit v1.2.1