forked from vulkano-rs/vulkano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader.rs
326 lines (280 loc) · 12.4 KB
/
shader.rs
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
// Copyright (c) 2018 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 descriptor_sets::Descriptor;
use entry_point::{EntryPoint, InterfaceVariable};
use enums::{Capability, Decoration, StorageClass};
use parse::{Instruction, Spirv};
use spec_consts::{SpecializationConstant, SpecializationConstantKind};
use types::{extract_types, Type};
use std::collections::{BTreeSet, BTreeMap};
pub struct Shader {
/// The shader's parsed SPIR-V bytecode
pub spirv: Spirv,
/// The device capabilities required by this shader. Since we will
/// use these capabilities during codegen, and codegen should be
/// deterministic, we store them in a sorted BTreeSet instead of a HashSet.
pub capabilities: BTreeSet<Capability>,
/// Entry Points to the shader binary
pub entry_points: BTreeSet<EntryPoint>,
/// Specialization constants defined in the shader binary
pub specialization_constants: Vec<SpecializationConstant>,
/// A map of (result id, Decoration) tuples to the decoration content
pub decorations: BTreeMap<(u32, Decoration), Vec<u32>>,
/// A map of (result id, member number, Decoration) tuples to the decoration content
pub member_decorations: BTreeMap<(u32, u32, Decoration), Vec<u32>>,
/// Types described by the shader binary
pub types: BTreeMap<u32, Type>,
/// Shader descriptors
pub descriptors: Vec<Descriptor>,
/// Shader push constants
pub push_constants: Vec<Type>,
}
impl Shader {
pub fn _decoration(&self, target_id: u32, decoration: Decoration) -> Option<&[u32]> {
self.decorations.get(&(target_id, decoration)).map(Vec::as_slice)
}
pub fn _member_decoration(
&self,
target_id: u32,
member_number: u32,
decoration: Decoration,
) -> Option<&[u32]> {
self.member_decorations.get(&(target_id, member_number, decoration)).map(Vec::as_slice)
}
/// Build a shader from parsed SPIR-V bytecode
pub fn from_spirv(spirv: Spirv) -> Shader {
// TODO: These should probbaly all be hashmaps/sets, with sorting happening
// during codegen.
let mut capabilities = BTreeSet::new();
let mut names = BTreeMap::new();
let mut decorations = BTreeMap::new();
let mut member_decorations = BTreeMap::new();
let mut variables = BTreeMap::new();
let mut execution_modes = BTreeMap::new();
let mut descriptors = Vec::new();
let mut push_constants = Vec::new();
for instruction in &spirv.instructions {
match instruction {
&Instruction::Capability(capability) => {
capabilities.insert(capability);
}
&Instruction::Name{target_id, ref name} => {
if names.contains_key(&target_id) {
panic!("Duplicate name: {} {}", target_id, name);
}
names.insert(target_id, name.clone());
}
&Instruction::Decorate{target_id, decoration, ref params} => {
decorations.insert((target_id, decoration), params.clone());
}
&Instruction::MemberDecorate{target_id, member, decoration, ref params} => {
member_decorations.insert((target_id, member, decoration), params.clone());
}
&Instruction::Variable{result_type_id, result_id, storage_class, .. } => {
variables.insert(result_id, (result_type_id, storage_class));
}
&Instruction::ExecutionMode{target_id, mode, ..} => {
(*execution_modes.entry(target_id).or_insert_with(|| Vec::new())).push(mode);
}
_ => {}
};
}
let types = extract_types(&spirv.instructions, &names, &decorations)
.expect("failed to extract types");
for spirv_type in types.values() {
if let Type::Pointer{
storage_class: StorageClass::StorageClassPushConstant,
ref pointee_type,
} = *spirv_type {
push_constants.push(*pointee_type.clone());
}
}
for (&(variable_id, decoration), params) in &decorations {
if decoration != Decoration::DecorationDescriptorSet {
continue;
}
let descriptor_set = params[0];
let &(type_id, _) = variables.get(&variable_id).unwrap();
let pointer_type = types.get(&type_id).unwrap().clone();
let spirv_type = if let Type::Pointer{pointee_type, ..} = pointer_type {
*pointee_type
} else {
panic!();
};
let name = names.get(&variable_id).unwrap().clone();
let binding_point = decorations.get(&(variable_id, Decoration::DecorationBinding))
.unwrap()[0];
descriptors.push(Descriptor {
binding_point,
descriptor_set,
name,
spirv_type,
});
}
let entry_points = spirv.instructions.iter().flat_map(|instruction| {
if let &Instruction::EntryPoint{ref execution, id, ref name, ref interface} = instruction {
let mut inputs = Vec::new();
let mut outputs = Vec::new();
for interface_id in interface {
// TODO: ::is_builtin contains checks to see if the type of the interface, not
// just the interface itself had the builtin decorator. I'd like to
// understand those checks better.
if decorations.contains_key(&(*interface_id, Decoration::DecorationBuiltIn)) {
continue;
}
if let Some(&(type_id, storage_class)) = variables.get(interface_id) {
let destination = match storage_class {
StorageClass::StorageClassInput => &mut inputs,
StorageClass::StorageClassOutput => &mut outputs,
_ => continue,
};
let name = names
.get(&interface_id)
.expect("interface with no name").clone();
if name == "" {
// TODO: What does this mean?
continue;
}
destination.push(InterfaceVariable {
spirv_type: types
.get(&type_id)
.expect("interface with no type").clone(),
location: decorations
.get(&(*interface_id, Decoration::DecorationLocation))
.expect("interface with no location")[0],
name,
});
} else {
panic!("interface element without associated variable")
}
}
Some(EntryPoint {
execution_model: *execution,
execution_modes: execution_modes.remove(&id).unwrap_or_else(Vec::new).clone(),
id: id,
name: name.clone(),
inputs,
outputs,
})
} else {
None
}
}).collect();
let mut specialization_constants = Vec::new();
for instruction in &spirv.instructions {
let (result_type_id, result_id, kind) = match instruction {
&Instruction::SpecConstantTrue {
result_type_id,
result_id,
} => {
(result_type_id, result_id, SpecializationConstantKind::True)
},
&Instruction::SpecConstantFalse {
result_type_id,
result_id,
} => {
(result_type_id, result_id, SpecializationConstantKind::False)
},
&Instruction::SpecConstant {
result_type_id,
result_id,
ref data,
} => {
let kind = SpecializationConstantKind::Scalar{default_value: data.clone()};
(result_type_id, result_id, kind)
},
&Instruction::SpecConstantComposite {
result_type_id,
result_id,
ref data,
} => {
let kind = SpecializationConstantKind::Composite{default_value: data.clone()};
(result_type_id, result_id, kind)
},
_ => continue,
};
let constant_id = decorations
.remove(&(result_id, Decoration::DecorationSpecId))
.expect("no id for specialization constant")
[0];
let name = names.get(&result_id)
.expect("unnamed specialization constant")
.clone();
let spirv_type = types.get(&result_type_id)
.expect("Specialization constant with no type")
.clone();
let rust_type = spirv_type.rust_type()
.expect("Specialization constant with no rust type");
let rust_size = rust_type.size
.expect("Specialization constant with unsized rust type");
specialization_constants.push(SpecializationConstant {
constant_id,
kind,
name,
rust_size,
rust_type,
spirv_type,
});
}
// Sort specialization constants by their constant IDs
specialization_constants.sort_by_key(|c| c.constant_id);
Shader {
capabilities,
decorations,
descriptors,
entry_points,
member_decorations,
push_constants,
specialization_constants,
spirv,
types,
}
}
}
#[cfg(test)]
mod test {
use std::io::prelude::*;
use enums::ExecutionModel;
use glsl_to_spirv::{compile, ShaderType};
use parse::parse_spirv;
use super::*;
const PASSTHROUGH_VERTEX_SHADER: &str = include_str!("../data/passthrough-vertex-shader.glsl");
#[test]
fn simple_shader() {
let mut spirv_output_file = compile(PASSTHROUGH_VERTEX_SHADER, ShaderType::Vertex)
.expect("failed to compile data/passthrough-vertex-shader.glsl");
let mut spirv_bytes = Vec::new();
spirv_output_file.read_to_end(&mut spirv_bytes)
.expect("failed to read SPIR-V output file");
let spirv = parse_spirv(&spirv_bytes)
.expect("failed to parse SPIR-V from data/passthrough-vertex-shader.glsl");
let shader = Shader::from_spirv(spirv);
let capabilities = shader.capabilities.into_iter().collect::<Vec<_>>();
let entry_points = shader.entry_points.into_iter().collect::<Vec<_>>();
assert_eq!(capabilities, &[Capability::CapabilityShader]);
assert_eq!(entry_points, &[EntryPoint {
execution_model: ExecutionModel::ExecutionModelVertex,
execution_modes: vec![],
id: 4,
name: "main".to_string(),
inputs: vec![InterfaceVariable {
name: "position".to_string(),
location: 0,
spirv_type: Type::Pointer {
storage_class: StorageClass::StorageClassInput,
pointee_type: Box::new(Type::Vector{
element_count: 4,
element_type: Box::new(Type::Float{width: 32}),
}),
},
}],
outputs: vec![],
}]);
}
}