spirv_webgpu_transform/
lib.rs

1//! See [https://github.com/davnotdev/spirv-webgpu-transform](https://github.com/davnotdev/spirv-webgpu-transform) for more details.
2//!
3
4use std::collections::{HashMap, HashSet};
5
6mod correction;
7mod splitcombined;
8mod splitdref;
9mod spv;
10mod util;
11
12#[cfg(test)]
13mod test;
14
15use spv::*;
16use util::*;
17
18pub use correction::*;
19pub use splitcombined::*;
20pub use splitdref::*;
21
22#[derive(Debug, Clone)]
23struct InstructionInsert {
24    previous_spv_idx: usize,
25    instruction: Vec<u32>,
26}
27
28#[derive(Debug, Clone)]
29struct WordInsert {
30    idx: usize,
31    word: u32,
32    head_idx: usize,
33}
34
35/// Helper to convert a `&[u8]` into a `Vec<u32>`.
36pub fn u8_slice_to_u32_vec(vec: &[u8]) -> Vec<u32> {
37    assert_eq!(
38        vec.len() % 4,
39        0,
40        "Input slice length must be a multiple of 4."
41    );
42
43    vec.chunks_exact(4)
44        .map(|chunk| {
45            (chunk[0] as u32)
46                | ((chunk[1] as u32) << 8)
47                | ((chunk[2] as u32) << 16)
48                | ((chunk[3] as u32) << 24)
49        })
50        .collect::<Vec<_>>()
51}
52
53/// Helper to convert a `&[u32]` into a `Vec<u8>`.
54pub fn u32_slice_to_u8_vec(vec: &[u32]) -> Vec<u8> {
55    vec.iter()
56        .flat_map(|&num| {
57            vec![
58                (num & 0xFF) as u8,
59                ((num >> 8) & 0xFF) as u8,
60                ((num >> 16) & 0xFF) as u8,
61                ((num >> 24) & 0xFF) as u8,
62            ]
63        })
64        .collect::<Vec<u8>>()
65}