librashader-reflect 0.11.4

RetroArch shaders for all.
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
use rspirv::dr::{Builder, Instruction, Module, Operand};
use spirv::{Decoration, Op, StorageClass};

/// Split aggregate (array) shader I/O variables into N scalar/vector
/// variables at consecutive locations.
///
/// WGSL forbids non-scalar/non-vector types at the user-defined I/O
/// interface between stages, so naga rejects modules that pass arrays
/// across the vertex/fragment boundary. SPIR-V emitted from GLSL `out
/// float arr[N]` declares a single Output (or Input) variable of array
/// type with one Location, occupying `N` consecutive locations. This
/// pass rewrites each such variable into `N` independent location
/// variables and inserts scatter/gather copies in `main()` so the
/// shader body, which still indexes the original array, continues to
/// work unchanged.
///
/// The original array variable is demoted to `Private` storage so that
/// existing `OpAccessChain`/`OpLoad`/`OpStore` instructions referencing
/// it remain valid (their result-type pointer classes are updated to
/// `Private`).
///
/// `inject_scatter`/`inject_gather` then adds loads/stores to read/write from
/// the original array variable into the I/O variables.
pub struct SplitArrayIo<'a> {
    pub builder: &'a mut Builder,
    pub io_class: StorageClass,
}

#[derive(Debug, Clone)]
struct ArrayIoVar {
    var_id: spirv::Word,
    array_type_id: spirv::Word,
    element_type_id: spirv::Word,
    array_length: u32,
    location: u32,
    /// Decorations that should be replicated on each scalar variable
    /// (interpolation, sampling, etc. — everything except Location/Component).
    replicated_decorations: Vec<Instruction>,
}

impl<'a> SplitArrayIo<'a> {
    pub fn new(builder: &'a mut Builder, io_class: StorageClass) -> Self {
        debug_assert!(matches!(
            io_class,
            StorageClass::Input | StorageClass::Output
        ));
        Self { builder, io_class }
    }

    pub fn do_pass(&mut self) {
        let arrays = self.collect_array_io_vars();
        if arrays.is_empty() {
            return;
        }

        for arr in arrays {
            self.split_one(&arr);
        }

        self.put_variables_to_end();
    }

    fn collect_array_io_vars(&self) -> Vec<ArrayIoVar> {
        let module = self.builder.module_ref();
        let mut result = Vec::new();

        for global in module.types_global_values.iter() {
            if global.class.opcode != Op::Variable {
                continue;
            }
            if global.operands.first() != Some(&Operand::StorageClass(self.io_class)) {
                continue;
            }

            let Some(var_id) = global.result_id else {
                continue;
            };
            let Some(ptr_type_id) = global.result_type else {
                continue;
            };

            let Some(ptr_type) = Self::find_global(module, ptr_type_id) else {
                continue;
            };
            if ptr_type.class.opcode != Op::TypePointer {
                continue;
            }
            let Some(&Operand::IdRef(pointee_type_id)) = ptr_type.operands.get(1) else {
                continue;
            };

            let Some(array_type) = Self::find_global(module, pointee_type_id) else {
                continue;
            };
            if array_type.class.opcode != Op::TypeArray {
                continue;
            }
            let Some(&Operand::IdRef(element_type_id)) = array_type.operands.get(0) else {
                continue;
            };
            let Some(&Operand::IdRef(length_id)) = array_type.operands.get(1) else {
                continue;
            };
            let Some(array_length) = Self::resolve_uint_constant(module, length_id) else {
                continue;
            };
            if array_length == 0 {
                continue;
            }

            let mut location = None;
            let mut replicated_decorations = Vec::new();
            for ann in module.annotations.iter() {
                if ann.class.opcode != Op::Decorate {
                    continue;
                }
                let Some(&Operand::IdRef(target)) = ann.operands.first() else {
                    continue;
                };
                if target != var_id {
                    continue;
                }
                let Some(Operand::Decoration(deco)) = ann.operands.get(1) else {
                    continue;
                };
                match deco {
                    Decoration::Location => {
                        if let Some(&Operand::LiteralBit32(loc)) = ann.operands.get(2) {
                            location = Some(loc);
                        }
                    }
                    // These propagate to each scalar variable.
                    Decoration::Flat
                    | Decoration::NoPerspective
                    | Decoration::Centroid
                    | Decoration::Sample
                    | Decoration::Invariant
                    | Decoration::Patch => {
                        replicated_decorations.push(ann.clone());
                    }
                    _ => {}
                }
            }
            let Some(location) = location else {
                continue;
            };

            result.push(ArrayIoVar {
                var_id,
                array_type_id: pointee_type_id,
                element_type_id,
                array_length,
                location,
                replicated_decorations,
            });
        }

        result
    }

    fn split_one(&mut self, arr: &ArrayIoVar) {
        // Pre-create all new IO pointer / variable types up front so subsequent
        // mutations of types_global_values don't invalidate the iteration we'd
        // need to do otherwise.
        let scalar_io_ptr = self
            .builder
            .type_pointer(None, self.io_class, arr.element_type_id);

        let mut scalar_vars = Vec::with_capacity(arr.array_length as usize);
        for i in 0..arr.array_length {
            let var = self
                .builder
                .variable(scalar_io_ptr, None, self.io_class, None);
            self.builder.decorate(
                var,
                Decoration::Location,
                std::iter::once(Operand::LiteralBit32(arr.location + i)),
            );
            for deco in &arr.replicated_decorations {
                let Operand::Decoration(deco_type) = deco.operands[1] else {
                    continue;
                };
                self.builder
                    .decorate(var, deco_type, deco.operands[2..].iter().cloned());
            }
            scalar_vars.push(var);
        }

        // Demote the original array variable to Private and prepare pointer
        // types for the rewritten accesses.
        let private_array_ptr =
            self.builder
                .type_pointer(None, StorageClass::Private, arr.array_type_id);
        let private_element_ptr =
            self.builder
                .type_pointer(None, StorageClass::Private, arr.element_type_id);
        let uint_type = self.builder.type_int(32, 0);
        let index_constants: Vec<spirv::Word> = (0..arr.array_length)
            .map(|i| self.builder.constant_bit32(uint_type, i))
            .collect();

        // Update the OpVariable and all access chains that reference it.
        for global in self.builder.module_mut().types_global_values.iter_mut() {
            if global.class.opcode == Op::Variable && global.result_id == Some(arr.var_id) {
                global.result_type = Some(private_array_ptr);
                if let Some(op) = global.operands.first_mut() {
                    *op = Operand::StorageClass(StorageClass::Private);
                }
                break;
            }
        }

        for function in self.builder.module_mut().functions.iter_mut() {
            for block in function.blocks.iter_mut() {
                for instr in block.instructions.iter_mut() {
                    if !matches!(
                        instr.class.opcode,
                        Op::AccessChain | Op::InBoundsAccessChain | Op::PtrAccessChain
                    ) {
                        continue;
                    }
                    let Some(&Operand::IdRef(base)) = instr.operands.first() else {
                        continue;
                    };
                    if base != arr.var_id {
                        continue;
                    }
                    instr.result_type = Some(private_element_ptr);
                }
            }
        }

        // Strip I/O-only decorations from the demoted variable. They are
        // forbidden on Private variables and would fail validation.
        let var_id = arr.var_id;
        self.builder.module_mut().annotations.retain(|ann| {
            if ann.class.opcode != Op::Decorate {
                return true;
            }
            let Some(&Operand::IdRef(target)) = ann.operands.first() else {
                return true;
            };
            if target != var_id {
                return true;
            }
            let Some(Operand::Decoration(deco)) = ann.operands.get(1) else {
                return true;
            };
            !matches!(
                deco,
                Decoration::Location
                    | Decoration::Component
                    | Decoration::Index
                    | Decoration::NoPerspective
                    | Decoration::Flat
                    | Decoration::Centroid
                    | Decoration::Sample
                    | Decoration::Patch
                    | Decoration::Invariant
                    | Decoration::Stream
                    | Decoration::XfbBuffer
                    | Decoration::XfbStride
            )
        });

        // Replace the array var with the new scalar vars in every entry-point
        // interface list. OpEntryPoint operands: [ExecutionModel, EntryPointId,
        // Name, Interface...]; only IdRef entries beyond Name are interface
        // variables.
        for entry_point in self.builder.module_mut().entry_points.iter_mut() {
            let mut rebuilt: Vec<Operand> = Vec::with_capacity(entry_point.operands.len());
            for (idx, op) in entry_point.operands.iter().enumerate() {
                if idx >= 3 {
                    if let Operand::IdRef(id) = op {
                        if *id == var_id {
                            for &sv in &scalar_vars {
                                rebuilt.push(Operand::IdRef(sv));
                            }
                            continue;
                        }
                    }
                }
                rebuilt.push(op.clone());
            }
            entry_point.operands = rebuilt;
        }

        // Find main entry point(s) referencing this variable and inject
        // scatter (Output) or gather (Input) code.
        let main_ids = self.entry_point_function_ids();
        for main_id in main_ids {
            match self.io_class {
                StorageClass::Output => self.inject_scatter(
                    main_id,
                    arr,
                    &scalar_vars,
                    &index_constants,
                    private_element_ptr,
                ),
                StorageClass::Input => self.inject_gather(
                    main_id,
                    arr,
                    &scalar_vars,
                    &index_constants,
                    private_element_ptr,
                ),
                _ => {}
            }
        }
    }

    fn inject_scatter(
        &mut self,
        main_id: spirv::Word,
        arr: &ArrayIoVar,
        scalar_vars: &[spirv::Word],
        index_constants: &[spirv::Word],
        private_element_ptr: spirv::Word,
    ) {
        // Pre-allocate all IDs so we don't need a `&mut self` mid-iteration.
        let ids: Vec<(spirv::Word, spirv::Word)> = (0..arr.array_length)
            .map(|_| (self.builder.id(), self.builder.id()))
            .collect();

        for function in self.builder.module_mut().functions.iter_mut() {
            if function.def.as_ref().and_then(|d| d.result_id) != Some(main_id) {
                continue;
            }
            for block in function.blocks.iter_mut() {
                let Some(term_idx) = block.instructions.iter().position(|i| {
                    matches!(
                        i.class.opcode,
                        Op::Return | Op::ReturnValue | Op::Kill | Op::Unreachable
                    )
                }) else {
                    continue;
                };
                let mut inserted = Vec::with_capacity(arr.array_length as usize * 3);
                for i in 0..arr.array_length as usize {
                    let (ptr_id, val_id) = ids[i];
                    inserted.push(Instruction::new(
                        Op::AccessChain,
                        Some(private_element_ptr),
                        Some(ptr_id),
                        vec![
                            Operand::IdRef(arr.var_id),
                            Operand::IdRef(index_constants[i]),
                        ],
                    ));
                    inserted.push(Instruction::new(
                        Op::Load,
                        Some(arr.element_type_id),
                        Some(val_id),
                        vec![Operand::IdRef(ptr_id)],
                    ));
                    inserted.push(Instruction::new(
                        Op::Store,
                        None,
                        None,
                        vec![Operand::IdRef(scalar_vars[i]), Operand::IdRef(val_id)],
                    ));
                }
                let tail = block.instructions.split_off(term_idx);
                block.instructions.extend(inserted);
                block.instructions.extend(tail);
                // Only inject into the first return-bearing block we find;
                // subsequent reuse of the same pre-allocated IDs would create
                // duplicate definitions.
                break;
            }
            break;
        }
    }

    fn inject_gather(
        &mut self,
        main_id: spirv::Word,
        arr: &ArrayIoVar,
        scalar_vars: &[spirv::Word],
        index_constants: &[spirv::Word],
        private_element_ptr: spirv::Word,
    ) {
        let ids: Vec<(spirv::Word, spirv::Word)> = (0..arr.array_length)
            .map(|_| (self.builder.id(), self.builder.id()))
            .collect();

        for function in self.builder.module_mut().functions.iter_mut() {
            if function.def.as_ref().and_then(|d| d.result_id) != Some(main_id) {
                continue;
            }
            let Some(block) = function.blocks.first_mut() else {
                continue;
            };
            // Skip the leading OpVariable instructions (which must come first
            // in the entry block per SPIR-V layout rules) before inserting.
            let insert_at = block
                .instructions
                .iter()
                .position(|i| i.class.opcode != Op::Variable)
                .unwrap_or(block.instructions.len());
            let mut inserted = Vec::with_capacity(arr.array_length as usize * 3);
            for i in 0..arr.array_length as usize {
                let (val_id, ptr_id) = ids[i];
                inserted.push(Instruction::new(
                    Op::Load,
                    Some(arr.element_type_id),
                    Some(val_id),
                    vec![Operand::IdRef(scalar_vars[i])],
                ));
                inserted.push(Instruction::new(
                    Op::AccessChain,
                    Some(private_element_ptr),
                    Some(ptr_id),
                    vec![
                        Operand::IdRef(arr.var_id),
                        Operand::IdRef(index_constants[i]),
                    ],
                ));
                inserted.push(Instruction::new(
                    Op::Store,
                    None,
                    None,
                    vec![Operand::IdRef(ptr_id), Operand::IdRef(val_id)],
                ));
            }
            let tail = block.instructions.split_off(insert_at);
            block.instructions.extend(inserted);
            block.instructions.extend(tail);
            break;
        }
    }

    fn entry_point_function_ids(&self) -> Vec<spirv::Word> {
        self.builder
            .module_ref()
            .entry_points
            .iter()
            .filter_map(|ep| match ep.operands.get(1) {
                Some(&Operand::IdRef(id)) => Some(id),
                _ => None,
            })
            .collect()
    }

    fn find_global(module: &Module, id: spirv::Word) -> Option<&Instruction> {
        module
            .types_global_values
            .iter()
            .find(|i| i.result_id == Some(id))
    }

    fn resolve_uint_constant(module: &Module, id: spirv::Word) -> Option<u32> {
        let inst = Self::find_global(module, id)?;
        if inst.class.opcode != Op::Constant {
            return None;
        }
        match inst.operands.first()? {
            &Operand::LiteralBit32(v) => Some(v),
            _ => None,
        }
    }

    /// Move every OpVariable after every type/constant declaration so that
    /// freshly created Private pointer types precede the demoted variable
    /// that now uses them. SPIR-V requires every operand to be declared
    /// before its use within `types_global_values`.
    fn put_variables_to_end(&mut self) {
        let mut vars = Vec::new();
        self.builder
            .module_mut()
            .types_global_values
            .retain(|instr| {
                if instr.class.opcode == Op::Variable {
                    vars.push(instr.clone());
                    return false;
                }
                true
            });
        self.builder.module_mut().types_global_values.extend(vars);
    }
}