playout 0.1.0

DSL for creating Vulkan pipeline layout and descriptor set layout.
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
use syn::{
    ext::IdentExt,
    parse::{Parse, ParseStream},
};

use crate::{
    Binding, DataStruct, DescriptorType, Field, ImageFormat, PlayoutModule, PrimitiveType,
    PrimitiveTypeSingle, PushConstantField, PushConstantsLayout, SetLayout, ShaderStages, Type,
};

impl Parse for DescriptorType {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let ty = input.parse::<syn::Ident>()?;
        let ty = match ty.to_string().as_str() {
            "StorageImage" => {
                let _left: syn::Token![<] = input.parse()?;
                let format: ImageFormat = input.parse()?;
                let _right: syn::Token![>] = input.parse()?;
                Self::StorageImage { format }
            }
            "SampledImage" => Self::SampledImage,
            "CombinedImageSampler" => Self::CombinedImageSampler,
            "AccelerationStructure" => Self::AccelerationStructure,
            "UniformBuffer" => {
                let _left: syn::Token![<] = input.parse()?;
                let ty: Type = input.parse()?;
                let _right: syn::Token![>] = input.parse()?;
                Self::UniformBuffer { ty }
            }
            "StorageBuffer" => {
                let _left: syn::Token![<] = input.parse()?;
                let ty: Type = input.parse()?;
                let _right: syn::Token![>] = input.parse()?;
                Self::StorageBuffer { ty }
            }
            "InlineUniformBlock" => {
                let _left: syn::Token![<] = input.parse()?;
                let ty: Type = input.parse()?;
                let _right: syn::Token![>] = input.parse()?;
                Self::InlineUniformBlock { ty }
            }
            _ => return Err(syn::Error::new(input.span(), "Invalid descriptor type")),
        };
        Ok(ty)
    }
}

impl Parse for ImageFormat {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let ident = input.parse::<syn::Ident>()?;
        let value = match ident.to_string().as_str() {
            "RGBA32_Float" => Self::RGBA32_Float,
            "RGBA16_Float" => Self::RGBA16_Float,
            "RG32_Float" => Self::RG32_Float,
            "RG16_Float" => Self::RG16_Float,
            "R11G11B10_Float" => Self::R11G11B10_Float,
            "R32_Float" => Self::R32_Float,
            "R16_Float" => Self::R16_Float,
            "RGBA16_UNorm" => Self::RGBA16_UNorm,
            "RGB10A2_UNorm" => Self::RGB10A2_UNorm,
            "RGBA8_UNorm" => Self::RGBA8_UNorm,
            "RG16_UNorm" => Self::RG16_UNorm,
            "RG8_UNorm" => Self::RG8_UNorm,
            "R16_UNorm" => Self::R16_UNorm,
            "R8_UNorm" => Self::R8_UNorm,
            "RGBA16_SNorm" => Self::RGBA16_SNorm,
            "RBGA8_SNorm" => Self::RBGA8_SNorm,
            "RG16_SNorm" => Self::RG16_SNorm,
            "RG8_SNorm" => Self::RG8_SNorm,
            "R16_SNorm" => Self::R16_SNorm,
            "R8_SNorm" => Self::R8_SNorm,
            "RGBA32_SInt" => Self::RGBA32_SInt,
            "RGBA16_SInt" => Self::RGBA16_SInt,
            "RGBA8_SInt" => Self::RGBA8_SInt,
            "RG32_SInt" => Self::RG32_SInt,
            "RG16_SInt" => Self::RG16_SInt,
            "RG8_SInt" => Self::RG8_SInt,
            "R32_SInt" => Self::R32_SInt,
            "R16_SInt" => Self::R16_SInt,
            "R8_SInt" => Self::R8_SInt,
            "RGBA32_UInt" => Self::RGBA32_UInt,
            "RGBA16_UInt" => Self::RGBA16_UInt,
            "RGBA8_UInt" => Self::RGBA8_UInt,
            "RG32_UInt" => Self::RG32_UInt,
            "RG16_UInt" => Self::RG16_UInt,
            "RG8_UInt" => Self::RG8_UInt,
            "R32_UInt" => Self::R32_UInt,
            "R16_UInt" => Self::R16_UInt,
            "R8_UInt" => Self::R8_UInt,
            _ => return Err(syn::Error::new(ident.span(), "Invalid image layout")),
        };
        Ok(value)
    }
}

impl Parse for ShaderStages {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let mut result = ShaderStages::empty();
        let stages: syn::punctuated::Punctuated<syn::Ident, syn::Token![|]> =
            input.parse_terminated(syn::Ident::parse, syn::Token![|])?;
        for stage in stages {
            let stage = match stage.to_string().as_str() {
                "VERTEX" => Self::VERTEX,
                "TELLESLATION_CONTROL" => Self::TELLESLATION_CONTROL,
                "TELLESLATION_EVALUATION" => Self::TELLESLATION_EVALUATION,
                "GEOMETRY" => Self::GEOMETRY,
                "FRAGMENT" => Self::FRAGMENT,
                "COMPUTE" => Self::COMPUTE,
                "RAYGEN" => Self::RAYGEN,
                "ANY_HIT" => Self::ANY_HIT,
                "CLOSEST_HIT" => Self::CLOSEST_HIT,
                "MISS" => Self::MISS,
                "INTERSECTION" => Self::INTERSECTION,
                "CALLABLE" => Self::CALLABLE,
                "TASK" => Self::TASK,
                "MESH" => Self::MESH,
                _ => return Err(syn::Error::new(stage.span(), "Invalid shader stage")),
            };
            result |= stage;
        }
        Ok(result)
    }
}

fn parse_shader_stage_attribute(input: ParseStream) -> syn::Result<ShaderStages> {
    let _pound: syn::Token![#] = input.parse()?;
    let _bang: syn::Token![!] = input.parse()?;

    let content;
    let _bracket: syn::token::Bracket = syn::bracketed!(content in input);

    let ident = content.parse::<syn::Ident>()?;
    if ident == "stage" {
        let inside;
        let _paren = syn::parenthesized!(inside in content);
        inside.parse::<ShaderStages>()
    } else {
        Err(syn::Error::new(ident.span(), "unknown attribute"))
    }
}

impl Parse for Binding {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let mut binding: u32 = 0;
        let mut layout: Option<String> = None;
        loop {
            if !input.peek(syn::Token![#]) {
                break;
            }
            let _pound: syn::Token![#] = input.parse()?;
            let content;
            let _bracket: syn::token::Bracket = syn::bracketed!(content in input);
            let ident = content.parse::<syn::Ident>()?;
            if ident == "binding" {
                let _eq = content.parse::<syn::Token![=]>()?;
                let binding_literal: syn::LitInt = content.parse::<syn::LitInt>()?;
                binding = binding_literal.base10_parse()?;
            } else if ident == "layout" {
                let _eq = content.parse::<syn::Token![=]>()?;
                let layout_ident: syn::Ident = content.parse::<syn::Ident>()?;
                layout = Some(layout_ident.to_string());
            } else {
                return Err(syn::Error::new(ident.span(), "unknown attribute"));
            }
        }
        let unnamed_field = input.peek(syn::Token![_]);
        let ident = if unnamed_field {
            input.call(syn::Ident::parse_any)
        } else {
            input.parse()
        }?;

        let _colon: syn::Token![:] = input.parse()?;

        let mut descriptor_count = 1;
        let descriptor_type: DescriptorType = if input.peek(syn::token::Bracket) {
            let content;
            let _bracket: syn::token::Bracket = syn::bracketed!(content in input);
            let ty: DescriptorType = content.parse()?;
            let _semicolon: syn::Token![;] = content.parse()?;
            let length = content.parse::<syn::LitInt>()?;
            descriptor_count = length.base10_parse()?;
            ty
        } else {
            input.parse()?
        };

        Ok(Binding {
            ident: ident.to_string(),
            binding,
            stages: ShaderStages::empty(),
            descriptor_type,
            descriptor_count,
            layout,
        })
    }
}

impl Parse for PushConstantsLayout {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let _struct = input.parse::<syn::Token![struct]>()?;
        let name = input.parse::<syn::Ident>()?;
        let lookahead = input.lookahead1();
        if !lookahead.peek(syn::token::Brace) {
            return Err(lookahead.error());
        }
        let content;
        let _paren: syn::token::Brace = syn::braced!(content in input);

        let mut current_shader_stages = ShaderStages::empty();
        let mut fields = Vec::new();
        loop {
            if content.peek(syn::Token![#]) && content.peek2(syn::Token![!]) {
                current_shader_stages = parse_shader_stage_attribute(&content)?;
                continue;
            }
            if content.is_empty() {
                break;
            }
            if current_shader_stages.is_empty() {
                return Err(syn::Error::new(
                    content.span(),
                    "No shader stages specified for this value",
                ));
            }
            fields.push(PushConstantField {
                field: content.parse()?,
                stages: current_shader_stages,
            });
            if content.is_empty() {
                break;
            }
            let _comma: syn::Token![,] = content.parse()?;
        }
        Ok(PushConstantsLayout {
            fields,
            name: name.to_string(),
        })
    }
}

impl Parse for SetLayout {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let _struct = input.parse::<syn::Token![struct]>()?;
        let name = input.parse::<syn::Ident>()?;
        let lookahead = input.lookahead1();
        if lookahead.peek(syn::token::Brace) {
            let content;
            let _paren: syn::token::Brace = syn::braced!(content in input);

            let mut current_shader_stages = ShaderStages::empty();
            let mut current_binding = 0;
            let mut bindings = Vec::new();
            loop {
                if content.peek(syn::Token![#]) && content.peek2(syn::Token![!]) {
                    current_shader_stages = parse_shader_stage_attribute(&content)?;
                    continue;
                }
                if content.is_empty() {
                    break;
                }
                if current_shader_stages.is_empty() {
                    return Err(syn::Error::new(
                        content.span(),
                        "No shader stages specified for this binding",
                    ));
                }
                let mut binding: Binding = content.parse()?;
                if binding.binding == 0 {
                    // If the binding number wasn't specified, use the automatically tracked number
                    binding.binding = current_binding;
                } else {
                    // If the binding number was specified, update the tracked binding number
                    current_binding = binding.binding;
                }
                binding.stages = current_shader_stages;
                bindings.push(binding);
                if content.is_empty() {
                    break;
                }
                let _comma: syn::Token![,] = content.parse()?;
                current_binding += 1;
            }
            Ok(SetLayout {
                bindings,
                name: name.to_string(),
                set: 0,
            })
        } else {
            Err(lookahead.error())
        }
    }
}

impl Parse for PlayoutModule {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let mut module = PlayoutModule::default();
        let mut current_set_id: u32 = 0;
        loop {
            if input.is_empty() {
                break;
            }

            let mut is_descriptor_set = None;
            let mut is_push_constants = false;
            if input.peek(syn::Token![#]) {
                let _pound: syn::Token![#] = input.parse()?;
                let content;
                let _bracket: syn::token::Bracket = syn::bracketed!(content in input);
                let ident = content.parse::<syn::Ident>()?;
                if ident == "set" {
                    if content.peek(syn::Token![=]) {
                        let _eq: syn::Token![=] = content.parse()?;
                        current_set_id = content.parse::<syn::LitInt>()?.base10_parse()?;
                    }
                    is_descriptor_set = Some(current_set_id);
                    current_set_id += 1;
                } else if ident == "push_constants" {
                    is_push_constants = true;
                } else {
                    return Err(syn::Error::new(ident.span(), "unknown attribute"));
                }
            }
            if input.is_empty() {
                break;
            }
            if input.peek(syn::Token![struct]) {
                if let Some(set_id) = is_descriptor_set {
                    let mut set_layout = input.parse::<SetLayout>()?;
                    set_layout.set = set_id;
                    module.descriptor_sets.push(set_layout);
                } else if is_push_constants {
                    module.push_constants = input.parse()?;
                } else {
                    let data_struct = input.parse::<DataStruct>()?;
                    module
                        .data_structs
                        .insert(data_struct.ident.clone(), data_struct);
                }
            }
        }
        Ok(module)
    }
}

impl Parse for PrimitiveTypeSingle {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let ident = input.parse::<syn::Ident>()?;
        match ident.to_string().as_str() {
            "u8" => Ok(Self::U8),
            "u16" => Ok(Self::U16),
            "u32" => Ok(Self::U32),
            "u64" => Ok(Self::U64),
            "i8" => Ok(Self::I8),
            "i16" => Ok(Self::I16),
            "i32" => Ok(Self::I32),
            "i64" => Ok(Self::I64),
            "bool" => Ok(Self::Bool),
            "f16" => Ok(Self::F16),
            "f32" => Ok(Self::F32),
            "f64" => Ok(Self::F64),
            _ => Err(syn::Error::new(ident.span(), "Invalid primitive type")),
        }
    }
}

impl Parse for PrimitiveType {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let old_input = input.fork();

        let ident = input.parse::<syn::Ident>()?;
        let mut result = match ident.to_string().as_str() {
            "Vec4" => Self::Vec {
                ty: PrimitiveTypeSingle::F32,
                length: 4,
            },
            "Vec3" => Self::Vec {
                ty: PrimitiveTypeSingle::F32,
                length: 3,
            },
            "Vec2" => Self::Vec {
                ty: PrimitiveTypeSingle::F32,
                length: 2,
            },
            "UVec4" => Self::Vec {
                ty: PrimitiveTypeSingle::U32,
                length: 4,
            }, // Shorthands
            "UVec3" => Self::Vec {
                ty: PrimitiveTypeSingle::U32,
                length: 3,
            },
            "UVec2" => Self::Vec {
                ty: PrimitiveTypeSingle::U32,
                length: 2,
            },
            "IVec4" => Self::Vec {
                ty: PrimitiveTypeSingle::I32,
                length: 4,
            },
            "IVec3" => Self::Vec {
                ty: PrimitiveTypeSingle::I32,
                length: 3,
            },
            "IVec2" => Self::Vec {
                ty: PrimitiveTypeSingle::I32,
                length: 2,
            },
            "Mat4" => Self::Mat {
                ty: PrimitiveTypeSingle::F32,
                rows: 4,
                columns: 4,
            },
            "Mat3" => Self::Mat {
                ty: PrimitiveTypeSingle::F32,
                rows: 3,
                columns: 3,
            },
            "Mat2" => Self::Mat {
                ty: PrimitiveTypeSingle::F32,
                rows: 2,
                columns: 2,
            },
            _ => Self::Single(old_input.parse()?),
        };
        if input.peek(syn::Token![<]) {
            // Attempt to resolve generic arg
            if let Self::Vec { ty, .. } = &mut result {
                if matches!(ty, PrimitiveTypeSingle::F32) {
                    let _left = input.parse::<syn::Token![<]>()?;
                    *ty = input.parse()?;
                    let _right = input.parse::<syn::Token![>]>()?;
                } else {
                    return Err(syn::Error::new(
                        ident.span(),
                        "Generic type cannot be used on this shorthand type",
                    ));
                }
            } else {
                return Err(syn::Error::new(
                    ident.span(),
                    "Generic type cannot be used on primitive types",
                ));
            }
        }
        Ok(result)
    }
}

impl Parse for Type {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        if input.peek(syn::token::Bracket) {
            // Array-like
            let content;
            let _bracket = syn::bracketed!(content in input);
            let ty: Type = content.parse()?;
            if content.peek(syn::Token![;]) {
                let _semicolon: syn::Token![;] = content.parse()?;
                let length = content.parse::<syn::LitInt>()?;
                Ok(Type::Array {
                    ty: Box::new(ty),
                    size: length.base10_parse()?,
                })
            } else {
                Ok(Type::Slice { ty: Box::new(ty) })
            }
        } else if let Ok(ty) = input.fork().parse::<PrimitiveType>() {
            input.parse::<PrimitiveType>()?;
            Ok(Type::Primitive(ty))
        } else {
            let path: syn::Path = input.parse()?;
            Ok(Type::Path(path.require_ident()?.to_string()))
        }
    }
}

impl Parse for Field {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let ident: syn::Ident = input.parse()?;
        let _colon: syn::Token![:] = input.parse()?;
        let ty: Type = input.parse()?;
        Ok(Self {
            ident: Some(ident.to_string()),
            ty,
        })
    }
}

impl Parse for DataStruct {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let _struct = input.parse::<syn::Token![struct]>()?;
        let name = input.parse::<syn::Ident>()?;
        let lookahead = input.lookahead1();
        if !lookahead.peek(syn::token::Brace) {
            return Err(lookahead.error());
        }
        let content;
        let _paren: syn::token::Brace = syn::braced!(content in input);
        let fields = content.parse_terminated(Field::parse, syn::Token![,])?;
        Ok(Self {
            ident: name.to_string(),
            fields: fields.into_iter().collect(),
        })
    }
}

impl TryFrom<&str> for PlayoutModule {
    type Error = syn::Error;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        let tokens = syn::parse_str::<PlayoutModule>(value)?;
        Ok(tokens)
    }
}