      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

    // Copyright (c) 2016 The vulkano developers
    // Licensed under the Apache License, Version 2.0
    // <LICENSE-APACHE or
    // http://www.apache.org/licenses/LICENSE-2.0> or the MIT
    // license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
    // at your option. All files in the project carrying such
    // notice may not be copied, modified, or distributed except
    // according to those terms.

    use crate::parse::{Instruction, Spirv};
    use crate::enums::Decoration;

    /// Returns the vulkano `Format` and number of occupied locations from an id.
    ///
    /// If `ignore_first_array` is true, the function expects the outermost instruction to be
    /// `OpTypeArray`. If it's the case, the OpTypeArray will be ignored. If not, the function will
    /// panic.
    pub fn format_from_id(doc: &Spirv, searched: u32, ignore_first_array: bool) -> (String, usize) {
        for instruction in doc.instructions.iter() {
            match instruction {
                &Instruction::TypeInt {
                    result_id,
                    width,
                    signedness,
                } if result_id == searched => {
                    assert!(!ignore_first_array);
                    let format = match (width, signedness) {
                        (8, true) => "R8Sint",
                        (8, false) => "R8Uint",
                        (16, true) => "R16Sint",
                        (16, false) => "R16Uint",
                        (32, true) => "R32Sint",
                        (32, false) => "R32Uint",
                        (64, true) => "R64Sint",
                        (64, false) => "R64Uint",
                        _ => panic!(),
                    };
                    return (format.to_string(), 1);
                },
                &Instruction::TypeFloat { result_id, width } if result_id == searched => {
                    assert!(!ignore_first_array);
                    let format = match width {
                        32 => "R32Sfloat",
                        64 => "R64Sfloat",
                        _ => panic!(),
                    };
                    return (format.to_string(), 1);
                }
                &Instruction::TypeVector {
                    result_id,
                    component_id,
                    count,
                } if result_id == searched => {
                    assert!(!ignore_first_array);
                    let (format, sz) = format_from_id(doc, component_id, false);
                    assert!(format.starts_with("R32"));
                    assert_eq!(sz, 1);
                    let format = match count {
                        1 => format,
                        2 => format!("R32G32{}", &format[3 ..]),
                        3 => format!("R32G32B32{}", &format[3 ..]),
                        4 => format!("R32G32B32A32{}", &format[3 ..]),
                        _ => panic!("Found vector type with more than 4 elements")
                    };
                    return (format, sz);
                },
                &Instruction::TypeMatrix {
                    result_id,
                    column_type_id,
                    column_count,
                } if result_id == searched => {
                    assert!(!ignore_first_array);
                    let (format, sz) = format_from_id(doc, column_type_id, false);
                    return (format, sz * column_count as usize);
                },
                &Instruction::TypeArray {
                    result_id,
                    type_id,
                    length_id,
                } if result_id == searched => {
                    if ignore_first_array {
                        return format_from_id(doc, type_id, false);
                    }

                    let (format, sz) = format_from_id(doc, type_id, false);
                    let len = doc.instructions
                        .iter()
                        .filter_map(|e| match e {
                            &Instruction::Constant { result_id, ref data, .. }
                                if result_id == length_id => Some(data.clone()),
                            _ => None,
                        })
                        .next()
                        .expect("failed to find array length");
                    let len = len.iter().rev().fold(0u64, |a, &b| (a << 32) | b as u64);
                    return (format, sz * len as usize);
                },
                &Instruction::TypePointer { result_id, type_id, .. }
                    if result_id == searched => {
                    return format_from_id(doc, type_id, ignore_first_array);
                },
                _ => (),
            }
        }

        panic!("Type #{} not found or invalid", searched)
    }

    pub fn name_from_id(doc: &Spirv, searched: u32) -> String {
        for instruction in &doc.instructions {
            if let &Instruction::Name { target_id, ref name } = instruction {
                if target_id == searched {
                    return name.clone()
                }
            }
        }

        String::from("__unnamed")
    }

    pub fn member_name_from_id(doc: &Spirv, searched: u32, searched_member: u32) -> String {
        for instruction in &doc.instructions {
            if let &Instruction::MemberName { target_id, member, ref name } = instruction {
                if target_id == searched && member == searched_member {
                    return name.clone()
                }
            }
        }

        String::from("__unnamed")
    }

    /// Returns true if a `BuiltIn` decorator is applied on an id.
    pub fn is_builtin(doc: &Spirv, id: u32) -> bool {
        if doc.get_decoration_params(id, Decoration::DecorationBuiltIn).is_some() {
            return true
        }
        if doc.get_member_decoration_builtin_params(id).is_some() {
            return true
        }

        for instruction in &doc.instructions {
            match *instruction {
                Instruction::Variable {
                    result_type_id,
                    result_id,
                    ..
                } if result_id == id => {
                    return is_builtin(doc, result_type_id);
                }
                Instruction::TypeArray { result_id, type_id, .. } if result_id == id => {
                    return is_builtin(doc, type_id);
                }
                Instruction::TypeRuntimeArray { result_id, type_id } if result_id == id => {
                    return is_builtin(doc, type_id);
                }
                Instruction::TypeStruct {
                    result_id,
                    ref member_types,
                } if result_id == id => {
                    for &mem in member_types {
                        if is_builtin(doc, mem) {
                            return true;
                        }
                    }
                }
                Instruction::TypePointer { result_id, type_id, .. } if result_id == id => {
                    return is_builtin(doc, type_id);
                }
                _ => ()
            }
        }

        false
    }

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)