viewport-lib 0.19.0

3D viewport rendering library
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
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
use crate::resources::*;

impl DeviceResources {
    /// Upload an RGBA texture to the GPU and return its texture ID.
    ///
    /// The ID can be stored in `Material::texture_id` to apply the texture to objects.
    /// `rgba_data` must be exactly `width * height * 4` bytes in RGBA8 format.
    ///
    /// # Errors
    ///
    /// Returns [`ViewportError::InvalidTextureData`](crate::error::ViewportError::InvalidTextureData) if the data length is incorrect.
    pub fn upload_texture(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        width: u32,
        height: u32,
        rgba_data: &[u8],
    ) -> crate::error::ViewportResult<crate::resources::TextureId> {
        // Sync wrapper around the async path: build the job, drive it to
        // completion on the calling thread, and take the typed result. The
        // worker performs the same texture creation, sampler setup, and
        // bind-group build that the apply closure runs from
        // `process_uploads`. The data is copied into an owned `Vec` for the
        // worker thread; small textures absorb this trivially, large
        // textures pay one extra memcpy.
        let id = self.begin_upload_texture(device, queue, width, height, rgba_data.to_vec())?;
        self.drain_until(device, queue, id)?;
        self.upload_result_texture(id)
    }

    /// Upload an RGBA texture as a normal map and return its texture ID.
    ///
    /// Uses Rgba8Unorm format (not sRGB) so values are linear : required for correct
    /// normal map decoding. `rgba_data` must be `width * height * 4` bytes.
    pub fn upload_normal_map(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        width: u32,
        height: u32,
        rgba_data: &[u8],
    ) -> crate::error::ViewportResult<crate::resources::TextureId> {
        // Sync wrapper; see `upload_texture` for the worker / apply layout.
        let id = self.begin_upload_normal_map(device, queue, width, height, rgba_data.to_vec())?;
        self.drain_until(device, queue, id)?;
        self.upload_result_texture(id)
    }

    // -----------------------------------------------------------------------
    // Async texture upload (routed through the upload-job runner)
    // -----------------------------------------------------------------------

    /// Start an asynchronous albedo texture upload.
    ///
    /// Returns a `JobId` immediately. The texture and bind group are built
    /// on a worker thread; `queue.write_texture` queues the pixel copy and
    /// the runner gates the job on a fresh submission that flushes those
    /// writes. Once the status is `Ready`, take the resulting texture id
    /// with `upload_result_texture` and store it in `Material::texture_id`.
    ///
    /// `rgba` transfers into the worker; clone at the call site to retain
    /// it. Format and binding match the synchronous `upload_texture`.
    ///
    /// # Errors
    ///
    /// Returns
    /// [`ViewportError::InvalidTextureData`](crate::error::ViewportError::InvalidTextureData)
    /// when `rgba.len() != width * height * 4`, reported before any job is
    /// submitted.
    pub fn begin_upload_texture(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        width: u32,
        height: u32,
        rgba: Vec<u8>,
    ) -> crate::error::ViewportResult<crate::resources::JobId> {
        let expected = (width * height * 4) as usize;
        if rgba.len() != expected {
            return Err(crate::error::ViewportError::InvalidTextureData {
                expected,
                actual: rgba.len(),
            });
        }
        Ok(self.spawn_texture_upload(
            device,
            queue,
            TextureUploadSpec {
                width,
                height,
                format: wgpu::TextureFormat::Rgba8UnormSrgb,
                is_normal_map: false,
                mip_levels: vec![rgba],
            },
        ))
    }

    /// Start an asynchronous normal-map upload.
    ///
    /// Same shape as `begin_upload_texture`, but the texture is created
    /// with the linear `Rgba8Unorm` format and bound into the normal-map
    /// slot. Take the result with `upload_result_texture` once `Ready`.
    ///
    /// # Errors
    ///
    /// Same as `begin_upload_texture`.
    pub fn begin_upload_normal_map(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        width: u32,
        height: u32,
        rgba: Vec<u8>,
    ) -> crate::error::ViewportResult<crate::resources::JobId> {
        let expected = (width * height * 4) as usize;
        if rgba.len() != expected {
            return Err(crate::error::ViewportError::InvalidTextureData {
                expected,
                actual: rgba.len(),
            });
        }
        Ok(self.spawn_texture_upload(
            device,
            queue,
            TextureUploadSpec {
                width,
                height,
                format: wgpu::TextureFormat::Rgba8Unorm,
                is_normal_map: true,
                mip_levels: vec![rgba],
            },
        ))
    }

    /// Take the texture id produced by a completed `begin_upload_texture`
    /// or `begin_upload_normal_map` job.
    ///
    /// Returns `JobNotReady` while the upload is still in flight, and
    /// `JobResultMissing` for ids that have already been taken, were
    /// issued by a different upload type, or never existed.
    pub fn upload_result_texture(
        &mut self,
        id: crate::resources::JobId,
    ) -> crate::error::ViewportResult<crate::resources::TextureId> {
        let mut map = self
            .job_results
            .texture
            .lock()
            .expect("texture result map poisoned");
        let slot = match map.get(&id) {
            Some(s) => s.clone(),
            None => {
                return Err(crate::error::ViewportError::JobResultMissing {
                    reason: "unknown id or wrong upload type",
                });
            }
        };
        match slot.take() {
            Some(tex_id) => {
                map.remove(&id);
                Ok(tex_id)
            }
            None => Err(crate::error::ViewportError::JobNotReady),
        }
    }

    /// Upload a pre-compressed, pre-mipped texture and return its texture ID.
    ///
    /// Sync wrapper around [`begin_upload_compressed_texture`](Self::begin_upload_compressed_texture):
    /// see that method for the format and layout requirements. Blocks the
    /// calling thread until the upload completes.
    ///
    /// # Errors
    ///
    /// See [`begin_upload_compressed_texture`](Self::begin_upload_compressed_texture).
    pub fn upload_compressed_texture(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        desc: CompressedTextureDesc<'_>,
    ) -> crate::error::ViewportResult<crate::resources::TextureId> {
        let id = self.begin_upload_compressed_texture(device, queue, desc)?;
        self.drain_until(device, queue, id)?;
        self.upload_result_texture(id)
    }

    /// Start an asynchronous upload of pre-compressed, pre-mipped texture data.
    ///
    /// Returns a `JobId` immediately; take the resulting texture id with
    /// [`upload_result_texture`](Self::upload_result_texture) once the status is
    /// `Ready`, then store it in a `Material` slot. The data is validated and
    /// copied into the worker before the job is submitted.
    ///
    /// # Errors
    ///
    /// Returns
    /// [`ViewportError::UnsupportedTextureFormat`](crate::error::ViewportError::UnsupportedTextureFormat)
    /// when `desc.format` is not block-compressed or the device lacks its
    /// required feature (check first with [`supports_texture_format`]), and
    /// [`ViewportError::InvalidCompressedTextureData`](crate::error::ViewportError::InvalidCompressedTextureData)
    /// when `mip_levels` is empty or a level's byte length does not match its
    /// block-packed size.
    pub fn begin_upload_compressed_texture(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        desc: CompressedTextureDesc<'_>,
    ) -> crate::error::ViewportResult<crate::resources::JobId> {
        if !desc.format.is_compressed() {
            return Err(crate::error::ViewportError::UnsupportedTextureFormat {
                format: desc.format,
            });
        }
        // wgpu requires block-compressed textures to be created with
        // block-aligned base dimensions. Reject early with a clear error rather
        // than letting `create_texture` fail on the worker (which would leave an
        // invalid texture bound into a bind group). Checked before device
        // support so the caller gets the specific reason either way.
        let (block_w, block_h) = desc.format.block_dimensions();
        if desc.width % block_w != 0 || desc.height % block_h != 0 {
            return Err(
                crate::error::ViewportError::CompressedTextureNotBlockAligned {
                    width: desc.width,
                    height: desc.height,
                    block_width: block_w,
                    block_height: block_h,
                },
            );
        }
        if !supports_texture_format(device, desc.format) {
            return Err(crate::error::ViewportError::UnsupportedTextureFormat {
                format: desc.format,
            });
        }
        if desc.mip_levels.is_empty() {
            let (_, _, expected) = mip_block_layout(desc.format, desc.width, desc.height);
            return Err(crate::error::ViewportError::InvalidCompressedTextureData {
                level: 0,
                expected,
                actual: 0,
            });
        }
        // Validate each level against its block-packed size and copy into owned
        // buffers for the worker thread.
        let mut mip_levels = Vec::with_capacity(desc.mip_levels.len());
        for (level, data) in desc.mip_levels.iter().enumerate() {
            let lw = (desc.width >> level).max(1);
            let lh = (desc.height >> level).max(1);
            let (_, _, expected) = mip_block_layout(desc.format, lw, lh);
            if data.len() != expected {
                return Err(crate::error::ViewportError::InvalidCompressedTextureData {
                    level: level as u32,
                    expected,
                    actual: data.len(),
                });
            }
            mip_levels.push(data.to_vec());
        }
        Ok(self.spawn_texture_upload(
            device,
            queue,
            TextureUploadSpec {
                width: desc.width,
                height: desc.height,
                format: desc.format,
                is_normal_map: desc.is_normal_map,
                mip_levels,
            },
        ))
    }

    /// Shared spawn path for the RGBA8 and compressed upload entry points.
    /// The spec carries the texture format, dimensions, mip chain, and the
    /// slot (albedo vs normal map) the texture occupies.
    fn spawn_texture_upload(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        spec: TextureUploadSpec,
    ) -> crate::resources::JobId {
        let slot = crate::resources::ResultSlot::<crate::resources::TextureId>::new();
        let slot_for_apply = slot.clone();

        // Clone the fallback views and the bind-group layout into the
        // worker so it can build the GpuTexture and bind group without
        // touching `self` from the worker thread.
        let bgl = self.texture_bind_group_layout.clone();
        let fallback_albedo_view = self.fallback_texture.view.clone();
        let fallback_normal_view = self.fallback_normal_map_view.clone();
        let fallback_ao_view = self.fallback_ao_map_view.clone();
        let data_bytes: u64 = spec.mip_levels.iter().map(|l| l.len() as u64).sum();

        let TextureUploadSpec {
            width,
            height,
            format,
            is_normal_map,
            mip_levels,
        } = spec;

        let id = {
            let mut runner = self.jobs.lock().expect("upload job runner poisoned");
            runner.submit_with_gpu(device, queue, move |dev, q, progress| {
                progress.set(0.2);
                let gpu_texture = build_gpu_texture(
                    dev,
                    q,
                    width,
                    height,
                    format,
                    is_normal_map,
                    &mip_levels,
                    &bgl,
                    &fallback_albedo_view,
                    &fallback_normal_view,
                    &fallback_ao_view,
                );
                progress.set(0.9);

                // Flush so the runner has a submission to gate on. Implicit
                // writes queued above are folded into this submit by wgpu.
                let encoder = dev.create_command_encoder(&wgpu::CommandEncoderDescriptor {
                    label: Some("async_texture_flush"),
                });
                let submission = q.submit(std::iter::once(encoder.finish()));
                progress.set(1.0);

                Ok(
                    crate::resources::upload_jobs::JobProduct::with_gpu_and_apply(
                        submission,
                        Box::new(move |resources: &mut DeviceResources| {
                            let tex_id = resources.content.textures.insert(gpu_texture, data_bytes);
                            slot_for_apply.set(tex_id);
                        }),
                    ),
                )
            })
        };

        self.job_results
            .texture
            .lock()
            .expect("texture result map poisoned")
            .insert(id, slot);
        id
    }

    // -----------------------------------------------------------------------
    // VRAM budget query
    // -----------------------------------------------------------------------

    /// Current GPU memory usage for user-uploaded textures.
    ///
    /// Counts bytes from `upload_texture`, `upload_normal_map`, and the
    /// async upload entries. Internal resources (shadow maps, colourmaps,
    /// IBL, post-processing targets) are not included.
    pub fn texture_memory_stats(&self) -> TextureMemoryStats {
        TextureMemoryStats {
            used_bytes: self.content.textures.allocated_bytes(),
            texture_count: self.content.textures.len() as u32,
        }
    }

    /// Release a user-uploaded texture, reclaiming its slot and GPU memory.
    ///
    /// Drops the `GpuTexture` (wgpu defers the real free until in-flight
    /// commands that reference it complete), bumps the slot generation so `id`
    /// no longer resolves, and evicts the cached bind groups that named the
    /// texture: the shared `material_bind_groups` / `instance_bind_groups`
    /// entries whose key contains `id`, and any per-mesh object bind group built
    /// against it (invalidated so the next `prepare` rebinds the fallback).
    ///
    /// Returns `true` if a texture was released, `false` if `id` did not resolve
    /// to a live texture (already freed, never uploaded, or a stale handle).
    /// Materials still holding `id` are not rewritten; they fall back to the
    /// fallback texture until reassigned.
    pub fn free_texture(&mut self, id: crate::resources::TextureId) -> bool {
        if !self.content.textures.remove(id) {
            return false;
        }
        self.evict_texture_bind_group_caches(id.raw());
        true
    }

    /// Replace the pixels of an already-uploaded texture in place, keeping the
    /// same `TextureId`.
    ///
    /// The handle stays valid: materials and items holding `id` pick up the new
    /// pixels on the next frame with no reassignment. The generation check is the
    /// in-flight guard, so a stale handle (its slot freed and reused) returns
    /// `StaleHandle` instead of overwriting whatever now occupies the slot. Use
    /// this for content that changes over time (a streamed or animated texture)
    /// where re-uploading and reassigning a fresh id would be wasteful.
    ///
    /// The texture is recreated as an `Rgba8UnormSrgb` albedo texture, matching
    /// [`upload_texture`](Self::upload_texture); `rgba_data` must be exactly
    /// `width * height * 4` bytes. Dimensions and format need not match the
    /// original upload.
    ///
    /// # Errors
    ///
    /// [`ViewportError::InvalidTextureData`](crate::error::ViewportError::InvalidTextureData)
    /// if the data length is wrong, or
    /// [`ViewportError::StaleHandle`](crate::error::ViewportError::StaleHandle)
    /// if `id` does not resolve to a live texture.
    pub fn replace_texture(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        id: crate::resources::TextureId,
        width: u32,
        height: u32,
        rgba_data: &[u8],
    ) -> crate::error::ViewportResult<()> {
        let expected = (width * height * 4) as usize;
        if rgba_data.len() != expected {
            return Err(crate::error::ViewportError::InvalidTextureData {
                expected,
                actual: rgba_data.len(),
            });
        }
        let gpu_texture = build_gpu_texture(
            device,
            queue,
            width,
            height,
            wgpu::TextureFormat::Rgba8UnormSrgb,
            false,
            std::slice::from_ref(&rgba_data.to_vec()),
            &self.texture_bind_group_layout,
            &self.fallback_texture.view,
            &self.fallback_normal_map_view,
            &self.fallback_ao_map_view,
        );
        let bytes = rgba_data.len() as u64;
        if self
            .content
            .textures
            .replace(id, gpu_texture, bytes)
            .is_none()
        {
            let (index, count) = (id.index(), self.content.textures.len());
            return Err(crate::error::ViewportError::StaleHandle { index, count });
        }
        // The slot's view changed, so bind groups cached against this id now
        // sample the old texture. Evict them exactly as `free_texture` does; they
        // rebuild against the new view on the next `prepare`.
        self.evict_texture_bind_group_caches(id.raw());
        Ok(())
    }

    /// Drop cached bind groups that named user texture slot `raw` so they rebuild
    /// against the current occupant (or the fallback). Shared by `free_texture`
    /// (slot now empty) and `replace_texture` (slot holds a new view).
    fn evict_texture_bind_group_caches(&mut self, raw: u64) {
        self.content
            .material_bind_groups
            .retain(|&(a, n, ao), _| a != raw && n != raw && ao != raw);
        self.instancing
            .bind_groups
            .retain(|&(a, n, ao), _| a != raw && n != raw && ao != raw);

        // Invalidate per-mesh object bind groups that sampled the texture so
        // `update_mesh_texture_bind_group` rebuilds them. `last_tex_key`
        // positions holding user-texture ids are albedo, normal, ao,
        // metallic-roughness, and emissive.
        const INVALID_KEY: (u64, u64, u64, u64, u64, u64, u64, u64, u64, u64, u64) = (
            u64::MAX,
            u64::MAX,
            u64::MAX,
            u64::MAX,
            u64::MAX,
            u64::MAX,
            u64::MAX,
            u64::MAX,
            u64::MAX,
            u64::MAX,
            u64::MAX,
        );
        for (_, mesh) in self.mesh_store.iter_mut() {
            let k = mesh.last_tex_key;
            if k.0 == raw || k.1 == raw || k.2 == raw || k.7 == raw || k.8 == raw {
                mesh.last_tex_key = INVALID_KEY;
            }
        }
    }
}

/// Everything `spawn_texture_upload` needs to create and fill a texture.
/// The RGBA8 entry points build a single-level spec; the compressed entry
/// point builds one with the caller's format and full mip chain.
struct TextureUploadSpec {
    width: u32,
    height: u32,
    format: wgpu::TextureFormat,
    /// Bind into the normal-map slot and label as such.
    is_normal_map: bool,
    /// One entry per mip level, level 0 first.
    mip_levels: Vec<Vec<u8>>,
}

/// Block-packed layout for a single mip level: `(bytes_per_row, rows_of_blocks,
/// total_bytes)`. Uses the format's block dimensions and size, so it is correct
/// for uncompressed formats (1x1 blocks) and block-compressed formats alike.
fn mip_block_layout(
    format: wgpu::TextureFormat,
    level_width: u32,
    level_height: u32,
) -> (u32, u32, usize) {
    let (block_w, block_h) = format.block_dimensions();
    let block_bytes = format
        .block_copy_size(None)
        .expect("texture format has no single block-copy size");
    let blocks_x = level_width.div_ceil(block_w);
    let blocks_y = level_height.div_ceil(block_h);
    let bytes_per_row = blocks_x * block_bytes;
    let total = (blocks_x * blocks_y * block_bytes) as usize;
    (bytes_per_row, blocks_y, total)
}

/// Build a `GpuTexture` (texture, view, sampler, bind group) from one or more
/// mip levels. Shared by the async upload worker and the synchronous
/// `replace_texture` path so both create identical resources.
///
/// `mip_levels` holds level 0 first; a single level keeps nearest-mip sampling,
/// more than one enables trilinear. `is_normal_map` selects which bind-group
/// slot the new view occupies (albedo slot 0 or normal slot 2), with the other
/// colour slots filled by the fallback views.
#[allow(clippy::too_many_arguments)]
fn build_gpu_texture(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    width: u32,
    height: u32,
    format: wgpu::TextureFormat,
    is_normal_map: bool,
    mip_levels: &[Vec<u8>],
    bgl: &wgpu::BindGroupLayout,
    fallback_albedo_view: &wgpu::TextureView,
    fallback_normal_view: &wgpu::TextureView,
    fallback_ao_view: &wgpu::TextureView,
) -> GpuTexture {
    let tex_label = if is_normal_map {
        "user_normal_map_texture"
    } else {
        "user_texture"
    };
    let mip_level_count = mip_levels.len() as u32;
    let texture = device.create_texture(&wgpu::TextureDescriptor {
        label: Some(tex_label),
        size: wgpu::Extent3d {
            width,
            height,
            depth_or_array_layers: 1,
        },
        mip_level_count,
        sample_count: 1,
        dimension: wgpu::TextureDimension::D2,
        format,
        usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
        view_formats: &[],
    });
    // Upload each mip level. Row/size math is block-based so it is correct for
    // both uncompressed (1x1 blocks) and block-compressed formats.
    for (level, data) in mip_levels.iter().enumerate() {
        let lw = (width >> level).max(1);
        let lh = (height >> level).max(1);
        let (bytes_per_row, blocks_high, _) = mip_block_layout(format, lw, lh);
        queue.write_texture(
            wgpu::TexelCopyTextureInfo {
                texture: &texture,
                mip_level: level as u32,
                origin: wgpu::Origin3d::ZERO,
                aspect: wgpu::TextureAspect::All,
            },
            data,
            wgpu::TexelCopyBufferLayout {
                offset: 0,
                bytes_per_row: Some(bytes_per_row),
                rows_per_image: Some(blocks_high),
            },
            wgpu::Extent3d {
                width: lw,
                height: lh,
                depth_or_array_layers: 1,
            },
        );
    }

    let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
    let mipmap_filter = if mip_level_count > 1 {
        wgpu::FilterMode::Linear
    } else {
        wgpu::FilterMode::Nearest
    };
    let sampler_label = if is_normal_map {
        "user_normal_map_sampler"
    } else {
        "user_texture_sampler"
    };
    let sampler =
        crate::resources::builders::repeat_linear_sampler(device, sampler_label, mipmap_filter);
    let (slot0_view, slot2_view) = if is_normal_map {
        (fallback_albedo_view, &view)
    } else {
        (&view, fallback_normal_view)
    };
    let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some(if is_normal_map {
            "user_normal_map_bg"
        } else {
            "user_texture_bg"
        }),
        layout: bgl,
        entries: &[
            wgpu::BindGroupEntry {
                binding: 0,
                resource: wgpu::BindingResource::TextureView(slot0_view),
            },
            wgpu::BindGroupEntry {
                binding: 1,
                resource: wgpu::BindingResource::Sampler(&sampler),
            },
            wgpu::BindGroupEntry {
                binding: 2,
                resource: wgpu::BindingResource::TextureView(slot2_view),
            },
            wgpu::BindGroupEntry {
                binding: 3,
                resource: wgpu::BindingResource::TextureView(fallback_ao_view),
            },
        ],
    });
    GpuTexture {
        texture,
        view,
        sampler,
        bind_group,
    }
}

/// True when `device` can sample `format`.
///
/// Checks the format's required wgpu feature (for example
/// `TEXTURE_COMPRESSION_BC`, `_ASTC`, or `_ETC2` for block-compressed
/// formats). Call this before `upload_compressed_texture` to decide whether to
/// hand the renderer compressed data or fall back to an uncompressed upload.
pub fn supports_texture_format(device: &wgpu::Device, format: wgpu::TextureFormat) -> bool {
    device.features().contains(format.required_features())
}

/// Pre-compressed, pre-mipped texture data, uploaded to the GPU as-is.
///
/// The library does no encoding or decoding: compress and build the mip chain
/// offline in your asset pipeline, then hand the block bytes here. `format`
/// must be a block-compressed format (BC, ASTC, or ETC2) that the device
/// supports (check with [`supports_texture_format`]). `mip_levels` holds one
/// tightly block-packed byte slice per level, level 0 (full size) first;
/// `mip_levels.len()` becomes the texture's mip count.
///
/// Color space is carried by `format` (for example `Bc7RgbaUnormSrgb` for
/// albedo versus `Bc5RgUnorm` for normals); `is_normal_map` only selects which
/// internal bind-group slot the texture occupies.
pub struct CompressedTextureDesc<'a> {
    /// Width of mip level 0, in texels.
    pub width: u32,
    /// Height of mip level 0, in texels.
    pub height: u32,
    /// Block-compressed texture format.
    pub format: wgpu::TextureFormat,
    /// Bind into the normal-map slot rather than the albedo slot.
    pub is_normal_map: bool,
    /// Block bytes per mip level, level 0 first.
    pub mip_levels: &'a [&'a [u8]],
}

impl DeviceResources {
    /// Get or create a cached material bind group for (albedo, normal_map, ao_map) texture combo.
    ///
    /// `u64::MAX` sentinel means "use fallback texture for that slot".
    /// The bind group is cached in `material_bind_groups` keyed by the 3-tuple.
    #[allow(dead_code)]
    pub(crate) fn get_material_bind_group(
        &mut self,
        device: &wgpu::Device,
        albedo_id: Option<crate::resources::TextureId>,
        normal_map_id: Option<crate::resources::TextureId>,
        ao_map_id: Option<crate::resources::TextureId>,
    ) -> &wgpu::BindGroup {
        let key = (
            albedo_id.map(|t| t.raw()).unwrap_or(u64::MAX),
            normal_map_id.map(|t| t.raw()).unwrap_or(u64::MAX),
            ao_map_id.map(|t| t.raw()).unwrap_or(u64::MAX),
        );

        if !self.content.material_bind_groups.contains_key(&key) {
            let albedo_view = match albedo_id {
                Some(id) if self.content.textures.get(id).is_some() => {
                    &self.content.textures.get(id).unwrap().view
                }
                _ => &self.fallback_texture.view,
            };
            let normal_view = match normal_map_id {
                Some(id) if self.content.textures.get(id).is_some() => {
                    &self.content.textures.get(id).unwrap().view
                }
                _ => &self.fallback_normal_map_view,
            };
            let ao_view = match ao_map_id {
                Some(id) if self.content.textures.get(id).is_some() => {
                    &self.content.textures.get(id).unwrap().view
                }
                _ => &self.fallback_ao_map_view,
            };

            let bg = device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("material_bg"),
                layout: &self.texture_bind_group_layout,
                entries: &[
                    wgpu::BindGroupEntry {
                        binding: 0,
                        resource: wgpu::BindingResource::TextureView(albedo_view),
                    },
                    wgpu::BindGroupEntry {
                        binding: 1,
                        resource: wgpu::BindingResource::Sampler(&self.material_sampler),
                    },
                    wgpu::BindGroupEntry {
                        binding: 2,
                        resource: wgpu::BindingResource::TextureView(normal_view),
                    },
                    wgpu::BindGroupEntry {
                        binding: 3,
                        resource: wgpu::BindingResource::TextureView(ao_view),
                    },
                ],
            });
            self.content.material_bind_groups.insert(key, bg);
        }

        self.content.material_bind_groups.get(&key).unwrap()
    }

    /// Rebuild `mesh.object_bind_group` so it includes the texture views, LUT, and scalar
    /// buffer for the given material + attribute key. Called from `prepare()` when
    /// `mesh.last_tex_key` differs from the current frame's material/attribute state.
    ///
    /// The bind group layout is `object_bgl`:
    ///   binding 0 -> object uniform buffer
    ///   binding 1 -> albedo texture view
    ///   binding 2 -> material sampler (also used for LUT sampling)
    ///   binding 3 -> normal map view
    ///   binding 4 -> AO map view
    ///   binding 5 -> LUT (colourmap) texture view
    ///   binding 6 -> scalar attribute storage buffer
    pub(crate) fn update_mesh_texture_bind_group(
        &mut self,
        device: &wgpu::Device,
        mesh_id: crate::resources::mesh::mesh_store::MeshId,
        albedo_id: Option<crate::resources::TextureId>,
        normal_map_id: Option<crate::resources::TextureId>,
        ao_map_id: Option<crate::resources::TextureId>,
        lut_id: Option<ColourmapId>,
        active_attr: Option<&str>,
        matcap_id: Option<crate::resources::MatcapId>,
        warp_attr: Option<&str>,
        metallic_roughness_id: Option<crate::resources::TextureId>,
        emissive_texture_id: Option<crate::resources::TextureId>,
    ) {
        let hash_str = |name: &str| -> u64 {
            use std::hash::{Hash, Hasher};
            let mut h = std::collections::hash_map::DefaultHasher::new();
            name.hash(&mut h);
            h.finish()
        };
        let attr_hash = active_attr.map(|n| hash_str(n)).unwrap_or(u64::MAX);
        let warp_hash = warp_attr.map(|n| hash_str(n)).unwrap_or(u64::MAX);

        // The last two slots track GPU position/normal override (re)bind events.
        // Bumped by `set_*_override_buffer` / `clear_*_override`, so a fresh
        // override forces a bind-group rebuild here.
        let (pos_override_gen, nrm_override_gen) = {
            let Some(mesh) = self.mesh_store.get(mesh_id) else {
                return;
            };
            (mesh.position_override_gen, mesh.normal_override_gen)
        };

        let key = (
            albedo_id.map(|t| t.raw()).unwrap_or(u64::MAX),
            normal_map_id.map(|t| t.raw()).unwrap_or(u64::MAX),
            ao_map_id.map(|t| t.raw()).unwrap_or(u64::MAX),
            lut_id.map(|id| id.0 as u64).unwrap_or(u64::MAX),
            attr_hash,
            matcap_id.map(|id| id.index as u64).unwrap_or(u64::MAX),
            warp_hash,
            metallic_roughness_id.map(|t| t.raw()).unwrap_or(u64::MAX),
            emissive_texture_id.map(|t| t.raw()).unwrap_or(u64::MAX),
            pos_override_gen,
            nrm_override_gen,
        );

        {
            let Some(mesh) = self.mesh_store.get(mesh_id) else {
                return;
            };
            if mesh.last_tex_key == key {
                return;
            }
        }

        let albedo_view = match albedo_id {
            Some(id) if self.content.textures.get(id).is_some() => {
                &self.content.textures.get(id).unwrap().view
            }
            _ => &self.fallback_texture.view,
        };
        let normal_view = match normal_map_id {
            Some(id) if self.content.textures.get(id).is_some() => {
                &self.content.textures.get(id).unwrap().view
            }
            _ => &self.fallback_normal_map_view,
        };
        let ao_view = match ao_map_id {
            Some(id) if self.content.textures.get(id).is_some() => {
                &self.content.textures.get(id).unwrap().view
            }
            _ => &self.fallback_ao_map_view,
        };
        let lut_view = match lut_id {
            Some(id) if id.0 < self.content.colourmap_views.len() => {
                &self.content.colourmap_views[id.0]
            }
            _ => &self.content.fallback_lut_view,
        };

        let Some(mesh) = self.mesh_store.get_mut(mesh_id) else {
            return;
        };

        let scalar_buf: &wgpu::Buffer = match active_attr {
            Some(name) => {
                let found_vertex = mesh.attribute_buffers.get(name);
                let found_face = mesh.face_attribute_buffers.get(name);
                found_vertex
                    .or(found_face)
                    .unwrap_or(&self.content.fallback_scalar_buf)
            }
            None => &self.content.fallback_scalar_buf,
        };

        let face_colour_buf: &wgpu::Buffer = match active_attr {
            Some(name) => mesh
                .face_colour_buffers
                .get(name)
                .unwrap_or(&self.content.fallback_face_colour_buf),
            None => &self.content.fallback_face_colour_buf,
        };

        // Resolve matcap texture view : fallback to 1x1 white when no matcap active.
        let matcap_view: &wgpu::TextureView = match matcap_id {
            Some(id) if id.index < self.content.matcap_views.len() => {
                &self.content.matcap_views[id.index]
            }
            _ => self
                .content
                .fallback_matcap_view
                .as_ref()
                .unwrap_or(&self.fallback_texture.view),
        };

        let warp_buf: &wgpu::Buffer = match warp_attr {
            Some(name) => mesh
                .vector_attribute_buffers
                .get(name)
                .unwrap_or(&self.content.fallback_warp_buf),
            None => &self.content.fallback_warp_buf,
        };

        let position_override_buf: &wgpu::Buffer = mesh
            .position_override_buffer
            .as_ref()
            .unwrap_or(&self.content.fallback_position_override_buf);
        let normal_override_buf: &wgpu::Buffer = mesh
            .normal_override_buffer
            .as_ref()
            .unwrap_or(&self.content.fallback_normal_override_buf);

        let metallic_roughness_view: &wgpu::TextureView = match metallic_roughness_id {
            Some(id) if self.content.textures.get(id).is_some() => {
                &self.content.textures.get(id).unwrap().view
            }
            _ => &self.fallback_metallic_roughness_texture_view,
        };
        let emissive_view: &wgpu::TextureView = match emissive_texture_id {
            Some(id) if self.content.textures.get(id).is_some() => {
                &self.content.textures.get(id).unwrap().view
            }
            _ => &self.fallback_emissive_texture_view,
        };

        mesh.object_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("object_bind_group"),
            layout: &self.object_bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: mesh.object_uniform_buf.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::TextureView(albedo_view),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: wgpu::BindingResource::Sampler(&self.material_sampler),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: wgpu::BindingResource::TextureView(normal_view),
                },
                wgpu::BindGroupEntry {
                    binding: 4,
                    resource: wgpu::BindingResource::TextureView(ao_view),
                },
                wgpu::BindGroupEntry {
                    binding: 5,
                    resource: wgpu::BindingResource::TextureView(lut_view),
                },
                wgpu::BindGroupEntry {
                    binding: 6,
                    resource: scalar_buf.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 7,
                    resource: wgpu::BindingResource::TextureView(matcap_view),
                },
                wgpu::BindGroupEntry {
                    binding: 8,
                    resource: face_colour_buf.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 9,
                    resource: warp_buf.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 10,
                    resource: wgpu::BindingResource::Sampler(&self.lut_sampler),
                },
                wgpu::BindGroupEntry {
                    binding: 11,
                    resource: wgpu::BindingResource::TextureView(metallic_roughness_view),
                },
                wgpu::BindGroupEntry {
                    binding: 12,
                    resource: wgpu::BindingResource::TextureView(emissive_view),
                },
                wgpu::BindGroupEntry {
                    binding: 13,
                    resource: position_override_buf.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 14,
                    resource: normal_override_buf.as_entire_binding(),
                },
            ],
        });
        mesh.last_tex_key = key;
    }

    /// Build an object bind group that pairs an external per-item uniform buffer with
    /// the mesh's textures/LUT/matcap/scalar/override resources. Returns the bind group
    /// and the cache key that was used to construct it.
    ///
    /// This mirrors the resource-resolution in `update_mesh_texture_bind_group`, but
    /// reads from a caller-supplied uniform buffer instead of the mesh's shared
    /// `object_uniform_buf`. The per-object draw path uses one of these per scene item
    /// so that items sharing a `MeshId` each get their own transform.
    pub(crate) fn build_per_item_object_bind_group(
        &self,
        device: &wgpu::Device,
        mesh_id: crate::resources::mesh::mesh_store::MeshId,
        item_uniform_buf: &wgpu::Buffer,
        albedo_id: Option<crate::resources::TextureId>,
        normal_map_id: Option<crate::resources::TextureId>,
        ao_map_id: Option<crate::resources::TextureId>,
        lut_id: Option<ColourmapId>,
        active_attr: Option<&str>,
        matcap_id: Option<crate::resources::MatcapId>,
        warp_attr: Option<&str>,
        metallic_roughness_id: Option<crate::resources::TextureId>,
        emissive_texture_id: Option<crate::resources::TextureId>,
        prev_key: Option<u64>,
    ) -> Option<(wgpu::BindGroup, u64)> {
        let hash_str = |name: &str| -> u64 {
            use std::hash::{Hash, Hasher};
            let mut h = std::collections::hash_map::DefaultHasher::new();
            name.hash(&mut h);
            h.finish()
        };
        let attr_hash = active_attr.map(|n| hash_str(n)).unwrap_or(u64::MAX);
        let warp_hash = warp_attr.map(|n| hash_str(n)).unwrap_or(u64::MAX);

        let mesh = self.mesh_store.get(mesh_id)?;
        let pos_override_gen = mesh.position_override_gen;
        let nrm_override_gen = mesh.normal_override_gen;

        let cache_key = {
            use std::hash::{Hash, Hasher};
            let mut h = std::collections::hash_map::DefaultHasher::new();
            mesh_id.index().hash(&mut h);
            albedo_id.map(|t| t.raw()).unwrap_or(u64::MAX).hash(&mut h);
            normal_map_id
                .map(|t| t.raw())
                .unwrap_or(u64::MAX)
                .hash(&mut h);
            ao_map_id.map(|t| t.raw()).unwrap_or(u64::MAX).hash(&mut h);
            lut_id
                .map(|id| id.0 as u64)
                .unwrap_or(u64::MAX)
                .hash(&mut h);
            attr_hash.hash(&mut h);
            matcap_id
                .map(|id| id.index as u64)
                .unwrap_or(u64::MAX)
                .hash(&mut h);
            warp_hash.hash(&mut h);
            metallic_roughness_id
                .map(|t| t.raw())
                .unwrap_or(u64::MAX)
                .hash(&mut h);
            emissive_texture_id
                .map(|t| t.raw())
                .unwrap_or(u64::MAX)
                .hash(&mut h);
            pos_override_gen.hash(&mut h);
            nrm_override_gen.hash(&mut h);
            h.finish()
        };

        // Cache hit: the previously built bind group is still valid, so skip the
        // create_bind_group below. The caller keeps its existing bind group. The
        // per-item uniform write happens at the call site regardless, since the
        // transform changes each frame.
        if prev_key == Some(cache_key) {
            return None;
        }

        let albedo_view = match albedo_id {
            Some(id) if self.content.textures.get(id).is_some() => {
                &self.content.textures.get(id).unwrap().view
            }
            _ => &self.fallback_texture.view,
        };
        let normal_view = match normal_map_id {
            Some(id) if self.content.textures.get(id).is_some() => {
                &self.content.textures.get(id).unwrap().view
            }
            _ => &self.fallback_normal_map_view,
        };
        let ao_view = match ao_map_id {
            Some(id) if self.content.textures.get(id).is_some() => {
                &self.content.textures.get(id).unwrap().view
            }
            _ => &self.fallback_ao_map_view,
        };
        let lut_view = match lut_id {
            Some(id) if id.0 < self.content.colourmap_views.len() => {
                &self.content.colourmap_views[id.0]
            }
            _ => &self.content.fallback_lut_view,
        };

        let scalar_buf: &wgpu::Buffer = match active_attr {
            Some(name) => {
                let found_vertex = mesh.attribute_buffers.get(name);
                let found_face = mesh.face_attribute_buffers.get(name);
                found_vertex
                    .or(found_face)
                    .unwrap_or(&self.content.fallback_scalar_buf)
            }
            None => &self.content.fallback_scalar_buf,
        };

        let face_colour_buf: &wgpu::Buffer = match active_attr {
            Some(name) => mesh
                .face_colour_buffers
                .get(name)
                .unwrap_or(&self.content.fallback_face_colour_buf),
            None => &self.content.fallback_face_colour_buf,
        };

        let matcap_view: &wgpu::TextureView = match matcap_id {
            Some(id) if id.index < self.content.matcap_views.len() => {
                &self.content.matcap_views[id.index]
            }
            _ => self
                .content
                .fallback_matcap_view
                .as_ref()
                .unwrap_or(&self.fallback_texture.view),
        };

        let warp_buf: &wgpu::Buffer = match warp_attr {
            Some(name) => mesh
                .vector_attribute_buffers
                .get(name)
                .unwrap_or(&self.content.fallback_warp_buf),
            None => &self.content.fallback_warp_buf,
        };

        let position_override_buf: &wgpu::Buffer = mesh
            .position_override_buffer
            .as_ref()
            .unwrap_or(&self.content.fallback_position_override_buf);
        let normal_override_buf: &wgpu::Buffer = mesh
            .normal_override_buffer
            .as_ref()
            .unwrap_or(&self.content.fallback_normal_override_buf);

        let metallic_roughness_view: &wgpu::TextureView = match metallic_roughness_id {
            Some(id) if self.content.textures.get(id).is_some() => {
                &self.content.textures.get(id).unwrap().view
            }
            _ => &self.fallback_metallic_roughness_texture_view,
        };
        let emissive_view: &wgpu::TextureView = match emissive_texture_id {
            Some(id) if self.content.textures.get(id).is_some() => {
                &self.content.textures.get(id).unwrap().view
            }
            _ => &self.fallback_emissive_texture_view,
        };

        let bg = device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("per_item_object_bind_group"),
            layout: &self.object_bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: item_uniform_buf.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::TextureView(albedo_view),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: wgpu::BindingResource::Sampler(&self.material_sampler),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: wgpu::BindingResource::TextureView(normal_view),
                },
                wgpu::BindGroupEntry {
                    binding: 4,
                    resource: wgpu::BindingResource::TextureView(ao_view),
                },
                wgpu::BindGroupEntry {
                    binding: 5,
                    resource: wgpu::BindingResource::TextureView(lut_view),
                },
                wgpu::BindGroupEntry {
                    binding: 6,
                    resource: scalar_buf.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 7,
                    resource: wgpu::BindingResource::TextureView(matcap_view),
                },
                wgpu::BindGroupEntry {
                    binding: 8,
                    resource: face_colour_buf.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 9,
                    resource: warp_buf.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 10,
                    resource: wgpu::BindingResource::Sampler(&self.lut_sampler),
                },
                wgpu::BindGroupEntry {
                    binding: 11,
                    resource: wgpu::BindingResource::TextureView(metallic_roughness_view),
                },
                wgpu::BindGroupEntry {
                    binding: 12,
                    resource: wgpu::BindingResource::TextureView(emissive_view),
                },
                wgpu::BindGroupEntry {
                    binding: 13,
                    resource: position_override_buf.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 14,
                    resource: normal_override_buf.as_entire_binding(),
                },
            ],
        });
        Some((bg, cache_key))
    }

    /// Upload a 256-sample RGBA colourmap to the GPU and return its `ColourmapId`.
    ///
    /// The returned ID can be stored in `SceneRenderItem::colourmap_id`.
    /// Use `BuiltinColourmap` variants + [`Self::builtin_colourmap_id`] for the built-in presets.
    pub fn upload_colourmap(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        rgba_data: &[[u8; 4]; 256],
    ) -> ColourmapId {
        let texture = device.create_texture(&wgpu::TextureDescriptor {
            label: Some("lut_texture"),
            size: wgpu::Extent3d {
                width: 256,
                height: 1,
                depth_or_array_layers: 1,
            },
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format: wgpu::TextureFormat::Rgba8Unorm,
            usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
            view_formats: &[],
        });
        let flat: Vec<u8> = rgba_data.iter().flat_map(|p| p.iter().copied()).collect();
        queue.write_texture(
            wgpu::TexelCopyTextureInfo {
                texture: &texture,
                mip_level: 0,
                origin: wgpu::Origin3d::ZERO,
                aspect: wgpu::TextureAspect::All,
            },
            &flat,
            wgpu::TexelCopyBufferLayout {
                offset: 0,
                bytes_per_row: Some(256 * 4),
                rows_per_image: Some(1),
            },
            wgpu::Extent3d {
                width: 256,
                height: 1,
                depth_or_array_layers: 1,
            },
        );
        let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
        let id = ColourmapId(self.content.colourmap_textures.len());
        self.content.colourmap_textures.push(texture);
        self.content.colourmap_views.push(view);
        self.content.colourmaps_cpu.push(*rgba_data);
        id
    }

    /// Return the CPU-side colourmap LUT for `id` as 256 RGBA8 entries, or `None` if the id is invalid.
    ///
    /// Useful for any non-GPU colourmap output: PDF export, table cell colouring, custom legend
    /// widgets, or sampling a colour at a specific scalar value. The data is always in memory
    /// (kept for GPU upload) so this accessor is free.
    pub fn get_colourmap_rgba(&self, id: ColourmapId) -> Option<&[[u8; 4]; 256]> {
        self.content.colourmaps_cpu.get(id.0)
    }

    /// Return the `ColourmapId` for a built-in preset.
    ///
    /// Call [`Self::ensure_colourmaps_initialized`] first (done automatically by
    /// `ViewportRenderer::prepare`).  Panics if colourmaps have not been initialized yet.
    pub fn builtin_colourmap_id(&self, preset: BuiltinColourmap) -> ColourmapId {
        self.content
            .builtin_colourmap_ids
            .expect("call ensure_colourmaps_initialized before using built-in colourmaps")
            [preset as usize]
    }

    /// Ensure built-in colourmaps are uploaded to the GPU.
    ///
    /// Called automatically by `ViewportRenderer::prepare()` on the first frame.
    /// Safe to call multiple times : no-op after first invocation.
    pub fn ensure_colourmaps_initialized(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) {
        if self.content.colourmaps_initialized {
            return;
        }
        let viridis = self.upload_colourmap(
            device,
            queue,
            &crate::resources::material::colourmap_data::viridis_rgba(),
        );
        let plasma = self.upload_colourmap(
            device,
            queue,
            &crate::resources::material::colourmap_data::plasma_rgba(),
        );
        let greyscale = self.upload_colourmap(
            device,
            queue,
            &crate::resources::material::colourmap_data::greyscale_rgba(),
        );
        let coolwarm = self.upload_colourmap(
            device,
            queue,
            &crate::resources::material::colourmap_data::coolwarm_rgba(),
        );
        let rainbow = self.upload_colourmap(
            device,
            queue,
            &crate::resources::material::colourmap_data::rainbow_rgba(),
        );
        let magma = self.upload_colourmap(
            device,
            queue,
            &crate::resources::material::colourmap_data::magma_rgba(),
        );
        let inferno = self.upload_colourmap(
            device,
            queue,
            &crate::resources::material::colourmap_data::inferno_rgba(),
        );
        let turbo = self.upload_colourmap(
            device,
            queue,
            &crate::resources::material::colourmap_data::turbo_rgba(),
        );
        let jet = self.upload_colourmap(
            device,
            queue,
            &crate::resources::material::colourmap_data::jet_rgba(),
        );
        let rdbu = self.upload_colourmap(
            device,
            queue,
            &crate::resources::material::colourmap_data::rdbu_r_rgba(),
        );
        self.content.builtin_colourmap_ids = Some([
            viridis, plasma, greyscale, coolwarm, rainbow, magma, inferno, turbo, jet, rdbu,
        ]);
        self.content.colourmaps_initialized = true;
    }

    // -----------------------------------------------------------------------
    // Matcap texture API
    // -----------------------------------------------------------------------

    /// Upload a 256x256 RGBA matcap texture and return its `MatcapId`.
    ///
    /// `rgba_data` must be exactly `256 * 256 * 4 = 262_144` bytes.
    /// Set `blendable = true` for matcaps whose alpha channel tints the base
    /// geometry colour; `false` for static matcaps that fully replace the colour.
    ///
    /// # Errors
    ///
    /// Returns [`ViewportError::InvalidTextureData`](crate::error::ViewportError::InvalidTextureData)
    /// if `rgba_data` has the wrong length.
    pub fn upload_matcap(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        rgba_data: &[u8],
        blendable: bool,
    ) -> crate::error::ViewportResult<crate::resources::MatcapId> {
        let (width, height) = (256u32, 256u32);
        let expected = (width * height * 4) as usize;
        if rgba_data.len() != expected {
            return Err(crate::error::ViewportError::InvalidTextureData {
                expected,
                actual: rgba_data.len(),
            });
        }

        let texture = device.create_texture(&wgpu::TextureDescriptor {
            label: Some("matcap_texture"),
            size: wgpu::Extent3d {
                width,
                height,
                depth_or_array_layers: 1,
            },
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format: wgpu::TextureFormat::Rgba8Unorm,
            usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
            view_formats: &[],
        });
        queue.write_texture(
            wgpu::TexelCopyTextureInfo {
                texture: &texture,
                mip_level: 0,
                origin: wgpu::Origin3d::ZERO,
                aspect: wgpu::TextureAspect::All,
            },
            rgba_data,
            wgpu::TexelCopyBufferLayout {
                offset: 0,
                bytes_per_row: Some(width * 4),
                rows_per_image: Some(height),
            },
            wgpu::Extent3d {
                width,
                height,
                depth_or_array_layers: 1,
            },
        );

        // Ensure the shared clamp sampler is created.
        if self.content.matcap_sampler.is_none() {
            self.content.matcap_sampler = Some(crate::resources::builders::clamp_linear_sampler(
                device,
                "matcap_sampler",
            ));
        }

        let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
        let index = self.content.matcap_textures.len();
        self.content.matcap_textures.push(texture);
        self.content.matcap_views.push(view);

        // Lazily initialise the fallback matcap view to binding 7 of the
        // first uploaded texture (a plain white 1x1 is fine as fallback).
        if self.content.fallback_matcap_view.is_none() {
            self.content.fallback_matcap_view = Some(
                self.fallback_texture
                    .texture
                    .create_view(&wgpu::TextureViewDescriptor::default()),
            );
        }

        tracing::debug!(matcap_index = index, blendable, "matcap uploaded");
        Ok(crate::resources::MatcapId { index, blendable })
    }

    /// Return the `MatcapId` for a built-in preset.
    ///
    /// Panics if called before the renderer has run at least one prepare pass
    /// (which calls [`Self::ensure_matcaps_initialized`] automatically).
    pub fn builtin_matcap_id(
        &self,
        preset: crate::resources::BuiltinMatcap,
    ) -> crate::resources::MatcapId {
        self.content.builtin_matcap_ids
            .expect("call ensure_matcaps_initialized (or run one prepare frame) before using built-in matcaps")
            [preset as usize]
    }

    /// Upload the eight built-in matcaps to the GPU if not already done.
    ///
    /// Called automatically by `ViewportRenderer::prepare()`. Safe to call
    /// multiple times : no-op after first invocation.
    pub fn ensure_matcaps_initialized(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) {
        if self.content.matcaps_initialized {
            return;
        }
        use crate::resources::material::matcap_data;
        let clay = self
            .upload_matcap(device, queue, &matcap_data::clay(), true)
            .unwrap();
        let wax = self
            .upload_matcap(device, queue, &matcap_data::wax(), true)
            .unwrap();
        let candy = self
            .upload_matcap(device, queue, &matcap_data::candy(), true)
            .unwrap();
        let flat = self
            .upload_matcap(device, queue, &matcap_data::flat(), true)
            .unwrap();
        let ceramic = self
            .upload_matcap(device, queue, &matcap_data::ceramic(), false)
            .unwrap();
        let jade = self
            .upload_matcap(device, queue, &matcap_data::jade(), false)
            .unwrap();
        let mud = self
            .upload_matcap(device, queue, &matcap_data::mud(), false)
            .unwrap();
        let normal = self
            .upload_matcap(device, queue, &matcap_data::normal(), false)
            .unwrap();
        self.content.builtin_matcap_ids =
            Some([clay, wax, candy, flat, ceramic, jade, mud, normal]);
        self.content.matcaps_initialized = true;
    }
}

#[cfg(test)]
mod async_texture_tests {
    use crate::DeviceResources;
    use crate::resources::UploadStatus;

    fn try_make_device() -> Option<(wgpu::Device, wgpu::Queue)> {
        let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor::default());
        let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
            power_preference: wgpu::PowerPreference::LowPower,
            compatible_surface: None,
            force_fallback_adapter: false,
        }))
        .ok()?;
        pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default())).ok()
    }

    /// Device with `TEXTURE_COMPRESSION_BC` enabled, or `None` when the adapter
    /// does not support BC (e.g. a software / mobile adapter) so the caller can
    /// skip the test.
    fn try_make_bc_device() -> Option<(wgpu::Device, wgpu::Queue)> {
        let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor::default());
        let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
            power_preference: wgpu::PowerPreference::LowPower,
            compatible_surface: None,
            force_fallback_adapter: false,
        }))
        .ok()?;
        if !adapter
            .features()
            .contains(wgpu::Features::TEXTURE_COMPRESSION_BC)
        {
            return None;
        }
        pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor {
            required_features: wgpu::Features::TEXTURE_COMPRESSION_BC,
            ..Default::default()
        }))
        .ok()
    }

    fn drive_until_ready(
        resources: &mut DeviceResources,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        id: crate::resources::JobId,
    ) {
        for _ in 0..200 {
            resources.process_uploads(device, queue);
            match resources.upload_status(id) {
                UploadStatus::Ready => return,
                UploadStatus::Failed(e) => panic!("upload failed: {e:?}"),
                UploadStatus::Pending { .. } => {
                    std::thread::sleep(std::time::Duration::from_millis(5));
                }
                UploadStatus::Unknown => panic!("job id disappeared"),
            }
        }
        panic!("texture upload did not complete in time");
    }

    #[test]
    fn invalid_size_errors_synchronously() {
        let Some((device, queue)) = try_make_device() else {
            eprintln!("skipping: no wgpu adapter available");
            return;
        };
        let mut resources = DeviceResources::new(&device, wgpu::TextureFormat::Rgba8UnormSrgb, 1);

        // 2x2 image requires 16 bytes. Pass 12 and confirm the error fires
        // before any job is submitted.
        let rgba = vec![0u8; 12];
        let err = resources
            .begin_upload_texture(&device, &queue, 2, 2, rgba)
            .expect_err("invalid size should error");
        assert!(matches!(
            err,
            crate::error::ViewportError::InvalidTextureData {
                expected: 16,
                actual: 12
            }
        ));
        assert_eq!(resources.uploads_pending(), 0);
    }

    #[test]
    fn begin_upload_texture_completes_and_yields_id() {
        let Some((device, queue)) = try_make_device() else {
            eprintln!("skipping: no wgpu adapter available");
            return;
        };
        let mut resources = DeviceResources::new(&device, wgpu::TextureFormat::Rgba8UnormSrgb, 1);

        let rgba = vec![128u8; 4 * 4 * 4];
        let id = resources
            .begin_upload_texture(&device, &queue, 4, 4, rgba)
            .unwrap();
        assert_eq!(resources.uploads_pending(), 1);

        // Result is not available until the worker finishes.
        let err = resources.upload_result_texture(id).unwrap_err();
        assert!(matches!(err, crate::error::ViewportError::JobNotReady));

        drive_until_ready(&mut resources, &device, &queue, id);

        let tex_id = resources.upload_result_texture(id).expect("ready result");
        // The first uploaded texture lands at index 0.
        assert_eq!(tex_id, crate::resources::TextureId(0));

        // Taking the result again reports missing.
        let err = resources.upload_result_texture(id).unwrap_err();
        assert!(matches!(
            err,
            crate::error::ViewportError::JobResultMissing { .. }
        ));
    }

    #[test]
    fn begin_upload_normal_map_routes_to_same_result_accessor() {
        let Some((device, queue)) = try_make_device() else {
            eprintln!("skipping: no wgpu adapter available");
            return;
        };
        let mut resources = DeviceResources::new(&device, wgpu::TextureFormat::Rgba8UnormSrgb, 1);

        let rgba = vec![64u8; 8 * 8 * 4];
        let id = resources
            .begin_upload_normal_map(&device, &queue, 8, 8, rgba)
            .unwrap();
        drive_until_ready(&mut resources, &device, &queue, id);
        let tex_id = resources.upload_result_texture(id).expect("ready result");
        assert_eq!(tex_id, crate::resources::TextureId(0));
    }

    #[test]
    fn sync_upload_still_works() {
        let Some((device, queue)) = try_make_device() else {
            eprintln!("skipping: no wgpu adapter available");
            return;
        };
        let mut resources = DeviceResources::new(&device, wgpu::TextureFormat::Rgba8UnormSrgb, 1);

        let rgba = vec![200u8; 4 * 4 * 4];
        let tex_id = resources
            .upload_texture(&device, &queue, 4, 4, &rgba)
            .unwrap();
        assert_eq!(tex_id, crate::resources::TextureId(0));
    }

    #[test]
    fn replace_texture_keeps_handle_and_updates_bytes() {
        let Some((device, queue)) = try_make_device() else {
            eprintln!("skipping: no wgpu adapter available");
            return;
        };
        let mut resources = DeviceResources::new(&device, wgpu::TextureFormat::Rgba8UnormSrgb, 1);

        let id = resources
            .upload_texture(&device, &queue, 4, 4, &vec![200u8; 4 * 4 * 4])
            .unwrap();
        let bytes_4x4 = resources.resident_bytes().texture_bytes;

        // Replace in place with a larger image: same handle, larger byte total.
        resources
            .replace_texture(&device, &queue, id, 8, 8, &vec![10u8; 8 * 8 * 4])
            .expect("replace on a live handle succeeds");
        assert!(resources.texture_view(id).is_some(), "handle stays valid");
        let bytes_8x8 = resources.resident_bytes().texture_bytes;
        assert!(
            bytes_8x8 > bytes_4x4,
            "replacing with a larger image must grow resident bytes"
        );

        // Wrong data length is rejected before touching the slot.
        let err = resources
            .replace_texture(&device, &queue, id, 8, 8, &[0u8; 3])
            .unwrap_err();
        assert!(matches!(
            err,
            crate::error::ViewportError::InvalidTextureData { .. }
        ));

        // A stale handle is rejected.
        assert!(resources.free_texture(id));
        let err = resources
            .replace_texture(&device, &queue, id, 4, 4, &vec![0u8; 4 * 4 * 4])
            .unwrap_err();
        assert!(matches!(
            err,
            crate::error::ViewportError::StaleHandle { .. }
        ));
    }

    #[test]
    fn block_layout_matches_format() {
        use super::mip_block_layout;
        use wgpu::TextureFormat as F;

        // Uncompressed RGBA8: 1x1 blocks, 4 bytes/texel.
        assert_eq!(mip_block_layout(F::Rgba8Unorm, 4, 4), (16, 4, 64));

        // BC7: 4x4 blocks, 16 bytes/block.
        assert_eq!(mip_block_layout(F::Bc7RgbaUnormSrgb, 4, 4), (16, 1, 16));
        assert_eq!(mip_block_layout(F::Bc7RgbaUnormSrgb, 8, 8), (32, 2, 64));
        // Non-4-aligned dimensions round up to whole blocks.
        assert_eq!(mip_block_layout(F::Bc7RgbaUnormSrgb, 6, 6), (32, 2, 64));

        // BC4: 4x4 blocks, 8 bytes/block.
        assert_eq!(mip_block_layout(F::Bc4RUnorm, 8, 8), (16, 2, 32));

        // ASTC 8x8: 8x8 blocks, 16 bytes/block.
        let astc = F::Astc {
            block: wgpu::AstcBlock::B8x8,
            channel: wgpu::AstcChannel::Unorm,
        };
        assert_eq!(mip_block_layout(astc, 16, 16), (32, 2, 64));
    }

    #[test]
    fn block_layout_full_mip_pyramid_sum() {
        use super::mip_block_layout;
        // 64x64 BC7 pyramid down to 1x1: 4096+1024+256+64+16+16+16.
        let mut total = 0usize;
        let (w, h) = (64u32, 64u32);
        for level in 0..=6 {
            let lw = (w >> level).max(1);
            let lh = (h >> level).max(1);
            let (_, _, bytes) = mip_block_layout(wgpu::TextureFormat::Bc7RgbaUnormSrgb, lw, lh);
            total += bytes;
        }
        assert_eq!(total, 5488);
    }

    #[test]
    fn supports_texture_format_false_without_feature() {
        let Some((device, _queue)) = try_make_device() else {
            eprintln!("skipping: no wgpu adapter available");
            return;
        };
        // The default device requests no features, so BC is never enabled even
        // when the adapter could support it.
        assert!(!crate::resources::supports_texture_format(
            &device,
            wgpu::TextureFormat::Bc7RgbaUnormSrgb
        ));
        // An uncompressed format needs no feature and is always supported.
        assert!(crate::resources::supports_texture_format(
            &device,
            wgpu::TextureFormat::Rgba8Unorm
        ));
    }

    #[test]
    fn compressed_upload_rejects_unsupported_and_non_block_formats() {
        let Some((device, queue)) = try_make_device() else {
            eprintln!("skipping: no wgpu adapter available");
            return;
        };
        let mut resources = DeviceResources::new(&device, wgpu::TextureFormat::Rgba8UnormSrgb, 1);

        // BC7 without the feature: rejected up front, no job submitted.
        let block = vec![0u8; 16];
        let err = resources
            .begin_upload_compressed_texture(
                &device,
                &queue,
                crate::resources::CompressedTextureDesc {
                    width: 4,
                    height: 4,
                    format: wgpu::TextureFormat::Bc7RgbaUnormSrgb,
                    is_normal_map: false,
                    mip_levels: &[&block],
                },
            )
            .expect_err("BC7 upload without the feature should error");
        assert!(matches!(
            err,
            crate::error::ViewportError::UnsupportedTextureFormat { .. }
        ));

        // A non-compressed format is also rejected by this path.
        let rgba = vec![0u8; 4 * 4 * 4];
        let err = resources
            .begin_upload_compressed_texture(
                &device,
                &queue,
                crate::resources::CompressedTextureDesc {
                    width: 4,
                    height: 4,
                    format: wgpu::TextureFormat::Rgba8Unorm,
                    is_normal_map: false,
                    mip_levels: &[&rgba],
                },
            )
            .expect_err("non-compressed format should error");
        assert!(matches!(
            err,
            crate::error::ViewportError::UnsupportedTextureFormat { .. }
        ));
        assert_eq!(resources.uploads_pending(), 0);
    }

    #[test]
    fn compressed_upload_validates_level_lengths() {
        let Some((device, queue)) = try_make_bc_device() else {
            eprintln!("skipping: no adapter with TEXTURE_COMPRESSION_BC");
            return;
        };
        let mut resources = DeviceResources::new(&device, wgpu::TextureFormat::Rgba8UnormSrgb, 1);

        // 4x4 BC7 needs one 16-byte block; pass 15 and confirm the level check
        // fires before any job is submitted.
        let bad = vec![0u8; 15];
        let err = resources
            .begin_upload_compressed_texture(
                &device,
                &queue,
                crate::resources::CompressedTextureDesc {
                    width: 4,
                    height: 4,
                    format: wgpu::TextureFormat::Bc7RgbaUnormSrgb,
                    is_normal_map: false,
                    mip_levels: &[&bad],
                },
            )
            .expect_err("wrong block length should error");
        assert!(matches!(
            err,
            crate::error::ViewportError::InvalidCompressedTextureData {
                level: 0,
                expected: 16,
                actual: 15,
            }
        ));

        // Empty mip chain is rejected too.
        let err = resources
            .begin_upload_compressed_texture(
                &device,
                &queue,
                crate::resources::CompressedTextureDesc {
                    width: 4,
                    height: 4,
                    format: wgpu::TextureFormat::Bc7RgbaUnormSrgb,
                    is_normal_map: false,
                    mip_levels: &[],
                },
            )
            .expect_err("empty mip chain should error");
        assert!(matches!(
            err,
            crate::error::ViewportError::InvalidCompressedTextureData { actual: 0, .. }
        ));
        assert_eq!(resources.uploads_pending(), 0);
    }

    #[test]
    fn compressed_upload_completes_and_counts_bytes() {
        let Some((device, queue)) = try_make_bc_device() else {
            eprintln!("skipping: no adapter with TEXTURE_COMPRESSION_BC");
            return;
        };
        let mut resources = DeviceResources::new(&device, wgpu::TextureFormat::Rgba8UnormSrgb, 1);

        let before = resources.texture_memory_stats().used_bytes;
        // 4x4 BC7 = one 16-byte block. Contents need not decode to anything in
        // particular; we only exercise the upload path and byte accounting.
        let block = vec![0u8; 16];
        let id = resources
            .begin_upload_compressed_texture(
                &device,
                &queue,
                crate::resources::CompressedTextureDesc {
                    width: 4,
                    height: 4,
                    format: wgpu::TextureFormat::Bc7RgbaUnormSrgb,
                    is_normal_map: false,
                    mip_levels: &[&block],
                },
            )
            .unwrap();
        drive_until_ready(&mut resources, &device, &queue, id);
        let tex_id = resources.upload_result_texture(id).expect("ready result");
        assert_eq!(tex_id, crate::resources::TextureId(0));

        let stats = resources.texture_memory_stats();
        assert_eq!(stats.used_bytes - before, 16);
        assert_eq!(stats.texture_count, 1);
    }

    #[test]
    fn compressed_upload_rejects_non_block_aligned_dimensions() {
        let Some((device, queue)) = try_make_device() else {
            eprintln!("skipping: no wgpu adapter available");
            return;
        };
        let mut resources = DeviceResources::new(&device, wgpu::TextureFormat::Rgba8UnormSrgb, 1);

        // wgpu cannot create a BC texture whose dimensions are not multiples of
        // the 4x4 block. The upload must reject this up front (before any GPU
        // work) so a consumer can fall back to an uncompressed upload rather
        // than binding an invalid texture. 1419 mirrors a real asset dimension.
        for (w, h) in [(6u32, 6u32), (1419, 1024), (1024, 1419)] {
            let blocks_x = w.div_ceil(4);
            let blocks_y = h.div_ceil(4);
            let block = vec![0u8; (blocks_x * blocks_y * 16) as usize];
            let err = resources
                .begin_upload_compressed_texture(
                    &device,
                    &queue,
                    crate::resources::CompressedTextureDesc {
                        width: w,
                        height: h,
                        format: wgpu::TextureFormat::Bc7RgbaUnormSrgb,
                        is_normal_map: false,
                        mip_levels: &[&block],
                    },
                )
                .expect_err("non-block-aligned dimensions must be rejected");
            assert!(matches!(
                err,
                crate::error::ViewportError::CompressedTextureNotBlockAligned { .. }
            ));
        }
        assert_eq!(resources.uploads_pending(), 0);
    }
}

// ---------------------------------------------------------------------------
// GpuTexture: GPU texture with sampler and bind group
// ---------------------------------------------------------------------------

/// A GPU texture with its view, sampler, and bind group for shader binding.
pub struct GpuTexture {
    /// Underlying wgpu texture object.
    pub texture: wgpu::Texture,
    /// Full-texture view used for sampling.
    pub view: wgpu::TextureView,
    /// Sampler bound alongside the view.
    pub sampler: wgpu::Sampler,
    /// Bind group that binds `view` and `sampler` for use in shaders.
    pub bind_group: wgpu::BindGroup,
}