Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Share render tiles lists between layers
Browse files Browse the repository at this point in the history
This helps to avoid unneeded allocations.
  • Loading branch information
pozdnyakov committed Jul 9, 2019
1 parent 81fc495 commit 929824e
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 58 deletions.
3 changes: 2 additions & 1 deletion src/mbgl/renderer/layers/render_circle_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ bool RenderCircleLayer::hasCrossfade() const {
}

void RenderCircleLayer::render(PaintParameters& parameters) {
assert(renderTiles);
if (parameters.pass == RenderPass::Opaque) {
return;
}

for (const RenderTile& tile : renderTiles) {
for (const RenderTile& tile : *renderTiles) {
const LayerRenderData* renderData = tile.getLayerRenderData(*baseImpl);
if (!renderData) {
continue;
Expand Down
5 changes: 3 additions & 2 deletions src/mbgl/renderer/layers/render_fill_extrusion_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ bool RenderFillExtrusionLayer::hasCrossfade() const {
}

void RenderFillExtrusionLayer::render(PaintParameters& parameters) {
assert(renderTiles);
if (parameters.pass != RenderPass::Translucent) {
return;
}
Expand Down Expand Up @@ -115,7 +116,7 @@ void RenderFillExtrusionLayer::render(PaintParameters& parameters) {
if (unevaluated.get<FillExtrusionPattern>().isUndefined()) {
// Draw solid color extrusions
auto drawTiles = [&](const gfx::StencilMode& stencilMode_, const gfx::ColorMode& colorMode_, const std::string& name) {
for (const RenderTile& tile : renderTiles) {
for (const RenderTile& tile : *renderTiles) {
const LayerRenderData* renderData = tile.getLayerRenderData(*baseImpl);
if (!renderData) {
continue;
Expand Down Expand Up @@ -162,7 +163,7 @@ void RenderFillExtrusionLayer::render(PaintParameters& parameters) {
// Draw textured extrusions
const auto fillPatternValue = evaluated.get<FillExtrusionPattern>().constantOr(mbgl::Faded<std::basic_string<char> >{"", ""});
auto drawTiles = [&](const gfx::StencilMode& stencilMode_, const gfx::ColorMode& colorMode_, const std::string& name) {
for (const RenderTile& tile : renderTiles) {
for (const RenderTile& tile : *renderTiles) {
const LayerRenderData* renderData = tile.getLayerRenderData(*baseImpl);
if (!renderData) {
continue;
Expand Down
5 changes: 3 additions & 2 deletions src/mbgl/renderer/layers/render_fill_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ bool RenderFillLayer::hasCrossfade() const {
}

void RenderFillLayer::render(PaintParameters& parameters) {
assert(renderTiles);
if (unevaluated.get<FillPattern>().isUndefined()) {
parameters.renderTileClippingMasks(renderTiles);
for (const RenderTile& tile : renderTiles) {
for (const RenderTile& tile : *renderTiles) {
const LayerRenderData* renderData = tile.getLayerRenderData(*baseImpl);
if (!renderData) {
continue;
Expand Down Expand Up @@ -162,7 +163,7 @@ void RenderFillLayer::render(PaintParameters& parameters) {

parameters.renderTileClippingMasks(renderTiles);

for (const RenderTile& tile : renderTiles) {
for (const RenderTile& tile : *renderTiles) {
const LayerRenderData* renderData = tile.getLayerRenderData(*baseImpl);
if (!renderData) {
continue;
Expand Down
3 changes: 2 additions & 1 deletion src/mbgl/renderer/layers/render_heatmap_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ void RenderHeatmapLayer::upload(gfx::UploadPass& uploadPass) {
}

void RenderHeatmapLayer::render(PaintParameters& parameters) {
assert(renderTiles);
if (parameters.pass == RenderPass::Opaque) {
return;
}
Expand Down Expand Up @@ -93,7 +94,7 @@ void RenderHeatmapLayer::render(PaintParameters& parameters) {
auto renderPass = parameters.encoder->createRenderPass(
"heatmap texture", { *renderTexture, Color{ 0.0f, 0.0f, 0.0f, 1.0f }, {}, {} });

for (const RenderTile& tile : renderTiles) {
for (const RenderTile& tile : *renderTiles) {
const LayerRenderData* renderData = tile.getLayerRenderData(*baseImpl);
if (!renderData) {
continue;
Expand Down
3 changes: 2 additions & 1 deletion src/mbgl/renderer/layers/render_hillshade_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ void RenderHillshadeLayer::prepare(const LayerPrepareParameters& params) {
}

void RenderHillshadeLayer::render(PaintParameters& parameters) {
assert(renderTiles);
if (parameters.pass != RenderPass::Translucent && parameters.pass != RenderPass::Pass3D)
return;
const auto& evaluated = static_cast<const HillshadeLayerProperties&>(*evaluatedProperties).evaluated;
Expand Down Expand Up @@ -126,7 +127,7 @@ void RenderHillshadeLayer::render(PaintParameters& parameters) {
matrix::ortho(mat, 0, util::EXTENT, -util::EXTENT, 0, 0, 1);
matrix::translate(mat, mat, 0, -util::EXTENT, 0);

for (const RenderTile& tile : renderTiles) {
for (const RenderTile& tile : *renderTiles) {
auto* bucket_ = tile.getBucket(*baseImpl);
if (!bucket_) {
continue;
Expand Down
5 changes: 3 additions & 2 deletions src/mbgl/renderer/layers/render_line_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ bool RenderLineLayer::hasCrossfade() const {

void RenderLineLayer::prepare(const LayerPrepareParameters& params) {
RenderLayer::prepare(params);
for (const RenderTile& tile : renderTiles) {
for (const RenderTile& tile : *renderTiles) {
const LayerRenderData* renderData = tile.getLayerRenderData(*baseImpl);
if (!renderData) continue;

Expand All @@ -84,13 +84,14 @@ void RenderLineLayer::upload(gfx::UploadPass& uploadPass) {
}

void RenderLineLayer::render(PaintParameters& parameters) {
assert(renderTiles);
if (parameters.pass == RenderPass::Opaque) {
return;
}

parameters.renderTileClippingMasks(renderTiles);

for (const RenderTile& tile : renderTiles) {
for (const RenderTile& tile : *renderTiles) {
const LayerRenderData* renderData = tile.getLayerRenderData(*baseImpl);
if (!renderData) {
continue;
Expand Down
7 changes: 4 additions & 3 deletions src/mbgl/renderer/layers/render_raster_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ static std::array<float, 3> spinWeights(float spin) {
}

void RenderRasterLayer::prepare(const LayerPrepareParameters& params) {
RenderLayer::prepare(params);
renderTiles = params.source->getRenderTiles();
imageData = params.source->getImageRenderData();
assert(renderTiles || imageData);
}

void RenderRasterLayer::render(PaintParameters& parameters) {
Expand Down Expand Up @@ -153,8 +154,8 @@ void RenderRasterLayer::render(PaintParameters& parameters) {
},
bucket.drawScopeID + std::to_string(i++));
}
} else {
for (const RenderTile& tile : renderTiles) {
} else if (renderTiles) {
for (const RenderTile& tile : *renderTiles) {
auto* bucket_ = tile.getBucket(*baseImpl);
if (!bucket_) {
continue;
Expand Down
20 changes: 5 additions & 15 deletions src/mbgl/renderer/layers/render_symbol_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ bool RenderSymbolLayer::hasCrossfade() const {
}

void RenderSymbolLayer::render(PaintParameters& parameters) {
assert(renderTiles);
if (parameters.pass == RenderPass::Opaque) {
return;
}
Expand Down Expand Up @@ -348,7 +349,7 @@ void RenderSymbolLayer::render(PaintParameters& parameters) {
);
};

for (const RenderTile& tile : renderTiles) {
for (const RenderTile& tile : *renderTiles) {
const LayerRenderData* renderData = tile.getLayerRenderData(*baseImpl);
if (!renderData) {
continue;
Expand Down Expand Up @@ -493,22 +494,11 @@ style::TextPaintProperties::PossiblyEvaluated RenderSymbolLayer::textPaintProper
}

void RenderSymbolLayer::prepare(const LayerPrepareParameters& params) {
renderTiles = params.source->getRenderTiles();
const auto comp = [bearing = params.state.getBearing()](const RenderTile& a, const RenderTile& b) {
Point<float> pa(a.id.canonical.x, a.id.canonical.y);
Point<float> pb(b.id.canonical.x, b.id.canonical.y);

auto par = util::rotate(pa, bearing);
auto pbr = util::rotate(pb, bearing);

return std::tie(b.id.canonical.z, par.y, par.x) < std::tie(a.id.canonical.z, pbr.y, pbr.x);
};
// Sort symbol tiles in opposite y position, so tiles with overlapping symbols are drawn
// on top of each other, with lower symbols being drawn on top of higher symbols.
std::sort(renderTiles.begin(), renderTiles.end(), comp);
renderTiles = params.source->getRenderTilesSortedByYPosition();
assert(renderTiles);

placementData.clear();
for (const RenderTile& renderTile : renderTiles) {
for (const RenderTile& renderTile : *renderTiles) {
auto* bucket = static_cast<SymbolBucket*>(renderTile.getBucket(*baseImpl));
if (bucket && bucket->bucketLeaderID == getID()) {
// Only place this layer if it's the "group leader" for the bucket
Expand Down
13 changes: 7 additions & 6 deletions src/mbgl/renderer/paint_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,25 @@ namespace {
// Detects a difference in keys of renderTiles and tileClippingMaskIDs
bool tileIDsIdentical(const RenderTiles& renderTiles,
const std::map<UnwrappedTileID, int32_t>& tileClippingMaskIDs) {
assert(std::is_sorted(renderTiles.begin(), renderTiles.end(),
assert(renderTiles);
assert(std::is_sorted(renderTiles->begin(), renderTiles->end(),
[](const RenderTile& a, const RenderTile& b) { return a.id < b.id; }));
if (renderTiles.size() != tileClippingMaskIDs.size()) {
if (renderTiles->size() != tileClippingMaskIDs.size()) {
return false;
}
return std::equal(renderTiles.begin(), renderTiles.end(), tileClippingMaskIDs.begin(),
return std::equal(renderTiles->begin(), renderTiles->end(), tileClippingMaskIDs.begin(),
[](const RenderTile& a, const auto& b) { return a.id == b.first; });
}

} // namespace

void PaintParameters::renderTileClippingMasks(const RenderTiles& renderTiles) {
if (renderTiles.empty() || tileIDsIdentical(renderTiles, tileClippingMaskIDs)) {
if (!renderTiles || renderTiles->empty() || tileIDsIdentical(renderTiles, tileClippingMaskIDs)) {
// The current stencil mask is for this source already; no need to draw another one.
return;
}

if (nextStencilID + renderTiles.size() > 256) {
if (nextStencilID + renderTiles->size() > 256) {
// we'll run out of fresh IDs so we need to clear and start from scratch
clearStencil();
}
Expand All @@ -120,7 +121,7 @@ void PaintParameters::renderTileClippingMasks(const RenderTiles& renderTiles) {
const style::Properties<>::PossiblyEvaluated properties {};
const ClippingMaskProgram::Binders paintAttributeData(properties, 0);

for (const RenderTile& renderTile : renderTiles) {
for (const RenderTile& renderTile : *renderTiles) {
const int32_t stencilID = nextStencilID++;
tileClippingMaskIDs.emplace(renderTile.id, stencilID);

Expand Down
15 changes: 2 additions & 13 deletions src/mbgl/renderer/render_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,14 @@ bool RenderLayer::supportsZoom(float zoom) const {

void RenderLayer::prepare(const LayerPrepareParameters& params) {
assert(params.source);
renderTiles = filterRenderTiles(params.source->getRenderTiles());
renderTiles = params.source->getRenderTiles();
assert(renderTiles);
}

optional<Color> RenderLayer::getSolidBackground() const {
return nullopt;
}

RenderTiles RenderLayer::filterRenderTiles(RenderTiles tiles) const {
RenderTiles filtered;

for (const RenderTile& tile : tiles) {
if (tile.holdForFade()) {
continue;
}
filtered.emplace_back(tile);
}
return filtered;
}

void RenderLayer::markContextDestroyed() {
// no-op
}
Expand Down
5 changes: 1 addition & 4 deletions src/mbgl/renderer/render_layer.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <mbgl/layout/layout.hpp>
#include <mbgl/renderer/render_pass.hpp>
#include <mbgl/renderer/render_source.hpp>
#include <mbgl/style/layer_properties.hpp>
#include <mbgl/tile/geometry_tile_data.hpp>
#include <mbgl/util/mat4.hpp>
Expand All @@ -15,7 +16,6 @@ class TransitionParameters;
class PropertyEvaluationParameters;
class UploadParameters;
class PaintParameters;
class RenderSource;
class RenderTile;
class TransformState;
class PatternAtlas;
Expand Down Expand Up @@ -43,8 +43,6 @@ class LayerPrepareParameters {
const TransformState& state;
};

using RenderTiles = std::vector<std::reference_wrapper<const RenderTile>>;

class RenderLayer {
protected:
RenderLayer(Immutable<style::LayerProperties>);
Expand Down Expand Up @@ -127,7 +125,6 @@ class RenderLayer {
std::vector<LayerPlacementData> placementData;

private:
RenderTiles filterRenderTiles(RenderTiles) const;
// Some layers may not render correctly on some hardware when the vertex attribute limit of
// that GPU is exceeded. More attributes are used when adding many data driven paint properties
// to a layer.
Expand Down
12 changes: 9 additions & 3 deletions src/mbgl/renderer/render_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SourcePrepareParameters {
const ImageManager& imageManager;
};

using RenderTiles = std::vector<std::reference_wrapper<const RenderTile>>;
using RenderTiles = std::shared_ptr<const std::vector<std::reference_wrapper<const RenderTile>>>;

class RenderSource : protected TileObserver {
public:
Expand All @@ -64,8 +64,14 @@ class RenderSource : protected TileObserver {
virtual void prepare(const SourcePrepareParameters&) = 0;
virtual void updateFadingTiles() = 0;
virtual bool hasFadingTiles() const = 0;
// Returns a list of RenderTiles, sorted by tile id.
virtual RenderTiles getRenderTiles() = 0;
// If supported, returns a shared list of RenderTiles, sorted by tile id and excluding tiles hold for fade;
// returns nullptr otherwise.
virtual RenderTiles getRenderTiles() const { return nullptr; }
// If supported, returns a shared list of RenderTiles, sorted in opposite y position, so tiles with overlapping
// symbols are drawn on top of each other, with lower symbols being drawn on top of higher symbols;
// returns nullptr otherwise.
virtual RenderTiles getRenderTilesSortedByYPosition() const { return nullptr; }
// If supported, returns pointer to image data; returns nullptr otherwise.
virtual const ImageSourceRenderData* getImageRenderData() const { return nullptr; }
virtual const Tile* getRenderedTile(const UnwrappedTileID&) const { return nullptr; }

Expand Down
2 changes: 0 additions & 2 deletions src/mbgl/renderer/sources/render_image_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ class RenderImageSource final : public RenderSource {
bool needsRelayout,
const TileParameters&) final;

RenderTiles getRenderTiles() override { return {}; }

const ImageSourceRenderData* getImageRenderData() const override {
return renderData.get();
}
Expand Down
41 changes: 39 additions & 2 deletions src/mbgl/renderer/sources/render_tile_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/renderer/tile_render_data.hpp>
#include <mbgl/tile/vector_tile.hpp>
#include <mbgl/util/math.hpp>

namespace mbgl {

Expand Down Expand Up @@ -56,6 +57,9 @@ std::unique_ptr<RenderItem> RenderTileSource::createRenderItem() {
}

void RenderTileSource::prepare(const SourcePrepareParameters& parameters) {
bearing = parameters.transform.state.getBearing();
filteredRenderTiles = nullptr;
renderTilesSortedByY = nullptr;
auto tiles = makeMutable<std::vector<RenderTile>>();
tiles->reserve(tilePyramid.getRenderedTiles().size());
for (auto& entry : tilePyramid.getRenderedTiles()) {
Expand All @@ -73,8 +77,41 @@ bool RenderTileSource::hasFadingTiles() const {
return tilePyramid.hasFadingTiles();
}

RenderTiles RenderTileSource::getRenderTiles() {
return { renderTiles->begin(), renderTiles->end() };
RenderTiles RenderTileSource::getRenderTiles() const {
if (!filteredRenderTiles) {
auto result = std::make_shared<std::vector<std::reference_wrapper<const RenderTile>>>();
for (const auto& renderTile : *renderTiles) {
if (renderTile.holdForFade()) {
continue;
}
result->emplace_back(renderTile);
}
filteredRenderTiles = std::move(result);
}
return filteredRenderTiles;
}

RenderTiles RenderTileSource::getRenderTilesSortedByYPosition() const {
if (!renderTilesSortedByY) {
const auto comp = [bearing = this->bearing](const RenderTile& a, const RenderTile& b) {
Point<float> pa(a.id.canonical.x, a.id.canonical.y);
Point<float> pb(b.id.canonical.x, b.id.canonical.y);

auto par = util::rotate(pa, bearing);
auto pbr = util::rotate(pb, bearing);

return std::tie(b.id.canonical.z, par.y, par.x) < std::tie(a.id.canonical.z, pbr.y, pbr.x);
};

auto result = std::make_shared<std::vector<std::reference_wrapper<const RenderTile>>>();
result->reserve(renderTiles->size());
for (const auto& renderTile : *renderTiles) {
result->emplace_back(renderTile);
}
std::sort(result->begin(), result->end(), comp);
renderTilesSortedByY = std::move(result);
}
return renderTilesSortedByY;
}

const Tile* RenderTileSource::getRenderedTile(const UnwrappedTileID& tileID) const {
Expand Down
Loading

0 comments on commit 929824e

Please sign in to comment.