Drawable Renderer (#1570), Move rendering into layer groups

This commit is contained in:
Tim Sylvester
2023-05-12 09:43:48 -07:00
committed by Bart Louwers
parent 57c165e604
commit f51c0820d3
15 changed files with 347 additions and 363 deletions

View File

@@ -914,6 +914,7 @@ if(MLN_WITH_OPENGL)
${PROJECT_SOURCE_DIR}/include/mbgl/gl/drawable_gl.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gl/drawable_gl_builder.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gl/drawable_gl_tweaker.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gl/layer_group_gl.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gl/renderable_resource.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gl/renderer_backend.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gl/uniform_block_gl.hpp
@@ -981,6 +982,7 @@ if(MLN_WITH_OPENGL)
${PROJECT_SOURCE_DIR}/src/mbgl/gl/framebuffer.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/index_buffer_resource.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/index_buffer_resource.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/layer_group_gl.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/object.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/object.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/offscreen_texture.cpp

View File

@@ -0,0 +1,24 @@
#pragma once
#include <mbgl/renderer/layer_group.hpp>
namespace mbgl {
namespace gl {
/**
A layer group for tile-based drawables
*/
class TileLayerGroupGL : public TileLayerGroup {
public:
TileLayerGroupGL(int32_t layerIndex, std::size_t initialCapacity);
~TileLayerGroupGL() override { }
void upload(gfx::Context&, gfx::UploadPass&) override;
void render(RenderOrchestrator&, PaintParameters&) override;
protected:
};
} // namespace gl
} // namespace mbgl

View File

@@ -32,8 +32,6 @@ public:
virtual ~ChangeRequest() = default;
virtual void execute(RenderOrchestrator &) = 0;
protected:
};
/**
@@ -48,50 +46,6 @@ protected:
util::SimpleIdentity id;
};
/**
Add a new drawable to the scene
*/
class AddDrawableRequest : public ChangeRequest {
public:
AddDrawableRequest(gfx::DrawablePtr drawable_)
: drawable(std::move(drawable_)) {}
AddDrawableRequest(AddDrawableRequest &&other)
: drawable(std::move(other.drawable)) {}
void execute(RenderOrchestrator &) override;
protected:
gfx::DrawablePtr drawable;
};
/**
Remove a drawable from the scene
*/
class RemoveDrawableRequest : public RefChangeRequest {
public:
RemoveDrawableRequest(util::SimpleIdentity id_)
: RefChangeRequest(id_) {}
RemoveDrawableRequest(const RemoveDrawableRequest &) = default;
void execute(RenderOrchestrator &) override;
};
/**
Change the color of all vertexes
*/
class ResetColorRequest : public RefChangeRequest {
public:
ResetColorRequest(util::SimpleIdentity id_, Color color)
: RefChangeRequest(id_),
newColor(color) {}
ResetColorRequest(const ResetColorRequest &other) = default;
void execute(RenderOrchestrator &) override;
protected:
Color newColor;
};
/**
Add a new layer group to the scene
*/
@@ -110,13 +64,16 @@ protected:
/**
Remove a layer group from the scene
*/
class RemoveLayerGroupRequest : public RefChangeRequest {
class RemoveLayerGroupRequest : public ChangeRequest {
public:
RemoveLayerGroupRequest(util::SimpleIdentity id_)
: RefChangeRequest(id_) {}
RemoveLayerGroupRequest(int32_t layerIndex_)
: layerIndex(layerIndex_) {}
RemoveLayerGroupRequest(const RemoveLayerGroupRequest &) = default;
void execute(RenderOrchestrator &) override;
protected:
int32_t layerIndex;
};
} // namespace mbgl

View File

@@ -4,6 +4,7 @@
#include <mbgl/tile/tile_id.hpp>
#include <mbgl/util/identity.hpp>
#include <functional>
#include <memory>
#include <vector>
@@ -46,24 +47,37 @@ public:
/// Called at the end of each frame
virtual void postRender(RenderOrchestrator&, PaintParameters&) {}
/// Call the provided function for each drawable in undefined order
virtual void observeDrawables(std::function<void(gfx::Drawable&)>) = 0;
virtual void observeDrawables(std::function<void(const gfx::Drawable&)>) const = 0;
/// Call the provided function for each drawable in undefined order, allowing ownership to be taken.
virtual void observeDrawables(std::function<void(gfx::UniqueDrawable&)>) = 0;
protected:
int32_t layerIndex;
};
/**
A layer group for tile-based drawables
A layer group for tile-based drawables.
*/
class TileLayerGroup : public LayerGroup {
public:
TileLayerGroup(int32_t layerIndex, std::size_t initialCapacity);
~TileLayerGroup() override;
void render(RenderOrchestrator&, PaintParameters&) override;
std::size_t getDrawableCount() const;
const gfx::UniqueDrawable& getDrawable(mbgl::RenderPass, const OverscaledTileID&) const;
gfx::UniqueDrawable removeDrawable(mbgl::RenderPass, const OverscaledTileID&);
bool addDrawable(mbgl::RenderPass, const OverscaledTileID&, gfx::UniqueDrawable&&);
void observeDrawables(std::function<void(gfx::Drawable&)>) override;
void observeDrawables(std::function<void(const gfx::Drawable&)>) const override;
void observeDrawables(std::function<void(gfx::UniqueDrawable&)>) override;
void clearDrawables();
protected:
struct Impl;
std::unique_ptr<Impl> impl;

View File

@@ -17,6 +17,8 @@ namespace mbgl {
class PaintParameters;
class ProgramParameters;
class TileLayerGroup;
using TileLayerGroupPtr = std::shared_ptr<TileLayerGroup>;
namespace gfx {
@@ -113,6 +115,9 @@ public:
/// Get the generic shader with the specified name
virtual gfx::ShaderProgramBasePtr getGenericShader(gfx::ShaderRegistry&, const std::string& name) = 0;
/// Create a layer group implementation
virtual TileLayerGroupPtr createTileLayerGroup(int32_t layerIndex, std::size_t initialCapacity) = 0;
};
} // namespace gfx

View File

@@ -8,6 +8,7 @@
#include <mbgl/gl/drawable_gl_tweaker.hpp>
#include <mbgl/gl/draw_scope_resource.hpp>
#include <mbgl/gl/enum.hpp>
#include <mbgl/gl/layer_group_gl.hpp>
#include <mbgl/gl/renderer_backend.hpp>
#include <mbgl/gl/renderbuffer_resource.hpp>
#include <mbgl/gl/uniform_buffer_gl.hpp>
@@ -506,9 +507,11 @@ gfx::ShaderProgramBasePtr Context::getGenericShader(gfx::ShaderRegistry& shaders
return shaders.get<gl::ShaderProgramGL>(name);
}
void Context::clear(std::optional<mbgl::Color> color,
std::optional<float> depth,
std::optional<int32_t> stencil) {
TileLayerGroupPtr Context::createTileLayerGroup(int32_t layerIndex, std::size_t initialCapacity) {
return std::make_shared<TileLayerGroupGL>(layerIndex, initialCapacity);
}
void Context::clear(std::optional<mbgl::Color> color, std::optional<float> depth, std::optional<int32_t> stencil) {
GLbitfield mask = 0;
if (color) {

View File

@@ -1,5 +1,10 @@
#pragma once
#include <mbgl/gfx/texture.hpp>
#include <mbgl/gfx/draw_mode.hpp>
#include <mbgl/gfx/depth_mode.hpp>
#include <mbgl/gfx/stencil_mode.hpp>
#include <mbgl/gfx/color_mode.hpp>
#include <mbgl/gfx/context.hpp>
#include <mbgl/gl/object.hpp>
#include <mbgl/gl/state.hpp>
@@ -7,17 +12,12 @@
#include <mbgl/gl/framebuffer.hpp>
#include <mbgl/gl/vertex_array.hpp>
#include <mbgl/gl/types.hpp>
#include <mbgl/gfx/texture.hpp>
#include <mbgl/gfx/draw_mode.hpp>
#include <mbgl/gfx/depth_mode.hpp>
#include <mbgl/gfx/stencil_mode.hpp>
#include <mbgl/gfx/color_mode.hpp>
#include <mbgl/platform/gl_functions.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <array>
#include <functional>
#include <vector>
#include <array>
namespace mbgl {
namespace gl {
@@ -110,6 +110,8 @@ public:
gfx::ShaderProgramBasePtr getGenericShader(gfx::ShaderRegistry&, const std::string& name) override;
TileLayerGroupPtr createTileLayerGroup(int32_t layerIndex, std::size_t initialCapacity) override;
private:
RendererBackend& backend;
bool cleanupOnDestruction = true;

View File

@@ -0,0 +1,93 @@
#include <mbgl/gl/layer_group_gl.hpp>
#include <mbgl/gfx/render_pass.hpp>
#include <mbgl/gl/drawable_gl.hpp>
#include <mbgl/gl/upload_pass.hpp>
#include <mbgl/gl/vertex_array.hpp>
#include <mbgl/gl/vertex_attribute_gl.hpp>
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/shaders/gl/shader_program_gl.hpp>
#include <mbgl/util/convert.hpp>
namespace mbgl {
namespace gl {
TileLayerGroupGL::TileLayerGroupGL(int32_t layerIndex, std::size_t initialCapacity)
: TileLayerGroup(layerIndex, initialCapacity) {
}
void TileLayerGroupGL::upload(gfx::Context& context, gfx::UploadPass& uploadPass) {
observeDrawables([&](gfx::Drawable& drawable) {
auto& drawableGL = static_cast<gl::DrawableGL&>(drawable);
const auto& shader = drawable.getShader();
// Generate a vertex array object for the drawable state, if necessary
if (shader && (!drawableGL.getVertexArray().isValid() || drawableGL.getVertexAttributes().isDirty())) {
const auto usage = gfx::BufferUsageType::StaticDraw;
// Build index buffer
const auto& indexData = drawable.getIndexData();
auto indexBuffer = gfx::IndexBuffer{
indexData.size(),
uploadPass.createIndexBufferResource(indexData.data(), indexData.size() * sizeof(indexData[0]), usage)};
// Apply drawable values to shader defaults
const auto& defaults = shader->getVertexAttributes();
const auto& overrides = drawable.getVertexAttributes();
const auto vertexCount = drawable.getVertexCount();
std::unique_ptr<gfx::VertexBufferResource> vertexBuffer;
auto bindings = uploadPass.buildAttributeBindings(vertexCount, defaults, overrides, usage, vertexBuffer);
auto& glContext = static_cast<gl::Context&>(context);
auto vertexArray = glContext.createVertexArray();
vertexArray.bind(glContext, indexBuffer, bindings);
glContext.bindVertexArray = gl::value::BindVertexArray::Default;
drawableGL.setVertexArray(std::move(vertexArray), std::move(vertexBuffer), std::move(indexBuffer));
}
});
}
void TileLayerGroupGL::render(RenderOrchestrator&, PaintParameters& parameters) {
auto& context = parameters.context;
// TODO: figure out how to make drawables participate in the depth system
// Maybe make it a function mapping draw priority to the depth range (always (0,1)?)
parameters.depthRangeSize = 1 - (1 + 2) * parameters.numSublayers * parameters.depthEpsilon;
// TODO: Render tile masks
// for (auto& layer : renderLayers) {
// parameters.renderTileClippingMasks(renderTiles);
//}
observeDrawables([&](gfx::Drawable& drawable) {
if (!drawable.hasRenderPass(parameters.pass)) {
return;
}
if (drawable.hasRenderPass(RenderPass::Opaque) && parameters.currentLayer >= parameters.opaquePassCutoff) {
return;
}
if (!context.setupDraw(parameters, drawable)) {
return;
}
mat4 matrix = drawable.getMatrix();
if (drawable.getTileID()) {
const UnwrappedTileID tileID = drawable.getTileID()->toUnwrapped();
const auto tileMat = parameters.matrixForTile(tileID);
matrix::multiply(matrix, drawable.getMatrix(), tileMat);
matrix = tileMat;
}
gfx::DrawableUBO drawableUBO;
drawableUBO.matrix = util::cast<float>(matrix);
auto uniformBuffer = context.createUniformBuffer(&drawableUBO, sizeof(drawableUBO));
drawable.mutableUniformBuffers().addOrReplace("DrawableUBO", uniformBuffer);
drawable.draw(parameters);
});
}
} // namespace gl
} // namespace mbgl

View File

@@ -3,20 +3,6 @@
namespace mbgl {
void AddDrawableRequest::execute(RenderOrchestrator &orchestrator) {
orchestrator.addDrawable(std::move(drawable));
}
void RemoveDrawableRequest::execute(RenderOrchestrator &orchestrator) {
orchestrator.removeDrawable(id);
}
void ResetColorRequest::execute(RenderOrchestrator &orchestrator) {
if (auto &drawable = orchestrator.getDrawable(id)) {
drawable->resetColor(newColor);
}
}
AddLayerGroupRequest::AddLayerGroupRequest(LayerGroupPtr layerGroup_, bool canReplace)
: layerGroup(std::move(layerGroup_)),
replace(canReplace) {}
@@ -29,7 +15,7 @@ void AddLayerGroupRequest::execute(RenderOrchestrator &orchestrator) {
}
void RemoveLayerGroupRequest::execute(RenderOrchestrator &orchestrator) {
orchestrator.removeLayerGroup(id);
orchestrator.removeLayerGroup(layerIndex);
}
} // namespace mbgl

View File

@@ -38,6 +38,10 @@ TileLayerGroup::TileLayerGroup(int32_t layerIndex_, std::size_t initialCapacity)
TileLayerGroup::~TileLayerGroup() {}
std::size_t TileLayerGroup::getDrawableCount() const {
return impl->tileDrawables.size();
}
static const gfx::UniqueDrawable no_tile;
const gfx::UniqueDrawable& TileLayerGroup::getDrawable(mbgl::RenderPass pass, const OverscaledTileID& id) const {
@@ -59,14 +63,56 @@ bool TileLayerGroup::addDrawable(mbgl::RenderPass pass, const OverscaledTileID&
const auto result = impl->tileDrawables.insert(
std::make_pair(TileLayerGroupTileKey{pass, id}, gfx::UniqueDrawable()));
if (result.second) {
return false;
} else {
// New item inserted, move the drawable into it
result.first->second = std::move(drawable);
return true;
} else {
// Not inserted
return false;
}
}
void TileLayerGroup::render([[maybe_unused]] RenderOrchestrator& orchestrator,
[[maybe_unused]] PaintParameters& parameters) {}
void TileLayerGroup::observeDrawables(std::function<void(gfx::Drawable&)> f) {
for (auto& pair : impl->tileDrawables) {
if (pair.second) {
f(*pair.second);
} else {
printf("");
}
}
}
void TileLayerGroup::observeDrawables(std::function<void(const gfx::Drawable&)> f) const {
for (const auto& pair : impl->tileDrawables) {
if (pair.second) {
f(*pair.second);
} else {
printf("");
}
}
}
void TileLayerGroup::observeDrawables(std::function<void(gfx::UniqueDrawable&)> f) {
for (auto i = impl->tileDrawables.begin(); i != impl->tileDrawables.end(); ) {
auto& drawable = i->second;
if (drawable) {
f(drawable);
if (drawable) {
// Not removed, keep going
++i;
} else {
// Removed, take it out of the map
i = impl->tileDrawables.erase(i);
}
} else {
// it was already null, this isn't supposed to happen
i = impl->tileDrawables.erase(i);
}
}
}
void TileLayerGroup::clearDrawables() {
impl->tileDrawables.clear();
}
} // namespace mbgl

View File

@@ -198,17 +198,11 @@ void RenderBackgroundLayer::prepare(const LayerPrepareParameters& params) {
}
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);
if (tileLayerGroup) {
changes.emplace_back(std::make_unique<RemoveLayerGroupRequest>(tileLayerGroup->getLayerIndex()));
tileLayerGroup.reset();
}
removeDrawables<decltype(localDrawables)::const_iterator>(
localDrawables.cbegin(), localDrawables.cend(), changes, [](auto& ii) { return ii->second->getId(); });
}
void RenderBackgroundLayer::update(const int32_t layerIndex,
@@ -222,8 +216,22 @@ void RenderBackgroundLayer::update(const int32_t layerIndex,
shader = context.getGenericShader(shaders, "BackgroundDrawable");
}
changes.emplace_back(
std::make_unique<AddLayerGroupRequest>(std::make_unique<TileLayerGroup>(layerIndex, 10), /*replace*/ true));
const auto removeAll = [&](){
if (tileLayerGroup) {
stats.tileDrawablesRemoved += tileLayerGroup->getDrawableCount();
tileLayerGroup->clearDrawables();
}
};
const auto zoom = state.getIntegerZoom();
const auto tileCover = util::tileCover(state, zoom);
// renderTiles is always empty, we use tileCover instead
//if (!renderTiles || renderTiles->empty()) {
if (tileCover.empty()) {
removeAll();
return;
}
const auto& evaluated = getEvaluated<BackgroundLayerProperties>(evaluatedProperties);
@@ -240,10 +248,8 @@ void RenderBackgroundLayer::update(const int32_t layerIndex,
// getCrossfade<BackgroundLayerProperties>(evaluatedProperties).t != 1;
// If the result is transparent or missing, just remove any existing drawables and stop
if (!evaluated.get<BackgroundPattern>().from.empty() || evaluated.get<style::BackgroundOpacity>() <= 0.0f) {
removeDrawables<decltype(tileDrawables)::const_iterator>(
tileDrawables.cbegin(), tileDrawables.cend(), changes, [](auto& ii) { return ii->second->getId(); });
tileDrawables.clear();
if (drawPasses == RenderPass::None) {
removeAll();
return;
}
@@ -255,11 +261,13 @@ void RenderBackgroundLayer::update(const int32_t layerIndex,
evaluatedPropertiesChange = false;
}
const bool layerChange = (layerIndex != lastLayerIndex);
lastLayerIndex = layerIndex;
const auto zoom = state.getIntegerZoom();
const auto tileCover = util::tileCover(state, zoom);
if (!tileLayerGroup) {
tileLayerGroup = context.createTileLayerGroup(layerIndex, /*initialCapacity=*/64);
if (!tileLayerGroup) {
return;
}
changes.emplace_back(std::make_unique<AddLayerGroupRequest>(tileLayerGroup, /*canReplace=*/true));
}
// Drawables per overscaled or canonical tile?
// const UnwrappedTileID unwrappedTileID = tileID.toUnwrapped();
@@ -269,70 +277,68 @@ void RenderBackgroundLayer::update(const int32_t layerIndex,
// 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 (layerChange) {
drawable->setLayerIndex(layerIndex);
}
// TODO: does render pass change dynamically?
}
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?
result.first->second->mutableUniformBuffers().addOrReplace("BackgroundLayerUBO", uniformBuffer);
continue;
}
for (const auto renderPass : {RenderPass::Opaque, RenderPass::Translucent}) {
tileLayerGroup->observeDrawables([&](gfx::UniqueDrawable& drawable) {
// Has this tile dropped out of the cover set?
const auto tileID = drawable->getTileID();
if (tileID && newTileIDs.find(*tileID) == newTileIDs.end()) {
drawable.reset();
++stats.tileDrawablesRemoved;
return;
}
});
// We actually need to build things, so set up a builder if we haven't already
if (!builder) {
builder = context.createDrawableBuilder("background");
builder->setRenderPass(drawPasses);
builder->setShader(shader);
builder->addTweaker(context.createDrawableTweaker());
builder->setColorMode(gfx::DrawableBuilder::ColorMode::PerDrawable);
builder->setDepthType(gfx::DepthMaskType::ReadWrite);
builder->setLayerIndex(layerIndex);
}
// 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) {
auto& tileDrawable = tileLayerGroup->getDrawable(renderPass, tileID);
// Tile coordinates are fixed...
builder->addQuad(0, 0, util::EXTENT, util::EXTENT);
if (!(renderPass & drawPasses)) {
// Not in this pass
if (tileDrawable) {
tileLayerGroup->removeDrawable(renderPass, tileID);
++stats.tileDrawablesRemoved;
}
continue;
}
// ... 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 newDrawables = builder->clearDrawables();
if (!newDrawables.empty()) {
auto& drawable = newDrawables[0];
drawable->setTileID(tileID);
drawable->mutableUniformBuffers().addOrReplace("BackgroundLayerUBO", uniformBuffer);
result.first->second = std::move(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 (tileDrawable) {
// Already created, update it.
tileDrawable->mutableUniformBuffers().addOrReplace("BackgroundLayerUBO", uniformBuffer);
continue;
}
// We actually need to build things, so set up a builder if we haven't already
if (!builder) {
builder = context.createDrawableBuilder("background");
builder->setRenderPass(drawPasses);
builder->setShader(shader);
builder->addTweaker(context.createDrawableTweaker());
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 newDrawables = builder->clearDrawables();
if (!newDrawables.empty()) {
auto& drawable = newDrawables[0];
drawable->setTileID(tileID);
drawable->mutableUniformBuffers().addOrReplace("BackgroundLayerUBO", uniformBuffer);
tileLayerGroup->addDrawable(renderPass, tileID, std::move(drawable));
++stats.tileDrawablesAdded;
// Log::Warning(Event::General, "Adding drawable for " + util::toString(tileID) + " total " +
// std::to_string(stats.tileDrawablesAdded+1));
}
}
}
}

View File

@@ -263,14 +263,10 @@ bool RenderFillLayer::queryIntersectsFeature(const GeometryCoordinates& queryGeo
void RenderFillLayer::layerRemoved(UniqueChangeRequestVec& changes) {
// Remove everything
decltype(tileDrawables) localDrawables;
{
std::unique_lock<std::mutex> guard(mutex);
localDrawables = std::move(tileDrawables);
if (tileLayerGroup) {
changes.emplace_back(std::make_unique<RemoveLayerGroupRequest>(tileLayerGroup->getLayerIndex()));
tileLayerGroup.reset();
}
removeDrawables<decltype(localDrawables)::const_iterator>(
localDrawables.cbegin(), localDrawables.cend(), changes, [](auto& ii) { return ii->second->getId(); });
}
void RenderFillLayer::update(const int32_t layerIndex,
@@ -284,111 +280,45 @@ void RenderFillLayer::update(const int32_t layerIndex,
shader = context.getGenericShader(shaders, "FillDrawable");
}
// 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);
// }
// }
if (!tileLayerGroup) {
tileLayerGroup = std::make_shared<TileLayerGroup>(layerIndex, /*initialCapacity=*/64);
changes.emplace_back(std::make_unique<AddLayerGroupRequest>(tileLayerGroup, /*canReplace=*/true));
}
if (!renderTiles || renderTiles->empty()) {
if (!tileDrawables.empty()) {
removeDrawables<decltype(tileDrawables)::const_iterator>(
tileDrawables.cbegin(), tileDrawables.cend(), changes, [](auto& ii) { return ii->second->getId(); });
tileDrawables.clear();
if (tileLayerGroup) {
stats.tileDrawablesRemoved += tileLayerGroup->getDrawableCount();
tileLayerGroup->clearDrawables();
}
return;
}
// Set up a layer group
if (!tileLayerGroup) {
tileLayerGroup = context.createTileLayerGroup(layerIndex, /*initialCapacity=*/64);
if (!tileLayerGroup) {
return;
}
changes.emplace_back(std::make_unique<AddLayerGroupRequest>(tileLayerGroup, /*canReplace=*/true));
}
std::unordered_set<OverscaledTileID> newTileIDs(renderTiles->size());
std::transform(renderTiles->begin(),
renderTiles->end(),
std::inserter(newTileIDs, newTileIDs.begin()),
[](const auto& renderTile) -> OverscaledTileID { return renderTile.get().getOverscaledTileID(); });
// 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
if (drawable) {
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;
// update...
}
std::unique_ptr<gfx::DrawableBuilder> builder;
std::vector<gfx::DrawablePtr> newTiles;
for (const auto renderPass : {RenderPass::Opaque, RenderPass::Translucent}) {
tileLayerGroup->observeDrawables([&](gfx::UniqueDrawable& drawable) {
// Has this tile dropped out of the cover set?
const auto tileID = drawable->getTileID();
if (tileID && newTileIDs.find(*tileID) == newTileIDs.end()) {
// remove it
drawable.reset();
++stats.tileDrawablesRemoved;
}
});
if (unevaluated.get<FillPattern>().isUndefined()) {
// parameters.renderTileClippingMasks(renderTiles);
// parameters.renderTileClippingMasks(renderTiles);
for (const RenderTile& tile : *renderTiles) {
const auto& tileID = tile.getOverscaledTileID();
@@ -396,7 +326,6 @@ void RenderFillLayer::update(const int32_t layerIndex,
const auto removeTile = [&]() {
if (tileDrawable) {
changes.emplace_back(std::make_unique<RemoveDrawableRequest>(tileDrawable->getId()));
tileLayerGroup->removeDrawable(renderPass, tileID);
++stats.tileDrawablesRemoved;
}
@@ -496,7 +425,7 @@ void RenderFillLayer::update(const int32_t layerIndex,
drawable->setTileID(tileID);
// Track it.
tileLayerGroup->addDrawable(renderPass, tileID, std::move(drawable));
//tileLayerGroup->addDrawable(renderPass, tileID, std::move(drawable));
++stats.tileDrawablesAdded;
// Log::Warning(Event::General, "Adding drawable for " + util::toString(tileID) + " total " +
// std::to_string(stats.tileDrawablesAdded+1));

View File

@@ -54,8 +54,6 @@ private:
std::shared_ptr<FillPatternProgram> fillPatternProgram;
std::shared_ptr<FillOutlineProgram> fillOutlineProgram;
std::shared_ptr<FillOutlinePatternProgram> fillOutlinePatternProgram;
TileLayerGroupPtr tileLayerGroup;
};
} // namespace mbgl

View File

@@ -22,10 +22,12 @@ class PaintParameters;
class PatternAtlas;
class RenderTile;
class SymbolBucket;
class TileLayerGroup;
class TransformState;
class TransitionParameters;
class UploadParameters;
using TileLayerGroupPtr = std::shared_ptr<TileLayerGroup>;
using UniqueChangeRequest = std::unique_ptr<ChangeRequest>;
using UniqueChangeRequestVec = std::vector<UniqueChangeRequest>;
@@ -154,17 +156,6 @@ 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;
@@ -175,12 +166,10 @@ protected:
LayerPlacementData placementData;
gfx::DrawablePtr drawables;
TileLayerGroupPtr tileLayerGroup;
std::mutex mutex;
gfx::ShaderProgramBasePtr shader;
std::unordered_map<OverscaledTileID, gfx::DrawablePtr> tileDrawables;
int32_t lastLayerIndex = -1;
bool evaluatedPropertiesChange = false;
gfx::UniformBufferPtr uniformBuffer = nullptr;

View File

@@ -128,40 +128,6 @@ void Renderer::Impl::render(const RenderTree& renderTree) {
renderTree.getLineAtlas().upload(*uploadPass);
renderTree.getPatternAtlas().upload(*uploadPass);
for (const auto& drawPtr : drawables) {
auto& drawable = *drawPtr;
auto& drawableGL = static_cast<gl::DrawableGL&>(drawable);
auto& shader = drawable.getShader();
// Generate a vertex array object for the drawable state, if necessary
if (shader && (!drawableGL.getVertexArray().isValid() || drawableGL.getVertexAttributes().isDirty())) {
const auto usage = gfx::BufferUsageType::StaticDraw;
// Build index buffer
const auto& indexData = drawable.getIndexData();
auto indexBuffer = gfx::IndexBuffer{
indexData.size(),
uploadPass->createIndexBufferResource(
indexData.data(), indexData.size() * sizeof(indexData[0]), usage)};
// Apply drawable values to shader defaults
const auto& defaults = shader->getVertexAttributes();
const auto& overrides = drawable.getVertexAttributes();
const auto vertexCount = drawable.getVertexCount();
std::unique_ptr<gfx::VertexBufferResource> vertexBuffer;
auto bindings = uploadPass->buildAttributeBindings(
vertexCount, defaults, overrides, usage, vertexBuffer);
auto& glContext = static_cast<gl::Context&>(context);
auto vertexArray = glContext.createVertexArray();
vertexArray.bind(glContext, indexBuffer, bindings);
glContext.bindVertexArray = gl::value::BindVertexArray::Default;
drawableGL.setVertexArray(std::move(vertexArray), std::move(vertexBuffer), std::move(indexBuffer));
}
}
// Give the layers a chance to upload
orchestrator.observeLayerGroups([&](LayerGroup& layerGroup) { layerGroup.upload(context, *uploadPass); });
}
@@ -210,55 +176,16 @@ void Renderer::Impl::render(const RenderTree& renderTree) {
"main buffer", {parameters.backend.getDefaultRenderable(), color, 1.0f, 0});
}
// Draw Drawables
const auto drawDrawables = [&](const mbgl::RenderPass renderPass) {
// TODO: figure out how to make drawables participate in the depth system
// Maybe make it a function mapping draw priority to the depth range (always (0,1)?)
parameters.depthRangeSize = 1 - (1 + 2) * parameters.numSublayers * parameters.depthEpsilon;
const auto debugGroup(parameters.renderPass->createDebugGroup("drawables"));
{
parameters.pass = RenderPass::Opaque;
parameters.currentLayer = 0;
parameters.opaquePassCutoff = 1;
// draw layer groups, opaque pass
const auto debugGroup(parameters.renderPass->createDebugGroup("drawables-opaque"));
// TODO: Render tile masks
// for (auto& layer : renderLayers) {
// parameters.renderTileClippingMasks(renderTiles);
//}
for (const auto& drawPtr : drawables) {
auto& drawable = *drawPtr;
if (!drawable.hasRenderPass(renderPass)) {
continue;
}
if (drawable.hasRenderPass(RenderPass::Opaque) && parameters.currentLayer >= parameters.opaquePassCutoff) {
continue;
}
if (!context.setupDraw(parameters, drawable)) {
continue;
}
mat4 matrix = drawable.getMatrix();
if (drawable.getTileID()) {
const UnwrappedTileID tileID = drawable.getTileID()->toUnwrapped();
const auto tileMat = parameters.matrixForTile(tileID);
matrix::multiply(matrix, drawable.getMatrix(), tileMat);
matrix = tileMat;
}
gfx::DrawableUBO drawableUBO;
drawableUBO.matrix = util::cast<float>(matrix);
auto uniformBuffer = context.createUniformBuffer(&drawableUBO, sizeof(drawableUBO));
drawable.mutableUniformBuffers().addOrReplace("DrawableUBO", uniformBuffer);
drawable.draw(parameters);
}
};
// draw layer groups, opaque pass
orchestrator.observeLayerGroups([&](LayerGroup& layerGroup) { layerGroup.render(orchestrator, parameters); });
parameters.pass = RenderPass::Opaque;
parameters.currentLayer = 0;
parameters.opaquePassCutoff = 1;
drawDrawables(parameters.pass);
orchestrator.observeLayerGroups([&](LayerGroup& layerGroup) { layerGroup.render(orchestrator, parameters); });
}
// Actually render the layers
parameters.opaquePassCutoff = renderTreeParameters.opaquePassCutOff;
@@ -283,15 +210,18 @@ void Renderer::Impl::render(const RenderTree& renderTree) {
}
}
parameters.pass = RenderPass::Translucent;
parameters.currentLayer = 1;
parameters.opaquePassCutoff = 1;
drawDrawables(parameters.pass);
// draw layer groups, translucent pass
orchestrator.observeLayerGroups([&](LayerGroup& layerGroup) { layerGroup.render(orchestrator, parameters); });
parameters.opaquePassCutoff = renderTreeParameters.opaquePassCutOff;
{
parameters.pass = RenderPass::Translucent;
parameters.currentLayer = 1;
parameters.opaquePassCutoff = 1;
// draw layer groups, translucent pass
const auto debugGroup(parameters.renderPass->createDebugGroup("drawables-translucent"));
orchestrator.observeLayerGroups([&](LayerGroup& layerGroup) { layerGroup.render(orchestrator, parameters); });
parameters.opaquePassCutoff = renderTreeParameters.opaquePassCutOff;
}
// - TRANSLUCENT PASS --------------------------------------------------------------------------
// Make a second pass, rendering translucent objects. This time, we render bottom-to-top.