nuance 0.3.2

A tool to run your shaders on the gpu. Also a good demo application for wgpu-rs.
Documentation
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Extract information from glsl source and transpiles it to valid glsl source code.

use core::panic;
use std::borrow::Borrow;

use anyhow::{anyhow, Result};
use glsl_lang::ast::{
    FunIdentifier, PreprocessorDefine, TypeQualifier, TypeSpecifier, TypeSpecifierNonArray,
};
use glsl_lang::{
    ast::{
        Block, Expr, Identifier, IdentifierData, LayoutQualifier, LayoutQualifierSpec, SmolStr,
        StructFieldSpecifier, TranslationUnit, TypeQualifierSpec,
    },
    parse::{Parsable, ParseOptions},
    transpiler::glsl::FormattingState,
    visitor::{HostMut, Visit, VisitorMut},
};
use log::{debug, error};
use mint::{Vector2, Vector3};

use crate::{ShaderMetadata, Slider};

impl VisitorMut for ShaderMetadata {
    fn visit_block(&mut self, block: &mut Block) -> Visit {
        if let Some(TypeQualifierSpec::Layout(layout)) = block.qualifier.qualifiers.first() {
            if let Some(LayoutQualifierSpec::Identifier(id, _)) = layout.ids.first() {
                if id.content.0 == "params" {
                    // We got the block we searched for
                    for field in block.fields.iter_mut() {
                        if let Ok(slider) = create_slider_from_field(field) {
                            self.sliders.push(slider);
                            convert_field(field);
                        } else {
                            panic!("Invalid field");
                        }
                    }
                    convert_params_block(block);
                }
            }
        }
        Visit::Parent
    }

    fn visit_preprocessor_define(&mut self, define: &mut PreprocessorDefine) -> Visit {
        if let PreprocessorDefine::ObjectLike { ident, .. } = define {
            if ident.content.0.as_str() == "NUANCE_STILL_IMAGE" {
                self.still_image = true;
            }
        }
        Visit::Parent
    }

    fn visit_expr(&mut self, expr: &mut Expr) -> Visit {
        if let Expr::Dot(expr2, ident1) = expr {
            if let Expr::Variable(ident0) = expr2.as_ref() {
                let slider_name = ident0.content.0.as_str();
                for slider in self.sliders.iter() {
                    match slider {
                        Slider::Float {
                            name,
                            min,
                            max,
                            default,
                            ..
                        } => {
                            if name == slider_name {
                                *expr = Expr::FloatConst(match ident1.content.0.as_str() {
                                    "max" => *max,
                                    "min" => *min,
                                    "init" => *default,
                                    // No . accessors on a float value
                                    other => panic!("No such property '{}' on float param", other),
                                });
                                return Visit::Parent;
                            }
                        }
                        Slider::Vec2 { name, default, .. } => {
                            if name == slider_name {
                                match ident1.content.0.as_str() {
                                    "init" => {
                                        *expr = Expr::FunCall(
                                            FunIdentifier::TypeSpecifier(TypeSpecifier {
                                                ty: TypeSpecifierNonArray::Vec2,
                                                array_specifier: None,
                                            }),
                                            default
                                                .as_ref()
                                                .iter()
                                                .map(|it| Expr::FloatConst(*it))
                                                .collect(),
                                        );
                                    }
                                    _ => {}
                                }
                                return Visit::Parent;
                            }
                        }
                        Slider::Vec3 { name, default, .. } => {
                            if name == slider_name {
                                match ident1.content.0.as_str() {
                                    "init" => {
                                        *expr = Expr::FunCall(
                                            FunIdentifier::TypeSpecifier(TypeSpecifier {
                                                ty: TypeSpecifierNonArray::Vec3,
                                                array_specifier: None,
                                            }),
                                            default
                                                .as_ref()
                                                .iter()
                                                .map(|it| Expr::FloatConst(*it))
                                                .collect(),
                                        );
                                    }
                                    _ => {}
                                }
                                return Visit::Parent;
                            }
                        }
                        _ => {}
                    }
                }
            }
        }
        Visit::Children
    }
}

pub fn create_slider_from_field(field: &StructFieldSpecifier) -> Result<Slider> {
    let name = field
        .identifiers
        .first()
        .unwrap()
        .ident
        .content
        .0
        .to_string();

    //debug!("{:#?}", field);

    match field.ty.ty {
        // To Slider::Float
        TypeSpecifierNonArray::Float => {
            let mut min = 0.0;
            let mut max = 1.0;
            let mut init = 0.0;

            if let Some(TypeQualifier { qualifiers }) = field.qualifier.as_ref() {
                if let TypeQualifierSpec::Layout(LayoutQualifier { ids }) =
                    qualifiers.first().unwrap()
                {
                    for qualifier in ids.iter() {
                        if let LayoutQualifierSpec::Identifier(id, param) = qualifier {
                            match id.content.0.as_str() {
                                "min" => {
                                    min = param.as_ref().unwrap().coerce_const();
                                }
                                "max" => {
                                    max = param.as_ref().unwrap().coerce_const();
                                }
                                "init" => {
                                    init = param.as_ref().unwrap().coerce_const();
                                }
                                other => {
                                    error!("Wrong slider setting : {}", other)
                                }
                            }
                        }
                    }
                }
            }
            return Ok(Slider::Float {
                name,
                min,
                max,
                value: init,
                default: init,
            });
        }
        TypeSpecifierNonArray::Vec2 => {
            let mut init: Vector2<f32> = Vector2::from([0.0, 0.0]);

            if let Some(TypeQualifier { qualifiers }) = field.qualifier.as_ref() {
                if let TypeQualifierSpec::Layout(LayoutQualifier { ids }) =
                    qualifiers.first().unwrap()
                {
                    for qualifier in ids.iter() {
                        if let LayoutQualifierSpec::Identifier(id, param) = qualifier {
                            match id.content.0.as_str() {
                                "init" => {
                                    if let Some(Expr::FunCall(
                                        FunIdentifier::TypeSpecifier(TypeSpecifier { ty, .. }),
                                        params,
                                    )) = param.as_deref()
                                    {
                                        if *ty == TypeSpecifierNonArray::Vec2 && params.len() == 2 {
                                            init = Vector2::from([
                                                params[0].coerce_const(),
                                                params[1].coerce_const(),
                                            ]);
                                            continue;
                                        }
                                        error!("Invalid initializer !");
                                    }
                                }
                                other => {
                                    error!("Unsupported setting : {}", other)
                                }
                            }
                        } else {
                            error!("Invalid qualifier shared");
                        }
                    }
                }
            }
            return Ok(Slider::Vec2 {
                name,
                value: init,
                default: init,
            });
        }
        // To Slider::Color if color layout qualifier is set
        TypeSpecifierNonArray::Vec3 => {
            let mut init: Vector3<f32> = Vector3::from([0.0, 0.0, 0.0]);
            let mut color = false;

            if let Some(TypeQualifier { qualifiers }) = field.qualifier.as_ref() {
                if let TypeQualifierSpec::Layout(LayoutQualifier { ids }) =
                    qualifiers.first().unwrap()
                {
                    for qualifier in ids.iter() {
                        if let LayoutQualifierSpec::Identifier(id, param) = qualifier {
                            match id.content.0.as_str() {
                                "color" => {
                                    color = true;
                                }
                                "init" => {
                                    if let Some(Expr::FunCall(
                                        FunIdentifier::TypeSpecifier(TypeSpecifier { ty, .. }),
                                        params,
                                    )) = param.as_deref()
                                    {
                                        if *ty == TypeSpecifierNonArray::Vec3 && params.len() == 3 {
                                            init = Vector3::from([
                                                params[0].coerce_const(),
                                                params[1].coerce_const(),
                                                params[2].coerce_const(),
                                            ]);
                                            continue;
                                        }
                                        error!("Invalid initializer !");
                                    }
                                }
                                other => {
                                    error!("Unsupported setting : {}", other)
                                }
                            }
                        } else {
                            error!("Invalid qualifier shared");
                        }
                    }
                }
            }
            return Ok(if color {
                Slider::Color {
                    name,
                    value: init,
                    default: init,
                }
            } else {
                Slider::Vec3 {
                    name,
                    value: init,
                    default: init,
                }
            });
        }
        TypeSpecifierNonArray::Bool => {
            let mut init = 0;

            if let Some(TypeQualifier { qualifiers }) = field.qualifier.as_ref() {
                if let TypeQualifierSpec::Layout(LayoutQualifier { ids }) =
                    qualifiers.first().unwrap()
                {
                    for qualifier in ids.iter() {
                        if let LayoutQualifierSpec::Identifier(id, param) = qualifier {
                            match id.content.0.as_str() {
                                "init" => match param.as_ref().unwrap().as_ref() {
                                    Expr::BoolConst(value) => {
                                        init = if *value { 1 } else { 0 };
                                    }
                                    _ => {
                                        error!("Expected boolean value");
                                    }
                                },
                                other => {
                                    error!("Wrong slider setting : {}", other);
                                }
                            }
                        }
                    }
                }
            }
            return Ok(Slider::Bool {
                name,
                value: init,
                default: init,
            });
        }
        _ => {}
    }
    Err(anyhow!("Invalid field in params block"))
}

/// Replace the layout(params) with a predefined layout(set=?, binding=?)
pub fn convert_params_block(block: &mut Block) {
    block.qualifier.qualifiers[0] = TypeQualifierSpec::Layout(LayoutQualifier {
        ids: vec![
            LayoutQualifierSpec::Identifier(
                Identifier {
                    content: IdentifierData(SmolStr::new("std140")),
                    span: None,
                },
                None,
            ),
            LayoutQualifierSpec::Identifier(
                Identifier {
                    content: IdentifierData(SmolStr::new("set")),
                    span: None,
                },
                Some(Box::new(Expr::IntConst(1))),
            ),
            LayoutQualifierSpec::Identifier(
                Identifier {
                    content: IdentifierData(SmolStr::new("binding")),
                    span: None,
                },
                Some(Box::new(Expr::IntConst(0))),
            ),
        ],
    });
}

/// Replace the layout(min=?, max=?) with nothing
pub fn convert_field(field: &mut StructFieldSpecifier) {
    field.qualifier = None;
}

pub fn extract(source: &str) -> Result<(ShaderMetadata, String)> {
    let mut metadata = ShaderMetadata::default();

    // The AST
    let (mut ast, _ctx) = TranslationUnit::parse_with_options(
        source,
        &ParseOptions {
            target_vulkan: true,
            source_id: 0,
            allow_rs_ident: false,
        }
        .build(),
    )?;

    // Extract some ast juice
    ast.visit_mut(&mut metadata);

    let mut transpiled = String::new();
    glsl_lang::transpiler::glsl::show_translation_unit(
        &mut transpiled,
        &ast,
        FormattingState::default(),
    )?;
    Ok((metadata, transpiled))
}

trait CoerceConst<T> {
    fn coerce_const(&self) -> T;
}

impl<T> CoerceConst<f32> for T
where
    T: Borrow<Expr>,
{
    fn coerce_const(&self) -> f32 {
        match self.borrow() {
            Expr::IntConst(value) => *value as f32,
            Expr::UIntConst(value) => *value as f32,
            Expr::FloatConst(value) => *value,
            Expr::DoubleConst(value) => *value as f32,
            _ => {
                panic!("Not a number constant")
            }
        }
    }
}

impl CoerceConst<f64> for Expr {
    fn coerce_const(&self) -> f64 {
        match self {
            Expr::IntConst(value) => *value as f64,
            Expr::UIntConst(value) => *value as f64,
            Expr::FloatConst(value) => *value as f64,
            Expr::DoubleConst(value) => *value,
            _ => {
                panic!("Not a number constant")
            }
        }
    }
}

impl CoerceConst<i32> for Expr {
    fn coerce_const(&self) -> i32 {
        match self {
            Expr::IntConst(value) => *value,
            Expr::UIntConst(value) => *value as i32,
            Expr::FloatConst(value) => *value as i32,
            Expr::DoubleConst(value) => *value as i32,
            _ => {
                panic!("Not a number constant")
            }
        }
    }
}

impl CoerceConst<u32> for Expr {
    fn coerce_const(&self) -> u32 {
        match self {
            Expr::IntConst(value) => *value as u32,
            Expr::UIntConst(value) => *value,
            Expr::FloatConst(value) => *value as u32,
            Expr::DoubleConst(value) => *value as u32,
            _ => {
                panic!("Not a number constant")
            }
        }
    }
}