wgsl_bindgen 0.22.2

Type safe Rust bindings workflow for wgsl shaders in wgpu
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
mod raw_shader_bind_group;
mod single_bind_group;

use std::collections::BTreeMap;

use generate::quote_shader_stages;
use quote::{format_ident, quote};
use quote_gen::{demangle_and_fully_qualify_str, rust_type};
pub use raw_shader_bind_group::{
  get_bind_group_data_for_entry, RawShaderEntryBindGroups, RawShadersBindGroups,
};
use single_bind_group::SingleBindGroupBuilder;
pub use single_bind_group::SingleBindGroupData;
use smol_str::{SmolStr, ToSmolStr};

use crate::quote_gen::{
  RustSourceItem, RustSourceItemCategory, RustSourceItemPath, MOD_REFERENCE_ROOT,
};
use crate::wgsl::buffer_binding_type;
use crate::*;

/// A collection of bind groups that are common to all entrypoints.
struct CommonShaderBindGroups<'a> {
  containing_module: SmolStr,
  bind_group_data: BTreeMap<u32, SingleBindGroupData<'a>>,
}

#[derive(Clone, Eq, Copy, PartialEq, Ord, PartialOrd, Hash)]
pub enum ShaderBindGroupRefKind {
  Common,
  Entrypoint,
}

pub struct ShaderBindGroupRef<'a> {
  pub kind: ShaderBindGroupRefKind,
  pub data: SingleBindGroupData<'a>,
}

pub struct ShaderEntryBindGroups<'a> {
  pub containing_module: SmolStr,
  pub shader_stages: wgpu::ShaderStages,
  pub bind_group_ref: BTreeMap<u32, ShaderBindGroupRef<'a>>,
  pub original_bind_group: BTreeMap<u32, SingleBindGroupData<'a>>,
}

pub struct ReusableShaderBindGroups<'a> {
  common_bind_groups: FastIndexMap<SmolStr, CommonShaderBindGroups<'a>>,
  pub entrypoint_bindgroups: FastIndexMap<SmolStr, ShaderEntryBindGroups<'a>>,
}

impl<'a> ReusableShaderBindGroups<'a> {
  pub fn new() -> Self {
    Self {
      common_bind_groups: FastIndexMap::default(),
      entrypoint_bindgroups: FastIndexMap::default(),
    }
  }

  pub fn generate_bind_groups(&self, options: &WgslBindgenOption) -> Vec<RustSourceItem> {
    let mut items = Vec::new();
    // generate the common single bind groups.
    for common_bind_groups in self.common_bind_groups.values() {
      for (&group_no, group_data) in &common_bind_groups.bind_group_data {
        let builder = SingleBindGroupBuilder {
          containing_module: &common_bind_groups.containing_module,
          group_no,
          group_data,
          options,
        };
        items.push(builder.build());
      }
    }

    // generate the entrypoint single bind groups.
    for (_, shader) in &self.entrypoint_bindgroups {
      for (&group_no, group_ref) in &shader.bind_group_ref {
        // skip common bind groups.
        if group_ref.kind == ShaderBindGroupRefKind::Common {
          continue;
        }

        let builder = SingleBindGroupBuilder {
          containing_module: &shader.containing_module,
          group_no,
          group_data: &group_ref.data,
          options,
        };
        items.push(builder.build());
      }
    }

    // generate the bind groups module extras.
    for (_, shader) in &self.entrypoint_bindgroups {
      items.extend(generate_bind_groups_module_extras(
        &shader.containing_module,
        options,
        &shader.bind_group_ref,
        shader.shader_stages,
      ));
    }

    items
  }
}

fn generate_bind_groups_module_extras(
  invoking_entry_module: &str,
  options: &WgslBindgenOption,
  bind_group_data: &BTreeMap<u32, ShaderBindGroupRef<'_>>,
  shader_stages: wgpu::ShaderStages,
) -> Vec<RustSourceItem> {
  let bind_group_fields: Vec<_> = bind_group_data
    .iter()
    .map(|(group_no, group_ref)| {
      let group_name = options
        .wgpu_binding_generator
        .bind_group_layout
        .bind_group_name_ident(*group_no);

      let group_name = match group_ref.kind {
        ShaderBindGroupRefKind::Common => {
          let containing_module = group_ref.data.first_module();
          let path = RustSourceItemPath::new(containing_module, group_name.to_smolstr());
          quote!(#path)
        }
        ShaderBindGroupRefKind::Entrypoint => quote!(#group_name),
      };

      let field = indexed_name_ident("bind_group", *group_no);
      quote!(pub #field: &'a #group_name)
    })
    .collect();

  let has_compute = shader_stages.contains(wgpu::ShaderStages::COMPUTE);
  let has_render = shader_stages.contains(wgpu::ShaderStages::VERTEX_FRAGMENT)
    || shader_stages.contains(wgpu::ShaderStages::FRAGMENT)
    || shader_stages.contains(wgpu::ShaderStages::VERTEX);

  // The set function for each bind group already sets the index.
  let set_groups: Vec<_> = bind_group_data
    .keys()
    .map(|group_no| {
      let group = indexed_name_ident("bind_group", *group_no);
      quote!(#group.set(pass);)
    })
    .collect();

  if bind_group_data.is_empty() {
    // Don't include empty modules.
    vec![]
  } else {
    let bind_group_trait = RustSourceItem::new(
      RustSourceItemCategory::TypeDefs.into(),
      RustSourceItemPath::new(MOD_REFERENCE_ROOT.into(), "SetBindGroup".into()),
      quote! {
        pub trait SetBindGroup {
          fn set_bind_group(
              &mut self,
              index: u32,
              bind_group: &wgpu::BindGroup,
              offsets: &[wgpu::DynamicOffset],
          );
        }
      },
    );

    let mut set_bind_group_impls = Vec::new();
    if has_compute {
      set_bind_group_impls.push(RustSourceItem::new(
        RustSourceItemCategory::TraitImpls.into(),
        RustSourceItemPath::new(
          MOD_REFERENCE_ROOT.into(),
          "impl SetBindGroup for wgpu::ComputePass<'_>".into(),
        ),
        quote! {
          impl SetBindGroup for wgpu::ComputePass<'_> {
            fn set_bind_group(
              &mut self,
              index: u32,
              bind_group: &wgpu::BindGroup,
              offsets: &[wgpu::DynamicOffset],
            ) {
                self.set_bind_group(index, bind_group, offsets);
            }
          }
        },
      ));
    }

    if has_render {
      set_bind_group_impls.extend([
        RustSourceItem::new(
          RustSourceItemCategory::TraitImpls.into(),
          RustSourceItemPath::new(
            MOD_REFERENCE_ROOT.into(),
            "impl SetBindGroup for wgpu::RenderPass<'_>".into(),
          ),
          quote! {
            impl SetBindGroup for wgpu::RenderPass<'_> {
              fn set_bind_group(
                &mut self,
                index: u32,
                bind_group: &wgpu::BindGroup,
                offsets: &[wgpu::DynamicOffset],
              ) {
                  self.set_bind_group(index, bind_group, offsets);
              }
            }
          },
        ),
        RustSourceItem::new(
          RustSourceItemCategory::TraitImpls.into(),
          RustSourceItemPath::new(
            MOD_REFERENCE_ROOT.into(),
            "impl SetBindGroup for wgpu::RenderBundleEncoder<'_>".into(),
          ),
          quote! {
            impl SetBindGroup for wgpu::RenderBundleEncoder<'_> {
              fn set_bind_group(
                &mut self,
                index: u32,
                bind_group: &wgpu::BindGroup,
                offsets: &[wgpu::DynamicOffset],
              ) {
                  self.set_bind_group(index, bind_group, offsets);
              }
            }
          },
        ),
      ]);
    };

    let entry_bind_groups = RustSourceItem::new(
      RustSourceItemCategory::TypeDefs | RustSourceItemCategory::TypeImpls,
      RustSourceItemPath::new(invoking_entry_module.into(), "WgpuBindGroups".into()),
      quote! {
        #[doc = " Bind groups can be set individually using their set(render_pass) method, or all at once using `WgpuBindGroups::set`."]
        #[doc = " For optimal performance with many draw calls, it's recommended to organize bindings into bind groups based on update frequency:"]
        #[doc = "   - Bind group 0: Least frequent updates (e.g. per frame resources)"]
        #[doc = "   - Bind group 1: More frequent updates"]
        #[doc = "   - Bind group 2: More frequent updates"]
        #[doc = "   - Bind group 3: Most frequent updates (e.g. per draw resources)"]
        #[derive(Debug, Copy, Clone)]
        pub struct WgpuBindGroups<'a> {
            #(#bind_group_fields),*
        }

        impl<'a> WgpuBindGroups<'a> {
            pub fn set(&self, pass: &mut impl SetBindGroup) {
                #(self.#set_groups)*
            }
        }
      },
    );

    [bind_group_trait, entry_bind_groups]
      .into_iter()
      .chain(set_bind_group_impls)
      .collect()
  }
}

#[cfg(test)]
mod tests {
  use indoc::indoc;

  use super::*;
  use crate::assert_tokens_snapshot;
  use crate::bind_group::raw_shader_bind_group::RawShaderEntryBindGroups;

  #[test]
  fn bind_group_data_consecutive_bind_groups() {
    let source = indoc! {r#"
            @group(0) @binding(0) var<uniform> a: vec4<f32>;
            @group(1) @binding(0) var<uniform> b: vec4<f32>;
            @group(2) @binding(0) var<uniform> c: vec4<f32>;

            @fragment
            fn main() {}
        "#};

    let module = naga::front::wgsl::parse_str(source).unwrap();
    assert_eq!(
      3,
      get_bind_group_data_for_entry(
        &module,
        wgpu::ShaderStages::NONE,
        &WgslBindgenOption::default(),
        "test"
      )
      .unwrap()
      .bind_group_data
      .len()
    );
  }

  #[test]
  fn bind_group_data_first_group_not_zero() {
    let source = indoc! {r#"
            @group(1) @binding(0) var<uniform> a: vec4<f32>;

            @fragment
            fn main() {}
        "#};

    let module = naga::front::wgsl::parse_str(source).unwrap();
    assert!(matches!(
      get_bind_group_data_for_entry(
        &module,
        wgpu::ShaderStages::FRAGMENT,
        &WgslBindgenOption::default(),
        "test"
      ),
      Err(CreateModuleError::NonConsecutiveBindGroups)
    ));
  }

  #[test]
  fn bind_group_data_non_consecutive_bind_groups() {
    let source = indoc! {r#"
            @group(0) @binding(0) var<uniform> a: vec4<f32>;
            @group(1) @binding(0) var<uniform> b: vec4<f32>;
            @group(3) @binding(0) var<uniform> c: vec4<f32>;

            @fragment
            fn main() {}
        "#};

    let module = naga::front::wgsl::parse_str(source).unwrap();
    assert!(matches!(
      get_bind_group_data_for_entry(
        &module,
        wgpu::ShaderStages::NONE,
        &WgslBindgenOption::default(),
        "test"
      ),
      Err(CreateModuleError::NonConsecutiveBindGroups)
    ));
  }

  fn generate_test_bind_groups_module(
    bind_group_data: &BTreeMap<u32, SingleBindGroupData>,
    shader_stages: wgpu::ShaderStages,
    options: &WgslBindgenOption,
  ) -> TokenStream {
    let raw_shader_entry_bind_groups = RawShaderEntryBindGroups {
      containing_module: "test".into(),
      shader_stages,
      bind_group_data: bind_group_data.clone(),
    };

    let mut raw_shaders_bind_groups = RawShadersBindGroups::new(options);
    raw_shaders_bind_groups.add(raw_shader_entry_bind_groups);
    let items = raw_shaders_bind_groups
      .create_reusable_shader_bind_groups()
      .generate_bind_groups(&WgslBindgenOption::default());
    let all_matching = items
      .into_iter()
      .filter(|item| item.path.name.contains("WgpuBindGroup"))
      .map(|item| item.tokenstream)
      .collect::<Vec<_>>();

    quote!(#(#all_matching)*)
  }

  #[test]
  fn bind_groups_module_compute() {
    let source = indoc! {r#"
            struct VertexInput0 {};
            struct VertexWeight {};
            struct Vertices {};
            struct VertexWeights {};
            struct Transforms {};

            @group(0) @binding(0) var<storage, read> src: array<vec4<f32>>;
            @group(0) @binding(1) var<storage, read> vertex_weights: VertexWeights;
            @group(0) @binding(2) var<storage, read_write> dst: Vertices;

            @group(1) @binding(0) var<uniform> transforms: Transforms;

            @compute
            @workgroup_size(64)
            fn main() {}
        "#};

    let module = naga::front::wgsl::parse_str(source).unwrap();
    let options = WgslBindgenOption::default();
    let bind_group_data = get_bind_group_data_for_entry(
      &module,
      wgpu::ShaderStages::COMPUTE,
      &options,
      "test",
    )
    .unwrap()
    .bind_group_data;

    let actual = generate_test_bind_groups_module(
      &bind_group_data,
      wgpu::ShaderStages::COMPUTE,
      &options,
    );

    assert_tokens_snapshot!(actual);
  }

  #[test]
  fn bind_groups_module_vertex_fragment() {
    // Test different texture and sampler types.
    // TODO: Storage textures.
    let source = indoc! {r#"
            struct Transforms {};

            @group(0) @binding(0)
            var color_texture: texture_2d<f32>;
            @group(0) @binding(1)
            var color_texture_i32: texture_2d<i32>;
            @group(0) @binding(2)
            var color_texture_u32: texture_2d<u32>;
            @group(0) @binding(3)
            var color_sampler: sampler;
            @group(0) @binding(4)
            var depth_texture: texture_depth_2d;
            @group(0) @binding(5)
            var comparison_sampler: sampler_comparison;

            @group(0) @binding(6)
            var storage_tex_read: texture_storage_2d<r32float, read>;
            @group(0) @binding(7)
            var storage_tex_write: texture_storage_2d<rg32sint, write>;
            @group(0) @binding(8)
            var storage_tex_read_write: texture_storage_2d<rgba8uint, read_write>;

            @group(0) @binding(9)
            var color_texture_msaa: texture_multisampled_2d<f32>;
            @group(0) @binding(10)
            var depth_texture_msaa: texture_depth_multisampled_2d;

            @group(1) @binding(0) var<uniform> transforms: Transforms;
            @group(1) @binding(1) var<uniform> one: f32;

            @vertex
            fn vs_main() {}

            @fragment
            fn fs_main() {}
        "#};

    let module = naga::front::wgsl::parse_str(source).unwrap();
    let options = WgslBindgenOption::default();
    let bind_group_data = get_bind_group_data_for_entry(
      &module,
      wgpu::ShaderStages::VERTEX_FRAGMENT,
      &options,
      "test",
    )
    .unwrap()
    .bind_group_data;

    let actual = generate_test_bind_groups_module(
      &bind_group_data,
      wgpu::ShaderStages::VERTEX_FRAGMENT,
      &options,
    );

    // TODO: Are storage buffers valid for vertex/fragment?
    assert_tokens_snapshot!(actual);
  }

  #[test]
  fn bind_groups_module_vertex() {
    // The actual content of the structs doesn't matter.
    // We only care about the groups and bindings.
    let source = indoc! {r#"
            struct Transforms {};

            @group(0) @binding(0) var<uniform> transforms: Transforms;

            @vertex
            fn vs_main() {}
        "#};

    let module = naga::front::wgsl::parse_str(source).unwrap();
    let options = WgslBindgenOption::default();
    let bind_group_data = get_bind_group_data_for_entry(
      &module,
      wgpu::ShaderStages::VERTEX,
      &options,
      "test",
    )
    .unwrap()
    .bind_group_data;

    let actual = generate_test_bind_groups_module(
      &bind_group_data,
      wgpu::ShaderStages::VERTEX,
      &options,
    );

    assert_tokens_snapshot!(actual);
  }

  #[test]
  fn bind_groups_module_fragment() {
    // The actual content of the structs doesn't matter.
    // We only care about the groups and bindings.
    let source = indoc! {r#"
            struct Transforms {};

            @group(0) @binding(0) var<uniform> transforms: Transforms;

            @fragment
            fn fs_main() {}
        "#};

    let module = naga::front::wgsl::parse_str(source).unwrap();
    let options = WgslBindgenOption::default();
    let bind_group_data = get_bind_group_data_for_entry(
      &module,
      wgpu::ShaderStages::FRAGMENT,
      &options,
      "test",
    )
    .unwrap()
    .bind_group_data;

    let actual = generate_test_bind_groups_module(
      &bind_group_data,
      wgpu::ShaderStages::FRAGMENT,
      &options,
    );

    assert_tokens_snapshot!(actual);
  }

  #[test]
  fn bind_groups_module_acceleration_structure() {
    // Test AccelerationStructure binding type.
    let source = indoc! {r#"
            enable wgpu_ray_query;
            
            struct Transforms {};

            @group(0) @binding(0) var<uniform> transforms: Transforms;
            @group(0) @binding(1) var acc_struct: acceleration_structure;

            @vertex
            fn vs_main() {}

            @fragment
            fn fs_main() {}
        "#};

    let module = naga::front::wgsl::parse_str(source).unwrap();
    let options = WgslBindgenOption::default();
    let bind_group_data = get_bind_group_data_for_entry(
      &module,
      wgpu::ShaderStages::VERTEX_FRAGMENT,
      &options,
      "test",
    )
    .unwrap()
    .bind_group_data;

    let actual = generate_test_bind_groups_module(
      &bind_group_data,
      wgpu::ShaderStages::VERTEX_FRAGMENT,
      &options,
    );

    assert_tokens_snapshot!(actual);
  }

  #[test]
  fn bind_groups_module_array_bindings() {
    // Test texture and sampler array bindings.
    let source = indoc! {r#"
            struct Transforms {};

            @group(0) @binding(0) var<uniform> transforms: Transforms;
            @group(0) @binding(1) var texture_array: binding_array<texture_2d<f32>, 4>;
            @group(0) @binding(2) var sampler_array: binding_array<sampler, 3>;

            @vertex
            fn vs_main() {}

            @fragment
            fn fs_main() {}
        "#};

    let module = naga::front::wgsl::parse_str(source).unwrap();
    let options = WgslBindgenOption::default();
    let bind_group_data = get_bind_group_data_for_entry(
      &module,
      wgpu::ShaderStages::VERTEX_FRAGMENT,
      &options,
      "test",
    )
    .unwrap()
    .bind_group_data;

    let actual = generate_test_bind_groups_module(
      &bind_group_data,
      wgpu::ShaderStages::VERTEX_FRAGMENT,
      &options,
    );

    assert_tokens_snapshot!(actual);
  }
}