-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbind.rs
More file actions
231 lines (207 loc) · 6.19 KB
/
bind.rs
File metadata and controls
231 lines (207 loc) · 6.19 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
//! 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 crate::wgpu::types as wgpu;
#[derive(Debug)]
/// Wrapper around `wgpu::BindGroupLayout` that preserves a label.
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();
}
}
#[derive(Debug)]
/// Wrapper around `wgpu::BindGroup` that preserves a label.
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();
}
}
#[derive(Clone, Copy, Debug)]
/// Visibility of a binding across shader stages.
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());
}
}
#[derive(Default)]
/// Builder for creating a `wgpu::BindGroupLayout`.
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;
}
/// Build the layout using the provided device.
pub fn build(self, device: &wgpu::Device) -> BindGroupLayout {
let raw =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: self.label.as_deref(),
entries: &self.entries,
});
return BindGroupLayout {
raw,
label: self.label,
};
}
}
#[derive(Default)]
/// Builder for creating a `wgpu::BindGroup`.
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 wgpu::Buffer,
offset: u64,
size: Option<NonZeroU64>,
) -> Self {
self.entries.push(wgpu::BindGroupEntry {
binding,
resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
buffer,
offset,
size,
}),
});
return self;
}
/// Build the bind group with the accumulated entries.
pub fn build(self, device: &wgpu::Device) -> BindGroup {
let layout = self
.layout
.expect("BindGroupBuilder requires a layout before build");
let raw = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: self.label.as_deref(),
layout,
entries: &self.entries,
});
return BindGroup {
raw,
label: self.label,
};
}
}