iridia-dxil 0.5.0

Translate Direct3D Shader Model 6 bytecode (DXIL, dxc output) to naga modules, for WGSL or SPIR-V output
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
//! The module's globals for the resources the program creates handles for, and the operations on them: samples,
//! loads, stores, queries, constant buffer reads.

use std::collections::{BTreeMap, BTreeSet};
use std::num::NonZeroU32;

use iridia::build::{Kind, Val, Width, component};
use iridia::{
    Access, BufferKind, Resource, ResourceId, ResourceKind, ResourceProvenance, SampleType, ScalarType,
    StorageTarget, TextureDim,
};
use naga::{
    AddressSpace, ArraySize, BinaryOperator as Bo, Expression as E, GlobalVariable, Handle, ImageClass,
    ImageQuery, ResourceBinding, SampleLevel, StorageAccess, TypeInner,
};

use super::setup::{binding_of, const_int, dx_opcode};
use super::{Lift, Lv};
use crate::Result;
use dxil_reader::module::{Instruction, Op, ValueId};

#[derive(Default)]
pub(super) struct Resources {
    pub bound: Vec<(ResourceId, Handle<GlobalVariable>)>,
    globals: Vec<Handle<GlobalVariable>>,
    /// Resources compared against a reference: depth images.
    depth: BTreeSet<usize>,
    /// The access the program makes of each unordered-access view: (loads, stores).
    views: BTreeMap<usize, (bool, bool)>,
    /// The unordered-access buffers an atomic operation reaches, and the integer type their atomics have.
    pub(super) atomic: BTreeMap<usize, naga::Scalar>,
    /// The unordered-access buffers whose counter the program updates.
    counted: BTreeSet<usize>,
    /// The hidden counter of such a buffer: its resource's global.
    counters: BTreeMap<usize, Handle<GlobalVariable>>,
    /// The table of D3D's standard sample positions, made when a program asks for one.
    sample_positions: Option<Handle<GlobalVariable>>,
    /// The globals are made (a program of several functions makes them once).
    pub(super) globals_made: bool,
}

/// D3D's standard sample positions in sixteenths of a pixel from its centre (x right, y down), for 1, 2, 4, 8 and 16
/// samples one after the other: the positions of a count start at `count - 1`.
const STANDARD_POSITIONS: [(i8, i8); 31] = [
    (0, 0),
    (4, 4),
    (-4, -4),
    (-2, -6),
    (6, -2),
    (-6, 2),
    (2, 6),
    (1, -3),
    (-1, 3),
    (5, 1),
    (-3, -5),
    (-5, 5),
    (-7, -1),
    (3, 7),
    (7, -7),
    (1, 1),
    (-1, -3),
    (-3, 2),
    (4, -1),
    (-5, -2),
    (2, 5),
    (5, 3),
    (3, -5),
    (-2, 6),
    (0, -7),
    (-4, -6),
    (-6, 4),
    (-8, 0),
    (7, -4),
    (6, 7),
    (-7, -8),
];

/// The kind an atomic integer type is read as.
pub(super) fn kind_of_atomic(scalar: naga::Scalar) -> Kind {
    if scalar == naga::Scalar::I32 {
        Kind::I
    } else {
        Kind::U
    }
}

fn dim_of(d: TextureDim) -> naga::ImageDimension {
    match d {
        TextureDim::D1 => naga::ImageDimension::D1,
        TextureDim::D2 => naga::ImageDimension::D2,
        TextureDim::D3 => naga::ImageDimension::D3,
        TextureDim::Cube => naga::ImageDimension::Cube,
    }
}

fn kind_of_sample(s: SampleType) -> Kind {
    match s {
        SampleType::Sint => Kind::I,
        SampleType::Uint => Kind::U,
        _ => Kind::F,
    }
}

impl Lift<'_> {
    /// The interface resource a handle value names: a `CreateHandle` call with constant arguments, or a handle
    /// annotated with its properties (which is the handle it annotates, however many annotations deep). A chain of
    /// annotations longer than the function has instructions loops back on itself and names no resource.
    fn handle_of(&self, mut v: ValueId) -> Option<usize> {
        for _ in 0..=self.f.instructions.len() {
            let inst = self
                .f
                .instructions
                .iter()
                .find(|i| i.result.is_some_and(|(r, _)| r == v))?;
            let Op::Call { function, args, .. } = &inst.op else {
                return None;
            };
            return match dx_opcode(self.m, self.f, *function, args)? {
                57 => {
                    let class = const_int(self.m, self.f, *args.get(1)?)?;
                    let id = const_int(self.m, self.f, *args.get(2)?)?;
                    self.setup
                        .resource(u8::try_from(class).ok()?, u32::try_from(id).ok()?)
                }
                217 => {
                    let (class, lower, space) = binding_of(self.m, self.f, *args.get(1)?)?;
                    self.setup.resource_by_binding(class, lower, space)
                }
                // a handle annotated with its properties is the same handle
                216 => {
                    v = *args.get(1)?;
                    continue;
                }
                // a library's handle is made from the value of the resource's global
                160 => self.setup.resource_of_variable(super::setup::variable_loaded(
                    self.m,
                    self.f,
                    *args.get(1)?,
                )?),
                218 => self.setup.heap.get(&v).copied(),
                _ => None,
            };
        }
        None
    }

    /// Scan the program for how it uses each resource, before the globals are made.
    fn scan_resources(&mut self) {
        let mut depth = BTreeSet::new();
        let mut views: BTreeMap<usize, (bool, bool)> = BTreeMap::new();
        let mut atomic: BTreeMap<usize, naga::Scalar> = BTreeMap::new();
        let mut counted = BTreeSet::new();
        let original = self.f;
        for f in self.functions.clone() {
            self.f = f;
            for inst in &f.instructions {
                let Op::Call { function, args, .. } = &inst.op else {
                    continue;
                };
                let Some(code) = dx_opcode(self.m, self.f, *function, args) else {
                    continue;
                };
                let handle = args.get(1).and_then(|&a| self.handle_of(a));
                let Some(r) = handle else { continue };
                match code {
                    64 | 65 | 74 => {
                        depth.insert(r);
                    }
                    66 | 68 | 139 => {
                        if matches!(self.setup.kind(r), ResourceKind::Storage { .. }) {
                            views.entry(r).or_default().0 = true;
                        }
                    }
                    67 | 69 | 140 => views.entry(r).or_default().1 = true,
                    70 => {
                        counted.insert(r);
                    }
                    // atomics read and write; a signed min or max (4, 5) makes the buffer's atomics signed
                    78 | 79 => {
                        views.insert(r, (true, true));
                        let signed = code == 78
                            && matches!(
                                args.get(2).and_then(|&a| const_int(self.m, self.f, a)),
                                Some(4 | 5)
                            );
                        let entry = atomic.entry(r).or_insert(naga::Scalar::U32);
                        if signed {
                            *entry = naga::Scalar::I32;
                        }
                    }
                    _ => {}
                }
            }
        }
        self.f = original;
        self.resources.depth = depth;
        self.resources.views = views;
        self.resources.atomic = atomic;
        self.resources.counted = counted;
    }

    pub(super) fn declare_resources(&mut self) -> Result<()> {
        self.scan_resources();
        let u4 = self.b.ty_of(Kind::U, Width::Four);
        let u1 = self.b.ty_of(Kind::U, Width::One);
        for i in 0..self.setup.interface.resources.len() {
            let (range, resource) = self.resource_parts(i)?;
            let depth = self.resources.depth.contains(&i);
            let (name, space, group, ty) = match &resource.kind {
                ResourceKind::ConstantBuffer { layout } => {
                    let rows = layout.size.div_ceil(16).max(1);
                    let ty = self.b.ty(TypeInner::Array {
                        base: u4,
                        size: ArraySize::Constant(
                            NonZeroU32::new(rows)
                                .map_or_else(|| self.refuse("a constant buffer of no rows"), Ok)?,
                        ),
                        stride: 16,
                    });
                    (format!("cb{}", range.lower), AddressSpace::Uniform, 0, ty)
                }
                ResourceKind::Texture {
                    dim,
                    arrayed,
                    multisampled,
                    sample,
                } => {
                    let class = if depth {
                        ImageClass::Depth { multi: *multisampled }
                    } else {
                        ImageClass::Sampled {
                            kind: kind_of_sample(*sample).kind(),
                            multi: *multisampled,
                        }
                    };
                    let ty = self.b.ty(TypeInner::Image {
                        dim: dim_of(*dim),
                        arrayed: *arrayed,
                        class,
                    });
                    if depth {
                        self.set_kind(
                            i,
                            ResourceKind::Texture {
                                dim: *dim,
                                arrayed: *arrayed,
                                multisampled: *multisampled,
                                sample: SampleType::Depth,
                            },
                        )?;
                    }
                    (format!("t{}", range.lower), AddressSpace::Handle, 1, ty)
                }
                // DXIL states no fixed-function state: its resources come from its own metadata
                ResourceKind::PipelineState(_) => {
                    return Err(crate::error(self.at, "a DXIL resource is pipeline state"));
                }
                ResourceKind::AccelerationStructure => {
                    let ty = self
                        .b
                        .ty(TypeInner::AccelerationStructure { vertex_return: false });
                    (format!("t{}", range.lower), AddressSpace::Handle, 1, ty)
                }
                ResourceKind::Sampler { comparison } => {
                    let ty = self.b.ty(TypeInner::Sampler {
                        comparison: *comparison,
                    });
                    (format!("s{}", range.lower), AddressSpace::Handle, 2, ty)
                }
                ResourceKind::Buffer(kind) => {
                    let (base, stride) = match kind {
                        BufferKind::Typed(_) => (u4, 16),
                        _ => (u1, 4),
                    };
                    let ty = self.b.ty(TypeInner::Array {
                        base,
                        size: ArraySize::Dynamic,
                        stride,
                    });
                    // a typed buffer is carried as four 32-bit lanes per element, whatever the view's own width
                    if let BufferKind::Typed(v) = kind {
                        self.set_kind(
                            i,
                            ResourceKind::Buffer(BufferKind::Typed(iridia::Value { width: 4, ..*v })),
                        )?;
                    }
                    (
                        format!("sb{}", range.lower),
                        AddressSpace::Storage {
                            access: StorageAccess::LOAD,
                        },
                        1,
                        ty,
                    )
                }
                ResourceKind::Storage {
                    target: StorageTarget::Image { dim, arrayed, sample },
                    ..
                } => {
                    let (load, store) = self.resources.views.get(&i).copied().unwrap_or((true, false));
                    let mut access = StorageAccess::empty();
                    access.set(StorageAccess::LOAD, load || !store);
                    access.set(StorageAccess::STORE, store);
                    // a view that atomic operations reach is one lane of 32 bits: the formats naga has image atomics for
                    let atomic = self.resources.atomic.get(&i).copied();
                    access.set(StorageAccess::ATOMIC, atomic.is_some());
                    let format = match (atomic, kind_of_sample(*sample)) {
                        (Some(naga::Scalar::I32), _) => naga::StorageFormat::R32Sint,
                        (Some(_), _) => naga::StorageFormat::R32Uint,
                        (None, Kind::I) => naga::StorageFormat::Rgba32Sint,
                        (None, Kind::U) => naga::StorageFormat::Rgba32Uint,
                        (None, _) => naga::StorageFormat::Rgba32Float,
                    };
                    let ty = self.b.ty(TypeInner::Image {
                        dim: dim_of(*dim),
                        arrayed: *arrayed,
                        class: ImageClass::Storage { format, access },
                    });
                    self.set_kind(
                        i,
                        ResourceKind::Storage {
                            target: StorageTarget::Image {
                                dim: *dim,
                                arrayed: *arrayed,
                                sample: *sample,
                            },
                            access: match (load, store) {
                                (true, true) => Access::ReadWrite,
                                (false, true) => Access::Write,
                                _ => Access::Read,
                            },
                        },
                    )?;
                    self.notes.insert(
                        "a typed unordered-access view is carried as a storage image of four 32-bit lanes: its format \
                         is the binding's"
                            .into(),
                    );
                    (format!("u{}", range.lower), AddressSpace::Handle, 3, ty)
                }
                ResourceKind::Storage {
                    target: StorageTarget::Buffer(kind),
                    ..
                } => {
                    let (load, store) = self.resources.views.get(&i).copied().unwrap_or((true, false));
                    // naga has no write-only storage buffers: the global is read-write, the interface says what is done
                    let mut access = StorageAccess::LOAD;
                    access.set(StorageAccess::STORE, store);
                    let atomic = self.resources.atomic.get(&i).copied();
                    let (base, stride) = match (kind, atomic) {
                        (BufferKind::Typed(_), Some(scalar)) => (self.b.atomic_quad(scalar), 16),
                        (BufferKind::Typed(_), None) => (u4, 16),
                        (_, Some(scalar)) => (self.b.ty(TypeInner::Atomic(scalar)), 4),
                        (_, None) => (u1, 4),
                    };
                    let ty = self.b.ty(TypeInner::Array {
                        base,
                        size: ArraySize::Dynamic,
                        stride,
                    });
                    // a typed buffer is carried as four 32-bit lanes per element, whatever the view's own width
                    let kind = match kind {
                        BufferKind::Typed(v) => BufferKind::Typed(iridia::Value { width: 4, ..*v }),
                        other => *other,
                    };
                    self.set_kind(
                        i,
                        ResourceKind::Storage {
                            target: StorageTarget::Buffer(kind),
                            access: match (load, store) {
                                (true, true) => Access::ReadWrite,
                                (false, true) => Access::Write,
                                _ => Access::Read,
                            },
                        },
                    )?;
                    (
                        format!("u{}", range.lower),
                        AddressSpace::Storage { access },
                        3,
                        ty,
                    )
                }
            };
            // a resource array is a binding array of the resource's own type, dynamically sized when unbounded
            let ty = if resource.is_array() {
                let buffers = matches!(
                    resource.kind,
                    ResourceKind::ConstantBuffer { .. }
                        | ResourceKind::Buffer(_)
                        | ResourceKind::Storage {
                            target: StorageTarget::Buffer(_),
                            ..
                        }
                );
                let images = matches!(
                    resource.kind,
                    ResourceKind::Storage {
                        target: StorageTarget::Image { .. },
                        ..
                    }
                );
                self.notes.insert(if matches!(resource.kind, ResourceKind::ConstantBuffer { .. }) {
                    "a constant-buffer array needs BUFFER_BINDING_ARRAY (with non-uniform indexing), each element a structure holding the buffer's rows, and an unbounded one is sized by the bound data".into()
                } else if buffers {
                    "a resource array is a binding array: it needs STORAGE_BUFFER_BINDING_ARRAY (with non-uniform \
                     indexing), each element a structure holding the buffer's words, and an unbounded one is sized by \
                     the bound data"
                        .into()
                } else if images {
                    "a resource array is a binding array: it needs STORAGE_TEXTURE_BINDING_ARRAY (with non-uniform \
                     indexing), and an unbounded one is sized by the bound data"
                        .into()
                } else {
                    "a resource array is a binding array: it needs TEXTURE_AND_SAMPLER_BINDING_ARRAY (with non-uniform \
                     indexing), and an unbounded one is sized by the bound data"
                        .into()
                });
                // a buffer is a structure holding its runtime-sized array, for naga to bind several of them
                let base = if buffers {
                    let stride = match &resource.kind {
                        ResourceKind::ConstantBuffer { layout } => {
                            layout.size.div_ceil(16).max(1).checked_mul(16).ok_or_else(|| {
                                crate::error(self.at, "constant buffer array element size overflows")
                            })?
                        }
                        ResourceKind::Buffer(BufferKind::Typed(_))
                        | ResourceKind::Storage {
                            target: StorageTarget::Buffer(BufferKind::Typed(_)),
                            ..
                        } => 16,
                        _ => 4,
                    };
                    self.b.ty(TypeInner::Struct {
                        members: vec![naga::StructMember {
                            name: Some("data".into()),
                            ty,
                            binding: None,
                            offset: 0,
                        }],
                        span: stride,
                    })
                } else {
                    ty
                };
                let size = match NonZeroU32::new(resource.count) {
                    Some(n) if !resource.is_unbounded() => ArraySize::Constant(n),
                    _ => ArraySize::Dynamic,
                };
                self.b.ty(TypeInner::BindingArray { base, size })
            } else {
                ty
            };
            let binding = if range.space == 0 {
                range.lower
            } else {
                (range.space << 16) | range.lower
            };
            let g = self.b.global(GlobalVariable {
                name: Some(name),
                space,
                binding: Some(ResourceBinding { group, binding }),
                ty,
                init: None,
                memory_decorations: Default::default(),
            });
            self.resources.bound.push((resource.id, g));
            self.resources.globals.push(g);
        }
        self.declare_counters()
    }

    /// The hidden counter of each unordered-access buffer whose counter the program updates: a resource of its own
    /// (one raw read-write buffer, `counter_of` the view), a buffer of its own in WebGPU, group 4 at the view's binding.
    fn declare_counters(&mut self) -> Result<()> {
        for view in self.resources.counted.clone() {
            let (range, view_resource) = self.resource_parts(view)?;
            let name = view_resource.provenance.name.clone();
            let id = ResourceId(
                u32::try_from(self.setup.interface.resources.len())
                    .map_err(|_| crate::error(self.at, "more than 4294967295 resources"))?,
            );
            self.setup.interface.resources.push(Resource {
                id,
                kind: ResourceKind::Storage {
                    target: StorageTarget::Buffer(BufferKind::Raw),
                    access: Access::ReadWrite,
                },
                count: 1,
                provenance: ResourceProvenance {
                    name: format!("{name}.counter"),
                    d3d: None,
                    counter_of: Some(view_resource.id),
                    layout: None,
                },
            });
            self.setup.ranges.push(range);
            let atomic = self.b.ty(TypeInner::Atomic(naga::Scalar::U32));
            let ty = self.b.ty(TypeInner::Array {
                base: atomic,
                size: ArraySize::Dynamic,
                stride: 4,
            });
            let binding = if range.space == 0 {
                range.lower
            } else {
                (range.space << 16) | range.lower
            };
            let g = self.b.global(GlobalVariable {
                name: Some(format!("u{}_counter", range.lower)),
                space: AddressSpace::Storage {
                    access: StorageAccess::LOAD | StorageAccess::STORE,
                },
                binding: Some(ResourceBinding { group: 4, binding }),
                ty,
                init: None,
                memory_decorations: Default::default(),
            });
            self.resources.bound.push((id, g));
            self.resources.globals.push(g);
            self.resources.counters.insert(view, g);
        }
        Ok(())
    }

    /// `BufferUpdateCounter(handle, increment)`: add 1 or subtract 1 from the view's counter. An increment gives the
    /// counter as it was (`IncrementCounter`), a decrement as it is after (`DecrementCounter`), as D3D's append and
    /// consume instructions do.
    pub(super) fn buffer_update_counter(&mut self, inst: &Instruction, args: &[ValueId]) -> Result<()> {
        let view = self.handle_resource(self.arg(args, 1)?, 1)?;
        let Some(&counter) = self.resources.counters.get(&view) else {
            return self.refuse("a counter update of a view the scan did not find");
        };
        let Some(step) = const_int(self.m, self.f, self.arg(args, 2)?) else {
            return self.refuse("a counter updated by a step that is not constant");
        };
        if step != 1 && step != -1 {
            return self.refuse(format!("a counter updated by {step}"));
        }
        self.notes.insert(
            "an unordered-access view's hidden counter is a buffer of its own (the resource marked counter_of), bound \
             with the view and started at its initial count"
                .into(),
        );
        let global = self.b.global_ptr(counter);
        let zero = self.b.lit_u(0);
        let slot = self.b.app(E::Access {
            base: global,
            index: zero.expr,
        });
        let amount = self.b.lit_u(super::low32(step as u64));
        let old = self
            .b
            .atomic(slot, naga::AtomicFunction::Add, amount, naga::Scalar::U32);
        let result = if step == 1 {
            old
        } else {
            let one = self.b.lit_u(1);
            self.b.binary(Bo::Subtract, old, one)
        };
        self.define_result(inst.result, Lv::Scalar(result))
    }

    pub(super) fn texture_shape(&self, resource: usize) -> Result<(TextureDim, bool, bool, Kind, bool)> {
        match self.setup.kind(resource) {
            ResourceKind::Texture {
                dim,
                arrayed,
                multisampled,
                sample,
            } => Ok((
                *dim,
                *arrayed,
                *multisampled,
                kind_of_sample(*sample),
                *sample == SampleType::Depth,
            )),
            ResourceKind::Storage {
                target: StorageTarget::Image { dim, arrayed, sample },
                ..
            } => Ok((*dim, *arrayed, false, kind_of_sample(*sample), false)),
            _ => self.refuse("a handle that is not a texture"),
        }
    }

    /// The interface resource a handle of `class` names, whether or not it picks an element of an array.
    pub(super) fn handle_resource(&mut self, v: ValueId, class: u8) -> Result<usize> {
        match self.get(v)? {
            Lv::Handle {
                class: c, resource, ..
            } if c == class => Ok(resource),
            _ => self.refuse(format!("a handle of class {class} was expected")),
        }
    }

    /// The interface resource of a buffer handle of `class` and a pointer to its array of words (an element of a
    /// buffer array is a structure holding the array: the handle's element picks it).
    pub(super) fn buffer_words(&mut self, v: ValueId, class: u8) -> Result<(usize, Handle<E>)> {
        let Lv::Handle {
            class: c,
            resource,
            element,
        } = self.get(v)?
        else {
            return self.refuse(format!("a handle of class {class} was expected"));
        };
        if c != class {
            return self.refuse(format!("a handle of class {class} was expected"));
        }
        let global = self.b.global_ptr(self.global_of(resource)?);
        let words = match element {
            None => global,
            Some(index) => {
                let item = self.b.app(E::Access {
                    base: global,
                    index: index.expr,
                });
                self.b.app(E::AccessIndex { base: item, index: 0 })
            }
        };
        Ok((resource, words))
    }

    /// The interface resource a handle of `class` names and the expression of the image or sampler it reads: the
    /// global, or the element of its binding array the program's index picks.
    pub(super) fn handle_expr(&mut self, v: ValueId, class: u8) -> Result<(usize, Handle<E>)> {
        let Lv::Handle {
            class: c,
            resource,
            element,
        } = self.get(v)?
        else {
            return self.refuse(format!("a handle of class {class} was expected"));
        };
        if c != class {
            return self.refuse(format!("a handle of class {class} was expected"));
        }
        let global = self.b.global_ptr(self.global_of(resource)?);
        let expr = match element {
            None => global,
            Some(index) => self.b.app(E::Access {
                base: global,
                index: index.expr,
            }),
        };
        Ok((resource, expr))
    }

    /// The element of an array a handle's index picks: the index less the range's first register.
    fn element_of(&mut self, resource: usize, index: ValueId, constant: Option<i64>) -> Result<Option<Val>> {
        let (range, described) = self.resource_parts(resource)?;
        let (count, lower) = (described.count, range.lower);
        if count == 1 {
            return match constant {
                Some(i) if i == i64::from(lower) => Ok(None),
                Some(_) => self.refuse("a handle at an index other than its range's register"),
                None => self.refuse("a handle of a computed index into a range of one"),
            };
        }
        let element = match constant {
            Some(i) => match u32::try_from(i).ok().and_then(|i| i.checked_sub(lower)) {
                Some(e) => self.b.lit_u(e),
                None => return self.refuse("a handle at an index below its range"),
            },
            None => {
                let at = self.uint(index)?;
                let lower = self.b.lit_u(lower);
                self.b.binary(Bo::Subtract, at, lower)
            }
        };
        Ok(Some(element))
    }

    /// `CreateHandle(class, range id, index, non-uniform)`: the interface resource of a constant range, at an element
    /// when the range is an array.
    pub(super) fn create_handle(&mut self, inst: &Instruction, args: &[ValueId]) -> Result<()> {
        let konst = |lift: &Self, n: usize| args.get(n).and_then(|&a| const_int(lift.m, lift.f, a));
        let (Some(class), Some(id)) = (konst(self, 1), konst(self, 2)) else {
            return self.refuse("a handle of a computed range");
        };
        let (Ok(class), Ok(id)) = (u8::try_from(class), u32::try_from(id)) else {
            return self.refuse(format!("a handle of range {id} of class {class}"));
        };
        let Some(resource) = self.setup.resource(class, id) else {
            return self.refuse("a handle of an undeclared range");
        };
        let constant = konst(self, 3);
        let element = self.element_of(resource, self.arg(args, 3)?, constant)?;
        self.define_result(
            inst.result,
            Lv::Handle {
                class,
                resource,
                element,
            },
        )
    }

    /// `CreateHandleFromBinding(binding, index, non-uniform)` (Shader Model 6.6): the resource of a constant binding,
    /// at an element when the binding is an array.
    pub(super) fn create_handle_from_binding(&mut self, inst: &Instruction, args: &[ValueId]) -> Result<()> {
        let Some((class, lower, space)) = binding_of(self.m, self.f, self.arg(args, 1)?) else {
            return self.refuse("a handle of a binding that is not constant");
        };
        let Some(resource) = self.setup.resource_by_binding(class, lower, space) else {
            return self.refuse("a handle of an undeclared binding");
        };
        let constant = const_int(self.m, self.f, self.arg(args, 2)?);
        let element = self.element_of(resource, self.arg(args, 2)?, constant)?;
        self.define_result(
            inst.result,
            Lv::Handle {
                class,
                resource,
                element,
            },
        )
    }

    /// `CreateHandleFromHeap(index, sampler heap, non-uniform)`: an element of the unbounded array the annotation
    /// makes of the heap's descriptors of that kind.
    pub(super) fn create_handle_from_heap(&mut self, inst: &Instruction, args: &[ValueId]) -> Result<()> {
        let Some((handle, _)) = inst.result else {
            return self.refuse("a heap handle without a result");
        };
        let Some(&resource) = self.setup.heap.get(&handle) else {
            return self.refuse("a heap handle the setup did not find");
        };
        self.notes.insert(
            "a descriptor heap's resources are carried as one unbounded binding array for each kind of view the \
             program annotates them as, in a register space of its own: the renderer binds a heap's descriptors of \
             that kind"
                .into(),
        );
        let class = match self.setup.kind(resource) {
            ResourceKind::Sampler { .. } => 3,
            ResourceKind::ConstantBuffer { .. } => 2,
            ResourceKind::Storage { .. } => 1,
            _ => 0,
        };
        let index = self.uint(self.arg(args, 1)?)?;
        self.define_result(
            inst.result,
            Lv::Handle {
                class,
                resource,
                element: Some(index),
            },
        )
    }

    /// `CreateHandleForLib(resource)` (a library): the value a load of the resource's global gave, which already names
    /// the resource.
    pub(super) fn create_handle_for_lib(&mut self, inst: &Instruction, args: &[ValueId]) -> Result<()> {
        let value = self.get(self.arg(args, 1)?)?;
        if !matches!(value, Lv::Handle { .. }) {
            return self.refuse("a handle made from something that is not a resource of the library");
        }
        self.define_result(inst.result, value)
    }

    /// `AnnotateHandle(handle, properties)`: the handle itself; the properties repeat what the resource declares.
    pub(super) fn annotate_handle(&mut self, inst: &Instruction, args: &[ValueId]) -> Result<()> {
        let handle = self.get(self.arg(args, 1)?)?;
        if !matches!(handle, Lv::Handle { .. }) {
            return self.refuse("an annotation of something that is not a handle");
        }
        self.define_result(inst.result, handle)
    }

    pub(super) fn global_of(&self, resource: usize) -> Result<Handle<GlobalVariable>> {
        self.resources.globals.get(resource).copied().ok_or_else(|| {
            crate::error(
                self.at,
                format!(
                    "resource {resource}, where {} have a global",
                    self.resources.globals.len()
                ),
            )
        })
    }

    /// The range and the interface's resource of resource `i`, which the setup keeps side by side.
    fn resource_parts(&self, i: usize) -> Result<(dxil_reader::reflection::Range, Resource)> {
        match (self.setup.ranges.get(i), self.setup.interface.resources.get(i)) {
            (Some(range), Some(resource)) => Ok((*range, resource.clone())),
            _ => self.refuse(format!(
                "resource {i}, where the program has {}",
                self.setup.interface.resources.len()
            )),
        }
    }

    /// Make resource `i` of the interface this kind.
    fn set_kind(&mut self, i: usize, kind: ResourceKind) -> Result<()> {
        match self.setup.interface.resources.get_mut(i) {
            Some(resource) => {
                resource.kind = kind;
                Ok(())
            }
            None => self.refuse(format!("resource {i}, which the interface does not have")),
        }
    }

    /// `CBufferLoadLegacy(handle, row)`: a row of four 32-bit lanes as the call's overload type.
    pub(super) fn cbuffer_load(&mut self, inst: &Instruction, args: &[ValueId]) -> Result<()> {
        let (resource, pointer) = self.buffer_words(self.arg(args, 1)?, 2)?;
        let rows = match self.setup.kind(resource) {
            ResourceKind::ConstantBuffer { layout } => layout.size.div_ceil(16).max(1),
            _ => return self.refuse("a constant load from a non-buffer"),
        };
        // a row known to be inside the buffer needs no guard
        let lanes = match const_int(self.m, self.f, self.arg(args, 2)?) {
            Some(r) if (0..i64::from(rows)).contains(&r) => {
                let element = self.b.app(E::AccessIndex {
                    base: pointer,
                    index: super::low32(r as u64),
                });
                self.b.load(element, Kind::U, Width::Four)
            }
            _ => {
                let row = self.uint(self.arg(args, 2)?)?;
                self.guarded_row(pointer, row, rows)
            }
        };
        let Some((_, ty)) = inst.result else {
            return self.refuse("a constant load with no result");
        };
        let dxil_reader::module::Type::Struct { elements, .. } = self.type_at(ty)?.clone() else {
            return self.refuse("a constant load whose result is not a struct");
        };
        if elements.len() != 4 {
            return self.refuse(
                "a constant load of other than four 32-bit lanes (16-bit and 64-bit rows are not lifted)",
            );
        }
        let mut out = Vec::new();
        for (lane, &e) in elements.iter().enumerate() {
            let kind = self.kind_of_type(e)?;
            let x = self.b.lane(lanes, super::lane_u8(lane));
            out.push(Lv::Scalar(self.b.bitcast_to(x, kind)));
        }
        self.define_result(inst.result, Lv::Agg(out))
    }

    /// Row `row` of a `vec4<u32>` array global, zero out of range as D3D reads.
    fn guarded_row(&mut self, pointer: Handle<E>, row: Val, rows: u32) -> Val {
        let length = self.b.lit_u(rows);
        let inside = self.b.binary(Bo::Less, row, length);
        let zero = self.b.lit_u(0);
        let safe = self.b.select(inside, row, zero);
        let element = self.b.app(E::Access {
            base: pointer,
            index: safe.expr,
        });
        let value = self.b.load(element, Kind::U, Width::Four);
        let none = {
            let z = self.b.lit_u(0);
            self.b.splat(z, Width::Four)
        };
        let _ = pointer;
        self.b.select(inside, value, none)
    }

    pub(super) fn coordinates(
        &mut self,
        args: &[ValueId],
        first: usize,
        dim: TextureDim,
        arrayed: bool,
        kind: Kind,
    ) -> Result<(Val, Option<Handle<E>>)> {
        let n = match dim {
            TextureDim::D1 => 1,
            TextureDim::D2 => 2,
            _ => 3,
        };
        let mut parts = Vec::new();
        for k in 0..n {
            let v = self.scalar(self.arg(args, first.saturating_add(k))?)?;
            parts.push(self.b.bitcast_to(v, kind));
        }
        let coordinate = self.b.compose(&parts);
        let layer = if arrayed {
            let v = self.scalar(self.arg(args, first.saturating_add(n))?)?;
            Some(self.b.bitcast_to(v, kind))
        } else {
            None
        };
        let array_index = match layer {
            Some(l) if kind == Kind::F => {
                let rounded = self.b.math(naga::MathFunction::Round, &[l]);
                let zero = self.b.lit_f(0.0);
                let clamped = self.b.d3d_min_max(naga::MathFunction::Max, rounded, zero);
                Some(self.b.convert(clamped, Kind::U).expr)
            }
            Some(l) => Some(l.expr),
            None => None,
        };
        Ok((coordinate, array_index))
    }

    fn sample_offset(
        &mut self,
        args: &[ValueId],
        first: usize,
        dim: TextureDim,
    ) -> Result<Option<Handle<E>>> {
        let n = match dim {
            TextureDim::D1 => Width::One,
            TextureDim::D2 => Width::Two,
            TextureDim::D3 => Width::Three,
            TextureDim::Cube => return Ok(None),
        };
        let mut values = Vec::new();
        for k in 0..n.count() {
            match const_int(self.m, self.f, self.arg(args, first.saturating_add(k))?) {
                Some(v) => values.push(i32::from_ne_bytes(super::low32(v as u64).to_ne_bytes())),
                None if self.is_undef(self.arg(args, first.saturating_add(k))?) => values.push(0),
                None => return self.refuse("a sample offset that is not constant"),
            }
        }
        if values.iter().all(|&v| v == 0) {
            return Ok(None);
        }
        let parts: Vec<_> = values
            .iter()
            .map(|&v| self.b.app(E::Literal(naga::Literal::I32(v))))
            .collect();
        Ok(Some(if let [only] = parts[..] {
            only
        } else {
            let ty = self.b.ty_of(Kind::I, n);
            self.b.app(E::Compose {
                ty,
                components: parts,
            })
        }))
    }

    fn is_undef(&self, v: ValueId) -> bool {
        matches!(
            self.value_ref(v),
            Some(dxil_reader::module::Value::Constant(
                dxil_reader::module::Constant::Undef
            ))
        )
    }

    /// The sample operations: `Sample`, `SampleBias`, `SampleLevel`, `SampleGrad`, `SampleCmp`,
    /// `SampleCmpLevelZero`, `TextureGather`, `TextureGatherCmp`.
    pub(super) fn sample(&mut self, inst: &Instruction, code: u64, args: &[ValueId]) -> Result<()> {
        let (resource, image) = self.handle_expr(self.arg(args, 1)?, 0)?;
        let (_, smp) = self.handle_expr(self.arg(args, 2)?, 3)?;
        let (dim, arrayed, multi, sample_kind, depth) = self.texture_shape(resource)?;
        if multi {
            return self.refuse("a sample of a multisampled texture");
        }
        let (coordinate, array_index) = self.coordinates(args, 3, dim, arrayed, Kind::F)?;
        let gather_offsets = matches!(code, 73 | 74);
        let offset = self.sample_offset(args, 7, dim)?;
        if gather_offsets && offset.is_some() && matches!(dim, TextureDim::D3) {
            return self.refuse("a gather of a 3D texture");
        }
        let expect_undef = |lift: &Self, v: ValueId| {
            if lift.is_undef(v) || const_int(lift.m, lift.f, v) == Some(0) {
                Ok(())
            } else {
                lift.refuse("a LOD clamp")
            }
        };
        let (gather, depth_ref, level) = match code {
            60 => {
                expect_undef(self, self.arg(args, 10)?)?;
                (None, None, SampleLevel::Auto)
            }
            61 if depth => {
                return self.refuse("a depth texture sampled with a bias (WGSL's depth sampling has none)");
            }
            63 if depth => {
                return self
                    .refuse("a depth texture sampled with gradients (WGSL's depth sampling has none)");
            }
            61 => {
                expect_undef(self, self.arg(args, 11)?)?;
                let bias = self.float(self.arg(args, 10)?)?;
                (None, None, SampleLevel::Bias(bias.expr))
            }
            62 => {
                let lod = self.float(self.arg(args, 10)?)?;
                // level 0 is WGSL's `Zero`, which a depth texture can be sampled at; any other level of a depth
                // texture is an integer there, which a float level is not
                if matches!(
                    self.b.function.expressions[lod.expr],
                    naga::Expression::Literal(naga::Literal::F32(0.0))
                ) {
                    (None, None, SampleLevel::Zero)
                } else if depth {
                    return self.refuse(
                        "a depth texture sampled at a level the program computes (WGSL takes an integer level there)",
                    );
                } else {
                    (None, None, SampleLevel::Exact(lod.expr))
                }
            }
            63 => {
                expect_undef(self, self.arg(args, 16)?)?;
                let n = match dim {
                    TextureDim::D1 => 1,
                    TextureDim::D2 => 2,
                    _ => 3,
                };
                let mut x = Vec::new();
                let mut y = Vec::new();
                for k in 0..n {
                    x.push(self.float(self.arg(args, 10usize.saturating_add(k))?)?);
                    y.push(self.float(self.arg(args, 13usize.saturating_add(k))?)?);
                }
                let (x, y) = (self.b.compose(&x), self.b.compose(&y));
                (None, None, SampleLevel::Gradient { x: x.expr, y: y.expr })
            }
            64 => {
                expect_undef(self, self.arg(args, 11)?)?;
                let reference = self.float(self.arg(args, 10)?)?;
                (None, Some(reference.expr), SampleLevel::Auto)
            }
            65 => {
                let reference = self.float(self.arg(args, 10)?)?;
                (None, Some(reference.expr), SampleLevel::Zero)
            }
            73 | 74 => {
                let channel = const_int(self.m, self.f, self.arg(args, 9)?)
                    .map_or_else(|| self.refuse("a gather channel that is not constant"), Ok)?;
                let reference = if code == 74 {
                    Some(self.float(self.arg(args, 10)?)?.expr)
                } else {
                    None
                };
                (
                    Some(component(super::lane_u8(channel))),
                    reference,
                    SampleLevel::Zero,
                )
            }
            other => return self.refuse(format!("sample operation {other}")),
        };
        let expr = self.b.app(E::ImageSample {
            image,
            sampler: smp,
            gather,
            coordinate: coordinate.expr,
            array_index,
            offset,
            level,
            depth_ref,
            clamp_to_edge: false,
        });
        // a gather of an integer texture returns integers
        let mut r = Val {
            expr,
            kind: sample_kind,
            width: if depth && gather.is_none() {
                Width::One
            } else {
                Width::Four
            },
        };
        if r.width == Width::One {
            // a depth sample is one float; D3D's float view returns it in x and (0, 0, 1) in the others, a comparison
            // in all four
            let (zero, one) = (self.b.lit_f(0.0), self.b.lit_f(1.0));
            r = if depth_ref.is_some() {
                self.b.splat(r, Width::Four)
            } else {
                self.b.compose(&[r, zero, zero, one])
            };
        }
        let lv = self.resret(r);
        self.define_result(inst.result, lv)
    }

    /// A four-lane result as the `ResRet` aggregate: four lanes and the status word (0, every access mapped).
    fn resret(&mut self, v: Val) -> Lv {
        let mut items = Vec::new();
        for lane in 0..4 {
            items.push(Lv::Scalar(self.b.lane(v, lane)));
        }
        items.push(Lv::Scalar(self.b.lit_u(0)));
        Lv::Agg(items)
    }

    /// `TextureLoad(handle, mip or sample, x, y, z, offsets)`.
    pub(super) fn texture_load(&mut self, inst: &Instruction, args: &[ValueId]) -> Result<()> {
        let class = match self.get(self.arg(args, 1)?)? {
            Lv::Handle { class, .. } => class,
            _ => return self.refuse("a load through something that is not a handle"),
        };
        let (resource, image) = self.handle_expr(self.arg(args, 1)?, class)?;
        let (dim, arrayed, multi, kind, depth) = self.texture_shape(resource)?;
        let (coordinate, array_index) = self.coordinates(args, 3, dim, arrayed, Kind::I)?;
        let mut coordinate = coordinate;
        // a load's constant texel offset moves the texel it reads
        if let Some(offset) = self.sample_offset(args, 6, dim)? {
            let offset = Val {
                expr: offset,
                kind: Kind::I,
                width: coordinate.width,
            };
            coordinate = self.b.binary(Bo::Add, coordinate, offset);
        }
        let storage = class == 1;
        let (level, sample) = if storage {
            (None, None)
        } else if multi {
            (None, Some(self.uint(self.arg(args, 2)?)?.expr))
        } else {
            let mip = self.uint(self.arg(args, 2)?)?;
            let mip = self.b.bitcast(mip, Kind::I);
            (Some(mip.expr), None)
        };
        let sample = sample.map(|s| {
            let v = Val {
                expr: s,
                kind: Kind::U,
                width: Width::One,
            };
            self.b.bitcast(v, Kind::I).expr
        });
        if storage {
            // a view takes unsigned addresses
            coordinate = self.b.bitcast(coordinate, Kind::U);
        }
        let array_index = array_index.map(|a| {
            if storage {
                let v = Val {
                    expr: a,
                    kind: Kind::I,
                    width: Width::One,
                };
                self.b.bitcast(v, Kind::U).expr
            } else {
                a
            }
        });
        let expr = self.b.app(E::ImageLoad {
            image,
            coordinate: coordinate.expr,
            array_index,
            sample,
            level,
        });
        let mut r = Val {
            expr,
            kind: if depth { Kind::F } else { kind },
            width: if depth { Width::One } else { Width::Four },
        };
        if depth {
            let (zero, one) = (self.b.lit_f(0.0), self.b.lit_f(1.0));
            r = self.b.compose(&[r, zero, zero, one]);
        }
        self.result_type(inst.result)?;
        let lv = self.resret(r);
        self.define_result(inst.result, lv)
    }

    /// `TextureStore(uav, x, y, z, v0..v3, mask)`.
    pub(super) fn texture_store(&mut self, args: &[ValueId]) -> Result<()> {
        let (resource, image) = self.handle_expr(self.arg(args, 1)?, 1)?;
        let (dim, arrayed, _, kind, _) = self.texture_shape(resource)?;
        if const_int(self.m, self.f, self.arg(args, 9)?) != Some(15) {
            return self.refuse("a store through a view with a partial write mask");
        }
        let (coordinate, array_index) = self.coordinates(args, 2, dim, arrayed, Kind::U)?;
        let mut lanes = Vec::new();
        for k in 0..4 {
            let v = self.scalar(self.arg(args, 5usize.saturating_add(k))?)?;
            lanes.push(self.b.bitcast_to(v, kind));
        }
        let value = self.b.compose(&lanes);
        self.b.push(naga::Statement::ImageStore {
            image,
            coordinate: coordinate.expr,
            array_index,
            value: value.expr,
        });
        Ok(())
    }

    /// `GetDimensions` of a buffer: the element count (the byte size of a raw buffer) and, for a structured buffer, the
    /// stride.
    fn buffer_dimensions(
        &mut self,
        inst: &Instruction,
        handle: ValueId,
        class: u8,
        kind: BufferKind,
    ) -> Result<()> {
        let (_, pointer) = self.buffer_words(handle, class)?;
        let words = Val {
            expr: self.b.app(E::ArrayLength(pointer)),
            kind: Kind::U,
            width: Width::One,
        };
        let four = self.b.lit_u(4);
        let zero = self.b.lit_u(0);
        let (x, y) = match kind {
            // elements of four lanes
            BufferKind::Typed(_) => (words, zero),
            BufferKind::Raw => (self.b.binary(Bo::Multiply, words, four), zero),
            BufferKind::Structured { stride } => {
                let stride = self.b.lit_u(stride);
                let bytes = self.b.binary(Bo::Multiply, words, four);
                (self.b.binary(Bo::Divide, bytes, stride), stride)
            }
        };
        self.define_result(
            inst.result,
            Lv::Agg(vec![
                Lv::Scalar(x),
                Lv::Scalar(y),
                Lv::Scalar(zero),
                Lv::Scalar(zero),
            ]),
        )
    }

    /// `GetDimensions(handle, mip)`: width, height, depth or layers, and the number of levels.
    pub(super) fn get_dimensions(&mut self, inst: &Instruction, args: &[ValueId]) -> Result<()> {
        let class = match self.get(self.arg(args, 1)?)? {
            Lv::Handle { class, .. } => class,
            _ => return self.refuse("dimensions of something that is not a handle"),
        };
        let resource = self.handle_resource(self.arg(args, 1)?, class)?;
        if let ResourceKind::Buffer(kind)
        | ResourceKind::Storage {
            target: StorageTarget::Buffer(kind),
            ..
        } = self.setup.kind(resource).clone()
        {
            return self.buffer_dimensions(inst, self.arg(args, 1)?, class, kind);
        }
        let (resource, g) = self.handle_expr(self.arg(args, 1)?, class)?;
        let (dim, arrayed, multi, _, _) = self.texture_shape(resource)?;
        let mip = self.uint(self.arg(args, 2)?)?;
        let dims = match dim {
            TextureDim::D1 => Width::One,
            TextureDim::D2 | TextureDim::Cube => Width::Two,
            TextureDim::D3 => Width::Three,
        };
        let size = Val {
            expr: self.b.app(E::ImageQuery {
                image: g,
                query: ImageQuery::Size {
                    level: (!multi && class == 0).then_some(mip.expr),
                },
            }),
            kind: Kind::U,
            width: dims,
        };
        let levels = if multi || class != 0 {
            self.b.lit_u(1)
        } else {
            Val {
                expr: self.b.app(E::ImageQuery {
                    image: g,
                    query: ImageQuery::NumLevels,
                }),
                kind: Kind::U,
                width: Width::One,
            }
        };
        let zero = self.b.lit_u(0);
        let mut parts: Vec<Val> = dims.lanes().map(|i| self.b.lane(size, i)).collect();
        if arrayed {
            parts.push(Val {
                expr: self.b.app(E::ImageQuery {
                    image: g,
                    query: ImageQuery::NumLayers,
                }),
                kind: Kind::U,
                width: Width::One,
            });
        }
        while parts.len() < 3 {
            parts.push(zero);
        }
        parts.truncate(3);
        parts.push(levels);
        self.define_result(inst.result, Lv::Agg(parts.into_iter().map(Lv::Scalar).collect()))
    }

    /// `Texture2DMSGetSamplePosition(handle, sample)`: the sample's offset from the pixel's centre in pixels, as D3D's
    /// standard pattern for the texture's sample count gives it.
    pub(super) fn sample_position(&mut self, inst: &Instruction, args: &[ValueId]) -> Result<()> {
        let (resource, image) = self.handle_expr(self.arg(args, 1)?, 0)?;
        let (_, _, multi, _, _) = self.texture_shape(resource)?;
        if !multi {
            return self.refuse("a sample position of a texture that is not multisampled");
        }
        self.notes.insert(
            "a sample position is D3D's standard pattern for the texture's sample count (1, 2, 4, 8 or 16)"
                .into(),
        );
        let table = match self.resources.sample_positions {
            Some(g) => g,
            None => {
                let v2 = self.b.ty_of(Kind::F, Width::Two);
                let ty = self.b.ty(TypeInner::Array {
                    base: v2,
                    size: ArraySize::Constant(NonZeroU32::MIN.saturating_add(30)),
                    stride: 8,
                });
                let mut rows = Vec::new();
                for (x, y) in STANDARD_POSITIONS {
                    let (x, y) = (f32::from(x) / 16.0, f32::from(y) / 16.0);
                    let x = self.b.global_expr(E::Literal(naga::Literal::F32(x)));
                    let y = self.b.global_expr(E::Literal(naga::Literal::F32(y)));
                    rows.push(self.b.global_expr(E::Compose {
                        ty: v2,
                        components: vec![x, y],
                    }));
                }
                let init = self.b.global_expr(E::Compose { ty, components: rows });
                let g = self.b.global(GlobalVariable {
                    name: Some("sample_positions".into()),
                    space: AddressSpace::Private,
                    binding: None,
                    ty,
                    init: Some(init),
                    memory_decorations: Default::default(),
                });
                self.resources.sample_positions = Some(g);
                g
            }
        };
        // the table row of sample `s` of `n`: n - 1 + (s & (n - 1)), kept inside the table
        let count = Val {
            expr: self.b.app(E::ImageQuery {
                image,
                query: ImageQuery::NumSamples,
            }),
            kind: Kind::U,
            width: Width::One,
        };
        let one = self.b.lit_u(1);
        let below = self.b.binary(Bo::Subtract, count, one);
        let sample = self.uint(self.arg(args, 2)?)?;
        let within = self.b.binary(Bo::And, sample, below);
        let row = self.b.binary(Bo::Add, below, within);
        let last = self.b.lit_u(30);
        let row = self.b.math(naga::MathFunction::Min, &[row, last]);
        let base = self.b.global_ptr(table);
        let element = self.b.app(E::Access {
            base,
            index: row.expr,
        });
        let position = self.b.load(element, Kind::F, Width::Two);
        let (x, y) = (self.b.lane(position, 0), self.b.lane(position, 1));
        self.define_result(inst.result, Lv::Agg(vec![Lv::Scalar(x), Lv::Scalar(y)]))
    }

    /// `BufferLoad(handle, index, offset)`: four lanes of a typed, raw or structured buffer.
    pub(super) fn buffer_load(&mut self, inst: &Instruction, args: &[ValueId]) -> Result<()> {
        let class = match self.get(self.arg(args, 1)?)? {
            Lv::Handle { class, .. } => class,
            _ => return self.refuse("a buffer load through something that is not a handle"),
        };
        let (resource, words_ptr) = self.buffer_words(self.arg(args, 1)?, class)?;
        let kind = match self.setup.kind(resource).clone() {
            ResourceKind::Buffer(k)
            | ResourceKind::Storage {
                target: StorageTarget::Buffer(k),
                ..
            } => k,
            _ => return self.refuse("a buffer load from something that is not a buffer"),
        };
        // the words of an atomic buffer are read by atomic loads, as the buffer's integer type
        let atomic = self.resources.atomic.get(&resource).copied();
        let index = self.uint(self.arg(args, 2)?)?;
        let Some((_, ty)) = inst.result else {
            return self.refuse("a buffer load with no result");
        };
        let dxil_reader::module::Type::Struct { elements, .. } = self.type_at(ty)?.clone() else {
            return self.refuse("a buffer load whose result is not a struct");
        };
        let Some(&first) = elements.first() else {
            return self.refuse("a buffer load whose result struct has no lanes");
        };
        let lane_kind = self.kind_of_type(first)?;
        let row = match kind {
            BufferKind::Typed(_) => {
                let length = Val {
                    expr: self.b.app(E::ArrayLength(words_ptr)),
                    kind: Kind::U,
                    width: Width::One,
                };
                let inside = self.b.binary(Bo::Less, index, length);
                let zero = self.b.lit_u(0);
                let safe = self.b.select(inside, index, zero);
                let element = self.b.app(E::Access {
                    base: words_ptr,
                    index: safe.expr,
                });
                let value = match atomic {
                    // four atomic words an element
                    Some(scalar) => {
                        let mut words = Vec::new();
                        for k in 0..4u32 {
                            let word = self.b.app(E::AccessIndex {
                                base: element,
                                index: k,
                            });
                            let loaded = self.b.load(word, kind_of_atomic(scalar), Width::One);
                            words.push(self.b.bitcast_to(loaded, Kind::U));
                        }
                        self.b.compose(&words)
                    }
                    None => self.b.load(element, Kind::U, Width::Four),
                };
                let none = {
                    let z = self.b.lit_u(0);
                    self.b.splat(z, Width::Four)
                };
                self.b.select(inside, value, none)
            }
            BufferKind::Raw | BufferKind::Structured { .. } => {
                // the word of the first lane: a raw buffer's index is a byte offset, a structured one's an element and
                // a byte offset within it
                let two = self.b.lit_u(2);
                let word = match kind {
                    BufferKind::Structured { stride } => {
                        let offset = self.uint(self.arg(args, 3)?)?;
                        let st = self.b.lit_u(stride);
                        let base = self.b.binary(Bo::Multiply, index, st);
                        let byte = self.b.binary(Bo::Add, base, offset);
                        self.b.binary(Bo::ShiftRight, byte, two)
                    }
                    _ => self.b.binary(Bo::ShiftRight, index, two),
                };
                let mut lanes = Vec::new();
                for lane in 0..4u32 {
                    let k = self.b.lit_u(lane);
                    let at = self.b.binary(Bo::Add, word, k);
                    let length = Val {
                        expr: self.b.app(E::ArrayLength(words_ptr)),
                        kind: Kind::U,
                        width: Width::One,
                    };
                    let inside = self.b.binary(Bo::Less, at, length);
                    let zero = self.b.lit_u(0);
                    let safe = self.b.select(inside, at, zero);
                    let element = self.b.app(E::Access {
                        base: words_ptr,
                        index: safe.expr,
                    });
                    let value = match atomic {
                        Some(scalar) => {
                            let loaded = self.b.load(element, kind_of_atomic(scalar), Width::One);
                            self.b.bitcast_to(loaded, Kind::U)
                        }
                        None => self.b.load(element, Kind::U, Width::One),
                    };
                    lanes.push(self.b.select(inside, value, zero));
                }
                self.b.compose(&lanes)
            }
        };
        let mut items = Vec::new();
        for lane in 0..4 {
            let x = self.b.lane(row, lane);
            items.push(Lv::Scalar(self.b.bitcast_to(x, lane_kind)));
        }
        items.push(Lv::Scalar(self.b.lit_u(0)));
        let _ = ScalarType::Float;
        self.define_result(inst.result, Lv::Agg(items))
    }
}