Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix projection matrix for raster image drawables #2242

Merged
merged 21 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/mbgl/renderer/layer_tweaker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ class LayerTweaker {
/// Determine whether this tweaker should apply to the given drawable
bool checkTweakDrawable(const gfx::Drawable&) const;

/// Multiplies with the projection matrix (either default, near clipped or aligned) for the given drawable
static void multiplyWithProjectionMatrix(/*in-out*/ mat4& matrix,
const PaintParameters& parameters,
const gfx::Drawable& drawable,
bool nearClipped,
bool aligned);

std::string id;
Immutable<style::LayerProperties> evaluatedProperties;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"network": [
[
"probeNetwork - default - end",
1,
48979
],
[
"probeNetwork - default - start",
0,
0
]
],
"gfx": [
[
"probeGFX - default - end",
13,
11,
19,
1,
[
1298436,
1298436
],
[
318,
318
],
[
236,
236
]
]
]
}
Binary file modified metrics/cache-style.db
TimSylvester marked this conversation as resolved.
Show resolved Hide resolved
Binary file not shown.
Binary file added metrics/integration/image/radar2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"version": 8,
"metadata": {
"test": {
"width": 456,
"height": 456
}
},
"center": [
-75.9705,
42.1865
],
"zoom": 5,
"sources": {
"image": {
"type": "image",
"url": "local://image/radar2.png",
"coordinates": [
[
-80.425,
46.437
],
[
-71.516,
46.437
],
[
-71.516,
37.936
],
[
-80.425,
37.936
]
]
},
"geojson": {
"type": "geojson",
"data": {
"type": "Polygon",
"coordinates": [
[
[
-80.425,
46.437
],
[
-71.516,
46.437
],
[
-71.516,
37.936
],
[
-80.425,
37.936
],
[
-80.425,
46.437
]
],
[
[
-78.425,
44.437
],
[
-78.425,
39.936
],
[
-73.516,
39.936
],
[
-73.516,
44.437
],
[
-78.425,
44.437
]
]

]
}
}
},
"layers": [
{
"id": "fill-opaque",
"type": "fill",
"source": "geojson",
"paint": {
"fill-color": "blue"
}
},
{
"id": "image-translucent",
"type": "raster",
"source": "image",
"paint": {
"raster-fade-duration": 0
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"network": [
[
"probeNetwork - default - end",
1,
48979
],
[
"probeNetwork - default - start",
0,
0
]
],
"gfx": [
[
"probeGFX - default - end",
13,
11,
19,
1,
[
1298436,
1298436
],
[
318,
318
],
[
236,
236
]
]
]
}
40 changes: 25 additions & 15 deletions src/mbgl/renderer/layer_tweaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,49 @@ mat4 LayerTweaker::getTileMatrix(const UnwrappedTileID& tileID,
style::TranslateAnchorType anchor,
bool nearClipped,
bool inViewportPixelUnits,
[[maybe_unused]] const gfx::Drawable& drawable,
const gfx::Drawable& drawable,
bool aligned) {
// from RenderTile::prepare
mat4 tileMatrix;
parameters.state.matrixFor(/*out*/ tileMatrix, tileID);
if (const auto& origin{drawable.getOrigin()}; origin.has_value()) {
matrix::translate(tileMatrix, tileMatrix, origin->x, origin->y, 0);
}
multiplyWithProjectionMatrix(/*in-out*/ tileMatrix, parameters, drawable, nearClipped, aligned);
return RenderTile::translateVtxMatrix(
tileID, tileMatrix, translation, anchor, parameters.state, inViewportPixelUnits);
}

// nearClippedMatrix has near plane moved further, to enhance depth buffer precision
auto projMatrix = aligned ? parameters.transformParams.alignedProjMatrix
: (nearClipped ? parameters.transformParams.nearClippedProjMatrix
: parameters.transformParams.projMatrix);
void LayerTweaker::updateProperties(Immutable<style::LayerProperties> newProps) {
evaluatedProperties = std::move(newProps);
propertiesUpdated = true;
}

void LayerTweaker::multiplyWithProjectionMatrix(/*in-out*/ mat4& matrix,
const PaintParameters& parameters,
[[maybe_unused]] const gfx::Drawable& drawable,
bool nearClipped,
bool aligned) {
// nearClippedMatrix has near plane moved further, to enhance depth buffer precision
const auto& projMatrixRef = aligned ? parameters.transformParams.alignedProjMatrix
: (nearClipped ? parameters.transformParams.nearClippedProjMatrix
: parameters.transformParams.projMatrix);
#if !MLN_RENDER_BACKEND_OPENGL
// 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()) {
// copy and adjust the projection matrix
mat4 projMatrix = projMatrixRef;
projMatrix[14] -= ((1 + drawable.getLayerIndex()) * PaintParameters::numSublayers -
drawable.getSubLayerIndex()) *
PaintParameters::depthEpsilon;
// multiply with the copy
matrix::multiply(matrix, projMatrix, matrix);
// early return
return;
}
#endif

matrix::multiply(tileMatrix, projMatrix, tileMatrix);

return RenderTile::translateVtxMatrix(
tileID, tileMatrix, translation, anchor, parameters.state, inViewportPixelUnits);
}

void LayerTweaker::updateProperties(Immutable<style::LayerProperties> newProps) {
evaluatedProperties = std::move(newProps);
propertiesUpdated = true;
matrix::multiply(matrix, projMatrixRef, matrix);
}

} // namespace mbgl
3 changes: 2 additions & 1 deletion src/mbgl/renderer/layers/raster_layer_tweaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ void RasterLayerTweaker::execute([[maybe_unused]] LayerGroupBase& layerGroup,
// this is an image drawable
if (const auto& data = drawable.getData()) {
const gfx::ImageDrawableData& imageData = static_cast<const gfx::ImageDrawableData&>(*data);

matrix = imageData.matrix;
multiplyWithProjectionMatrix(
/*in-out*/ matrix, parameters, drawable, /*nearClipped*/ false, /*aligned*/ true);
} else {
Log::Error(Event::General, "Invalid raster layer drawable: neither tile id nor image data is set.");
return;
Expand Down
2 changes: 2 additions & 0 deletions src/mbgl/renderer/sources/render_image_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ void RenderImageSource::prepare(const SourcePrepareParameters& parameters) {
mat4& matrix = matrices[i];
matrix::identity(matrix);
transformParams.state.matrixFor(matrix, tileIds[i]);
#if MLN_LEGACY_RENDERER
matrix::multiply(matrix, transformParams.alignedProjMatrix, matrix);
#endif
}
renderData = std::make_unique<ImageSourceRenderData>(bucket, std::move(matrices), baseImpl->id);
}
Expand Down
Loading