Drawable Renderer (#1570), Eliminate extra style evaluation in layer update

This commit is contained in:
Tim Sylvester
2023-05-04 08:45:44 -07:00
committed by Bart Louwers
parent 17d997d395
commit 03598ff229
5 changed files with 51 additions and 39 deletions

View File

@@ -38,12 +38,11 @@ void RenderBackgroundLayer::transition(const TransitionParameters& parameters) {
unevaluated = impl_cast(baseImpl).paint.transitioned(parameters, std::move(unevaluated));
}
void RenderBackgroundLayer::evaluate(
const PropertyEvaluationParameters &parameters) {
auto properties = makeMutable<BackgroundLayerProperties>(
staticImmutableCast<BackgroundLayer::Impl>(baseImpl),
parameters.getCrossfadeParameters(),
unevaluated.evaluate(parameters));
void RenderBackgroundLayer::evaluate(const PropertyEvaluationParameters& parameters) {
auto evaluated = unevaluated.evaluate(parameters);
auto properties = makeMutable<BackgroundLayerProperties>(staticImmutableCast<BackgroundLayer::Impl>(baseImpl),
parameters.getCrossfadeParameters(),
evaluated);
passes = properties->evaluated.get<style::BackgroundOpacity>() == 0.0f ? RenderPass::None
: (!unevaluated.get<style::BackgroundPattern>().isUndefined() ||
@@ -53,6 +52,12 @@ void RenderBackgroundLayer::evaluate(
// Supply both - evaluated based on opaquePassCutoff in render().
: RenderPass::Opaque | RenderPass::Translucent;
properties->renderPasses = mbgl::underlying_type(passes);
// Store a copy in the layer for layer::update.
// TODO: This whole method should eventually move to BackgroundLayer
const_cast<BackgroundLayer::Impl&>(impl_cast(baseImpl)).setUnevaluated(unevaluated);
const_cast<BackgroundLayer::Impl&>(impl_cast(baseImpl)).setEvaluated(makeMutable<BackgroundPaintProperties::PossiblyEvaluated>(evaluated));
evaluatedProperties = std::move(properties);
}

View File

@@ -794,7 +794,7 @@ void RenderOrchestrator::updateLayers(gfx::ShaderRegistry& shaders,
kv.second->layerRemoved(changes);
}
for (auto& kv : layersAdded) {
kv.second->layerAdded(shaders, context, state, evalParameters, changes);
kv.second->layerAdded(shaders, context, state, changes);
}
// Layer index, similar but not identical to `layerRenderItems` index in render_impl
@@ -803,7 +803,7 @@ void RenderOrchestrator::updateLayers(gfx::ShaderRegistry& shaders,
auto index = static_cast<int32_t>(layerImpls->size()) - 1;
for (auto& impl : *layerImpls) {
impl->update(index--, context, state, evalParameters, changes);
impl->update(index--, context, state, changes);
}
addChanges(changes);

View File

@@ -1,8 +1,11 @@
#pragma once
#include <mbgl/gfx/drawable.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/style/types.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/renderer/change_request.hpp>
#include <mbgl/util/suppress_copies.hpp>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
@@ -66,13 +69,11 @@ public:
virtual void update(int32_t /*layerIndex*/,
gfx::Context&,
const TransformState&,
const PropertyEvaluationParameters&,
UniqueChangeRequestVec&) const {}
virtual void layerAdded(gfx::ShaderRegistry&,
gfx::Context&,
const TransformState&,
const PropertyEvaluationParameters&,
UniqueChangeRequestVec&) const {}
virtual void layerRemoved(UniqueChangeRequestVec&) const {}
@@ -86,6 +87,27 @@ public:
protected:
Impl(const Impl&) = default;
// Add a deletion change request for each drawable in a collection
template <typename T>
static void removeDrawables(T beg,
const T end,
UniqueChangeRequestVec& changes,
std::function<util::SimpleIdentity(const T&)> f) {
for (; beg != end; ++beg) {
changes.emplace_back(std::make_unique<RemoveDrawableRequest>(f(beg)));
}
}
mutable util::SuppressCopies<std::mutex> mutex;
mutable gfx::ShaderProgramBasePtr shader;
mutable std::unordered_map<OverscaledTileID, gfx::DrawablePtr> tileDrawables;
mutable struct Stats {
size_t propertyEvaluations = 0;
size_t tileDrawablesAdded = 0;
size_t tileDrawablesRemoved = 0;
} stats;
};
// To be used in the inherited classes.

View File

@@ -19,7 +19,6 @@ namespace style {
BackgroundLayer::Impl::Impl(const Impl& other)
: Layer::Impl(other),
tileDrawables(other.tileDrawables),
lastColor(other.lastColor),
paint(other.paint) {}
@@ -32,7 +31,6 @@ constexpr auto shaderName = "background_generic";
void BackgroundLayer::Impl::layerAdded(gfx::ShaderRegistry& shaders,
gfx::Context& context,
const TransformState&,
const PropertyEvaluationParameters&,
UniqueChangeRequestVec&) const {
{
std::unique_lock<std::mutex> guard(mutex);
@@ -59,14 +57,14 @@ void BackgroundLayer::Impl::layerRemoved(UniqueChangeRequestVec& changes) const
void BackgroundLayer::Impl::update(const int32_t layerIndex,
gfx::Context& context,
const TransformState& state,
const PropertyEvaluationParameters& evalParameters,
UniqueChangeRequestVec& changes) const {
std::unique_lock<std::mutex> guard(mutex);
if (!unevaluated) {
unevaluated = paint.untransitioned();
if (!unevaluatedProperties || !evaluatedProperties) {
// not evaluated yet, we can't update
return;
}
const auto evaluated = unevaluated->evaluate(evalParameters);
const auto& evaluated = **evaluatedProperties;
// TODO: If background is solid, we can skip drawables and rely on the clear color
// const auto passes = eval.get<style::BackgroundOpacity>() == 0.0f

View File

@@ -1,7 +1,5 @@
#pragma once
#include <mbgl/gfx/drawable.hpp>
#include <mbgl/renderer/change_request.hpp>
#include <mbgl/style/layer_impl.hpp>
#include <mbgl/style/layers/background_layer.hpp>
#include <mbgl/style/layers/background_layer_properties.hpp>
@@ -36,7 +34,6 @@ public:
void layerAdded(gfx::ShaderRegistry&,
gfx::Context&,
const TransformState&,
const PropertyEvaluationParameters&,
UniqueChangeRequestVec&) const override;
void layerRemoved(UniqueChangeRequestVec&) const override;
@@ -44,33 +41,23 @@ public:
void update(int32_t layerIndex,
gfx::Context&,
const TransformState&,
const PropertyEvaluationParameters&,
UniqueChangeRequestVec&) const override;
private:
// Add a deletion change request for each drawable in a collection
template <typename T>
static void removeDrawables(T beg,
const T end,
UniqueChangeRequestVec& changes,
std::function<util::SimpleIdentity(const T&)> f) {
for (; beg != end; ++beg) {
changes.emplace_back(std::make_unique<RemoveDrawableRequest>(f(beg)));
}
void setUnevaluated(BackgroundPaintProperties::Unevaluated value) {
unevaluatedProperties = value;
}
void setEvaluated(Immutable<BackgroundPaintProperties::PossiblyEvaluated> value) {
evaluatedProperties = std::move(value);
++stats.propertyEvaluations;
}
mutable std::mutex mutex;
mutable gfx::ShaderProgramBasePtr shader;
mutable std::unordered_map<OverscaledTileID, gfx::DrawablePtr> tileDrawables;
private:
mutable std::optional<Color> lastColor;
mutable int32_t lastLayerIndex = -1;
mutable struct Stats {
size_t tileDrawablesAdded = 0;
size_t tileDrawablesRemoved = 0;
} stats;
mutable std::optional<BackgroundPaintProperties::Unevaluated> unevaluated;
std::optional<BackgroundPaintProperties::Unevaluated> unevaluatedProperties;
// Latest evaluated properties.
std::optional<Immutable<BackgroundPaintProperties::PossiblyEvaluated>> evaluatedProperties;
public:
BackgroundPaintProperties::Transitionable paint;