|
| 1 | +//! Bind group and bind group layout builders for the platform layer. |
| 2 | +//! |
| 3 | +//! These types provide a thin, explicit wrapper around `wgpu` bind resources |
| 4 | +//! so higher layers can compose layouts and groups without pulling in raw |
| 5 | +//! `wgpu` descriptors throughout the codebase. |
| 6 | +
|
| 7 | +use std::num::NonZeroU64; |
| 8 | + |
| 9 | +use crate::wgpu::types as wgpu; |
| 10 | + |
| 11 | +#[derive(Debug)] |
| 12 | +/// Wrapper around `wgpu::BindGroupLayout` that preserves a label. |
| 13 | +pub struct BindGroupLayout { |
| 14 | + pub(crate) raw: wgpu::BindGroupLayout, |
| 15 | + pub(crate) label: Option<String>, |
| 16 | +} |
| 17 | + |
| 18 | +impl BindGroupLayout { |
| 19 | + /// Borrow the underlying `wgpu::BindGroupLayout`. |
| 20 | + pub fn raw(&self) -> &wgpu::BindGroupLayout { |
| 21 | + &self.raw |
| 22 | + } |
| 23 | + |
| 24 | + /// Optional debug label used during creation. |
| 25 | + pub fn label(&self) -> Option<&str> { |
| 26 | + self.label.as_deref() |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Debug)] |
| 31 | +/// Wrapper around `wgpu::BindGroup` that preserves a label. |
| 32 | +pub struct BindGroup { |
| 33 | + pub(crate) raw: wgpu::BindGroup, |
| 34 | + pub(crate) label: Option<String>, |
| 35 | +} |
| 36 | + |
| 37 | +impl BindGroup { |
| 38 | + /// Borrow the underlying `wgpu::BindGroup`. |
| 39 | + pub fn raw(&self) -> &wgpu::BindGroup { |
| 40 | + &self.raw |
| 41 | + } |
| 42 | + |
| 43 | + /// Optional debug label used during creation. |
| 44 | + pub fn label(&self) -> Option<&str> { |
| 45 | + self.label.as_deref() |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +#[derive(Clone, Copy, Debug)] |
| 50 | +/// Visibility of a binding across shader stages. |
| 51 | +pub enum Visibility { |
| 52 | + Vertex, |
| 53 | + Fragment, |
| 54 | + Compute, |
| 55 | + VertexAndFragment, |
| 56 | + All, |
| 57 | +} |
| 58 | + |
| 59 | +impl Visibility { |
| 60 | + fn to_wgpu(self) -> wgpu::ShaderStages { |
| 61 | + match self { |
| 62 | + Visibility::Vertex => wgpu::ShaderStages::VERTEX, |
| 63 | + Visibility::Fragment => wgpu::ShaderStages::FRAGMENT, |
| 64 | + Visibility::Compute => wgpu::ShaderStages::COMPUTE, |
| 65 | + Visibility::VertexAndFragment => { |
| 66 | + wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT |
| 67 | + } |
| 68 | + Visibility::All => wgpu::ShaderStages::all(), |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +#[derive(Default)] |
| 74 | +/// Builder for creating a `wgpu::BindGroupLayout`. |
| 75 | +pub struct BindGroupLayoutBuilder { |
| 76 | + label: Option<String>, |
| 77 | + entries: Vec<wgpu::BindGroupLayoutEntry>, |
| 78 | +} |
| 79 | + |
| 80 | +impl BindGroupLayoutBuilder { |
| 81 | + /// Create a builder with no entries. |
| 82 | + pub fn new() -> Self { |
| 83 | + Self { |
| 84 | + label: None, |
| 85 | + entries: Vec::new(), |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /// Attach a human‑readable label. |
| 90 | + pub fn with_label(mut self, label: &str) -> Self { |
| 91 | + self.label = Some(label.to_string()); |
| 92 | + self |
| 93 | + } |
| 94 | + |
| 95 | + /// Declare a uniform buffer binding at the provided index. |
| 96 | + pub fn with_uniform(mut self, binding: u32, visibility: Visibility) -> Self { |
| 97 | + self.entries.push(wgpu::BindGroupLayoutEntry { |
| 98 | + binding, |
| 99 | + visibility: visibility.to_wgpu(), |
| 100 | + ty: wgpu::BindingType::Buffer { |
| 101 | + ty: wgpu::BufferBindingType::Uniform, |
| 102 | + has_dynamic_offset: false, |
| 103 | + min_binding_size: None, |
| 104 | + }, |
| 105 | + count: None, |
| 106 | + }); |
| 107 | + self |
| 108 | + } |
| 109 | + |
| 110 | + /// Declare a uniform buffer binding with dynamic offsets at the provided index. |
| 111 | + pub fn with_uniform_dynamic( |
| 112 | + mut self, |
| 113 | + binding: u32, |
| 114 | + visibility: Visibility, |
| 115 | + ) -> Self { |
| 116 | + self.entries.push(wgpu::BindGroupLayoutEntry { |
| 117 | + binding, |
| 118 | + visibility: visibility.to_wgpu(), |
| 119 | + ty: wgpu::BindingType::Buffer { |
| 120 | + ty: wgpu::BufferBindingType::Uniform, |
| 121 | + has_dynamic_offset: true, |
| 122 | + min_binding_size: None, |
| 123 | + }, |
| 124 | + count: None, |
| 125 | + }); |
| 126 | + self |
| 127 | + } |
| 128 | + |
| 129 | + /// Build the layout using the provided device. |
| 130 | + pub fn build(self, device: &wgpu::Device) -> BindGroupLayout { |
| 131 | + let raw = |
| 132 | + device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { |
| 133 | + label: self.label.as_deref(), |
| 134 | + entries: &self.entries, |
| 135 | + }); |
| 136 | + BindGroupLayout { |
| 137 | + raw, |
| 138 | + label: self.label, |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +#[derive(Default)] |
| 144 | +/// Builder for creating a `wgpu::BindGroup`. |
| 145 | +pub struct BindGroupBuilder<'a> { |
| 146 | + label: Option<String>, |
| 147 | + layout: Option<&'a wgpu::BindGroupLayout>, |
| 148 | + entries: Vec<wgpu::BindGroupEntry<'a>>, |
| 149 | +} |
| 150 | + |
| 151 | +impl<'a> BindGroupBuilder<'a> { |
| 152 | + /// Create a new builder with no layout or entries. |
| 153 | + pub fn new() -> Self { |
| 154 | + Self { |
| 155 | + label: None, |
| 156 | + layout: None, |
| 157 | + entries: Vec::new(), |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /// Attach a human‑readable label. |
| 162 | + pub fn with_label(mut self, label: &str) -> Self { |
| 163 | + self.label = Some(label.to_string()); |
| 164 | + self |
| 165 | + } |
| 166 | + |
| 167 | + /// Specify the layout to use for this bind group. |
| 168 | + pub fn with_layout(mut self, layout: &'a BindGroupLayout) -> Self { |
| 169 | + self.layout = Some(layout.raw()); |
| 170 | + self |
| 171 | + } |
| 172 | + |
| 173 | + /// Bind a uniform buffer at a binding index with optional size slice. |
| 174 | + pub fn with_uniform( |
| 175 | + mut self, |
| 176 | + binding: u32, |
| 177 | + buffer: &'a wgpu::Buffer, |
| 178 | + offset: u64, |
| 179 | + size: Option<NonZeroU64>, |
| 180 | + ) -> Self { |
| 181 | + self.entries.push(wgpu::BindGroupEntry { |
| 182 | + binding, |
| 183 | + resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { |
| 184 | + buffer, |
| 185 | + offset, |
| 186 | + size, |
| 187 | + }), |
| 188 | + }); |
| 189 | + self |
| 190 | + } |
| 191 | + |
| 192 | + /// Build the bind group with the accumulated entries. |
| 193 | + pub fn build(self, device: &wgpu::Device) -> BindGroup { |
| 194 | + let layout = self |
| 195 | + .layout |
| 196 | + .expect("BindGroupBuilder requires a layout before build"); |
| 197 | + let raw = device.create_bind_group(&wgpu::BindGroupDescriptor { |
| 198 | + label: self.label.as_deref(), |
| 199 | + layout, |
| 200 | + entries: &self.entries, |
| 201 | + }); |
| 202 | + BindGroup { |
| 203 | + raw, |
| 204 | + label: self.label, |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments