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
extern crate glsl_to_spirv;
extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate quote;
use proc_macro::TokenStream;
use std::fs::File;
use std::io::Read;
use std::path::Path;
#[proc_macro_derive(GLSLEmbedImpl, attributes(src, path, ty))]
pub fn macro_impl(input: TokenStream) -> TokenStream {
let source = input.to_string();
let ast = syn::parse_macro_input(&source).ok().expect("Invalid shader macro arguments - you must specify a single string literal.");
let mut source_string = None;
let mut path_string = None;
let mut ty_string = None;
for attr in &ast.attrs {
match attr.value {
syn::MetaItem::NameValue(ref ident, syn::Lit::Str(ref value, _)) => {
if ident == "src" {
source_string = Some(value.clone());
} else if ident == "path" {
path_string = Some(value.clone());
} else if ident == "ty" {
ty_string = Some(value.clone());
} else {
panic!("Invalid shader macro arguments - you must specify a single string literal.")
}
}
syn::MetaItem::List(ref ident, _) if ident == "allow" => {}
_ => panic!("Invalid shader macro arguments - you must specify a single string literal.")
}
};
let source_string = if let Some(source_string) = source_string {
source_string
} else {
let root = std::env::var("CARGO_MANIFEST_DIR").unwrap_or(".".into());
let path = path_string.unwrap();
let full_path = Path::new(&root).join(&path);
let mut s = String::new();
File::open(full_path)
.and_then(|mut file| file.read_to_string(&mut s))
.expect(&format!("Error reading shader source file: {:?}", path));
s
};
let ty = match &*ty_string.unwrap() {
"vs" => glsl_to_spirv::ShaderType::Vertex,
"fs" => glsl_to_spirv::ShaderType::Fragment,
"gs" => glsl_to_spirv::ShaderType::Geometry,
"tcs" => glsl_to_spirv::ShaderType::TessellationControl,
"tes" => glsl_to_spirv::ShaderType::TessellationEvaluation,
"cs" => glsl_to_spirv::ShaderType::Compute,
_ => panic!()
};
let mut spirv_file: File = match glsl_to_spirv::compile(&source_string, ty) {
Ok(compiled) => compiled,
Err(message) => panic!("{}\nfailed to compile shader", message),
};
let mut spirv_data = Vec::new();
spirv_file.read_to_end(&mut spirv_data).unwrap();
let count = spirv_data.len();
let expanded = quote! {
static DATA: [u8; #count] = #spirv_data;
};
expanded.parse().unwrap()
}