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
extern crate spirv_headers as spirv;

pub mod back;
pub mod front;
mod storage;

use crate::storage::{Storage, Token};

use std::{
    collections::{HashMap, HashSet},
    hash::BuildHasherDefault,
};


type FastHashMap<K, T> = HashMap<K, T, BuildHasherDefault<fxhash::FxHasher>>;
type FastHashSet<K> = HashSet<K, BuildHasherDefault<fxhash::FxHasher>>;

#[derive(Debug)]
pub struct Header {
    pub version: (u8, u8, u8),
    pub generator: u32,
}

pub type Bytes = u8;

#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum VectorSize {
    Bi = 2,
    Tri = 3,
    Quad = 4,
}

#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ScalarKind {
    Sint,
    Uint,
    Float,
}

#[derive(Debug)]
pub struct PointerDeclaration {
    pub name: Option<String>,
    pub base: Type,
    pub class: spirv::StorageClass,
}

#[derive(Debug)]
pub struct ArrayDeclaration {
    pub name: Option<String>,
    pub base: Type,
    pub length: u32,
}

#[derive(Debug)]
pub struct StructMember {
    pub name: Option<String>,
    pub binding: Option<Binding>,
    pub ty: Type,
}

#[derive(Debug)]
pub struct StructDeclaration {
    pub name: Option<String>,
    pub members: Vec<StructMember>,
}

bitflags::bitflags! {
    pub struct ImageFlags: u32 {
        const ARRAYED = 0x1;
        const MULTISAMPLED = 0x2;
        const READABLE = 0x4;
        const WRITABLE = 0x8;
    }
}

#[derive(Debug)]
pub struct ImageDeclaration {
    pub name: Option<String>,
    pub binding: Option<Binding>,
    pub ty: Type,
    pub dim: spirv::Dim,
    pub flags: ImageFlags,
}

#[derive(Debug)]
pub struct SamplerDeclaration {
    pub name: Option<String>,
    pub binding: Option<Binding>,
}

#[derive(Clone, Debug)]
pub enum Type {
    Void,
    Scalar { kind: ScalarKind, width: Bytes },
    Vector { size: VectorSize, kind: ScalarKind, width: Bytes },
    Matrix { columns: VectorSize, rows: VectorSize, kind: ScalarKind, width: Bytes },
    Pointer(Token<PointerDeclaration>),
    Array(Token<ArrayDeclaration>),
    Struct(Token<StructDeclaration>),
    Image(Token<ImageDeclaration>),
    Sampler(Token<SamplerDeclaration>),
}

#[derive(Clone, Debug)]
pub enum Constant {
    Sint(i64),
    Uint(u64),
    Float(f64),
}

#[derive(Clone, Debug)]
pub enum Binding {
    BuiltIn(spirv::BuiltIn),
    Location(spirv::Word),
    Descriptor { set: spirv::Word, binding: spirv::Word },
}

#[derive(Clone, Debug)]
pub struct GlobalVariable {
    pub name: Option<String>,
    pub class: spirv::StorageClass,
    pub binding: Option<Binding>,
    pub ty: Type,
}

#[derive(Clone, Debug)]
pub enum Expression {
    Access {
        base: Token<Expression>,
        index: Token<Expression>, //int
    },
    AccessIndex {
        base: Token<Expression>,
        index: u32,
    },
    Constant(Constant),
    Compose {
        ty: Type,
        components: Vec<Token<Expression>>,
    },
    FunctionParameter(u32),
    GlobalVariable(Token<GlobalVariable>),
    Load {
        pointer: Token<Expression>,
    },
    Mul(Token<Expression>, Token<Expression>),
    ImageSample {
        image: Token<Expression>,
        sampler: Token<Expression>,
        coordinate: Token<Expression>,
    },
}

pub type Block = Vec<Statement>;
#[derive(Debug)]
pub struct FallThrough;

#[derive(Debug)]
pub enum Statement {
    Block(Block),
    If {
        condition: Token<Expression>, //bool
        accept: Block,
        reject: Block,
    },
    Switch {
        selector: Token<Expression>, //int
        cases: FastHashMap<i32, (Block, Option<FallThrough>)>,
        default: Block,
    },
    Return {
        value: Option<Token<Expression>>,
    },
    Kill,
    Store {
        pointer: Token<Expression>,
        value: Token<Expression>,
    },
}

#[derive(Debug)]
pub struct Function {
    pub name: Option<String>,
    pub control: spirv::FunctionControl,
    pub parameter_types: Vec<Type>,
    pub return_type: Type,
    pub expressions: Storage<Expression>,
    pub body: Block,
}

#[derive(Debug)]
pub struct EntryPoint {
    pub exec_model: spirv::ExecutionModel,
    pub name: String,
    pub inputs: Vec<Token<GlobalVariable>>,
    pub outputs: Vec<Token<GlobalVariable>>,
    pub function: Token<Function>,
}

#[derive(Debug)]
pub struct ComplexTypes {
    pub pointers: Storage<PointerDeclaration>,
    pub arrays: Storage<ArrayDeclaration>,
    pub structs: Storage<StructDeclaration>,
    pub images: Storage<ImageDeclaration>,
    pub samplers: Storage<SamplerDeclaration>,
}

#[derive(Debug)]
pub struct Module {
    pub header: Header,
    pub complex_types: ComplexTypes,
    pub global_variables: Storage<GlobalVariable>,
    pub functions: Storage<Function>,
    pub entry_points: Vec<EntryPoint>,
}