mln-rs-webgpu 0.0.17

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
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
//! The [`Gpu`] interface on wgpu.

use std::any::Any;
use std::ops::Range;
use std::sync::{Arc, OnceLock};

use mln_gfx::gpu::validation::{
    self, BindingsTags, BufferInfo, GeometryTags, LayoutTags, Lookup, PassTarget, PipelineTags, ShaderBinding,
    TextureInfo,
};
use mln_gfx::gpu::{
    BindGroupEntry, BindingsDesc, BindingsId, BufferDesc, BufferId, BufferUsage, Caps, ClipDepthRange, Draw,
    DynamicState, GeometryDesc, GeometryId, Gpu, GpuError, LoadOp, PassDesc, PipelineDesc, PipelineId, ReadbackId,
    ResourceId, SamplerId, Scissor, ShaderId, ShaderKey, SlotMap, TextureDesc, TextureFormat, TextureId, TextureRegion,
    TextureUsage,
};
use mln_gfx::texture2d::SamplerState;
use mln_gfx::types::{TextureFilterType, TextureWrapType};
use mln_gfx::{Rgba, Size};

use crate::index_table::IndexTable;
use crate::mipmaps::Mipmapper;
use crate::shaders::declaration;
use crate::shaders::shader_program::{self, BindingType, ShaderProgram, validated};
use crate::writes::Writes;
use crate::{CustomLayerEncoder, CustomLayerPass, Device};

/// The size of the buffers that small buffers share: uniform and storage
/// buffers in one kind of page, vertex and index buffers in another.
const PAGE: u64 = 16 << 10;

/// How a vertex buffer binds: to a draw's vertex buffer slots, and as a
/// storage buffer that a shader reads by vertex index
/// (`Caps::binds_vertex_buffers`).
const VERTEX_USAGES: wgpu::BufferUsages = wgpu::BufferUsages::VERTEX.union(wgpu::BufferUsages::STORAGE);

/// The most storage buffers that a program's vertex stage reads, which a
/// device's `max_storage_buffers_per_shader_stage` has to allow for its
/// bindings to bind vertex buffers (`Caps::binds_vertex_buffers`). A device
/// that allows fewer, such as one made with
/// `wgpu::Limits::downlevel_defaults()`, draws instances instead.
pub(crate) const VERTEX_STAGE_STORAGE_BUFFERS: u32 = 5;

/// The alignment of a small uniform or storage buffer in its page: WebGPU's
/// largest allowed `minUniformBufferOffsetAlignment`, so an offset any
/// device binds.
const UNIFORM_ALIGNMENT: u64 = 256;

/// A buffer, or the part of a shared page that a small buffer takes.
struct Buffer {
    buffer: wgpu::Buffer,
    usage: BufferUsage,
    /// Where the buffer starts in `buffer`.
    offset: u64,
    /// The bytes of `buffer` from `offset` that the buffer takes, which
    /// its writes and bindings stay in.
    size: u64,
    /// Of a buffer in a page, the end of the bytes written so far: those
    /// after it still hold the zeros it was made with. `None` for a buffer
    /// of its own.
    written: Option<u64>,
}

struct Texture {
    texture: wgpu::Texture,
    /// The view of every level, which shaders sample.
    view: wgpu::TextureView,
    /// Of a texture with mips, a view of each level, which passes draw into
    /// level 0 of.
    levels: Vec<wgpu::TextureView>,
    /// Of a texture whose mips a compute pass writes, what the write of each
    /// level binds, made when its mips are first written.
    mip_groups: Vec<wgpu::BindGroup>,
}

impl Texture {
    /// The view of level 0, which passes draw into.
    fn attachment(&self) -> &wgpu::TextureView {
        self.levels.first().unwrap_or(&self.view)
    }
}

/// A compiled program, and what it reads from its bindings.
struct Shader {
    program: ShaderProgram,
    reads: Vec<ShaderBinding>,
}

struct Pipeline {
    pipeline: wgpu::RenderPipeline,
    tags: PipelineTags,
}

/// A shader's bind groups, by the pipeline layout's slot, and the slot of
/// its group of `globalIndex`.
struct Bindings {
    groups: Vec<(u32, wgpu::BindGroup)>,
    index_slot: Option<u32>,
    tags: BindingsTags,
}

/// The vertex buffers and the index buffer of a draw, each with the range
/// of its wgpu buffer that it binds: from its offset to the end of its
/// buffer, which in a page is short of the page's end.
struct Geometry {
    vertex_buffers: Vec<(wgpu::Buffer, Range<u64>)>,
    index_buffer: (wgpu::Buffer, Range<u64>),
    /// The ids and offsets of the vertex buffers and of the index buffer,
    /// which a draw compares with what the pass has bound.
    vertex_ids: Vec<(BufferId, u64)>,
    index_id: (BufferId, u64),
    tags: GeometryTags,
}

/// A texture's texels, copied into a buffer that the CPU maps.
struct Readback {
    buffer: wgpu::Buffer,
    /// The bytes in each row of `buffer`, which WebGPU pads, and of the
    /// texels.
    padded_bytes_per_row: usize,
    bytes_per_row: usize,
    rows: usize,
    #[cfg_attr(target_family = "wasm", expect(dead_code, reason = "a browser can't wait for a submission"))]
    submission: wgpu::SubmissionIndex,
    /// How the map ended, once it has.
    mapped: Arc<OnceLock<Result<(), wgpu::BufferAsyncError>>>,
}

/// The pass draws record into, and what the draws so far set on it.
struct OpenPass {
    pass: wgpu::RenderPass<'static>,
    /// Whether a pipeline statistics query counts the pass's work.
    #[cfg(feature = "bench")]
    counted: bool,
    target: PassTarget,
    /// The wgpu formats of its attachments, which a custom layer's pass
    /// tells.
    color_format: Option<wgpu::TextureFormat>,
    depth_stencil_format: Option<wgpu::TextureFormat>,
    /// The pipeline, bindings, global index and geometry set, `None` where
    /// a draw sets them again.
    pipeline: Option<PipelineId>,
    bindings: Option<BindingsId>,
    global_index: Option<u32>,
    geometry: Option<GeometryId>,
    /// The vertex buffers bound, by slot, and the index buffer, as
    /// [`Geometry::vertex_ids`]: geometries of other indices or attributes often
    /// share vertex buffers.
    vertex_buffers: Vec<(BufferId, u64)>,
    index_buffer: Option<(BufferId, u64)>,
    dynamic: Option<DynamicState>,
}

impl OpenPass {
    /// Forgets what the draws set, as after a custom layer drew.
    fn forget_state(&mut self) {
        self.pipeline = None;
        self.bindings = None;
        self.global_index = None;
        self.geometry = None;
        self.vertex_buffers.clear();
        self.index_buffer = None;
        self.dynamic = None;
    }
}

/// A [`Gpu`] on a WebGPU [`Device`].
///
/// A shader's groups are WebGPU bind groups, made with the bindings.
/// `globalIndex` is a dynamic offset into a table of indices. Draws record
/// into a pass of the frame's command encoder at once, setting only what
/// changed since the draw before.
///
/// Small buffers share larger ones, so that buffers made together sit
/// together, their writes join one copy, and a frame that remakes many
/// small buffers makes few wgpu buffers. Writes are gathered and copied into
/// place ahead of the next flush's commands.
///
/// On a browser's WebGPU, a `create_*` call returns before the browser
/// validates it, so a rejected descriptor reaches the device's
/// uncaptured-error callback instead of the call's result.
pub struct WebGpu {
    device: Device,
    caps: Caps,
    index_table: IndexTable,
    buffers: SlotMap<BufferId, Buffer>,
    /// The page that small uniform and storage buffers are taken from, and
    /// how many of its bytes are taken.
    uniform_page: Option<(wgpu::Buffer, u64)>,
    /// The page that small vertex and index buffers are taken from, and how
    /// many of its bytes are taken.
    geometry_page: Option<(wgpu::Buffer, u64)>,
    /// The alignment of the offset a storage buffer binds at, which a small
    /// vertex buffer starts at in its page.
    storage_alignment: u64,
    writes: Writes,
    textures: SlotMap<TextureId, Texture>,
    samplers: SlotMap<SamplerId, wgpu::Sampler>,
    shaders: SlotMap<ShaderId, Shader>,
    layout_tags: LayoutTags,
    pipelines: SlotMap<PipelineId, Pipeline>,
    bindings: SlotMap<BindingsId, Bindings>,
    geometries: SlotMap<GeometryId, Geometry>,
    dependents: validation::Dependents,
    readbacks: SlotMap<ReadbackId, Readback>,
    /// What draws mips, made for the first texture whose mips are made.
    mipmapper: Option<Mipmapper>,
    /// The buffer of the last readback taken, which the next readback of
    /// its size copies into, as a frame read back each time is.
    spare_readback: Option<wgpu::Buffer>,
    /// The commands recorded since the last flush.
    encoder: Option<wgpu::CommandEncoder>,
    pass: Option<OpenPass>,
    /// The pipeline statistics and timestamp queries of each pass, where
    /// the device has them.
    #[cfg(feature = "bench")]
    queries: Option<crate::bench::Queries>,
}

impl WebGpu {
    /// How many textures the `WebGpu` has, imports included.
    #[cfg(test)]
    pub(crate) fn texture_count(&self) -> usize {
        self.textures.len()
    }

    #[must_use]
    pub fn new(device: &Device) -> WebGpu {
        let limits = device.device().limits();
        let caps = Caps {
            buffer_offset_alignment: limits
                .min_uniform_buffer_offset_alignment
                .max(limits.min_storage_buffer_offset_alignment),
            max_texture_size: limits.max_texture_dimension_2d,
            half_float_render_targets: true,
            clip_depth: ClipDepthRange::ZeroToOne,
            render_target_depth: Some(TextureFormat::Depth32Float),
            clear_stencil_with_pass: false,
            cpu_rasterizer: device.adapter_info().device_type == wgpu::DeviceType::Cpu,
            max_line_width: 1.0,
            binds_vertex_buffers: limits.max_storage_buffers_per_shader_stage >= VERTEX_STAGE_STORAGE_BUFFERS,
        };
        WebGpu {
            index_table: IndexTable::new(device.device()),
            device: device.clone(),
            caps,
            buffers: SlotMap::with_key(),
            uniform_page: None,
            geometry_page: None,
            storage_alignment: u64::from(limits.min_storage_buffer_offset_alignment).max(wgpu::COPY_BUFFER_ALIGNMENT),
            writes: Writes::default(),
            textures: SlotMap::with_key(),
            samplers: SlotMap::with_key(),
            shaders: SlotMap::with_key(),
            layout_tags: LayoutTags::default(),
            pipelines: SlotMap::with_key(),
            bindings: SlotMap::with_key(),
            geometries: SlotMap::with_key(),
            dependents: validation::Dependents::default(),
            readbacks: SlotMap::with_key(),
            mipmapper: None,
            spare_readback: None,
            #[cfg(feature = "bench")]
            queries: crate::bench::Queries::new(device.device())
                .map(|queries| queries.timed(device.device(), device.queue())),
            encoder: None,
            pass: None,
        }
    }

    /// Takes a texture made elsewhere, such as a surface's frame, as a
    /// texture of this backend. Destroying it drops this backend's
    /// reference.
    pub fn import_texture(&mut self, texture: &wgpu::Texture) -> TextureId {
        let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
        self.textures.insert(Texture { texture: texture.clone(), view, levels: Vec::new(), mip_groups: Vec::new() })
    }

    /// The wgpu texture of `texture`.
    #[cfg(test)]
    pub(crate) fn native_texture(&self, texture: TextureId) -> Option<&wgpu::Texture> {
        self.textures.get(texture).map(|texture| &texture.texture)
    }

    /// The wgpu buffer that `readback` copies into.
    #[cfg(test)]
    pub(crate) fn readback_buffer(&self, readback: ReadbackId) -> Option<&wgpu::Buffer> {
        self.readbacks.get(readback).map(|readback| &readback.buffer)
    }

    /// Submits the commands since the last flush after the copies of the
    /// writes made so far, with `then` recorded after them.
    fn submit(&mut self, then: impl FnOnce(&mut wgpu::CommandEncoder)) -> Result<wgpu::SubmissionIndex, GpuError> {
        let mut encoder = self.encoder.take().unwrap_or_else(|| {
            self.device.device().create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("frame") })
        });
        then(&mut encoder);
        #[cfg(feature = "bench")]
        let measured = self.queries.as_mut().and_then(|queries| queries.resolve(self.device.device(), &mut encoder));
        // wgpu reports what it rejects in the passes when they finish. The
        // writes stay for the next flush.
        let command_buffer = self.validated("flush", |_| encoder.finish())?;
        let writes = self.writes.encode(self.device.device(), self.device.queue());
        let submission = self.device.queue().submit(writes.into_iter().chain([command_buffer]));
        #[cfg(feature = "bench")]
        if let Some(measured) = measured {
            crate::bench::Queries::read_back(measured);
        }
        Ok(submission)
    }

    fn info(&self) -> (impl Fn(BufferId) -> Option<BufferInfo> + '_, impl Fn(TextureId) -> Option<TextureInfo> + '_) {
        let buffer =
            |id| self.buffers.get(id).map(|buffer: &Buffer| BufferInfo { usage: buffer.usage, size: buffer.size });
        let texture = |id| self.textures.get(id).map(|texture: &Texture| texture_info(&texture.texture));
        (buffer, texture)
    }

    /// Begins a pass of `desc`, which the checks passed, on the frame's
    /// encoder. Returns it, and the formats of its color and depth-stencil
    /// attachments.
    fn begin_render_pass(
        &mut self,
        desc: &PassDesc<'_>,
    ) -> Result<(wgpu::RenderPass<'static>, Option<wgpu::TextureFormat>, Option<wgpu::TextureFormat>), GpuError> {
        let texture = |id| self.textures.get(id).ok_or(GpuError::UnknownId("texture"));
        let color = desc
            .color
            .map(|attachment| Ok::<_, GpuError>((texture(attachment.texture)?, attachment.load)))
            .transpose()?;
        let depth_stencil = desc
            .depth_stencil
            .map(|attachment| Ok::<_, GpuError>((texture(attachment.texture)?, attachment)))
            .transpose()?;
        let color_format = color.map(|(texture, _)| texture.texture.format());
        let depth_stencil_format = depth_stencil.map(|(texture, _)| texture.texture.format());
        let color_attachment = color.map(|(texture, load)| wgpu::RenderPassColorAttachment {
            view: texture.attachment(),
            depth_slice: None,
            resolve_target: None,
            ops: wgpu::Operations {
                load: match load {
                    LoadOp::Load => wgpu::LoadOp::Load,
                    LoadOp::Clear(color) => wgpu::LoadOp::Clear(wgpu_color(color)),
                },
                store: wgpu::StoreOp::Store,
            },
        });
        let depth_stencil_attachment =
            depth_stencil.map(|(texture, attachment)| wgpu::RenderPassDepthStencilAttachment {
                view: texture.attachment(),
                depth_ops: Some(wgpu::Operations {
                    load: match attachment.depth {
                        LoadOp::Load => wgpu::LoadOp::Load,
                        LoadOp::Clear(depth) => wgpu::LoadOp::Clear(depth),
                    },
                    store: wgpu::StoreOp::Store,
                }),
                stencil_ops: texture.texture.format().has_stencil_aspect().then_some(wgpu::Operations {
                    load: match attachment.stencil {
                        LoadOp::Load => wgpu::LoadOp::Load,
                        LoadOp::Clear(stencil) => wgpu::LoadOp::Clear(stencil),
                    },
                    store: wgpu::StoreOp::Store,
                }),
            });
        let device = self.device.device();
        let encoder = self.encoder.get_or_insert_with(|| {
            device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("frame") })
        });
        #[cfg(feature = "bench")]
        let timestamp_writes = self.queries.as_ref().and_then(crate::bench::Queries::timestamp_writes);
        #[cfg(not(feature = "bench"))]
        let timestamp_writes = None;
        let pass = encoder
            .begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some(desc.label),
                color_attachments: &[color_attachment],
                depth_stencil_attachment,
                timestamp_writes,
                occlusion_query_set: None,
                multiview_mask: None,
            })
            .forget_lifetime();
        Ok((pass, color_format, depth_stencil_format))
    }

    fn no_pass(what: &str) -> GpuError {
        GpuError::Invalid(format!("{what} outside a pass"))
    }

    fn open_pass(what: &str) -> GpuError {
        GpuError::Invalid(format!("{what} while a pass is open"))
    }

    /// Blocks until the GPU has finished `submission`, or everything
    /// submitted with `None`.
    #[cfg(not(target_family = "wasm"))]
    fn wait_for(&self, submission: Option<&wgpu::SubmissionIndex>) -> Result<(), GpuError> {
        /// How long the wait goes on asking after waits that reported a
        /// timeout.
        const WAIT_AGAIN_LIMIT: std::time::Duration = std::time::Duration::from_secs(1);
        let device = self.device.device();
        let wait = || device.poll(wgpu::PollType::Wait { submission_index: submission.cloned(), timeout: None });
        // A wait without a timeout can still report one: PowerVR's Vulkan
        // driver on Android ends the wait shortly before the fence value
        // that wgpu reads afterwards shows the submission finished. Waiting
        // again returns at once then, so this asks until the value caught up.
        let start = web_time::Instant::now();
        let mut waited = wait();
        while matches!(waited, Err(wgpu::PollError::Timeout)) && start.elapsed() < WAIT_AGAIN_LIMIT {
            waited = wait();
        }
        waited.map(drop).map_err(|error| GpuError::Invalid(format!("waiting for the GPU: {error}")))
    }

    /// `size` bytes of a page that small uniform and storage buffers share,
    /// zero filled: the page and their offset in it.
    fn allocate_uniform(&mut self, size: u64) -> Result<(wgpu::Buffer, u64), GpuError> {
        let mut page = self.uniform_page.take();
        let allocated =
            allocate_in_page(&mut page, size.next_multiple_of(UNIFORM_ALIGNMENT), UNIFORM_ALIGNMENT, || {
                self.create_uniform_buffer("uniform page", PAGE)
            });
        self.uniform_page = page;
        allocated
    }

    /// `size` bytes of a page that small vertex and index buffers share,
    /// zero filled: the page and their offset in it, a multiple of
    /// `alignment`.
    fn allocate_geometry(&mut self, size: u64, alignment: u64) -> Result<(wgpu::Buffer, u64), GpuError> {
        let mut page = self.geometry_page.take();
        let allocated = allocate_in_page(&mut page, size, alignment, || {
            self.validated("geometry page", |device| {
                device.create_buffer(&wgpu::BufferDescriptor {
                    label: Some("geometry page"),
                    size: PAGE,
                    usage: VERTEX_USAGES | wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST,
                    mapped_at_creation: false,
                })
            })
        });
        self.geometry_page = page;
        allocated
    }

    /// A buffer that binds as a uniform or a storage buffer.
    fn create_uniform_buffer(&self, label: &str, size: u64) -> Result<wgpu::Buffer, GpuError> {
        self.validated(label, |device| {
            device.create_buffer(&wgpu::BufferDescriptor {
                label: Some(label),
                size,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            })
        })
    }

    /// Runs `create` in a validation error scope, as the error of the call
    /// that `what` names.
    fn validated<T>(&self, what: &str, create: impl FnOnce(&wgpu::Device) -> T) -> Result<T, GpuError> {
        let device = self.device.device();
        validated(device, || create(device)).map_err(|error| GpuError::Invalid(format!("{what}: {error}")))
    }
}

/// `size` bytes of `page`, or of a new page that `create` makes when it has
/// too few left: the page and the offset in it, a multiple of `alignment`.
fn allocate_in_page(
    page: &mut Option<(wgpu::Buffer, u64)>,
    size: u64,
    alignment: u64,
    create: impl FnOnce() -> Result<wgpu::Buffer, GpuError>,
) -> Result<(wgpu::Buffer, u64), GpuError> {
    let (buffer, taken) = match page {
        Some((buffer, taken)) if taken.next_multiple_of(alignment) + size <= PAGE => (buffer, taken),
        _ => {
            let (buffer, taken) = page.insert((create()?, 0));
            (buffer, taken)
        }
    };
    let offset = taken.next_multiple_of(alignment);
    *taken = offset + size;
    Ok((buffer.clone(), offset))
}

/// What the checks read of `texture`.
fn texture_info(texture: &wgpu::Texture) -> TextureInfo {
    let usage = texture.usage();
    TextureInfo {
        format: from_wgpu_format(texture.format()),
        size: Size { width: texture.width(), height: texture.height() },
        usage: TextureUsage {
            sampled: usage.contains(wgpu::TextureUsages::TEXTURE_BINDING),
            render_target: usage.contains(wgpu::TextureUsages::RENDER_ATTACHMENT),
            copy_src: usage.contains(wgpu::TextureUsages::COPY_SRC),
        },
    }
}

fn wgpu_color(color: Rgba) -> wgpu::Color {
    wgpu::Color { r: f64::from(color.r), g: f64::from(color.g), b: f64::from(color.b), a: f64::from(color.a) }
}

fn texture_format(format: TextureFormat) -> wgpu::TextureFormat {
    match format {
        // WGSL reads an alpha texture's one channel as red.
        TextureFormat::R8Unorm | TextureFormat::A8Unorm => wgpu::TextureFormat::R8Unorm,
        TextureFormat::R16Float => wgpu::TextureFormat::R16Float,
        TextureFormat::R32Float => wgpu::TextureFormat::R32Float,
        TextureFormat::Rgba8Unorm => wgpu::TextureFormat::Rgba8Unorm,
        TextureFormat::Rgba8UnormSrgb => wgpu::TextureFormat::Rgba8UnormSrgb,
        TextureFormat::Bgra8Unorm => wgpu::TextureFormat::Bgra8Unorm,
        TextureFormat::Bgra8UnormSrgb => wgpu::TextureFormat::Bgra8UnormSrgb,
        TextureFormat::Rgb10a2Unorm => wgpu::TextureFormat::Rgb10a2Unorm,
        TextureFormat::Rgba16Float => wgpu::TextureFormat::Rgba16Float,
        TextureFormat::Rgba32Float => wgpu::TextureFormat::Rgba32Float,
        TextureFormat::Depth32Float => wgpu::TextureFormat::Depth32Float,
        TextureFormat::Depth24PlusStencil8 => wgpu::TextureFormat::Depth24PlusStencil8,
        TextureFormat::Depth32FloatStencil8 => wgpu::TextureFormat::Depth32FloatStencil8,
    }
}

/// What `shader` reads from its bindings, by the shaders' ids.
fn shader_bindings(shader: &ShaderProgram) -> Vec<ShaderBinding> {
    let mut bindings = Vec::new();
    for &group in shader.bind_group_order() {
        if Some(group) == shader.index_group() {
            continue;
        }
        for info in shader.binding_infos(group) {
            let binding = match info.type_ {
                BindingType::UniformBuffer | BindingType::ReadOnlyStorageBuffer => {
                    ShaderBinding::Buffer { id: info.binding, min_size: info.min_size }
                }
                BindingType::Sampler
                | BindingType::NonFilteringSampler
                | BindingType::Texture
                | BindingType::UnfilterableTexture => {
                    ShaderBinding::Texture { slot: declaration::texture_slot(info.binding) as u32 }
                }
            };
            if !bindings.contains(&binding) {
                bindings.push(binding);
            }
        }
    }
    bindings
}

/// The format of `format`, `None` for a format the interface has no name
/// for.
pub(crate) fn from_wgpu_format(format: wgpu::TextureFormat) -> Option<TextureFormat> {
    Some(match format {
        wgpu::TextureFormat::R8Unorm => TextureFormat::R8Unorm,
        wgpu::TextureFormat::R16Float => TextureFormat::R16Float,
        wgpu::TextureFormat::R32Float => TextureFormat::R32Float,
        wgpu::TextureFormat::Rgba8Unorm => TextureFormat::Rgba8Unorm,
        wgpu::TextureFormat::Rgba8UnormSrgb => TextureFormat::Rgba8UnormSrgb,
        wgpu::TextureFormat::Bgra8Unorm => TextureFormat::Bgra8Unorm,
        wgpu::TextureFormat::Bgra8UnormSrgb => TextureFormat::Bgra8UnormSrgb,
        wgpu::TextureFormat::Rgb10a2Unorm => TextureFormat::Rgb10a2Unorm,
        wgpu::TextureFormat::Rgba16Float => TextureFormat::Rgba16Float,
        wgpu::TextureFormat::Rgba32Float => TextureFormat::Rgba32Float,
        wgpu::TextureFormat::Depth32Float => TextureFormat::Depth32Float,
        wgpu::TextureFormat::Depth24PlusStencil8 => TextureFormat::Depth24PlusStencil8,
        wgpu::TextureFormat::Depth32FloatStencil8 => TextureFormat::Depth32FloatStencil8,
        _ => return None,
    })
}

impl Gpu for WebGpu {
    fn caps(&self) -> &Caps {
        &self.caps
    }

    fn create_buffer(&mut self, desc: &BufferDesc<'_>) -> Result<BufferId, GpuError> {
        let size = desc.size.next_multiple_of(wgpu::COPY_BUFFER_ALIGNMENT);
        let usage = match desc.usage {
            BufferUsage::Vertex | BufferUsage::Index if size <= PAGE / 4 => {
                // A vertex buffer starts where a storage buffer may bind.
                let alignment = match desc.usage {
                    BufferUsage::Vertex => self.storage_alignment,
                    _ => wgpu::COPY_BUFFER_ALIGNMENT,
                };
                let (buffer, offset) = self.allocate_geometry(size, alignment)?;
                let written = Some(0);
                return Ok(self.buffers.insert(Buffer { buffer, usage: desc.usage, offset, size, written }));
            }
            BufferUsage::Vertex => VERTEX_USAGES,
            BufferUsage::Index => wgpu::BufferUsages::INDEX,
            BufferUsage::Uniform | BufferUsage::Storage => {
                let (buffer, offset, written) = if size <= PAGE / 4 {
                    let (page, offset) = self.allocate_uniform(size)?;
                    (page, offset, Some(0))
                } else {
                    (self.create_uniform_buffer(desc.label, size)?, 0, None)
                };
                return Ok(self.buffers.insert(Buffer { buffer, usage: desc.usage, offset, size, written }));
            }
        };
        let buffer = self.validated(desc.label, |device| {
            device.create_buffer(&wgpu::BufferDescriptor {
                label: Some(desc.label),
                size,
                usage: usage | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            })
        })?;
        Ok(self.buffers.insert(Buffer { buffer, usage: desc.usage, offset: 0, size, written: None }))
    }

    fn write_buffer(&mut self, buffer: BufferId, offset: u64, data: &[u8]) -> Result<(), GpuError> {
        let entry = self.buffers.get_mut(buffer).ok_or(GpuError::UnknownId("buffer"))?;
        validation::check_write(offset, data.len(), entry.size)?;
        let end = offset + data.len() as u64;
        // Where the rest of a buffer in a page still holds its zeros, they
        // can be written with the data, so that the copy joins the next
        // buffer's.
        let padded = match &mut entry.written {
            Some(written) if end >= *written => {
                *written = end;
                entry.size - offset
            }
            _ => data.len() as u64,
        };
        let (device, queue) = (self.device.device(), self.device.queue());
        self.writes.write_padded(device, queue, &entry.buffer, entry.offset + offset, data, padded);
        Ok(())
    }

    /// Writes from the JS array with `GPUQueue.writeBuffer`
    /// (`Writes::write_from_host`).
    #[cfg(target_family = "wasm")]
    fn write_buffer_from_host(
        &mut self,
        buffer: BufferId,
        offset: u64,
        data: &js_sys::Uint8Array,
    ) -> Result<(), GpuError> {
        let entry = self.buffers.get_mut(buffer).ok_or(GpuError::UnknownId("buffer"))?;
        let len = data.length() as usize;
        validation::check_write(offset, len.next_multiple_of(4), entry.size)?;
        let len = len as u64;
        // A later write past these bytes pads only past them.
        if let Some(written) = &mut entry.written {
            *written = (*written).max(offset + len);
        }
        let (device, queue) = (self.device.device(), self.device.queue());
        let at = entry.offset + offset;
        let aligned = len & !(wgpu::COPY_BUFFER_ALIGNMENT - 1);
        self.writes.write_from_host(device, queue, &entry.buffer, at, data, aligned);
        if aligned < len {
            // writeBuffer takes whole 4-byte words.
            let mut tail = [0u8; wgpu::COPY_BUFFER_ALIGNMENT as usize];
            data.subarray(aligned as u32, len as u32).copy_to(&mut tail[..(len - aligned) as usize]);
            self.writes.write_padded(device, queue, &entry.buffer, at + aligned, &tail, wgpu::COPY_BUFFER_ALIGNMENT);
        }
        Ok(())
    }

    fn create_texture(&mut self, desc: &TextureDesc<'_>) -> Result<TextureId, GpuError> {
        let mip_level_count = validation::check_mips(desc)?;
        let mut usage = wgpu::TextureUsages::empty();
        if desc.usage.sampled {
            usage |= wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST;
        }
        if desc.usage.render_target {
            usage |= wgpu::TextureUsages::RENDER_ATTACHMENT;
        }
        if desc.usage.copy_src {
            usage |= wgpu::TextureUsages::COPY_SRC;
        }
        let format = texture_format(desc.format);
        // A compute pass writes the mips of a texture that shaders can write,
        // on a device that runs it (`generate_mipmaps`).
        if desc.mipmapped
            && format == wgpu::TextureFormat::Rgba8Unorm
            && crate::mipmaps::computes(&self.device.device().limits())
        {
            usage |= wgpu::TextureUsages::STORAGE_BINDING;
        }
        let required = format.required_features();
        if !self.device.device().features().contains(required) {
            return Err(GpuError::Unsupported(format!("{:?} textures need {required:?}", desc.format)));
        }
        let texture = self.validated(desc.label, |device| {
            device.create_texture(&wgpu::TextureDescriptor {
                label: Some(desc.label),
                size: wgpu::Extent3d { width: desc.size.width, height: desc.size.height, depth_or_array_layers: 1 },
                mip_level_count,
                sample_count: 1,
                dimension: wgpu::TextureDimension::D2,
                format,
                usage,
                view_formats: &[],
            })
        })?;
        let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
        let levels = if desc.mipmapped {
            let level = |base_mip_level| wgpu::TextureViewDescriptor {
                base_mip_level,
                mip_level_count: Some(1),
                ..Default::default()
            };
            (0..mip_level_count).map(|base| texture.create_view(&level(base))).collect()
        } else {
            Vec::new()
        };
        Ok(self.textures.insert(Texture { texture, view, levels, mip_groups: Vec::new() }))
    }

    fn write_texture(&mut self, texture: TextureId, region: TextureRegion, data: &[u8]) -> Result<(), GpuError> {
        let texture = &self.textures.get(texture).ok_or(GpuError::UnknownId("texture"))?.texture;
        if !texture.usage().contains(wgpu::TextureUsages::COPY_DST) {
            return Err(GpuError::Invalid("a write to a texture that isn't sampled".to_owned()));
        }
        let format = from_wgpu_format(texture.format())
            .ok_or_else(|| GpuError::Invalid(format!("{:?} textures take no writes", texture.format())))?;
        let size = Size::new(texture.width(), texture.height());
        let bytes_per_row = region.write_bytes_per_row(size, format, data)?;
        self.device.queue().write_texture(
            wgpu::TexelCopyTextureInfo {
                texture,
                mip_level: 0,
                origin: wgpu::Origin3d { x: region.x, y: region.y, z: 0 },
                aspect: wgpu::TextureAspect::All,
            },
            data,
            wgpu::TexelCopyBufferLayout {
                offset: 0,
                bytes_per_row: Some(bytes_per_row as u32),
                rows_per_image: Some(region.size.height),
            },
            wgpu::Extent3d { width: region.size.width, height: region.size.height, depth_or_array_layers: 1 },
        );
        Ok(())
    }

    /// Draws each level from the one before, in a pass of its own.
    fn generate_mipmaps(&mut self, texture: TextureId) -> Result<(), GpuError> {
        if self.pass.is_some() {
            return Err(Self::open_pass("generating mips"));
        }
        let texture = self.textures.get_mut(texture).ok_or(GpuError::UnknownId("texture"))?;
        if texture.levels.is_empty() {
            return Err(GpuError::Invalid("generating the mips of a texture without them".to_owned()));
        }
        let device = self.device.device();
        let encoder = self.encoder.get_or_insert_with(|| {
            device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("frame") })
        });
        let mipmapper = self.mipmapper.get_or_insert_with(|| Mipmapper::new(device));
        if texture.texture.usage().contains(wgpu::TextureUsages::STORAGE_BINDING) {
            let size = texture.texture.size();
            mipmapper.generate_in_compute(
                device,
                encoder,
                size,
                &texture.levels,
                &mut texture.mip_groups,
                #[cfg(feature = "bench")]
                self.queries.as_mut(),
            );
            return Ok(());
        }
        mipmapper.generate(
            device,
            encoder,
            texture.texture.format(),
            &texture.levels,
            #[cfg(feature = "bench")]
            self.queries.as_mut(),
        );
        Ok(())
    }

    fn create_sampler(&mut self, state: &SamplerState) -> Result<SamplerId, GpuError> {
        let filter = match state.filter {
            TextureFilterType::Linear => wgpu::FilterMode::Linear,
            TextureFilterType::Nearest => wgpu::FilterMode::Nearest,
        };
        let address_mode = |wrap| match wrap {
            TextureWrapType::Repeat => wgpu::AddressMode::Repeat,
            TextureWrapType::Clamp => wgpu::AddressMode::ClampToEdge,
        };
        let sampler = self.validated("sampler", |device| {
            device.create_sampler(&wgpu::SamplerDescriptor {
                label: None,
                address_mode_u: address_mode(state.wrap_u),
                address_mode_v: address_mode(state.wrap_v),
                address_mode_w: wgpu::AddressMode::ClampToEdge,
                mag_filter: filter,
                min_filter: filter,
                mipmap_filter: if state.mipmapped {
                    wgpu::MipmapFilterMode::Linear
                } else {
                    wgpu::MipmapFilterMode::Nearest
                },
                // Without mips, level 0 only.
                lod_max_clamp: if state.mipmapped { 32.0 } else { 0.0 },
                anisotropy_clamp: u16::from(state.anisotropy()),
                ..Default::default()
            })
        })?;
        Ok(self.samplers.insert(sampler))
    }

    fn create_shader(&mut self, key: &ShaderKey) -> Result<ShaderId, GpuError> {
        let declared = key.program();
        let source =
            declared.sources.wgsl.ok_or_else(|| GpuError::Unsupported(format!("{} has no WGSL", declared.name)))?;
        let program = shader_program::compile(self.device.device(), self.index_table.layout(), key, source)?;
        let reads = shader_bindings(&program);
        Ok(self.shaders.insert(Shader { program, reads }))
    }

    fn create_pipeline(&mut self, desc: &PipelineDesc<'_>) -> Result<PipelineId, GpuError> {
        let shader = self.shaders.get(desc.shader).ok_or(GpuError::UnknownId("shader"))?;
        let pipeline = shader.program.render_pipeline(
            desc.vertex_buffers,
            &desc.state,
            desc.targets.color.map(texture_format),
            desc.targets.depth_stencil.map(texture_format),
        )?;
        let layout = self.layout_tags.tag(desc.shader, desc.vertex_buffers);
        let tags = PipelineTags { shader: desc.shader, layout, targets: desc.targets };
        let id = self.pipelines.insert(Pipeline { pipeline, tags });
        self.dependents.pipeline(id, desc.shader);
        Ok(id)
    }

    fn create_bindings(&mut self, desc: &BindingsDesc<'_>) -> Result<BindingsId, GpuError> {
        let shader = self.shaders.get(desc.shader).ok_or(GpuError::UnknownId("shader"))?;
        let elements = {
            let (buffer, texture) = self.info();
            let sampler = |id| self.samplers.contains_key(id);
            let lookup = Lookup { buffer: &buffer, texture: &texture, sampler: &sampler };
            validation::check_bindings(
                desc,
                &shader.reads,
                self.caps.buffer_offset_alignment,
                self.caps.binds_vertex_buffers,
                &lookup,
            )?
        };
        let program = &shader.program;
        let mut groups = Vec::new();
        let mut index_slot = None;
        for &group in program.bind_group_order() {
            if Some(group) == program.index_group() {
                index_slot = Some(group);
                continue;
            }
            let layout = program.bind_group_layout(group).ok_or(GpuError::UnknownId("bind group layout"))?;
            let mut entries = Vec::new();
            for info in program.binding_infos(group) {
                let missing = || {
                    GpuError::Invalid(format!(
                        "{} reads binding {} of group {}, which no entry binds",
                        program.name(),
                        info.binding,
                        info.group
                    ))
                };
                let resource = match info.type_ {
                    BindingType::UniformBuffer | BindingType::ReadOnlyStorageBuffer => {
                        let (buffer, offset, size) = desc
                            .entries
                            .iter()
                            .find_map(|entry| match *entry {
                                BindGroupEntry::Buffer { id, buffer, offset, size } if id == info.binding => {
                                    Some((buffer, offset, size))
                                }
                                _ => None,
                            })
                            .ok_or_else(missing)?;
                        let buffer = self.buffers.get(buffer).ok_or(GpuError::UnknownId("buffer"))?;
                        wgpu::BindingResource::Buffer(wgpu::BufferBinding {
                            buffer: &buffer.buffer,
                            offset: buffer.offset + offset,
                            size: wgpu::BufferSize::new(size),
                        })
                    }
                    BindingType::Sampler
                    | BindingType::NonFilteringSampler
                    | BindingType::Texture
                    | BindingType::UnfilterableTexture => {
                        let sampler = matches!(info.type_, BindingType::Sampler | BindingType::NonFilteringSampler);
                        let texture_slot = declaration::texture_slot(info.binding) as u32;
                        let (texture, texture_sampler) = desc
                            .entries
                            .iter()
                            .find_map(|entry| match *entry {
                                BindGroupEntry::Texture { slot, texture, sampler } if slot == texture_slot => {
                                    Some((texture, sampler))
                                }
                                _ => None,
                            })
                            .ok_or_else(missing)?;
                        if sampler {
                            wgpu::BindingResource::Sampler(
                                self.samplers.get(texture_sampler).ok_or(GpuError::UnknownId("sampler"))?,
                            )
                        } else {
                            wgpu::BindingResource::TextureView(
                                &self.textures.get(texture).ok_or(GpuError::UnknownId("texture"))?.view,
                            )
                        }
                    }
                };
                entries.push(wgpu::BindGroupEntry { binding: info.binding, resource });
            }
            let bind_group = self.validated(desc.label, |device| {
                device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: Some(desc.label),
                    layout,
                    entries: &entries,
                })
            })?;
            groups.push((group, bind_group));
        }
        let tags = BindingsTags { shader: desc.shader, elements };
        let id = self.bindings.insert(Bindings { groups, index_slot, tags });
        self.dependents.bindings(id, desc);
        Ok(id)
    }

    fn create_geometry(&mut self, desc: &GeometryDesc<'_>) -> Result<GeometryId, GpuError> {
        if !self.shaders.contains_key(desc.shader) {
            return Err(GpuError::UnknownId("shader"));
        }
        let ranges = {
            let (buffer, texture) = self.info();
            let lookup = Lookup { buffer: &buffer, texture: &texture, sampler: &|_| false };
            validation::check_geometry(desc, &lookup)?
        };
        let native = |id: BufferId, offset: u64| {
            let buffer = self.buffers.get(id).ok_or(GpuError::UnknownId("buffer"))?;
            Ok::<_, GpuError>((buffer.buffer.clone(), buffer.offset + offset..buffer.offset + buffer.size))
        };
        let vertex_buffers =
            desc.vertex_buffers.iter().map(|&(id, offset)| native(id, offset)).collect::<Result<Vec<_>, _>>()?;
        let index_buffer = native(desc.index_buffer, 0)?;
        let tags = GeometryTags { layout: self.layout_tags.tag(desc.shader, desc.layouts), ranges };
        let id = self.geometries.insert(Geometry {
            vertex_buffers,
            index_buffer,
            vertex_ids: desc.vertex_buffers.to_vec(),
            index_id: (desc.index_buffer, 0),
            tags,
        });
        self.dependents.geometry(id, desc);
        Ok(id)
    }

    /// Drops this backend's references: wgpu keeps a resource while
    /// bindings, geometry or recorded commands hold it.
    fn destroy(&mut self, resource: ResourceId) -> Result<(), GpuError> {
        self.dependents.destroy(resource)?;
        match resource {
            ResourceId::Buffer(id) => drop(self.buffers.remove(id)),
            ResourceId::Texture(id) => drop(self.textures.remove(id)),
            ResourceId::Sampler(id) => drop(self.samplers.remove(id)),
            ResourceId::Shader(id) => drop(self.shaders.remove(id)),
            ResourceId::Pipeline(id) => drop(self.pipelines.remove(id)),
            ResourceId::Bindings(id) => drop(self.bindings.remove(id)),
            ResourceId::Geometry(id) => drop(self.geometries.remove(id)),
            ResourceId::Readback(id) => drop(self.readbacks.remove(id)),
        }
        Ok(())
    }

    fn begin_pass(&mut self, desc: &PassDesc<'_>) -> Result<(), GpuError> {
        if self.pass.is_some() {
            return Err(Self::open_pass("a pass"));
        }
        let target = {
            let (_, texture) = self.info();
            validation::check_pass(desc, &texture)?
        };
        #[cfg_attr(not(feature = "bench"), expect(unused_mut, reason = "only the benchmarks' builds count the pass"))]
        let (mut pass, color_format, depth_stencil_format) = self.begin_render_pass(desc)?;
        #[cfg(feature = "bench")]
        let counted = self.queries.as_mut().is_some_and(|queries| queries.begin(&mut pass));
        self.pass = Some(OpenPass {
            #[cfg(feature = "bench")]
            counted,
            pass,
            target,
            color_format,
            depth_stencil_format,
            pipeline: None,
            bindings: None,
            global_index: None,
            geometry: None,
            vertex_buffers: Vec::new(),
            index_buffer: None,
            dynamic: Some(DynamicState {
                depth_range: (0.0, 1.0),
                scissor: Scissor { x: 0, y: 0, width: target.size.width, height: target.size.height },
                stencil_reference: 0,
                blend_constant: Rgba::new(0.0, 0.0, 0.0, 0.0),
                line_width: 1.0,
            }),
        });
        Ok(())
    }

    fn draw(&mut self, draw: &Draw) -> Result<(), GpuError> {
        let open = self.pass.as_mut().ok_or_else(|| Self::no_pass("a draw"))?;
        let pipeline = self.pipelines.get(draw.pipeline).ok_or(GpuError::UnknownId("pipeline"))?;
        let bindings = self.bindings.get(draw.bindings).ok_or(GpuError::UnknownId("bindings"))?;
        let geometry = self.geometries.get(draw.geometry).ok_or(GpuError::UnknownId("geometry"))?;
        validation::check_draw(draw, open.target.targets, &pipeline.tags, &bindings.tags, &geometry.tags)?;
        let pass = &mut open.pass;
        if open.pipeline != Some(draw.pipeline) {
            pass.set_pipeline(&pipeline.pipeline);
            open.pipeline = Some(draw.pipeline);
        }
        let bindings_changed = open.bindings != Some(draw.bindings);
        if bindings_changed {
            for (slot, group) in &bindings.groups {
                pass.set_bind_group(*slot, group, &[]);
            }
            open.bindings = Some(draw.bindings);
        }
        if let Some(slot) = bindings.index_slot
            && (bindings_changed || open.global_index != Some(draw.global_index))
        {
            self.index_table.reserve(self.device.device(), self.device.queue(), draw.global_index);
            let (group, offset) = self
                .index_table
                .get(draw.global_index)
                .ok_or_else(|| GpuError::Invalid(format!("global index {}", draw.global_index)))?;
            pass.set_bind_group(slot, group, &[offset]);
            open.global_index = Some(draw.global_index);
        }
        if open.geometry != Some(draw.geometry) {
            for (slot, ((buffer, range), id)) in geometry.vertex_buffers.iter().zip(&geometry.vertex_ids).enumerate() {
                if open.vertex_buffers.get(slot) != Some(id) {
                    pass.set_vertex_buffer(slot as u32, buffer.slice(range.clone()));
                    match open.vertex_buffers.get_mut(slot) {
                        Some(bound) => *bound = *id,
                        None => open.vertex_buffers.push(*id),
                    }
                }
            }
            if open.index_buffer != Some(geometry.index_id) {
                let (buffer, range) = &geometry.index_buffer;
                pass.set_index_buffer(buffer.slice(range.clone()), wgpu::IndexFormat::Uint16);
                open.index_buffer = Some(geometry.index_id);
            }
            open.geometry = Some(draw.geometry);
        }
        let dynamic = draw.dynamic;
        let last = open.dynamic.replace(dynamic);
        let changed = |same: fn(&DynamicState, &DynamicState) -> bool| last.is_none_or(|last| !same(&last, &dynamic));
        if changed(|a, b| a.depth_range == b.depth_range) {
            let size = open.target.size;
            let (min, max) = dynamic.depth_range;
            pass.set_viewport(0.0, 0.0, size.width as f32, size.height as f32, min, max);
        }
        if changed(|a, b| a.scissor == b.scissor) {
            let Scissor { x, y, width, height } = dynamic.scissor;
            pass.set_scissor_rect(x, y, width, height);
        }
        if changed(|a, b| a.stencil_reference == b.stencil_reference) {
            pass.set_stencil_reference(dynamic.stencil_reference);
        }
        if changed(|a, b| a.blend_constant == b.blend_constant) {
            pass.set_blend_constant(wgpu_color(dynamic.blend_constant));
        }
        // WebGPU draws lines 1 pixel wide.
        let indices = draw.first_index..draw.first_index + draw.index_count;
        let instances = draw.first_instance..draw.first_instance + draw.instance_count;
        pass.draw_indexed(indices, draw.base_vertex, instances);
        Ok(())
    }

    /// Lends the open pass, or the frame's command encoder. A pass that a
    /// custom layer draws into has a color attachment.
    fn lend(&mut self, f: &mut dyn FnMut(&mut dyn Any)) -> Result<(), GpuError> {
        let (device, queue) = (self.device.device().clone(), self.device.queue().clone());
        if let Some(mut open) = self.pass.take() {
            let Some(color_format) = open.color_format else {
                self.pass = Some(open);
                return Err(GpuError::Unsupported("a custom layer's pass without a color attachment".to_owned()));
            };
            let depth_stencil_format = open.depth_stencil_format;
            let mut lent =
                CustomLayerPass { device, queue, pass: open.pass, color_format, depth_stencil_format, sample_count: 1 };
            f(&mut lent);
            open.pass = lent.pass;
            open.forget_state();
            self.pass = Some(open);
            return Ok(());
        }
        let encoder = self
            .encoder
            .take()
            .unwrap_or_else(|| device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("frame") }));
        let mut lent = CustomLayerEncoder { device, queue, encoder };
        f(&mut lent);
        self.encoder = Some(lent.encoder);
        Ok(())
    }

    fn end_pass(&mut self) -> Result<(), GpuError> {
        #[cfg_attr(not(feature = "bench"), expect(unused_mut, reason = "only the benchmarks' builds count the pass"))]
        let mut open = self.pass.take().ok_or_else(|| Self::no_pass("the end of a pass"))?;
        #[cfg(feature = "bench")]
        if open.counted {
            crate::bench::Queries::end(&mut open.pass);
        }
        drop(open);
        Ok(())
    }

    fn flush(&mut self, wait: bool) -> Result<(), GpuError> {
        if self.pass.is_some() {
            return Err(Self::open_pass("a flush"));
        }
        let submission = self.submit(|_| {})?;
        #[cfg(not(target_family = "wasm"))]
        if wait {
            return self.wait_for(Some(&submission));
        }
        let _ = (wait, submission);
        Ok(())
    }

    /// Copies into a buffer that is mapped once the copy is done: the last
    /// readback's where it's as big, else a new one.
    fn read_texture(&mut self, texture: TextureId, region: TextureRegion) -> Result<ReadbackId, GpuError> {
        if self.pass.is_some() {
            return Err(Self::open_pass("a readback"));
        }
        let texture = self.textures.get(texture).map(|texture| texture.texture.clone());
        let readback = validation::check_readback(texture.as_ref().map(texture_info), region)?;
        let texture = texture.ok_or(GpuError::UnknownId("texture"))?;
        let padded_bytes_per_row = readback.bytes_per_row.next_multiple_of(wgpu::COPY_BYTES_PER_ROW_ALIGNMENT as usize);
        let (width, rows) = (region.size.width, region.size.height);
        let size = (padded_bytes_per_row * rows as usize) as u64;
        let buffer = match self.spare_readback.take() {
            Some(buffer) if buffer.size() == size => buffer,
            _ => self.validated("readback", |device| {
                device.create_buffer(&wgpu::BufferDescriptor {
                    label: Some("readback"),
                    size,
                    usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
                    mapped_at_creation: false,
                })
            })?,
        };
        let submission = self.submit(|encoder| {
            encoder.copy_texture_to_buffer(
                wgpu::TexelCopyTextureInfo {
                    texture: &texture,
                    mip_level: 0,
                    origin: wgpu::Origin3d { x: region.x, y: region.y, z: 0 },
                    aspect: wgpu::TextureAspect::All,
                },
                wgpu::TexelCopyBufferInfo {
                    buffer: &buffer,
                    layout: wgpu::TexelCopyBufferLayout {
                        offset: 0,
                        bytes_per_row: Some(padded_bytes_per_row as u32),
                        rows_per_image: Some(rows),
                    },
                },
                wgpu::Extent3d { width, height: rows, depth_or_array_layers: 1 },
            );
        })?;
        let mapped = Arc::new(OnceLock::new());
        let map_ended = Arc::clone(&mapped);
        buffer.map_async(wgpu::MapMode::Read, .., move |result| {
            let _ = map_ended.set(result);
        });
        Ok(self.readbacks.insert(Readback {
            buffer,
            padded_bytes_per_row,
            bytes_per_row: readback.bytes_per_row,
            rows: rows as usize,
            submission,
            mapped,
        }))
    }

    /// Polls the device for the map, or on a browser leaves it to the
    /// browser's event loop, which can't wait.
    fn take_readback(&mut self, readback: ReadbackId, wait: bool) -> Result<Option<Vec<u8>>, GpuError> {
        let entry = self.readbacks.get(readback).ok_or(GpuError::UnknownId("readback"))?;
        let mapped = Arc::clone(&entry.mapped);
        if mapped.get().is_none() {
            #[cfg(not(target_family = "wasm"))]
            if wait {
                if let Err(error) = self.wait_for(Some(&entry.submission)) {
                    self.readbacks.remove(readback);
                    return Err(error);
                }
            } else {
                let _ = self.device.device().poll(wgpu::PollType::Poll);
            }
            #[cfg(target_family = "wasm")]
            let _ = wait;
        }
        let Some(mapped) = mapped.get() else {
            return Ok(None);
        };
        let Readback { buffer, padded_bytes_per_row, bytes_per_row, rows, .. } =
            self.readbacks.remove(readback).ok_or(GpuError::UnknownId("readback"))?;
        if let Err(error) = mapped {
            return Err(GpuError::Invalid(format!("mapping a readback: {error}")));
        }
        let texels = {
            let range = buffer
                .get_mapped_range(..)
                .map_err(|error| GpuError::Invalid(format!("a readback's range: {error}")))?;
            if padded_bytes_per_row == bytes_per_row {
                range[..bytes_per_row * rows].to_vec()
            } else {
                let mut texels = Vec::with_capacity(bytes_per_row * rows);
                for row in range.chunks(padded_bytes_per_row).take(rows) {
                    texels.extend_from_slice(&row[..bytes_per_row]);
                }
                texels
            }
        };
        buffer.unmap();
        self.spare_readback = Some(buffer);
        Ok(Some(texels))
    }
}