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
// Where a bucket's vertices and indices live once the render thread draws
// them.
//
// Buckets cross from the worker threads, so they hold plain vectors
// (`GeometryVec`). A bucket's upload moves each vector into the context's
// stores, which the draws of the bucket's tile share, so the bucket and the
// draws don't each keep a copy; the tile keeps their handles, by the key of
// the vector they were made of, in a `BucketGeometry`. The stores free the
// data once it is in a GPU buffer, and the draws a layer builds again for
// the same bucket reuse that buffer. Vertices that a layer makes of a
// bucket's vector are kept there too, made before the vector moves.
//
// A copy of a vector keeps its key. The tiles of a tile's other world
// copies hold copies of its buckets, and the upload of either takes what
// the other already moved into the stores, freeing its own copy.
//
// In the browser, the arrays a bucket gets from a tile worker in another
// wasm instance stay the JS arrays they arrived in (mln_wire::host_bytes)
// and move to the store as they are, which uploads them from there.
// Anything that reads or changes them first copies them into wasm memory.

use std::borrow::Cow;

#[cfg(target_family = "wasm")]
use mln_wire::host_bytes::HostBytes;
use mln_wire::payload::{Payload, pod};

use crate::draw::context::Context;
use crate::draw::identity::SimpleIdentity;
use crate::draw::indexes::{DrawIndexes, Indexes, SharedIndexes};
use crate::draw::vertices::SharedVertices;
use crate::layer::paint_property_binder::InterleavedVertexBuffer;

/// A bucket's vertices or indices. The worker fills it through `get_mut`;
/// the render thread can move the data out (`BucketGeometry`), after which
/// `len` still counts it.
#[derive(Clone, Debug)]
pub struct GeometryVec<T> {
    data: Data<T>,
    /// What `BucketGeometry` keeps the vector's stored geometry under. A
    /// copy keeps it, so the stored geometry of either serves both.
    key: SimpleIdentity,
}

/// Where a [`GeometryVec`]'s data is.
#[derive(Clone, Debug)]
enum Data<T> {
    /// In memory.
    Vec(Vec<T>),
    /// In the JS array it arrived in.
    #[cfg(target_family = "wasm")]
    Host(HostBytes),
    /// Moved out, `len` elements of it.
    Moved { len: usize },
}

impl<T> Default for GeometryVec<T> {
    fn default() -> Self {
        GeometryVec::new(Vec::new())
    }
}

impl<T> GeometryVec<T> {
    #[must_use]
    pub(crate) fn new(v: Vec<T>) -> Self {
        GeometryVec::of(Data::Vec(v))
    }

    fn of(data: Data<T>) -> Self {
        GeometryVec { data, key: SimpleIdentity::new() }
    }

    /// The data, to fill. Not after it is moved out.
    pub fn get_mut(&mut self) -> &mut Vec<T>
    where
        T: bytemuck::Pod,
    {
        debug_assert!(!self.is_moved(), "geometry modified after it moved to the render thread");
        self.data.vec_mut()
    }

    /// The element count, which stays when the data is moved out.
    #[must_use]
    pub fn len(&self) -> usize {
        match &self.data {
            Data::Vec(v) => v.len(),
            #[cfg(target_family = "wasm")]
            Data::Host(bytes) => bytes.len() / size_of::<T>(),
            Data::Moved { len } => *len,
        }
    }

    /// Whether the vector holds no element.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// True once the data has moved to the context's store.
    #[must_use]
    pub fn is_moved(&self) -> bool {
        matches!(self.data, Data::Moved { .. })
    }

    /// The data, empty once it is moved out.
    #[must_use]
    pub fn data(&self) -> Cow<'_, [T]>
    where
        T: bytemuck::Pod,
    {
        self.data.data()
    }

    /// The data, moved out, leaving its count.
    fn take(&mut self) -> Data<T> {
        let len = self.len();
        std::mem::replace(&mut self.data, Data::Moved { len })
    }
}

impl<T: bytemuck::Pod> Data<T> {
    /// The data, empty once it is moved out.
    fn data(&self) -> Cow<'_, [T]> {
        match self {
            Data::Vec(v) => Cow::Borrowed(v),
            #[cfg(target_family = "wasm")]
            Data::Host(bytes) => Cow::Owned(bytes.to_pod_vec()),
            Data::Moved { .. } => Cow::Borrowed(&[]),
        }
    }

    /// The data's vector, which replaces the data where it isn't in memory:
    /// a copy of the JS array's, or an empty one where it moved out.
    fn vec_mut(&mut self) -> &mut Vec<T> {
        match self {
            Data::Vec(v) => v,
            #[cfg(target_family = "wasm")]
            host @ Data::Host(_) => {
                *host = Data::Vec(host.data().into_owned());
                host.vec_mut()
            }
            moved @ Data::Moved { .. } => {
                *moved = Data::Vec(Vec::new());
                moved.vec_mut()
            }
        }
    }
}

impl<T: bytemuck::Pod> GeometryVec<T> {
    /// Frees the data, which nothing will draw. `len` still counts it.
    pub fn discard(&mut self) {
        self.take();
    }

    /// The data, moved out into `context`'s store.
    fn store_vertices(&mut self, context: &mut Context) -> SharedVertices {
        match self.take() {
            Data::Vec(v) => context.add_vertices(v),
            #[cfg(target_family = "wasm")]
            Data::Host(bytes) => context.add_host_vertices::<T>(bytes),
            Data::Moved { .. } => context.add_vertices(Vec::<T>::new()),
        }
    }
}

impl GeometryVec<u16> {
    /// Indexes of the data, moved out.
    fn indexes(&mut self) -> Indexes {
        match self.take() {
            Data::Vec(v) => Indexes::new(v),
            #[cfg(target_family = "wasm")]
            Data::Host(bytes) => Indexes::from_host(bytes),
            Data::Moved { .. } => Indexes::new(Vec::new()),
        }
    }
}

impl<T: bytemuck::Pod + PartialEq> PartialEq for GeometryVec<T> {
    fn eq(&self, other: &Self) -> bool {
        self.is_moved() == other.is_moved() && self.len() == other.len() && self.data() == other.data()
    }
}

impl<T> From<Vec<T>> for GeometryVec<T> {
    fn from(v: Vec<T>) -> Self {
        GeometryVec::new(v)
    }
}

/// As the plain values' bytes (`payload::pod`). Workers send buckets before
/// the render thread moves anything out. A payload the host kept in JS
/// stays there (`mln_wire::host_bytes`).
impl<T: bytemuck::Pod> mln_wire::Wire for GeometryVec<T> {
    fn write(&self, w: &mut mln_wire::Writer) {
        debug_assert!(!self.is_moved());
        pod::write(&self.data(), w);
    }

    fn read(r: &mut mln_wire::Reader<'_>) -> Option<Self> {
        Some(match pod::read_payload::<T>(r)? {
            Payload::Bytes(bytes) => GeometryVec::new(bytemuck::pod_collect_to_vec(&bytes)),
            #[cfg(target_family = "wasm")]
            Payload::Host(bytes) => GeometryVec::of(Data::Host(bytes)),
        })
    }
}

/// The geometry of a tile's buckets in the context's stores, by the key of
/// the bucket's vector it was made of. The tile keeps one per layout result,
/// so it lives as long as the buckets it was made from.
#[derive(Clone, Default)]
pub struct BucketGeometry {
    vertices: Kept<SharedVertices>,
    indexes: Kept<SharedIndexes>,
    /// The vertices a layer made of a bucket's vector.
    derived: Kept<SharedVertices>,
}

impl BucketGeometry {
    /// The vertices that `vec`, a bucket's, moved into.
    fn vertices<V>(&self, vec: &GeometryVec<V>) -> Option<&SharedVertices> {
        self.vertices.get(vec.key)
    }

    /// Frees the room that the entries' lists keep for more, where it is
    /// over a quarter of what they keep, once a tile's buckets uploaded.
    pub(crate) fn shrink(&mut self) {
        self.vertices.shrink();
        self.indexes.shrink();
        self.derived.shrink();
    }
}

/// Entries by the key of the vector they were made of, in key order: a
/// tile's are few, and a list takes less memory than a map.
#[derive(Clone)]
struct Kept<T>(Vec<(SimpleIdentity, T)>);

impl<T> Default for Kept<T> {
    fn default() -> Self {
        Kept(Vec::new())
    }
}

impl<T> Kept<T> {
    fn get(&self, key: SimpleIdentity) -> Option<&T> {
        let at = self.0.binary_search_by_key(&key, |(key, _)| *key).ok()?;
        self.0.get(at).map(|(_, value)| value)
    }

    fn insert(&mut self, key: SimpleIdentity, value: T) {
        match self.0.binary_search_by_key(&key, |(key, _)| *key) {
            Ok(at) => {
                if let Some(entry) = self.0.get_mut(at) {
                    entry.1 = value;
                }
            }
            Err(at) => self.0.insert(at, (key, value)),
        }
    }

    /// Whether the list has an entry under `key`, taking the first of
    /// `others`' where it has none.
    fn has_or_takes<'a>(&mut self, key: SimpleIdentity, mut others: impl Iterator<Item = &'a Kept<T>>) -> bool
    where
        T: Clone + 'a,
    {
        if self.get(key).is_some() {
            return true;
        }
        let Some(shared) = others.find_map(|other| other.get(key)) else {
            return false;
        };
        self.insert(key, shared.clone());
        true
    }

    fn shrink(&mut self) {
        if self.0.capacity() - self.0.len() > self.0.len() / 4 {
            self.0.shrink_to_fit();
        }
    }
}

/// A bucket's upload: what moves the bucket's geometry into the context's
/// stores, and keeps its handles in its tile's [`BucketGeometry`].
pub(crate) struct GeometryUpload<'a> {
    geometry: &'a mut BucketGeometry,
    /// The geometry of the tile's other world copies, whose buckets the
    /// tile's are copies of, or they of the tile's.
    copies: &'a [&'a BucketGeometry],
    context: &'a mut Context,
}

impl<'a> GeometryUpload<'a> {
    /// The upload of buckets whose geometry `geometry` keeps, into
    /// `context`'s stores, which takes what of it `copies`, the geometry of
    /// the tile's other world copies, keep.
    pub(crate) fn new(
        geometry: &'a mut BucketGeometry,
        copies: &'a [&'a BucketGeometry],
        context: &'a mut Context,
    ) -> Self {
        GeometryUpload { geometry, copies, context }
    }

    /// What the context's device allows.
    pub(crate) fn caps(&self) -> &mln_gfx::gpu::Caps {
        self.context.caps()
    }

    /// Moves the data of `vec`, a bucket's, into the store, unless it moved
    /// before. Where a copy of it moved, this frees it and shares the
    /// copy's.
    pub(crate) fn move_vertices<V: bytemuck::Pod>(&mut self, vec: &mut GeometryVec<V>) {
        if vec.is_moved() {
            return;
        }
        if self.geometry.vertices.has_or_takes(vec.key, self.copies.iter().map(|copy| &copy.vertices)) {
            vec.discard();
        } else {
            let vertices = vec.store_vertices(self.context);
            self.geometry.vertices.insert(vec.key, vertices);
        }
    }

    /// Moves the data of `vec`, a bucket's, into the store's indexes,
    /// unless it moved before, as [`move_vertices`](Self::move_vertices)
    /// does.
    pub(crate) fn move_indexes(&mut self, vec: &mut GeometryVec<u16>) {
        if vec.is_moved() {
            return;
        }
        if self.geometry.indexes.has_or_takes(vec.key, self.copies.iter().map(|copy| &copy.indexes)) {
            vec.discard();
        } else {
            let indexes = self.context.add_indexes(vec.indexes());
            self.geometry.indexes.insert(vec.key, indexes);
        }
    }

    /// Moves the values of `buffer`, a layer's data-driven paint values,
    /// into the store, unless they `depend_on_feature_state`: those stay in
    /// the bucket, where feature state rewrites them.
    pub(crate) fn move_paint(&mut self, buffer: &mut InterleavedVertexBuffer, depends_on_feature_state: bool) {
        if !depends_on_feature_state {
            self.move_vertices(&mut buffer.shared_vertex_vector);
        }
    }

    /// Keeps the vertices that `make` makes of the data of `vec`, a
    /// bucket's, for [`derived_vertices`], unless it or a copy has some.
    /// Before the data moves.
    pub(crate) fn derive_vertices<V: bytemuck::Pod, D: bytemuck::Pod>(
        &mut self,
        vec: &GeometryVec<V>,
        make: impl FnOnce(&[V]) -> Vec<D>,
    ) {
        if !self.geometry.derived.has_or_takes(vec.key, self.copies.iter().map(|copy| &copy.derived)) {
            debug_assert!(!vec.is_moved(), "vertices derived from data that moved to the store");
            let derived = self.context.add_vertices(make(&vec.data()));
            self.geometry.derived.insert(vec.key, derived);
        }
    }
}

/// The vertices that a bucket's upload made of `vec`, one of its vectors,
/// with `GeometryUpload::derive_vertices`, which its tile's `geometry`
/// keeps.
#[must_use]
pub fn derived_vertices<V>(geometry: &BucketGeometry, vec: &GeometryVec<V>) -> Option<SharedVertices> {
    let derived = geometry.derived.get(vec.key);
    debug_assert!(derived.is_some(), "vertices that the bucket's upload didn't derive");
    derived.cloned()
}

/// Vertices of `context`'s store holding a copy of `vertices`, which the
/// draws that bind them don't change: draws replace such vertices instead.
pub fn copied_vertices<V: bytemuck::Pod>(context: &mut Context, vertices: &[V]) -> SharedVertices {
    context.add_vertices(vertices.to_vec())
}

/// Indexes holding a copy of `indexes`, which a draw owns.
#[must_use]
pub fn copied_indexes(indexes: &[u16]) -> DrawIndexes {
    DrawIndexes::new(indexes.to_vec())
}

/// The vertices that a bucket's upload moved `vec`, one of its vectors,
/// into, which its tile's `geometry` keeps.
#[must_use]
pub fn bucket_vertices<V>(geometry: &BucketGeometry, vec: &GeometryVec<V>) -> Option<SharedVertices> {
    let vertices = geometry.vertices(vec);
    debug_assert!(vertices.is_some(), "vertices that the bucket's upload didn't move");
    vertices.cloned()
}

/// The indexes that a bucket's upload moved `vec`, one of its vectors,
/// into, which its tile's `geometry` keeps and its draws share.
#[must_use]
pub fn bucket_indexes(geometry: &BucketGeometry, vec: &GeometryVec<u16>) -> Option<SharedIndexes> {
    let indexes = geometry.indexes.get(vec.key);
    debug_assert!(indexes.is_some(), "indexes that the bucket's upload didn't move");
    indexes.cloned()
}

/// The vertices of a layer's data-driven paint values: those that a
/// bucket's upload moved, which its tile's `geometry` keeps, or a copy in
/// `context`'s store of the values that feature state rewrites, which stay
/// in the bucket, and which the draws copy again each time they change.
pub fn paint_vertices(
    context: &mut Context,
    geometry: &BucketGeometry,
    buffer: &InterleavedVertexBuffer,
    depends_on_feature_state: bool,
) -> Option<SharedVertices> {
    if depends_on_feature_state {
        Some(copied_vertices(context, &buffer.shared_vertex_vector.data()))
    } else {
        bucket_vertices(geometry, &buffer.shared_vertex_vector)
    }
}