mirror of
https://github.com/maplibre/maplibre-rs.git
synced 2025-12-08 19:05:57 +00:00
Minor refactors
This commit is contained in:
parent
50a7dfa8f8
commit
77a7639ef1
@ -1,3 +1,4 @@
|
|||||||
|
use cint::{Alpha, EncodedSrgb};
|
||||||
use csscolorparser::Color;
|
use csscolorparser::Color;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
@ -37,6 +38,19 @@ pub enum LayerPaint {
|
|||||||
Fill(FillPaint),
|
Fill(FillPaint),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl LayerPaint {
|
||||||
|
pub fn get_color(&self) -> Option<Alpha<EncodedSrgb<f32>>> {
|
||||||
|
match self {
|
||||||
|
LayerPaint::Background(paint) => paint
|
||||||
|
.background_color
|
||||||
|
.as_ref()
|
||||||
|
.map(|color| color.clone().into()),
|
||||||
|
LayerPaint::Line(paint) => paint.line_color.as_ref().map(|color| color.clone().into()),
|
||||||
|
LayerPaint::Fill(paint) => paint.fill_color.as_ref().map(|color| color.clone().into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub struct StyleLayer {
|
pub struct StyleLayer {
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
|
|||||||
@ -145,14 +145,6 @@ impl WorldTileCoords {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_world_coords(self, _z: u8, _zoom: f64) -> WorldCoords {
|
|
||||||
let _tile_scale = 2.0.pow(self.z as f64) * TILE_SIZE;
|
|
||||||
let x = self.x as f64 * 512.0;
|
|
||||||
let y = self.y as f64 * 512.0;
|
|
||||||
|
|
||||||
WorldCoords { x, y }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn transform_for_zoom(&self, zoom: f64) -> Matrix4<f64> {
|
pub fn transform_for_zoom(&self, zoom: f64) -> Matrix4<f64> {
|
||||||
/*
|
/*
|
||||||
For tile.z = zoom:
|
For tile.z = zoom:
|
||||||
@ -330,7 +322,7 @@ impl WorldCoords {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_world_tile(self, z: u8, zoom: f64) -> WorldTileCoords {
|
pub fn into_world_tile(self, z: u8, zoom: f64) -> WorldTileCoords {
|
||||||
let tile_scale = 2.0.pow(z as f64 - zoom) / TILE_SIZE;
|
let tile_scale = 2.0.pow(z as f64 - zoom) / TILE_SIZE; // TODO: Deduplicate
|
||||||
let x = self.x * tile_scale;
|
let x = self.x * tile_scale;
|
||||||
let y = self.y * tile_scale;
|
let y = self.y * tile_scale;
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,6 @@ use crate::io::{LayerTessellateResult, TileIndexResult};
|
|||||||
use cgmath::num_traits::Pow;
|
use cgmath::num_traits::Pow;
|
||||||
use std::collections::{btree_map, BTreeMap, HashSet};
|
use std::collections::{btree_map, BTreeMap, HashSet};
|
||||||
|
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct TileCache {
|
pub struct TileCache {
|
||||||
cache_index: BTreeMap<Quadkey, Vec<LayerTessellateResult>>,
|
cache_index: BTreeMap<Quadkey, Vec<LayerTessellateResult>>,
|
||||||
@ -55,7 +54,7 @@ impl TileCache {
|
|||||||
.build_quad_key()
|
.build_quad_key()
|
||||||
.and_then(|key| self.tile_geometry_index.get(&key))
|
.and_then(|key| self.tile_geometry_index.get(&key))
|
||||||
{
|
{
|
||||||
let scale = 2.0.pow(z as f64 - zoom);
|
let scale = 2.0.pow(z as f64 - zoom); // TODO deduplicate
|
||||||
|
|
||||||
let delta_x = world_coords.x / TILE_SIZE * scale - world_tile_coords.x as f64;
|
let delta_x = world_coords.x / TILE_SIZE * scale - world_tile_coords.x as f64;
|
||||||
let delta_y = world_coords.y / TILE_SIZE * scale - world_tile_coords.y as f64;
|
let delta_y = world_coords.y / TILE_SIZE * scale - world_tile_coords.y as f64;
|
||||||
|
|||||||
@ -376,7 +376,7 @@ impl IndexEntry {
|
|||||||
self.buffer_vertices.clone()
|
self.buffer_vertices.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn metadata_buffer_range(&self) -> Range<wgpu::BufferAddress> {
|
pub fn tile_metadata_buffer_range(&self) -> Range<wgpu::BufferAddress> {
|
||||||
self.buffer_tile_metadata.clone()
|
self.buffer_tile_metadata.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -419,14 +419,6 @@ impl RingIndex {
|
|||||||
|
|
||||||
pub fn get_layers_fallback(&self, coords: &WorldTileCoords) -> Option<&VecDeque<IndexEntry>> {
|
pub fn get_layers_fallback(&self, coords: &WorldTileCoords) -> Option<&VecDeque<IndexEntry>> {
|
||||||
let mut current = *coords;
|
let mut current = *coords;
|
||||||
|
|
||||||
/*index.get_layers(&world_coords)
|
|
||||||
.or_else(|| {
|
|
||||||
world_coords
|
|
||||||
.get_parent()
|
|
||||||
.and_then(|parent| index.get_layers(&parent))
|
|
||||||
})*/
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if let Some(entries) = self.get_layers(¤t) {
|
if let Some(entries) = self.get_layers(¤t) {
|
||||||
return Some(entries);
|
return Some(entries);
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
use cgmath::Matrix4;
|
use cgmath::{Matrix4, Vector4};
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::{cmp, iter};
|
use std::{cmp, iter};
|
||||||
@ -6,7 +6,7 @@ use std::{cmp, iter};
|
|||||||
use crate::coords::{ViewRegion, TILE_SIZE};
|
use crate::coords::{ViewRegion, TILE_SIZE};
|
||||||
|
|
||||||
use crate::io::scheduler::IOScheduler;
|
use crate::io::scheduler::IOScheduler;
|
||||||
use crate::io::{LayerTessellateResult};
|
use crate::io::LayerTessellateResult;
|
||||||
use style_spec::layer::LayerPaint;
|
use style_spec::layer::LayerPaint;
|
||||||
use style_spec::{EncodedSrgb, Style};
|
use style_spec::{EncodedSrgb, Style};
|
||||||
use wgpu::{Buffer, Limits, Queue};
|
use wgpu::{Buffer, Limits, Queue};
|
||||||
@ -380,7 +380,6 @@ impl RenderState {
|
|||||||
// Update tile metadata for all required tiles on the GPU according to current zoom, camera and perspective
|
// Update tile metadata for all required tiles on the GPU according to current zoom, camera and perspective
|
||||||
// We perform the update before uploading new tessellated tiles, such that each
|
// We perform the update before uploading new tessellated tiles, such that each
|
||||||
// tile metadata in the the `buffer_pool` gets updated exactly once and not twice.
|
// tile metadata in the the `buffer_pool` gets updated exactly once and not twice.
|
||||||
|
|
||||||
for entries in self.buffer_pool.index().iter() {
|
for entries in self.buffer_pool.index().iter() {
|
||||||
for entry in entries {
|
for entry in entries {
|
||||||
let world_coords = entry.coords;
|
let world_coords = entry.coords;
|
||||||
@ -407,7 +406,7 @@ impl RenderState {
|
|||||||
|
|
||||||
// Factor which determines how much we need to adjust the width of lines for example.
|
// Factor which determines how much we need to adjust the width of lines for example.
|
||||||
// If zoom == z -> zoom_factor == 1
|
// If zoom == z -> zoom_factor == 1
|
||||||
let zoom_factor = 2.0_f64.powf(visible_z as f64 - self.zoom) as f32;
|
let zoom_factor = 2.0_f64.powf(visible_z as f64 - self.zoom) as f32; // TODO deduplicate
|
||||||
|
|
||||||
// Upload all tessellated layers which are in view
|
// Upload all tessellated layers which are in view
|
||||||
if let Some(view_region) = &view_region {
|
if let Some(view_region) = &view_region {
|
||||||
@ -425,19 +424,11 @@ impl RenderState {
|
|||||||
.iter()
|
.iter()
|
||||||
.find(|layer| source_layer.as_str() == layer.layer_name())
|
.find(|layer| source_layer.as_str() == layer.layer_name())
|
||||||
{
|
{
|
||||||
let color: Option<style_spec::Alpha<EncodedSrgb<f32>>> =
|
let color: Option<Vec4f32> = style_layer
|
||||||
style_layer.paint.as_ref().and_then(|paint| match paint {
|
.paint
|
||||||
LayerPaint::Background(paint) => paint
|
|
||||||
.background_color
|
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|color| color.clone().into()),
|
.and_then(|paint| paint.get_color())
|
||||||
LayerPaint::Line(paint) => {
|
.map(|color| color.into());
|
||||||
paint.line_color.as_ref().map(|color| color.clone().into())
|
|
||||||
}
|
|
||||||
LayerPaint::Fill(paint) => {
|
|
||||||
paint.fill_color.as_ref().map(|color| color.clone().into())
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
LayerTessellateResult::UnavailableLayer { .. } => {}
|
LayerTessellateResult::UnavailableLayer { .. } => {}
|
||||||
@ -456,9 +447,9 @@ impl RenderState {
|
|||||||
.enumerate()
|
.enumerate()
|
||||||
.flat_map(|(i, _feature)| {
|
.flat_map(|(i, _feature)| {
|
||||||
iter::repeat(ShaderFeatureStyle {
|
iter::repeat(ShaderFeatureStyle {
|
||||||
color: color.unwrap().into(),
|
color: color.unwrap(),
|
||||||
})
|
})
|
||||||
.take(*feature_indices.get(i).unwrap() as usize)
|
.take(feature_indices[i] as usize)
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
@ -470,23 +461,24 @@ impl RenderState {
|
|||||||
))
|
))
|
||||||
.downcast();
|
.downcast();
|
||||||
|
|
||||||
|
let tile_metadata = ShaderTileMetadata::new(
|
||||||
|
transform.into(),
|
||||||
|
zoom_factor,
|
||||||
|
style_layer.index as f32,
|
||||||
|
);
|
||||||
self.buffer_pool.allocate_tile_geometry(
|
self.buffer_pool.allocate_tile_geometry(
|
||||||
&self.queue,
|
&self.queue,
|
||||||
*coords,
|
*coords,
|
||||||
style_layer.clone(),
|
style_layer.clone(),
|
||||||
buffer,
|
buffer,
|
||||||
ShaderTileMetadata::new(
|
tile_metadata,
|
||||||
transform.into(),
|
|
||||||
zoom_factor,
|
|
||||||
style_layer.index as f32,
|
|
||||||
),
|
|
||||||
&feature_metadata,
|
&feature_metadata,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -587,7 +579,7 @@ impl RenderState {
|
|||||||
0,
|
0,
|
||||||
self.buffer_pool
|
self.buffer_pool
|
||||||
.metadata()
|
.metadata()
|
||||||
.slice(entry.metadata_buffer_range()),
|
.slice(entry.tile_metadata_buffer_range()),
|
||||||
);
|
);
|
||||||
pass.draw(0..6, 0..1);
|
pass.draw(0..6, 0..1);
|
||||||
}
|
}
|
||||||
@ -616,7 +608,7 @@ impl RenderState {
|
|||||||
1,
|
1,
|
||||||
self.buffer_pool
|
self.buffer_pool
|
||||||
.metadata()
|
.metadata()
|
||||||
.slice(entry.metadata_buffer_range()),
|
.slice(entry.tile_metadata_buffer_range()),
|
||||||
);
|
);
|
||||||
pass.set_vertex_buffer(
|
pass.set_vertex_buffer(
|
||||||
2,
|
2,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user