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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use cuneus::compute::{
ComputeShader, PassDescription, StorageBufferSpec, COMPUTE_TEXTURE_FORMAT_RGBA16};
use cuneus::{Core, ExportManager, RenderKit, ShaderControls, ShaderManager};
use log::error;
use cuneus::WindowEvent;
cuneus::uniform_params! {
struct FFTParams {
filter_type: i32,
filter_strength: f32,
filter_direction: f32,
filter_radius: f32,
show_freqs: i32,
resolution: u32,
is_bw: i32,
_padding: u32}
}
struct FFTShader {
base: RenderKit,
compute_shader: ComputeShader,
should_initialize: bool,
current_params: FFTParams, // Store current parameters
}
impl ShaderManager for FFTShader {
fn init(core: &Core) -> Self {
let initial_params = FFTParams {
filter_type: 1,
filter_strength: 0.3,
filter_direction: 0.0,
filter_radius: 3.0,
show_freqs: 0,
resolution: 1024,
is_bw: 0,
_padding: 0};
let base = RenderKit::new(core);
// Define the FFT multi-pass pipeline
let passes = vec![
PassDescription::new("initialize_data", &[]), // Stage 0: Initialize from input texture
PassDescription::new("fft_horizontal", &["initialize_data"]), // Stage 1: FFT horizontal pass
PassDescription::new("fft_vertical", &["fft_horizontal"]), // Stage 2: FFT vertical pass
PassDescription::new("modify_frequencies", &["fft_vertical"]), // Stage 3: Apply frequency domain filters
PassDescription::new("ifft_horizontal", &["modify_frequencies"]), // Stage 4: Inverse FFT horizontal
PassDescription::new("ifft_vertical", &["ifft_horizontal"]), // Stage 5: Inverse FFT vertical
PassDescription::new("main_image", &["ifft_vertical"]), // Stage 6: Final display
];
let config = ComputeShader::builder()
.with_entry_point("initialize_data") // Start with data initialization
.with_multi_pass(&passes)
.with_input_texture() // Re-enable input texture support
.with_custom_uniforms::<FFTParams>()
.with_storage_buffer(StorageBufferSpec::new("image_data", 2048 * 2048 * 3 * 8)) // FFT working memory: max res to avoid crash
.with_workgroup_size([16, 16, 1])
.with_texture_format(COMPUTE_TEXTURE_FORMAT_RGBA16)
.with_label("FFT Multi-Pass")
.build();
let compute_shader = cuneus::compute_shader!(core, "shaders/fft.wgsl", config);
// Initialize custom uniform with initial parameters
compute_shader.set_custom_params(initial_params, &core.queue);
Self {
base,
compute_shader,
should_initialize: true,
current_params: initial_params}
}
fn update(&mut self, core: &Core) {
// Update time
let current_time = self.base.controls.get_time(&self.base.start_time);
let delta = 1.0 / 60.0;
self.compute_shader
.set_time(current_time, delta, &core.queue);
// Update input textures for image proc.
self.base.update_current_texture(core, &core.queue);
if let Some(texture_manager) = self.base.get_current_texture_manager() {
// Update input texture in unified ComputeShader
self.compute_shader.update_input_texture(
&texture_manager.view,
&texture_manager.sampler,
&core.device,
);
}
// Handle export
self.compute_shader.handle_export(core, &mut self.base);
}
fn resize(&mut self, core: &Core) {
self.compute_shader
.resize(core, core.size.width, core.size.height);
}
fn render(&mut self, core: &Core) -> Result<(), cuneus::SurfaceError> {
let mut frame = self.base.begin_frame(core)?;
// Handle UI and controls - using original transparent UI design
let mut params = self.current_params;
let mut changed = false;
let mut should_start_export = false;
let mut export_request = self.base.export_manager.get_ui_request();
let mut controls_request = self
.base
.controls
.get_ui_request(&self.base.start_time, &core.size, self.base.fps_tracker.fps());
let using_video_texture = self.base.using_video_texture;
let using_hdri_texture = self.base.using_hdri_texture;
let using_webcam_texture = self.base.using_webcam_texture;
let video_info = self.base.get_video_info();
let hdri_info = self.base.get_hdri_info();
let webcam_info = self.base.get_webcam_info();
let full_output = if self.base.key_handler.show_ui {
self.base.render_ui(core, |ctx| {
RenderKit::apply_default_style(ctx);
egui::Window::new("fourier workflow")
.collapsible(true)
.resizable(true)
.default_width(250.0)
.show(ctx, |ui| {
// Media controls
ShaderControls::render_media_panel(
ui,
&mut controls_request,
using_video_texture,
video_info,
using_hdri_texture,
hdri_info,
using_webcam_texture,
webcam_info,
);
ui.separator();
egui::CollapsingHeader::new("FFT Settings")
.default_open(false)
.show(ui, |ui| {
ui.label("Resolution:");
ui.horizontal(|ui| {
changed |= ui
.radio_value(&mut params.resolution, 256, "256")
.changed();
changed |= ui
.radio_value(&mut params.resolution, 512, "512")
.changed();
changed |= ui
.radio_value(&mut params.resolution, 1024, "1024")
.changed();
changed |= ui
.radio_value(&mut params.resolution, 2048, "2048")
.changed();
});
if changed {
self.should_initialize = true;
}
ui.separator();
ui.label("View Mode:");
changed |= ui
.radio_value(&mut params.show_freqs, 0, "Filtered")
.changed();
changed |= ui
.radio_value(&mut params.show_freqs, 1, "Frequency Domain")
.changed();
let mut is_bw_bool = params.is_bw != 0;
if ui.checkbox(&mut is_bw_bool, "Black & White").changed() {
params.is_bw = if is_bw_bool { 1 } else { 0 };
changed = true;
}
ui.separator();
});
egui::CollapsingHeader::new("Filter Settings")
.default_open(false)
.show(ui, |ui| {
ui.label("Filter Type:");
// Keep the improved ComboBox as requested
changed |= egui::ComboBox::from_label("")
.selected_text(match params.filter_type {
0 => "LP",
1 => "HP",
2 => "BP",
3 => "Directional",
_ => "None"})
.show_ui(ui, |ui| {
ui.selectable_value(&mut params.filter_type, 0, "LP")
.changed()
|| ui
.selectable_value(&mut params.filter_type, 1, "HP")
.changed()
|| ui
.selectable_value(&mut params.filter_type, 2, "BP")
.changed()
|| ui
.selectable_value(
&mut params.filter_type,
3,
"Directional",
)
.changed()
})
.inner
.unwrap_or(false);
ui.separator();
changed |= ui
.add(
egui::Slider::new(&mut params.filter_strength, 0.0..=1.0)
.text("Filter Strength"),
)
.changed();
if params.filter_type == 2 {
changed |= ui
.add(
egui::Slider::new(
&mut params.filter_radius,
0.0..=6.28,
)
.text("Band Radius"),
)
.changed();
}
if params.filter_type == 3 {
changed |= ui
.add(
egui::Slider::new(
&mut params.filter_direction,
0.0..=6.28,
)
.text("Direction"),
)
.changed();
}
});
ui.separator();
ShaderControls::render_controls_widget(ui, &mut controls_request);
ui.separator();
should_start_export =
ExportManager::render_export_ui_widget(ui, &mut export_request);
});
})
} else {
self.base.render_ui(core, |_ctx| {})
};
// Keep current parameters - don't reset to defaults
// The UI will modify 'params' directly, and we'll apply changes at the end
// Apply controls
self.base.apply_media_requests(core, &controls_request);
// Handle export requests
self.base.export_manager.apply_ui_request(export_request);
if should_start_export {
self.base.export_manager.start_export();
}
if controls_request.load_media_path.is_some() {
self.should_initialize = true;
}
if controls_request.start_webcam {
self.should_initialize = true;
}
// Apply parameter changes
if changed {
self.current_params = params;
self.compute_shader.set_custom_params(params, &core.queue);
self.should_initialize = true; // Trigger FFT reprocessing
}
// FFT dispatch - only run full pipeline when needed, otherwise just display
let mut should_run_full_fft = self.should_initialize
|| self.base.using_video_texture
|| self.base.using_webcam_texture
|| changed; // Also run when parameters change
// FORCE run FFT if there's any texture to debug the issue
let has_any_texture = self.base.get_current_texture_manager().is_some();
if has_any_texture && !should_run_full_fft {
should_run_full_fft = true;
}
// Get FFT resolution for proper workgroup calculation
let n = params.resolution;
if should_run_full_fft {
// Stage 0: Initialize data from input texture (16x16 workgroups)
self.compute_shader.dispatch_stage_with_workgroups(
&mut frame.encoder,
0,
[n.div_ceil(16), n.div_ceil(16), 1],
);
// Stage 1: FFT horizontal (Nx1 workgroups)
self.compute_shader
.dispatch_stage_with_workgroups(&mut frame.encoder, 1, [n, 1, 1]);
// Stage 2: FFT vertical (Nx1 workgroups)
self.compute_shader
.dispatch_stage_with_workgroups(&mut frame.encoder, 2, [n, 1, 1]);
// Stage 3: Modify frequencies - apply filter (16x16 workgroups)
self.compute_shader.dispatch_stage_with_workgroups(
&mut frame.encoder,
3,
[n.div_ceil(16), n.div_ceil(16), 1],
);
if params.show_freqs == 0 {
// Stage 4: Inverse FFT horizontal
self.compute_shader
.dispatch_stage_with_workgroups(&mut frame.encoder, 4, [n, 1, 1]);
// Stage 5: Inverse FFT vertical
self.compute_shader
.dispatch_stage_with_workgroups(&mut frame.encoder, 5, [n, 1, 1]);
}
self.should_initialize = false;
log::info!("Completed full FFT pipeline");
} else {
log::debug!("Skipping full FFT pipeline - using cached result");
}
// Stage 6: Main rendering - always run for display (uses screen size)
self.compute_shader.dispatch_stage(&mut frame.encoder, core, 6);
self.base.renderer.render_to_view(&mut frame.encoder, &frame.view, &self.compute_shader.get_output_texture().bind_group);
self.base.end_frame(core, frame, full_output);
Ok(())
}
fn handle_input(&mut self, core: &Core, event: &WindowEvent) -> bool {
if let WindowEvent::DroppedFile(path) = event {
if let Err(e) = self.base.load_media(core, path) {
error!("Failed to load dropped file: {e:?}");
} else {
self.should_initialize = true;
}
return true;
}
self.base.default_handle_input(core, event)
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
cuneus::gst::init()?;
env_logger::init();
let (app, event_loop) = cuneus::ShaderApp::new("FFT", 800, 600);
app.run(event_loop, FFTShader::init)
}