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

Simplify and move Stencil reference functions #241

Merged
merged 3 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions maplibre/src/coords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,20 @@ impl WorldTileCoords {
z: self.z - 1,
})
}

/// Returns unique stencil reference values for WorldTileCoords which are 3D.
/// Tiles from arbitrary `z` can lie next to each other, because we mix tiles from
/// different levels based on availability.
pub fn stencil_reference_value_3d(&self) -> u8 {
const CASES: u8 = 4;
let z = u8::from(self.z);
match (self.x % 2 == 0, self.y % 2 == 0) {
(true, true) => 0 + z * CASES,
(true, false) => 1 + z * CASES,
(false, true) => 2 + z * CASES,
(false, false) => 3 + z * CASES,
}
}
}

impl From<(i32, i32, ZoomLevel)> for WorldTileCoords {
Expand Down
4 changes: 2 additions & 2 deletions maplibre/src/render/render_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl RenderCommand<TileShape> for DrawMask {
tracing::trace!("Drawing mask {}", &source_shape.coords());

// Draw mask with stencil value of e.g. parent
let reference = tile_view_pattern.stencil_reference_value_3d(&source_shape.coords()) as u32;
let reference = source_shape.coords().stencil_reference_value_3d() as u32;

pass.set_stencil_reference(reference);
pass.set_vertex_buffer(
Expand Down Expand Up @@ -135,7 +135,7 @@ impl RenderCommand<(IndexEntry, TileShape)> for DrawTile {
(&state.buffer_pool, &state.tile_view_pattern) else { return RenderCommandResult::Failure; };

// Uses stencil value of requested tile and the shape of the requested tile
let reference = tile_view_pattern.stencil_reference_value_3d(&shape.coords()) as u32;
let reference = shape.coords().stencil_reference_value_3d() as u32;

tracing::trace!(
"Drawing layer {:?} at {}",
Expand Down
26 changes: 0 additions & 26 deletions maplibre/src/render/tile_view_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,30 +235,4 @@ impl<Q: Queue<B>, B> TileViewPattern<Q, B> {
}
queue.write_buffer(&self.buffer.inner, 0, raw_buffer);
}

/// 2D version of [`TileViewPattern::stencil_reference_value_3d`]. This is kept for reference.
/// For the 2D case we do not take into account the Z value, so only 4 cases exist.
pub fn stencil_reference_value_2d(&self, world_coords: &WorldTileCoords) -> u8 {
match (world_coords.x, world_coords.y) {
(x, y) if x % 2 == 0 && y % 2 == 0 => 2,
(x, y) if x % 2 == 0 && y % 2 != 0 => 1,
(x, y) if x % 2 != 0 && y % 2 == 0 => 4,
(x, y) if x % 2 != 0 && y % 2 != 0 => 3,
_ => unreachable!(),
}
}

/// Returns unique stencil reference values for WorldTileCoords which are 3D.
/// Tiles from arbitrary `z` can lie next to each other, because we mix tiles from
/// different levels based on availability.
pub fn stencil_reference_value_3d(&self, world_coords: &WorldTileCoords) -> u8 {
const CASES: u8 = 4;
match (world_coords.x, world_coords.y, u8::from(world_coords.z)) {
(x, y, z) if x % 2 == 0 && y % 2 == 0 => 0 + z * CASES,
(x, y, z) if x % 2 == 0 && y % 2 != 0 => 1 + z * CASES,
(x, y, z) if x % 2 != 0 && y % 2 == 0 => 2 + z * CASES,
(x, y, z) if x % 2 != 0 && y % 2 != 0 => 3 + z * CASES,
_ => unreachable!(),
}
}
}