Minor refactors

This commit is contained in:
Maximilian Ammann 2022-03-31 15:58:16 +02:00
parent 50a7dfa8f8
commit 77a7639ef1
5 changed files with 36 additions and 47 deletions

View File

@ -1,3 +1,4 @@
use cint::{Alpha, EncodedSrgb};
use csscolorparser::Color;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
@ -37,6 +38,19 @@ pub enum LayerPaint {
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)]
pub struct StyleLayer {
#[serde(skip)]

View File

@ -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> {
/*
For tile.z = zoom:
@ -330,7 +322,7 @@ impl WorldCoords {
}
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 y = self.y * tile_scale;

View File

@ -4,7 +4,6 @@ use crate::io::{LayerTessellateResult, TileIndexResult};
use cgmath::num_traits::Pow;
use std::collections::{btree_map, BTreeMap, HashSet};
#[derive(Default)]
pub struct TileCache {
cache_index: BTreeMap<Quadkey, Vec<LayerTessellateResult>>,
@ -55,7 +54,7 @@ impl TileCache {
.build_quad_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_y = world_coords.y / TILE_SIZE * scale - world_tile_coords.y as f64;

View File

@ -376,7 +376,7 @@ impl IndexEntry {
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()
}
@ -419,14 +419,6 @@ impl RingIndex {
pub fn get_layers_fallback(&self, coords: &WorldTileCoords) -> Option<&VecDeque<IndexEntry>> {
let mut current = *coords;
/*index.get_layers(&world_coords)
.or_else(|| {
world_coords
.get_parent()
.and_then(|parent| index.get_layers(&parent))
})*/
loop {
if let Some(entries) = self.get_layers(&current) {
return Some(entries);

View File

@ -1,4 +1,4 @@
use cgmath::Matrix4;
use cgmath::{Matrix4, Vector4};
use std::collections::HashSet;
use std::default::Default;
use std::{cmp, iter};
@ -6,7 +6,7 @@ use std::{cmp, iter};
use crate::coords::{ViewRegion, TILE_SIZE};
use crate::io::scheduler::IOScheduler;
use crate::io::{LayerTessellateResult};
use crate::io::LayerTessellateResult;
use style_spec::layer::LayerPaint;
use style_spec::{EncodedSrgb, Style};
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
// 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.
for entries in self.buffer_pool.index().iter() {
for entry in entries {
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.
// 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
if let Some(view_region) = &view_region {
@ -425,19 +424,11 @@ impl RenderState {
.iter()
.find(|layer| source_layer.as_str() == layer.layer_name())
{
let color: Option<style_spec::Alpha<EncodedSrgb<f32>>> =
style_layer.paint.as_ref().and_then(|paint| match paint {
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())
}
});
let color: Option<Vec4f32> = style_layer
.paint
.as_ref()
.and_then(|paint| paint.get_color())
.map(|color| color.into());
match result {
LayerTessellateResult::UnavailableLayer { .. } => {}
@ -456,9 +447,9 @@ impl RenderState {
.enumerate()
.flat_map(|(i, _feature)| {
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<_>>();
@ -470,23 +461,24 @@ impl RenderState {
))
.downcast();
let tile_metadata = ShaderTileMetadata::new(
transform.into(),
zoom_factor,
style_layer.index as f32,
);
self.buffer_pool.allocate_tile_geometry(
&self.queue,
*coords,
style_layer.clone(),
buffer,
ShaderTileMetadata::new(
transform.into(),
zoom_factor,
style_layer.index as f32,
),
tile_metadata,
&feature_metadata,
);
}
}
}
}
};
}
}
}
@ -587,7 +579,7 @@ impl RenderState {
0,
self.buffer_pool
.metadata()
.slice(entry.metadata_buffer_range()),
.slice(entry.tile_metadata_buffer_range()),
);
pass.draw(0..6, 0..1);
}
@ -616,7 +608,7 @@ impl RenderState {
1,
self.buffer_pool
.metadata()
.slice(entry.metadata_buffer_range()),
.slice(entry.tile_metadata_buffer_range()),
);
pass.set_vertex_buffer(
2,