Skip to main content

bevy_shader_plugin/
extra_bind_group.rs

1use bevy::render::{
2    render_resource::{BindGroup, BindGroupEntry, BindGroupLayout, BindingResource, Buffer},
3    renderer::RenderDevice,
4};
5
6/// Fluent builder for creating a single extra bind group.
7///
8/// Bindings are assigned sequentially starting at 0 for each call to
9/// [`buffer`](Self::buffer). Use [`at`](Self::at) to set an explicit index.
10pub struct FragmentBindGroupBuilder<'a> {
11    label: Option<&'a str>,
12    layout: &'a BindGroupLayout,
13    render_device: &'a RenderDevice,
14    entries: Vec<BindGroupEntry<'a>>,
15}
16
17impl<'a> FragmentBindGroupBuilder<'a> {
18    pub fn new(layout: &'a BindGroupLayout, render_device: &'a RenderDevice) -> Self {
19        Self {
20            label: None,
21            layout,
22            render_device,
23            entries: Vec::new(),
24        }
25    }
26
27    pub fn label(mut self, label: &'a str) -> Self {
28        self.label = Some(label);
29        self
30    }
31
32    pub fn buffer(mut self, buffer: &'a Buffer) -> Self {
33        let binding = self.entries.len() as u32;
34        self.entries.push(BindGroupEntry {
35            binding,
36            resource: buffer.as_entire_binding(),
37        });
38        self
39    }
40
41    /// Bind an arbitrary [`BindingResource`] at an explicit binding index.
42    pub fn at(mut self, binding: u32, resource: BindingResource<'a>) -> Self {
43        self.entries.push(BindGroupEntry { binding, resource });
44        self
45    }
46
47    pub fn build(self) -> BindGroup {
48        self.render_device.create_bind_group(
49            self.label.unwrap_or("fragment_extra_bind_group"),
50            self.layout,
51            &self.entries,
52        )
53    }
54}