1use bytemuck::{Pod, Zeroable};
20use std::collections::HashMap;
21use wgpu::util::DeviceExt;
22
23pub const MAX_PARTICLES_PER_SYSTEM: u32 = 100_000;
25
26#[repr(C)]
29#[derive(Clone, Copy, Debug, Pod, Zeroable)]
30pub struct GpuParticle {
31 pub position_life: [f32; 4],
33 pub velocity_max_life: [f32; 4],
35 pub color: [f32; 4],
37 pub size_rotation: [f32; 4],
39}
40
41impl Default for GpuParticle {
42 fn default() -> Self {
43 Self {
44 position_life: [0.0, 0.0, 0.0, 0.0], velocity_max_life: [0.0, 0.0, 0.0, 1.0],
46 color: [1.0, 1.0, 1.0, 1.0],
47 size_rotation: [0.1, 0.1, 0.0, 0.0],
48 }
49 }
50}
51
52#[repr(C)]
54#[derive(Clone, Copy, Debug, Pod, Zeroable)]
55pub struct GpuEmitter {
56 pub position_shape: [f32; 4],
58 pub shape_params: [f32; 4],
60 pub direction_randomness: [f32; 4],
62 pub emission_config: [f32; 4],
64 pub lifetime_speed: [f32; 4],
66 pub size_config: [f32; 4],
68 pub start_color: [f32; 4],
70 pub mid_color: [f32; 4],
72 pub end_color: [f32; 4],
74}
75
76impl Default for GpuEmitter {
77 fn default() -> Self {
78 Self {
79 position_shape: [0.0, 0.0, 0.0, 0.0], shape_params: [0.0; 4],
81 direction_randomness: [0.0, 1.0, 0.0, 0.0], emission_config: [100.0, 0.0, 0.0, 1.0], lifetime_speed: [1.0, 2.0, 1.0, 2.0],
84 size_config: [0.1, 0.2, 0.0, 0.1],
85 start_color: [1.0, 1.0, 1.0, 1.0],
86 mid_color: [1.0, 1.0, 1.0, 0.5],
87 end_color: [1.0, 1.0, 1.0, 0.0],
88 }
89 }
90}
91
92#[repr(C)]
94#[derive(Clone, Copy, Debug, Pod, Zeroable)]
95pub struct GpuForce {
96 pub type_strength: [f32; 4],
99 pub direction_params: [f32; 4],
101}
102
103impl Default for GpuForce {
104 fn default() -> Self {
105 Self {
106 type_strength: [0.0, 0.0, 0.0, 0.0],
107 direction_params: [0.0, -9.8, 0.0, 0.0], }
109 }
110}
111
112#[repr(C)]
114#[derive(Clone, Copy, Debug, Pod, Zeroable)]
115pub struct GpuSimulationUniforms {
116 pub time_config: [f32; 4],
118 pub random_seed: [f32; 4],
120 pub force_config: [f32; 4],
122}
123
124impl Default for GpuSimulationUniforms {
125 fn default() -> Self {
126 Self {
127 time_config: [0.016, 0.0, MAX_PARTICLES_PER_SYSTEM as f32, 0.0],
128 random_seed: [0.0; 4],
129 force_config: [0.0; 4],
130 }
131 }
132}
133
134#[repr(C)]
136#[derive(Clone, Copy, Debug, Pod, Zeroable)]
137pub struct GpuRenderUniforms {
138 pub view_proj: [[f32; 4]; 4],
140 pub camera_pos_fov: [f32; 4],
142 pub camera_right_aspect: [f32; 4],
144 pub camera_up: [f32; 4],
146 pub viewport_config: [f32; 4],
148}
149
150impl Default for GpuRenderUniforms {
151 fn default() -> Self {
152 Self {
153 view_proj: [
154 [1.0, 0.0, 0.0, 0.0],
155 [0.0, 1.0, 0.0, 0.0],
156 [0.0, 0.0, 1.0, 0.0],
157 [0.0, 0.0, 0.0, 1.0],
158 ],
159 camera_pos_fov: [0.0, 0.0, 5.0, 0.8],
160 camera_right_aspect: [1.0, 0.0, 0.0, 1.0],
161 camera_up: [0.0, 1.0, 0.0, 0.0],
162 viewport_config: [800.0, 600.0, 0.0, 0.0],
163 }
164 }
165}
166
167#[derive(Clone, Debug)]
169pub struct ParticleViewport {
170 pub emitter: GpuEmitter,
172 pub forces: Vec<GpuForce>,
174 pub max_particles: u32,
176 pub camera_pos: [f32; 3],
178 pub camera_target: [f32; 3],
180 pub camera_up: [f32; 3],
182 pub fov: f32,
184 pub time: f32,
186 pub delta_time: f32,
188 pub bounds: [f32; 4],
190 pub blend_mode: u32,
192 pub playing: bool,
194}
195
196impl Default for ParticleViewport {
197 fn default() -> Self {
198 Self {
199 emitter: GpuEmitter::default(),
200 forces: Vec::new(),
201 max_particles: 10000,
202 camera_pos: [0.0, 0.0, 5.0],
203 camera_target: [0.0, 0.0, 0.0],
204 camera_up: [0.0, 1.0, 0.0],
205 fov: 0.8,
206 time: 0.0,
207 delta_time: 0.016,
208 bounds: [0.0, 0.0, 800.0, 600.0],
209 blend_mode: 0,
210 playing: true,
211 }
212 }
213}
214
215pub const PARTICLE_COMPUTE_SHADER: &str = r#"
217// ============================================================================
218// Blinc GPU Particle Compute Shader
219// ============================================================================
220
221struct Particle {
222 position_life: vec4<f32>, // xyz=position, w=life remaining
223 velocity_max_life: vec4<f32>, // xyz=velocity, w=max lifetime
224 color: vec4<f32>, // rgba
225 size_rotation: vec4<f32>, // current, start, end, rotation
226}
227
228struct Emitter {
229 position_shape: vec4<f32>, // xyz=position, w=shape type
230 shape_params: vec4<f32>, // shape-specific params
231 direction_randomness: vec4<f32>, // xyz=direction, w=randomness
232 emission_config: vec4<f32>, // rate, burst, spawn_acc, gravity_scale
233 lifetime_speed: vec4<f32>, // min_life, max_life, min_speed, max_speed
234 size_config: vec4<f32>, // start_min, start_max, end_min, end_max
235 start_color: vec4<f32>, // rgba - base (young particles)
236 mid_color: vec4<f32>, // rgba - middle age
237 end_color: vec4<f32>, // rgba - tip (old particles)
238}
239
240struct Force {
241 type_strength: vec4<f32>, // type, strength, 0, 0
242 direction_params: vec4<f32>, // xyz=dir/pos, w=extra
243}
244
245struct SimUniforms {
246 time_config: vec4<f32>, // dt, time, max_particles, active
247 random_seed: vec4<f32>, // 4 random seeds
248 force_config: vec4<f32>, // num_forces, 0, 0, 0
249}
250
251@group(0) @binding(0) var<storage, read_write> particles: array<Particle>;
252@group(0) @binding(1) var<uniform> emitter: Emitter;
253@group(0) @binding(2) var<uniform> uniforms: SimUniforms;
254@group(0) @binding(3) var<storage, read> forces: array<Force>;
255
256// Constants for emitter shapes
257const SHAPE_POINT: u32 = 0u;
258const SHAPE_SPHERE: u32 = 1u;
259const SHAPE_HEMISPHERE: u32 = 2u;
260const SHAPE_CONE: u32 = 3u;
261const SHAPE_BOX: u32 = 4u;
262const SHAPE_CIRCLE: u32 = 5u;
263
264// Constants for force types
265const FORCE_GRAVITY: u32 = 0u;
266const FORCE_WIND: u32 = 1u;
267const FORCE_VORTEX: u32 = 2u;
268const FORCE_DRAG: u32 = 3u;
269const FORCE_TURBULENCE: u32 = 4u;
270const FORCE_ATTRACTOR: u32 = 5u;
271const FORCE_RADIAL: u32 = 6u;
272
273// PCG random number generator
274fn pcg_hash(input: u32) -> u32 {
275 let state = input * 747796405u + 2891336453u;
276 let word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
277 return (word >> 22u) ^ word;
278}
279
280fn random_float(seed: ptr<function, u32>) -> f32 {
281 *seed = pcg_hash(*seed);
282 return f32(*seed) / 4294967295.0;
283}
284
285fn random_range(seed: ptr<function, u32>, min_val: f32, max_val: f32) -> f32 {
286 return min_val + random_float(seed) * (max_val - min_val);
287}
288
289fn random_unit_vector(seed: ptr<function, u32>) -> vec3<f32> {
290 let theta = random_float(seed) * 6.283185;
291 let z = random_range(seed, -1.0, 1.0);
292 let r = sqrt(1.0 - z * z);
293 return vec3<f32>(r * cos(theta), r * sin(theta), z);
294}
295
296// Get spawn position based on emitter shape
297fn get_spawn_position(seed: ptr<function, u32>) -> vec3<f32> {
298 let shape = u32(emitter.position_shape.w);
299 let base_pos = emitter.position_shape.xyz;
300
301 switch (shape) {
302 case SHAPE_POINT: {
303 return base_pos;
304 }
305 case SHAPE_SPHERE: {
306 let radius = emitter.shape_params.x;
307 let dir = random_unit_vector(seed);
308 let r = radius * pow(random_float(seed), 1.0/3.0);
309 return base_pos + dir * r;
310 }
311 case SHAPE_HEMISPHERE: {
312 let radius = emitter.shape_params.x;
313 var dir = random_unit_vector(seed);
314 dir.y = abs(dir.y); // Upper hemisphere
315 let r = radius * pow(random_float(seed), 1.0/3.0);
316 return base_pos + dir * r;
317 }
318 case SHAPE_CONE: {
319 let angle = emitter.shape_params.x;
320 let radius = emitter.shape_params.y;
321 let theta = random_float(seed) * 6.283185;
322 let r = radius * sqrt(random_float(seed));
323 return base_pos + vec3<f32>(r * cos(theta), 0.0, r * sin(theta));
324 }
325 case SHAPE_BOX: {
326 let half = emitter.shape_params.xyz;
327 return base_pos + vec3<f32>(
328 random_range(seed, -half.x, half.x),
329 random_range(seed, -half.y, half.y),
330 random_range(seed, -half.z, half.z)
331 );
332 }
333 case SHAPE_CIRCLE: {
334 let radius = emitter.shape_params.x;
335 let theta = random_float(seed) * 6.283185;
336 let r = radius * sqrt(random_float(seed));
337 return base_pos + vec3<f32>(r * cos(theta), 0.0, r * sin(theta));
338 }
339 default: {
340 return base_pos;
341 }
342 }
343}
344
345// Get spawn velocity based on direction and randomness
346fn get_spawn_velocity(seed: ptr<function, u32>) -> vec3<f32> {
347 let base_dir = normalize(emitter.direction_randomness.xyz);
348 let randomness = emitter.direction_randomness.w;
349
350 // Mix base direction with random direction
351 let random_dir = random_unit_vector(seed);
352 let dir = normalize(mix(base_dir, random_dir, randomness));
353
354 let speed = random_range(seed, emitter.lifetime_speed.z, emitter.lifetime_speed.w);
355 return dir * speed;
356}
357
358// Apply force to velocity
359fn apply_force(force: Force, pos: vec3<f32>, vel: vec3<f32>, dt: f32) -> vec3<f32> {
360 let force_type = u32(force.type_strength.x);
361 let strength = force.type_strength.y;
362 var new_vel = vel;
363
364 switch (force_type) {
365 case FORCE_GRAVITY: {
366 new_vel = vel + force.direction_params.xyz * dt;
367 }
368 case FORCE_WIND: {
369 let turbulence = force.direction_params.w;
370 new_vel = vel + force.direction_params.xyz * strength * dt;
371 }
372 case FORCE_VORTEX: {
373 let axis = normalize(force.direction_params.xyz);
374 let to_particle = pos;
375 let tangent = cross(axis, to_particle);
376 new_vel = vel + normalize(tangent) * strength * dt;
377 }
378 case FORCE_DRAG: {
379 new_vel = vel * (1.0 - strength * dt);
380 }
381 case FORCE_TURBULENCE: {
382 // 3D turbulence with per-particle variation
383 let freq = force.direction_params.w;
384 let t = uniforms.time_config.y;
385
386 // Use velocity magnitude as per-particle seed for variation
387 // This makes each particle get unique turbulence even at same position
388 let vel_seed = dot(vel, vec3<f32>(12.9898, 78.233, 37.719));
389 let particle_offset = fract(sin(vel_seed) * 43758.5453) * 6.28318; // 0 to 2*PI
390
391 // Position-based noise with per-particle phase offset
392 let p1 = pos * freq + vec3<f32>(t * 1.3 + particle_offset, t * 0.7, t * 1.1);
393 let p2 = pos * freq * 1.7 + vec3<f32>(t * 0.9, t * 1.4 + particle_offset, t * 0.6);
394
395 // Multi-octave noise
396 let n1 = sin(p1.x + particle_offset) * cos(p1.y) * sin(p1.z + t);
397 let n2 = cos(p1.y * 1.3 + particle_offset) * sin(p1.z * 0.8) * cos(p1.x + t * 0.7);
398 let n3 = sin(p1.z * 1.1) * cos(p1.x * 0.9 + particle_offset) * sin(p1.y + t * 1.2);
399
400 // Second octave
401 let m1 = sin(p2.x + particle_offset * 0.7) * cos(p2.z) * 0.5;
402 let m2 = cos(p2.y) * sin(p2.x + particle_offset * 1.3) * 0.5;
403 let m3 = sin(p2.z + particle_offset * 0.5) * cos(p2.y) * 0.5;
404
405 // Combine - allow more vertical variation for dancing flames
406 let turb = vec3<f32>(
407 n1 + m1,
408 (n2 + m2) * 0.5,
409 n3 + m3
410 );
411
412 new_vel = vel + turb * strength * dt;
413 }
414 case FORCE_ATTRACTOR: {
415 let attractor_pos = force.direction_params.xyz;
416 let to_attractor = attractor_pos - pos;
417 let dist = max(length(to_attractor), 0.1);
418 new_vel = vel + normalize(to_attractor) * strength / (dist * dist) * dt;
419 }
420 case FORCE_RADIAL: {
421 // Centering force - pulls particles toward the Y-axis (center line)
422 // Negative strength = pull toward center, positive = push away
423 let center = force.direction_params.xyz;
424 let to_center = vec3<f32>(center.x - pos.x, 0.0, center.z - pos.z);
425 let dist = max(length(to_center), 0.01);
426 // Linear falloff - stronger when further from center
427 new_vel = vel + to_center * strength * dt;
428 }
429 default: {}
430 }
431
432 return new_vel;
433}
434
435@compute @workgroup_size(64)
436fn cs_main(@builtin(global_invocation_id) global_id: vec3<u32>) {
437 let idx = global_id.x;
438 let max_particles = u32(uniforms.time_config.z);
439
440 if (idx >= max_particles) {
441 return;
442 }
443
444 var p = particles[idx];
445 let dt = uniforms.time_config.x;
446 let time = uniforms.time_config.y;
447
448 // Initialize random seed based on particle index and time
449 var seed = idx + u32(time * 1000.0);
450 seed = pcg_hash(seed + u32(uniforms.random_seed.x * 1000000.0));
451
452 // Check if particle is alive
453 if (p.position_life.w > 0.0) {
454 // Update particle
455 p.position_life.w -= dt;
456
457 if (p.position_life.w <= 0.0) {
458 // Particle died
459 p.position_life.w = 0.0;
460 } else {
461 // Apply forces
462 var vel = p.velocity_max_life.xyz;
463 let num_forces = u32(uniforms.force_config.x);
464 let gravity_scale = emitter.emission_config.w;
465
466 // Apply forces from the forces array
467 // Note: Gravity is handled via Force::Gravity in the forces array, not hardcoded
468 for (var i = 0u; i < num_forces; i++) {
469 let force = forces[i];
470 // For gravity forces (type 0), apply gravity_scale
471 if (u32(force.type_strength.x) == FORCE_GRAVITY) {
472 let gravity_dir = force.direction_params.xyz * gravity_scale;
473 vel = vel + gravity_dir * dt;
474 } else {
475 vel = apply_force(force, p.position_life.xyz, vel, dt);
476 }
477 }
478
479 p.velocity_max_life = vec4<f32>(vel, p.velocity_max_life.w);
480
481 // Update position
482 p.position_life = vec4<f32>(
483 p.position_life.xyz + vel * dt,
484 p.position_life.w
485 );
486
487 // Update color based on lifetime (3-color gradient)
488 // life_ratio: 1.0 = just born (start), 0.5 = mid-life (mid), 0.0 = dying (end)
489 let life_ratio = p.position_life.w / p.velocity_max_life.w;
490 if (life_ratio > 0.5) {
491 // Young particle: interpolate from start to mid
492 let t = (life_ratio - 0.5) * 2.0; // 0.5->1.0 maps to 0->1
493 p.color = mix(emitter.mid_color, emitter.start_color, t);
494 } else {
495 // Older particle: interpolate from mid to end
496 let t = life_ratio * 2.0; // 0->0.5 maps to 0->1
497 p.color = mix(emitter.end_color, emitter.mid_color, t);
498 }
499
500 // Update size based on lifetime
501 let start_size = p.size_rotation.y;
502 let end_size = p.size_rotation.z;
503 p.size_rotation.x = mix(end_size, start_size, life_ratio);
504 }
505 } else {
506 // Try to spawn new particle
507 let emission_rate = emitter.emission_config.x;
508 let burst_count = emitter.emission_config.y;
509
510 // Calculate spawn chance: combines continuous emission with burst
511 var spawn_chance = 0.0;
512 if (emission_rate > 0.0) {
513 spawn_chance = emission_rate * dt / f32(max_particles);
514 }
515 // For burst effects, use burst_count to determine spawn probability
516 if (burst_count > 0.0) {
517 spawn_chance = spawn_chance + burst_count / f32(max_particles);
518 }
519
520 if (random_float(&seed) < spawn_chance) {
521 // Spawn new particle
522 let pos = get_spawn_position(&seed);
523 let vel = get_spawn_velocity(&seed);
524 let lifetime = random_range(&seed, emitter.lifetime_speed.x, emitter.lifetime_speed.y);
525 let size = random_range(&seed, emitter.size_config.x, emitter.size_config.y);
526 let end_size = random_range(&seed, emitter.size_config.z, emitter.size_config.w);
527
528 p.position_life = vec4<f32>(pos, lifetime);
529 p.velocity_max_life = vec4<f32>(vel, lifetime);
530 p.color = emitter.start_color;
531 p.size_rotation = vec4<f32>(size, size, end_size, 0.0);
532 }
533 }
534
535 particles[idx] = p;
536}
537"#;
538
539pub const PARTICLE_RENDER_SHADER: &str = r#"
541// ============================================================================
542// Blinc GPU Particle Render Shader
543// ============================================================================
544
545struct Particle {
546 position_life: vec4<f32>,
547 velocity_max_life: vec4<f32>,
548 color: vec4<f32>,
549 size_rotation: vec4<f32>,
550}
551
552struct RenderUniforms {
553 view_proj: mat4x4<f32>,
554 camera_pos_fov: vec4<f32>,
555 camera_right_aspect: vec4<f32>,
556 camera_up: vec4<f32>,
557 viewport_config: vec4<f32>,
558}
559
560struct VertexOutput {
561 @builtin(position) position: vec4<f32>,
562 @location(0) uv: vec2<f32>,
563 @location(1) color: vec4<f32>,
564}
565
566@group(0) @binding(0) var<storage, read> particles: array<Particle>;
567@group(0) @binding(1) var<uniform> uniforms: RenderUniforms;
568
569@vertex
570fn vs_main(
571 @builtin(vertex_index) vertex_index: u32,
572 @builtin(instance_index) instance_index: u32,
573) -> VertexOutput {
574 var out: VertexOutput;
575
576 let p = particles[instance_index];
577
578 // Skip dead particles (move to clip space far away)
579 if (p.position_life.w <= 0.0) {
580 out.position = vec4<f32>(0.0, 0.0, 1000.0, 1.0);
581 out.uv = vec2<f32>(0.0);
582 out.color = vec4<f32>(0.0);
583 return out;
584 }
585
586 // Billboard quad vertices
587 let quad_verts = array<vec2<f32>, 6>(
588 vec2<f32>(-1.0, -1.0),
589 vec2<f32>( 1.0, -1.0),
590 vec2<f32>(-1.0, 1.0),
591 vec2<f32>( 1.0, -1.0),
592 vec2<f32>( 1.0, 1.0),
593 vec2<f32>(-1.0, 1.0),
594 );
595
596 let quad_uvs = array<vec2<f32>, 6>(
597 vec2<f32>(0.0, 1.0),
598 vec2<f32>(1.0, 1.0),
599 vec2<f32>(0.0, 0.0),
600 vec2<f32>(1.0, 1.0),
601 vec2<f32>(1.0, 0.0),
602 vec2<f32>(0.0, 0.0),
603 );
604
605 let local_pos = quad_verts[vertex_index];
606 let size = p.size_rotation.x;
607
608 // Calculate billboard orientation
609 let camera_right = uniforms.camera_right_aspect.xyz;
610 let camera_up = uniforms.camera_up.xyz;
611
612 // World position with billboard offset
613 let world_pos = p.position_life.xyz +
614 camera_right * local_pos.x * size +
615 camera_up * local_pos.y * size;
616
617 // Project to clip space
618 out.position = uniforms.view_proj * vec4<f32>(world_pos, 1.0);
619 out.uv = quad_uvs[vertex_index];
620 out.color = p.color;
621
622 return out;
623}
624
625@fragment
626fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
627 // Circular particle with soft edges
628 let center = vec2<f32>(0.5);
629 let dist = length(in.uv - center) * 2.0;
630
631 // Soft circle falloff
632 let alpha = 1.0 - smoothstep(0.8, 1.0, dist);
633
634 // Discard if too far from center
635 if (alpha < 0.01) {
636 discard;
637 }
638
639 return vec4<f32>(in.color.rgb, in.color.a * alpha);
640}
641"#;
642
643#[derive(Debug)]
645pub struct ParticleSystemGpu {
646 particle_buffer: wgpu::Buffer,
648 emitter_buffer: wgpu::Buffer,
650 sim_uniform_buffer: wgpu::Buffer,
652 render_uniform_buffer: wgpu::Buffer,
654 forces_buffer: wgpu::Buffer,
656 compute_pipeline: wgpu::ComputePipeline,
658 render_pipeline: wgpu::RenderPipeline,
660 compute_bind_group: wgpu::BindGroup,
662 render_bind_group: wgpu::BindGroup,
664 max_particles: u32,
666 time: f32,
668}
669
670impl ParticleSystemGpu {
671 pub fn new(
673 device: &wgpu::Device,
674 surface_format: wgpu::TextureFormat,
675 max_particles: u32,
676 ) -> Self {
677 let max_particles = max_particles.min(MAX_PARTICLES_PER_SYSTEM);
678
679 let particle_buffer = device.create_buffer(&wgpu::BufferDescriptor {
681 label: Some("Particle Buffer"),
682 size: (std::mem::size_of::<GpuParticle>() * max_particles as usize) as u64,
683 usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
684 mapped_at_creation: false,
685 });
686
687 let emitter_buffer = device.create_buffer(&wgpu::BufferDescriptor {
689 label: Some("Emitter Buffer"),
690 size: std::mem::size_of::<GpuEmitter>() as u64,
691 usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
692 mapped_at_creation: false,
693 });
694
695 let sim_uniform_buffer = device.create_buffer(&wgpu::BufferDescriptor {
697 label: Some("Simulation Uniforms"),
698 size: std::mem::size_of::<GpuSimulationUniforms>() as u64,
699 usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
700 mapped_at_creation: false,
701 });
702
703 let render_uniform_buffer = device.create_buffer(&wgpu::BufferDescriptor {
705 label: Some("Render Uniforms"),
706 size: std::mem::size_of::<GpuRenderUniforms>() as u64,
707 usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
708 mapped_at_creation: false,
709 });
710
711 let forces_buffer = device.create_buffer(&wgpu::BufferDescriptor {
713 label: Some("Forces Buffer"),
714 size: (std::mem::size_of::<GpuForce>() * 8) as u64,
715 usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
716 mapped_at_creation: false,
717 });
718
719 let compute_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
721 label: Some("Particle Compute Shader"),
722 source: wgpu::ShaderSource::Wgsl(PARTICLE_COMPUTE_SHADER.into()),
723 });
724
725 let render_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
727 label: Some("Particle Render Shader"),
728 source: wgpu::ShaderSource::Wgsl(PARTICLE_RENDER_SHADER.into()),
729 });
730
731 let compute_bind_group_layout =
733 device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
734 label: Some("Particle Compute Bind Group Layout"),
735 entries: &[
736 wgpu::BindGroupLayoutEntry {
738 binding: 0,
739 visibility: wgpu::ShaderStages::COMPUTE,
740 ty: wgpu::BindingType::Buffer {
741 ty: wgpu::BufferBindingType::Storage { read_only: false },
742 has_dynamic_offset: false,
743 min_binding_size: None,
744 },
745 count: None,
746 },
747 wgpu::BindGroupLayoutEntry {
749 binding: 1,
750 visibility: wgpu::ShaderStages::COMPUTE,
751 ty: wgpu::BindingType::Buffer {
752 ty: wgpu::BufferBindingType::Uniform,
753 has_dynamic_offset: false,
754 min_binding_size: None,
755 },
756 count: None,
757 },
758 wgpu::BindGroupLayoutEntry {
760 binding: 2,
761 visibility: wgpu::ShaderStages::COMPUTE,
762 ty: wgpu::BindingType::Buffer {
763 ty: wgpu::BufferBindingType::Uniform,
764 has_dynamic_offset: false,
765 min_binding_size: None,
766 },
767 count: None,
768 },
769 wgpu::BindGroupLayoutEntry {
771 binding: 3,
772 visibility: wgpu::ShaderStages::COMPUTE,
773 ty: wgpu::BindingType::Buffer {
774 ty: wgpu::BufferBindingType::Storage { read_only: true },
775 has_dynamic_offset: false,
776 min_binding_size: None,
777 },
778 count: None,
779 },
780 ],
781 });
782
783 let render_bind_group_layout =
785 device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
786 label: Some("Particle Render Bind Group Layout"),
787 entries: &[
788 wgpu::BindGroupLayoutEntry {
790 binding: 0,
791 visibility: wgpu::ShaderStages::VERTEX,
792 ty: wgpu::BindingType::Buffer {
793 ty: wgpu::BufferBindingType::Storage { read_only: true },
794 has_dynamic_offset: false,
795 min_binding_size: None,
796 },
797 count: None,
798 },
799 wgpu::BindGroupLayoutEntry {
801 binding: 1,
802 visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
803 ty: wgpu::BindingType::Buffer {
804 ty: wgpu::BufferBindingType::Uniform,
805 has_dynamic_offset: false,
806 min_binding_size: None,
807 },
808 count: None,
809 },
810 ],
811 });
812
813 let compute_pipeline_layout =
815 device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
816 label: Some("Particle Compute Pipeline Layout"),
817 bind_group_layouts: &[&compute_bind_group_layout],
818 push_constant_ranges: &[],
819 });
820
821 let compute_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
822 label: Some("Particle Compute Pipeline"),
823 layout: Some(&compute_pipeline_layout),
824 module: &compute_shader,
825 entry_point: Some("cs_main"),
826 compilation_options: Default::default(),
827 cache: None,
828 });
829
830 let render_pipeline_layout =
832 device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
833 label: Some("Particle Render Pipeline Layout"),
834 bind_group_layouts: &[&render_bind_group_layout],
835 push_constant_ranges: &[],
836 });
837
838 let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
839 label: Some("Particle Render Pipeline"),
840 layout: Some(&render_pipeline_layout),
841 vertex: wgpu::VertexState {
842 module: &render_shader,
843 entry_point: Some("vs_main"),
844 buffers: &[],
845 compilation_options: Default::default(),
846 },
847 fragment: Some(wgpu::FragmentState {
848 module: &render_shader,
849 entry_point: Some("fs_main"),
850 targets: &[Some(wgpu::ColorTargetState {
851 format: surface_format,
852 blend: Some(wgpu::BlendState {
853 color: wgpu::BlendComponent {
854 src_factor: wgpu::BlendFactor::SrcAlpha,
855 dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
856 operation: wgpu::BlendOperation::Add,
857 },
858 alpha: wgpu::BlendComponent {
859 src_factor: wgpu::BlendFactor::One,
860 dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
861 operation: wgpu::BlendOperation::Add,
862 },
863 }),
864 write_mask: wgpu::ColorWrites::ALL,
865 })],
866 compilation_options: Default::default(),
867 }),
868 primitive: wgpu::PrimitiveState {
869 topology: wgpu::PrimitiveTopology::TriangleList,
870 strip_index_format: None,
871 front_face: wgpu::FrontFace::Ccw,
872 cull_mode: None, unclipped_depth: false,
874 polygon_mode: wgpu::PolygonMode::Fill,
875 conservative: false,
876 },
877 depth_stencil: None,
878 multisample: wgpu::MultisampleState::default(),
879 multiview: None,
880 cache: None,
881 });
882
883 let compute_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
885 label: Some("Particle Compute Bind Group"),
886 layout: &compute_bind_group_layout,
887 entries: &[
888 wgpu::BindGroupEntry {
889 binding: 0,
890 resource: particle_buffer.as_entire_binding(),
891 },
892 wgpu::BindGroupEntry {
893 binding: 1,
894 resource: emitter_buffer.as_entire_binding(),
895 },
896 wgpu::BindGroupEntry {
897 binding: 2,
898 resource: sim_uniform_buffer.as_entire_binding(),
899 },
900 wgpu::BindGroupEntry {
901 binding: 3,
902 resource: forces_buffer.as_entire_binding(),
903 },
904 ],
905 });
906
907 let render_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
908 label: Some("Particle Render Bind Group"),
909 layout: &render_bind_group_layout,
910 entries: &[
911 wgpu::BindGroupEntry {
912 binding: 0,
913 resource: particle_buffer.as_entire_binding(),
914 },
915 wgpu::BindGroupEntry {
916 binding: 1,
917 resource: render_uniform_buffer.as_entire_binding(),
918 },
919 ],
920 });
921
922 Self {
923 particle_buffer,
924 emitter_buffer,
925 sim_uniform_buffer,
926 render_uniform_buffer,
927 forces_buffer,
928 compute_pipeline,
929 render_pipeline,
930 compute_bind_group,
931 render_bind_group,
932 max_particles,
933 time: 0.0,
934 }
935 }
936
937 pub fn update(
939 &mut self,
940 queue: &wgpu::Queue,
941 encoder: &mut wgpu::CommandEncoder,
942 viewport: &ParticleViewport,
943 ) {
944 if !viewport.playing {
945 return;
946 }
947
948 self.time = viewport.time;
949
950 queue.write_buffer(
952 &self.emitter_buffer,
953 0,
954 bytemuck::bytes_of(&viewport.emitter),
955 );
956
957 let sim_uniforms = GpuSimulationUniforms {
959 time_config: [
960 viewport.delta_time,
961 viewport.time,
962 self.max_particles as f32,
963 0.0,
964 ],
965 random_seed: [
966 viewport.time * 12_345.679,
967 viewport.time * 98_765.43,
968 (viewport.time * 11_111.111).fract(),
969 (viewport.time * 22_222.223).fract(),
970 ],
971 force_config: [viewport.forces.len() as f32, 0.0, 0.0, 0.0],
972 };
973 queue.write_buffer(
974 &self.sim_uniform_buffer,
975 0,
976 bytemuck::bytes_of(&sim_uniforms),
977 );
978
979 let mut forces = [GpuForce::default(); 8];
981 for (i, force) in viewport.forces.iter().take(8).enumerate() {
982 forces[i] = *force;
983 }
984 queue.write_buffer(&self.forces_buffer, 0, bytemuck::cast_slice(&forces));
985
986 let workgroups = self.max_particles.div_ceil(64);
988 let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
989 label: Some("Particle Compute Pass"),
990 timestamp_writes: None,
991 });
992 compute_pass.set_pipeline(&self.compute_pipeline);
993 compute_pass.set_bind_group(0, &self.compute_bind_group, &[]);
994 compute_pass.dispatch_workgroups(workgroups, 1, 1);
995 }
996
997 pub fn render<'a>(
999 &'a self,
1000 queue: &wgpu::Queue,
1001 render_pass: &mut wgpu::RenderPass<'a>,
1002 viewport: &ParticleViewport,
1003 ) {
1004 let view = Self::look_at(
1006 viewport.camera_pos,
1007 viewport.camera_target,
1008 viewport.camera_up,
1009 );
1010 let aspect = viewport.bounds[2] / viewport.bounds[3];
1011 let proj = Self::perspective(viewport.fov, aspect, 0.1, 100.0);
1012 let view_proj = Self::mat4_mul(&proj, &view);
1013
1014 let forward = [
1016 viewport.camera_target[0] - viewport.camera_pos[0],
1017 viewport.camera_target[1] - viewport.camera_pos[1],
1018 viewport.camera_target[2] - viewport.camera_pos[2],
1019 ];
1020 let forward = Self::normalize(forward);
1021 let right = Self::cross(forward, viewport.camera_up);
1022 let right = Self::normalize(right);
1023 let up = Self::cross(right, forward);
1024
1025 let render_uniforms = GpuRenderUniforms {
1027 view_proj,
1028 camera_pos_fov: [
1029 viewport.camera_pos[0],
1030 viewport.camera_pos[1],
1031 viewport.camera_pos[2],
1032 viewport.fov,
1033 ],
1034 camera_right_aspect: [right[0], right[1], right[2], aspect],
1035 camera_up: [up[0], up[1], up[2], 0.0],
1036 viewport_config: [
1037 viewport.bounds[2],
1038 viewport.bounds[3],
1039 0.0, viewport.blend_mode as f32,
1041 ],
1042 };
1043 queue.write_buffer(
1044 &self.render_uniform_buffer,
1045 0,
1046 bytemuck::bytes_of(&render_uniforms),
1047 );
1048
1049 render_pass.set_pipeline(&self.render_pipeline);
1051 render_pass.set_bind_group(0, &self.render_bind_group, &[]);
1052 render_pass.draw(0..6, 0..self.max_particles); }
1054
1055 fn normalize(v: [f32; 3]) -> [f32; 3] {
1057 let len = (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt();
1058 if len > 0.0001 {
1059 [v[0] / len, v[1] / len, v[2] / len]
1060 } else {
1061 [0.0, 1.0, 0.0]
1062 }
1063 }
1064
1065 fn cross(a: [f32; 3], b: [f32; 3]) -> [f32; 3] {
1066 [
1067 a[1] * b[2] - a[2] * b[1],
1068 a[2] * b[0] - a[0] * b[2],
1069 a[0] * b[1] - a[1] * b[0],
1070 ]
1071 }
1072
1073 fn look_at(eye: [f32; 3], target: [f32; 3], up: [f32; 3]) -> [[f32; 4]; 4] {
1074 let f = Self::normalize([target[0] - eye[0], target[1] - eye[1], target[2] - eye[2]]);
1075 let r = Self::normalize(Self::cross(f, up));
1076 let u = Self::cross(r, f);
1077
1078 [
1079 [r[0], u[0], -f[0], 0.0],
1080 [r[1], u[1], -f[1], 0.0],
1081 [r[2], u[2], -f[2], 0.0],
1082 [
1083 -(r[0] * eye[0] + r[1] * eye[1] + r[2] * eye[2]),
1084 -(u[0] * eye[0] + u[1] * eye[1] + u[2] * eye[2]),
1085 f[0] * eye[0] + f[1] * eye[1] + f[2] * eye[2],
1086 1.0,
1087 ],
1088 ]
1089 }
1090
1091 fn perspective(fov: f32, aspect: f32, near: f32, far: f32) -> [[f32; 4]; 4] {
1092 let f = 1.0 / (fov * 0.5).tan();
1093 let nf = 1.0 / (near - far);
1094
1095 [
1096 [f / aspect, 0.0, 0.0, 0.0],
1097 [0.0, f, 0.0, 0.0],
1098 [0.0, 0.0, (far + near) * nf, -1.0],
1099 [0.0, 0.0, 2.0 * far * near * nf, 0.0],
1100 ]
1101 }
1102
1103 fn mat4_mul(a: &[[f32; 4]; 4], b: &[[f32; 4]; 4]) -> [[f32; 4]; 4] {
1104 let mut result = [[0.0f32; 4]; 4];
1105 for i in 0..4 {
1106 for j in 0..4 {
1107 result[i][j] =
1108 a[0][j] * b[i][0] + a[1][j] * b[i][1] + a[2][j] * b[i][2] + a[3][j] * b[i][3];
1109 }
1110 }
1111 result
1112 }
1113}
1114
1115pub struct ParticleManager {
1117 systems: HashMap<u64, ParticleSystemGpu>,
1118 next_id: u64,
1119}
1120
1121impl ParticleManager {
1122 pub fn new() -> Self {
1123 Self {
1124 systems: HashMap::new(),
1125 next_id: 0,
1126 }
1127 }
1128
1129 pub fn create_system(
1130 &mut self,
1131 device: &wgpu::Device,
1132 surface_format: wgpu::TextureFormat,
1133 max_particles: u32,
1134 ) -> u64 {
1135 let id = self.next_id;
1136 self.next_id += 1;
1137 let system = ParticleSystemGpu::new(device, surface_format, max_particles);
1138 self.systems.insert(id, system);
1139 id
1140 }
1141
1142 pub fn get_system(&self, id: u64) -> Option<&ParticleSystemGpu> {
1143 self.systems.get(&id)
1144 }
1145
1146 pub fn get_system_mut(&mut self, id: u64) -> Option<&mut ParticleSystemGpu> {
1147 self.systems.get_mut(&id)
1148 }
1149
1150 pub fn remove_system(&mut self, id: u64) {
1151 self.systems.remove(&id);
1152 }
1153}
1154
1155impl Default for ParticleManager {
1156 fn default() -> Self {
1157 Self::new()
1158 }
1159}