Skip to content

Commit

Permalink
Change parameters.currentLayer to index instead of layerIndex (#2956)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcristici authored Oct 29, 2024
1 parent ab00edf commit e95e0e1
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 48 deletions.
4 changes: 0 additions & 4 deletions include/mbgl/gfx/drawable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,6 @@ class Drawable {
/// Set sub-layer index
virtual void setSubLayerIndex(int32_t value) { subLayerIndex = value; }

void setLayerIndex(int32_t value) { layerIndex = value; }
int32_t getLayerIndex() const { return layerIndex; }

/// Depth writability for 2D drawables
DepthMaskType getDepthType() const { return depthType; }

Expand Down Expand Up @@ -293,7 +290,6 @@ class Drawable {
DrawPriority drawPriority = 0;
int32_t lineWidth = 1;
int32_t subLayerIndex = 0;
int32_t layerIndex = 0;
DepthMaskType depthType; // = DepthMaskType::ReadOnly;
UniqueDrawableData drawableData{};
gfx::VertexAttributeArrayPtr vertexAttributes;
Expand Down
14 changes: 2 additions & 12 deletions include/mbgl/renderer/layer_group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,7 @@ class TileLayerGroup : public LayerGroupBase {

void setStencilTiles(RenderTiles);

void updateLayerIndex(int32_t value) override {
layerIndex = value;
for (auto* drawable : sortedDrawables) {
drawable->setLayerIndex(value);
}
}
void updateLayerIndex(int32_t value) override { layerIndex = value; }

protected:
// When stencil clipping is enabled for the layer, this is the set
Expand Down Expand Up @@ -245,12 +240,7 @@ class LayerGroup : public LayerGroupBase {

std::size_t clearDrawables() override;

void updateLayerIndex(int32_t value) override {
layerIndex = value;
for (auto& drawable : drawables) {
drawable->setLayerIndex(value);
}
}
void updateLayerIndex(int32_t value) override { layerIndex = value; }

protected:
using DrawableCollection = std::set<gfx::UniqueDrawable, gfx::DrawableLessByPriority>;
Expand Down
10 changes: 10 additions & 0 deletions include/mbgl/renderer/render_target.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ class RenderTarget {
}
}

/// Execute the given function for each contained layer group in reversed order
template <typename Func /* void(LayerGroupBase&) */>
void visitLayerGroupsReversed(Func f) {
for (auto rit = layerGroupsByLayerIndex.rbegin(); rit != layerGroupsByLayerIndex.rend(); ++rit) {
if (rit->second) {
f(*rit->second);
}
}
}

/// Upload the layer groups
void upload(gfx::UploadPass& uploadPass);

Expand Down
1 change: 0 additions & 1 deletion src/mbgl/renderer/layer_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace mbgl {

void LayerGroupBase::addDrawable(gfx::UniqueDrawable& drawable) {
drawable->setLayerIndex(layerIndex);
// init their tweakers
for (const auto& tweaker : drawable->getTweakers()) {
tweaker->init(*drawable);
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/layer_tweaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void LayerTweaker::multiplyWithProjectionMatrix(/*in-out*/ mat4& matrix,
if (!drawable.getIs3D() && drawable.getEnableDepth()) {
// copy and adjust the projection matrix
mat4 projMatrix = projMatrixRef;
projMatrix[14] -= ((1 + drawable.getLayerIndex()) * PaintParameters::numSublayers -
projMatrix[14] -= ((1 + parameters.currentLayer) * PaintParameters::numSublayers -
drawable.getSubLayerIndex()) *
PaintParameters::depthEpsilon;
// multiply with the copy
Expand Down
10 changes: 0 additions & 10 deletions src/mbgl/renderer/render_orchestrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -959,16 +959,6 @@ size_t RenderOrchestrator::numLayerGroups() const noexcept {
return layerGroupsByLayerIndex.size();
}

int32_t RenderOrchestrator::maxLayerIndex() const {
MLN_TRACE_FUNC();

if (!layerGroupsByLayerIndex.empty()) {
assert(layerGroupsByLayerIndex.crbegin()->first == layerGroupsByLayerIndex.crbegin()->second->getLayerIndex());
return layerGroupsByLayerIndex.crbegin()->first;
}
return -1;
}

void RenderOrchestrator::updateLayers(gfx::ShaderRegistry& shaders,
gfx::Context& context,
const TransformState& state,
Expand Down
10 changes: 9 additions & 1 deletion src/mbgl/renderer/render_orchestrator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ class RenderOrchestrator final : public GlyphManagerObserver, public ImageManage
bool addLayerGroup(LayerGroupBasePtr);
bool removeLayerGroup(const LayerGroupBasePtr&);
size_t numLayerGroups() const noexcept;
int32_t maxLayerIndex() const;
void updateLayerIndex(LayerGroupBasePtr, int32_t newIndex);

template <typename Func /* void(LayerGroupBase&) */>
Expand All @@ -118,6 +117,15 @@ class RenderOrchestrator final : public GlyphManagerObserver, public ImageManage
}
}

template <typename Func /* void(LayerGroupBase&) */>
void visitLayerGroupsReversed(Func f) {
for (auto rit = layerGroupsByLayerIndex.rbegin(); rit != layerGroupsByLayerIndex.rend(); ++rit) {
if (rit->second) {
f(*rit->second);
}
}
}

void updateLayers(gfx::ShaderRegistry&,
gfx::Context&,
const TransformState&,
Expand Down
14 changes: 9 additions & 5 deletions src/mbgl/renderer/render_target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,32 @@ void RenderTarget::render(RenderOrchestrator& orchestrator, const RenderTree& re
"render target", {*offscreenTexture, Color{0.0f, 0.0f, 0.0f, 1.0f}, {}, {}});

// Run layer tweakers to update any dynamic elements
visitLayerGroups([&](LayerGroupBase& layerGroup) { layerGroup.runTweakers(renderTree, parameters); });
parameters.currentLayer = 0;
visitLayerGroups([&](LayerGroupBase& layerGroup) {
layerGroup.runTweakers(renderTree, parameters);
parameters.currentLayer++;
});

// draw layer groups, opaque pass
parameters.pass = RenderPass::Opaque;
parameters.currentLayer = 0;
parameters.depthRangeSize = 1 -
(numLayerGroups() + 2) * PaintParameters::numSublayers * PaintParameters::depthEpsilon;

visitLayerGroups([&](LayerGroupBase& layerGroup) {
parameters.currentLayer = 0;
visitLayerGroupsReversed([&](LayerGroupBase& layerGroup) {
layerGroup.render(orchestrator, parameters);
parameters.currentLayer++;
});

// draw layer groups, translucent pass
parameters.pass = RenderPass::Translucent;
parameters.currentLayer = static_cast<int32_t>(numLayerGroups()) - 1;
parameters.depthRangeSize = 1 -
(numLayerGroups() + 2) * PaintParameters::numSublayers * PaintParameters::depthEpsilon;

parameters.currentLayer = static_cast<uint32_t>(numLayerGroups()) - 1;
visitLayerGroups([&](LayerGroupBase& layerGroup) {
layerGroup.render(orchestrator, parameters);
if (parameters.currentLayer != 0) {
if (parameters.currentLayer > 0) {
parameters.currentLayer--;
}
});
Expand Down
34 changes: 20 additions & 14 deletions src/mbgl/renderer/renderer_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,11 @@ void Renderer::Impl::render(const RenderTree& renderTree,
#endif

// Tweakers are run in the upload pass so they can set up uniforms.
orchestrator.visitLayerGroups(
[&](LayerGroupBase& layerGroup) { layerGroup.runTweakers(renderTree, parameters); });
parameters.currentLayer = 0;
orchestrator.visitLayerGroups([&](LayerGroupBase& layerGroup) {
layerGroup.runTweakers(renderTree, parameters);
parameters.currentLayer++;
});
orchestrator.visitDebugLayerGroups(
[&](LayerGroupBase& layerGroup) { layerGroup.runTweakers(renderTree, parameters); });

Expand Down Expand Up @@ -315,10 +318,12 @@ void Renderer::Impl::render(const RenderTree& renderTree,
assert(parameters.pass == RenderPass::Pass3D);

// draw layer groups, 3D pass
const auto maxLayerIndex = orchestrator.maxLayerIndex();
parameters.currentLayer = static_cast<uint32_t>(orchestrator.numLayerGroups()) - 1;
orchestrator.visitLayerGroups([&](LayerGroupBase& layerGroup) {
layerGroup.render(orchestrator, parameters);
parameters.currentLayer = maxLayerIndex - layerGroup.getLayerIndex();
if (parameters.currentLayer > 0) {
parameters.currentLayer--;
}
});
};
#endif // MLN_DRAWABLE_RENDERER
Expand Down Expand Up @@ -368,30 +373,31 @@ void Renderer::Impl::render(const RenderTree& renderTree,
// Drawables
const auto drawableOpaquePass = [&] {
const auto debugGroup(parameters.renderPass->createDebugGroup("drawables-opaque"));
const auto maxLayerIndex = orchestrator.maxLayerIndex();
parameters.pass = RenderPass::Opaque;
parameters.currentLayer = 0;
parameters.depthRangeSize = 1 -
(maxLayerIndex + 3) * PaintParameters::numSublayers * PaintParameters::depthEpsilon;
parameters.depthRangeSize = 1 - (orchestrator.numLayerGroups() + 2) * PaintParameters::numSublayers *
PaintParameters::depthEpsilon;

// draw layer groups, opaque pass
orchestrator.visitLayerGroups([&](LayerGroupBase& layerGroup) {
parameters.currentLayer = layerGroup.getLayerIndex();
parameters.currentLayer = 0;
orchestrator.visitLayerGroupsReversed([&](LayerGroupBase& layerGroup) {
layerGroup.render(orchestrator, parameters);
parameters.currentLayer++;
});
};

const auto drawableTranslucentPass = [&] {
const auto debugGroup(parameters.renderPass->createDebugGroup("drawables-translucent"));
const auto maxLayerIndex = orchestrator.maxLayerIndex();
parameters.pass = RenderPass::Translucent;
parameters.depthRangeSize = 1 -
(maxLayerIndex + 3) * PaintParameters::numSublayers * PaintParameters::depthEpsilon;
parameters.depthRangeSize = 1 - (orchestrator.numLayerGroups() + 2) * PaintParameters::numSublayers *
PaintParameters::depthEpsilon;

// draw layer groups, translucent pass
parameters.currentLayer = static_cast<uint32_t>(orchestrator.numLayerGroups()) - 1;
orchestrator.visitLayerGroups([&](LayerGroupBase& layerGroup) {
parameters.currentLayer = maxLayerIndex - layerGroup.getLayerIndex();
layerGroup.render(orchestrator, parameters);
if (parameters.currentLayer > 0) {
parameters.currentLayer--;
}
});

// Finally, render any legacy layers which have not been converted to drawables.
Expand Down

0 comments on commit e95e0e1

Please sign in to comment.