tomoxide 0.14.0

GPU/CPU tomographic reconstruction toolkit (tomopy + tomocupy, in Rust)
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
//! WGSL kernel ports backing the [`Backend`](crate::backend::Backend)
//! capability traits. Only compiled under `gpu-wgpu`.
//!
//! GPU f32 is not bit-exact with the CPU/numpy reference (parallel reduction
//! order and approximate transcendentals), so the parity bar here is a
//! tolerance, not Δ=0 — see the `gpu_tests` in [`crate`].

use crate::backend::{
    make_fbp_filter, Elementwise, FbpFilter, Fft, FilteredBackproject, ForwardProject,
    FourierReconstruct, LpRecReconstruct, RampShape, RankFilter,
};
use crate::data::{Frames, Layout, Tomo, Volume};
use crate::dtype::Complex32;
use crate::error::{Error, Result};
use crate::geometry::{Beam, Geometry};
use crate::params::FilterName;
use bytemuck::{Pod, Zeroable};
use ndarray::{Array3, Axis};

use crate::wgpu::shaders::{
    BACKPROJECT_WGSL, BLUESTEIN_WGSL, ELEMENTWISE_WGSL, FBP_FILTER_WGSL, FFT_SHARED_WGSL,
    FFT_TRANSPOSE_WGSL, FFT_WGSL, FOURIERREC_WGSL, LPREC_WGSL, MEDFILT3D_WGSL, PROJECT_WGSL,
};
use crate::wgpu::WgpuBackend;

/// Max window the GPU median kernel can hold (must match `MAX_WIN` in
/// `medfilt3d.wgsl`): diameter 7, i.e. `size ≤ 7`.
const MEDFILT_MAX_WIN: usize = 343;

/// Largest transform length the shared-memory FFT kernel handles. At `vec2<f32>`
/// (8 bytes) per element this is 32 KiB of workgroup memory, within the common
/// desktop 48 KiB budget; longer transforms fall back to the global multi-pass
/// kernel. The per-adapter `max_compute_workgroup_storage_size` is still checked
/// at dispatch so downlevel adapters (16 KiB) drop to the global path sooner.
const SHARED_FFT_MAX: usize = 4096;

/// Threads per workgroup for the shared-memory FFT of length `n`: one thread per
/// two elements, capped at 256 (a good occupancy point; each thread strides over
/// the remaining butterflies). Always ≥ 1 and a power of two for power-of-two `n`.
fn shared_fft_workgroup(n: usize) -> u32 {
    ((n / 2).clamp(1, 256)) as u32
}

/// Uniform block for the `darkflat` kernel. Padded to 16 bytes to satisfy the
/// WGSL uniform-buffer layout rules.
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct DfParams {
    n_elems: u32,
    plane_size: u32,
    _pad0: u32,
    _pad1: u32,
}

impl Elementwise for WgpuBackend {
    /// `(data − dark) / (flat − dark)`, averaging the flat/dark frame stacks.
    ///
    /// Mirrors [`CpuBackend::darkflat`](../../tomoxide_cpu): the frame averaging
    /// and the divide-by-zero guard run host-side (a single mean per plane,
    /// cheap and order-sensitive), then the per-element broadcast subtraction
    /// and division run on the GPU.
    fn darkflat(&self, data: &mut Tomo<f32>, flat: &Frames<f32>, dark: &Frames<f32>) -> Result<()> {
        let dark2d = dark
            .array
            .mean_axis(Axis(0))
            .ok_or_else(|| Error::InvalidParam("empty dark stack".into()))?;
        let flat2d = flat
            .array
            .mean_axis(Axis(0))
            .ok_or_else(|| Error::InvalidParam("empty flat stack".into()))?;
        let mut denom = &flat2d - &dark2d;
        // Guard against divide-by-zero where flat == dark.
        denom.mapv_inplace(|v| if v.abs() < 1e-6 { 1.0 } else { v });

        // Work in projection layout so element i = proj*plane + (row*cols+col)
        // and the dark/denom plane is indexed by `i % plane`.
        let restore = data.layout == Layout::Sinogram;
        if restore {
            *data = data.to_layout(Layout::Projection);
        }
        let (np, nr, nc) = data.array.dim();
        if dark2d.dim() != (nr, nc) {
            return Err(Error::ShapeMismatch {
                expected: format!("{:?}", (nr, nc)),
                found: format!("{:?}", dark2d.dim()),
            });
        }
        let plane = nr * nc;

        let host: Vec<f32> = data.array.iter().copied().collect();
        let dark_std = dark2d.as_standard_layout();
        let denom_std = denom.as_standard_layout();
        let data_buf = self.storage_rw("df_data", &host);
        let dark_buf = self.storage_ro("df_dark", dark_std.as_slice().expect("standard layout"));
        let denom_buf = self.storage_ro("df_denom", denom_std.as_slice().expect("standard layout"));
        let params = DfParams {
            n_elems: host.len() as u32,
            plane_size: plane as u32,
            _pad0: 0,
            _pad1: 0,
        };
        let param_buf = self.uniform("df_params", &params);
        self.dispatch1d(
            ELEMENTWISE_WGSL,
            "darkflat",
            &[&data_buf, &dark_buf, &denom_buf, &param_buf],
            host.len() as u32,
        );
        let out = self.download_f32(&data_buf, host.len());
        data.array = Array3::from_shape_vec((np, nr, nc), out).expect("len matches dims");

        if restore {
            *data = data.to_layout(Layout::Sinogram);
        }
        Ok(())
    }

    /// In-place `−ln(max(x, 1e-6))` with non-finite results scrubbed to `0`.
    ///
    /// Order-independent, so it runs on whatever layout `data` is in. GPU `log`
    /// differs from libm `ln` by a few ULP — callers compare with a tolerance.
    fn minus_log(&self, data: &mut Tomo<f32>) -> Result<()> {
        let dims = data.array.dim();
        let host: Vec<f32> = data.array.iter().copied().collect();
        let buf = self.storage_rw("ml_data", &host);
        self.dispatch1d(ELEMENTWISE_WGSL, "minus_log", &[&buf], host.len() as u32);
        let out = self.download_f32(&buf, host.len());
        data.array = Array3::from_shape_vec(dims, out).expect("len matches dims");
        Ok(())
    }
}

/// Uniform block for the `backproject` kernel. Padded to 32 bytes (a 16-byte
/// multiple) to satisfy the WGSL uniform-buffer layout rules.
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct BpParams {
    nproj: u32,
    ncols: u32,
    ny: u32,
    nx: u32,
    scale: f32,
    _pad0: u32,
    _pad1: u32,
    _pad2: u32,
}

impl FilteredBackproject for WgpuBackend {
    /// Parallel-beam voxel-driven back-projection on the GPU — the pure adjoint
    /// `Wᵀ` (gain 1; the analytic dispatcher applies the FBP `π/nang` dθ weight
    /// itself).
    ///
    /// Mirrors [`CpuBackend::backproject`](../../tomoxide_cpu): one GPU thread
    /// per output voxel sums the (already filtered) sinogram along all angles by
    /// linear interpolation, unweighted. The per-angle `(cosθ, sinθ)` and the
    /// per-row `center` are computed host-side with the same `sin_cos` as the
    /// CPU path, so the only GPU/CPU divergence is the multiply-accumulate
    /// rounding — callers compare with a tolerance, not Δ=0.
    fn backproject(&self, sino: &Tomo<f32>, geom: &Geometry, out: &mut Volume<f32>) -> Result<()> {
        if geom.beam != Beam::Parallel {
            return Err(Error::InvalidParam(
                "wgpu back-projection currently supports parallel beam only".into(),
            ));
        }
        let s = sino.as_layout(Layout::Sinogram); // [row, angle, col]
        let nz = s.n_rows();
        let nang = s.n_angles();
        let ncols = s.n_cols();
        let (oz, ny, nx) = out.dims();
        if oz != nz {
            return Err(Error::ShapeMismatch {
                expected: format!("{nz} sinogram rows"),
                found: oz.to_string(),
            });
        }
        let angles = &geom.angles.0;
        if angles.len() != nang {
            return Err(Error::ShapeMismatch {
                expected: format!("{nang} angles"),
                found: angles.len().to_string(),
            });
        }

        let sino_std = s.array.as_standard_layout();
        let sino_buf = self.storage_ro("bp_sino", sino_std.as_slice().expect("standard layout"));
        out.array = self.backproject_from_dev(&sino_buf, nz, nang, ncols, geom, (ny, nx), 1.0)?;
        Ok(())
    }
}

impl WgpuBackend {
    /// Device-resident back-projection from an already-uploaded (filtered)
    /// sinogram buffer (`[nz·nang, ncols]`, sinogram C-order), returning the
    /// reconstructed volume as `[nz, ny, nx]`. Shared by
    /// [`FilteredBackproject::backproject`] (which uploads the host sinogram
    /// first and passes `gain = 1.0` — the pure adjoint) and the fused
    /// [`AnalyticReconstruct`] path for fbp/linerec (which passes the filter's
    /// on-device output straight in with `gain = π/nang`, the FBP
    /// angular-quadrature dθ weight). `geom` carries the per-row centre and
    /// angles used for the sampling.
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn backproject_from_dev(
        &self,
        sino_buf: &wgpu::Buffer,
        nz: usize,
        nang: usize,
        ncols: usize,
        geom: &Geometry,
        out_dims: (usize, usize),
        gain: f32,
    ) -> Result<Array3<f32>> {
        let (ny, nx) = out_dims;
        let (cossin, center) = cossin_center(geom, nz);

        let cossin_buf = self.storage_ro("bp_cossin", &cossin);
        let center_buf = self.storage_ro("bp_center", &center);
        let total = nz * ny * nx;
        let vol_buf = self.storage_rw("bp_vol", &vec![0.0f32; total]);
        let params = BpParams {
            nproj: nang as u32,
            ncols: ncols as u32,
            ny: ny as u32,
            nx: nx as u32,
            scale: gain,
            _pad0: 0,
            _pad1: 0,
            _pad2: 0,
        };
        let param_buf = self.uniform("bp_params", &params);
        self.dispatch1d(
            BACKPROJECT_WGSL,
            "backproject",
            &[sino_buf, &cossin_buf, &center_buf, &vol_buf, &param_buf],
            total as u32,
        );
        let result = self.download_f32(&vol_buf, total);
        Ok(Array3::from_shape_vec((nz, ny, nx), result).expect("len matches dims"))
    }
}

/// Per-angle `(cosθ, sinθ)` interleaved plus the per-row `center`, both computed
/// host-side with the same `sin_cos` as the CPU reference so the GPU sampling
/// (and its inclusion-boundary decision) is bit-identical to the CPU path. The
/// center is expanded to one value per row so the `Scalar` and `PerRow` cases
/// share a single buffer path. Shared by the back- and forward-projectors.
fn cossin_center(geom: &Geometry, nz: usize) -> (Vec<f32>, Vec<f32>) {
    let mut cossin = Vec::with_capacity(geom.angles.0.len() * 2);
    for &a in &geom.angles.0 {
        let (sn, c) = a.sin_cos();
        cossin.push(c);
        cossin.push(sn);
    }
    let center = (0..nz).map(|r| geom.center.at(r)).collect();
    (cossin, center)
}

/// Transpose every `d0 × d1` image in a contiguous `[batch][d0][d1]` complex
/// buffer to `[batch][d1][d0]`, in place via a per-image scratch. Used by the
/// non-power-of-two [`fft_2d`](WgpuBackend::fft_2d) fallback to make the column
/// axis contiguous for a 1-D pass and to restore row-major order afterwards.
fn transpose_images(buf: &mut [Complex32], d0: usize, d1: usize, batch: usize) {
    let img = d0 * d1;
    let mut tmp = vec![Complex32::default(); img];
    for b in 0..batch {
        let base = b * img;
        for r in 0..d0 {
            for c in 0..d1 {
                tmp[c * d0 + r] = buf[base + r * d1 + c];
            }
        }
        buf[base..base + img].copy_from_slice(&tmp);
    }
}

impl ForwardProject for WgpuBackend {
    /// Parallel-beam pixel-driven forward projection — the plain line-integral
    /// Radon transform (no gain).
    ///
    /// Mirrors [`CpuBackend::project`](../../tomoxide_cpu) — the exact linear-
    /// interp adjoint of [`Self::backproject`] (both unweighted, so a converged
    /// iterative solve yields the physical μ). Forward projection is a scatter
    /// (each pixel splats onto two detector columns); the GPU maps one thread
    /// per object voxel and resolves the detector-column collisions with an
    /// atomic f32 accumulate (native `atomicAdd` where the device supports it,
    /// else the CAS emulation). The atomic order is nondeterministic, so the
    /// result matches the CPU reference to a tolerance (accumulation-order
    /// rounding), not bit-for-bit. `out` is overwritten with a fresh
    /// `[row, angle, col]` sinogram.
    fn project(&self, vol: &Volume<f32>, geom: &Geometry, out: &mut Tomo<f32>) -> Result<()> {
        if geom.beam != Beam::Parallel {
            return Err(Error::InvalidParam(
                "wgpu forward projection currently supports parallel beam only".into(),
            ));
        }
        let (nz, ny, nx) = vol.dims();
        let nang = geom.angles.0.len();
        let ncols = geom.detector.width;
        if nang == 0 || ncols == 0 {
            return Err(Error::InvalidParam(
                "geometry has no angles or zero detector width".into(),
            ));
        }

        let (cossin, center) = cossin_center(geom, nz);
        let vol_std = vol.array.as_standard_layout();
        let vol_buf = self.storage_ro("fp_vol", vol_std.as_slice().expect("standard layout"));
        let cossin_buf = self.storage_ro("fp_cossin", &cossin);
        let center_buf = self.storage_ro("fp_center", &center);
        let total = nz * nang * ncols;
        let sino_buf = self.storage_rw("fp_sino", &vec![0.0f32; total]);
        let params = FpParams {
            nproj: nang as u32,
            ncols: ncols as u32,
            ny: ny as u32,
            nx: nx as u32,
            _pad0: 0,
            _pad1: 0,
            _pad2: 0,
            _pad3: 0,
        };
        let param_buf = self.uniform("fp_params", &params);
        // One thread per object voxel; voxels atomic-splat onto the sinogram
        // (the exact transpose of the voxel-driven back-projector). sino_buf was
        // zero-initialised above, which the accumulating kernel requires. The
        // `sino` accumulation binding/helper is injected (native f32 atomicAdd
        // or the CAS emulation, per device support).
        let src = format!("{}{PROJECT_WGSL}", self.atomic_f32_decl("sino", 3));
        self.dispatch1d(
            &src,
            "project",
            &[&vol_buf, &cossin_buf, &center_buf, &sino_buf, &param_buf],
            (nz * ny * nx) as u32,
        );
        let result = self.download_f32(&sino_buf, total);
        let array = Array3::from_shape_vec((nz, nang, ncols), result).expect("len matches dims");
        *out = Tomo::new(array, Layout::Sinogram);
        Ok(())
    }
}

/// Uniform block for the `project` kernel. Padded to 32 bytes (a 16-byte
/// multiple) to satisfy the WGSL uniform-buffer layout rules.
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct FpParams {
    nproj: u32,
    ncols: u32,
    ny: u32,
    nx: u32,
    _pad0: u32,
    _pad1: u32,
    _pad2: u32,
    _pad3: u32,
}

/// Uniform block for the `medfilt3d` kernel. Padded to 32 bytes (a 16-byte
/// multiple) to satisfy the WGSL uniform-buffer layout rules.
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct MfParams {
    dz: u32,
    dy: u32,
    dx: u32,
    radius: u32,
    threshold: f32,
    _pad0: u32,
    _pad1: u32,
    _pad2: u32,
}

impl WgpuBackend {
    /// 3-D median/dezinger core shared by [`RankFilter::median3d`] and
    /// [`RankFilter::remove_outlier3d`] — one GPU thread per voxel, bit-exact
    /// with `medfilt3d_core` on the CPU (pure gather + order statistic).
    fn medfilt3d_gpu(
        &self,
        arr: &Array3<f32>,
        radius: usize,
        threshold: f32,
    ) -> Result<Array3<f32>> {
        let diameter = 2 * radius + 1;
        let total = diameter * diameter * diameter;
        if total > MEDFILT_MAX_WIN {
            return Err(Error::InvalidParam(format!(
                "wgpu median window {diameter}³={total} exceeds the GPU cap of \
                 {MEDFILT_MAX_WIN} (size ≤ 7); use the CPU backend for larger windows"
            )));
        }
        let (dz, dy, dx) = arr.dim();
        let host: Vec<f32> = arr.iter().copied().collect();
        let n = host.len();
        let inp = self.storage_ro("mf_inp", &host);
        let outp = self.storage_rw("mf_outp", &vec![0.0f32; n]);
        let params = MfParams {
            dz: dz as u32,
            dy: dy as u32,
            dx: dx as u32,
            radius: radius as u32,
            threshold,
            _pad0: 0,
            _pad1: 0,
            _pad2: 0,
        };
        let param_buf = self.uniform("mf_params", &params);
        self.dispatch1d(
            MEDFILT3D_WGSL,
            "medfilt3d",
            &[&inp, &outp, &param_buf],
            n as u32,
        );
        let out = self.download_f32(&outp, n);
        Ok(Array3::from_shape_vec((dz, dy, dx), out).expect("len matches dims"))
    }
}

impl RankFilter for WgpuBackend {
    /// 3-D median filter (tomopy `median_filter3d`). Bit-exact with the CPU.
    fn median3d(&self, vol: &mut Volume<f32>, size: usize) -> Result<()> {
        let radius = (size.max(3) - 1) / 2;
        vol.array = self.medfilt3d_gpu(&vol.array, radius, 0.0)?;
        Ok(())
    }

    /// Outlier (zinger) removal (tomopy `remove_outlier3d`): the same kernel as
    /// [`Self::median3d`] but replacing a voxel by the local median only where
    /// it deviates from it by at least `diff`. Bit-exact with the CPU.
    fn remove_outlier3d(&self, data: &mut Tomo<f32>, diff: f32, size: usize) -> Result<()> {
        let radius = (size.max(3) - 1) / 2;
        data.array = self.medfilt3d_gpu(&data.array, radius, diff)?;
        Ok(())
    }
}

/// Uniform block for the FBP filter-multiply kernel. Padded to 16 bytes.
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct FfltParams {
    pad: u32,
    ncols: u32,
    pad_side: u32,
    _pad0: u32,
    scale: f32,
    _pad1: f32,
    _pad2: f32,
    _pad3: f32,
}

/// Uniform block for the radix-2 FFT kernel (16 bytes — already a multiple of 16,
/// so no padding is needed).
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct FftParams {
    n: u32,
    logn: u32,
    m: u32,
    sign: f32,
}

/// Uniform block for the Bluestein convolution-multiply kernel. Padded to 16 bytes.
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct BzParams {
    m: u32,
    _pad0: u32,
    _pad1: u32,
    _pad2: u32,
}

/// Uniform block for the FFT transpose kernel. Padded to 16 bytes.
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct TParams {
    rows: u32,
    cols: u32,
    _pad0: u32,
    _pad1: u32,
}

impl WgpuBackend {
    /// In-place radix-2 FFT of `data` as `lanes` contiguous transforms of length
    /// `n`. Does not normalize; the inverse `1/n` is applied by the caller.
    ///
    /// Dispatches the single-pass shared-memory kernel when the transform fits
    /// workgroup memory (the common FBP/gridrec/fourierrec lengths), and falls
    /// back to the multi-submission global kernel for larger `n` — see
    /// [`Self::fft_shared_pass`] and [`Self::fft_global_passes`].
    fn fft_passes(&self, data: &wgpu::Buffer, n: usize, lanes: usize, inverse: bool) {
        let lim = self.device.limits();
        let wg = shared_fft_workgroup(n);
        let fits_shared = n <= SHARED_FFT_MAX
            && (n * std::mem::size_of::<[f32; 2]>()) as u32
                <= lim.max_compute_workgroup_storage_size
            && wg <= lim.max_compute_invocations_per_workgroup;
        if fits_shared {
            self.fft_shared_pass(data, n, lanes, inverse, wg);
        } else {
            self.fft_global_passes(data, n, lanes, inverse);
        }
    }

    /// Single-pass shared-memory FFT: one workgroup per transform, all stages in
    /// workgroup memory. `wg` threads per workgroup (must equal
    /// [`shared_fft_workgroup`]); the caller guarantees `n` fits shared memory.
    /// The butterfly math matches [`Self::fft_global_passes`] bit-for-bit — only
    /// the backing memory differs.
    fn fft_shared_pass(&self, data: &wgpu::Buffer, n: usize, lanes: usize, inverse: bool, wg: u32) {
        let logn = n.trailing_zeros();
        let sign = if inverse { 1.0f32 } else { -1.0f32 };
        let params = self.uniform(
            "fft_sh_p",
            &FftParams {
                n: n as u32,
                logn,
                m: 0,
                sign,
            },
        );
        // Inject the workgroup size, transform length, and log2 as `const`, so the
        // shared array is sized exactly and `@workgroup_size` has one source of
        // truth matching the dispatched workgroup count.
        let src = format!(
            "const WG : u32 = {wg}u;\nconst NN : u32 = {n}u;\nconst LOGN : u32 = {logn}u;\n{FFT_SHARED_WGSL}"
        );
        let pipeline = self.cached_pipeline(&src, "fft_shared");
        let bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("fft_shared"),
            layout: &pipeline.get_bind_group_layout(0),
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: data.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: params.as_entire_binding(),
                },
            ],
        });
        let mut enc = self
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("fft_shared"),
            });
        {
            let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("fft_shared"),
                timestamp_writes: None,
            });
            pass.set_pipeline(&pipeline);
            pass.set_bind_group(0, &bind_group, &[]);
            // One workgroup per transform, folded into a 2-D grid so neither
            // dimension exceeds WebGPU's 65535 cap (the kernel recovers the lane
            // as `wg.y * num_workgroups.x + wg.x`).
            const MAX_DIM: u32 = 65535;
            let lanes = lanes as u32;
            let (wx, wy) = if lanes <= MAX_DIM {
                (lanes, 1)
            } else {
                (MAX_DIM, lanes.div_ceil(MAX_DIM))
            };
            pass.dispatch_workgroups(wx, wy, 1);
        }
        self.queue.submit(Some(enc.finish()));
    }

    /// Multi-submission global-memory radix-2 passes (bit-reversal + `log2(n)`
    /// butterfly stages). Used for transforms too large for shared memory.
    /// Submissions serialize on the queue, so each stage observes the previous
    /// one's writes; transient uniform buffers stay alive through the pending
    /// submissions even after this returns.
    fn fft_global_passes(&self, data: &wgpu::Buffer, n: usize, lanes: usize, inverse: bool) {
        let logn = n.trailing_zeros();
        let sign = if inverse { 1.0f32 } else { -1.0f32 };
        let p0 = self.uniform(
            "fft_p",
            &FftParams {
                n: n as u32,
                logn,
                m: 0,
                sign,
            },
        );
        self.dispatch1d(FFT_WGSL, "bitrev", &[data, &p0], (n * lanes) as u32);
        let mut m = 2u32;
        for _ in 0..logn {
            let p = self.uniform(
                "fft_p",
                &FftParams {
                    n: n as u32,
                    logn,
                    m,
                    sign,
                },
            );
            self.dispatch1d(FFT_WGSL, "butterfly", &[data, &p], (n * lanes / 2) as u32);
            m <<= 1;
        }
    }

    /// Transpose each `rows × cols` image of `src` into `cols × rows` in `dst`.
    fn fft_transpose(
        &self,
        src: &wgpu::Buffer,
        dst: &wgpu::Buffer,
        rows: usize,
        cols: usize,
        batch: usize,
    ) {
        let params = TParams {
            rows: rows as u32,
            cols: cols as u32,
            _pad0: 0,
            _pad1: 0,
        };
        let p = self.uniform("fft_t", &params);
        self.dispatch1d(
            FFT_TRANSPOSE_WGSL,
            "transpose",
            &[src, dst, &p],
            (rows * cols * batch) as u32,
        );
    }

    /// Upload an interleaved complex buffer to a `read_write` storage buffer
    /// (each `Complex32` becomes a `vec2<f32>`).
    fn upload_complex(&self, label: &str, buf: &[Complex32]) -> wgpu::Buffer {
        let host: Vec<f32> = buf.iter().flat_map(|c| [c.re, c.im]).collect();
        self.storage_rw(label, &host)
    }

    /// Read an interleaved complex buffer back into `buf`, scaling by `norm`.
    fn download_complex(&self, data: &wgpu::Buffer, buf: &mut [Complex32], norm: f32) {
        let out = self.download_f32(data, buf.len() * 2);
        for (c, chunk) in buf.iter_mut().zip(out.as_chunks::<2>().0) {
            c.re = chunk[0] * norm;
            c.im = chunk[1] * norm;
        }
    }

    /// Bluestein (chirp-z) batched 1-D DFT for an **arbitrary** length `n`, used
    /// by [`Fft::fft_1d`] when `n` is not a power of two (the radix-2 kernel only
    /// handles power-of-two lengths). `buf` holds `lanes` contiguous transforms
    /// of length `n`; the result overwrites it in place. `inverse` selects the
    /// IDFT and applies the matching `1/n` normalization (like the radix-2 path).
    ///
    /// A length-`n` DFT `X[k] = Σ x[j]·exp(s·2πi·jk/n)` (s = −1 forward, +1
    /// inverse) is rewritten via `jk = (j² + k² − (k−j)²)/2` as
    /// `X[k] = p[k]·Σ (x[j]·p[j])·h[k−j]` with chirps `p[j] = exp(s·πi·j²/n)` and
    /// `h[m] = conj(p[|m|])` — a linear convolution evaluated by a power-of-two
    /// circular convolution of length `m = next_power_of_two(2n−1)` (FFT both,
    /// multiply spectra, inverse FFT). The chirps, the input premultiply, and the
    /// output postmultiply/crop are done host-side so the `j² mod 2n` argument
    /// reduction matches the CPU reference's precision; the three FFTs and the
    /// spectral multiply run on the GPU in one serialized submission chain.
    fn fft_bluestein(&self, buf: &mut [Complex32], n: usize, lanes: usize, inverse: bool) {
        let m = (2 * n - 1).next_power_of_two();
        let s = if inverse { 1.0f32 } else { -1.0f32 };
        let pi = std::f32::consts::PI;

        // Chirp p[j] = exp(s·πi·j²/n); reduce j² mod 2n first so the angle stays
        // small and precise even for large j (πj²/n grows quadratically).
        let two_n = 2 * n as u64;
        let p: Vec<Complex32> = (0..n)
            .map(|j| {
                let r = ((j as u64 * j as u64) % two_n) as f32;
                let ang = s * pi * r / n as f32;
                Complex32::new(ang.cos(), ang.sin())
            })
            .collect();

        // Per-lane premultiplied, zero-padded input a[l·m + j] = x[l·n + j]·p[j].
        let mut a_host = vec![Complex32::new(0.0, 0.0); lanes * m];
        for l in 0..lanes {
            for j in 0..n {
                a_host[l * m + j] = buf[l * n + j] * p[j];
            }
        }

        // Symmetric kernel h on the length-m ring: h[0], h[±j] = conj(p[j]).
        let mut h_host = vec![Complex32::new(0.0, 0.0); m];
        h_host[0] = p[0].conj();
        for j in 1..n {
            let hj = p[j].conj();
            h_host[j] = hj;
            h_host[m - j] = hj;
        }

        // FFT both, multiply spectra (h broadcast across lanes), inverse FFT.
        let a_buf = self.upload_complex("bz_a", &a_host);
        let h_buf = self.upload_complex("bz_h", &h_host);
        self.fft_passes(&h_buf, m, 1, false);
        self.fft_passes(&a_buf, m, lanes, false);
        let p_u = self.uniform(
            "bz_p",
            &BzParams {
                m: m as u32,
                _pad0: 0,
                _pad1: 0,
                _pad2: 0,
            },
        );
        self.dispatch1d(
            BLUESTEIN_WGSL,
            "cmul",
            &[&a_buf, &h_buf, &p_u],
            (lanes * m) as u32,
        );
        self.fft_passes(&a_buf, m, lanes, true);

        // Download the (unnormalized inverse → ×m) convolution, then postmultiply
        // by p[k], crop to n, and apply 1/m (convolution) plus 1/n (IDFT).
        let out = self.download_f32(&a_buf, lanes * m * 2);
        let conv_norm = 1.0 / m as f32;
        let inv_norm = if inverse { 1.0 / n as f32 } else { 1.0 };
        let scale = conv_norm * inv_norm;
        for l in 0..lanes {
            for k in 0..n {
                let off = l * m + k;
                let c = Complex32::new(out[2 * off], out[2 * off + 1]) * p[k];
                buf[l * n + k] = Complex32::new(c.re * scale, c.im * scale);
            }
        }
    }
}

impl Fft for WgpuBackend {
    /// Batched 1-D FFT. Power-of-two `len` runs the direct radix-2 kernel; any
    /// other `len` runs the Bluestein chirp-z transform (also radix-2 under the
    /// hood), so the GPU handles arbitrary lengths like the CPU backend rather
    /// than erroring out. `inverse` divides by `len`, matching the CPU backend.
    fn fft_1d(&self, buf: &mut [Complex32], len: usize, batch: usize, inverse: bool) -> Result<()> {
        if len == 0 || batch == 0 {
            return Ok(());
        }
        if buf.len() != len * batch {
            return Err(Error::ShapeMismatch {
                expected: (len * batch).to_string(),
                found: buf.len().to_string(),
            });
        }
        if len.is_power_of_two() {
            let data = self.upload_complex("fft_1d", buf);
            self.fft_passes(&data, len, batch, inverse);
            let norm = if inverse { 1.0 / len as f32 } else { 1.0 };
            self.download_complex(&data, buf, norm);
        } else {
            self.fft_bluestein(buf, len, batch, inverse);
        }
        Ok(())
    }

    /// Batched 2-D FFT. Power-of-two `rows` and `cols` run the fast on-device
    /// path — a row pass + transpose + row pass + transpose, so both axes run as
    /// contiguous radix-2 transforms. Any other dims fall back to a separable
    /// pair of [`fft_1d`](crate::backend::Fft::fft_1d) passes (each radix-2 or Bluestein per length) with a
    /// host transpose between them, so the GPU handles arbitrary 2-D shapes like
    /// the CPU backend rather than erroring. `inverse` divides by `rows·cols`.
    fn fft_2d(
        &self,
        buf: &mut [Complex32],
        rows: usize,
        cols: usize,
        batch: usize,
        inverse: bool,
    ) -> Result<()> {
        if rows == 0 || cols == 0 || batch == 0 {
            return Ok(());
        }
        if buf.len() != rows * cols * batch {
            return Err(Error::ShapeMismatch {
                expected: (rows * cols * batch).to_string(),
                found: buf.len().to_string(),
            });
        }
        if !rows.is_power_of_two() || !cols.is_power_of_two() {
            // Separable fallback: FFT along the contiguous `cols` axis, transpose
            // each image so `rows` become contiguous, FFT along `rows`, transpose
            // back. Each 1-D pass selects radix-2 or Bluestein per length, and the
            // per-axis inverse divisors (1/cols then 1/rows) compose to the 2-D
            // 1/(rows·cols) — so the result matches the on-device pow2 path and
            // the CPU backend exactly.
            self.fft_1d(buf, cols, rows * batch, inverse)?;
            transpose_images(buf, rows, cols, batch);
            self.fft_1d(buf, rows, cols * batch, inverse)?;
            transpose_images(buf, cols, rows, batch);
            return Ok(());
        }
        let data = self.upload_complex("fft_2d", buf);
        // Row pass: `batch·rows` contiguous transforms of length `cols`.
        self.fft_passes(&data, cols, rows * batch, inverse);
        // Transpose to make columns contiguous, transform length `rows`, back.
        let scratch = self.storage_rw("fft_2d_t", &vec![0.0f32; rows * cols * batch * 2]);
        self.fft_transpose(&data, &scratch, rows, cols, batch);
        self.fft_passes(&scratch, rows, cols * batch, inverse);
        self.fft_transpose(&scratch, &data, cols, rows, batch);
        let norm = if inverse {
            1.0 / (rows * cols) as f32
        } else {
            1.0
        };
        self.download_complex(&data, buf, norm);
        Ok(())
    }
}

impl FbpFilter for WgpuBackend {
    /// Build the FBP apodized ramp filter on the host via the shared
    /// [`make_fbp_filter`] with [`RampShape::Linear`] — wgpu is a portable
    /// fallback that mirrors the CPU (tomopy) ramp, not the CUDA (tomocupy)
    /// `_wint` ramp.
    fn make_filter(&self, name: FilterName, n: usize) -> Result<Vec<f32>> {
        make_fbp_filter(name, n, RampShape::Linear)
    }

    /// Apply `filter` to every projection of `sino` on the GPU. Each detector
    /// lane (axis 2 in both layouts) is centred in a `pad = filter.len()`-wide
    /// buffer and edge-replicate-padded on both borders, forward-transformed,
    /// multiplied by the real filter, inverse-transformed, scaled by `1/pad`,
    /// and the centred `n_cols`-wide window cropped back out.
    /// Forward FFT, frequency-domain multiply, and inverse FFT all run on the
    /// GPU in one serialized submission chain — no host round-trip between the
    /// transforms. Mirrors `CpuBackend::apply`, including the per-row
    /// rotation-centre phase (tomocupy `fbp_filter_center`): the per-lane shift
    /// `ncols/2 − center` is uploaded and folded into the GPU filter-multiply,
    /// so after this pass the back-projectors assume centre = `ncols/2`. At the
    /// default centre the shift is zero and only the ramp applies. Requires a
    /// power-of-two `pad` (the GPU FFT is radix-2 only); other lengths error so
    /// the caller can fall back to CPU.
    fn apply(&self, sino: &mut Tomo<f32>, filter: &[f32], geom: &Geometry) -> Result<()> {
        let ncols = sino.n_cols();
        let batch = sino.array.len() / ncols;
        // Filter on-device, then download the compact real result and scatter it
        // back into `sino` (lane order matches `filter_to_device`'s C-order upload).
        let out = self.filter_to_device(sino, filter, geom)?;
        let host_out = self.download_f32(&out, batch * ncols);
        for (l, mut lane) in sino.array.lanes_mut(Axis(2)).into_iter().enumerate() {
            let base = l * ncols;
            for (i, slot) in lane.iter_mut().enumerate() {
                *slot = host_out[base + i];
            }
        }
        Ok(())
    }
}

impl WgpuBackend {
    /// Device-resident FBP filter: upload only the raw REAL sinogram
    /// (`batch·ncols` f32), then pack / pad / FFT / ×filter / IFFT / crop entirely
    /// on-GPU, returning the filtered real sinogram **as a device buffer**
    /// (`[batch·ncols]`, sinogram C-order — lane `l` at `[l·ncols, (l+1)·ncols)`).
    ///
    /// This is the shared core of [`FbpFilter::apply`] (which downloads + scatters
    /// the result to host) and the fused [`AnalyticReconstruct`] path (which feeds
    /// the buffer straight into the gridding / back-projection kernels, skipping
    /// the filtered-sinogram download + re-upload round-trip). The old host path
    /// built the full complex padded batch on the host and round-tripped 2× the
    /// data; only the compact real sinogram crosses the bus now.
    pub(crate) fn filter_to_device(
        &self,
        sino: &Tomo<f32>,
        filter: &[f32],
        geom: &Geometry,
    ) -> Result<wgpu::Buffer> {
        let pad = filter.len();
        if pad == 0 {
            return Err(Error::InvalidParam("empty filter".into()));
        }
        let ncols = sino.n_cols();
        if pad < ncols {
            return Err(Error::ShapeMismatch {
                expected: format!(">= {ncols} (n_cols)"),
                found: pad.to_string(),
            });
        }
        if !pad.is_power_of_two() {
            return Err(Error::InvalidParam(format!(
                "wgpu FBP filter requires a power-of-two length (got {pad}); use the CPU backend"
            )));
        }
        let batch = sino.array.len() / ncols;
        let pad_side = pad / 2 - ncols / 2;
        // Per-lane centre shift δ = ncols/2 − center(row). Lane `l` (C-order over
        // the two non-detector axes) maps to slice row `l/d1` (Sinogram) or
        // `l%d1` (Projection), matching CpuBackend::apply. δ=0 at the default
        // centre keeps the GPU goldens identical.
        let half = ncols as f32 / 2.0;
        let d1 = sino.array.shape()[1];
        let deltas: Vec<f32> = (0..batch)
            .map(|l| {
                let row = match sino.layout {
                    Layout::Sinogram => l / d1,
                    Layout::Projection => l % d1,
                };
                half - geom.center.at(row)
            })
            .collect();
        // Upload the sinogram in C order; lane `l` then occupies
        // `[l·ncols, (l+1)·ncols)`, matching `pack`/`unpack` and the `lanes`
        // enumeration order used for `deltas`.
        let std = sino.array.as_standard_layout();
        let sino_host = std
            .as_slice()
            .expect("as_standard_layout yields a contiguous slice");
        let sino_buf = self.storage_ro("fbp_sino", sino_host);
        let data = self.storage_empty("fbp_spectrum", batch * pad * 2);
        let w = self.storage_ro("fbp_w", filter);
        let deltas_buf = self.storage_ro("fbp_deltas", &deltas);
        let p = self.uniform(
            "fbp_p",
            &FfltParams {
                pad: pad as u32,
                ncols: ncols as u32,
                pad_side: pad_side as u32,
                _pad0: 0,
                scale: 1.0 / pad as f32,
                _pad1: 0.0,
                _pad2: 0.0,
                _pad3: 0.0,
            },
        );
        self.dispatch1d(
            FBP_FILTER_WGSL,
            "pack",
            &[&sino_buf, &data, &p],
            (batch * pad) as u32,
        );
        self.fft_passes(&data, pad, batch, false);
        self.dispatch1d(
            FBP_FILTER_WGSL,
            "apply_filter",
            &[&data, &w, &p, &deltas_buf],
            (batch * pad) as u32,
        );
        self.fft_passes(&data, pad, batch, true);
        // `unpack` crops the real central window and folds the 1/pad inverse-FFT
        // normalisation (fft_passes leaves the inverse unscaled) into `scale`.
        let out = self.storage_empty("fbp_out", batch * ncols);
        self.dispatch1d(
            FBP_FILTER_WGSL,
            "unpack",
            &[&data, &out, &p],
            (batch * ncols) as u32,
        );
        Ok(out)
    }
}

/// Uniform block for the device-resident fourierrec kernels. 64 bytes (all
/// scalars, so 16-byte-aligned as WGSL uniforms require). Mirrors `FrParams` in
/// `fourierrec.wgsl`.
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct FrParams {
    nz: u32,
    nang: u32,
    nd: u32,
    n: u32,
    ng: u32,
    nf: u32,
    crop: u32,
    _p0: u32,
    mu: f32,
    coeff0: f32,
    coeff1: f32,
    gscale: f32,
    phi_sign: f32,
    norm: f32,
    _p1: f32,
    _p2: f32,
}

impl FourierReconstruct for WgpuBackend {
    /// Device-resident Gaussian-USFFT (tomocupy `cfunc_fourierrec`) — the whole
    /// gather / wrap / FFT / deapodize chain runs on the GPU, mirroring
    /// `crate::recon::fourierrec` step-for-step. Only the filtered sinogram is
    /// uploaded and the volume downloaded, replacing the host-gridding + per-call
    /// FFT-round-trip path that dominated wgpu fourierrec wall time.
    fn reconstruct(&self, filtered: &Tomo<f32>, geom: &Geometry, n: usize) -> Result<Volume<f32>> {
        let b = filtered.as_layout(Layout::Sinogram);
        let nz = b.n_rows();
        let nang = b.n_angles();
        let nd = b.n_cols();

        // The device path drives the radix-2 `fft_passes` directly, so both the
        // length-`nd` 1-D FFT and the `2*nd` 2-D FFT must be power-of-two. For a
        // non-power-of-two detector width, delegate to the generic host gridding
        // (which selects Bluestein per length through the `Fft` capability).
        if !nd.is_power_of_two() {
            return Ok(Volume::new(crate::recon::fourierrec::fourierrec(
                filtered, geom, n, self,
            )?));
        }

        let bdata = b
            .array
            .as_slice()
            .expect("contiguous sinogram (as_layout yields a standard-layout copy)");
        // Upload the filtered sinogram, then run the device-resident core.
        let sino_buf = self.storage_ro("fr_sino", bdata);
        self.fourierrec_from_dev(&sino_buf, nz, nang, nd, geom, n)
    }
}

impl WgpuBackend {
    /// Device-resident Fourier-grid (regridding) reconstruction from an
    /// already-uploaded filtered sinogram buffer (`[nz·nang, nd]`, sinogram
    /// C-order). Shared by [`Fourierrec::reconstruct`] (which uploads the host
    /// sinogram first) and the fused [`AnalyticReconstruct`] path (which passes
    /// the filter's on-device output straight in, skipping the round-trip). All
    /// Gaussian-kernel arithmetic mirrors the CPU port (recon/fourierrec.rs) so
    /// the grids coincide. Caller guarantees `nd.is_power_of_two()`.
    pub(crate) fn fourierrec_from_dev(
        &self,
        sino_buf: &wgpu::Buffer,
        nz: usize,
        nang: usize,
        nd: usize,
        geom: &Geometry,
        n: usize,
    ) -> Result<Volume<f32>> {
        // Gaussian-kernel parameters — identical arithmetic to the CPU port so
        // the grids coincide (recon/fourierrec.rs).
        const EPS: f64 = 1e-3;
        let ndf = nd as f64;
        let neg_log_eps = -EPS.ln();
        let mu = neg_log_eps / (2.0 * ndf * ndf);
        let inside = mu * neg_log_eps + (mu * ndf) * (mu * ndf) / 4.0;
        let m = (2.0 * ndf / std::f64::consts::PI * inside.sqrt()).ceil() as usize;

        let ng = 2 * nd + 2 * m;
        let nf = 2 * nd;
        let crop = (nd - n.min(nd)) / 2;
        let mu32 = mu as f32;
        let ndf32 = nd as f32;
        let params = FrParams {
            nz: nz as u32,
            nang: nang as u32,
            nd: nd as u32,
            n: n as u32,
            ng: ng as u32,
            nf: nf as u32,
            crop: crop as u32,
            _p0: 0,
            mu: mu32,
            coeff0: std::f32::consts::PI / (mu32 * 4.0 * ndf32 * ndf32),
            coeff1: -std::f32::consts::PI * std::f32::consts::PI / mu32,
            gscale: 4.0 / ndf32,
            phi_sign: 1.0 - (nd % 4) as f32,
            // Unified amplitude: 1/nf² normalizes the unnormalized device
            // inverse FFT, ×π·nd² lifts to the fbp/tomopy scale (see
            // recon/fourierrec.rs `phi_amp`); nf = 2·nd folds the two to π/4.
            norm: std::f32::consts::FRAC_PI_4,
            _p1: 0.0,
            _p2: 0.0,
        };

        // (cos θ, sin θ) per angle — the +sin gather convention of the CPU port.
        let trig: Vec<f32> = geom
            .angles
            .0
            .iter()
            .flat_map(|&a| {
                let (s, c) = a.sin_cos();
                [c, s]
            })
            .collect();

        // Inject the Gaussian half-width `m` as a `const` so `array<f32, 2m+1>`
        // is sized exactly (dispatch1d injects `WG` on top of this).
        // Inject the atomic-accumulate bindings/helpers for the gather (ga_grid)
        // and wrap (wr_grid) kernels — native f32 atomicAdd or CAS emulation per
        // device support — alongside the Gaussian half-width constant.
        let src = format!(
            "const M : u32 = {m}u;\n{}{}{FOURIERREC_WGSL}",
            self.atomic_f32_decl("ga_grid", 1),
            self.atomic_f32_decl("wr_grid", 0),
        );

        let trig_buf = self.storage_ro("fr_trig", &trig);
        let radial = self.storage_empty("fr_radial", nz * nang * nd * 2);
        let grid = self.storage_empty("fr_grid", nz * ng * ng * 2);
        let inner = self.storage_empty("fr_inner", nz * nf * nf * 2);
        let scratch = self.storage_empty("fr_scratch", nz * nf * nf * 2);
        let out = self.storage_empty("fr_out", nz * n * n);
        let p = self.uniform("fr_p", &params);

        // Zero the accumulation grid before the atomic gather/wrap.
        self.zero_buffer(&grid);

        let radial_threads = (nz * nang * nd) as u32;
        // 1. complex radial buffer with pre-FFT shift modulation.
        self.dispatch1d(
            &src,
            "build_radial",
            &[sino_buf, &radial, &p],
            radial_threads,
        );
        // 2. centred 1-D FFT of every projection (length nd), then post-modulate.
        self.fft_passes(&radial, nd, nz * nang, false);
        self.dispatch1d(&src, "postmod", &[&radial, &p], radial_threads);
        // 3. Gaussian gather onto the oversampled grid; fold borders.
        self.dispatch1d(
            &src,
            "gather",
            &[&radial, &grid, &trig_buf, &p],
            radial_threads,
        );
        self.dispatch1d(&src, "wrap", &[&grid, &p], (nz * ng * ng) as u32);
        // 4. extract the 2*nd interior with the pre-inverse-FFT shift.
        self.dispatch1d(&src, "extract", &[&grid, &inner, &p], (nz * nf * nf) as u32);
        // 5. centred inverse 2-D FFT (row pass, transpose, row pass, transpose);
        //    the 1/nf^2 normalisation is folded into deapodize.
        self.fft_passes(&inner, nf, nf * nz, true);
        self.fft_transpose(&inner, &scratch, nf, nf, nz);
        self.fft_passes(&scratch, nf, nf * nz, true);
        self.fft_transpose(&scratch, &inner, nf, nf, nz);
        self.dispatch1d(&src, "fftshift", &[&inner, &p], (nz * nf * nf) as u32);
        // 6. deapodize, central crop, unit-disk mask → real output.
        self.dispatch1d(&src, "deapodize", &[&inner, &out, &p], (nz * n * n) as u32);

        let host = self.download_f32(&out, nz * n * n);
        let arr = Array3::from_shape_vec((nz, n, n), host)
            .expect("out buffer length nz*n*n matches the volume shape");
        Ok(Volume::new(arr))
    }
}

/// Uniform block for the device-resident lprec kernels. 48 bytes (16-aligned).
/// Mirrors `LpParams` in `lprec.wgsl`.
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct LpParams {
    nz: u32,
    nang: u32,
    n: u32,
    ntheta: u32,
    nrho: u32,
    npts: u32,
    _p0: u32,
    _p1: u32,
    scale: f32,
    _p2: f32,
    _p3: f32,
    _p4: f32,
}

/// Per-span (log-polar overlap set) gather/scatter coordinate buffers, uploaded
/// once. `wrap` holds the wrapping-set coordinates when `grids.wids` is nonempty.
struct LprecSpan {
    xs_main: wgpu::Buffer,
    ys_main: wgpu::Buffer,
    wrap: Option<(wgpu::Buffer, wgpu::Buffer)>,
    xs_c: wgpu::Buffer,
    ys_c: wgpu::Buffer,
}

/// Precomputed, device-resident log-polar grids for lprec — the host
/// `build_grids` output uploaded to the GPU. Angle-count / grid-size dependent
/// but **nz-independent**, so [`WgpuFbpStream`](super::streaming::WgpuFbpStream)
/// builds this once via [`WgpuBackend::build_lprec_grids`] and reuses it across
/// every z-chunk. The whole-volume [`LpRecReconstruct::reconstruct`] builds one
/// per call (as before), so its behaviour is unchanged.
pub(crate) struct WgpuLprecGrids {
    ntheta: usize,
    nrho: usize,
    n: usize,
    nang: usize,
    scale: f32,
    lpids_len: usize,
    wids_len: usize,
    cids_len: usize,
    kfull_buf: wgpu::Buffer,
    lpids_buf: wgpu::Buffer,
    wids_buf: wgpu::Buffer,
    cids_buf: wgpu::Buffer,
    spans: Vec<LprecSpan>,
}

impl WgpuBackend {
    /// Geometry guards + host `build_grids` + upload of every nz-independent
    /// lprec buffer (the kfull kernel, the gather/scatter index sets, and the
    /// per-span coordinate arrays). Split out of [`LpRecReconstruct::reconstruct`]
    /// so the streaming path can build the grids once and reuse them per chunk
    /// (a naive per-chunk `reconstruct` would rerun this ~175-230 ms precompute
    /// every chunk — a regression vs the whole-volume path).
    pub(crate) fn build_lprec_grids(&self, geom: &Geometry, n: usize) -> Result<WgpuLprecGrids> {
        // Same geometry guards as the CPU port (recon/lprec.rs): square geometry
        // with equally spaced angles spanning [0, π). The detector-width == n
        // check is deferred to `lprec_run` (it needs the sinogram's ncols).
        let nang = geom.angles.0.len();
        if nang < 2 {
            return Err(Error::InvalidParam(format!(
                "lprec requires square geometry with detector width == grid size (got n={n})"
            )));
        }
        let angles = &geom.angles.0;
        let dth = (angles[1] - angles[0]).abs();
        let nproj_test = (std::f32::consts::PI / dth).round() as usize;
        if nproj_test != nang {
            return Err(Error::InvalidParam(
                "lprec requires equally spaced angles spanning [0, π)".into(),
            ));
        }

        // Precompute the log-polar grids on the host, reusing the CPU port's
        // builder (its precompute FFTs run on the CpuBackend's Fft).
        let grids = crate::recon::lprec::build_grids(n, nang, &crate::cpu::CpuBackend::new())?;
        let ntheta = grids.ntheta;
        let nrho = grids.nrho;
        let scale = 2.0 / (nrho as f32 * ntheta as f32);

        let kfull_host: Vec<f32> = grids.kfull.iter().flat_map(|c| [c.re, c.im]).collect();
        let kfull_buf = self.storage_ro("lp_kfull", &kfull_host);
        let lpids: Vec<u32> = grids.lpids.iter().map(|&x| x as u32).collect();
        let wids: Vec<u32> = grids.wids.iter().map(|&x| x as u32).collect();
        let cids: Vec<u32> = grids.cids.iter().map(|&x| x as u32).collect();
        let lpids_buf = self.storage_ro_u32("lp_lpids", &lpids);
        let wids_buf = self.storage_ro_u32("lp_wids", &wids);
        let cids_buf = self.storage_ro_u32("lp_cids", &cids);

        let mut spans = Vec::with_capacity(grids.lp2p1.len());
        for k in 0..grids.lp2p1.len() {
            let xs_main = self.storage_ro("lp_xs", &grids.lp2p2[k]);
            let ys_main = self.storage_ro("lp_ys", &grids.lp2p1[k]);
            let wrap = if !wids.is_empty() {
                Some((
                    self.storage_ro("lp_xsw", &grids.lp2p2w[k]),
                    self.storage_ro("lp_ysw", &grids.lp2p1w[k]),
                ))
            } else {
                None
            };
            let xs_c = self.storage_ro("lp_xsc", &grids.c2lp1[k]);
            let ys_c = self.storage_ro("lp_ysc", &grids.c2lp2[k]);
            spans.push(LprecSpan {
                xs_main,
                ys_main,
                wrap,
                xs_c,
                ys_c,
            });
        }

        Ok(WgpuLprecGrids {
            ntheta,
            nrho,
            n,
            nang,
            scale,
            lpids_len: lpids.len(),
            wids_len: wids.len(),
            cids_len: cids.len(),
            kfull_buf,
            lpids_buf,
            wids_buf,
            cids_buf,
            spans,
        })
    }

    /// Per-chunk lprec core: the cubic-B-spline prefilter + gather / 2-D FFT
    /// convolution / scatter over `nz` slices, reusing the precomputed
    /// [`WgpuLprecGrids`]. `filtered` is the FBP-filtered sinogram for this chunk;
    /// only the nz-sized scratch/output buffers are allocated here.
    pub(crate) fn lprec_run(
        &self,
        filtered: &Tomo<f32>,
        grids: &WgpuLprecGrids,
    ) -> Result<Volume<f32>> {
        let b = filtered.as_layout(Layout::Sinogram);
        let nz = b.n_rows();
        let nang = b.n_angles();
        let nd = b.n_cols();
        let n = grids.n;
        if nd != n || nang != grids.nang {
            return Err(Error::InvalidParam(format!(
                "lprec chunk geometry {nang}×{nd} does not match prepared grids {}×{n}",
                grids.nang
            )));
        }
        let ntheta = grids.ntheta;
        let nrho = grids.nrho;
        let scale = grids.scale;

        let bdata = b
            .array
            .as_slice()
            .expect("contiguous sinogram (as_layout yields a standard-layout copy)");

        let g = self.storage_rw("lp_g", bdata);
        let fl = self.storage_empty("lp_fl", nz * nrho * ntheta);
        let flc = self.storage_empty("lp_flc", nz * nrho * ntheta * 2);
        let scratch = self.storage_empty("lp_scratch", nz * nrho * ntheta * 2);
        let flcre = self.storage_empty("lp_flcre", nz * nrho * ntheta);
        let f = self.storage_empty("lp_f", nz * n * n);

        let mk = |npts: usize| {
            self.uniform(
                "lp_p",
                &LpParams {
                    nz: nz as u32,
                    nang: nang as u32,
                    n: n as u32,
                    ntheta: ntheta as u32,
                    nrho: nrho as u32,
                    npts: npts as u32,
                    _p0: 0,
                    _p1: 0,
                    scale,
                    _p2: 0.0,
                    _p3: 0.0,
                    _p4: 0.0,
                },
            )
        };
        // npts=0 uniform shared by the whole-grid kernels (prefilter, r2c, cmul,
        // take_re); they read only nz/nang/n/ntheta/nrho/scale.
        let p0 = mk(0);
        // Inject the gather accumulation binding/helper (native f32 atomicAdd or
        // CAS emulation per device support); one source string shared by every
        // entry of the module so the pipeline cache reuses compiled kernels.
        let src = format!("{}{LPREC_WGSL}", self.atomic_f32_decl("ga_fl", 1));

        // Cubic-B-spline prefilter: detector axis then angle axis.
        self.dispatch1d(&src, "prefilter_rows", &[&g, &p0], (nz * nang) as u32);
        self.dispatch1d(&src, "prefilter_cols", &[&g, &p0], (nz * n) as u32);

        self.zero_buffer(&f);
        for span in &grids.spans {
            self.zero_buffer(&fl);

            // 1. gather polar → log-polar (main set, then wrapping set).
            let p_main = mk(grids.lpids_len);
            self.dispatch1d(
                &src,
                "gather",
                &[
                    &g,
                    &fl,
                    &grids.lpids_buf,
                    &span.xs_main,
                    &span.ys_main,
                    &p_main,
                ],
                (nz * grids.lpids_len) as u32,
            );
            if let Some((xs_w, ys_w)) = &span.wrap {
                let p_w = mk(grids.wids_len);
                self.dispatch1d(
                    &src,
                    "gather",
                    &[&g, &fl, &grids.wids_buf, xs_w, ys_w, &p_w],
                    (nz * grids.wids_len) as u32,
                );
            }

            // 2. 2-D FFT convolution: fl → complex, forward, ×kfull, inverse,
            //    take real × scale.
            let grid_threads = (nz * nrho * ntheta) as u32;
            self.dispatch1d(&src, "real_to_complex", &[&fl, &flc, &p0], grid_threads);
            self.fft_passes(&flc, ntheta, nrho * nz, false);
            self.fft_transpose(&flc, &scratch, nrho, ntheta, nz);
            self.fft_passes(&scratch, nrho, ntheta * nz, false);
            self.fft_transpose(&scratch, &flc, ntheta, nrho, nz);
            self.dispatch1d(&src, "cmul", &[&flc, &grids.kfull_buf, &p0], grid_threads);
            self.fft_passes(&flc, ntheta, nrho * nz, true);
            self.fft_transpose(&flc, &scratch, nrho, ntheta, nz);
            self.fft_passes(&scratch, nrho, ntheta * nz, true);
            self.fft_transpose(&scratch, &flc, ntheta, nrho, nz);
            self.dispatch1d(&src, "take_re", &[&flc, &flcre, &p0], grid_threads);

            // 3. scatter log-polar → Cartesian disk (accumulates across spans).
            let p_c = mk(grids.cids_len);
            self.dispatch1d(
                &src,
                "scatter",
                &[&flcre, &f, &grids.cids_buf, &span.xs_c, &span.ys_c, &p_c],
                (nz * grids.cids_len) as u32,
            );
        }

        let host = self.download_f32(&f, nz * n * n);
        let arr = Array3::from_shape_vec((nz, n, n), host)
            .expect("out buffer length nz*n*n matches the volume shape");
        Ok(Volume::new(arr))
    }
}

impl LpRecReconstruct for WgpuBackend {
    /// Device-resident log-polar reconstruction (tomocupy `lprec`) — the cubic
    /// B-spline prefilter, the polar↔log-polar↔Cartesian gather/scatter, and the
    /// 2-D FFT convolution all run on the GPU, mirroring `crate::recon::lprec`
    /// step-for-step. Composes the once-per-call grid precompute
    /// (`WgpuBackend::build_lprec_grids`) with the per-chunk core
    /// (`WgpuBackend::lprec_run`); the streaming path reuses the same two pieces
    /// but caches the grids across chunks.
    fn reconstruct(&self, filtered: &Tomo<f32>, geom: &Geometry, n: usize) -> Result<Volume<f32>> {
        let grids = self.build_lprec_grids(geom, n)?;
        self.lprec_run(filtered, &grids)
    }
}

impl crate::backend::AnalyticReconstruct for WgpuBackend {
    /// Fused fbp/linerec/fourierrec: FBP-filter the sinogram on-device and feed
    /// the filtered buffer straight into the back-projection / Fourier-gridding
    /// kernels, keeping it device-resident. The composed capability path
    /// (recon::recon's fallback) downloads the filtered sinogram after
    /// `FbpFilter::apply` and re-uploads it inside the recon; this fuses the two
    /// so the sinogram crosses the bus once (up) and only the volume comes back.
    fn reconstruct(
        &self,
        sino: &Tomo<f32>,
        geom: &Geometry,
        algorithm: crate::params::Algorithm,
        params: &crate::params::ReconParams,
    ) -> Result<Volume<f32>> {
        use crate::params::Algorithm;
        let ncols = sino.n_cols();
        let n = params.num_gridx.unwrap_or(ncols);
        let nz = sino.n_rows();
        let nang = sino.n_angles();
        let kernel = self.make_filter(params.filter_name, ncols)?;

        // fourierrec's device core drives the radix-2 FFT directly, so it needs a
        // power-of-two detector width. For other widths, compose the host path
        // (filter on-device to host, then the generic Bluestein-capable gridding)
        // exactly as recon::recon's fallback does — the fused path is a pure
        // optimisation and must not change which inputs are accepted.
        if algorithm == Algorithm::Fourierrec && !ncols.is_power_of_two() {
            let mut filtered = sino.clone();
            <Self as FbpFilter>::apply(self, &mut filtered, &kernel, geom)?;
            return Ok(Volume::new(crate::recon::fourierrec::fourierrec(
                &filtered, geom, n, self,
            )?));
        }

        // Filter in sinogram layout so the on-device buffer is [nz·nang, ncols] in
        // the C-order the recon cores expect; the per-row centre shift is folded
        // in by the filter (after it, the back-projector assumes centre = ncols/2).
        let s = sino.as_layout(Layout::Sinogram);
        let filt_dev = self.filter_to_device(&s, &kernel, geom)?;

        match algorithm {
            // fourierrec grids against the original angles; the centre shift is
            // already baked into the filtered data.
            Algorithm::Fourierrec => self.fourierrec_from_dev(&filt_dev, nz, nang, ncols, geom, n),
            // fbp / linerec back-project against a centre = ncols/2 geometry, the
            // axis the filter shifted the projections onto.
            Algorithm::Fbp | Algorithm::Linerec => {
                let centered = Geometry {
                    center: crate::geometry::Center::Scalar(ncols as f32 / 2.0),
                    ..geom.clone()
                };
                let arr = self.backproject_from_dev(
                    &filt_dev,
                    nz,
                    nang,
                    ncols,
                    &centered,
                    (n, n),
                    std::f32::consts::PI / nang as f32, // FBP angular quadrature dθ
                )?;
                Ok(Volume::new(arr))
            }
            // recon::recon only routes these three algorithms here.
            other => Err(Error::InvalidParam(format!(
                "wgpu analytic_reconstruct does not handle {other:?}"
            ))),
        }
    }

    /// Build a handle-reusing streaming reconstructor for the analytic methods
    /// this backend fuses (fbp / linerec / fourierrec), so the CLI's overlapped
    /// streaming pipeline drives wgpu instead of falling back to whole-volume.
    /// Covers fbp / linerec / fourierrec (fused `AnalyticReconstruct` path) and
    /// lprec (its own dispatch; the stream caches the log-polar grids across
    /// chunks). Returns `None` for every other algorithm so the caller uses
    /// per-chunk `recon`. `ncols`/`max_nz` are not needed: the reconstructor
    /// recomputes its (cheap) filter/geometry per chunk, so it handles any
    /// `nz ≤ max_nz`; the lprec grids are (re)built lazily on the first chunk.
    fn streaming(
        &self,
        algorithm: crate::params::Algorithm,
        params: &crate::params::ReconParams,
        _geom: &Geometry,
        _ncols: usize,
        _max_nz: usize,
    ) -> Result<Option<Box<dyn crate::backend::StreamingAnalytic>>> {
        use crate::params::Algorithm;
        if !matches!(
            algorithm,
            Algorithm::Fbp | Algorithm::Linerec | Algorithm::Fourierrec | Algorithm::Lprec
        ) {
            return Ok(None);
        }
        Ok(Some(Box::new(super::streaming::WgpuFbpStream::new(
            self.share(),
            algorithm,
            params.clone(),
        ))))
    }
}

impl WgpuBackend {
    /// Forward-project `vol_buf` into `sino_buf` (both device-resident), zeroing
    /// `sino_buf` first (the per-voxel atomic-splat kernel accumulates). `cossin_buf` /
    /// `center_buf` are the geometry buffers uploaded once by the iterative solver
    /// so the per-iteration projection reuses them. Unweighted (the plain
    /// line-integral Radon transform), matching [`Self::backproject_into_dev`]'s
    /// gain-1 adjoint.
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn forward_into_dev(
        &self,
        vol_buf: &wgpu::Buffer,
        cossin_buf: &wgpu::Buffer,
        center_buf: &wgpu::Buffer,
        sino_buf: &wgpu::Buffer,
        dims: (usize, usize, usize), // (nz, ny, nx)
        nang: usize,
        ncols: usize,
    ) {
        let (nz, ny, nx) = dims;
        self.zero_buffer(sino_buf);
        let params = FpParams {
            nproj: nang as u32,
            ncols: ncols as u32,
            ny: ny as u32,
            nx: nx as u32,
            _pad0: 0,
            _pad1: 0,
            _pad2: 0,
            _pad3: 0,
        };
        let pbuf = self.uniform("fp_params", &params);
        // One thread per object voxel (atomic-splat); sino zeroed above.
        let src = format!("{}{PROJECT_WGSL}", self.atomic_f32_decl("sino", 3));
        self.dispatch1d(
            &src,
            "project",
            &[vol_buf, cossin_buf, center_buf, sino_buf, &pbuf],
            (nz * ny * nx) as u32,
        );
    }

    /// Back-project `sino_buf` into `vol_buf` (both device-resident; the voxel
    /// kernel overwrites, so no pre-zero). Gain 1 — the pure adjoint of
    /// [`Self::forward_into_dev`]'s unweighted scatter (the FBP π/nproj dθ
    /// weight belongs to the analytic paths only). Shares the geometry buffers
    /// with [`Self::forward_into_dev`].
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn backproject_into_dev(
        &self,
        sino_buf: &wgpu::Buffer,
        cossin_buf: &wgpu::Buffer,
        center_buf: &wgpu::Buffer,
        vol_buf: &wgpu::Buffer,
        dims: (usize, usize, usize), // (nz, ny, nx)
        nang: usize,
        ncols: usize,
    ) {
        let (nz, ny, nx) = dims;
        let params = BpParams {
            nproj: nang as u32,
            ncols: ncols as u32,
            ny: ny as u32,
            nx: nx as u32,
            scale: 1.0, // pure adjoint Wᵀ — no angular-quadrature gain
            _pad0: 0,
            _pad1: 0,
            _pad2: 0,
        };
        let pbuf = self.uniform("bp_params", &params);
        self.dispatch1d(
            BACKPROJECT_WGSL,
            "backproject",
            &[sino_buf, cossin_buf, center_buf, vol_buf, &pbuf],
            (nz * ny * nx) as u32,
        );
    }

    /// Device-resident SIRT (tomopy `sirt`): keep the volume, measured sinogram,
    /// and the R/C weights resident on the GPU across every iteration — one
    /// upload, one download — instead of the generic host solver's per-iteration
    /// forward/back-projection round-trips. Mirrors the CUDA `sirt_device`
    /// arithmetic (unweighted matched projector pair, R = 1/A(1), C = 1/Aᵀ(1),
    /// x += C∘Aᵀ(R∘(b−Ax))), so the result matches the host SIRT within GPU ULPs.
    fn sirt_device(
        &self,
        sino: &Tomo<f32>,
        geom: &Geometry,
        params: &crate::params::ReconParams,
    ) -> Result<Volume<f32>> {
        let s = sino.as_layout(Layout::Sinogram);
        let nz = s.n_rows();
        let nang = s.n_angles();
        let ncols = s.n_cols();
        let n = params.num_gridx.unwrap_or(ncols);
        let nvol = nz * n * n;
        let nsino = nz * nang * ncols;

        // Geometry buffers uploaded once, reused every iteration.
        let (cossin, center) = cossin_center(geom, nz);
        let cossin_buf = self.storage_ro("sirt_cossin", &cossin);
        let center_buf = self.storage_ro("sirt_center", &center);

        // Measured sinogram b (device-resident for the whole solve).
        let sino_std = s.array.as_standard_layout();
        let b_buf = self.storage_ro(
            "sirt_b",
            sino_std
                .as_slice()
                .expect("as_standard_layout yields a contiguous slice"),
        );

        // Seed x: warm-start `init` (validated [nz, n, n]) or zeros.
        let vol_buf = match &params.init {
            Some(v) => {
                if v.dims() != (nz, n, n) {
                    return Err(Error::ShapeMismatch {
                        expected: format!("init volume [{nz}, {n}, {n}]"),
                        found: format!("{:?}", v.dims()),
                    });
                }
                let vstd = v.array.as_standard_layout();
                self.storage_rw(
                    "sirt_vol",
                    vstd.as_slice().expect("standard layout init volume"),
                )
            }
            None => self.storage_rw("sirt_vol", &vec![0.0f32; nvol]),
        };

        let ax_buf = self.storage_empty("sirt_ax", nsino); // A x, reused as residual
        let corr_buf = self.storage_empty("sirt_corr", nvol); // Aᵀ(…)
        let rw_buf = self.storage_empty("sirt_rw", nsino); // R = 1/A(1)
        let cw_buf = self.storage_empty("sirt_cw", nvol); // C = 1/Aᵀ(1)
        let ones_v = self.storage_ro("sirt_ones_v", &vec![1.0f32; nvol]);
        let ones_s = self.storage_ro("sirt_ones_s", &vec![1.0f32; nsino]);

        let dims = (nz, n, n);
        // R = 1 / A(1)
        self.forward_into_dev(
            &ones_v,
            &cossin_buf,
            &center_buf,
            &ax_buf,
            dims,
            nang,
            ncols,
        );
        self.dispatch1d(
            ELEMENTWISE_WGSL,
            "iter_recip",
            &[&ax_buf, &rw_buf],
            nsino as u32,
        );
        // C = 1 / Aᵀ(1)
        self.backproject_into_dev(
            &ones_s,
            &cossin_buf,
            &center_buf,
            &corr_buf,
            dims,
            nang,
            ncols,
        );
        self.dispatch1d(
            ELEMENTWISE_WGSL,
            "iter_recip",
            &[&corr_buf, &cw_buf],
            nvol as u32,
        );

        for _ in 0..params.num_iter.max(1) {
            // ax = A x
            self.forward_into_dev(
                &vol_buf,
                &cossin_buf,
                &center_buf,
                &ax_buf,
                dims,
                nang,
                ncols,
            );
            // ax = (b − ax) ∘ R
            self.dispatch1d(
                ELEMENTWISE_WGSL,
                "iter_residual",
                &[&ax_buf, &b_buf, &rw_buf],
                nsino as u32,
            );
            // corr = Aᵀ(ax)
            self.backproject_into_dev(
                &ax_buf,
                &cossin_buf,
                &center_buf,
                &corr_buf,
                dims,
                nang,
                ncols,
            );
            // x += C ∘ corr
            self.dispatch1d(
                ELEMENTWISE_WGSL,
                "iter_update",
                &[&vol_buf, &cw_buf, &corr_buf],
                nvol as u32,
            );
        }

        let host = self.download_f32(&vol_buf, nvol);
        let array = Array3::from_shape_vec((nz, n, n), host)
            .expect("out buffer length nz*n*n matches the volume shape");
        Ok(Volume::new(array))
    }
}

impl crate::backend::IterativeReconstruct for WgpuBackend {
    /// SIRT runs device-resident; every other iterative algorithm returns `None`
    /// so recon::recon falls back to the generic host solver.
    fn solve(
        &self,
        sino: &Tomo<f32>,
        geom: &Geometry,
        algorithm: crate::params::Algorithm,
        params: &crate::params::ReconParams,
    ) -> Result<Option<Volume<f32>>> {
        // The device forward/back-projectors are parallel-beam only; anything else
        // falls back to the host solver.
        if geom.beam != Beam::Parallel {
            return Ok(None);
        }
        match algorithm {
            crate::params::Algorithm::Sirt => self.sirt_device(sino, geom, params).map(Some),
            _ => Ok(None),
        }
    }
}