-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind_group.rs
276 lines (244 loc) · 8.34 KB
/
bind_group.rs
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
use std::sync::Arc;
use crate::{
Buffer, BufferAccess, Context, Image, ImageDimension, Sampler, SamplerBindingType,
StorageImageAccess,
};
/// Represents a [`Buffer`]
#[derive(Debug)]
struct BufferBinding<'a> {
resource: wgpu::BindingResource<'a>,
access: BufferAccess,
}
impl<'a> From<&BufferBinding<'a>> for wgpu::BindingType {
fn from(binding: &BufferBinding<'a>) -> Self {
wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage {
read_only: binding.access == BufferAccess::ReadOnly,
},
has_dynamic_offset: false,
min_binding_size: None,
}
}
}
/// Represents an [`Image`] for sampling.
#[derive(Debug)]
struct TextureBinding<'a> {
resource: wgpu::BindingResource<'a>,
dimension: wgpu::TextureViewDimension,
sample_type: wgpu::TextureSampleType,
}
impl<'a> From<&TextureBinding<'a>> for wgpu::BindingType {
fn from(binding: &TextureBinding<'a>) -> Self {
wgpu::BindingType::Texture {
sample_type: binding.sample_type,
view_dimension: binding.dimension,
multisampled: false,
}
}
}
/// Represents an [`Image`] for storing.
#[derive(Debug)]
struct StorageTextureBinding<'a> {
resource: wgpu::BindingResource<'a>,
access: wgpu::StorageTextureAccess,
format: wgpu::TextureFormat,
dimension: wgpu::TextureViewDimension,
}
impl<'a> From<&StorageTextureBinding<'a>> for wgpu::BindingType {
fn from(binding: &StorageTextureBinding<'a>) -> Self {
wgpu::BindingType::StorageTexture {
access: binding.access,
format: binding.format,
view_dimension: binding.dimension,
}
}
}
/// Represents a [`Sampler`].
#[derive(Debug)]
struct SamplerBinding<'a> {
resource: wgpu::BindingResource<'a>,
binding_type: wgpu::SamplerBindingType,
}
impl<'a> From<&SamplerBinding<'a>> for wgpu::BindingType {
fn from(binding: &SamplerBinding<'a>) -> Self {
wgpu::BindingType::Sampler(binding.binding_type)
}
}
/// Everything that can be bound to a `wgpu::BindGroup`.
#[derive(Debug)]
enum Binding<'a> {
Buffer(BufferBinding<'a>),
Sampler(SamplerBinding<'a>),
Texture(TextureBinding<'a>),
StorageTexture(StorageTextureBinding<'a>),
}
impl<'a> Binding<'a> {
fn into_resource(self) -> wgpu::BindingResource<'a> {
match self {
Binding::Buffer(buffer_binding) => buffer_binding.resource,
Binding::Sampler(sampler_binding) => sampler_binding.resource,
Binding::Texture(texture_binding) => texture_binding.resource,
Binding::StorageTexture(storage_texture_binding) => storage_texture_binding.resource,
}
}
}
impl<'a> From<&Binding<'a>> for wgpu::BindingType {
fn from(binding: &Binding<'a>) -> Self {
match binding {
Binding::Buffer(buffer_binding) => buffer_binding.into(),
Binding::Sampler(sampler_binding) => sampler_binding.into(),
Binding::Texture(texture_binding) => texture_binding.into(),
Binding::StorageTexture(storage_texture_binding) => storage_texture_binding.into(),
}
}
}
/// Contains the information to create BindGroups.
///
/// This may change in the future to be able to reutilize `wgpu::BindGroupLayout`s.
#[derive(Debug)]
pub struct BindGroupDescriptor<'a> {
device: Arc<crate::Device>,
bindings: Vec<Binding<'a>>,
}
impl<'a> BindGroupDescriptor<'a> {
/// Creates an empty bind group layout.
pub fn new(context: &Context) -> Self {
Self {
device: Arc::clone(&context.device),
bindings: Vec::new(),
}
}
/// Pushes `buffer` as the last binding with `accessor` access.
///
/// # Example wgsl syntax
/// ```cpp,ignore
/// @group(X) @binding(Y)
/// var<storage, 'access'> buffer: array<'T'>; // T is the type of the buffer
/// ```
pub fn push_buffer<T>(mut self, buffer: &'a Buffer<T>, access: BufferAccess) -> Self {
let binding = Binding::Buffer(BufferBinding {
resource: buffer.handle.as_entire_binding(),
access,
});
self.bindings.push(binding);
self
}
/// Pushes `sampler` as the last binding with the spacified `binding_type`.
///
/// The `binding_type` should be filtering if it uses `FilterMode::Linear`.
///
/// # Example wgsl syntax
/// ```cpp,ignore
/// @group(X) @binding(Y)
/// var i_sampler: sampler;
///
/// // valid usage
/// let pixel = textureSampleLevel(texture, i_sampler, uv, 0.0);
///
/// // invalid: wgsl doesn't allow textureSample in compute stages
/// let pixel = textureSample(texture, i_sample, level);
/// ```
pub fn push_sampler(mut self, sampler: &'a Sampler, binding_type: SamplerBindingType) -> Self {
let binding = Binding::Sampler(SamplerBinding {
resource: wgpu::BindingResource::Sampler(&sampler.handle),
binding_type,
});
self.bindings.push(binding);
self
}
/// Pushes `image` as the last binding.
///
/// # Example wgsl syntax
/// ```cpp,ignore
/// @group(X) @binding(Y)
/// var image: texture_'Nd'<'T'>;
/// // T is the format of the image:
/// // - if it's format ends with Unorm => T is f32
/// // - if it ends with Uint => T is u32
/// // - if it ends with Sint => T is i32
/// ```
pub fn push_image(mut self, image: &'a Image) -> Self {
let dimension = if image.dimension == ImageDimension::D2 {
wgpu::TextureViewDimension::D2
} else {
wgpu::TextureViewDimension::D3
};
let sample_type = image.format.describe().sample_type;
let binding = Binding::Texture(TextureBinding {
dimension,
sample_type,
resource: wgpu::BindingResource::TextureView(&image.view),
});
self.bindings.push(binding);
self
}
/// Pushes an image for storage.
///
/// # Example wgsl syntax
/// ```cpp,ignore
/// @group(X) @binding(Y)
/// var image: texture_storage_2d<rgba8unorm, write>;
/// ```
pub fn push_storage_image(mut self, image: &'a Image, access: StorageImageAccess) -> Self {
let dimension = if image.dimension == ImageDimension::D2 {
wgpu::TextureViewDimension::D2
} else {
wgpu::TextureViewDimension::D3
};
let binding = Binding::StorageTexture(StorageTextureBinding {
access,
dimension,
resource: wgpu::BindingResource::TextureView(&image.view),
format: image.format,
});
self.bindings.push(binding);
self
}
/// Creates a bind group.
pub fn into_bind_group(self) -> BindGroup {
let num_entries = self.bindings.len();
let mut layout_entries = Vec::with_capacity(num_entries);
let mut bind_group_entries = Vec::with_capacity(num_entries);
self.bindings
.into_iter()
.enumerate()
.for_each(|(i, binding)| {
layout_entries.push(wgpu::BindGroupLayoutEntry {
binding: i as u32,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::from(&binding),
count: None,
});
bind_group_entries.push(wgpu::BindGroupEntry {
binding: i as u32,
resource: binding.into_resource(),
})
});
let layout =
self.device
.handle
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Bind group layout"),
entries: &layout_entries,
});
let bind_group = Arc::new(self.device.handle.create_bind_group(
&wgpu::BindGroupDescriptor {
label: Some("Bind group"),
layout: &layout,
entries: &bind_group_entries,
},
));
BindGroup {
layout,
handle: bind_group,
}
}
}
/// Hold the data necesary to set bind groups (a.k.a. descriptor sets) in the Kernel.
///
/// bind groups are created from [`BindGroupLayout`]s.
#[derive(Debug)]
pub struct BindGroup {
pub(crate) layout: wgpu::BindGroupLayout,
pub(crate) handle: Arc<wgpu::BindGroup>,
}