mln-rs-engine 0.0.18

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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
use std::mem::offset_of;
use std::rc::Rc;
use std::sync::Arc;

use mln_gfx::gpu::{Gpu, TextureFormat};
use mln_gfx::shaders::bindings::PROJECTION_ID;
use mln_gfx::shaders::layer_ubo::ProjectionUBO;
use mln_gfx::texture2d::SamplerState;
use mln_gfx::types::{TextureFilterType, TextureWrapType};
use mln_gfx::{AttributeDataType, ColorMode, CullFaceMode, DrawMode};
use mln_style::bitmap::PremultipliedImage;
use mln_style::color::Color;
use mln_style::error::Error;
use mln_style::expression::{Dependency, EvaluationContext, GlobalStateMap, global_state_refs_intersect};
use mln_style::layer::LayerStyle;
use mln_style::layers::Line;
use mln_style::layers::line_layer_properties::LinePaintProperties;
use mln_style::size::Size;
use mln_style::types::{AlignmentType, LayerBlendType, LineCapType, TranslateAnchorType};
use mln_style::{PropertyEvaluationParameters, StyleValue as _};

use super::{cast_matrix, refresh_paint_attributes};
use crate::buckets::line_bucket::{LineBinders, LineBucket};
use crate::draw::atlas_binding::AtlasBinding;
use crate::draw::context::Context;
use crate::draw::identity::SimpleIdentity;
use crate::draw::objects::{DrawGeometry, DrawObjects, Textures};
use crate::draw::segment::SegmentBase;
use crate::draw::store::SharedTexture;
use crate::draw::texture_size;
use crate::draw::texture2d::Texture2D;
use crate::draw::vertex_attribute::{SharedAttribute, Uniforms, VertexAttributeArray};
use crate::gfx::line_mesh::LineLayoutVertex;
use crate::layer::bucket_geometry::{bucket_indexes, bucket_vertices, paint_vertices};
use crate::layer::layer_drawing::{DrawList, DrawObjectsMut, FrameDraw, TileDraws, TileObjects, UniformWriter};
use crate::layer::layer_type::{EvaluatedLayer, LayerType};
use crate::layer::layout::{LayoutParameters, SourceLayer};
use crate::layer::render_layer::{
    Atlases, LayerBucket, LayerUpdateParameters, LayerValues, Parts, PartsMut, RenderLayer, RenderLayerBase, TypedLayer,
};
use crate::layers::line::programs;
use crate::layers::line::ubo::{
    LineDrawableUBO, LineDrawableUnionUBO, LineEvaluatedPropsUBO, LineGradientDrawableUBO, LinePatternDrawableUBO,
    LinePatternTilePropsUBO, LineSDFDrawableUBO, LineSDFTilePropsUBO, LineTilePropsUnionUBO,
};
use crate::layout::pattern_layout::{LineLayoutSpec, PatternLayout, uses_pattern};
use crate::line_atlas::{LineAtlas, LinePatternCap};
use crate::query::feature_index::QueryRegion;
use crate::render::layer_composite::Composite;
use crate::render::paint_parameters::PaintParameters;
use crate::render::pass::RenderPass;
use crate::render::tile::FrameTile;
use crate::render::tile_matrix::{projection_data, to_projection_ubo};
use crate::tile::UnwrappedTileID;

/// The ids of the buffers that the draws bind.
const DRAWABLE_ID: usize = programs::LINE.buffer_id("drawableVector");
const TILE_PROPS_ID: usize = programs::LINE_PATTERN.buffer_id("tilePropsVector");
const PROPS_ID: usize = programs::LINE.buffer_id("props");

/// The slots of the programs' textures that the draws bind.
const DASH_SLOT: usize = programs::LINE_GRADIENT_SDF.texture_slot("image_dash");
const IMAGE_SLOT: usize = programs::LINE_PATTERN.texture_slot("image");

/// What a line draws: its shader and the uniforms it reads.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum LineType {
    Simple,
    Gradient,
    Pattern,
    /// Dashes.
    Sdf,
    /// Dashes over a gradient.
    GradientSdf,
}

/// What the layer keeps of a tile: the draw of its bucket's lines, as it was
/// built for the bucket.
pub(crate) struct LineTile {
    /// The id of the draw's GPU objects, made with them.
    id: SimpleIdentity,
    objects: DrawObjects,
    line_type: LineType,
    /// The passes the draw draws in.
    pass: RenderPass,
    /// The ends of the dashes of the bucket, which pick its pattern in the
    /// line atlas.
    dash_cap: LinePatternCap,
    /// Gives a pattern draw the texture of the tile's icon atlas.
    atlases: Option<AtlasBinding>,
}

impl TileObjects for LineTile {
    type Part = ();

    fn objects(&mut self, (): ()) -> Option<DrawObjectsMut<'_>> {
        Some(DrawObjectsMut { objects: &mut self.objects, atlases: self.atlases.as_ref() })
    }

    fn drop_bindings(&mut self) {
        self.objects.drop_bindings();
    }
}

/// A draw's entries in the layer's storage buffers.
struct LineUniforms {
    draw_ubo: LineDrawableUnionUBO,
    tile_props: LineTilePropsUnionUBO,
    projection: ProjectionUBO,
}

/// Where a dash draw samples its patterns. Where the dash array differs by
/// feature, the shaders divide the scales' x and the gamma by each pattern's
/// length.
struct SdfPatterns {
    patternscale_a: [f32; 2],
    patternscale_b: [f32; 2],
    tex_y_a: f32,
    tex_y_b: f32,
    atlas_height: f32,
    sdfgamma: f32,
}

fn cast_tlbr(a: [u16; 4]) -> [f32; 4] {
    a.map(f32::from)
}

/// The ends of a bucket's dashes. A dash pattern is shared by the whole
/// bucket, so a `line-cap` that varies by feature gets square dashes; each
/// line still gets its own cap at its ends.
fn dash_pattern_cap(bucket: &LineBucket) -> LinePatternCap {
    if bucket.layout.line_cap.constant_or(&LineCapType::Butt) == LineCapType::Round {
        LinePatternCap::Round
    } else {
        LinePatternCap::Square
    }
}

pub(crate) fn apply_color_ramp(
    color_value: &mln_style::property::color_ramp::ColorRampPropertyValue,
    image: &mut PremultipliedImage,
    global_state: Option<&GlobalStateMap>,
) -> bool {
    if color_value.is_undefined() {
        return false;
    }

    let length = image.bytes();

    let mut i: u32 = 0;
    while (i as usize) < length {
        let color = color_value.evaluate_ramp(f64::from(i) / length as f64, global_state);
        let i_ = i as usize;
        image.data[i_] = (color.r * 255.0).floor() as u8;
        image.data[i_ + 1] = (color.g * 255.0).floor() as u8;
        image.data[i_ + 2] = (color.b * 255.0).floor() as u8;
        image.data[i_ + 3] = (color.a * 255.0).floor() as u8;
        i += 4;
    }
    true
}

/// A texture of the bucket's dash patterns, for a `line-dasharray` that
/// differs by feature.
fn dash_atlas_texture(gpu: &mut dyn Gpu, context: &mut Context, binders: &LineBinders) -> Option<SharedTexture> {
    let image = binders.line_dasharray.atlas()?.image();
    if !image.valid() {
        return None;
    }
    let texture = context.add_texture_with(|texture| {
        texture.set_format(TextureFormat::A8Unorm);
        texture.set_sampler(SamplerState {
            filter: TextureFilterType::Linear,
            wrap_u: TextureWrapType::Repeat,
            wrap_v: TextureWrapType::Clamp,
            ..Default::default()
        });
    });
    if let Err(error) = context.upload_texture_data(gpu, &texture, &image.data, texture_size(image.size)) {
        context.fail(format!("Uploading the dash patterns failed: {error}"));
        return None;
    }
    Some(texture)
}

impl LayerType for Line {
    type Layout = PatternLayout<LineLayoutSpec>;
    type Bucket = LineBucket;
    type TileBucket<'a> = LayerBucket<'a, Line>;
    const CROSSFADES: bool = true;

    /// Whether a layer of the group has a pattern.
    fn layout_can_need_dependencies(group: &[Arc<EvaluatedLayer<Line>>]) -> bool {
        uses_pattern::<LineLayoutSpec>(group)
    }

    fn create_layout(
        group: &[Arc<EvaluatedLayer<Line>>],
        parameters: &mut LayoutParameters<'_>,
        source_layer: SourceLayer,
    ) -> Result<Option<PatternLayout<LineLayoutSpec>>, Error> {
        PatternLayout::new(parameters, group, source_layer).map(Some)
    }

    fn query_radius(bucket: &LineBucket, layer: &EvaluatedLayer<Line>) -> f32 {
        bucket.query_radius(layer)
    }

    fn render_layer(style: Arc<LayerStyle<Line>>) -> Box<dyn RenderLayer> {
        Box::new(RenderLineLayer::new(style))
    }
}

pub(crate) struct RenderLineLayer {
    base: RenderLayerBase,
    values: LayerValues<Line>,
    drawing: TileDraws<LineTile>,
    /// The frame's draws as the update makes them.
    draw_list: DrawList<LineUniforms>,

    color_ramp: Rc<PremultipliedImage>,
    /// The global-state snapshot used to generate the cached color ramp and
    /// detect when it needs to be rebuilt.
    color_ramp_global_state: Option<Arc<GlobalStateMap>>,
    /// The color ramp's texture, from the first draw that reads it until
    /// the ramp changes. A draw that reads another is built again.
    color_ramp_texture: Option<SharedTexture>,
}

impl RenderLineLayer {
    pub(crate) fn new(impl_: Arc<LayerStyle<Line>>) -> Self {
        let (base, values) = LayerValues::new(impl_);
        RenderLineLayer {
            base,
            values,
            drawing: TileDraws::default(),
            draw_list: DrawList::default(),
            color_ramp: Rc::new(PremultipliedImage::new(Size::new(256, 1))),
            color_ramp_global_state: None,
            color_ramp_texture: None,
        }
    }

    fn update_color_ramp(&mut self) {
        let color_value = self.values.unevaluated.line_gradient.get_value().clone();
        let mut image = (*self.color_ramp).clone();
        if !apply_color_ramp(&color_value, &mut image, self.color_ramp_global_state.as_deref()) {
            return;
        }
        // The texture shares the ramp image, so a new one replaces it.
        self.color_ramp = Rc::new(image);

        self.color_ramp_texture = None;
    }

    // query
    fn line_width(
        &self,
        feature: &dyn mln_style::feature::GeometryTileFeature,
        zoom: f32,
        feature_state: &mln_style::feature::FeatureState,
    ) -> f32 {
        let evaluated = &self.values.evaluated.paint;
        let line_width = evaluated.line_width.evaluate(&EvaluationContext::with_zoom_feature_state(
            zoom,
            Some(feature),
            Some(feature_state),
        ));
        let gap_width = evaluated.line_gap_width.evaluate(&EvaluationContext::with_zoom_feature_state(
            zoom,
            Some(feature),
            Some(feature_state),
        ));
        if gap_width == 0.0 { line_width } else { gap_width + 2.0 * line_width }
    }
}

// query
fn offset_line(
    rings: &mln_style::feature::GeometryCollection,
    offset: f64,
) -> Vec<mln_style::feature::GeometryCoordinates> {
    use geometry::Point;

    use crate::geo::math::{perp, unit};

    debug_assert!(offset != 0.0);
    debug_assert!(!rings.is_empty());

    let to_f64 = |p: Point<i16>| Point::new(f64::from(p.x), f64::from(p.y));
    let sub = |a: Point<i16>, b: Point<i16>| Point::new(a.x.wrapping_sub(b.x), a.y.wrapping_sub(b.y));

    let zero = Point::new(0.0, 0.0);
    let mut new_rings = Vec::with_capacity(rings.len());
    for ring in rings.iter() {
        let mut new_ring = Vec::with_capacity(ring.len());

        for i in 0..ring.len() {
            let p = ring[i];

            let a_to_b = if i == 0 { zero } else { perp(unit(to_f64(sub(p, ring[i - 1])))) };
            // At the last vertex, use the incoming normal so the endpoint receives the full offset.
            let b_to_c = if i + 1 == ring.len() { a_to_b } else { perp(unit(to_f64(sub(ring[i + 1], p)))) };
            let mut extrude = unit(Point::new(a_to_b.x + b_to_c.x, a_to_b.y + b_to_c.y));

            let cos_half_angle = extrude.x * b_to_c.x + extrude.y * b_to_c.y;
            let scale = if cos_half_angle == 0.0 { 0.0 } else { 1.0 / cos_half_angle };
            extrude = Point::new(extrude.x * scale, extrude.y * scale);

            // The offset rounds toward zero to tile units, which wrap.
            let shifted = Point::new((extrude.x * offset) as i16, (extrude.y * offset) as i16);
            new_ring.push(Point::new(shifted.x.wrapping_add(p.x), shifted.y.wrapping_add(p.y)));
        }
        new_rings.push(new_ring);
    }

    new_rings
}

impl TypedLayer for RenderLineLayer {
    type Type = Line;
    type Drawing = TileDraws<LineTile>;
    const DRAPES: bool = true;

    fn parts(&self) -> Parts<'_, Self> {
        Parts { base: &self.base, values: &self.values, drawing: &self.drawing }
    }

    fn parts_mut(&mut self) -> PartsMut<'_, Self> {
        PartsMut { base: &mut self.base, values: &mut self.values, drawing: &mut self.drawing }
    }

    fn complete_evaluation(
        &mut self,
        parameters: &PropertyEvaluationParameters,
        _evaluated: &mut EvaluatedLayer<Line>,
    ) {
        // Build the ramp here rather than in transition(), which has no global
        // state. A new or changed layer always needs one; otherwise only a
        // change to a state key that the ramp reads does.
        let global_state_differs = match (&self.color_ramp_global_state, &parameters.global_state) {
            (None, None) => false,
            (Some(a), Some(b)) => !Arc::ptr_eq(a, b),
            _ => true,
        };
        let ramp_value = self.values.unevaluated.line_gradient.get_value();
        let ramp_affected = parameters.layer_changed
            || (global_state_differs
                && ramp_value.get_dependencies().intersects(Dependency::GLOBAL_STATE)
                && global_state_refs_intersect(
                    ramp_value.get_global_state_refs(),
                    parameters.changed_global_state_keys.as_deref(),
                ));
        self.color_ramp_global_state = parameters.global_state.clone();
        if ramp_affected {
            self.update_color_ramp();
        }
    }

    fn drapes_by_bearing(&self, evaluated: &EvaluatedLayer<Line>) -> bool {
        let paint = &evaluated.paint;
        *paint.line_translate_anchor.uniform() == TranslateAnchorType::Viewport
            && *paint.line_translate.uniform() != [0.0, 0.0]
    }

    fn blends(&self, evaluated: &EvaluatedLayer<Line>) -> bool {
        *evaluated.paint.line_layer_blend.uniform() != LayerBlendType::Normal
    }

    fn passes(&self, evaluated: &EvaluatedLayer<Line>) -> RenderPass {
        let evaluated = &evaluated.paint;
        if *evaluated.line_layer_opacity.uniform() > 0.0
            && evaluated.line_opacity.constant_or(&1.0) > 0.0
            && evaluated.line_color.constant_or(&Color::black()).a > 0.0
            && evaluated.line_width.constant_or(&1.0) > 0.0
        {
            RenderPass::Translucent
        } else {
            RenderPass::None
        }
    }

    /// Makes the frame's draws of the layer, those of each of its tiles,
    /// which it builds for a tile it keeps none of, and writes the layer's
    /// buffers they read.
    fn update_draws(
        &mut self,
        gpu: &mut dyn Gpu,
        paint_parameters: &mut PaintParameters<'_>,
        atlases: &Atlases,
        render_tiles: &[FrameTile<'_, Line>],
    ) {
        let frame = self.drawing.frame_mut();
        let paint = &self.values.evaluated.paint;
        frame.set_composite(Composite::of(*paint.line_layer_opacity.uniform(), *paint.line_layer_blend.uniform()));
        frame.set_stencil_tiles(render_tiles.iter().map(|tile| tile.draw_id(paint_parameters).to_unwrapped()));

        let render_pass = RenderPass(self.values.evaluated.render_passes & !RenderPass::Opaque.underlying_type());
        for tile in render_tiles {
            let tile_id = tile.draw_id(paint_parameters);

            let Some(render_data) = tile.bucket_for_pass(render_pass) else {
                self.drawing.tiles_mut().drop_value(&tile_id);
                continue;
            };
            let bucket = render_data.bucket;
            if bucket.triangles.is_empty() {
                self.drawing.tiles_mut().drop_value(&tile_id);
                continue;
            }
            let Some(binders) = bucket.paint_property_binders.get(&self.base.style.base().id) else {
                continue;
            };
            let evaluated = &render_data.layer.paint;

            let Some(mut entry) = self.drawing.tiles_mut().entry(&tile_id) else {
                continue;
            };
            if entry.value.as_ref().is_some_and(|cached| {
                cached.pass != render_pass
                    || matches!(cached.line_type, LineType::Gradient | LineType::GradientSdf)
                        && cached.objects.texture(IMAGE_SLOT) != self.color_ramp_texture.as_ref()
            }) {
                // The tile's draw was built for other passes, or reads a color
                // ramp the layer replaced.
                *entry.value = None;
            }
            // A draw built from another bucket goes.
            entry.set_bucket(bucket.base.bucket_id);

            // The draw's attributes read the bucket's vertices in the
            // context's store, which upload again when they change.
            let changes = entry.take_changes();
            let context = &mut *paint_parameters.context;
            let cached = if let Some(cached) = entry.value {
                refresh_paint_attributes(&mut cached.objects, changes, |attrs| {
                    let buffer = &binders.interleaved_vertex_buffer;
                    let paint =
                        paint_vertices(context, render_data.geometry(), buffer, binders.depends_on_feature_state());
                    binders.bind(evaluated, paint.as_ref(), attrs, &mut Uniforms::default());
                });
                cached
            } else {
                let built = self.build_tile(gpu, context, render_data, binders, evaluated, render_pass);
                let Some(cached) = built.and_then(|built| self.drawing.tiles_mut().insert(&tile_id, built).ok()) else {
                    continue;
                };
                cached
            };
            if let Some(uniforms) = Self::uniforms(
                &self.values.evaluated,
                tile_id.to_unwrapped(),
                render_data,
                binders,
                cached,
                paint_parameters,
                &atlases.line,
            ) {
                let draw = FrameDraw { pass: cached.pass, clip: true, ..FrameDraw::new(Some(tile_id), cached.id) };
                self.draw_list.push(draw, uniforms);
            }
        }

        let layer = &self.values.evaluated;
        self.drawing.finish(&mut self.draw_list, gpu, paint_parameters.context, |writer| {
            Self::write_uniforms(layer, writer);
        });
    }

    /// Adds the dash patterns of the tiles' buckets to the line atlas, then
    /// the layer's own, which the encode draws a dashed tile with when its
    /// bucket has no per-feature patterns.
    fn update(&mut self, params: &mut LayerUpdateParameters<'_>, tiles: &[FrameTile<'_, Line>]) {
        for tile in tiles {
            let Some(bucket) = tile.bucket() else {
                continue;
            };
            let dasharray = bucket.layer.paint.line_dasharray.faded();
            if dasharray.from.is_empty() {
                continue;
            }
            let cap = dash_pattern_cap(bucket.bucket);
            params.atlases.line.add(dasharray.from, dasharray.to, cap);
        }
        // A dashed draw asks for the layer's pattern even where it is empty.
        let layer_dasharray = self.values.evaluated.paint.line_dasharray.faded();
        for tile in tiles {
            let Some(bucket) = tile.bucket() else {
                continue;
            };
            let laid_out = &bucket.layer.paint.line_dasharray;
            let dashed = !laid_out.is_constant() || !laid_out.faded().from.is_empty();
            let per_feature = bucket
                .bucket
                .paint_property_binders
                .get(&self.base.style.base().id)
                .is_some_and(|binders| binders.line_dasharray.atlas().is_some());
            if dashed && !per_feature {
                let cap = dash_pattern_cap(bucket.bucket);
                params.atlases.line.add(layer_dasharray.from, layer_dasharray.to, cap);
            }
        }
    }
}

impl RenderLayer for RenderLineLayer {
    // query
    fn query_intersects_feature(
        &self,
        region: &QueryRegion<'_>,
        feature: &dyn mln_style::feature::GeometryTileFeature,
        geometry: &mln_style::feature::GeometryCollection,
        feature_state: &mln_style::feature::FeatureState,
    ) -> bool {
        let QueryRegion { geometry: query_geometry, view, pixels_to_tile_units, .. } = *region;
        let zoom = view.zoom() as f32;
        let evaluated = &self.values.evaluated.paint;
        // Translate query geometry
        let translated_query_geometry = crate::query::feature_index::FeatureIndex::translate_query_geometry(
            query_geometry,
            *evaluated.line_translate.uniform(),
            *evaluated.line_translate_anchor.uniform(),
            view.angle() as f32,
            pixels_to_tile_units,
        );
        let query = translated_query_geometry.as_ref().unwrap_or(query_geometry);

        // Evaluate function
        let offset = evaluated.line_offset.evaluate(&EvaluationContext::with_zoom_feature_state(
            zoom,
            Some(feature),
            Some(feature_state),
        )) * pixels_to_tile_units;
        // Test intersection
        let half_width =
            (f64::from(self.line_width(feature, zoom, feature_state)) / 2.0 * f64::from(pixels_to_tile_units)) as f32;

        // Apply offset to geometry
        if offset != 0.0 && !geometry.is_empty() {
            return crate::geo::intersection_tests::polygon_intersects_buffered_multi_line(
                query,
                &offset_line(geometry, f64::from(offset)),
                half_width,
            );
        }

        crate::geo::intersection_tests::polygon_intersects_buffered_multi_line(query, geometry, half_width)
    }
}

impl RenderLineLayer {
    /// The draw of the tile's `bucket`, laid out with `evaluated`, in
    /// `pass`, if the layer has what it draws with.
    fn build_tile(
        &mut self,
        gpu: &mut dyn Gpu,
        context: &mut Context,
        tile_bucket: LayerBucket<'_, Line>,
        binders: &LineBinders,
        evaluated: &LinePaintProperties::PossiblyEvaluated,
        pass: RenderPass,
    ) -> Option<LineTile> {
        let (bucket, geometry) = (tile_bucket.bucket, tile_bucket.geometry());
        let buffer = &binders.interleaved_vertex_buffer;
        let paint = paint_vertices(context, geometry, buffer, binders.depends_on_feature_state());
        let mut uniforms = Uniforms::default();
        let mut attrs = VertexAttributeArray::new();
        binders.bind(evaluated, paint.as_ref(), &mut attrs, &mut uniforms);

        let has_dasharray =
            !evaluated.line_dasharray.is_constant() || !evaluated.line_dasharray.faded().from.is_empty();
        // A constant dash array's texture comes from the line atlas, with the
        // draw's uniforms.
        let dash_texture = if evaluated.line_dasharray.is_constant() {
            None
        } else {
            Some(dash_atlas_texture(gpu, context, binders)?)
        };
        let has_gradient = !self.values.unevaluated.line_gradient.get_value().is_undefined();

        let mut textures = Textures::default();
        let mut atlases = None;
        let (line_type, program) = if has_dasharray && has_gradient {
            textures[IMAGE_SLOT] = Some(self.color_ramp_texture(context)?);
            textures[DASH_SLOT] = dash_texture;
            (LineType::GradientSdf, &programs::LINE_GRADIENT_SDF)
        } else if has_dasharray {
            textures[IMAGE_SLOT] = dash_texture;
            (LineType::Sdf, &programs::LINE_SDF)
        } else if !self.values.unevaluated.line_pattern.is_undefined() {
            let icons = Some(tile_bucket.atlas_textures()).filter(|atlases| atlases.icon().is_some())?;
            atlases = Some(AtlasBinding::new(
                Some(icons.clone()),
                None,
                Some(IMAGE_SLOT),
                /*is_text*/ false,
                /*sdf_icons*/ true, // to force linear filter
                AlignmentType::Auto,
                /*icon_scaled*/ false,
                /*text_size_is_zoom_constant*/ false,
            ));
            (LineType::Pattern, &programs::LINE_PATTERN)
        } else if has_gradient {
            textures[IMAGE_SLOT] = Some(self.color_ramp_texture(context)?);
            (LineType::Gradient, &programs::LINE_GRADIENT)
        } else {
            (LineType::Simple, &programs::LINE)
        };
        let shader = context.shader_key(program, uniforms, self.base.projection_variant);
        if let Some(atlases) = &atlases {
            // A pattern binds the atlas's texture and no other.
            textures = atlases.textures(&shader);
        }

        let shared = bucket_vertices(geometry, &bucket.vertices)?;
        let stride = size_of::<LineLayoutVertex>() as u32;
        attrs.set_shared(
            "pos_normal",
            SharedAttribute::new(
                shared.clone(),
                AttributeDataType::Short2,
                offset_of!(LineLayoutVertex, a1) as u32,
                stride,
            ),
        );
        attrs.set_shared(
            "data",
            SharedAttribute::new(shared, AttributeDataType::UByte4, offset_of!(LineLayoutVertex, a2) as u32, stride),
        );

        let draw_geometry = DrawGeometry {
            vertices: attrs,
            vertex_count: if bucket.segments.is_empty() { 0 } else { bucket.vertices.len() },
            mode: DrawMode::Triangles(),
            indexes: bucket_indexes(geometry, &bucket.triangles)?.into(),
            segments: bucket.segments.iter().map(SegmentBase::copy).collect(),
            instances: None,
        };
        let color_mode =
            if pass == RenderPass::Translucent { ColorMode::alpha_blended() } else { ColorMode::unblended() };
        let objects = DrawObjects::new(shader, color_mode, CullFaceMode::disabled(), draw_geometry, textures);
        Some(LineTile {
            id: SimpleIdentity::new(),
            objects,
            line_type,
            pass,
            dash_cap: dash_pattern_cap(bucket),
            atlases,
        })
    }

    /// The texture of the color ramp, which every gradient of the layer
    /// shares, if the ramp is valid.
    fn color_ramp_texture(&mut self, context: &mut Context) -> Option<SharedTexture> {
        if self.color_ramp_texture.is_none() && self.color_ramp.valid() {
            self.color_ramp_texture = Some(context.add_texture_with(|texture| {
                texture.set_image(Some(Rc::clone(&self.color_ramp)));
                texture.set_sampler(SamplerState {
                    filter: TextureFilterType::Linear,
                    wrap_u: TextureWrapType::Clamp,
                    wrap_v: TextureWrapType::Clamp,
                    ..Default::default()
                });
            }));
        }
        self.color_ramp_texture.clone()
    }

    /// The entries in the layer's storage buffers of `cached`, the draw of
    /// the tile `tile_id` of `tile_bucket`, whose binders are `binders`, with
    /// the layer's values
    /// `layer`: its matrix, the interpolation of the layer's values, and
    /// where its patterns are. A dashed draw of the line atlas's pattern
    /// takes the atlas's texture here; `None` where it has none yet, and
    /// doesn't draw.
    fn uniforms(
        layer: &EvaluatedLayer<Line>,
        tile_id: UnwrappedTileID,
        tile_bucket: LayerBucket<'_, Line>,
        binders: &LineBinders,
        cached: &mut LineTile,
        parameters: &PaintParameters<'_>,
        line_atlas: &LineAtlas,
    ) -> Option<LineUniforms> {
        const NEAR_CLIPPED: bool = false;
        const IN_VIEWPORT_PIXEL_UNITS: bool = false;
        let evaluated = &layer.paint;
        let crossfade = layer.crossfade;
        let state = parameters.view;
        let zoom = state.zoom() as f32;
        let int_zoom = state.integer_zoom();
        let pixel_scale = state.projection().pixel_scale(state) as f32;

        let projection = projection_data(
            &tile_id,
            parameters,
            *evaluated.line_translate.uniform(),
            *evaluated.line_translate_anchor.uniform(),
            NEAR_CLIPPED,
            IN_VIEWPORT_PIXEL_UNITS,
            None,
            false,
        );
        let matrix = cast_matrix(&projection.fallback_matrix);
        let mut uniforms = LineUniforms {
            draw_ubo: LineDrawableUnionUBO::default(),
            tile_props: LineTilePropsUnionUBO::default(),
            projection: to_projection_ubo(&projection),
        };
        let ratio = pixel_scale / tile_id.pixels_to_tile_units(1.0, zoom);

        match cached.line_type {
            LineType::Simple => uniforms.draw_ubo.set(&LineDrawableUBO {
                matrix,
                ratio,
                color_t: binders.line_color.interpolation_factor(zoom),
                blur_t: binders.line_blur.interpolation_factor(zoom),
                opacity_t: binders.line_opacity.interpolation_factor(zoom),
                gapwidth_t: binders.line_gap_width.interpolation_factor(zoom),
                offset_t: binders.line_offset.interpolation_factor(zoom),
                width_t: binders.line_width.interpolation_factor(zoom),
                pad1: 0.0,
            }),
            LineType::Gradient => uniforms.draw_ubo.set(&LineGradientDrawableUBO {
                matrix,
                ratio,
                blur_t: binders.line_blur.interpolation_factor(zoom),
                opacity_t: binders.line_opacity.interpolation_factor(zoom),
                gapwidth_t: binders.line_gap_width.interpolation_factor(zoom),
                offset_t: binders.line_offset.interpolation_factor(zoom),
                width_t: binders.line_width.interpolation_factor(zoom),
                pad1: 0.0,
                pad2: 0.0,
            }),
            LineType::Pattern => {
                let texture =
                    cached.objects.texture(IMAGE_SLOT).and_then(|texture| parameters.context.texture(texture));
                let texture_size = texture.map(Texture2D::size).unwrap_or_default();
                uniforms.draw_ubo.set(&LinePatternDrawableUBO {
                    matrix,
                    ratio,
                    blur_t: binders.line_blur.interpolation_factor(zoom),
                    opacity_t: binders.line_opacity.interpolation_factor(zoom),
                    gapwidth_t: binders.line_gap_width.interpolation_factor(zoom),
                    offset_t: binders.line_offset.interpolation_factor(zoom),
                    width_t: binders.line_width.interpolation_factor(zoom),
                    pattern_from_t: binders.line_pattern.interpolation_factor(zoom).0,
                    pattern_to_t: binders.line_pattern.interpolation_factor(zoom).1,
                    floorwidth_t: binders.line_floor_width.interpolation_factor(zoom),
                    pad1: 0.0,
                    pad2: 0.0,
                    pad3: 0.0,
                });
                let pattern = evaluated.line_pattern.faded();
                let tlbr = |id: &str| tile_bucket.pattern(id).map_or([0.0; 4], |position| cast_tlbr(position.tlbr()));
                uniforms.tile_props.set(&LinePatternTilePropsUBO {
                    pattern_from: tlbr(pattern.from.id()),
                    pattern_to: tlbr(pattern.to.id()),
                    scale: [
                        parameters.pixel_ratio,
                        1.0 / tile_id.pixels_to_tile_units(1.0, f32::from(int_zoom)),
                        crossfade.from_scale,
                        crossfade.to_scale,
                    ],
                    texsize: [texture_size.width as f32, texture_size.height as f32],
                    fade: crossfade.t,
                    pad1: 0.0,
                });
            }
            LineType::Sdf | LineType::GradientSdf => {
                // The gradient keeps its ramp in the image slot, so the dash
                // pattern takes the second.
                let dash_slot = if cached.line_type == LineType::GradientSdf { DASH_SLOT } else { IMAGE_SLOT };
                let tile_ratio = |scale: f32| 1.0 / tile_id.pixels_to_tile_units(scale, f32::from(int_zoom));
                let sdf = if let Some(atlas) = binders.line_dasharray.atlas() {
                    // Per-feature patterns don't fade from the zoom before:
                    // both samples are of this zoom's pattern, at the
                    // cross-fade's two scales.
                    let atlas_size = atlas.image().size;
                    let min_scale = crossfade.from_scale.min(crossfade.to_scale);
                    SdfPatterns {
                        patternscale_a: [tile_ratio(crossfade.from_scale), 0.0],
                        patternscale_b: [tile_ratio(crossfade.to_scale), 0.0],
                        tex_y_a: 0.0,
                        tex_y_b: 0.0,
                        atlas_height: atlas_size.height as f32,
                        sdfgamma: atlas_size.width as f32 / (min_scale * 256.0 * parameters.pixel_ratio) / 2.0,
                    }
                } else {
                    let dasharray = evaluated.line_dasharray.faded();
                    let dash_pattern = line_atlas.get(dasharray.from, dasharray.to, cached.dash_cap)?;
                    cached.objects.set_texture(dash_slot, Some(dash_pattern.texture()?));

                    let pos_a = dash_pattern.from();
                    let pos_b = dash_pattern.to();
                    let width_a = pos_a.width * crossfade.from_scale;
                    let width_b = pos_b.width * crossfade.to_scale;
                    let min_width = if width_b < width_a { width_b } else { width_a };
                    SdfPatterns {
                        patternscale_a: [tile_ratio(width_a), -pos_a.height / 2.0],
                        patternscale_b: [tile_ratio(width_b), -pos_b.height / 2.0],
                        tex_y_a: pos_a.y,
                        tex_y_b: pos_b.y,
                        atlas_height: 0.0,
                        sdfgamma: dash_pattern.size().width as f32 / (min_width * 256.0 * parameters.pixel_ratio) / 2.0,
                    }
                };

                uniforms.draw_ubo.set(&LineSDFDrawableUBO {
                    matrix,
                    patternscale_a: sdf.patternscale_a,
                    patternscale_b: sdf.patternscale_b,
                    tex_y_a: sdf.tex_y_a,
                    tex_y_b: sdf.tex_y_b,
                    ratio,
                    color_t: binders.line_color.interpolation_factor(zoom),
                    blur_t: binders.line_blur.interpolation_factor(zoom),
                    opacity_t: binders.line_opacity.interpolation_factor(zoom),
                    gapwidth_t: binders.line_gap_width.interpolation_factor(zoom),
                    offset_t: binders.line_offset.interpolation_factor(zoom),
                    width_t: binders.line_width.interpolation_factor(zoom),
                    floorwidth_t: binders.line_floor_width.interpolation_factor(zoom),
                    atlas_height: sdf.atlas_height,
                    pad2: 0.0,
                });
                uniforms.tile_props.set(&LineSDFTilePropsUBO {
                    sdfgamma: sdf.sdfgamma,
                    mix: crossfade.t,
                    pad1: 0.0,
                    pad2: 0.0,
                });
            }
        }
        Some(uniforms)
    }

    /// Writes the layer's buffers of the frame's draws.
    fn write_uniforms(layer: &EvaluatedLayer<Line>, writer: &mut UniformWriter<'_, LineUniforms>) {
        let evaluated = &layer.paint;
        let props = LineEvaluatedPropsUBO {
            color: evaluated.line_color.constant_or(&LinePaintProperties::Defaults::line_color()).to_float_array(),
            blur: evaluated.line_blur.constant_or(&LinePaintProperties::Defaults::line_blur()),
            opacity: evaluated.line_opacity.constant_or(&LinePaintProperties::Defaults::line_opacity()),
            gapwidth: evaluated.line_gap_width.constant_or(&LinePaintProperties::Defaults::line_gap_width()),
            offset: evaluated.line_offset.constant_or(&LinePaintProperties::Defaults::line_offset()),
            width: evaluated.line_width.constant_or(&LinePaintProperties::Defaults::line_width()),
            floorwidth: evaluated.line_floor_width.constant_or(&LinePaintProperties::Defaults::line_floor_width()),
            pad0: 0.0,
            pad1: 0.0,
        };
        writer.uniform(PROPS_ID, &props);
        writer.storage(DRAWABLE_ID, |uniforms| uniforms.draw_ubo);
        writer.storage(TILE_PROPS_ID, |uniforms| uniforms.tile_props);
        writer.storage(PROJECTION_ID, |uniforms| uniforms.projection);
    }
}