gizmo-physics-soft 0.1.5

A custom ECS and physics engine aimed for realistic simulations.
Documentation
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
use std::sync::Arc;

use gizmo_physics_core::components::GpuPhysicsLink;

pub struct GpuCompute {
    pub device: Arc<wgpu::Device>,
    pub queue: Arc<wgpu::Queue>,
    pub compute_pipeline: wgpu::ComputePipeline,
    pub integrate_pipeline: wgpu::ComputePipeline,
    pub bind_group_layout: wgpu::BindGroupLayout,

    pub nodes_buffer: Option<wgpu::Buffer>,
    pub elements_buffer: Option<wgpu::Buffer>,
    pub params_buffer: Option<wgpu::Buffer>,
    pub forces_x_buffer: Option<wgpu::Buffer>,
    pub forces_y_buffer: Option<wgpu::Buffer>,
    pub forces_z_buffer: Option<wgpu::Buffer>,
    pub staging_nodes_buffer: Option<wgpu::Buffer>,

    pub node_capacity: usize,
    pub element_capacity: usize,
}

impl GpuCompute {
    pub async fn new() -> Option<Self> {
        let instance = wgpu::Instance::default();
        let adapter = instance
            .request_adapter(&wgpu::RequestAdapterOptions {
                power_preference: wgpu::PowerPreference::HighPerformance,
                compatible_surface: None,
                force_fallback_adapter: false,
            })
            .await?;

        let (device, queue) = adapter
            .request_device(
                &wgpu::DeviceDescriptor {
                    label: Some("Gizmo Physics Compute Device"),
                    required_features: wgpu::Features::empty(),
                    required_limits: wgpu::Limits::default(),
                },
                None,
            )
            .await
            .ok()?;

        let shader = device.create_shader_module(wgpu::include_wgsl!("soft_body.wgsl"));

        let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("Soft Body Bind Group Layout"),
            entries: &[
                wgpu::BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStages::COMPUTE,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Storage { read_only: false },
                        has_dynamic_offset: false,
                        min_binding_size: None,
                    },
                    count: None,
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 1,
                    visibility: wgpu::ShaderStages::COMPUTE,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Storage { read_only: true },
                        has_dynamic_offset: false,
                        min_binding_size: None,
                    },
                    count: None,
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 2,
                    visibility: wgpu::ShaderStages::COMPUTE,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Uniform,
                        has_dynamic_offset: false,
                        min_binding_size: None,
                    },
                    count: None,
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 3,
                    visibility: wgpu::ShaderStages::COMPUTE,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Storage { read_only: false },
                        has_dynamic_offset: false,
                        min_binding_size: None,
                    },
                    count: None,
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 4,
                    visibility: wgpu::ShaderStages::COMPUTE,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Storage { read_only: false },
                        has_dynamic_offset: false,
                        min_binding_size: None,
                    },
                    count: None,
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 5,
                    visibility: wgpu::ShaderStages::COMPUTE,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Storage { read_only: false },
                        has_dynamic_offset: false,
                        min_binding_size: None,
                    },
                    count: None,
                },
            ],
        });

        let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("Soft Body Pipeline Layout"),
            bind_group_layouts: &[&bind_group_layout],
            push_constant_ranges: &[],
        });

        let compute_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
            label: Some("Soft Body Forces Compute Pipeline"),
            layout: Some(&pipeline_layout),
            module: &shader,
            entry_point: "main",
            compilation_options: Default::default(),
        });

        let integrate_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
            label: Some("Soft Body Integrate Compute Pipeline"),
            layout: Some(&pipeline_layout),
            module: &shader,
            entry_point: "integrate",
            compilation_options: Default::default(),
        });

        Some(Self {
            device: Arc::new(device),
            queue: Arc::new(queue),
            compute_pipeline,
            integrate_pipeline,
            bind_group_layout,
            nodes_buffer: None,
            elements_buffer: None,
            params_buffer: None,
            forces_x_buffer: None,
            forces_y_buffer: None,
            forces_z_buffer: None,
            staging_nodes_buffer: None,
            node_capacity: 0,
            element_capacity: 0,
        })
    }

    pub fn step_soft_bodies(
        &mut self,
        soft_bodies: &mut [(
            gizmo_core::entity::Entity,
            crate::soft_body::SoftBodyMesh,
            crate::components::Transform,
        )],
        rigid_colliders: &[(
            gizmo_core::entity::Entity,
            crate::components::Transform,
            crate::components::Collider,
        )],
        dt: f32,
        gravity: gizmo_math::Vec3,
    ) {
        if soft_bodies.is_empty() {
            return;
        }

        #[cfg(debug_assertions)]
        if soft_bodies
            .iter()
            .any(|(_, sb, _)| sb.mu != soft_bodies[0].1.mu)
        {
            tracing::error!("Warning: Mixed soft body materials not supported on GPU path");
        }

        // --- GPU PATHWAY ---
        // 1. Flatten all nodes and elements
        let mut gpu_nodes = Vec::new();
        let mut gpu_elements = Vec::new();
        let mut node_offsets = Vec::new();

        let mut current_node_offset = 0;
        for (_, sb, _) in soft_bodies.iter() {
            node_offsets.push(current_node_offset);

            for node in &sb.nodes {
                gpu_nodes.push(GpuSoftBodyNode {
                    position: node.position.into(),
                    mass: node.mass,
                    velocity: node.velocity.into(),
                    is_fixed: if node.is_fixed { 1 } else { 0 },
                });
            }

            for elem in &sb.elements {
                gpu_elements.push(GpuTetrahedron {
                    indices: [
                        elem.node_indices[0] + current_node_offset,
                        elem.node_indices[1] + current_node_offset,
                        elem.node_indices[2] + current_node_offset,
                        elem.node_indices[3] + current_node_offset,
                    ],
                    inv_rest_matrix_col0: elem.inv_rest_matrix.x_axis.into(),
                    pad0: 0.0,
                    inv_rest_matrix_col1: elem.inv_rest_matrix.y_axis.into(),
                    pad1: 0.0,
                    inv_rest_matrix_col2: elem.inv_rest_matrix.z_axis.into(),
                    pad2: 0.0,
                    rest_volume: elem.rest_volume,
                    pad3: [0.0; 3],
                });
            }

            current_node_offset = current_node_offset
                .checked_add(sb.nodes.len() as u32)
                .expect("Soft body node offset overflow. Too many nodes!");
        }

        let params = GpuParameters {
            dt,
            mu: soft_bodies[0].1.mu, // We assume uniform material for now
            lambda: soft_bodies[0].1.lambda,
            damping: soft_bodies[0].1.damping,
            gravity: gravity.into(),
            num_elements: gpu_elements.len() as u32,
            num_nodes: gpu_nodes.len() as u32,
            pad0: 0.0,
            pad1: 0.0,
            pad2: 0.0,
        };

        // 2. Create GPU Buffers
        let node_count = gpu_nodes.len();
        let element_count = gpu_elements.len();

        if self.node_capacity < node_count || self.nodes_buffer.is_none() {
            let capacity = node_count.max(self.node_capacity * 2).max(1024);
            self.node_capacity = capacity;

            self.nodes_buffer = Some(self.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("Nodes Buffer"),
                size: (capacity * std::mem::size_of::<GpuSoftBodyNode>()) as u64,
                usage: wgpu::BufferUsages::STORAGE
                    | wgpu::BufferUsages::COPY_DST
                    | wgpu::BufferUsages::COPY_SRC,
                mapped_at_creation: false,
            }));

            self.forces_x_buffer = Some(self.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("Forces X Buffer"),
                size: (capacity * 4) as u64,
                usage: wgpu::BufferUsages::STORAGE
                    | wgpu::BufferUsages::COPY_SRC
                    | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            }));

            self.forces_y_buffer = Some(self.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("Forces Y Buffer"),
                size: (capacity * 4) as u64,
                usage: wgpu::BufferUsages::STORAGE
                    | wgpu::BufferUsages::COPY_SRC
                    | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            }));

            self.forces_z_buffer = Some(self.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("Forces Z Buffer"),
                size: (capacity * 4) as u64,
                usage: wgpu::BufferUsages::STORAGE
                    | wgpu::BufferUsages::COPY_SRC
                    | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            }));

            self.staging_nodes_buffer = Some(self.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("Staging Nodes Buffer"),
                size: (capacity * std::mem::size_of::<GpuSoftBodyNode>()) as u64,
                usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            }));
        }

        if self.element_capacity < element_count || self.elements_buffer.is_none() {
            let capacity = element_count.max(self.element_capacity * 2).max(1024);
            self.element_capacity = capacity;

            self.elements_buffer = Some(self.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("Elements Buffer"),
                size: (capacity * std::mem::size_of::<GpuTetrahedron>()) as u64,
                usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            }));
        }

        if self.params_buffer.is_none() {
            self.params_buffer = Some(self.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("Params Buffer"),
                size: std::mem::size_of::<GpuParameters>() as u64,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            }));
        }

        // Write data to persistent buffers
        self.queue.write_buffer(
            self.nodes_buffer.as_ref().unwrap(),
            0,
            bytemuck::cast_slice(&gpu_nodes),
        );
        self.queue.write_buffer(
            self.elements_buffer.as_ref().unwrap(),
            0,
            bytemuck::cast_slice(&gpu_elements),
        );
        self.queue.write_buffer(
            self.params_buffer.as_ref().unwrap(),
            0,
            bytemuck::cast_slice(&[params]),
        );

        // Clear forces buffers
        let zero_forces = vec![0u8; node_count * 4];
        self.queue
            .write_buffer(self.forces_x_buffer.as_ref().unwrap(), 0, &zero_forces);
        self.queue
            .write_buffer(self.forces_y_buffer.as_ref().unwrap(), 0, &zero_forces);
        self.queue
            .write_buffer(self.forces_z_buffer.as_ref().unwrap(), 0, &zero_forces);

        // 3. Bind Group
        let bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("Soft Body Compute Bind Group"),
            layout: &self.bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: self.nodes_buffer.as_ref().unwrap().as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: self.elements_buffer.as_ref().unwrap().as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: self.params_buffer.as_ref().unwrap().as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: self.forces_x_buffer.as_ref().unwrap().as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 4,
                    resource: self.forces_y_buffer.as_ref().unwrap().as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 5,
                    resource: self.forces_z_buffer.as_ref().unwrap().as_entire_binding(),
                },
            ],
        });

        // 4. Encode Commands
        let mut encoder = self
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
        {
            let mut cpass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: None,
                timestamp_writes: None,
            });
            cpass.set_bind_group(0, &bind_group, &[]);

            // Pass 1: Compute Forces
            cpass.set_pipeline(&self.compute_pipeline);
            let workgroups_forces = params.num_elements.div_ceil(64);
            cpass.dispatch_workgroups(workgroups_forces, 1, 1);

            // Pass 2: Integrate Positions
            cpass.set_pipeline(&self.integrate_pipeline);
            let workgroups_integrate = params.num_nodes.div_ceil(64);
            cpass.dispatch_workgroups(workgroups_integrate, 1, 1);
        }

        // Copy nodes back to CPU for rendering/CPU collision
        let nodes_byte_size = (node_count * std::mem::size_of::<GpuSoftBodyNode>()) as u64;
        encoder.copy_buffer_to_buffer(
            self.nodes_buffer.as_ref().unwrap(),
            0,
            self.staging_nodes_buffer.as_ref().unwrap(),
            0,
            nodes_byte_size,
        );

        self.queue.submit(Some(encoder.finish()));

        // 5. Read back the mapped buffers
        let staging_nodes = self.staging_nodes_buffer.as_ref().unwrap();
        let slice = staging_nodes.slice(0..nodes_byte_size);

        let (tx, rx) = std::sync::mpsc::channel();
        slice.map_async(wgpu::MapMode::Read, move |v| {
            let _ = tx.send(v);
        });

        let device_clone = self.device.clone();
        std::thread::spawn(move || {
            device_clone.poll(wgpu::Maintain::Wait);
        });

        if rx.recv().is_err() {
            return;
        }

        let mapped = slice.get_mapped_range();
        let output_nodes: &[GpuSoftBodyNode] = bytemuck::cast_slice(&mapped);

        // 6. Update CPU models and apply CPU collisions
        for (sb_idx, (_, sb, _)) in soft_bodies.iter_mut().enumerate() {
            let offset = node_offsets[sb_idx] as usize;
            for (i, node) in sb.nodes.iter_mut().enumerate() {
                if node.is_fixed {
                    continue;
                }

                let global_idx = offset + i;
                let out_node = &output_nodes[global_idx];

                let integrated_pos = gizmo_math::Vec3::from(out_node.position);
                let integrated_vel = gizmo_math::Vec3::from(out_node.velocity);

                let (new_pos, new_vel, collided) = crate::soft_body::resolve_node_collision(
                    integrated_pos,
                    integrated_vel,
                    dt,
                    rigid_colliders,
                );

                if collided {
                    node.position = new_pos;
                    node.velocity = new_vel;
                    // Note: This modifies the CPU side. Next substep will upload this fixed position to GPU.
                } else {
                    node.position = integrated_pos;
                    node.velocity = integrated_vel;
                }
            }
        }

        // Unmap
        drop(mapped);
        staging_nodes.unmap();
    }
}

#[repr(C)]
#[derive(Debug, Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub struct GpuSoftBodyNode {
    pub position: [f32; 3],
    pub mass: f32,
    pub velocity: [f32; 3],
    pub is_fixed: u32,
}

#[repr(C)]
#[derive(Debug, Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub struct GpuTetrahedron {
    pub indices: [u32; 4],
    pub inv_rest_matrix_col0: [f32; 3],
    pub pad0: f32,
    pub inv_rest_matrix_col1: [f32; 3],
    pub pad1: f32,
    pub inv_rest_matrix_col2: [f32; 3],
    pub pad2: f32,
    pub rest_volume: f32,
    pub pad3: [f32; 3],
}

#[repr(C)]
#[derive(Debug, Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub struct GpuParameters {
    pub dt: f32,
    pub mu: f32,
    pub lambda: f32,
    pub damping: f32,
    pub gravity: [f32; 3],
    pub num_elements: u32,
    pub num_nodes: u32,
    pub pad0: f32,
    pub pad1: f32,
    pub pad2: f32,
}