Skip to main content

dreamwell_engine/physics/
gpu_types.rs

1// GPU-uploadable physics types — #[repr(C)] structs for CPU↔GPU contract.
2// Gated behind `physics-gpu-types` feature (bytemuck derive).
3// All structs are 32 bytes for uniform buffer alignment.
4
5/// Physics simulation config for GPU upload (32 bytes).
6#[repr(C)]
7#[derive(Debug, Clone, Copy, PartialEq)]
8#[cfg_attr(feature = "physics-gpu-types", derive(bytemuck::Pod, bytemuck::Zeroable))]
9pub struct GpuPhysicsConfig {
10    pub gravity: [f32; 3],
11    pub damping: f32,
12    pub max_speed: f32,
13    pub restitution: f32,
14    pub _pad: [f32; 2],
15}
16
17/// Collision profile for GPU upload (32 bytes).
18#[repr(C)]
19#[derive(Debug, Clone, Copy, PartialEq)]
20#[cfg_attr(feature = "physics-gpu-types", derive(bytemuck::Pod, bytemuck::Zeroable))]
21pub struct GpuCollisionProfile {
22    pub center_offset: [f32; 3],
23    pub radius: f32,
24    pub layer_mask: u32,
25    pub _pad: [u32; 3],
26}
27
28/// Observer context for GPU upload (32 bytes).
29#[repr(C)]
30#[derive(Debug, Clone, Copy, PartialEq)]
31#[cfg_attr(feature = "physics-gpu-types", derive(bytemuck::Pod, bytemuck::Zeroable))]
32pub struct GpuObserverConfig {
33    pub position: [f32; 3],
34    pub fov_radius_cells: f32,
35    pub topology_layer: u32,
36    pub _pad: [u32; 3],
37}
38
39/// Particle spawn configuration for GPU upload (32 bytes).
40#[repr(C)]
41#[derive(Debug, Clone, Copy, PartialEq)]
42#[cfg_attr(feature = "physics-gpu-types", derive(bytemuck::Pod, bytemuck::Zeroable))]
43pub struct GpuParticleSpawnConfig {
44    pub emission_rate: f32,
45    pub lifetime_min: f32,
46    pub lifetime_max: f32,
47    pub speed_min: f32,
48    pub speed_max: f32,
49    pub _pad: [f32; 3],
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn gpu_physics_config_size() {
58        assert_eq!(std::mem::size_of::<GpuPhysicsConfig>(), 32);
59    }
60
61    #[test]
62    fn gpu_collision_profile_size() {
63        assert_eq!(std::mem::size_of::<GpuCollisionProfile>(), 32);
64    }
65
66    #[test]
67    fn gpu_observer_config_size() {
68        assert_eq!(std::mem::size_of::<GpuObserverConfig>(), 32);
69    }
70
71    #[test]
72    fn gpu_particle_spawn_config_size() {
73        assert_eq!(std::mem::size_of::<GpuParticleSpawnConfig>(), 32);
74    }
75
76    #[test]
77    fn gpu_physics_config_default_values() {
78        let config = GpuPhysicsConfig {
79            gravity: [0.0, -9.81, 0.0],
80            damping: 0.99,
81            max_speed: 100.0,
82            restitution: 0.5,
83            _pad: [0.0; 2],
84        };
85        assert_eq!(config.gravity[1], -9.81);
86    }
87
88    #[test]
89    fn gpu_observer_config_layer_index() {
90        let config = GpuObserverConfig {
91            position: [0.0; 3],
92            fov_radius_cells: 10.0,
93            topology_layer: 6, // Area
94            _pad: [0; 3],
95        };
96        assert_eq!(config.topology_layer, 6);
97    }
98}