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
use crate::*;
#[derive(Clone, Debug)]
pub struct NewParam {
pub sid: String,
pub annotate: Vec<Annotate>,
pub semantic: Option<String>,
pub modifier: Option<Modifier>,
pub ty: ParamType,
}
impl XNode for NewParam {
const NAME: &'static str = "newparam";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
let res = NewParam {
sid: element.attr("sid").ok_or("expecting sid attr")?.into(),
annotate: Annotate::parse_list(&mut it)?,
semantic: parse_opt("semantic", &mut it, parse_text)?,
modifier: parse_opt("modifier", &mut it, parse_elem)?,
ty: parse_one_many(&mut it, ParamType::parse)?,
};
finish(res, it)
}
}
#[derive(Clone, Debug)]
pub struct EffectSetParam {
pub ref_: String,
pub value: AnnotType,
}
impl XNode for EffectSetParam {
const NAME: &'static str = "setparam";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
let res = EffectSetParam {
ref_: element.attr("ref").ok_or("expected ref attr")?.into(),
value: parse_one_many(&mut it, AnnotType::parse)?,
};
finish(res, it)
}
}
#[derive(Clone, Debug)]
pub struct Annotate {
pub name: String,
pub value: AnnotType,
}
impl XNode for Annotate {
const NAME: &'static str = "annotate";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
let res = Annotate {
name: element.attr("name").ok_or("expecting name attr")?.into(),
value: parse_one_many(&mut it, AnnotType::parse)?,
};
finish(res, it)
}
}
mk_extensible_enum! {
pub enum Modifier {
Const = "CONST",
Uniform = "UNIFORM",
Varying = "VARYING",
Static = "STATIC",
Volatile = "VOLATILE",
Extern = "EXTERN",
Shared = "SHARED",
}
}
#[derive(Clone, Debug)]
pub enum AnnotType {
Bool(bool),
Bool2([bool; 2]),
Bool3([bool; 3]),
Bool4([bool; 4]),
Int(u32),
Int2([u32; 2]),
Int3(Box<[u32; 3]>),
Int4(Box<[u32; 4]>),
Float(f32),
Float2([f32; 2]),
Float3(Box<[f32; 3]>),
Float4(Box<[f32; 4]>),
Float2x2(Box<[f32; 2 * 2]>),
Float3x3(Box<[f32; 3 * 3]>),
Float4x4(Box<[f32; 4 * 4]>),
String(Box<str>),
}
impl AnnotType {
pub fn parse(e: &Element) -> Result<Option<Self>> {
Ok(Some(match e.name() {
"bool" => Self::Bool(parse_elem(e)?),
"bool2" => Self::Bool2(*parse_array_n(e)?),
"bool3" => Self::Bool3(*parse_array_n(e)?),
"bool4" => Self::Bool4(*parse_array_n(e)?),
"int" => Self::Int(parse_elem(e)?),
"int2" => Self::Int2(*parse_array_n(e)?),
"int3" => Self::Int3(parse_array_n(e)?),
"int4" => Self::Int4(parse_array_n(e)?),
"float" => Self::Float(parse_elem(e)?),
"float2" => Self::Float2(*parse_array_n(e)?),
"float3" => Self::Float3(parse_array_n(e)?),
"float4" => Self::Float4(parse_array_n(e)?),
"float2x2" => Self::Float2x2(parse_array_n(e)?),
"float3x3" => Self::Float3x3(parse_array_n(e)?),
"float4x4" => Self::Float4x4(parse_array_n(e)?),
"string" => Self::String(parse_text(e)?.into()),
_ => return Ok(None),
}))
}
}
#[derive(Clone, Debug)]
pub enum ParamType {
Float(f32),
Float2([f32; 2]),
Float3(Box<[f32; 3]>),
Float4(Box<[f32; 4]>),
Surface(Box<Surface>),
Sampler2D(Box<Sampler2D>),
Other(Box<Element>),
}
impl ParamType {
pub fn parse(e: &Element) -> Result<Option<Self>> {
Ok(Some(match e.name() {
"float" => Self::Float(parse_elem(e)?),
"float2" => Self::Float2(*parse_array_n(e)?),
"float3" => Self::Float3(parse_array_n(e)?),
"float4" => Self::Float4(parse_array_n(e)?),
Surface::NAME => Self::Surface(Surface::parse_box(e)?),
Sampler2D::NAME => Self::Sampler2D(Sampler2D::parse_box(e)?),
_ => Self::Other(Box::new(e.clone())),
}))
}
}