mln-rs-engine 0.0.19

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! The tests' own layer types: render halves of the specification's layer
//! types, whose style halves are `mln_style`'s, so that the tests' styles
//! parse as they are. Each draws one color: a geometry layer a quad at each
//! of its features' points, a raster or raster-dem layer and a background
//! each tile's square. A symbol layer asks for the glyphs of its text and its
//! icons, as the built-in type does, and doesn't place. They meet terrain, blend, turn with the map and read
//! feature state as the built-in types do as far as the engine's tests need,
//! and no further: what the built-in types draw is their own tests'.

use std::marker::PhantomData;
use std::sync::Arc;

use bytemuck::{Pod, Zeroable};
use geometry::Point;
use mln_gfx::shaders::block::uniform_block;
use mln_gfx::shaders::program::{Program, ProgramBuffer, Projection, Stages};
use mln_gfx::shaders::source::{Sources, WgslSource};
use mln_gfx::{AttributeDataType, ColorMode, CullFaceMode, DepthMaskType, DrawMode};
use mln_style::error::Error;
use mln_style::expression::EvaluationContext;
use mln_style::expression::functions::RegisterError;
use mln_style::feature::GeometryTileFeature;
use mln_style::layer::{PaintSet, StyleLayerType};
use mln_style::layers::{
    Background, Circle, ColorRelief, Fill, FillExtrusion, Heatmap, Hillshade, Line, Raster, Symbol,
};
use mln_style::range::Range;
use mln_style::types::{HillshadeIlluminationAnchorType, TranslateAnchorType};

use crate::RegistryBuilder;
use crate::draw::draws::{Depth, Draw, Draws, LayerFrame};
use crate::draw::indexes::DrawIndexes;
use crate::draw::mesh::{Indexed, add_point_quads};
use crate::draw::objects::{DrawGeometry, Mesh, Textures, raw_vertices};
use crate::draw::segment::SegmentBase;
use crate::draw::vertex_attribute::{SharedAttribute, Uniforms, VertexAttributeArray};
use crate::image::image_store::ImageType;
use crate::layer::bucket::{Bucket, Upload};
use crate::layer::bucket_geometry::{GeometryVec, bucket_indexes, bucket_vertices};
use crate::layer::layer_type::EvaluatedLayer;
use crate::layer::layout::{BuildCx, Layout, LayoutCx};
use crate::layer::paint_property_binder::{LayerBinders, PaintBinders};
use crate::layer::render_layer::{EvaluateCx, Evaluation, Layer, LayerValues, OnTerrain, PrepareCx, TileCx, UpdateCx};
use crate::layer::tile_input::{Features, NoTiles, TileData};
use crate::render::static_data::RenderStaticData;
use crate::render::terrain::SurfaceLight;
use crate::render::tile::FrameTile;
use crate::render::tile_matrix::{projection_data, to_projection_ubo};
use crate::shaders::programs::POS;
use crate::source::{DemTiles, RasterTiles};
use crate::text::glyph::GlyphID;
use crate::tile::OverscaledTileID;
use crate::tile_store::tile_cover::{TileCoverParameters, tile_cover};

/// Adds the tests' layer types.
pub(crate) fn register_layer_types(builder: &mut RegistryBuilder) -> Result<(), RegisterError> {
    builder.add_layer_type::<TestFeatures<Fill>>()?;
    builder.add_layer_type::<TestFeatures<Line>>()?;
    builder.add_layer_type::<TestFeatures<Circle>>()?;
    builder.add_layer_type::<TestFeatures<Heatmap>>()?;
    builder.add_layer_type::<TestFeatures<FillExtrusion>>()?;
    builder.add_layer_type::<TestFeatures<Symbol>>()?;
    builder.add_layer_type::<TestRaster>()?;
    builder.add_layer_type::<TestDem<Hillshade>>()?;
    builder.add_layer_type::<TestDem<ColorRelief>>()?;
    builder.add_layer_type::<TestBackground>()
}

uniform_block! {
    /// The test program's color, premultiplied.
    pub struct TestPropsUBO {
        pub color: [f32; 4],
    }
}

const TEST_WGSL: WgslSource = WgslSource {
    vertex: Some(
        r"
@vertex
fn main(in: VertexInput) -> @builtin(position) vec4<f32> {
    return projectTileFor3D(vec2<f32>(f32(in.pos.x), f32(in.pos.y)), 0.0, projectionVector[globalIndex.value]);
}
",
    ),
    fragment: Some(
        r"
@fragment
fn main() -> @location(0) vec4<f32> {
    return props.color;
}
",
    ),
    prelude: None,
};

/// The tests' layers' program, of WebGPU alone: a mesh of tile positions in
/// one color.
pub(crate) static TEST: Program = Program {
    name: "TestShader",
    buffers: &[ProgramBuffer::uniform::<TestPropsUBO>("props", 0, Stages::Fragment)],
    paint_params: None,
    projection: Projection::PerDraw,
    attributes: &[POS],
    instance_attributes: &[],
    textures: &[],
    properties: &[],
    sources: Sources::NONE.wgsl(&TEST_WGSL),
};

/// Pushes a draw of `mesh` for each of `tiles` whose value is a mesh, and
/// the layer's color.
fn push_draws<L: Layer<Tile = Mesh>>(cx: &mut PrepareCx<'_, '_, L>, ids: &[OverscaledTileID], out: &mut Draws) {
    for &tile_id in ids {
        let Some(entry) = cx.tiles.entry(&tile_id) else { continue };
        let Some(mesh) = entry.value.as_ref() else { continue };
        let projection = projection_data(
            &tile_id.to_unwrapped(),
            cx.frame,
            [0.0, 0.0],
            TranslateAnchorType::Viewport,
            false,
            false,
            None,
            false,
        );
        let draw = Draw {
            order: mesh.seq(),
            depth: Depth::Layer { sub_layer: 0, mask: DepthMaskType::ReadOnly },
            ..Draw::DEFAULT
        };
        out.main().push_at(mesh, tile_id, &to_projection_ubo(&projection), draw, ());
    }
    out.main().block(&TestPropsUBO { color: [0.0, 0.0, 1.0, 1.0] });
}

/// The square of a tile, of the test program.
fn square(cx: &mut TileCx<'_, '_, impl Layer>) -> Mesh {
    let vertices = RenderStaticData::tile_vertices();
    let count = vertices.len();
    let geometry = DrawGeometry {
        vertices: raw_vertices("pos", bytemuck::cast_slice(&vertices).to_vec(), count, AttributeDataType::Short2),
        vertex_count: count,
        mode: DrawMode::Triangles(),
        indexes: DrawIndexes::new(RenderStaticData::quad_triangle_indices()),
        segments: RenderStaticData::tile_triangle_segments().iter().map(SegmentBase::copy).collect(),
        instances: None,
    };
    let shader = cx.res.context.shader_key(&TEST, Uniforms::default(), cx.base.projection_variant);
    cx.res.mesh(shader, ColorMode::alpha_blended(), CullFaceMode::disabled(), geometry, Textures::default())
}

/// A geometry layer type whose render half the tests give: how its layers
/// meet terrain and turn with the map, and the binders of its data-driven
/// paint values, which feature state rewrites.
pub(crate) trait TestGeometry: StyleLayerType + Sized {
    type Binders: PaintBinders<Paint = <Self::PaintProperties as PaintSet>::Evaluated>;
    const TERRAIN: OnTerrain = OnTerrain::Flat;
    const VOLUME: bool = false;

    /// The pattern that `layer` draws with, an image its layout waits for.
    fn pattern(_layer: &EvaluatedLayer<Self>) -> Option<String> {
        None
    }

    /// Whether the layer draws by the map's bearing with `paint`.
    fn drapes_by_bearing(_paint: &<Self::PaintProperties as PaintSet>::Evaluated) -> bool {
        false
    }

    /// Whether a layout of `group` may ask for glyphs or images.
    fn needs_resources(group: &[Arc<EvaluatedLayer<Self>>]) -> bool {
        group.iter().any(|layer| Self::pattern(layer).is_some())
    }

    /// Asks for what `feature`, which the layout selects, draws with but
    /// the group's pattern.
    fn need(_cx: &mut LayoutCx<'_, Self>, _feature: &dyn GeometryTileFeature) {}
}

pub(crate) use binders::{
    TestCircleBinders, TestFillBinders, TestFillExtrusionBinders, TestHeatmapBinders, TestLineBinders,
    TestSymbolBinders,
};

/// The binders of one data-driven paint value of each geometry type, which
/// feature state rewrites. The tests' draws don't read them.
#[expect(dead_code, reason = "the binders' draws, which the tests' layers don't make")]
mod binders {
    use mln_style::color::Color;
    use mln_style::layers::circle_layer_properties::CirclePaintProperties;
    use mln_style::layers::fill_extrusion_layer_properties::FillExtrusionPaintProperties;
    use mln_style::layers::fill_layer_properties::FillPaintProperties;
    use mln_style::layers::heatmap_layer_properties::HeatmapPaintProperties;
    use mln_style::layers::line_layer_properties::LinePaintProperties;
    use mln_style::layers::symbol_layer_properties::SymbolPaintProperties;

    use crate::layer::paint_property_binder::PaintPropertyBinder;

    crate::paint_property_binders!(
        pub struct TestFillBinders for FillPaintProperties::PossiblyEvaluated {
            fill_color: PaintPropertyBinder<Color> => ["color"],
        }
    );

    crate::paint_property_binders!(
        pub struct TestLineBinders for LinePaintProperties::PossiblyEvaluated {
            line_color: PaintPropertyBinder<Color> => ["color"],
        }
    );

    crate::paint_property_binders!(
        pub struct TestCircleBinders for CirclePaintProperties::PossiblyEvaluated {
            circle_color: PaintPropertyBinder<Color> => ["color"],
        }
    );

    crate::paint_property_binders!(
        pub struct TestHeatmapBinders for HeatmapPaintProperties::PossiblyEvaluated {
            heatmap_weight: PaintPropertyBinder<f32> => ["weight"],
        }
    );

    crate::paint_property_binders!(
        pub struct TestFillExtrusionBinders for FillExtrusionPaintProperties::PossiblyEvaluated {
            fill_extrusion_color: PaintPropertyBinder<Color> => ["color"],
        }
    );

    crate::paint_property_binders!(
        pub struct TestSymbolBinders for SymbolPaintProperties::PossiblyEvaluated {
            text_color: PaintPropertyBinder<Color> => ["color"],
        }
    );
}

impl TestGeometry for Fill {
    type Binders = TestFillBinders;
    const TERRAIN: OnTerrain = OnTerrain::Drapes;

    fn pattern(layer: &EvaluatedLayer<Self>) -> Option<String> {
        let pattern = layer.paint.fill_pattern.faded().to.id();
        (!pattern.is_empty()).then(|| pattern.to_owned())
    }
}

impl TestGeometry for Line {
    type Binders = TestLineBinders;
    const TERRAIN: OnTerrain = OnTerrain::Drapes;

    /// A line translated in the viewport turns with the map.
    fn drapes_by_bearing(paint: &<Self::PaintProperties as PaintSet>::Evaluated) -> bool {
        *paint.line_translate_anchor.uniform() == TranslateAnchorType::Viewport
            && *paint.line_translate.uniform() != [0.0, 0.0]
    }
}

impl TestGeometry for Circle {
    type Binders = TestCircleBinders;
}

impl TestGeometry for Heatmap {
    type Binders = TestHeatmapBinders;
}

impl TestGeometry for FillExtrusion {
    type Binders = TestFillExtrusionBinders;
    const VOLUME: bool = true;
}

impl TestGeometry for Symbol {
    type Binders = TestSymbolBinders;

    fn needs_resources(_group: &[Arc<EvaluatedLayer<Self>>]) -> bool {
        true
    }

    /// Asks for the glyphs of the feature's text, in the stack of its
    /// section or of `text-font`, the images of its text's image sections,
    /// and its icon, as the leader's layout evaluates them.
    fn need(cx: &mut LayoutCx<'_, Self>, feature: &dyn GeometryTileFeature) {
        let layout = cx.layers()[0].style.layout.evaluate(&cx.parameters());
        let context = EvaluationContext::with_zoom_feature(cx.zoom(), Some(feature))
            .with_available_images(Some(cx.available_images()))
            .with_unavailable_images(Some(cx.unavailable_images()));
        let text = layout.text_field.evaluate(&context);
        let font_stack: Vec<String> = layout.text_font.evaluate(&context);
        for section in &text.sections {
            if let Some(image) = &section.image {
                if !image.empty() {
                    cx.need_image(image.id(), ImageType::Icon);
                }
                continue;
            }
            let stack = section.font_stack.clone().unwrap_or_else(|| font_stack.clone());
            let ids = section.text.chars().filter_map(GlyphID::from_char);
            cx.glyphs().glyphs.entry(stack).or_default().extend(ids);
        }
        let icon = layout.icon_image.evaluate(&context);
        if !icon.empty() {
            cx.need_image(icon.id(), ImageType::Icon);
        }
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Pod, Zeroable)]
#[repr(C)]
pub(crate) struct TestVertex {
    pos: [i16; 2],
}

/// A tile's quads, one at each point of its features, and their paint
/// values.
#[derive(Clone, mln_wire::Wire)]
pub(crate) struct TestBucket<B: PaintBinders> {
    pub(crate) vertices: GeometryVec<TestVertex>,
    triangles: GeometryVec<u16>,
    segments: Vec<SegmentBase>,
    binders: LayerBinders<B>,
}

impl<B: PaintBinders> Bucket for TestBucket<B> {
    fn upload(&mut self, up: &mut Upload<'_>) {
        up.vec(&mut self.vertices);
        up.indexes(&mut self.triangles);
        up.binders(&mut self.binders);
    }
}

/// The features of a tile that the group's filter selects.
pub(crate) struct TestLayout<S> {
    features: Vec<usize>,
    style: PhantomData<fn() -> S>,
}

impl<S: TestGeometry> Layout<S> for TestLayout<S> {
    type Bucket = TestBucket<S::Binders>;

    fn needs_resources(group: &[Arc<EvaluatedLayer<S>>]) -> bool {
        S::needs_resources(group)
    }

    fn new(cx: &mut LayoutCx<'_, S>) -> Result<Self, Error> {
        for layer in cx.layers() {
            if let Some(pattern) = S::pattern(layer) {
                cx.need_image(&pattern, ImageType::Pattern);
            }
        }
        let tile_layer = cx.source_layer()?;
        let selection = cx.select(&tile_layer, cx.filter_context());
        let mut features = Vec::new();
        for i in 0..tile_layer.len() {
            if !selection.may_select(i) {
                continue;
            }
            let feature = tile_layer.feature(i)?;
            if selection.selects(&feature) {
                S::need(cx, &feature);
                features.push(i);
            }
        }
        Ok(TestLayout { features, style: PhantomData })
    }

    fn build(self, cx: &mut BuildCx<'_, S>) -> Result<Option<Self::Bucket>, Error> {
        let source_layer = cx.source_layer()?;
        let mut bucket = TestBucket {
            vertices: GeometryVec::default(),
            triangles: GeometryVec::default(),
            segments: Vec::new(),
            binders: cx.binders(),
        };
        for &i in &self.features {
            let feature = source_layer.feature(i)?;
            let geometry = feature.geometry()?;
            let points = geometry.iter().flatten().copied().collect::<Vec<Point<i16>>>();
            let mut triangles = Indexed { indexes: bucket.triangles.get_mut(), segments: &mut bucket.segments };
            add_point_quads(bucket.vertices.get_mut(), &mut triangles, points, |pos| TestVertex { pos });
            cx.populate(&mut bucket.binders, i, &feature, bucket.vertices.len(), None, None);
            cx.index(i, &geometry);
        }
        Ok((!bucket.segments.is_empty()).then_some(bucket))
    }
}

/// The tests' render half of the geometry layer type `S`.
pub(crate) struct TestFeatures<S>(PhantomData<fn() -> S>);

impl<S: TestGeometry> Layer for TestFeatures<S> {
    type Style = S;
    type Input = Features<TestLayout<S>>;
    type Tile = Mesh;
    const TERRAIN: OnTerrain = S::TERRAIN;
    const VOLUME: bool = S::VOLUME;

    fn new(_values: &LayerValues<S>) -> Self {
        TestFeatures(PhantomData)
    }

    fn evaluate(&mut self, cx: EvaluateCx<'_, Self>) -> Evaluation {
        Evaluation { drapes_by_bearing: S::drapes_by_bearing(&cx.evaluated.paint), ..Evaluation::TRANSLUCENT }
    }

    fn tile(
        &mut self,
        cx: &mut TileCx<'_, '_, Self>,
        _tile: FrameTile<'_, Self>,
        data: TileData<'_, Self>,
        _previous: Option<Mesh>,
    ) -> Option<Mesh> {
        let bucket = data.bucket;
        let geometry = data.geometry();
        let (vertices, indexes) =
            (bucket_vertices(geometry, &bucket.vertices)?, bucket_indexes(geometry, &bucket.triangles)?);
        let mut attrs = VertexAttributeArray::new();
        attrs.set_shared(
            "pos",
            SharedAttribute::new(vertices, AttributeDataType::Short2, 0, size_of::<TestVertex>() as u32),
        );
        let geometry = DrawGeometry {
            vertices: attrs,
            vertex_count: bucket.vertices.len(),
            mode: DrawMode::Triangles(),
            indexes: indexes.into(),
            segments: bucket.segments.iter().map(SegmentBase::copy).collect(),
            instances: None,
        };
        let shader = cx.res.context.shader_key(&TEST, Uniforms::default(), cx.base.projection_variant);
        Some(cx.res.mesh(shader, ColorMode::alpha_blended(), CullFaceMode::disabled(), geometry, Textures::default()))
    }

    fn prepare(&mut self, cx: &mut PrepareCx<'_, '_, Self>, tiles: &[FrameTile<'_, Self>], out: &mut Draws) {
        for tile in tiles {
            if let Some(mut entry) = cx.tiles.entry(&tile.id()) {
                entry.take_changes();
            }
        }
        let ids: Vec<_> = tiles.iter().map(|tile| tile.id()).collect();
        push_draws(cx, &ids, out);
    }
}

/// The tests' render half of the raster layer type.
pub(crate) struct TestRaster;

impl Layer for TestRaster {
    type Style = Raster;
    type Input = RasterTiles;
    type Tile = Mesh;
    const TERRAIN: OnTerrain = OnTerrain::Drapes;

    fn new(_values: &LayerValues<Raster>) -> Self {
        TestRaster
    }

    fn tile(
        &mut self,
        cx: &mut TileCx<'_, '_, Self>,
        _tile: FrameTile<'_, Self>,
        _data: TileData<'_, Self>,
        _previous: Option<Mesh>,
    ) -> Option<Mesh> {
        Some(square(cx))
    }

    fn prepare(&mut self, cx: &mut PrepareCx<'_, '_, Self>, tiles: &[FrameTile<'_, Self>], out: &mut Draws) {
        let ids: Vec<_> = tiles.iter().map(|tile| tile.id()).collect();
        push_draws(cx, &ids, out);
    }
}

/// A raster-dem layer type whose render half the tests give.
pub(crate) trait TestDemStyle: StyleLayerType + Sized {
    /// Whether the terrain's surface shades the layer's drapes by the map's
    /// bearing with `layer`'s values.
    fn lit_by_bearing(_layer: &EvaluatedLayer<Self>) -> bool {
        false
    }
}

/// A hillshade lit from the viewport.
impl TestDemStyle for Hillshade {
    fn lit_by_bearing(layer: &EvaluatedLayer<Self>) -> bool {
        *layer.paint.hillshade_illumination_anchor.uniform() == HillshadeIlluminationAnchorType::Viewport
    }
}

impl TestDemStyle for ColorRelief {}

/// The tests' render half of the raster-dem layer type `S`: a layer lit
/// by the bearing has the terrain's surface shade its drapes with a light
/// of the frame's bearing.
pub(crate) struct TestDem<S>(PhantomData<fn() -> S>);

impl<S: TestDemStyle> Layer for TestDem<S> {
    type Style = S;
    type Input = DemTiles;
    type Tile = Mesh;
    const TERRAIN: OnTerrain = OnTerrain::Drapes;

    fn new(_values: &LayerValues<S>) -> Self {
        TestDem(PhantomData)
    }

    fn surface_light(&mut self, values: &LayerValues<S>, frame: &LayerFrame<'_>) -> Option<SurfaceLight> {
        let mut light = SurfaceLight::default();
        light.props.azimuths[0] = frame.view().angle() as f32;
        S::lit_by_bearing(&values.evaluated).then_some(light)
    }

    fn evaluate(&mut self, cx: EvaluateCx<'_, Self>) -> Evaluation {
        Evaluation { drapes_by_bearing: S::lit_by_bearing(cx.evaluated), ..Evaluation::TRANSLUCENT }
    }

    fn tile(
        &mut self,
        cx: &mut TileCx<'_, '_, Self>,
        _tile: FrameTile<'_, Self>,
        dem: TileData<'_, Self>,
        _previous: Option<Mesh>,
    ) -> Option<Mesh> {
        dem.has_data().then(|| square(cx))
    }

    fn prepare(&mut self, cx: &mut PrepareCx<'_, '_, Self>, tiles: &[FrameTile<'_, Self>], out: &mut Draws) {
        let ids: Vec<_> = tiles.iter().map(|tile| tile.id()).collect();
        push_draws(cx, &ids, out);
    }
}

/// The tests' render half of the background layer type: the tiles that
/// cover the view.
pub(crate) struct TestBackground;

impl Layer for TestBackground {
    type Style = Background;
    type Input = NoTiles;
    type Tile = Mesh;
    const TERRAIN: OnTerrain = OnTerrain::Drapes;
    const PICKS_TILES: bool = true;

    fn new(_values: &LayerValues<Background>) -> Self {
        TestBackground
    }

    fn update(&mut self, mut cx: UpdateCx<'_, '_, Self>, _tiles: &[FrameTile<'_, Self>]) {
        let (view, update) = (cx.parameters.view, cx.parameters.update_parameters);
        let zoom = view.integer_zoom();
        let tiles = tile_cover(
            &TileCoverParameters {
                view: view.clone(),
                tile_lod_min_radius: update.tile_lod_min_radius,
                tile_lod_scale: update.tile_lod_scale,
                tile_lod_pitch_threshold: update.tile_lod_pitch_threshold,
                tile_lod_mode: update.tile_lod_mode,
                requested_zoom: None,
                round_zoom: false,
                max_content_elevation: 0.0,
            },
            zoom,
            Range::new(0u8, zoom),
            None,
        );
        cx.pick_tiles(tiles);
    }

    fn prepare(&mut self, cx: &mut PrepareCx<'_, '_, Self>, _tiles: &[FrameTile<'_, Self>], out: &mut Draws) {
        let picked = cx.picked.to_vec();
        for &tile_id in &picked {
            let Some(entry) = cx.tiles.entry(&tile_id) else { continue };
            if entry.value.is_none() {
                let vertices = RenderStaticData::tile_vertices();
                let count = vertices.len();
                let geometry = DrawGeometry {
                    vertices: raw_vertices(
                        "pos",
                        bytemuck::cast_slice(&vertices).to_vec(),
                        count,
                        AttributeDataType::Short2,
                    ),
                    vertex_count: count,
                    mode: DrawMode::Triangles(),
                    indexes: DrawIndexes::new(RenderStaticData::quad_triangle_indices()),
                    segments: RenderStaticData::tile_triangle_segments().iter().map(SegmentBase::copy).collect(),
                    instances: None,
                };
                let context = &mut *cx.res.context;
                let shader = context.shader_key(&TEST, Uniforms::default(), cx.base.projection_variant);
                let mesh = Mesh::new(
                    context,
                    shader,
                    ColorMode::alpha_blended(),
                    CullFaceMode::disabled(),
                    geometry,
                    Textures::default(),
                );
                *entry.value = Some(mesh);
            }
        }
        push_draws(cx, &picked, out);
    }
}