-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbind.rs
More file actions
343 lines (312 loc) · 9.26 KB
/
bind.rs
File metadata and controls
343 lines (312 loc) · 9.26 KB
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
//! Bind group and bind group layout builders for the platform layer.
//!
//! These types provide a thin, explicit wrapper around `wgpu` bind resources
//! so higher layers can compose layouts and groups without pulling in raw
//! `wgpu` descriptors throughout the codebase.
use std::num::NonZeroU64;
use wgpu;
use crate::wgpu::{
buffer,
gpu::Gpu,
};
/// Wrapper around `wgpu::BindGroupLayout` that preserves a label.
#[derive(Debug)]
pub struct BindGroupLayout {
pub(crate) raw: wgpu::BindGroupLayout,
pub(crate) label: Option<String>,
}
impl BindGroupLayout {
/// Borrow the underlying `wgpu::BindGroupLayout`.
pub fn raw(&self) -> &wgpu::BindGroupLayout {
return &self.raw;
}
/// Optional debug label used during creation.
pub fn label(&self) -> Option<&str> {
return self.label.as_deref();
}
}
/// Wrapper around `wgpu::BindGroup` that preserves a label.
#[derive(Debug)]
pub struct BindGroup {
pub(crate) raw: wgpu::BindGroup,
pub(crate) label: Option<String>,
}
impl BindGroup {
/// Borrow the underlying `wgpu::BindGroup`.
pub fn raw(&self) -> &wgpu::BindGroup {
return &self.raw;
}
/// Optional debug label used during creation.
pub fn label(&self) -> Option<&str> {
return self.label.as_deref();
}
}
/// Visibility of a binding across shader stages.
#[derive(Clone, Copy, Debug)]
pub enum Visibility {
Vertex,
Fragment,
Compute,
VertexAndFragment,
All,
}
impl Visibility {
fn to_wgpu(self) -> wgpu::ShaderStages {
return match self {
Visibility::Vertex => wgpu::ShaderStages::VERTEX,
Visibility::Fragment => wgpu::ShaderStages::FRAGMENT,
Visibility::Compute => wgpu::ShaderStages::COMPUTE,
Visibility::VertexAndFragment => {
wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT
}
Visibility::All => wgpu::ShaderStages::all(),
};
}
}
#[cfg(test)]
mod tests {
use super::*;
/// This test verifies that each public binding visibility option is
/// converted into the correct set of shader stage flags expected by the
/// underlying graphics layer. It checks single stage selections, a
/// combination of vertex and fragment stages, and the catch‑all option that
/// enables all stages. The goal is to demonstrate that the mapping logic is
/// precise and predictable so higher level code can rely on it when building
/// layouts and groups.
#[test]
fn visibility_maps_to_expected_shader_stages() {
assert_eq!(Visibility::Vertex.to_wgpu(), wgpu::ShaderStages::VERTEX);
assert_eq!(Visibility::Fragment.to_wgpu(), wgpu::ShaderStages::FRAGMENT);
assert_eq!(Visibility::Compute.to_wgpu(), wgpu::ShaderStages::COMPUTE);
assert_eq!(
Visibility::VertexAndFragment.to_wgpu(),
wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT
);
assert_eq!(Visibility::All.to_wgpu(), wgpu::ShaderStages::all());
}
#[test]
fn sampled_texture_2d_layout_entry_is_correct() {
let builder = BindGroupLayoutBuilder::new()
.with_sampled_texture_2d(1, Visibility::Fragment)
.with_sampler(2, Visibility::Fragment);
assert_eq!(builder.entries.len(), 2);
match builder.entries[0].ty {
wgpu::BindingType::Texture {
sample_type,
view_dimension,
multisampled,
} => {
assert_eq!(view_dimension, wgpu::TextureViewDimension::D2);
assert!(!multisampled);
match sample_type {
wgpu::TextureSampleType::Float { filterable } => assert!(filterable),
_ => panic!("expected float sample type"),
}
}
_ => panic!("expected texture binding type"),
}
match builder.entries[1].ty {
wgpu::BindingType::Sampler(kind) => {
assert_eq!(kind, wgpu::SamplerBindingType::Filtering);
}
_ => panic!("expected sampler binding type"),
}
}
}
/// Builder for creating a `wgpu::BindGroupLayout`.
#[derive(Default)]
pub struct BindGroupLayoutBuilder {
label: Option<String>,
entries: Vec<wgpu::BindGroupLayoutEntry>,
}
impl BindGroupLayoutBuilder {
/// Create a builder with no entries.
pub fn new() -> Self {
return Self {
label: None,
entries: Vec::new(),
};
}
/// Attach a human‑readable label.
pub fn with_label(mut self, label: &str) -> Self {
self.label = Some(label.to_string());
return self;
}
/// Declare a uniform buffer binding at the provided index.
pub fn with_uniform(mut self, binding: u32, visibility: Visibility) -> Self {
self.entries.push(wgpu::BindGroupLayoutEntry {
binding,
visibility: visibility.to_wgpu(),
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
});
return self;
}
/// Declare a uniform buffer binding with dynamic offsets at the provided index.
pub fn with_uniform_dynamic(
mut self,
binding: u32,
visibility: Visibility,
) -> Self {
self.entries.push(wgpu::BindGroupLayoutEntry {
binding,
visibility: visibility.to_wgpu(),
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: true,
min_binding_size: None,
},
count: None,
});
return self;
}
/// Declare a sampled texture binding (2D) at the provided index.
pub fn with_sampled_texture_2d(
mut self,
binding: u32,
visibility: Visibility,
) -> Self {
self.entries.push(wgpu::BindGroupLayoutEntry {
binding,
visibility: visibility.to_wgpu(),
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
});
return self;
}
/// Declare a sampled texture binding with an explicit view dimension.
pub fn with_sampled_texture_dim(
mut self,
binding: u32,
visibility: Visibility,
view_dimension: crate::wgpu::texture::ViewDimension,
) -> Self {
self.entries.push(wgpu::BindGroupLayoutEntry {
binding,
visibility: visibility.to_wgpu(),
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: view_dimension.to_wgpu(),
multisampled: false,
},
count: None,
});
return self;
}
/// Declare a filtering sampler binding at the provided index.
pub fn with_sampler(mut self, binding: u32, visibility: Visibility) -> Self {
self.entries.push(wgpu::BindGroupLayoutEntry {
binding,
visibility: visibility.to_wgpu(),
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
});
return self;
}
/// Build the layout using the provided device.
pub fn build(self, gpu: &Gpu) -> BindGroupLayout {
let raw =
gpu
.device()
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: self.label.as_deref(),
entries: &self.entries,
});
return BindGroupLayout {
raw,
label: self.label,
};
}
}
/// Builder for creating a `wgpu::BindGroup`.
#[derive(Default)]
pub struct BindGroupBuilder<'a> {
label: Option<String>,
layout: Option<&'a wgpu::BindGroupLayout>,
entries: Vec<wgpu::BindGroupEntry<'a>>,
}
impl<'a> BindGroupBuilder<'a> {
/// Create a new builder with no layout or entries.
pub fn new() -> Self {
return Self {
label: None,
layout: None,
entries: Vec::new(),
};
}
/// Attach a human‑readable label.
pub fn with_label(mut self, label: &str) -> Self {
self.label = Some(label.to_string());
return self;
}
/// Specify the layout to use for this bind group.
pub fn with_layout(mut self, layout: &'a BindGroupLayout) -> Self {
self.layout = Some(layout.raw());
return self;
}
/// Bind a uniform buffer at a binding index with optional size slice.
pub fn with_uniform(
mut self,
binding: u32,
buffer: &'a buffer::Buffer,
offset: u64,
size: Option<NonZeroU64>,
) -> Self {
self.entries.push(wgpu::BindGroupEntry {
binding,
resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
buffer: buffer.raw(),
offset,
size,
}),
});
return self;
}
/// Bind a texture view at a binding index.
pub fn with_texture(
mut self,
binding: u32,
texture: &'a crate::wgpu::texture::Texture,
) -> Self {
self.entries.push(wgpu::BindGroupEntry {
binding,
resource: wgpu::BindingResource::TextureView(texture.view()),
});
return self;
}
/// Bind a sampler at a binding index.
pub fn with_sampler(
mut self,
binding: u32,
sampler: &'a crate::wgpu::texture::Sampler,
) -> Self {
self.entries.push(wgpu::BindGroupEntry {
binding,
resource: wgpu::BindingResource::Sampler(sampler.raw()),
});
return self;
}
/// Build the bind group with the accumulated entries.
pub fn build(self, gpu: &Gpu) -> BindGroup {
let layout = self
.layout
.expect("BindGroupBuilder requires a layout before build");
let raw = gpu.device().create_bind_group(&wgpu::BindGroupDescriptor {
label: self.label.as_deref(),
layout,
entries: &self.entries,
});
return BindGroup {
raw,
label: self.label,
};
}
}