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
use std::{borrow::Cow, path::Path};
use crate::{
primitives::{BufOps, ImgOps, PixelInfo},
DescriptorSet, Framework, GpuBuffer, GpuBufferUsage, GpuConstImage, GpuImage, GpuUniformBuffer,
Kernel, Program, Shader,
};
impl<'res> DescriptorSet<'res> {
/// Binds a [`GpuUniformBuffer`] as a uniform buffer in the shader.
///
/// ### Example WGSL syntax:
/// ```ignore
/// struct UniformStruct {
/// a: vec3<u32>;
/// b: vec3<u32>;
/// c: vec3<u32>;
/// };
///
/// [[group(0), binding(0)]]
/// var<uniform> myUniformBuffer: UniformStruct;
/// ```
///
/// ### Example GLSL syntax:
/// ```glsl
/// layout(std140, binding = 0)
/// uniform UniformStruct {
/// uvec3 a;
/// uvec3 b;
/// uvec3 c;
/// };
/// ```
pub fn bind_uniform_buffer<T>(mut self, uniform_buf: &'res GpuUniformBuffer<T>) -> Self
where
T: bytemuck::Pod,
{
let bind_id = self.set_layout.len() as u32;
let bind_entry = wgpu::BindGroupLayoutEntry {
binding: bind_id,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
has_dynamic_offset: false,
min_binding_size: None,
ty: wgpu::BufferBindingType::Uniform,
},
count: None,
};
let bind = wgpu::BindGroupEntry {
binding: bind_id,
resource: uniform_buf.as_binding_resource(),
};
self.set_layout.push(bind_entry);
self.binds.push(bind);
self
}
/// Binds a [`GpuBuffer`] as a storage buffer in the shader with a specific `usage`.
///
/// ### Example WGSL syntax:
/// ```ignore
/// struct StorageStruct {
/// data: [[stride(4)]] array<i32>;
/// };
///
/// [[group(0), binding(0)]]
/// var<storage, read_write> myStorageBuffer: StorageStruct;
/// ```
///
/// ### Example GLSL syntax:
/// ```glsl
/// layout (set=0, binding=0) buffer myStorageBuffer {
/// int data[];
/// };
/// ```
pub fn bind_buffer<T>(mut self, storage_buf: &'res GpuBuffer<T>, usage: GpuBufferUsage) -> Self
where
T: bytemuck::Pod,
{
let bind_id = self.set_layout.len() as u32;
let bind_entry = wgpu::BindGroupLayoutEntry {
binding: bind_id,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
has_dynamic_offset: false,
min_binding_size: None,
ty: wgpu::BufferBindingType::Storage {
read_only: usage == GpuBufferUsage::ReadOnly,
},
},
count: None,
};
let bind = wgpu::BindGroupEntry {
binding: bind_id,
resource: storage_buf.as_binding_resource(),
};
self.set_layout.push(bind_entry);
self.binds.push(bind);
self
}
/// Binds a [`GpuImage`] as a storage image in the shader.
/// This image is write-only.
/// ### Example WGSL syntax:
/// ```ignore
/// [[group(0), binding(0)]]
/// var myStorageImg: texture_storage_2d<rgba8uint, write>;
/// ```
///
/// ### Example GLSL syntax:
/// ```glsl
/// layout (set=0, binding=0, rgba8uint) uimage2D myStorageImg;
/// ```
pub fn bind_image<P: PixelInfo>(mut self, img: &'res GpuImage<P>) -> Self {
let bind_id = self.set_layout.len() as u32;
let bind_entry = wgpu::BindGroupLayoutEntry {
binding: bind_id,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::StorageTexture {
access: wgpu::StorageTextureAccess::WriteOnly,
format: P::wgpu_format(),
view_dimension: wgpu::TextureViewDimension::D2,
},
count: None,
};
let bind = wgpu::BindGroupEntry {
binding: bind_id,
resource: img.as_binding_resource(),
};
self.set_layout.push(bind_entry);
self.binds.push(bind);
self
}
/// Binds a [`GpuConstImage`] as a texture in the shader.
/// This image is read-only.
/// ### Example WGSL syntax:
/// ```ignore
/// [[group(0), binding(0)]]
/// var myTexture: texture_2d<u32>;
/// ```
///
/// ### Example GLSL syntax:
/// ```glsl
/// layout (set=0, binding=0) utexture2D myTexture;
/// ```
pub fn bind_const_image<P>(mut self, img: &'res GpuConstImage<P>) -> Self
where
P: PixelInfo,
{
let bind_id = self.set_layout.len() as u32;
let bind_entry = wgpu::BindGroupLayoutEntry {
binding: bind_id,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Texture {
sample_type: P::wgpu_texture_sample(),
multisampled: false,
view_dimension: wgpu::TextureViewDimension::D2,
},
count: None,
};
let bind = wgpu::BindGroupEntry {
binding: bind_id,
resource: img.as_binding_resource(),
};
self.set_layout.push(bind_entry);
self.binds.push(bind);
self
}
}
impl Shader {
/// Initialises a [`Shader`] from a SPIR-V file.
pub fn from_spirv_file(fw: &Framework, path: impl AsRef<Path>) -> std::io::Result<Self> {
let bytes = std::fs::read(&path)?;
let shader_name = path.as_ref().to_str();
Ok(Self::from_spirv_bytes(fw, &bytes, shader_name))
}
/// Initialises a [`Shader`] from SPIR-V bytes with an optional `name`.
pub fn from_spirv_bytes(fw: &Framework, bytes: &[u8], name: Option<&str>) -> Self {
let source = wgpu::util::make_spirv(bytes);
let shader = fw
.device
.create_shader_module(&wgpu::ShaderModuleDescriptor {
label: name,
source,
});
Self(shader)
}
/// Initialises a [`Shader`] from a `WGSL` file.
pub fn from_wgsl_file(fw: &Framework, path: impl AsRef<Path>) -> std::io::Result<Self> {
let source_string = std::fs::read_to_string(&path)?;
let shader_name = path.as_ref().to_str();
Ok(Self(fw.device.create_shader_module(
&wgpu::ShaderModuleDescriptor {
label: shader_name,
source: wgpu::ShaderSource::Wgsl(Cow::Owned(source_string)),
},
)))
}
}
impl<'sha, 'res> Program<'sha, 'res> {
/// Creates a new [`Program`] using a `shader` and an `entry_point`.
pub fn new(shader: &'sha Shader, entry_point: impl Into<String>) -> Self {
Self {
shader,
entry_point: entry_point.into(),
descriptors: Vec::new(),
}
}
/// Adds a [`DescriptorSet`] to this [`Program`] layout.
pub fn add_descriptor_set(mut self, desc: DescriptorSet<'res>) -> Self {
self.descriptors.push(desc);
self
}
}
impl<'fw> Kernel<'fw> {
/// Creates a [`Kernel`] from a [`Program`].
pub fn new<'sha, 'res>(fw: &'fw Framework, program: Program<'sha, 'res>) -> Self {
let mut layouts = Vec::new();
let mut sets = Vec::new();
// Unwraping of descriptors from program
for desc in &program.descriptors {
let set_layout = fw
.device
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: None,
entries: &desc.set_layout,
});
let set = fw.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &set_layout,
entries: &desc.binds,
});
layouts.push(set_layout);
sets.push(set);
}
// Compute pipeline bindings
let group_layouts = layouts.iter().collect::<Vec<_>>();
let pipeline_layout = fw
.device
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &group_layouts,
push_constant_ranges: &[],
});
let pipeline = fw
.device
.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
label: None,
module: &program.shader.0,
entry_point: &program.entry_point,
layout: Some(&pipeline_layout),
});
Self {
fw,
pipeline,
sets,
entry_point: program.entry_point,
}
}
/// Enqueues the execution of this [`Kernel`] onto the GPU.
///
/// [`Kernel`] will dispatch `x`, `y` and `z` workgroups per dimension.
pub fn enqueue(&self, x: u32, y: u32, z: u32) {
let mut encoder = self
.fw
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("Kernel::enqueue"),
});
{
let mut cpass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("Kernel::enqueue"),
});
cpass.set_pipeline(&self.pipeline);
for (id_set, set) in self.sets.iter().enumerate() {
cpass.set_bind_group(id_set as u32, set, &[]);
}
cpass.insert_debug_marker(&self.entry_point);
cpass.dispatch(x, y, z);
}
self.fw.queue.submit(Some(encoder.finish()));
}
}