Custom drawable layer v2 (#1754)

This commit is contained in:
Stefan Karschti
2023-10-26 21:36:50 +03:00
committed by GitHub
parent 619408d09d
commit 40e283cdbb
26 changed files with 672 additions and 227 deletions

View File

@@ -70,7 +70,7 @@ void DrawableBuilder::Impl::setupForPolylines(gfx::DrawableBuilder& builder) {
gfx::VertexAttributeArray attrs;
using VertexVector = gfx::VertexVector<LineLayoutVertex>;
std::shared_ptr<VertexVector> verts = std::make_shared<VertexVector>(polylineVertices);
std::shared_ptr<VertexVector> verts = std::make_shared<VertexVector>(std::move(polylineVertices));
if (const auto& attr = attrs.add(idVertexAttribName)) {
attr->setSharedRawData(verts,

View File

@@ -65,6 +65,16 @@ public:
return std::max(rawVerticesCount, std::max(vertices.elements(), polylineVertices.elements()));
}
void clear() {
vertices.clear();
rawVertices.clear();
rawVerticesCount = 0;
polylineVertices.clear();
polylineIndexes.clear();
buildIndexes.clear();
segments.clear();
}
private:
LineLayoutVertex layoutVertex(
Point<int16_t> p, Point<double> e, bool round, bool up, int8_t dir, int32_t linesofar = 0);

View File

@@ -227,7 +227,7 @@ void PolylineGenerator<PLV, PS>::generate(const GeometryCoordinates& coordinates
false,
startVertex,
triangleStore,
options.lineDistances);
options.clipDistances);
prevCoordinate = newPrevVertex;
}
}
@@ -279,7 +279,7 @@ void PolylineGenerator<PLV, PS>::generate(const GeometryCoordinates& coordinates
false,
startVertex,
triangleStore,
options.lineDistances);
options.clipDistances);
} else if (middleVertex && currentJoin == style::LineJoinType::FlipBevel) {
// miter is too big, flip the direction to make a beveled join
@@ -302,7 +302,7 @@ void PolylineGenerator<PLV, PS>::generate(const GeometryCoordinates& coordinates
false,
startVertex,
triangleStore,
options.lineDistances);
options.clipDistances);
addCurrentVertex(*currentCoordinate,
distance,
@@ -312,7 +312,7 @@ void PolylineGenerator<PLV, PS>::generate(const GeometryCoordinates& coordinates
false,
startVertex,
triangleStore,
options.lineDistances);
options.clipDistances);
} else if (middleVertex &&
(currentJoin == style::LineJoinType::Bevel || currentJoin == style::LineJoinType::FakeRound)) {
const bool lineTurnsLeft = (prevNormal->x * nextNormal->y - prevNormal->y * nextNormal->x) > 0;
@@ -338,7 +338,7 @@ void PolylineGenerator<PLV, PS>::generate(const GeometryCoordinates& coordinates
false,
startVertex,
triangleStore,
options.lineDistances);
options.clipDistances);
}
if (currentJoin == style::LineJoinType::FakeRound) {
@@ -369,7 +369,7 @@ void PolylineGenerator<PLV, PS>::generate(const GeometryCoordinates& coordinates
lineTurnsLeft,
startVertex,
triangleStore,
options.lineDistances);
options.clipDistances);
}
}
@@ -383,7 +383,7 @@ void PolylineGenerator<PLV, PS>::generate(const GeometryCoordinates& coordinates
false,
startVertex,
triangleStore,
options.lineDistances);
options.clipDistances);
}
} else if (!middleVertex && currentCap == style::LineCapType::Butt) {
@@ -397,7 +397,7 @@ void PolylineGenerator<PLV, PS>::generate(const GeometryCoordinates& coordinates
false,
startVertex,
triangleStore,
options.lineDistances);
options.clipDistances);
}
// Start next segment with a butt
@@ -410,7 +410,7 @@ void PolylineGenerator<PLV, PS>::generate(const GeometryCoordinates& coordinates
false,
startVertex,
triangleStore,
options.lineDistances);
options.clipDistances);
}
} else if (!middleVertex && currentCap == style::LineCapType::Square) {
@@ -424,7 +424,7 @@ void PolylineGenerator<PLV, PS>::generate(const GeometryCoordinates& coordinates
false,
startVertex,
triangleStore,
options.lineDistances);
options.clipDistances);
// The segment is done. Unset vertices to disconnect segments.
e1 = e2 = -1;
@@ -440,7 +440,7 @@ void PolylineGenerator<PLV, PS>::generate(const GeometryCoordinates& coordinates
false,
startVertex,
triangleStore,
options.lineDistances);
options.clipDistances);
}
} else if (middleVertex ? currentJoin == style::LineJoinType::Round : currentCap == style::LineCapType::Round) {
@@ -454,7 +454,7 @@ void PolylineGenerator<PLV, PS>::generate(const GeometryCoordinates& coordinates
false,
startVertex,
triangleStore,
options.lineDistances);
options.clipDistances);
// Add round cap or linejoin at end of segment
addCurrentVertex(*currentCoordinate,
@@ -465,7 +465,7 @@ void PolylineGenerator<PLV, PS>::generate(const GeometryCoordinates& coordinates
true,
startVertex,
triangleStore,
options.lineDistances);
options.clipDistances);
// The segment is done. Unset vertices to disconnect segments.
e1 = e2 = -1;
@@ -482,7 +482,7 @@ void PolylineGenerator<PLV, PS>::generate(const GeometryCoordinates& coordinates
true,
startVertex,
triangleStore,
options.lineDistances);
options.clipDistances);
addCurrentVertex(*currentCoordinate,
distance,
@@ -492,7 +492,7 @@ void PolylineGenerator<PLV, PS>::generate(const GeometryCoordinates& coordinates
false,
startVertex,
triangleStore,
options.lineDistances);
options.clipDistances);
}
}
@@ -512,7 +512,7 @@ void PolylineGenerator<PLV, PS>::generate(const GeometryCoordinates& coordinates
false,
startVertex,
triangleStore,
options.lineDistances);
options.clipDistances);
currentCoordinate = newCurrentVertex;
}
}

View File

@@ -39,11 +39,10 @@ void DrawableGLBuilder::init() {
if (!impl->sharedIndexes && !impl->buildIndexes.empty()) {
impl->sharedIndexes = std::make_shared<gfx::IndexVectorBase>(std::move(impl->buildIndexes));
}
assert(impl->sharedIndexes && impl->sharedIndexes->elements());
drawableGL.setIndexData(std::move(impl->sharedIndexes), std::move(impl->segments));
impl->buildIndexes.clear();
impl->segments.clear();
impl->vertices.clear();
impl->clear();
textures.clear();
}

View File

@@ -27,6 +27,7 @@ void DrawableBuilder::init() {
if (impl->rawVerticesCount) {
auto raw = impl->rawVertices;
drawable.setVertices(std::move(raw), impl->rawVerticesCount, impl->rawVerticesType);
impl->rawVerticesCount = 0;
} else {
const auto& verts = impl->vertices.vector();
constexpr auto vertSize = sizeof(std::remove_reference<decltype(verts)>::type::value_type);
@@ -41,9 +42,7 @@ void DrawableBuilder::init() {
assert(impl->sharedIndexes && impl->sharedIndexes->elements());
drawable.setIndexData(std::move(impl->sharedIndexes), std::move(impl->segments));
impl->buildIndexes.clear();
impl->segments.clear();
impl->vertices.clear();
impl->clear();
textures.clear();
}

View File

@@ -99,7 +99,7 @@ void LineBucket::addGeometry(const GeometryCoordinates& coordinates,
total_length += util::dist<double>(coordinates[i], coordinates[i + 1]);
}
options.lineDistances = gfx::PolylineGeneratorDistances{
options.clipDistances = gfx::PolylineGeneratorDistances{
*numericValue<double>(clip_start_it->second), *numericValue<double>(clip_end_it->second), total_length};
}

View File

@@ -52,10 +52,6 @@ bool RenderCustomDrawableLayer::hasCrossfade() const {
return false;
}
void RenderCustomDrawableLayer::markContextDestroyed() {
contextDestroyed = true;
}
void RenderCustomDrawableLayer::prepare(const LayerPrepareParameters&) {}
#if MLN_LEGACY_RENDERER
@@ -84,9 +80,12 @@ void RenderCustomDrawableLayer::update(gfx::ShaderRegistry& shaders,
// delegate the call to the custom layer
if (host) {
host->update(*this, shaders, context, state, updateParameters, renderTree, changes);
CustomDrawableLayerHost::Interface interface(
*this, layerGroup, shaders, context, state, updateParameters, renderTree, changes);
host->update(interface);
}
}
#endif
} // namespace mbgl

View File

@@ -25,14 +25,12 @@ private:
void evaluate(const PropertyEvaluationParameters&) override;
bool hasTransition() const override;
bool hasCrossfade() const override;
void markContextDestroyed() override;
void prepare(const LayerPrepareParameters&) override;
#if MLN_LEGACY_RENDERER
void render(PaintParameters&) override;
#endif
bool contextDestroyed = false;
std::shared_ptr<style::CustomDrawableLayerHost> host;
};

View File

@@ -1,7 +1,20 @@
#include <mbgl/style/layers/custom_drawable_layer.hpp>
#include <mbgl/style/layers/custom_drawable_layer_impl.hpp>
#include <mbgl/renderer/layers/render_custom_drawable_layer.hpp>
#include <mbgl/style/layer_observer.hpp>
#include <mbgl/gfx/context.hpp>
#include <mbgl/renderer/change_request.hpp>
#include <mbgl/renderer/layer_group.hpp>
#include <mbgl/gfx/cull_face_mode.hpp>
#include <mbgl/shaders/shader_program_base.hpp>
#include <mbgl/renderer/layer_tweaker.hpp>
#include <mbgl/gfx/drawable.hpp>
#include <mbgl/gfx/drawable_tweaker.hpp>
#include <mbgl/shaders/line_layer_ubo.hpp>
#include <mbgl/util/string_indexer.hpp>
#include <mbgl/util/convert.hpp>
#include <mbgl/util/geometry.hpp>
namespace mbgl {
namespace style {
@@ -53,5 +66,185 @@ const LayerTypeInfo* CustomDrawableLayer::Impl::staticTypeInfo() noexcept {
return &typeInfoCustomDrawable;
}
// CustomDrawableLayerHost::Interface
class LineDrawableTweaker : public gfx::DrawableTweaker {
public:
LineDrawableTweaker(const shaders::LinePropertiesUBO& properties)
: linePropertiesUBO(properties) {}
~LineDrawableTweaker() override = default;
void init(gfx::Drawable&) override{};
void execute(gfx::Drawable& drawable, const PaintParameters& parameters) override {
if (!drawable.getTileID().has_value()) {
return;
}
const UnwrappedTileID tileID = drawable.getTileID()->toUnwrapped();
const auto zoom = parameters.state.getZoom();
mat4 tileMatrix;
parameters.state.matrixFor(/*out*/ tileMatrix, tileID);
const auto matrix = LayerTweaker::getTileMatrix(
tileID, parameters, {{0, 0}}, style::TranslateAnchorType::Viewport, false, false, false);
static const StringIdentity idLineUBOName = stringIndexer().get("LineUBO");
const shaders::LineUBO lineUBO{
/*matrix = */ util::cast<float>(matrix),
/*units_to_pixels = */ {1.0f / parameters.pixelsToGLUnits[0], 1.0f / parameters.pixelsToGLUnits[1]},
/*ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, zoom),
/*device_pixel_ratio = */ parameters.pixelRatio};
static const StringIdentity idLinePropertiesUBOName = stringIndexer().get("LinePropertiesUBO");
static const StringIdentity idLineInterpolationUBOName = stringIndexer().get("LineInterpolationUBO");
const shaders::LineInterpolationUBO lineInterpolationUBO{/*color_t =*/0.f,
/*blur_t =*/0.f,
/*opacity_t =*/0.f,
/*gapwidth_t =*/0.f,
/*offset_t =*/0.f,
/*width_t =*/0.f,
0,
0};
auto& uniforms = drawable.mutableUniformBuffers();
uniforms.createOrUpdate(idLineUBOName, &lineUBO, parameters.context);
uniforms.createOrUpdate(idLinePropertiesUBOName, &linePropertiesUBO, parameters.context);
uniforms.createOrUpdate(idLineInterpolationUBOName, &lineInterpolationUBO, parameters.context);
#if MLN_RENDER_BACKEND_METAL
static const StringIdentity idExpressionInputsUBOName = stringIndexer().get("ExpressionInputsUBO");
const auto expressionUBO = LayerTweaker::buildExpressionUBO(zoom, parameters.frameCount);
uniforms.createOrUpdate(idExpressionInputsUBOName, &expressionUBO, parameters.context);
static const StringIdentity idLinePermutationUBOName = stringIndexer().get("LinePermutationUBO");
const shaders::LinePermutationUBO permutationUBO = {
/* .color = */ {/*.source=*/shaders::AttributeSource::Constant, /*.expression=*/{}},
/* .blur = */ {/*.source=*/shaders::AttributeSource::Constant, /*.expression=*/{}},
/* .opacity = */ {/*.source=*/shaders::AttributeSource::Constant, /*.expression=*/{}},
/* .gapwidth = */ {/*.source=*/shaders::AttributeSource::Constant, /*.expression=*/{}},
/* .offset = */ {/*.source=*/shaders::AttributeSource::Constant, /*.expression=*/{}},
/* .width = */ {/*.source=*/shaders::AttributeSource::Constant, /*.expression=*/{}},
/* .floorwidth = */ {/*.source=*/shaders::AttributeSource::Constant, /*.expression=*/{}},
/* .pattern_from = */ {/*.source=*/shaders::AttributeSource::Constant, /*.expression=*/{}},
/* .pattern_to = */ {/*.source=*/shaders::AttributeSource::Constant, /*.expression=*/{}},
/* .overdrawInspector = */ false,
/* .pad = */ 0,
0,
0,
0};
uniforms.createOrUpdate(idLinePermutationUBOName, &permutationUBO, parameters.context);
#endif // MLN_RENDER_BACKEND_METAL
};
private:
shaders::LinePropertiesUBO linePropertiesUBO;
};
CustomDrawableLayerHost::Interface::Interface(RenderLayer& layer_,
LayerGroupBasePtr& layerGroup_,
gfx::ShaderRegistry& shaders_,
gfx::Context& context_,
const TransformState& state_,
const std::shared_ptr<UpdateParameters>& updateParameters_,
const RenderTree& renderTree_,
UniqueChangeRequestVec& changes_)
: layer(layer_),
layerGroup(layerGroup_),
shaders(shaders_),
context(context_),
state(state_),
updateParameters(updateParameters_),
renderTree(renderTree_),
changes(changes_) {
// ensure we have a default layer group set up
if (!layerGroup) {
if (auto aLayerGroup = context.createTileLayerGroup(
/*layerIndex*/ layer.getLayerIndex(), /*initialCapacity=*/64, layer.getID())) {
changes.emplace_back(std::make_unique<AddLayerGroupRequest>(aLayerGroup));
layerGroup = std::move(aLayerGroup);
}
}
}
std::size_t CustomDrawableLayerHost::Interface::getDrawableCount() const {
return layerGroup->getDrawableCount();
}
void CustomDrawableLayerHost::Interface::setTileID(OverscaledTileID tileID_) {
tileID = tileID_;
}
void CustomDrawableLayerHost::Interface::setLineOptions(const LineOptions& options) {
finish();
lineOptions = options;
}
void CustomDrawableLayerHost::Interface::addPolyline(const GeometryCoordinates& coordinates) {
if (!builder) {
builder = createBuilder("thick-lines", lineShaderDefault());
} else {
// TODO: check builder
}
builder->addPolyline(coordinates, lineOptions.geometry);
}
void CustomDrawableLayerHost::Interface::finish() {
if (builder && builder->curVertexCount()) {
// create tweaker
const shaders::LinePropertiesUBO linePropertiesUBO{lineOptions.color,
lineOptions.blur,
lineOptions.opacity,
lineOptions.gapWidth,
lineOptions.offset,
lineOptions.width,
0,
0,
0};
auto tweaker = std::make_shared<LineDrawableTweaker>(linePropertiesUBO);
// finish
builder->flush();
for (auto& drawable : builder->clearDrawables()) {
assert(tileID.has_value());
drawable->setTileID(tileID.value());
drawable->addTweaker(tweaker);
TileLayerGroup* tileLayerGroup = static_cast<TileLayerGroup*>(layerGroup.get());
tileLayerGroup->addDrawable(RenderPass::Translucent, tileID.value(), std::move(drawable));
}
}
}
gfx::ShaderPtr CustomDrawableLayerHost::Interface::lineShaderDefault() const {
gfx::ShaderGroupPtr lineShaderGroup = shaders.getShaderGroup("LineShader");
const std::unordered_set<StringIdentity> propertiesAsUniforms{
stringIndexer().get("a_color"),
stringIndexer().get("a_blur"),
stringIndexer().get("a_opacity"),
stringIndexer().get("a_gapwidth"),
stringIndexer().get("a_offset"),
stringIndexer().get("a_width"),
};
return lineShaderGroup->getOrCreateShader(context, propertiesAsUniforms);
}
std::unique_ptr<gfx::DrawableBuilder> CustomDrawableLayerHost::Interface::createBuilder(const std::string& name,
gfx::ShaderPtr shader) const {
std::unique_ptr<gfx::DrawableBuilder> builder_ = context.createDrawableBuilder(name);
builder_->setShader(std::static_pointer_cast<gfx::ShaderProgramBase>(shader));
builder_->setSubLayerIndex(0);
builder_->setEnableDepth(false);
builder_->setColorMode(gfx::ColorMode::alphaBlended());
builder_->setCullFaceMode(gfx::CullFaceMode::disabled());
builder_->setEnableStencil(false);
builder_->setRenderPass(RenderPass::Translucent);
return builder_;
}
} // namespace style
} // namespace mbgl