Skip to content

Commit

Permalink
Mark layer renderability inline, don't copy render items (#1882)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
mwilsnd and pre-commit-ci[bot] authored Nov 22, 2023
1 parent 5dbe89d commit 287f202
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 37 deletions.
3 changes: 3 additions & 0 deletions src/mbgl/renderer/render_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ class RenderLayer {
/// @param willRender Indicates if this layer should render or not
/// @param changes The collection of current pending change requests
virtual void markLayerRenderable(bool willRender, UniqueChangeRequestVec& changes);

/// Returns the current renderability mode of the layer
bool isLayerRenderable() const noexcept { return isRenderable; }
#endif

protected:
Expand Down
81 changes: 44 additions & 37 deletions src/mbgl/renderer/render_orchestrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,35 @@ namespace mbgl {

using namespace style;

LayerRenderItem::LayerRenderItem(RenderLayer& layer_, RenderSource* source_, uint32_t index_)
: layer(layer_),
source(source_),
index(index_) {}

bool LayerRenderItem::hasRenderPass(RenderPass pass) const {
return layer.get().hasRenderPass(pass);
}
void LayerRenderItem::upload(gfx::UploadPass& pass) const {
layer.get().upload(pass);
}
void LayerRenderItem::render(PaintParameters& parameters) const {
layer.get().render(parameters);
}
const std::string& LayerRenderItem::getName() const {
return layer.get().getID();
}

#if MLN_DRAWABLE_RENDERER
void LayerRenderItem::updateDebugDrawables(DebugLayerGroupMap&, PaintParameters&) const {};
#endif

namespace {

RendererObserver& nullObserver() {
static RendererObserver observer;
return observer;
}

class LayerRenderItem final : public RenderItem {
public:
LayerRenderItem(RenderLayer& layer_, RenderSource* source_, uint32_t index_)
: layer(layer_),
source(source_),
index(index_) {}
bool operator<(const LayerRenderItem& other) const { return index < other.index; }

std::reference_wrapper<RenderLayer> layer;
RenderSource* source;
const uint32_t index;

private:
bool hasRenderPass(RenderPass pass) const override { return layer.get().hasRenderPass(pass); }
void upload(gfx::UploadPass& pass) const override { layer.get().upload(pass); }
void render(PaintParameters& parameters) const override { layer.get().render(parameters); }
const std::string& getName() const override { return layer.get().getID(); }
#if MLN_DRAWABLE_RENDERER
void updateDebugDrawables(DebugLayerGroupMap&, PaintParameters&) const override{};
#endif
};

class RenderTreeImpl final : public RenderTree {
public:
RenderTreeImpl(std::unique_ptr<RenderTreeParameters> parameters_,
Expand All @@ -89,6 +89,7 @@ class RenderTreeImpl final : public RenderTree {
}
}

const std::set<LayerRenderItem>& getLayerRenderItemMap() const noexcept override { return layerRenderItems; }
RenderItems getLayerRenderItems() const override { return {layerRenderItems.begin(), layerRenderItems.end()}; }
RenderItems getSourceRenderItems() const override {
RenderItems result;
Expand Down Expand Up @@ -333,6 +334,11 @@ std::unique_ptr<RenderTree> RenderOrchestrator::createRenderTree(
filteredLayersForSource.reserve(layerImpls->size());
}

#if MLN_DRAWABLE_RENDERER
// Track which layers are flagged for rendering
std::vector<bool> updateList(orderedLayers.size());
#endif

// Update all sources and initialize renderItems.
for (const auto& sourceImpl : *sourceImpls) {
RenderSource* source = renderSources.at(sourceImpl->id).get();
Expand All @@ -357,6 +363,9 @@ std::unique_ptr<RenderTree> RenderOrchestrator::createRenderTree(
sourceNeedsRendering = true;
renderItemsEmplaceHint = layerRenderItems.emplace_hint(
renderItemsEmplaceHint, layer, source, static_cast<uint32_t>(index));
#if MLN_DRAWABLE_RENDERER
updateList[index] = true;
#endif
}
}
}
Expand All @@ -375,26 +384,24 @@ std::unique_ptr<RenderTree> RenderOrchestrator::createRenderTree(
}
renderItemsEmplaceHint = layerRenderItems.emplace_hint(
renderItemsEmplaceHint, layer, nullptr, static_cast<uint32_t>(index));
#if MLN_DRAWABLE_RENDERER
updateList[index] = true;
#endif
}
}
source->update(sourceImpl, filteredLayersForSource, sourceNeedsRendering, sourceNeedsRelayout, tileParameters);
filteredLayersForSource.clear();
}

#if MLN_DRAWABLE_RENDERER
// Mark layers included in the renderable set as renderable
// @TODO: Optimize this logic, combine with the above
for (auto orderedLayer : orderedLayers) {
RenderLayer& layer = orderedLayer;
layer.markLayerRenderable(
layerRenderItems.find(LayerRenderItem(layer, nullptr, static_cast<uint32_t>(layer.getLayerIndex()))) !=
layerRenderItems.end(),
changes);
}

// Add all change requests up to this point
addChanges(changes);
// Update all layers with their new renderability status, if it changed.
for (size_t i = 0; i < updateList.size(); i++) {
if (orderedLayers[i].get().isLayerRenderable() != updateList[i]) {
orderedLayers[i].get().markLayerRenderable(updateList[i], changes);
}
}
addChanges(changes);
#endif
}

renderTreeParameters->loaded = updateParameters->styleLoaded && isLoaded();
if (!isMapModeContinuous && !renderTreeParameters->loaded) {
Expand Down Expand Up @@ -903,8 +910,8 @@ void RenderOrchestrator::updateLayers(gfx::ShaderRegistry& shaders,
};

std::vector<std::unique_ptr<ChangeRequest>> changes;
for (const auto& item : renderTree.getLayerRenderItems()) {
auto& renderLayer = static_cast<const LayerRenderItem&>(item.get()).layer.get();
for (const auto& item : renderTree.getLayerRenderItemMap()) {
auto& renderLayer = item.layer.get();
renderLayer.update(shaders, context, state, updateParameters, renderTree, changes);
}
addChanges(changes);
Expand Down
21 changes: 21 additions & 0 deletions src/mbgl/renderer/render_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ class RenderItem {
#endif
};

class RenderSource;
class LayerRenderItem final : public RenderItem {
public:
LayerRenderItem(RenderLayer& layer_, RenderSource* source_, uint32_t index_);
bool operator<(const LayerRenderItem& other) const { return index < other.index; }

std::reference_wrapper<RenderLayer> layer;
RenderSource* source;
const uint32_t index;

private:
bool hasRenderPass(RenderPass pass) const override;
void upload(gfx::UploadPass& pass) const override;
void render(PaintParameters& parameters) const override;
const std::string& getName() const override;
#if MLN_DRAWABLE_RENDERER
void updateDebugDrawables(DebugLayerGroupMap&, PaintParameters&) const override;
#endif
};

using RenderItems = std::vector<std::reference_wrapper<const RenderItem>>;

class RenderTreeParameters {
Expand Down Expand Up @@ -76,6 +96,7 @@ class RenderTree {
virtual ~RenderTree() = default;
virtual void prepare() {}
// Render items
virtual const std::set<LayerRenderItem>& getLayerRenderItemMap() const noexcept = 0;
virtual RenderItems getLayerRenderItems() const = 0;
virtual RenderItems getSourceRenderItems() const = 0;
// Resources
Expand Down
5 changes: 5 additions & 0 deletions src/mbgl/renderer/renderer_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ void Renderer::Impl::render(const RenderTree& renderTree,
parameters.symbolFadeChange = renderTreeParameters.symbolFadeChange;
parameters.opaquePassCutoff = renderTreeParameters.opaquePassCutOff;
const auto& sourceRenderItems = renderTree.getSourceRenderItems();

#if MLN_DRAWABLE_RENDERER
const auto& layerRenderItems = renderTree.getLayerRenderItemMap();
#else
const auto& layerRenderItems = renderTree.getLayerRenderItems();
#endif

// - UPLOAD PASS -------------------------------------------------------------------------------
// Uploads all required buffers and images before we do any actual rendering.
Expand Down

0 comments on commit 287f202

Please sign in to comment.