Skip to content

Commit

Permalink
Use enableDepth option (#2073)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimSylvester authored Jan 31, 2024
1 parent 61a6b66 commit 867e7cc
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion include/mbgl/gfx/drawable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class Drawable {
void setDrawPriority(DrawPriority value) { drawPriority = value; }

/// Whether to enable depth testing
bool getEnableDepth() { return enableDepth; }
bool getEnableDepth() const { return enableDepth; }
virtual void setEnableDepth(bool value) { enableDepth = value; }

/// Determines depth range within the layer for 2D drawables
Expand Down
7 changes: 7 additions & 0 deletions include/mbgl/mtl/tile_layer_group.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#pragma once

#include <mbgl/mtl/mtl_fwd.hpp>
#include <mbgl/renderer/layer_group.hpp>

#include <Foundation/NSSharedPtr.hpp>

#include <optional>

namespace mbgl {
namespace mtl {

Expand All @@ -17,6 +22,8 @@ class TileLayerGroup : public mbgl::TileLayerGroup {
void render(RenderOrchestrator&, PaintParameters&) override;

protected:
std::optional<MTLDepthStencilStatePtr> stateNone;
std::optional<MTLDepthStencilStatePtr> stateDepth;
};

} // namespace mtl
Expand Down
4 changes: 3 additions & 1 deletion src/mbgl/mtl/drawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ void Drawable::draw(PaintParameters& parameters) const {
if (enableStencil && !newStencilMode) {
newStencilMode = parameters.stencilModeForClipping(tileID->toUnwrapped());
}
const auto depthMode = parameters.depthModeForSublayer(getSubLayerIndex(), getDepthType());
const auto depthMode = getEnableDepth()
? parameters.depthModeForSublayer(getSubLayerIndex(), getDepthType())
: gfx::DepthMode::disabled();
const auto stencilMode = enableStencil ? parameters.stencilModeForClipping(tileID->toUnwrapped())
: gfx::StencilMode::disabled();
impl->depthStencilState = context.makeDepthStencilState(depthMode, stencilMode, renderable);
Expand Down
42 changes: 38 additions & 4 deletions src/mbgl/mtl/tile_layer_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,49 @@ void TileLayerGroup::render(RenderOrchestrator&, PaintParameters& parameters) {
// If we're doing 3D stenciling and have any features to draw, set up the single-value stencil mask.
// If we're doing 2D stenciling and have any drawables with tile IDs, render each tile into the stencil buffer with
// a different value.
MTLDepthStencilStatePtr stateWithStencil, stateWithoutStencil;
// We can keep the depth-based descriptors, but the stencil-based ones can change
// every time, as a new value is assigned in each call to `stencilModeFor3D`.
std::optional<MTLDepthStencilStatePtr> stateStencil, stateDepthStencil;
std::function<const MTLDepthStencilStatePtr&(bool, bool)> getDepthStencilState;
if (features3d) {
// If we're using group-wide states, build only the ones that actually get used
getDepthStencilState = [&](bool depth, bool stencil) -> const MTLDepthStencilStatePtr& {
if (depth) {
// We assume this doesn't change over the lifetime of a layer group.
const auto depthMode = parameters.depthModeFor3D();
if (stencil) {
if (!stateDepthStencil.has_value()) {
stateDepthStencil = context.makeDepthStencilState(depthMode, stencilMode3d, renderable);
}
return *stateDepthStencil;
} else {
if (!stateDepth) {
stateDepth = context.makeDepthStencilState(depthMode, gfx::StencilMode::disabled(), renderable);
}
return *stateDepth;
}
} else {
if (stencil) {
if (!stateStencil.has_value()) {
stateStencil = context.makeDepthStencilState(
gfx::DepthMode::disabled(), stencilMode3d, renderable);
}
return *stateStencil;
} else {
if (!stateNone) {
stateNone = context.makeDepthStencilState(
gfx::DepthMode::disabled(), gfx::StencilMode::disabled(), renderable);
}
return *stateNone;
}
}
};

const auto depthMode = parameters.depthModeFor3D();
if (stencil3d) {
stencilMode3d = parameters.stencilModeFor3D();
stateWithStencil = context.makeDepthStencilState(depthMode, stencilMode3d, renderable);
encoder->setStencilReferenceValue(stencilMode3d.ref);
}
stateWithoutStencil = context.makeDepthStencilState(depthMode, gfx::StencilMode::disabled(), renderable);
} else if (stencilTiles && !stencilTiles->empty()) {
parameters.renderTileClippingMasks(stencilTiles);
}
Expand All @@ -100,7 +134,7 @@ void TileLayerGroup::render(RenderOrchestrator&, PaintParameters& parameters) {
// stencil mode for features with stencil enabled or disable stenciling.
// 2D drawables will set their own stencil mode within `draw`.
if (features3d) {
const auto& state = drawable.getEnableStencil() ? stateWithStencil : stateWithoutStencil;
const auto state = getDepthStencilState(drawable.getEnableDepth(), drawable.getEnableStencil());
renderPass.setDepthStencilState(state);
}

Expand Down
5 changes: 3 additions & 2 deletions src/mbgl/renderer/layer_tweaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ mat4 LayerTweaker::getTileMatrix(const UnwrappedTileID& tileID,
: parameters.transformParams.projMatrix);

#if !MLN_RENDER_BACKEND_OPENGL
// Offset the projection matrix NDC depth range for the drawable's layer and sublayer.
if (!drawable.getIs3D()) {
// If this drawable is participating in depth testing, offset the
// projection matrix NDC depth range for the drawable's layer and sublayer.
if (!drawable.getIs3D() && drawable.getEnableDepth()) {
projMatrix[14] -= ((1 + drawable.getLayerIndex()) * PaintParameters::numSublayers -
drawable.getSubLayerIndex()) *
PaintParameters::depthEpsilon;
Expand Down

0 comments on commit 867e7cc

Please sign in to comment.