fix depth clipping in perspective view

The depth range is now set slightly differently. Both ends of the range
are shifted for each layer to solve some precision issues.

Some layers draw multiple things at different depths. For example, fill
layers draw fills and antialiasing separately. To handle this case, I
added setDepthSublayer(n) which a
This commit is contained in:
Ansis Brammanis
2015-08-17 19:02:30 -04:00
parent 0d68c4fedf
commit 9e68e49c89
10 changed files with 33 additions and 34 deletions

View File

@@ -39,7 +39,7 @@ void TransformState::getProjMatrix(mat4& projMatrix) const {
matrix::rotate_z(projMatrix, projMatrix, getAngle());
matrix::translate(projMatrix, projMatrix, pixel_x() - getWidth() / 2.0f,
pixel_y() - getWidth() / 2.0f, 0);
pixel_y() - getHeight() / 2.0f, 0);
}
box TransformState::cornersToBox(uint32_t z) const {

View File

@@ -93,7 +93,7 @@ private:
double angle = 0;
double scale = 1;
double altitude = 1.5;
double pitch = 0.001;
double pitch = 0.0;
// cache values for spherical mercator math
double Bc = (scale * util::tileSize) / 360;

View File

@@ -117,7 +117,6 @@ struct DepthTest {
static const Type Default;
inline static void Set(const Type& value) {
MBGL_CHECK_ERROR(value ? glEnable(GL_DEPTH_TEST) : glDisable(GL_DEPTH_TEST));
//MBGL_CHECK_ERROR(glDisable(GL_DEPTH_TEST));
}
};

View File

@@ -154,10 +154,6 @@ void Painter::clear() {
MBGL_CHECK_ERROR(glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
}
void Painter::setStrata(float value) {
strata = value;
}
void Painter::prepareTile(const Tile& tile) {
const GLint ref = (GLint)tile.clip.reference.to_ulong();
const GLuint mask = (GLuint)tile.clip.mask.to_ulong();
@@ -223,19 +219,19 @@ void Painter::render(const Style& style, TransformState state_, const FrameData&
if (debug::renderTree) { Log::Info(Event::Render, "{"); indent++; }
// TODO: Correctly compute the number of layers recursively beforehand.
const float strataThickness = 1.0f / (order.size() + 1);
depthRangeSize = 1 - (order.size() + 2) * numSublayers * depthEpsilon;
// - OPAQUE PASS -------------------------------------------------------------------------------
// Render everything top-to-bottom by using reverse iterators. Render opaque objects first.
renderPass(RenderPass::Opaque,
order.rbegin(), order.rend(),
0, 1, strataThickness);
0, 1);
// - TRANSLUCENT PASS --------------------------------------------------------------------------
// Make a second pass, rendering translucent objects. This time, we render bottom-to-top.
renderPass(RenderPass::Translucent,
order.begin(), order.end(),
order.size() - 1, -1, strataThickness);
order.size() - 1, -1);
if (debug::renderTree) { Log::Info(Event::Render, "}"); indent--; }
@@ -266,8 +262,7 @@ void Painter::render(const Style& style, TransformState state_, const FrameData&
template <class Iterator>
void Painter::renderPass(RenderPass pass_,
Iterator it, Iterator end,
std::size_t i, int8_t increment,
const float strataThickness) {
std::size_t i, int8_t increment) {
pass = pass_;
const double zoom = state.getZoom();
@@ -282,6 +277,7 @@ void Painter::renderPass(RenderPass pass_,
config.blend = pass == RenderPass::Translucent;
for (; it != end; ++it, i += increment) {
currentLayer = i;
const auto& item = *it;
if (item.bucket && item.tile) {
// Skip this layer if it's outside the range of min/maxzoom.
@@ -293,13 +289,11 @@ void Painter::renderPass(RenderPass pass_,
}
if (item.layer.hasRenderPass(pass)) {
MBGL_DEBUG_GROUP(item.layer.id + " - " + std::string(item.tile->id));
setStrata(i * strataThickness);
prepareTile(*item.tile);
item.bucket->render(*this, item.layer, item.tile->id, item.tile->matrix);
}
} else {
MBGL_DEBUG_GROUP("background");
setStrata(i * strataThickness);
renderBackground(item.layer);
}
}
@@ -426,7 +420,8 @@ void Painter::renderBackground(const StyleLayer &layer_desc) {
config.stencilTest = false;
config.depthTest = true;
config.depthRange = { strata + strata_epsilon, 1.0f };
config.depthRange = { 1.0f, 1.0f };
MBGL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, 4));
}
@@ -455,3 +450,9 @@ mat4 Painter::translatedMatrix(const mat4& matrix, const std::array<float, 2> &t
return vtxMatrix;
}
}
void Painter::setDepthSublayer(int n) {
float nearDepth = ((1 + currentLayer) * numSublayers + n) * depthEpsilon;
float farDepth = nearDepth + depthRangeSize;
config.depthRange = { nearDepth, farDepth };
}

View File

@@ -123,9 +123,6 @@ public:
// Changes whether debug information is drawn onto the map
void setDebug(bool enabled);
// Configures the painter strata that is used for early z-culling of fragments.
void setStrata(float strata);
void drawClippingMasks(const std::set<Source*>&);
void drawClippingMask(const mat4& matrix, const ClipID& clip);
@@ -146,8 +143,7 @@ private:
template <class Iterator>
void renderPass(RenderPass,
Iterator it, Iterator end,
std::size_t i, int8_t iIncrement,
const float strataThickness);
std::size_t i, int8_t increment);
void prepareTile(const Tile& tile);
@@ -162,6 +158,8 @@ private:
SDFShader& sdfShader,
void (SymbolBucket::*drawSDF)(SDFShader&));
void setDepthSublayer(int n);
public:
void useProgram(uint32_t program);
void lineWidth(float lineWidth);
@@ -199,13 +197,16 @@ private:
uint32_t gl_program = 0;
float gl_lineWidth = 0;
std::array<uint16_t, 2> gl_viewport = {{ 0, 0 }};
float strata = 0;
RenderPass pass = RenderPass::Opaque;
const float strata_epsilon = 1.0f / (1 << 16);
Color background = {{ 0, 0, 0, 0 }};
std::vector<RenderItem> order;
int numSublayers = 3;
int currentLayer;
float depthRangeSize;
const float depthEpsilon = 1.0f / (1 << 16);
public:
FrameHistory frameHistory;

View File

@@ -55,7 +55,7 @@ void Painter::renderFill(FillBucket& bucket, const StyleLayer &layer_desc, const
static_cast<float>(frame.framebufferSize[0]),
static_cast<float>(frame.framebufferSize[1])
}};
config.depthRange = { strata, 1.0f };
setDepthSublayer(0);
bucket.drawVertices(*outlineShader);
}
@@ -95,7 +95,7 @@ void Painter::renderFill(FillBucket& bucket, const StyleLayer &layer_desc, const
// Draw the actual triangles into the color & stencil buffer.
config.depthMask = GL_TRUE;
config.depthRange = { strata, 1.0f };
setDepthSublayer(0);
bucket.drawElements(*patternShader);
}
}
@@ -112,7 +112,7 @@ void Painter::renderFill(FillBucket& bucket, const StyleLayer &layer_desc, const
// Draw the actual triangles into the color & stencil buffer.
config.depthMask = GL_TRUE;
config.depthRange = { strata + strata_epsilon, 1.0f };
setDepthSublayer(1);
bucket.drawElements(*plainShader);
}
}
@@ -132,7 +132,7 @@ void Painter::renderFill(FillBucket& bucket, const StyleLayer &layer_desc, const
static_cast<float>(frame.framebufferSize[1])
}};
config.depthRange = { strata + strata_epsilon + strata_epsilon, 1.0f };
setDepthSublayer(2);
bucket.drawVertices(*outlineShader);
}
}

View File

@@ -67,7 +67,7 @@ void Painter::renderLine(LineBucket& bucket, const StyleLayer &layer_desc, const
mat4 vtxMatrix = translatedMatrix(matrix, properties.translate, id, properties.translateAnchor);
config.depthRange = { strata, 1.0f };
setDepthSublayer(0);
if (!properties.dash_array.from.empty()) {
@@ -125,7 +125,6 @@ void Painter::renderLine(LineBucket& bucket, const StyleLayer &layer_desc, const
MBGL_CHECK_ERROR(glActiveTexture(GL_TEXTURE0));
spriteAtlas->bind(true);
config.depthRange = { strata + strata_epsilon, 1.0f }; // may or may not matter
bucket.drawLinePatterns(*linepatternShader);

View File

@@ -24,7 +24,7 @@ void Painter::renderRaster(RasterBucket& bucket, const StyleLayer &layer_desc, c
config.stencilTest = true;
config.depthTest = true;
config.depthRange = { strata + strata_epsilon, 1.0f };
setDepthSublayer(0);
bucket.drawRaster(*rasterShader, tileStencilBuffer, coveringRasterArray);
}
}

View File

@@ -100,7 +100,7 @@ void Painter::renderSDF(SymbolBucket &bucket,
sdfShader.u_buffer = (haloOffset - styleProperties.halo_width / fontScale) / sdfPx;
config.depthRange = { strata, 1.0f };
setDepthSublayer(0);
(bucket.*drawSDF)(sdfShader);
}
@@ -121,7 +121,7 @@ void Painter::renderSDF(SymbolBucket &bucket,
sdfShader.u_buffer = (256.0f - 64.0f) / 256.0f;
config.depthRange = { strata + strata_epsilon, 1.0f };
setDepthSublayer(1);
(bucket.*drawSDF)(sdfShader);
}
}
@@ -147,7 +147,7 @@ void Painter::renderSymbol(SymbolBucket &bucket, const StyleLayer &layer_desc, c
collisionBoxShader->u_maxzoom = (id.z + 1) * 10;
lineWidth(1.0f);
config.depthRange = { strata, 1.0f };
setDepthSublayer(0);
bucket.drawCollisionBoxes(*collisionBoxShader);
}
@@ -230,7 +230,7 @@ void Painter::renderSymbol(SymbolBucket &bucket, const StyleLayer &layer_desc, c
iconShader->u_fadezoom = state.getNormalizedZoom() * 10;
iconShader->u_opacity = properties.icon.opacity;
config.depthRange = { strata, 1.0f };
setDepthSublayer(0);
bucket.drawIcons(*iconShader);
}
}

View File

@@ -4,5 +4,4 @@ uniform mat4 u_matrix;
void main() {
gl_Position = u_matrix * vec4(a_pos, 0, 1);
gl_Position.z = 1.0;
}