Drawable Renderer (#1570), Move drawable management back to render layers, start on fill layer
This commit is contained in:
committed by
Bart Louwers
parent
ef9ef1a106
commit
408a1e39e2
@@ -1,7 +1,11 @@
|
||||
#include <mbgl/renderer/layers/render_background_layer.hpp>
|
||||
|
||||
#include <mbgl/gfx/context.hpp>
|
||||
#include <mbgl/gfx/cull_face_mode.hpp>
|
||||
#include <mbgl/gfx/shader_registry.hpp>
|
||||
#include <mbgl/renderer/layers/render_background_layer.hpp>
|
||||
#include <mbgl/gfx/drawable_builder.hpp>
|
||||
#include <mbgl/style/layers/background_layer_impl.hpp>
|
||||
#include <mbgl/style/layer_properties.hpp>
|
||||
#include <mbgl/renderer/bucket.hpp>
|
||||
#include <mbgl/renderer/change_request.hpp>
|
||||
#include <mbgl/renderer/upload_parameters.hpp>
|
||||
@@ -10,11 +14,13 @@
|
||||
#include <mbgl/renderer/image_manager.hpp>
|
||||
#include <mbgl/renderer/render_static_data.hpp>
|
||||
#include <mbgl/programs/programs.hpp>
|
||||
#include <mbgl/shaders/gl/shader_program_gl.hpp>
|
||||
#include <mbgl/shaders/shader_program_base.hpp>
|
||||
#include <mbgl/util/tile_cover.hpp>
|
||||
#include <mbgl/map/transform_state.hpp>
|
||||
#include <mbgl/util/logging.hpp>
|
||||
|
||||
#include <unordered_set>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
using namespace style;
|
||||
@@ -52,12 +58,6 @@ void RenderBackgroundLayer::evaluate(const PropertyEvaluationParameters& paramet
|
||||
: 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);
|
||||
}
|
||||
|
||||
@@ -193,4 +193,146 @@ void RenderBackgroundLayer::prepare(const LayerPrepareParameters& params) {
|
||||
}
|
||||
}
|
||||
|
||||
constexpr auto shaderName = "background_generic";
|
||||
|
||||
void RenderBackgroundLayer::layerRemoved(UniqueChangeRequestVec& changes) {
|
||||
// TODO: This isn't happening on style change, so old tile drawables are being left active
|
||||
|
||||
// Remove everything
|
||||
decltype(tileDrawables) localDrawables;
|
||||
{
|
||||
std::unique_lock<std::mutex> guard(mutex);
|
||||
localDrawables = std::move(tileDrawables);
|
||||
}
|
||||
|
||||
removeDrawables<decltype(localDrawables)::const_iterator>(
|
||||
localDrawables.cbegin(), localDrawables.cend(), changes, [](auto& ii) { return ii->second->getId(); });
|
||||
}
|
||||
|
||||
void RenderBackgroundLayer::update(const int32_t layerIndex,
|
||||
gfx::ShaderRegistry& shaders,
|
||||
gfx::Context& context,
|
||||
const TransformState& state,
|
||||
UniqueChangeRequestVec& changes) {
|
||||
std::unique_lock<std::mutex> guard(mutex);
|
||||
|
||||
if (!shader) {
|
||||
shader = context.getGenericShader(shaders, shaderName);
|
||||
}
|
||||
|
||||
const auto& evaluated = getEvaluated<BackgroundLayerProperties>(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
|
||||
// ? RenderPass::None
|
||||
// : (!uneval.get<style::BackgroundPattern>().isUndefined()
|
||||
// || eval.get<style::BackgroundOpacity>() < 1.0f
|
||||
// || eval.get<style::BackgroundColor>().a < 1.0f)
|
||||
// ? RenderPass::Translucent
|
||||
// : RenderPass::Opaque | RenderPass::Translucent; // evaluated based on opaquePassCutoff in render()
|
||||
|
||||
// unevaluated.hasTransition();
|
||||
// getCrossfade<BackgroundLayerProperties>(evaluatedProperties).t != 1;
|
||||
|
||||
std::optional<Color> color;
|
||||
if (evaluated.get<BackgroundPattern>().from.empty()) {
|
||||
const auto opacity = evaluated.get<style::BackgroundOpacity>();
|
||||
if (opacity > 0.0f) {
|
||||
color = evaluated.get<BackgroundColor>() * evaluated.get<BackgroundOpacity>();
|
||||
}
|
||||
}
|
||||
|
||||
// If the result is transparent or missing, just remove any existing drawables and stop
|
||||
if (!color) {
|
||||
removeDrawables<decltype(tileDrawables)::const_iterator>(
|
||||
tileDrawables.cbegin(), tileDrawables.cend(), changes, [](auto& ii) { return ii->second->getId(); });
|
||||
tileDrawables.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
const bool colorChange = (color != lastColor);
|
||||
const bool layerChange = (layerIndex != lastLayerIndex);
|
||||
lastColor = color;
|
||||
lastLayerIndex = layerIndex;
|
||||
|
||||
const auto zoom = state.getIntegerZoom();
|
||||
const auto tileCover = util::tileCover(state, zoom);
|
||||
|
||||
// Drawables per overscaled or canonical tile?
|
||||
// const UnwrappedTileID unwrappedTileID = tileID.toUnwrapped();
|
||||
|
||||
// Put the tile cover into a searchable form.
|
||||
// TODO: Likely better to sort and `std::binary_search` the vector.
|
||||
// If it's returned in a well-defined order, we might not even need to sort.
|
||||
const std::unordered_set<OverscaledTileID> newTileIDs(tileCover.begin(), tileCover.end());
|
||||
|
||||
// For each existing tile drawable...
|
||||
for (auto iter = tileDrawables.begin(); iter != tileDrawables.end();) {
|
||||
const auto& drawable = iter->second;
|
||||
|
||||
// Has this tile dropped out of the cover set?
|
||||
if (newTileIDs.find(iter->first) == newTileIDs.end()) {
|
||||
// remove it
|
||||
changes.emplace_back(std::make_unique<RemoveDrawableRequest>(drawable->getId()));
|
||||
// Log::Warning(Event::General, "Removing drawable for " + util::toString(iter->first) + " total " +
|
||||
// std::to_string(stats.tileDrawablesRemoved+1));
|
||||
iter = tileDrawables.erase(iter);
|
||||
++stats.tileDrawablesRemoved;
|
||||
continue;
|
||||
}
|
||||
++iter;
|
||||
|
||||
// If the color evaluated to a new value, update all vertexes of the drawable to the new color
|
||||
if (colorChange) {
|
||||
drawable->resetColor(*color);
|
||||
}
|
||||
if (layerChange) {
|
||||
drawable->setLayerIndex(layerIndex);
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<gfx::DrawableBuilder> builder;
|
||||
|
||||
// For each tile in the cover set, add a tile drawable if one doesn't already exist.
|
||||
// We currently assume only one drawable per tile.
|
||||
for (const auto& tileID : tileCover) {
|
||||
const auto result = tileDrawables.insert(std::make_pair(tileID, gfx::DrawablePtr()));
|
||||
if (!result.second) {
|
||||
// Already present
|
||||
// TODO: Update matrix here or in the tweaker?
|
||||
continue;
|
||||
}
|
||||
|
||||
// We actually need to build things, so set up a builder if we haven't already
|
||||
if (!builder) {
|
||||
builder = context.createDrawableBuilder("background");
|
||||
builder->setShader(shader);
|
||||
builder->addTweaker(context.createDrawableTweaker());
|
||||
builder->setColor(*color);
|
||||
builder->setColorMode(gfx::DrawableBuilder::ColorMode::PerDrawable);
|
||||
builder->setDepthType(gfx::DepthMaskType::ReadWrite);
|
||||
builder->setLayerIndex(layerIndex);
|
||||
}
|
||||
|
||||
// Tile coordinates are fixed...
|
||||
builder->addQuad(0, 0, util::EXTENT, util::EXTENT);
|
||||
|
||||
// ... they're placed with the matrix in the uniforms, which changes with the view
|
||||
builder->setMatrix(/*parameters.matrixForTile(tileID.toUnwrapped())*/ matrix::identity4());
|
||||
|
||||
builder->flush();
|
||||
|
||||
auto drawables = builder->clearDrawables();
|
||||
if (!drawables.empty()) {
|
||||
auto& drawable = drawables[0];
|
||||
drawable->setTileID(tileID);
|
||||
result.first->second = drawable;
|
||||
changes.emplace_back(std::make_unique<AddDrawableRequest>(std::move(drawable)));
|
||||
++stats.tileDrawablesAdded;
|
||||
// Log::Warning(Event::General, "Adding drawable for " + util::toString(tileID) + " total " +
|
||||
// std::to_string(stats.tileDrawablesAdded+1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -23,6 +23,11 @@ public:
|
||||
explicit RenderBackgroundLayer(Immutable<style::BackgroundLayer::Impl>);
|
||||
~RenderBackgroundLayer() override;
|
||||
|
||||
void layerRemoved(UniqueChangeRequestVec&) override;
|
||||
|
||||
/// Generate any changes needed by the layer
|
||||
void update(int32_t layerIndex, gfx::ShaderRegistry&, gfx::Context&, const TransformState&, UniqueChangeRequestVec&) override;
|
||||
|
||||
private:
|
||||
void transition(const TransitionParameters&) override;
|
||||
void evaluate(const PropertyEvaluationParameters&) override;
|
||||
|
||||
@@ -259,4 +259,261 @@ bool RenderFillLayer::queryIntersectsFeature(const GeometryCoordinates& queryGeo
|
||||
feature.getGeometries());
|
||||
}
|
||||
|
||||
constexpr auto shaderName = "background_generic";
|
||||
|
||||
void RenderFillLayer::layerRemoved(UniqueChangeRequestVec& changes) {
|
||||
// TODO: This isn't happening on style change, so old tile drawables are being left active
|
||||
|
||||
// Remove everything
|
||||
decltype(tileDrawables) localDrawables;
|
||||
{
|
||||
std::unique_lock<std::mutex> guard(mutex);
|
||||
localDrawables = std::move(tileDrawables);
|
||||
}
|
||||
|
||||
removeDrawables<decltype(localDrawables)::const_iterator>(
|
||||
localDrawables.cbegin(), localDrawables.cend(), changes, [](auto& ii) { return ii->second->getId(); });
|
||||
}
|
||||
|
||||
void RenderFillLayer::update(const int32_t layerIndex,
|
||||
gfx::ShaderRegistry& shaders,
|
||||
gfx::Context& context,
|
||||
const TransformState& state,
|
||||
UniqueChangeRequestVec& changes) {
|
||||
std::unique_lock<std::mutex> guard(mutex);
|
||||
|
||||
if (!shader) {
|
||||
shader = context.getGenericShader(shaders, shaderName);
|
||||
}
|
||||
|
||||
//const auto& evaluated = getEvaluated<FillLayerProperties>(evaluatedProperties);
|
||||
// std::optional<Color> color;
|
||||
// if (evaluated.get<FillPattern>().from.empty()) {
|
||||
// const auto opacity = evaluated.get<style::FillOpacity>();
|
||||
// if (opacity > 0.0f) {
|
||||
// color = evaluated.get<FillColor>() * evaluated.get<FillOpacity>();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // If the result is transparent or missing, just remove any existing drawables and stop
|
||||
// if (!color) {
|
||||
// removeDrawables<decltype(tileDrawables)::const_iterator>(
|
||||
// tileDrawables.cbegin(), tileDrawables.cend(), changes, [](auto& ii) { return ii->second->getId(); });
|
||||
// tileDrawables.clear();
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// const bool colorChange = (color != lastColor);
|
||||
// const bool layerChange = (layerIndex != lastLayerIndex);
|
||||
// lastColor = color;
|
||||
// lastLayerIndex = layerIndex;
|
||||
//
|
||||
// const auto zoom = state.getIntegerZoom();
|
||||
// const auto tileCover = util::tileCover(state, zoom);
|
||||
//
|
||||
// // Drawables per overscaled or canonical tile?
|
||||
// // const UnwrappedTileID unwrappedTileID = tileID.toUnwrapped();
|
||||
//
|
||||
// // Put the tile cover into a searchable form.
|
||||
// // TODO: Likely better to sort and `std::binary_search` the vector.
|
||||
// // If it's returned in a well-defined order, we might not even need to sort.
|
||||
// const std::unordered_set<OverscaledTileID> newTileIDs(tileCover.begin(), tileCover.end());
|
||||
//
|
||||
// // For each existing tile drawable...
|
||||
// for (auto iter = tileDrawables.begin(); iter != tileDrawables.end();) {
|
||||
// const auto& drawable = iter->second;
|
||||
//
|
||||
// // Has this tile dropped out of the cover set?
|
||||
// if (newTileIDs.find(iter->first) == newTileIDs.end()) {
|
||||
// // remove it
|
||||
// changes.emplace_back(std::make_unique<RemoveDrawableRequest>(drawable->getId()));
|
||||
// // Log::Warning(Event::General, "Removing drawable for " + util::toString(iter->first) + " total " +
|
||||
// // std::to_string(stats.tileDrawablesRemoved+1));
|
||||
// iter = tileDrawables.erase(iter);
|
||||
// ++stats.tileDrawablesRemoved;
|
||||
// continue;
|
||||
// }
|
||||
// ++iter;
|
||||
//
|
||||
// // If the color evaluated to a new value, update all vertexes of the drawable to the new color
|
||||
// if (colorChange) {
|
||||
// drawable->resetColor(*color);
|
||||
// }
|
||||
// if (layerChange) {
|
||||
// drawable->setLayerIndex(layerIndex);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// std::unique_ptr<gfx::DrawableBuilder> builder;
|
||||
//
|
||||
// // For each tile in the cover set, add a tile drawable if one doesn't already exist.
|
||||
// // We currently assume only one drawable per tile.
|
||||
// for (const auto& tileID : tileCover) {
|
||||
// const auto result = tileDrawables.insert(std::make_pair(tileID, gfx::DrawablePtr()));
|
||||
// if (!result.second) {
|
||||
// // Already present
|
||||
// // TODO: Update matrix here or in the tweaker?
|
||||
// continue;
|
||||
// }
|
||||
//
|
||||
// // We actually need to build things, so set up a builder if we haven't already
|
||||
// if (!builder) {
|
||||
// builder = context.createDrawableBuilder("background");
|
||||
// builder->setShader(shader);
|
||||
// builder->addTweaker(context.createDrawableTweaker());
|
||||
// builder->setColor(*color);
|
||||
// builder->setColorMode(gfx::DrawableBuilder::ColorMode::PerDrawable);
|
||||
// builder->setDepthType(gfx::DepthMaskType::ReadWrite);
|
||||
// builder->setLayerIndex(layerIndex);
|
||||
// }
|
||||
//
|
||||
// // Tile coordinates are fixed...
|
||||
// builder->addQuad(0, 0, util::EXTENT, util::EXTENT);
|
||||
//
|
||||
// // ... they're placed with the matrix in the uniforms, which changes with the view
|
||||
// builder->setMatrix(/*parameters.matrixForTile(tileID.toUnwrapped())*/ matrix::identity4());
|
||||
//
|
||||
// builder->flush();
|
||||
//
|
||||
// auto drawables = builder->clearDrawables();
|
||||
// if (!drawables.empty()) {
|
||||
// auto& drawable = drawables[0];
|
||||
// drawable->setTileID(tileID);
|
||||
// result.first->second = drawable;
|
||||
// changes.emplace_back(std::make_unique<AddDrawableRequest>(std::move(drawable)));
|
||||
// ++stats.tileDrawablesAdded;
|
||||
// // Log::Warning(Event::General, "Adding drawable for " + util::toString(tileID) + " total " +
|
||||
// // std::to_string(stats.tileDrawablesAdded+1));
|
||||
// }
|
||||
// }
|
||||
|
||||
if (unevaluated.get<FillPattern>().isUndefined()) {
|
||||
// parameters.renderTileClippingMasks(renderTiles);
|
||||
for (const RenderTile& tile : *renderTiles) {
|
||||
const auto renderPass = RenderPass::Translucent;
|
||||
const LayerRenderData* renderData = getRenderDataForPass(tile, renderPass);
|
||||
if (!renderData) {
|
||||
continue;
|
||||
}
|
||||
//auto& bucket = static_cast<FillBucket&>(*renderData->bucket);
|
||||
//const auto& evaluated = getEvaluated<FillLayerProperties>(renderData->layerProperties);
|
||||
|
||||
// evaluated.get<FillTranslate>(),
|
||||
// evaluated.get<FillTranslateAnchor>(),
|
||||
// parameters.stencilModeForClipping(tile.id),
|
||||
// parameters.colorModeForRenderPass(),
|
||||
// gfx::CullFaceMode::disabled(),
|
||||
//
|
||||
// const auto isOpaque = (evaluated.get<FillColor>().constantOr(Color()).a >= 1.0f &&
|
||||
// evaluated.get<FillOpacity>().constantOr(0) >= 1.0f
|
||||
// );// && layerIndex >= parameters.opaquePassCutoff);
|
||||
// const auto fillRenderPass = isOpaque ? RenderPass::Opaque : RenderPass::Translucent;
|
||||
//
|
||||
// if (renderPass == fillRenderPass) {
|
||||
// }
|
||||
|
||||
//const auto depthMask = (renderPass == RenderPass::Opaque) ? gfx::DepthMaskType::ReadWrite
|
||||
// : gfx::DepthMaskType::ReadOnly;
|
||||
|
||||
// const auto depthMode = parameters.depthModeForSublayer(1, depthMask);
|
||||
|
||||
// if (evaluated.get<FillAntialias>() && parameters.pass == RenderPass::Translucent) {
|
||||
// draw(*fillOutlineProgram,
|
||||
// gfx::Lines{2.0f},
|
||||
// parameters.depthModeForSublayer(unevaluated.get<FillOutlineColor>().isUndefined() ? 2 : 0,
|
||||
// gfx::DepthMaskType::ReadOnly),
|
||||
// *bucket.lineIndexBuffer,
|
||||
// bucket.lineSegments,
|
||||
// FillOutlineProgram::TextureBindings{});
|
||||
// }
|
||||
}
|
||||
} else {
|
||||
// if (parameters.pass != RenderPass::Translucent) {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// parameters.renderTileClippingMasks(renderTiles);
|
||||
//
|
||||
// for (const RenderTile& tile : *renderTiles) {
|
||||
// const LayerRenderData* renderData = getRenderDataForPass(tile, parameters.pass);
|
||||
// if (!renderData) {
|
||||
// continue;
|
||||
// }
|
||||
// auto& bucket = static_cast<FillBucket&>(*renderData->bucket);
|
||||
// const auto& evaluated = getEvaluated<FillLayerProperties>(renderData->layerProperties);
|
||||
// const auto& crossfade = getCrossfade<FillLayerProperties>(renderData->layerProperties);
|
||||
//
|
||||
// const auto& fillPatternValue = evaluated.get<FillPattern>().constantOr(Faded<expression::Image>{"", ""});
|
||||
// std::optional<ImagePosition> patternPosA = tile.getPattern(fillPatternValue.from.id());
|
||||
// std::optional<ImagePosition> patternPosB = tile.getPattern(fillPatternValue.to.id());
|
||||
//
|
||||
// auto draw = [&](auto& programInstance,
|
||||
// const auto& drawMode,
|
||||
// const auto& depthMode,
|
||||
// const auto& indexBuffer,
|
||||
// const auto& segments,
|
||||
// auto&& textureBindings) {
|
||||
// const auto& paintPropertyBinders = bucket.paintPropertyBinders.at(getID());
|
||||
// paintPropertyBinders.setPatternParameters(patternPosA, patternPosB, crossfade);
|
||||
//
|
||||
// const auto allUniformValues = programInstance.computeAllUniformValues(
|
||||
// FillPatternProgram::layoutUniformValues(
|
||||
// tile.translatedMatrix(
|
||||
// evaluated.get<FillTranslate>(), evaluated.get<FillTranslateAnchor>(), parameters.state),
|
||||
// parameters.backend.getDefaultRenderable().getSize(),
|
||||
// tile.getIconAtlasTexture().size,
|
||||
// crossfade,
|
||||
// tile.id,
|
||||
// parameters.state,
|
||||
// parameters.pixelRatio),
|
||||
// paintPropertyBinders,
|
||||
// evaluated,
|
||||
// static_cast<float>(parameters.state.getZoom()));
|
||||
// const auto allAttributeBindings = programInstance.computeAllAttributeBindings(
|
||||
// *bucket.vertexBuffer, paintPropertyBinders, evaluated);
|
||||
//
|
||||
// checkRenderability(parameters, programInstance.activeBindingCount(allAttributeBindings));
|
||||
//
|
||||
// programInstance.draw(parameters.context,
|
||||
// *parameters.renderPass,
|
||||
// drawMode,
|
||||
// depthMode,
|
||||
// parameters.stencilModeForClipping(tile.id),
|
||||
// parameters.colorModeForRenderPass(),
|
||||
// gfx::CullFaceMode::disabled(),
|
||||
// indexBuffer,
|
||||
// segments,
|
||||
// allUniformValues,
|
||||
// allAttributeBindings,
|
||||
// std::forward<decltype(textureBindings)>(textureBindings),
|
||||
// getID());
|
||||
// };
|
||||
//
|
||||
// if (bucket.triangleIndexBuffer) {
|
||||
// draw(*fillPatternProgram,
|
||||
// gfx::Triangles(),
|
||||
// parameters.depthModeForSublayer(1, gfx::DepthMaskType::ReadWrite),
|
||||
// *bucket.triangleIndexBuffer,
|
||||
// bucket.triangleSegments,
|
||||
// FillPatternProgram::TextureBindings{
|
||||
// textures::image::Value{tile.getIconAtlasTexture().getResource(),
|
||||
// gfx::TextureFilterType::Linear},
|
||||
// });
|
||||
// }
|
||||
// if (evaluated.get<FillAntialias>() && unevaluated.get<FillOutlineColor>().isUndefined()) {
|
||||
// draw(*fillOutlinePatternProgram,
|
||||
// gfx::Lines{2.0f},
|
||||
// parameters.depthModeForSublayer(2, gfx::DepthMaskType::ReadOnly),
|
||||
// *bucket.lineIndexBuffer,
|
||||
// bucket.lineSegments,
|
||||
// FillOutlinePatternProgram::TextureBindings{
|
||||
// textures::image::Value{tile.getIconAtlasTexture().getResource(),
|
||||
// gfx::TextureFilterType::Linear},
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -18,6 +18,11 @@ public:
|
||||
explicit RenderFillLayer(Immutable<style::FillLayer::Impl>);
|
||||
~RenderFillLayer() override;
|
||||
|
||||
void layerRemoved(UniqueChangeRequestVec&) override;
|
||||
|
||||
/// Generate any changes needed by the layer
|
||||
void update(int32_t layerIndex, gfx::ShaderRegistry&, gfx::Context&, const TransformState&, UniqueChangeRequestVec&) override;
|
||||
|
||||
private:
|
||||
void transition(const TransitionParameters&) override;
|
||||
void evaluate(const PropertyEvaluationParameters&) override;
|
||||
|
||||
@@ -1,29 +1,39 @@
|
||||
#pragma once
|
||||
#include <list>
|
||||
#include <mbgl/gfx/drawable.hpp>
|
||||
#include <mbgl/gfx/drawable.hpp>
|
||||
#include <mbgl/layout/layout.hpp>
|
||||
#include <mbgl/renderer/change_request.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>
|
||||
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
class Bucket;
|
||||
class ChangeRequest;
|
||||
class TransitionParameters;
|
||||
class PropertyEvaluationParameters;
|
||||
class UploadParameters;
|
||||
class PaintParameters;
|
||||
class RenderTile;
|
||||
class TransformState;
|
||||
class PatternAtlas;
|
||||
class LineAtlas;
|
||||
class SymbolBucket;
|
||||
class DynamicFeatureIndex;
|
||||
class LineAtlas;
|
||||
class PropertyEvaluationParameters;
|
||||
class PaintParameters;
|
||||
class PatternAtlas;
|
||||
class RenderTile;
|
||||
class SymbolBucket;
|
||||
class TransformState;
|
||||
class TransitionParameters;
|
||||
class UploadParameters;
|
||||
|
||||
using UniqueChangeRequest = std::unique_ptr<ChangeRequest>;
|
||||
using UniqueChangeRequestVec = std::vector<UniqueChangeRequest>;
|
||||
|
||||
namespace gfx {
|
||||
class Context;
|
||||
class ShaderRegistry;
|
||||
} // namespace gfx
|
||||
|
||||
class LayerRenderData {
|
||||
public:
|
||||
@@ -130,6 +140,11 @@ public:
|
||||
// TODO: Only for background layers.
|
||||
virtual std::optional<Color> getSolidBackground() const;
|
||||
|
||||
/// Generate any changes needed by the layer
|
||||
virtual void update(int32_t /*layerIndex*/, gfx::ShaderRegistry&, gfx::Context&, const TransformState&, UniqueChangeRequestVec&) {}
|
||||
|
||||
virtual void layerRemoved(UniqueChangeRequestVec&) {}
|
||||
|
||||
protected:
|
||||
// Checks whether the current hardware can render this layer. If it can't,
|
||||
// we'll show a warning in the console to inform the developer.
|
||||
@@ -139,6 +154,17 @@ protected:
|
||||
|
||||
const LayerRenderData* getRenderDataForPass(const RenderTile&, RenderPass) const;
|
||||
|
||||
// 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)));
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
// Stores current set of tiles to be rendered for this layer.
|
||||
RenderTiles renderTiles;
|
||||
@@ -151,6 +177,18 @@ protected:
|
||||
|
||||
gfx::DrawablePtr drawables;
|
||||
|
||||
std::mutex mutex;
|
||||
gfx::ShaderProgramBasePtr shader;
|
||||
std::unordered_map<OverscaledTileID, gfx::DrawablePtr> tileDrawables;
|
||||
std::optional<Color> lastColor;
|
||||
int32_t lastLayerIndex = -1;
|
||||
|
||||
struct Stats {
|
||||
size_t propertyEvaluations = 0;
|
||||
size_t tileDrawablesAdded = 0;
|
||||
size_t tileDrawablesRemoved = 0;
|
||||
} stats;
|
||||
|
||||
private:
|
||||
// Some layers may not render correctly on some hardware when the vertex
|
||||
// attribute limit of that GPU is exceeded. More attributes are used when
|
||||
|
||||
@@ -138,8 +138,6 @@ void RenderOrchestrator::setObserver(RendererObserver* observer_) {
|
||||
|
||||
std::unique_ptr<RenderTree> RenderOrchestrator::createRenderTree(
|
||||
const std::shared_ptr<UpdateParameters>& updateParameters) {
|
||||
layersAdded.clear();
|
||||
layersRemoved.clear();
|
||||
|
||||
const bool isMapModeContinuous = updateParameters->mode == MapMode::Continuous;
|
||||
if (!isMapModeContinuous) {
|
||||
@@ -221,15 +219,23 @@ std::unique_ptr<RenderTree> RenderOrchestrator::createRenderTree(
|
||||
imageManager->notifyIfMissingImageAdded();
|
||||
imageManager->setLoaded(updateParameters->spriteLoaded);
|
||||
|
||||
LayerDifference layerDiff = diffLayers(layerImpls, updateParameters->layers);
|
||||
const LayerDifference layerDiff = diffLayers(layerImpls, updateParameters->layers);
|
||||
layerImpls = updateParameters->layers;
|
||||
const bool layersAddedOrRemoved = !layerDiff.added.empty() || !layerDiff.removed.empty();
|
||||
|
||||
std::vector<std::unique_ptr<ChangeRequest>> changes;
|
||||
|
||||
// Remove render layers for removed layers.
|
||||
for (const auto& entry : layerDiff.removed) {
|
||||
renderLayers.erase(entry.first);
|
||||
const auto hit = renderLayers.find(entry.first);
|
||||
if (hit != renderLayers.end()) {
|
||||
hit->second->layerRemoved(changes);
|
||||
renderLayers.erase(hit);
|
||||
}
|
||||
}
|
||||
|
||||
addChanges(changes);
|
||||
|
||||
// Create render layers for newly added layers.
|
||||
for (const auto& entry : layerDiff.added) {
|
||||
auto renderLayer = LayerManager::get()->createRenderLayer(entry.second);
|
||||
@@ -456,9 +462,6 @@ std::unique_ptr<RenderTree> RenderOrchestrator::createRenderTree(
|
||||
}
|
||||
}
|
||||
|
||||
layersAdded = std::move(layerDiff.added);
|
||||
layersRemoved = std::move(layerDiff.removed);
|
||||
|
||||
return std::make_unique<RenderTreeImpl>(std::move(renderTreeParameters),
|
||||
std::move(layerRenderItems),
|
||||
std::move(sourceRenderItems),
|
||||
@@ -790,20 +793,13 @@ void RenderOrchestrator::updateLayers(gfx::ShaderRegistry& shaders,
|
||||
|
||||
std::vector<std::unique_ptr<ChangeRequest>> changes;
|
||||
|
||||
for (auto& kv : layersRemoved) {
|
||||
kv.second->layerRemoved(changes);
|
||||
}
|
||||
for (auto& kv : layersAdded) {
|
||||
kv.second->layerAdded(shaders, context, state, changes);
|
||||
}
|
||||
|
||||
// Layer index, similar but not identical to `layerRenderItems` index in render_impl
|
||||
// int32_t i = static_cast<int32_t>(layerRenderItems.size()) - 1;
|
||||
// for (auto it = layerRenderItems.begin(); it != layerRenderItems.end() && i >= 0; ++it, --i) {
|
||||
auto index = static_cast<int32_t>(layerImpls->size()) - 1;
|
||||
auto index = static_cast<int32_t>(renderLayers.size()) - 1;
|
||||
|
||||
for (auto& impl : *layerImpls) {
|
||||
impl->update(index--, context, state, changes);
|
||||
for (auto& kv : renderLayers) {
|
||||
kv.second->update(index--, shaders, context, state, changes);
|
||||
}
|
||||
|
||||
addChanges(changes);
|
||||
|
||||
@@ -160,10 +160,6 @@ private:
|
||||
std::unordered_map<std::string, std::unique_ptr<RenderLayer>> renderLayers;
|
||||
RenderLight renderLight;
|
||||
|
||||
// Layers added and removed in the last update
|
||||
std::unordered_map<std::string, ImmutableLayer> layersAdded;
|
||||
std::unordered_map<std::string, ImmutableLayer> layersRemoved;
|
||||
|
||||
CrossTileSymbolIndex crossTileSymbolIndex;
|
||||
PlacementController placementController;
|
||||
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
#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>
|
||||
@@ -16,20 +13,6 @@
|
||||
#include <vector>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
class ChangeRequest;
|
||||
class PaintParameters;
|
||||
class PropertyEvaluationParameters;
|
||||
class TransformState;
|
||||
|
||||
using UniqueChangeRequest = std::unique_ptr<ChangeRequest>;
|
||||
using UniqueChangeRequestVec = std::vector<UniqueChangeRequest>;
|
||||
|
||||
namespace gfx {
|
||||
class Context;
|
||||
class ShaderRegistry;
|
||||
} // namespace gfx
|
||||
|
||||
namespace style {
|
||||
|
||||
/**
|
||||
@@ -65,13 +48,6 @@ public:
|
||||
// Populates the given \a fontStack with fonts being used by the layer.
|
||||
virtual void populateFontStack(std::set<FontStack>& fontStack) const;
|
||||
|
||||
/// Generate any changes needed by the layer
|
||||
virtual void update(int32_t /*layerIndex*/, gfx::Context&, const TransformState&, UniqueChangeRequestVec&) const {}
|
||||
|
||||
virtual void layerAdded(gfx::ShaderRegistry&, gfx::Context&, const TransformState&, UniqueChangeRequestVec&) const {
|
||||
}
|
||||
virtual void layerRemoved(UniqueChangeRequestVec&) const {}
|
||||
|
||||
std::string id;
|
||||
std::string source;
|
||||
std::string sourceLayer;
|
||||
@@ -82,27 +58,6 @@ 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.
|
||||
|
||||
@@ -1,183 +1,15 @@
|
||||
#include <mbgl/style/layers/background_layer_impl.hpp>
|
||||
|
||||
#include <mbgl/gfx/shader_registry.hpp>
|
||||
#include <mbgl/gfx/drawable_builder.hpp>
|
||||
#include <mbgl/gl/context.hpp>
|
||||
#include <mbgl/renderer/change_request.hpp>
|
||||
#include <mbgl/renderer/paint_parameters.hpp>
|
||||
// #include <mbgl/shaders/gl/shader_program_gl.hpp>
|
||||
// #include <mbgl/shaders/gl/shader_program_gl.hpp>
|
||||
#include <mbgl/shaders/shader_program_base.hpp>
|
||||
#include <mbgl/style/layer_properties.hpp>
|
||||
#include <mbgl/util/logging.hpp>
|
||||
#include <mbgl/util/tile_cover.hpp>
|
||||
|
||||
#include <unordered_set>
|
||||
|
||||
namespace mbgl {
|
||||
namespace style {
|
||||
|
||||
BackgroundLayer::Impl::Impl(const Impl& other)
|
||||
: Layer::Impl(other),
|
||||
lastColor(other.lastColor),
|
||||
paint(other.paint) {}
|
||||
|
||||
bool BackgroundLayer::Impl::hasLayoutDifference(const Layer::Impl&) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
constexpr auto shaderName = "background_generic";
|
||||
|
||||
void BackgroundLayer::Impl::layerAdded(gfx::ShaderRegistry& shaders,
|
||||
gfx::Context& context,
|
||||
const TransformState&,
|
||||
UniqueChangeRequestVec&) const {
|
||||
{
|
||||
std::unique_lock<std::mutex> guard(mutex);
|
||||
if (!shader) {
|
||||
shader = context.getGenericShader(shaders, shaderName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BackgroundLayer::Impl::layerRemoved(UniqueChangeRequestVec& changes) const {
|
||||
// TODO: This isn't happening on style change, so old tile drawables are being left active
|
||||
|
||||
// Remove everything
|
||||
decltype(tileDrawables) localDrawables;
|
||||
{
|
||||
std::unique_lock<std::mutex> guard(mutex);
|
||||
localDrawables = std::move(tileDrawables);
|
||||
}
|
||||
|
||||
removeDrawables<decltype(localDrawables)::const_iterator>(
|
||||
localDrawables.cbegin(), localDrawables.cend(), changes, [](auto& ii) { return ii->second->getId(); });
|
||||
}
|
||||
|
||||
void BackgroundLayer::Impl::update(const int32_t layerIndex,
|
||||
gfx::Context& context,
|
||||
const TransformState& state,
|
||||
UniqueChangeRequestVec& changes) const {
|
||||
std::unique_lock<std::mutex> guard(mutex);
|
||||
|
||||
if (!unevaluatedProperties || !evaluatedProperties) {
|
||||
// not evaluated yet, we can't update
|
||||
return;
|
||||
}
|
||||
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
|
||||
// ? RenderPass::None
|
||||
// : (!uneval.get<style::BackgroundPattern>().isUndefined()
|
||||
// || eval.get<style::BackgroundOpacity>() < 1.0f
|
||||
// || eval.get<style::BackgroundColor>().a < 1.0f)
|
||||
// ? RenderPass::Translucent
|
||||
// : RenderPass::Opaque | RenderPass::Translucent; // evaluated based on opaquePassCutoff in render()
|
||||
|
||||
// unevaluated.hasTransition();
|
||||
// getCrossfade<BackgroundLayerProperties>(evaluatedProperties).t != 1;
|
||||
|
||||
std::optional<Color> color;
|
||||
if (evaluated.get<BackgroundPattern>().from.empty()) {
|
||||
const auto opacity = evaluated.get<style::BackgroundOpacity>();
|
||||
if (opacity > 0.0f) {
|
||||
color = evaluated.get<BackgroundColor>() * evaluated.get<BackgroundOpacity>();
|
||||
}
|
||||
}
|
||||
|
||||
// If the result is transparent or missing, just remove any existing drawables and stop
|
||||
if (!color) {
|
||||
removeDrawables<decltype(tileDrawables)::const_iterator>(
|
||||
tileDrawables.cbegin(), tileDrawables.cend(), changes, [](auto& ii) { return ii->second->getId(); });
|
||||
tileDrawables.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
const bool colorChange = (color != lastColor);
|
||||
const bool layerChange = (layerIndex != lastLayerIndex);
|
||||
lastColor = color;
|
||||
lastLayerIndex = layerIndex;
|
||||
|
||||
const auto zoom = state.getIntegerZoom();
|
||||
const auto tileCover = util::tileCover(state, zoom);
|
||||
|
||||
// Drawables per overscaled or canonical tile?
|
||||
// const UnwrappedTileID unwrappedTileID = tileID.toUnwrapped();
|
||||
|
||||
// Put the tile cover into a searchable form.
|
||||
// TODO: Likely better to sort and `std::binary_search` the vector.
|
||||
// If it's returned in a well-defined order, we might not even need to sort.
|
||||
const std::unordered_set<OverscaledTileID> newTileIDs(tileCover.begin(), tileCover.end());
|
||||
|
||||
// For each existing tile drawable...
|
||||
for (auto iter = tileDrawables.begin(); iter != tileDrawables.end();) {
|
||||
const auto& drawable = iter->second;
|
||||
|
||||
// Has this tile dropped out of the cover set?
|
||||
if (newTileIDs.find(iter->first) == newTileIDs.end()) {
|
||||
// remove it
|
||||
changes.emplace_back(std::make_unique<RemoveDrawableRequest>(drawable->getId()));
|
||||
// Log::Warning(Event::General, "Removing drawable for " + util::toString(iter->first) + " total " +
|
||||
// std::to_string(stats.tileDrawablesRemoved+1));
|
||||
iter = tileDrawables.erase(iter);
|
||||
++stats.tileDrawablesRemoved;
|
||||
continue;
|
||||
}
|
||||
++iter;
|
||||
|
||||
// If the color evaluated to a new value, update all vertexes of the drawable to the new color
|
||||
if (colorChange) {
|
||||
drawable->resetColor(*color);
|
||||
}
|
||||
if (layerChange) {
|
||||
drawable->setLayerIndex(layerIndex);
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<gfx::DrawableBuilder> builder;
|
||||
|
||||
// For each tile in the cover set, add a tile drawable if one doesn't already exist.
|
||||
// We currently assume only one drawable per tile.
|
||||
for (const auto& tileID : tileCover) {
|
||||
const auto result = tileDrawables.insert(std::make_pair(tileID, gfx::DrawablePtr()));
|
||||
if (!result.second) {
|
||||
// Already present
|
||||
// TODO: Update matrix here or in the tweaker?
|
||||
continue;
|
||||
}
|
||||
|
||||
// We actually need to build things, so set up a builder if we haven't already
|
||||
if (!builder) {
|
||||
builder = context.createDrawableBuilder("background");
|
||||
builder->setShader(shader);
|
||||
builder->addTweaker(context.createDrawableTweaker());
|
||||
builder->setColor(*color);
|
||||
builder->setColorMode(gfx::DrawableBuilder::ColorMode::PerDrawable);
|
||||
builder->setDepthType(gfx::DepthMaskType::ReadWrite);
|
||||
builder->setLayerIndex(layerIndex);
|
||||
}
|
||||
|
||||
// Tile coordinates are fixed...
|
||||
builder->addQuad(0, 0, util::EXTENT, util::EXTENT);
|
||||
|
||||
// ... they're placed with the matrix in the uniforms, which changes with the view
|
||||
builder->setMatrix(/*parameters.matrixForTile(tileID.toUnwrapped())*/ matrix::identity4());
|
||||
|
||||
builder->flush();
|
||||
|
||||
auto drawables = builder->clearDrawables();
|
||||
if (!drawables.empty()) {
|
||||
auto& drawable = drawables[0];
|
||||
drawable->setTileID(tileID);
|
||||
result.first->second = drawable;
|
||||
changes.emplace_back(std::make_unique<AddDrawableRequest>(std::move(drawable)));
|
||||
++stats.tileDrawablesAdded;
|
||||
// Log::Warning(Event::General, "Adding drawable for " + util::toString(tileID) + " total " +
|
||||
// std::to_string(stats.tileDrawablesAdded+1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace style
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -31,26 +31,6 @@ public:
|
||||
bool hasLayoutDifference(const Layer::Impl&) const override;
|
||||
void stringifyLayout(rapidjson::Writer<rapidjson::StringBuffer>&) const override;
|
||||
|
||||
void layerAdded(gfx::ShaderRegistry&, gfx::Context&, const TransformState&, UniqueChangeRequestVec&) const override;
|
||||
void layerRemoved(UniqueChangeRequestVec&) const override;
|
||||
|
||||
/// Generate any changes needed by the layer
|
||||
void update(int32_t layerIndex, gfx::Context&, const TransformState&, UniqueChangeRequestVec&) const override;
|
||||
|
||||
void setUnevaluated(BackgroundPaintProperties::Unevaluated value) { unevaluatedProperties = value; }
|
||||
void setEvaluated(Immutable<BackgroundPaintProperties::PossiblyEvaluated> value) {
|
||||
evaluatedProperties = std::move(value);
|
||||
++stats.propertyEvaluations;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable std::optional<Color> lastColor;
|
||||
mutable int32_t lastLayerIndex = -1;
|
||||
|
||||
std::optional<BackgroundPaintProperties::Unevaluated> unevaluatedProperties;
|
||||
// Latest evaluated properties.
|
||||
std::optional<Immutable<BackgroundPaintProperties::PossiblyEvaluated>> evaluatedProperties;
|
||||
|
||||
public:
|
||||
BackgroundPaintProperties::Transitionable paint;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user