Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1#import bevy_hanabi::vfx_common::{
2 ChildInfo, ChildInfoBuffer, EventBuffer, DispatchIndirectArgs, IndirectBuffer,
3 EffectMetadata, RenderGroupIndirect, SimParams, Spawner, DrawIndexedIndirectArgs,
4 seed, tau, pcg_hash, to_float01, frand, frand2, frand3, frand4,
5 rand_uniform_f, rand_uniform_vec2, rand_uniform_vec3, rand_uniform_vec4,
6 rand_normal_f, rand_normal_vec2, rand_normal_vec3, rand_normal_vec4, proj
7}
9struct Particle {
10{{ATTRIBUTES}}
13struct ParticleBuffer {
14 particles: array<Particle>,
17#ifdef READ_PARENT_PARTICLE
19struct ParentParticle {
20 {{PARENT_ATTRIBUTES}}
23struct ParentParticleBuffer {
24 particles: array<ParentParticle>,
27#endif
29{{PROPERTIES}}
31@group(0) @binding(0) var<uniform> sim_params : SimParams;
32@group(0) @binding(1) var<storage, read_write> draw_indirect_buffer : array<DrawIndexedIndirectArgs>;
34// "particle" group @1
35@group(1) @binding(0) var<storage, read_write> particle_buffer : ParticleBuffer;
36@group(1) @binding(1) var<storage, read_write> indirect_buffer : IndirectBuffer;
37#ifdef READ_PARENT_PARTICLE
38@group(1) @binding(2) var<storage, read> parent_particle_buffer : ParentParticleBuffer;
39#endif
41// "spawner" group @2
42@group(2) @binding(0) var<storage, read> spawner : Spawner;
43{{PROPERTIES_BINDING}}
45// "metadata" group @3
46@group(3) @binding(0) var<storage, read_write> effect_metadata : EffectMetadata;
47#ifdef EMITS_GPU_SPAWN_EVENTS
48{{EMIT_EVENT_BUFFER_BINDINGS}}
49#endif
51{{UPDATE_EXTRA}}
53#ifdef EMITS_GPU_SPAWN_EVENTS
54{{EMIT_EVENT_BUFFER_APPEND_FUNCS}}
55#endif
57@compute @workgroup_size(64)
58fn main(@builtin(global_invocation_id) global_invocation_id: vec3<u32>) {
59 let thread_index = global_invocation_id.x;
61 // Cap at maximum number of alive particles.
62 if (thread_index >= effect_metadata.max_update) {
63 return;
64 }
66 let write_index = effect_metadata.indirect_write_index;
67 let read_index = 1u - write_index;
69 let particle_index = indirect_buffer
70 .rows[thread_index]
71 .particle_index[read_index];
73 // Initialize the PRNG seed
74 seed = pcg_hash(particle_index ^ spawner.seed);
76 var particle: Particle = particle_buffer.particles[particle_index];
77 {{AGE_CODE}}
78 {{REAP_CODE}}
79 {{UPDATE_CODE}}
81 {{WRITEBACK_CODE}}
83 // Check if alive
84 if (!is_alive) {
85 // Save dead index
86 let alive_index = atomicSub(&effect_metadata.alive_count, 1u);
87 let dead_index = effect_metadata.capacity - alive_index;
88 indirect_buffer.rows[dead_index].dead_index = particle_index;
90 // Also increment copy of dead count, which was updated in dispatch indirect
91 // pass just before, and need to remain correct after this pass. We wouldn't have
92 // to do that here if we had a per-effect pass between update and the next init.
93 atomicAdd(&effect_metadata.max_spawn, 1u);
94 } else {
95 // Increment visible particle count (in the absence of any GPU culling), and write
96 // the indirection index for later rendering.
97 let indirect_index = atomicAdd(&draw_indirect_buffer[effect_metadata.indirect_render_index].instance_count, 1u);
98 indirect_buffer.rows[indirect_index].particle_index[write_index] = particle_index;
99 }
Contents