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
//! Shader compilation and pipeline management
use crate::{GpuDevice, Result};
use std::borrow::Cow;
use wgpu::{
BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, BindGroupLayoutDescriptor,
BindGroupLayoutEntry, BindingType, BufferBindingType, ComputePipeline,
ComputePipelineDescriptor, PipelineLayoutDescriptor, ShaderModule, ShaderModuleDescriptor,
ShaderStages,
};
/// Shader source type
pub enum ShaderSource<'a> {
/// WGSL source code
Wgsl(Cow<'a, str>),
/// Embedded shader (included at compile time)
Embedded(&'a str),
}
/// Shader compiler and pipeline builder
pub struct ShaderCompiler {
device: std::sync::Arc<wgpu::Device>,
}
impl ShaderCompiler {
/// Create a new shader compiler
#[must_use]
pub fn new(device: &GpuDevice) -> Self {
Self {
device: std::sync::Arc::clone(device.device()),
}
}
/// Compile a shader module from source
///
/// # Arguments
///
/// * `label` - Shader label for debugging
/// * `source` - Shader source code
///
/// # Errors
///
/// Returns an error if shader compilation fails.
pub fn compile(&self, label: &str, source: ShaderSource<'_>) -> Result<ShaderModule> {
let source_str = match source {
ShaderSource::Wgsl(code) => code,
ShaderSource::Embedded(code) => Cow::Borrowed(code),
};
Ok(self.device.create_shader_module(ShaderModuleDescriptor {
label: Some(label),
source: wgpu::ShaderSource::Wgsl(source_str),
}))
}
/// Create a compute pipeline
///
/// # Arguments
///
/// * `label` - Pipeline label for debugging
/// * `shader` - Compiled shader module
/// * `entry_point` - Entry point function name
/// * `bind_group_layout` - Bind group layout for resources
///
/// # Errors
///
/// Returns an error if pipeline creation fails.
pub fn create_pipeline(
&self,
label: &str,
shader: &ShaderModule,
entry_point: &str,
bind_group_layout: &BindGroupLayout,
) -> Result<ComputePipeline> {
let pipeline_layout = self
.device
.create_pipeline_layout(&PipelineLayoutDescriptor {
label: Some(&format!("{label} Layout")),
bind_group_layouts: &[Some(bind_group_layout)],
immediate_size: 0,
});
Ok(self
.device
.create_compute_pipeline(&ComputePipelineDescriptor {
label: Some(label),
layout: Some(&pipeline_layout),
module: shader,
entry_point: Some(entry_point),
cache: None,
compilation_options: Default::default(),
}))
}
/// Create a bind group layout for compute operations
///
/// # Arguments
///
/// * `label` - Layout label for debugging
/// * `entries` - Bind group layout entries
#[must_use]
pub fn create_bind_group_layout(
&self,
label: &str,
entries: &[BindGroupLayoutEntry],
) -> BindGroupLayout {
self.device
.create_bind_group_layout(&BindGroupLayoutDescriptor {
label: Some(label),
entries,
})
}
/// Create a bind group
///
/// # Arguments
///
/// * `label` - Bind group label for debugging
/// * `layout` - Bind group layout
/// * `entries` - Bind group entries
#[must_use]
pub fn create_bind_group(
&self,
label: &str,
layout: &BindGroupLayout,
entries: &[BindGroupEntry<'_>],
) -> BindGroup {
self.device.create_bind_group(&BindGroupDescriptor {
label: Some(label),
layout,
entries,
})
}
}
/// Helper for creating standard bind group layouts
pub struct BindGroupLayoutBuilder {
entries: Vec<BindGroupLayoutEntry>,
}
impl BindGroupLayoutBuilder {
/// Create a new bind group layout builder
#[must_use]
pub fn new() -> Self {
Self {
entries: Vec::new(),
}
}
/// Add a storage buffer binding (read-only)
#[must_use]
pub fn add_storage_buffer_read_only(mut self, binding: u32) -> Self {
self.entries.push(BindGroupLayoutEntry {
binding,
visibility: ShaderStages::COMPUTE,
ty: BindingType::Buffer {
ty: BufferBindingType::Storage { read_only: true },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
});
self
}
/// Add a storage buffer binding (read-write)
#[must_use]
pub fn add_storage_buffer(mut self, binding: u32) -> Self {
self.entries.push(BindGroupLayoutEntry {
binding,
visibility: ShaderStages::COMPUTE,
ty: BindingType::Buffer {
ty: BufferBindingType::Storage { read_only: false },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
});
self
}
/// Add a uniform buffer binding
#[must_use]
pub fn add_uniform_buffer(mut self, binding: u32) -> Self {
self.entries.push(BindGroupLayoutEntry {
binding,
visibility: ShaderStages::COMPUTE,
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
});
self
}
/// Build the bind group layout entries
#[must_use]
pub fn build(self) -> Vec<BindGroupLayoutEntry> {
self.entries
}
}
impl Default for BindGroupLayoutBuilder {
fn default() -> Self {
Self::new()
}
}
/// Precompiled shaders embedded at compile time
pub mod embedded {
/// Color space conversion shader source
pub const COLORSPACE_SHADER: &str = include_str!("shaders/colorspace.wgsl");
/// Image scaling shader source
pub const SCALE_SHADER: &str = include_str!("shaders/scale.wgsl");
/// Convolution filter shader source
pub const FILTER_SHADER: &str = include_str!("shaders/filter.wgsl");
/// Transform operations shader source
pub const TRANSFORM_SHADER: &str = include_str!("shaders/transform.wgsl");
/// Bilateral filter denoising shader source
pub const BILATERAL_SHADER: &str = include_str!("shaders/bilateral.wgsl");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bind_group_layout_builder() {
let layout = BindGroupLayoutBuilder::new()
.add_storage_buffer_read_only(0)
.add_storage_buffer(1)
.add_uniform_buffer(2)
.build();
assert_eq!(layout.len(), 3);
}
}