-
-
Notifications
You must be signed in to change notification settings - Fork 305
/
particle_sdf.rs
265 lines (233 loc) · 7.38 KB
/
particle_sdf.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
use nannou::prelude::bevy_render::renderer::RenderDevice;
use nannou::prelude::bevy_render::storage::ShaderStorageBuffer;
use nannou::prelude::*;
use std::sync::Arc;
const WORKGROUP_SIZE: u32 = 64;
fn main() {
nannou::app(model)
.compute(compute)
.update(update)
.shader_model::<ShaderModel>()
.run();
}
pub enum Shape {
Circle,
Square,
Triangle,
}
struct Model {
particles: Handle<ShaderStorageBuffer>,
indirect_params: Handle<ShaderStorageBuffer>,
circle_radius: f32,
size: u32,
buffer_size: u32,
scaling_factor: u32,
}
impl Model {
fn material(&self) -> ShaderModel {
ShaderModel {
particles: self.particles.clone(),
}
}
}
// This struct isn't used on the CPU side, but it's necessary to define the layout of the data
// to correctly size the buffer on the GPU side.
#[repr(C)]
#[derive(ShaderType)]
struct Particle {
position: Vec2,
original_position: Vec2,
velocity: Vec2,
energy: f32,
_pad: f32,
color: Vec4,
}
// The draw indirect args struct is used to pass the number of instances to draw to the GPU
// The compute shader will write the number of particles to draw to `instance_count`
#[repr(C)]
#[derive(ShaderType)]
struct DrawIndirectArgs {
pub index_count: u32,
pub instance_count: u32,
pub first_index: u32,
pub base_vertex: i32,
pub first_instance: u32,
}
// Our compute shader has two states, `Init` and `Update`
// The `Init` state is used to initialize the particles
// The `Update` state is used to run the simulation
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
enum State {
Init(u32),
Update(u32),
}
impl Default for State {
fn default() -> Self {
State::Init(1)
}
}
// Compute model, defining uniforms and storage buffers that will be passed to the compute shader
#[derive(AsBindGroup, Clone)]
struct ComputeModel {
#[storage(0, visibility(compute))]
particles: Handle<ShaderStorageBuffer>,
#[uniform(1)]
circle_center: Vec4,
#[uniform(2)]
circle_radius: f32,
#[uniform(3)]
scaling_factor: u32,
#[uniform(4)]
resolution: UVec2,
#[storage(5, visibility(compute))]
indirect_params: Handle<ShaderStorageBuffer>,
}
impl Compute for ComputeModel {
type State = State;
fn shader() -> ShaderRef {
"shaders/particle_sdf_compute.wgsl".into()
}
fn entry(state: &Self::State) -> &'static str {
match state {
State::Init(_) => "init",
State::Update(_) => "update",
}
}
fn dispatch_size(state: &Self::State) -> (u32, u32, u32) {
let size = match state {
State::Init(size) => size,
State::Update(size) => size,
};
((size + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE, 1, 1)
}
}
// Our shader model that will be used to render the particles
#[shader_model(
fragment = "shaders/particle_sdf_material.wgsl",
vertex = "shaders/particle_sdf_material.wgsl"
)]
struct ShaderModel {
#[storage(0, read_only, visibility(vertex))]
particles: Handle<ShaderStorageBuffer>,
}
fn model(app: &App) -> Model {
let _window_id = app
.new_window()
.primary()
.size(1024, 768)
.view(view)
.build();
// Create a buffer to store the particles.
let size = 1; // This will be updated in the `update` function
let particles = create_particle_buffer(app, size);
// Create a buffer store our indirect draw params
let indirect_params = create_indirect_params_buffer(app, size);
Model {
particles,
indirect_params,
circle_radius: 0.5,
size,
buffer_size: size,
scaling_factor: 50,
}
}
// Controls:
// - Arrow keys to change the circle radius
// - Left and right arrow keys to change the scaling factor (i.e. density)
fn update(app: &App, model: &mut Model) {
if app.keys().pressed(KeyCode::ArrowUp) {
model.circle_radius += 0.01;
}
if app.keys().pressed(KeyCode::ArrowDown) {
model.circle_radius -= 0.01;
}
if app.keys().pressed(KeyCode::ArrowRight) {
model.scaling_factor += 1;
}
if app.keys().pressed(KeyCode::ArrowLeft) {
// Don't let the scaling factor go below 20
model.scaling_factor = model.scaling_factor.saturating_sub(1).max(20);
}
let pixels = app.main_window().size_pixels().element_product();
let new_size = pixels / model.scaling_factor.clamp(20, 1000);
// Resize the buffer if necessary
if new_size > model.buffer_size {
model.particles = create_particle_buffer(app, new_size);
model.buffer_size = new_size;
}
model.size = new_size;
}
fn compute(
app: &App,
model: &Model,
previous_state: State,
_view: Entity,
) -> (State, ComputeModel) {
let window = app.main_window();
let window_rect = window.rect();
let mouse_pos = app.mouse();
let mouse_norm = Vec2::new(
mouse_pos.x / window_rect.w() * 2.0,
mouse_pos.y / window_rect.h() * 2.0,
);
let compute_model = ComputeModel {
particles: model.particles.clone(),
circle_center: mouse_norm.extend(1.0).extend(0.0),
circle_radius: model.circle_radius,
scaling_factor: model.scaling_factor,
resolution: window.size_pixels(),
indirect_params: model.indirect_params.clone(),
};
// If the size has changed, we need to re-initialize the particles
// Otherwise, we can just update them
match previous_state {
State::Init(size) => {
// Even if we are in the `Init` state, we still need to update the size of the buffer
// in case the window has been resized
if size != model.size {
(State::Init(model.size), compute_model)
} else {
(State::Update(model.size), compute_model)
}
}
State::Update(size) => {
if size != model.size {
(State::Init(model.size), compute_model)
} else {
(State::Update(model.size), compute_model)
}
}
}
}
fn view(app: &App, model: &Model) {
let draw = app.draw();
draw.background().color(GRAY);
let draw = draw.material(model.material());
draw.indirect()
.primitive(draw.rect().w_h(2.0, 2.0))
.buffer(model.indirect_params.clone());
}
fn create_particle_buffer(app: &App, size: u32) -> Handle<ShaderStorageBuffer> {
let particle_size = Particle::min_size().get() as usize;
let mut particles = ShaderStorageBuffer::with_size(
size as usize * particle_size * 2,
RenderAssetUsages::RENDER_WORLD,
);
particles.buffer_description.label = Some("particles");
particles.buffer_description.usage |= BufferUsages::STORAGE | BufferUsages::VERTEX;
let particles = app.assets_mut().add(particles);
particles
}
fn create_indirect_params_buffer(app: &App, size: u32) -> Handle<ShaderStorageBuffer> {
let mut indirect_params = ShaderStorageBuffer::from(DrawIndirectArgs {
index_count: 6, // Hardcoded for now, 2 triangles
instance_count: size,
first_index: 0,
base_vertex: 0,
first_instance: 0,
});
indirect_params.buffer_description.label = Some("indirect_params");
indirect_params.buffer_description.usage |= BufferUsages::STORAGE | BufferUsages::INDIRECT;
let indirect_params = app.assets_mut().add(indirect_params);
indirect_params
}