1#define_import_path bevy_hanabi::vfx_common3struct SimParams {4 /// Delta time in seconds since last simulation tick.5 delta_time: f32,6 /// Time in seconds since the start of simulation.7 time: f32,8 /// Virtual delta time in seconds since last simulation tick.9 virtual_delta_time: f32,10 /// Virtual time in seconds since the start of simulation.11 virtual_time: f32,12 /// Real delta time in seconds since last simulation tick.13 real_delta_time: f32,14 /// Real time in seconds since the start of simulation.15 real_time: f32,16 /// Total number of effects to update this frame. Used by the indirect17 /// compute pipeline to cap the compute thread to the actual number of18 /// effects to process.19 num_effects: u32,20}22struct Spawner {23 /// Compressed transform of the emitter.24 transform: mat3x4<f32>, // transposed (row-major)25 /// Inverse compressed transform of the emitter.26 inverse_transform: mat3x4<f32>, // transposed (row-major)27 /// Number of particles to spawn this frame, as calculated by the CPU Spawner.28 ///29 /// This is only used if the effect is not a child effect (driven by GPU events).30 spawn: i32,31 /// PRNG seed for this effect instance. Currently this can change each time the32 /// effect is recompiled, and cannot be set deterministically (TODO).33 seed: u32,34 /// Index of the ping-pong buffer of particle indices to read particles from35 /// during rendering. Cached from RenderIndirect::ping after it's swapped36 /// in the indirect dispatch, because the RenderIndirect struct is used by GPU37 /// as an indirect draw source so cannot also be bound as regular storage38 /// buffer for reading.39 render_pong: u32,40 /// Index of the [`GpuEffectMetadata`] for this effect.41 effect_metadata_index: u32,42#ifdef SPAWNER_PADDING43 {{SPAWNER_PADDING}}44#endif45}47const SPAWNER_OFFSET_PONG: u32 = 27u;49struct IndirectBuffer {50 indices: array<u32>,51}53/// A event emitted by another effect during its update pass, to trigger the spawning54/// of one or more particle in this effect.55struct SpawnEvent {56 /// The particle index in the parent effect buffer of the source particle which57 /// triggered the event. This is used to inherit attributes like position or velocity.58 particle_index: u32,59}61/// Append buffer populated during the Update pass of the previous frame by a parent effect,62/// and read back by its child effect(s) during the Init pass of the next frame.63struct EventBuffer {64 /// The spawn events themselves.65 spawn_events: array<SpawnEvent>,66}68/// Info about a single child of a parent effect.69struct ChildInfo {70 /// Index of the effect's IndirectDispatch entry in the global init indirect dispatch array.71 init_indirect_dispatch_index: u32,72 /// Number of events in the associated event buffer.73#ifdef CHILD_INFO_EVENT_COUNT_IS_ATOMIC74 event_count: atomic<i32>,75#else76 event_count: i32,77#endif78}80/// Buffer storing all the ChildInfo structs for several effects.81struct ChildInfoBuffer {82 /// The child info structs themselves.83 rows: array<ChildInfo>,84}86/// Indirect compute dispatch struct for GPU-driven passes. The layout of this struct is dictated by WGSL.87struct IndirectDispatch {88 /// Number of workgroups. Each workgroup has exactly 64 threads.89 x: u32,90 /// Unused; always 1.91 y: u32,92 /// Unused; always 1.93 z: u32,94}96/// Stride in u32 count (4 bytes) of the IndirectDispatch struct.97const DISPATCH_INDIRECT_STRIDE: u32 = 3u;99// Effect metadata offsets. Used when accessing a tightly packed array of EffectMetadata100// as a raw array<u32>, so that we can avoid WGSL struct padding and keep data more compact101// in the GPU buffer. Each offset corresponds to a field in the EffectMetadata struct.102const EM_OFFSET_VERTEX_COUNT: u32 = 0u;103const EM_OFFSET_INSTANCE_COUNT: u32 = 1u;104const EM_OFFSET_FIRST_INDEX_OR_VERTEX_OFFSET: u32 = 2u;105const EM_OFFSET_VERTEX_OFFSET_OR_BASE_INSTANCE: u32 = 3u;106const EM_OFFSET_BASE_INSTANCE: u32 = 4u;107const EM_OFFSET_ALIVE_COUNT: u32 = 5u;108const EM_OFFSET_MAX_UPDATE: u32 = 6u;109const EM_OFFSET_DEAD_COUNT: u32 = 7u;110const EM_OFFSET_MAX_SPAWN: u32 = 8u;111const EM_OFFSET_PING: u32 = 9u;112const EM_OFFSET_INDIRECT_DISPATCH_INDEX: u32 = 10u;114/// Draw indirect parameters for GPU-driven rendering, and additional effect data.115struct EffectMetadata {116 /// Number of vertices in the particle mesh.117 vertex_count: u32,118 /// Number of mesh instances, equal to the number of particles.119 instance_count: atomic<u32>,120 /// First index (if indexed) or vertex offset (if non-indexed).121 first_index_or_vertex_offset: u32,122 /// Vertex offset (if indexed) or base instance (if non-indexed).123 vertex_offset_or_base_instance: i32,124 /// Base instance (if indexed).125 base_instance: u32,127 /// Number of particles alive after the init pass, used to calculate the number128 /// of compute threads to spawn for the update pass and to cap those threads129 /// via `max_update`.130 alive_count: atomic<u32>,131 /// Maximum number of update threads to run. This is cached from `alive_count`132 /// during the indirect dispatch, so that the update compute pass can cap its133 /// thread count while also modifying the actual `alive_count` if some particle134 /// dies during the update pass.135 max_update: u32,136 /// Number of dead particles, decremented during the init pass as new particles137 /// are spawned, and incremented during the update pass as existing particles die.138 dead_count: atomic<u32>,139 /// Maxmimum number of init threads to run on next frame. This is cached from140 /// `dead_count` during the indirect dispatch of the previous frame, so that the141 /// init compute pass can cap its thread count while also decrementing the actual142 /// `dead_count` as particles are spawned.143 max_spawn: atomic<u32>,144 /// Index of the ping buffer for particle indices. Init and update compute passes145 /// always write into the ping buffer and read from the pong buffer. The buffers146 /// are swapped (ping = 1 - ping) during the indirect dispatch.147 ping: u32,148 /// Index of the [`GpuDispatchIndirect`] struct inside the global149 /// [`EffectsMeta::dispatch_indirect_buffer`].150 indirect_dispatch_index: u32,151 /// Index of the [`GpuRenderIndirect`] struct inside the global152 /// [`EffectsMeta::render_group_dispatch_buffer`].153 indirect_render_index: u32,154 /// Offset (in u32 count) of the init indirect dispatch struct inside its155 /// buffer. This avoids having to align those 16-byte structs to the GPU156 /// alignment (at least 32 bytes, even 256 bytes on some).157 init_indirect_dispatch_index: u32,158 /// Index of this effect into its parent's ChildInfo array159 /// ([`EffectChildren::effect_cache_ids`] and its associated GPU160 /// array). This starts at zero for the first child of each effect, and is161 /// only unique per parent, not globally. Only available if this effect is a162 /// child of another effect (i.e. if it has a parent).163 local_child_index: u32,164 /// For children, global index of the ChildInfo into the shared array.165 global_child_index: u32,166 /// For parents, base index of the their first ChildInfo into the shared array.167 base_child_index: u32,169 /// Particle stride, in number of u32.170 particle_stride: u32,171 /// Offset from the particle start to the first sort key, in number of u32.172 sort_key_offset: u32,173 /// Offset from the particle start to the second sort key, in number of u32.174 sort_key2_offset: u32,176 /// Atomic counter incremented each time a particle spawns. Useful for177 /// things like RIBBON_ID or any other use where a unique value is needed.178 /// The value loops back after some time, but unless some particle lives179 /// forever there's little chance of repetition.180 particle_counter: atomic<u32>,182 /// Padding for storage buffer alignment. This struct is sometimes bound as part183 /// of an array, or sometimes individually as a single unit. In the later case,184 /// we need it to be aligned to the GPU limits of the device. That limit is only185 /// known at runtime when initializing the WebGPU device.186 // FIXME - not anymore, but would be again with proper batching, so keep for now187 {{EFFECT_METADATA_PADDING}}188}190/// Stride, in u32 count, between elements of an array<EffectMetadata>.191const EFFECT_METADATA_STRIDE: u32 = {{EFFECT_METADATA_STRIDE}} / 4u;193var<private> seed : u32 = 0u;195const tau: f32 = 6.283185307179586476925286766559;197// Rand: PCG198// https://www.reedbeta.com/blog/hash-functions-for-gpu-rendering/199fn pcg_hash(input: u32) -> u32 {200 var state: u32 = input * 747796405u + 2891336453u;201 var word: u32 = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;202 return (word >> 22u) ^ word;203}205fn to_float01(u: u32) -> f32 {206 // Note: could generate only 24 bits of randomness207 return bitcast<f32>((u & 0x007fffffu) | 0x3f800000u) - 1.;208}210// Random floating-point number in [0:1]211fn frand() -> f32 {212 seed = pcg_hash(seed);213 return to_float01(pcg_hash(seed));214}216// Random floating-point number in [0:1]^2217fn frand2() -> vec2<f32> {218 seed = pcg_hash(seed);219 var x = to_float01(seed);220 seed = pcg_hash(seed);221 var y = to_float01(seed);222 return vec2<f32>(x, y);223}225// Random floating-point number in [0:1]^3226fn frand3() -> vec3<f32> {227 seed = pcg_hash(seed);228 var x = to_float01(seed);229 seed = pcg_hash(seed);230 var y = to_float01(seed);231 seed = pcg_hash(seed);232 var z = to_float01(seed);233 return vec3<f32>(x, y, z);234}236// Random floating-point number in [0:1]^4237fn frand4() -> vec4<f32> {238 // Each rand() produces 32 bits, and we need 24 bits per component,239 // so can get away with only 3 calls.240 var r0 = pcg_hash(seed);241 var r1 = pcg_hash(r0);242 var r2 = pcg_hash(r1);243 seed = r2;244 var x = to_float01(r0);245 var r01 = (r0 & 0xff000000u) >> 8u | (r1 & 0x0000ffffu);246 var y = to_float01(r01);247 var r12 = (r1 & 0xffff0000u) >> 8u | (r2 & 0x000000ffu);248 var z = to_float01(r12);249 var r22 = r2 >> 8u;250 var w = to_float01(r22);251 return vec4<f32>(x, y, z, w);252}254fn rand_uniform_f(a: f32, b: f32) -> f32 {255 return a + frand() * (b - a);256}258fn rand_uniform_vec2(a: vec2<f32>, b: vec2<f32>) -> vec2<f32> {259 return a + frand2() * (b - a);260}262fn rand_uniform_vec3(a: vec3<f32>, b: vec3<f32>) -> vec3<f32> {263 return a + frand3() * (b - a);264}266fn rand_uniform_vec4(a: vec4<f32>, b: vec4<f32>) -> vec4<f32> {267 return a + frand4() * (b - a);268}270// Normal distribution computed using Box-Muller transform271fn rand_normal_f(mean: f32, std_dev: f32) -> f32 {272 var u = frand();273 var v = frand();274 var r = sqrt(-2.0 * log(u));275 return mean + std_dev * r * cos(tau * v);276}278fn rand_normal_vec2(mean: vec2f, std_dev: vec2f) -> vec2f {279 var u = frand();280 var v = frand2();281 var r = sqrt(-2.0 * log(u));282 return mean + std_dev * r * cos(tau * v);283}285fn rand_normal_vec3(mean: vec3f, std_dev: vec3f) -> vec3f {286 var u = frand();287 var v = frand3();288 var r = sqrt(-2.0 * log(u));289 return mean + std_dev * r * cos(tau * v);290}292fn rand_normal_vec4(mean: vec4f, std_dev: vec4f) -> vec4f {293 var u = frand();294 var v = frand4();295 var r = sqrt(-2.0 * log(u));296 return mean + std_dev * r * cos(tau * v);297}299fn proj(u: vec3<f32>, v: vec3<f32>) -> vec3<f32> {300 return dot(v, u) / dot(u,u) * u;301}