mln-rs-webgpu 0.0.22

Part of mln-rs, a MapLibre map renderer written in Rust. 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
// Frames that fail on the GPU, through a GPU that fails a kind of pass, the
// draws in one, or a shader, as a device out of memory or a backend that
// rejects a shader would: a still image ends with the error, and the next
// one draws once the device works; a continuous map whose draw always fails
// reports it once, shows what did draw, and goes idle; and a terrain whose
// shader fails draws flat.

use std::sync::Arc;
use std::time::Duration;

use mln_engine::exec::pool::Pool;
use mln_engine::map::{Map, MapEvent, MapMode, MapOptions};
use mln_engine::storage::stub_file_source::{ResponseType, StubFileSource};
use mln_engine::storage::{Kind, Resource, Response};
use mln_engine::{Renderer, RendererEvent};
use mln_gfx::RendererBackend as _;
use mln_gfx::frame::FrameTarget as _;
use mln_gfx::gpu::{
    BindingsDesc, BindingsId, BufferDesc, BufferId, Caps, Draw, GeometryDesc, GeometryId, Gpu, GpuError, PassDesc,
    PipelineDesc, PipelineId, ReadbackId, ResourceId, SamplerId, ShaderId, ShaderKey, TextureDesc, TextureId,
    TextureRegion,
};
use mln_style::bitmap::PremultipliedImage;
use mln_style::error::{Error, ErrorKind};
use mln_style::size::Size;
use mln_style::time::Instant;

use crate::{OffscreenTarget, RendererBackend};

const SIDE: u32 = 256;

/// How far a frame moves the map's clock.
const FRAME: Duration = Duration::from_millis(16);

/// A DEM tile of sea level everywhere, in the Mapbox encoding: (1 · 65536 +
/// 134 · 256 + 160) · 0.1 − 10000 = 0 m.
fn sea_level_png() -> Vec<u8> {
    let mut image = PremultipliedImage::new(Size::new(256, 256));
    for pixel in image.data.as_chunks_mut::<4>().0 {
        pixel.copy_from_slice(&[1, 134, 160, 255]);
    }
    mln_engine::image::encode_png(&image)
}

/// A white map with a red square around the center, a blue line across it
/// and the hillshade of flat ground, which prepares in a render target.
fn style() -> String {
    styled(true)
}

/// [`style`], with its line if `line`.
fn styled(line: bool) -> String {
    let line = if line {
        r#",{
            "id": "line",
            "type": "line",
            "source": "shapes",
            "filter": ["==", "$type", "LineString"],
            "paint": {"line-color": "blue", "line-width": 6}
        }"#
    } else {
        ""
    };
    format!(
        r#"{{
            "version": 8,
            "center": [0, 0],
            "zoom": 2,
            "sources": {{
                "dem": {{"type": "raster-dem", "tiles": ["dem/{{z}}/{{x}}/{{y}}.png"], "tileSize": 256, "maxzoom": 10}},
                "shapes": {{
                    "type": "geojson",
                    "data": {{
                        "type": "FeatureCollection",
                        "features": [
                            {{
                                "type": "Feature",
                                "properties": {{}},
                                "geometry": {{
                                    "type": "Polygon",
                                    "coordinates": [[[-5, -5], [5, -5], [5, 5], [-5, 5], [-5, -5]]]
                                }}
                            }},
                            {{
                                "type": "Feature",
                                "properties": {{}},
                                "geometry": {{"type": "LineString", "coordinates": [[-60, 10], [60, 10]]}}
                            }}
                        ]
                    }}
                }}
            }},
            "layers": [
                {{"id": "background", "type": "background", "paint": {{"background-color": "white"}}}},
                {{"id": "hillshade", "type": "hillshade", "source": "dem"}},
                {{
                    "id": "fill",
                    "type": "fill",
                    "source": "shapes",
                    "filter": ["==", "$type", "Polygon"],
                    "paint": {{"fill-color": "red"}}
                }}
                {line}
            ]
        }}"#
    )
}

/// What a [`Failing`] GPU fails.
#[derive(Clone, Copy, Debug)]
enum Fail {
    /// Beginning the passes of this label: "main buffer" for the frame's,
    /// "render target" for a layer's render target's, such as a
    /// hillshade's.
    Passes(&'static str),
    /// The draws in the passes of this label, which begin.
    DrawsIn(&'static str),
    /// Making the shaders whose program's name starts so, as a backend that
    /// rejects them would: "Line" for the line layer's.
    Shaders(&'static str),
    /// Submitting the frame.
    Submits,
}

/// A GPU that fails what `fail` says, and does the rest on `gpu`.
struct Failing<'a> {
    gpu: &'a mut dyn Gpu,
    fail: Fail,
    /// Whether the pass open is one whose draws fail.
    failing_pass: bool,
}

fn failed<T>() -> Result<T, GpuError> {
    Err(GpuError::Invalid("failed on purpose".to_owned()))
}

impl Gpu for Failing<'_> {
    fn caps(&self) -> &Caps {
        self.gpu.caps()
    }
    fn create_buffer(&mut self, desc: &BufferDesc<'_>) -> Result<BufferId, GpuError> {
        self.gpu.create_buffer(desc)
    }
    fn write_buffer(&mut self, buffer: BufferId, offset: u64, data: &[u8]) -> Result<(), GpuError> {
        self.gpu.write_buffer(buffer, offset, data)
    }
    fn create_texture(&mut self, desc: &TextureDesc<'_>) -> Result<TextureId, GpuError> {
        self.gpu.create_texture(desc)
    }
    fn write_texture(&mut self, texture: TextureId, region: TextureRegion, data: &[u8]) -> Result<(), GpuError> {
        self.gpu.write_texture(texture, region, data)
    }
    fn generate_mipmaps(&mut self, texture: TextureId) -> Result<(), GpuError> {
        self.gpu.generate_mipmaps(texture)
    }
    fn create_sampler(&mut self, state: mln_gfx::texture2d::SamplerState) -> Result<SamplerId, GpuError> {
        self.gpu.create_sampler(state)
    }
    fn create_shader(&mut self, key: &ShaderKey) -> Result<ShaderId, GpuError> {
        match self.fail {
            Fail::Shaders(name) if key.program().name.starts_with(name) => failed(),
            _ => self.gpu.create_shader(key),
        }
    }
    fn create_pipeline(&mut self, desc: &PipelineDesc<'_>) -> Result<PipelineId, GpuError> {
        self.gpu.create_pipeline(desc)
    }
    fn create_bindings(&mut self, desc: &BindingsDesc<'_>) -> Result<BindingsId, GpuError> {
        self.gpu.create_bindings(desc)
    }
    fn create_geometry(&mut self, desc: &GeometryDesc<'_>) -> Result<GeometryId, GpuError> {
        self.gpu.create_geometry(desc)
    }
    fn destroy(&mut self, resource: ResourceId) -> Result<(), GpuError> {
        self.gpu.destroy(resource)
    }
    fn begin_pass(&mut self, desc: &PassDesc<'_>) -> Result<(), GpuError> {
        match self.fail {
            Fail::Passes(label) if desc.label == label => return failed(),
            Fail::DrawsIn(label) => self.failing_pass = desc.label == label,
            Fail::Passes(_) | Fail::Shaders(_) | Fail::Submits => {}
        }
        self.gpu.begin_pass(desc)
    }
    fn attaches_with_any_color(&self, depth_stencil: TextureId) -> bool {
        self.gpu.attaches_with_any_color(depth_stencil)
    }
    fn draw(&mut self, draw: &Draw) -> Result<(), GpuError> {
        if self.failing_pass {
            return failed();
        }
        self.gpu.draw(draw)
    }
    fn lend(&mut self, f: &mut dyn FnMut(&mut dyn std::any::Any)) -> Result<(), GpuError> {
        self.gpu.lend(f)
    }
    fn end_pass(&mut self) -> Result<(), GpuError> {
        self.failing_pass = false;
        self.gpu.end_pass()
    }
    fn flush(&mut self, wait: bool) -> Result<(), GpuError> {
        if matches!(self.fail, Fail::Submits) {
            return failed();
        }
        self.gpu.flush(wait)
    }
    fn read_texture(&mut self, texture: TextureId, region: TextureRegion) -> Result<ReadbackId, GpuError> {
        self.gpu.read_texture(texture, region)
    }
    fn take_readback(&mut self, readback: ReadbackId, wait: bool) -> Result<Option<Vec<u8>>, GpuError> {
        self.gpu.take_readback(readback, wait)
    }
}

/// A map of [`style`], and a renderer on WebGPU that draws each update the
/// map publishes, as a frontend does, through a GPU that fails what `fail`
/// says in the next `failing` frames it draws. It repaints the map when the
/// map asks to be ([`Map::repaint_at`]), and keeps the pixels of the last
/// frame, as a surface shows them.
///
/// The map's clock is held, and each frame moves it on by [`FRAME`]. The
/// map's and the renderer's background work runs on the test's thread
/// before each frame, so a frame sees all the work that the ones before
/// it started, however slowly the test runs.
struct FailingMap {
    map: Map,
    backend: RendererBackend,
    target: OffscreenTarget,
    renderer: Renderer,
    /// The generation of the last update drawn.
    drawn: u64,
    fail: Fail,
    failing: usize,
    /// The pixels of the last frame.
    shown: Option<Vec<u8>>,
    pool: Pool,
    /// The time the map's clock is held at.
    clock: Instant,
}

impl FailingMap {
    fn new(mode: MapMode, fail: Fail) -> Option<Self> {
        Self::with_style(mode, &style(), fail)
    }

    fn with_style(mode: MapMode, style: &str, fail: Fail) -> Option<Self> {
        let device = super::device()?;
        let mut backend = RendererBackend::new(&device);
        let pool = Pool::local();
        let size = Size::new(SIDE, SIDE);
        let file_source = Arc::new(StubFileSource::new(ResponseType::Synchronous));
        let dem = Arc::new(sea_level_png());
        file_source.set_response(Kind::Tile, move |_: &Resource| {
            let mut response = Response::new();
            response.data = Some(Arc::clone(&dem));
            Some(response)
        });
        let options = MapOptions { map_mode: mode, size, ..Default::default() };
        let mut map = Map::with_file_source(file_source, options, mln_builtins::registry(), &pool).unwrap();
        let clock = Instant::now();
        map.frame_clock().hold(Some(clock));
        let events = map.renderer_events();
        let invalidate = events.clone();
        let mut renderer = Renderer::with_glyph_cache(
            backend.gpu(),
            1.0,
            mln_engine::text::local_fonts::LocalGlyphs::default(),
            Arc::default(),
            mln_builtins::registry(),
            Arc::new(move || invalidate.push(RendererEvent::Invalidate)),
            &pool,
        );
        renderer.set_event_queue(Some(events));
        map.load_style_json(style);
        let camera = mln_engine::map::style_camera(map.style());
        map.jump_to(&camera);
        let target = OffscreenTarget::new(mln_gfx::Size::new(size.width, size.height));
        Some(FailingMap { map, backend, target, renderer, drawn: 0, fail, failing: 0, shown: None, pool, clock })
    }

    /// Runs the background work, takes in the renderer's reports, repaints
    /// the map if its time came, then draws the map's latest update unless
    /// it drew it already. A still that hasn't loaded draws no frame.
    fn draw(&mut self) {
        self.pool.run_all();
        self.map.process_events();
        if self.map.repaint_at().is_some_and(|at| self.clock >= at) {
            self.map.trigger_repaint();
        }
        let Some(update) = self.map.update_reader().latest().filter(|update| update.generation != self.drawn) else {
            return;
        };
        self.drawn = update.generation;
        let gpu = self.backend.gpu();
        let failing = self.failing > 0;
        let updated = if failing {
            self.renderer.update(&mut Failing { gpu, fail: self.fail, failing_pass: false }, &update.parameters)
        } else {
            self.renderer.update(gpu, &update.parameters)
        };
        if let Some(frame) = updated {
            let image = self.target.begin_frame(gpu).expect("the frame's image");
            let drawn = if failing {
                self.failing -= 1;
                self.renderer.render(&mut Failing { gpu, fail: self.fail, failing_pass: false }, &image, frame)
            } else {
                self.renderer.render(gpu, &image, frame)
            };
            // A frame fails through a shader only once it draws with it.
            let failed = drawn.error().is_some();
            let shader = matches!(self.fail, Fail::Shaders(_));
            assert!(failed == failing || (failing && shader), "{:?} failed: {:?}", self.fail, drawn.error());
            let handoff = self.target.end_frame(gpu);
            self.shown = Some(self.target.read_image(gpu).expect("the frame's pixels").data);
            self.renderer.finish_frame(gpu, drawn, handoff);
        }
    }

    /// Moves the map's clock on by a frame.
    fn tick(&mut self) {
        self.clock += FRAME;
        self.map.frame_clock().hold(Some(self.clock));
        self.map.sample_frame();
    }

    /// Asks for a still and draws the map's updates until it ends, with its
    /// pixels: `None` for a still that doesn't end in a few thousand turns
    /// of the map's work, which is all the work there is.
    fn still(&mut self) -> Option<Result<Vec<u8>, Error>> {
        self.pool.run_all();
        self.map.process_events();
        self.map.render_still().unwrap();
        for _ in 0..2_000 {
            self.pool.run_all();
            self.map.process_events();
            if let Some(ended) = self.map.take_still_image() {
                return Some(
                    ended.map(|()| self.target.read_image(self.backend.gpu()).expect("the frame's pixels").data),
                );
            }
            self.draw();
        }
        None
    }

    /// Draws a frame at a time until `done` says so of the map's events, or
    /// `within` passed on the map's clock, and returns the events and
    /// whether `done` said so.
    fn run(&mut self, within: Duration, mut done: impl FnMut(&[MapEvent]) -> bool) -> (Vec<MapEvent>, bool) {
        let mut events = Vec::new();
        let end = self.clock + within;
        while self.clock < end {
            self.draw();
            events.extend(self.map.drain_events());
            if done(&events) {
                return (events, true);
            }
            self.tick();
        }
        (events, false)
    }

    /// The frames the renderer drew.
    fn frames(&self) -> i32 {
        self.renderer.rendering_stats().num_frames
    }
}

impl Drop for FailingMap {
    fn drop(&mut self) {
        let gpu = self.backend.gpu();
        self.target.tear_down(gpu);
    }
}

/// Asserts that a still ended with the error of a GPU that failed on
/// purpose.
fn assert_failed(ended: Option<Result<Vec<u8>, Error>>, fail: Fail) {
    let error = ended.expect("the still never ended").expect_err(&format!("the still drew through {fail:?}"));
    assert_eq!(ErrorKind::Failed, error.kind);
    assert!(error.message.contains("failed on purpose"), "{fail:?}: {error}");
}

/// The kinds of failures of a map.
const FAILURES: [Fail; 6] = [
    Fail::Passes("main buffer"),
    Fail::Passes("render target"),
    Fail::DrawsIn("main buffer"),
    Fail::DrawsIn("render target"),
    Fail::Shaders("Line"),
    Fail::Submits,
];

/// A still whose one frame fails, in any of its passes or draws, ends with
/// the GPU's error rather than a wrong image. A still asked for right after
/// draws what a new map's does.
#[test]
fn a_still_whose_frame_fails_ends_with_the_error() {
    let Some(mut new) = FailingMap::new(MapMode::Static, Fail::DrawsIn("none")) else {
        return;
    };
    let new = new.still().expect("a new map's still never ended").expect("a new map's still");
    for fail in FAILURES {
        let Some(mut map) = FailingMap::new(MapMode::Static, fail) else {
            return;
        };
        map.failing = 1;
        assert_failed(map.still(), fail);
        assert_eq!((0, 1), (map.failing, map.frames()), "{fail:?}");
        let image = map.still().expect("the next still never ended").expect("the next still");
        assert_eq!(2, map.frames(), "{fail:?}");
        assert!(new == image, "the next still after {fail:?} draws other pixels than a new map's");
    }
}

fn idle(events: &[MapEvent]) -> bool {
    events.iter().any(|event| matches!(event, MapEvent::Idle))
}

fn errors(events: &[MapEvent]) -> usize {
    events.iter().filter(|event| matches!(event, MapEvent::RenderError(_))).count()
}

/// What a continuous map of `style` shows once it goes idle, on a GPU that
/// works.
fn idle_image(style: &str) -> Option<Vec<u8>> {
    let mut map = FailingMap::with_style(MapMode::Continuous, style, Fail::DrawsIn("none"))?;
    assert!(map.run(Duration::from_secs(10), idle).1, "a new map never went idle");
    Some(map.shown.take().expect("a new map showed nothing"))
}

/// A continuous map whose line's shader always fails, as on a backend that
/// rejects it, shows the rest of the map, as a map without the line does,
/// and reports the failure once. It goes idle, after drawing again once, and
/// draws no more on its own; whatever else draws a frame, such as a camera
/// change, still draws.
#[test]
fn a_continuous_map_whose_draw_always_fails_shows_the_rest_and_goes_idle() {
    let Some(expected) = idle_image(&styled(false)) else {
        return;
    };
    let Some(with_line) = idle_image(&style()) else {
        return;
    };
    assert!(expected != with_line, "the line draws nothing");
    let Some(mut map) = FailingMap::new(MapMode::Continuous, Fail::Shaders("Line")) else {
        return;
    };
    map.failing = usize::MAX;
    let (events, went_idle) = map.run(Duration::from_secs(10), idle);
    assert!(went_idle, "the map never went idle: {events:?}");
    assert_eq!(1, errors(&events), "{events:?}");
    assert!(map.shown.as_ref() == Some(&expected), "the map shows other pixels");

    let frames = map.frames();
    let (events, _) = map.run(Duration::from_millis(1500), |_| false);
    assert!(frames == map.frames() && events.is_empty(), "the idle map drew: {events:?}");
    map.map.jump_to(&mln_engine::camera::CameraOptions { zoom: Some(2.5), ..Default::default() });
    let (events, went_idle) = map.run(Duration::from_secs(10), idle);
    assert!(went_idle && map.frames() > frames, "{events:?}");
    assert_eq!(0, errors(&events), "the same failure reported again");
}

/// A still of a terrain on a backend that fails to make the terrain's
/// shader draws the map flat, its draped layers too, as a map without
/// terrain does, with the camera on no ground.
#[test]
fn a_still_without_the_terrains_shader_draws_flat() {
    let Some(mut flat) = FailingMap::new(MapMode::Static, Fail::DrawsIn("none")) else {
        return;
    };
    let flat = flat.still().expect("a flat map's still never ended").expect("a flat map's still");
    let style = style().replacen(r#""version": 8,"#, r#""version": 8, "terrain": {"source": "dem"},"#, 1);
    let Some(mut map) = FailingMap::with_style(MapMode::Static, &style, Fail::Shaders("TerrainShader")) else {
        return;
    };
    map.failing = usize::MAX;
    let image = map.still().expect("the still never ended").expect("the still");
    assert!(flat == image, "the still draws other pixels than a flat map's");
    assert_eq!(None, map.map.transform_state().view().terrain_min_elevation(), "the camera stands on ground");
}