Skip to content

Commit c7bdf1c

Browse files
committed
webassembly, q_rsqrt, and pressure fix
1 parent 344ed23 commit c7bdf1c

File tree

14 files changed

+146
-94
lines changed

14 files changed

+146
-94
lines changed

.github/workflows/web.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ jobs:
3838

3939
- name: Build
4040
run: |
41-
cd crates/fluidsim
42-
wasm-pack build --target web --release --locked
43-
mv pkg ../..
44-
cd ../..
41+
wasm-pack build --target web --release --locked ./crates/fluidsim
42+
mv crates/fluidsim/pkg .
4543
env:
4644
RUSTC_WRAPPER: ""
4745

crates/fluidsim/src/renderer/mod.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,19 @@ impl SimRenderer {
215215
queue.submit(Some(encoder.finish()));
216216
surface_tex.present();
217217

218-
for buf in readback_buffers {
219-
let Some(buf) = buf else { continue };
220-
let p = Arc::clone(&self.perf.perf);
221-
self.compute.pipelines.profile(queue, buf, move |profile| {
222-
*p.lock().unwrap() = profile;
223-
});
224-
}
218+
cfg_if! {
219+
if #[cfg(not(target_arch = "wasm32"))] {
220+
for buf in readback_buffers {
221+
let Some(buf) = buf else { continue };
222+
let p = Arc::clone(&self.perf.perf);
223+
self.compute.pipelines.profile(queue, buf, move |profile| {
224+
*p.lock().unwrap() = profile;
225+
});
226+
}
225227

226-
device.poll(wgpu::PollType::Poll).unwrap();
228+
device.poll(wgpu::PollType::Poll).unwrap();
229+
}
230+
}
227231

228232
Ok(())
229233
}

crates/fluidsim/src/renderer/panel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ impl Panel {
7676

7777
ui.add(Slider::new(&mut sim.smoothing_radius, 0.01..=4.0).text("Smoothing Radius"));
7878

79-
ui.add(Slider::new(&mut sim.target_density, 10.0..=175.0).text("Target Density"));
79+
ui.add(Slider::new(&mut sim.target_density, 0.1..=175.0).text("Target Density"));
8080

8181
ui.add(
82-
Slider::new(&mut sim.pressure_multiplier, 300.0..=700.0)
82+
Slider::new(&mut sim.pressure_multiplier, 1.0..=700.0)
8383
.text("Pressure Multiplier"),
8484
);
8585

crates/fluidsim/src/renderer/shader/buffers.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,13 @@ macro_rules! buffers {
9797
}
9898
}
9999

100+
#[cfg(not(target_arch = "wasm32"))]
100101
pub struct Profiler {
101102
pub query_set: wgpu::QuerySet,
102103
pub query_buffer: Arc<wgpu::Buffer>,
103104
}
104105

106+
#[cfg(not(target_arch = "wasm32"))]
105107
impl Profiler {
106108
fn new(device: &wgpu::Device) -> Self {
107109
let query_set = device.create_query_set(&wgpu::QuerySetDescriptor {
@@ -127,6 +129,7 @@ macro_rules! buffers {
127129
pub struct Buffers {
128130
$(pub $gid: $gty,)+
129131
pub sort: Sort,
132+
#[cfg(not(target_arch = "wasm32"))]
130133
pub profiler: Profiler,
131134
}
132135

@@ -135,6 +138,7 @@ macro_rules! buffers {
135138
Self {
136139
$($gid: $gty::buffers(&device),)+
137140
sort,
141+
#[cfg(not(target_arch = "wasm32"))]
138142
profiler: Profiler::new(device),
139143
}
140144
}

crates/fluidsim/src/renderer/shader/compute.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,29 @@ impl ComputeData {
158158
self.update.mouse = false;
159159
}
160160

161-
Some(self.pipelines.dispatch_all(
162-
device,
163-
encoder,
164-
queue,
165-
&self.buffers,
166-
&self.pass_desc,
167-
self.user.settings.num_particles,
168-
))
161+
cfg_if::cfg_if! {
162+
if #[cfg(not(target_arch = "wasm32"))] {
163+
return Some(self.pipelines.dispatch_all(
164+
device,
165+
encoder,
166+
queue,
167+
&self.buffers,
168+
&self.pass_desc,
169+
self.user.settings.num_particles,
170+
));
171+
} else {
172+
self.pipelines.dispatch_all(
173+
device,
174+
encoder,
175+
queue,
176+
&self.buffers,
177+
&self.pass_desc,
178+
self.user.settings.num_particles,
179+
);
180+
181+
None
182+
}
183+
}
169184
}
170185
}
171186

crates/fluidsim/src/renderer/shader/pipelines.rs

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,48 @@ macro_rules! pipelines {
2020
(@dispatcher $v:ident $i:expr;) => ({});
2121

2222
(@dispatcher $v:ident $i:expr; pre_sort $($entries:ident)*) => {
23-
$v.push((|s, e, _q, b, d, n| {
24-
e.write_timestamp(&b.profiler.query_set, $i);
25-
s.pre_sort.dispatch(e, d, n);
26-
e.write_timestamp(&b.profiler.query_set, $i + 1);
27-
}) as Dispatcher);
28-
29-
$v.push((|s, e, q, b, _d, n| {
30-
e.write_timestamp(&b.profiler.query_set, $i + 2);
31-
s.sorter.sort(e, q, &b.sort.sort_buffers, Some(n));
32-
e.write_timestamp(&b.profiler.query_set, $i + 3);
33-
}) as Dispatcher);
23+
::cfg_if::cfg_if! {
24+
if #[cfg(not(target_arch = "wasm32"))] {
25+
$v.push((|s, e, _q, b, d, n| {
26+
e.write_timestamp(&b.profiler.query_set, $i);
27+
s.pre_sort.dispatch(e, d, n);
28+
e.write_timestamp(&b.profiler.query_set, $i + 1);
29+
}) as Dispatcher);
30+
31+
$v.push((|s, e, q, b, _d, n| {
32+
e.write_timestamp(&b.profiler.query_set, $i + 2);
33+
s.sorter.sort(e, q, &b.sort.sort_buffers, Some(n));
34+
e.write_timestamp(&b.profiler.query_set, $i + 3);
35+
}) as Dispatcher);
36+
} else {
37+
$v.push((|s, e, _q, _b, d, n| {
38+
s.pre_sort.dispatch(e, d, n);
39+
}) as Dispatcher);
40+
41+
$v.push((|s, e, q, b, _d, n| {
42+
s.sorter.sort(e, q, &b.sort.sort_buffers, Some(n));
43+
}) as Dispatcher);
44+
}
45+
}
3446

3547
pipelines!(@dispatcher $v $i + 4; $($entries)*);
3648
};
3749

3850
(@dispatcher $v:ident $i:expr; $entry:ident $($entries:ident)*) => {
39-
$v.push((|s, e, _q, b, d, n| {
40-
e.write_timestamp(&b.profiler.query_set, $i);
41-
s.$entry.dispatch(e, d, n);
42-
e.write_timestamp(&b.profiler.query_set, $i + 1);
43-
}) as Dispatcher);
51+
::cfg_if::cfg_if! {
52+
if #[cfg(not(target_arch = "wasm32"))] {
53+
$v.push((|s, e, _q, b, d, n| {
54+
e.write_timestamp(&b.profiler.query_set, $i);
55+
s.$entry.dispatch(e, d, n);
56+
e.write_timestamp(&b.profiler.query_set, $i + 1);
57+
}) as Dispatcher);
58+
} else {
59+
$v.push((|s, e, _q, _b, d, n| {
60+
s.$entry.dispatch(e, d, n);
61+
}) as Dispatcher);
62+
}
63+
}
64+
4465
pipelines!(@dispatcher $v $i + 2; $($entries)*);
4566
};
4667

@@ -242,6 +263,7 @@ macro_rules! pipelines {
242263
dispatchers.into_iter()
243264
}
244265

266+
#[cfg(not(target_arch = "wasm32"))]
245267
pub fn dispatch_all(
246268
&self,
247269
device: &wgpu::Device,
@@ -280,6 +302,22 @@ macro_rules! pipelines {
280302
readback_buffer
281303
}
282304

305+
#[cfg(target_arch = "wasm32")]
306+
pub fn dispatch_all(
307+
&self,
308+
device: &wgpu::Device,
309+
encoder: &mut wgpu::CommandEncoder,
310+
queue: &wgpu::Queue,
311+
buffers: &Buffers,
312+
descriptor: &wgpu::ComputePassDescriptor<'_>,
313+
num_particles: u32,
314+
) {
315+
for dispatch in Self::iter() {
316+
dispatch(self, encoder, queue, buffers, descriptor, num_particles);
317+
}
318+
}
319+
320+
#[cfg(not(target_arch = "wasm32"))]
283321
pub fn profile(&self, queue: &wgpu::Queue, readback_buffer: wgpu::Buffer, set_profiler: impl FnOnce(ComputeShaderPerformance) + Send + Sync + 'static) {
284322
let period = queue.get_timestamp_period(); // ns/tick
285323

crates/fluidsim/src/renderer/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ impl GameState {
5454
let dtime = now.duration_since(self.time.last_instant).as_secs_f64();
5555
self.time.last_instant = now;
5656

57-
dtime as f32 * self.gfx.speed
57+
(dtime as f32).min(1.0 / 60.0) * self.gfx.speed
5858
}
5959
}

crates/fluidsim/src/renderer/wgpu_state.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl WgpuState {
2424
info!("Initializing renderer");
2525

2626
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
27-
backends: wgpu::Backends::VULKAN,
27+
backends: wgpu::Backends::VULKAN | wgpu::Backends::BROWSER_WEBGPU,
2828
..wgpu::InstanceDescriptor::from_env_or_default()
2929
});
3030

@@ -44,12 +44,11 @@ impl WgpuState {
4444
})
4545
.await?;
4646

47-
let features =
48-
wgpu::Features::TIMESTAMP_QUERY | wgpu::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS;
4947
let (device, queue) = adapter
5048
.request_device(&wgpu::DeviceDescriptor {
5149
label: None,
52-
required_features: features,
50+
required_features: wgpu::Features::TIMESTAMP_QUERY
51+
| wgpu::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS,
5352
#[cfg(not(target_arch = "wasm32"))]
5453
required_limits: wgpu::Limits::default(),
5554
#[cfg(target_arch = "wasm32")]

crates/gpu-shared/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl Default for Settings {
4949
smoothing_radius: 0.60,
5050
target_density: 20.0,
5151

52-
near_pressure_multiplier: 18.0,
52+
near_pressure_multiplier: 50.0,
5353
pressure_multiplier: 500.0,
5454
viscosity_strength: 0.12,
5555

@@ -68,7 +68,7 @@ impl Default for Settings {
6868
num_particles: 30 * 30,
6969

7070
mass: 1.0,
71-
particle_radius: 0.045,
71+
particle_radius: 0.05,
7272

7373
_pad1: 0,
7474
}

crates/physics/src/curves.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn smoothing_deriv(dist: f32, radius: f32) -> f32 {
3434
(dist - radius) * (VOLUME_DERIV / radius.powi(4))
3535
}
3636

37-
pub fn smoothing_near_deriv(dist: f32, radius: f32) -> f32 {
37+
pub fn nsmoothing_deriv(dist: f32, radius: f32) -> f32 {
3838
if dist >= radius || dist == 0.0 {
3939
return 0.0;
4040
}

0 commit comments

Comments
 (0)