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
use wgpu_upstream::BufferBinding;
use crate::wgpu;
#[derive(Debug, Default)]
pub struct LayoutBuilder {
bindings: Vec<(wgpu::ShaderStage, wgpu::BindingType)>,
}
#[derive(Debug, Default)]
pub struct Builder<'a> {
resources: Vec<wgpu::BindingResource<'a>>,
}
impl LayoutBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn binding(mut self, visibility: wgpu::ShaderStage, ty: wgpu::BindingType) -> Self {
self.bindings.push((visibility, ty));
self
}
pub fn uniform_buffer(self, visibility: wgpu::ShaderStage, has_dynamic_offset: bool) -> Self {
let ty = wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset,
min_binding_size: None,
};
self.binding(visibility, ty)
}
pub fn storage_buffer(
self,
visibility: wgpu::ShaderStage,
has_dynamic_offset: bool,
read_only: bool,
) -> Self {
let ty = wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only },
has_dynamic_offset,
min_binding_size: None,
};
self.binding(visibility, ty)
}
pub fn sampler(self, visibility: wgpu::ShaderStage, filtering: bool) -> Self {
let comparison = false;
let ty = wgpu::BindingType::Sampler {
filtering,
comparison,
};
self.binding(visibility, ty)
}
pub fn comparison_sampler(self, visibility: wgpu::ShaderStage, filtering: bool) -> Self {
let comparison = true;
let ty = wgpu::BindingType::Sampler {
filtering,
comparison,
};
self.binding(visibility, ty)
}
pub fn texture(
self,
visibility: wgpu::ShaderStage,
multisampled: bool,
view_dimension: wgpu::TextureViewDimension,
sample_type: wgpu::TextureSampleType,
) -> Self {
let ty = wgpu::BindingType::Texture {
multisampled,
view_dimension,
sample_type,
};
self.binding(visibility, ty)
}
pub fn texture_from(self, visibility: wgpu::ShaderStage, texture: &wgpu::Texture) -> Self {
self.texture(
visibility,
texture.sample_count() > 1,
texture.view_dimension(),
texture.sample_type(),
)
}
pub fn storage_texture(
self,
visibility: wgpu::ShaderStage,
format: wgpu::TextureFormat,
view_dimension: wgpu::TextureViewDimension,
access: wgpu::StorageTextureAccess,
) -> Self {
let ty = wgpu::BindingType::StorageTexture {
view_dimension,
format,
access,
};
self.binding(visibility, ty)
}
pub fn storage_texture_from(
self,
visibility: wgpu::ShaderStage,
texture: &wgpu::Texture,
access: wgpu::StorageTextureAccess,
) -> Self {
self.storage_texture(
visibility,
texture.format(),
texture.view_dimension(),
access,
)
}
pub fn build(self, device: &wgpu::Device) -> wgpu::BindGroupLayout {
let mut entries = Vec::with_capacity(self.bindings.len());
for (i, (visibility, ty)) in self.bindings.into_iter().enumerate() {
let layout_binding = wgpu::BindGroupLayoutEntry {
binding: i as u32,
visibility,
ty,
count: None,
};
entries.push(layout_binding);
}
let descriptor = wgpu::BindGroupLayoutDescriptor {
label: Some("nannou bind group layout"),
entries: &entries,
};
device.create_bind_group_layout(&descriptor)
}
}
impl<'a> Builder<'a> {
pub fn new() -> Self {
Self::default()
}
pub fn binding(mut self, resource: wgpu::BindingResource<'a>) -> Self {
self.resources.push(resource);
self
}
pub fn buffer_bytes(
self,
buffer: &'a wgpu::Buffer,
offset: wgpu::BufferAddress,
size: Option<wgpu::BufferSize>,
) -> Self {
let resource = wgpu::BindingResource::Buffer(BufferBinding {
buffer,
offset,
size,
});
self.binding(resource)
}
pub fn buffer<T>(self, buffer: &'a wgpu::Buffer, range: std::ops::Range<usize>) -> Self
where
T: Copy,
{
let size_bytes = std::mem::size_of::<T>() as wgpu::BufferAddress;
let start = range.start as wgpu::BufferAddress * size_bytes;
let end = range.end as wgpu::BufferAddress * size_bytes;
let size = std::num::NonZeroU64::new(end - start).expect("buffer slice must not be empty");
self.buffer_bytes(buffer, start, Some(size))
}
pub fn sampler(self, sampler: &'a wgpu::Sampler) -> Self {
let resource = wgpu::BindingResource::Sampler(sampler);
self.binding(resource)
}
pub fn texture_view(self, view: &'a wgpu::TextureViewHandle) -> Self {
let resource = wgpu::BindingResource::TextureView(view);
self.binding(resource)
}
pub fn build(self, device: &wgpu::Device, layout: &wgpu::BindGroupLayout) -> wgpu::BindGroup {
let mut entries = Vec::with_capacity(self.resources.len());
for (i, resource) in self.resources.into_iter().enumerate() {
let binding = wgpu::BindGroupEntry {
binding: i as u32,
resource,
};
entries.push(binding);
}
let descriptor = wgpu::BindGroupDescriptor {
label: Some("nannou bind group"),
layout,
entries: &entries,
};
device.create_bind_group(&descriptor)
}
}