-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
smoothvoro.rs
335 lines (329 loc) · 12.1 KB
/
smoothvoro.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
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
use nannou::image;
use nannou::image::{open,RgbaImage,DynamicImage};
use nannou::prelude::*;
use nannou_egui::{self, egui, Egui};
use rfd::FileDialog;
use std::option::Option;
use nannou::wgpu::Texture;
struct Model {
bind_group: wgpu::BindGroup,
render_pipeline: wgpu::RenderPipeline,
vertex_buffer: wgpu::Buffer,
time_uniform: wgpu::Buffer,
time_bind_group: wgpu::BindGroup,
params_uniform: wgpu::Buffer,
params_bind_group: wgpu::BindGroup,
settings:Settings,
egui:Egui,
img: Option<RgbaImage>,
texture: Option<Texture>,
sampler: wgpu::Sampler,
bind_group_layout: wgpu::BindGroupLayout,
}
struct Settings {
lambda: f32,
theta: f32,
alpha:f32,
sigma: f32,
gamma:f32,
blue:f32,
show_ui: bool,
open_file_dialog: bool,
}
#[repr(C)]
#[derive(Clone, Copy)]
struct Vertex {
position: [f32; 2],
}
const VERTICES: [Vertex; 4] = [
Vertex {
position: [-1.0, 1.0],
},
Vertex {
position: [-1.0, -1.0],
},
Vertex {
position: [1.0, 1.0],
},
Vertex {
position: [1.0, -1.0],
},
];
fn main() {
nannou::app(model)
.update(update)
.run();
}
fn update(app: &App, model: &mut Model, update: Update) {
let egui = &mut model.egui;
egui.set_elapsed_time(update.since_start);
let ctx = egui.begin_frame();
if app.keys.down.contains(&Key::H) {
model.settings.show_ui = !model.settings.show_ui;
}
let mut open_file_dialog: bool = model.settings.open_file_dialog;
egui::Window::new("Shader Settings").show(&ctx, |ui| {
if ui.button("Load Image").clicked() {
open_file_dialog = true;
}
ui.add(egui::Slider::new(&mut model.settings.lambda, 1.0..=400.0).text("sample"));
ui.add(egui::Slider::new(&mut model.settings.theta, -1.0..=1.0).text("S"));
ui.add(egui::Slider::new(&mut model.settings.alpha, 0.0..=1.0).text("a"));
ui.add(egui::Slider::new(&mut model.settings.sigma, -1.0..=1.0).text("r"));
ui.add(egui::Slider::new(&mut model.settings.gamma, 0.0..=10.0).text("g"));
ui.add(egui::Slider::new(&mut model.settings.blue, -15.0..=15.0).text("b"));
});
if open_file_dialog {
if let Some(file_path) = FileDialog::new().pick_file() {
if let Ok(img) = open(&file_path).map(|i| i.to_rgba8()) {
let dyn_image = DynamicImage::ImageRgba8(img.clone());
model.img = Some(img);
let main_window = app.main_window();
let device = main_window.device();
let new_texture = Texture::from_image(app, &dyn_image);
model.texture = Some(new_texture);
let new_texture_view = model.texture.as_ref().unwrap().view().build();
model.bind_group = create_bind_group(device, &model.bind_group_layout, &new_texture_view, &model.sampler);
}
model.settings.open_file_dialog = false;
}
}
let params_data = [model.settings.lambda, model.settings.theta, model.settings.alpha, model.settings.sigma, model.settings.gamma, model.settings.blue];
let params_bytes = bytemuck::cast_slice(¶ms_data);
app.main_window().queue().write_buffer(&model.params_uniform, 0, ¶ms_bytes);
}
fn raw_window_event(app: &App, model: &mut Model, event: &nannou::winit::event::WindowEvent) {
model.egui.handle_raw_event(event);
if let nannou::winit::event::WindowEvent::KeyboardInput { input, .. } = event {
if let (Some(nannou::winit::event::VirtualKeyCode::F), true) =
(input.virtual_keycode, input.state == nannou::winit::event::ElementState::Pressed)
{
let window = app.main_window();
let fullscreen = window.fullscreen().is_some();
window.set_fullscreen(!fullscreen);
}
}
}
fn model(app: &App) -> Model {
let w_id = app.new_window().raw_event(raw_window_event).size(800, 600).view(view).build().unwrap();
let window = app.window(w_id).unwrap();
let device = window.device();
let format = Frame::TEXTURE_FORMAT;
let msaa_samples = window.msaa_samples();
let vs_desc = wgpu::include_wgsl!("../shaders/verteximg.wgsl");
let fs_desc = wgpu::include_wgsl!("../shaders/smoothvoro.wgsl");
let vs_mod = device.create_shader_module(vs_desc);
let fs_mod = device.create_shader_module(fs_desc);
let sampler_desc = wgpu::SamplerBuilder::new().into_descriptor();
let sampler_filtering = wgpu::sampler_filtering(&sampler_desc);
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("Texture Sampler"),
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_w: wgpu::AddressMode::ClampToEdge,
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
mipmap_filter: wgpu::FilterMode::Linear,
..Default::default()
});
let settings = Settings {
lambda:50.0,
theta:0.5,
alpha:1.0,
sigma:0.1,
gamma:2.0,
blue:0.05,
show_ui:true,
open_file_dialog:false,
};
let params_data = [settings.lambda, settings.theta, settings.alpha,settings.sigma,settings.gamma,settings.blue];
let params_bytes = bytemuck::cast_slice(¶ms_data);
let params_uniform = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Params Uniform"),
contents: params_bytes,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
let params_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("params_bind_group_layout"),
entries: &[wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new((std::mem::size_of::<f32>() * 6) as _),
},
count: None,
}],
});
let time_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new(std::mem::size_of::<f32>() as _),
},
count: None,
},
],
label: Some("time_bind_group_layout"),
});
let mut dummy_img = RgbaImage::new(800, 600);
dummy_img.put_pixel(0, 0, image::Rgba([255, 255, 255, 255]));
let texture = Texture::from_image(app, &image::DynamicImage::ImageRgba8(dummy_img));
let texture_view = texture.view().build();
let texture_bind_group_layout = create_bind_group_layout(device, wgpu::TextureSampleType::Float { filterable: true }, true);
let bind_group_layout =
create_bind_group_layout(device, texture_view.sample_type(), sampler_filtering);
let bind_group = create_bind_group(device, &bind_group_layout, &texture_view, &sampler);
let pipeline_layout = create_pipeline_layout(device, &[&texture_bind_group_layout, &time_bind_group_layout, ¶ms_bind_group_layout]);
let render_pipeline = create_render_pipeline(
device,
&pipeline_layout,
&vs_mod,
&fs_mod,
format,
msaa_samples,
);
let vertices_bytes = vertices_as_bytes(&VERTICES[..]);
let usage = wgpu::BufferUsages::VERTEX;
let vertex_buffer = device.create_buffer_init(&BufferInitDescriptor {
label: None,
contents: vertices_bytes,
usage,
});
let time_uniform = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Time Uniform Buffer"),
size: std::mem::size_of::<f32>() as wgpu::BufferAddress,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let time_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &time_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: time_uniform.as_entire_binding(),
},
],
label: Some("time_bind_group"),
});
let params_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: ¶ms_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 2,
resource: params_uniform.as_entire_binding(),
},
],
label: Some("params_bind_group"),
});
let window = app.window(w_id).unwrap();
let egui = Egui::from_window(&window);
Model {
params_bind_group,
settings,
params_uniform,
egui,
bind_group,
vertex_buffer,
render_pipeline,
time_uniform,
time_bind_group,
img: None,
texture: Some(texture),
sampler,
bind_group_layout,
}
}
fn view(app: &App, model: &Model, frame: Frame) {
let time = app.time;
let time_bytes = time.to_ne_bytes();
let binding = app.main_window();
let queue = binding.queue();
{
let mut encoder = frame.command_encoder();
queue.write_buffer(&model.time_uniform, 0, &time_bytes);
let mut render_pass = wgpu::RenderPassBuilder::new()
.color_attachment(frame.texture_view(), |color| color)
.begin(&mut encoder);
render_pass.set_bind_group(0, &model.bind_group, &[]);
render_pass.set_pipeline(&model.render_pipeline);
render_pass.set_bind_group(1, &model.time_bind_group, &[]);
render_pass.set_bind_group(2, &model.params_bind_group, &[]);
render_pass.set_vertex_buffer(0, model.vertex_buffer.slice(..));
let vertex_range = 0..VERTICES.len() as u32;
let instance_range = 0..1;
render_pass.draw(vertex_range, instance_range);
}
if model.settings.show_ui {
model.egui.draw_to_frame(&frame).unwrap();
}
if app.keys.down.contains(&Key::Space) {
let file_path = app
.project_path()
.expect("failed to locate project directory")
.join("frames")
.join(format!("{:0}.png", app.elapsed_frames()));
app.main_window().capture_frame(file_path);
}
}
fn create_bind_group_layout(
device: &wgpu::Device,
texture_sample_type: wgpu::TextureSampleType,
sampler_filtering: bool,
) -> wgpu::BindGroupLayout {
wgpu::BindGroupLayoutBuilder::new()
.texture(
wgpu::ShaderStages::FRAGMENT,
false,
wgpu::TextureViewDimension::D2,
texture_sample_type,
)
.sampler(wgpu::ShaderStages::FRAGMENT, sampler_filtering)
.build(device)
}
fn create_bind_group(
device: &wgpu::Device,
layout: &wgpu::BindGroupLayout,
texture: &wgpu::TextureView,
sampler: &wgpu::Sampler,
) -> wgpu::BindGroup {
wgpu::BindGroupBuilder::new()
.texture_view(texture)
.sampler(sampler)
.build(device, layout)
}
fn create_pipeline_layout(
device: &wgpu::Device,
bind_group_layouts: &[&wgpu::BindGroupLayout],
) -> wgpu::PipelineLayout {
let desc = wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts,
push_constant_ranges: &[],
};
device.create_pipeline_layout(&desc)
}
fn create_render_pipeline(
device: &wgpu::Device,
layout: &wgpu::PipelineLayout,
vs_mod: &wgpu::ShaderModule,
fs_mod: &wgpu::ShaderModule,
dst_format: wgpu::TextureFormat,
sample_count: u32,
) -> wgpu::RenderPipeline {
wgpu::RenderPipelineBuilder::from_layout(layout, vs_mod)
.fragment_shader(fs_mod)
.color_format(dst_format)
.add_vertex_buffer::<Vertex>(&wgpu::vertex_attr_array![0 => Float32x2])
.sample_count(sample_count)
.primitive_topology(wgpu::PrimitiveTopology::TriangleStrip)
.build(device)
}
fn vertices_as_bytes(data: &[Vertex]) -> &[u8] {
unsafe { wgpu::bytes::from_slice(data) }
}