Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

310 KB
Raw Blame
use std::{
    borrow::Cow,
    hash::{DefaultHasher, Hash, Hasher},
    marker::PhantomData,
    num::{NonZeroU32, NonZeroU64},
    ops::{Deref, DerefMut, Range},
    time::Duration,
    vec,
};

#[cfg(feature = "2d")]
use bevy::core_pipeline::core_2d::{Transparent2d, CORE_2D_DEPTH_FORMAT};
#[cfg(feature = "2d")]
use bevy::math::FloatOrd;
use bevy::{
    camera::primitives::Frustum,
    ecs::{
        change_detection::Tick,
        prelude::*,
        system::{lifetimeless::*, SystemParam, SystemState},
    },
    log::trace,
    mesh::MeshVertexBufferLayoutRef,
    platform::collections::HashMap,
    prelude::*,
    render::{
        mesh::{allocator::MeshAllocator, RenderMesh, RenderMeshBufferInfo},
        render_asset::RenderAssets,
        render_phase::{
            Draw, DrawError, DrawFunctions, PhaseItemExtraIndex, SortedPhaseItem,
            TrackedRenderPass, ViewSortedRenderPhases,
        },
        render_resource::{
            binding_types::{
                storage_buffer, storage_buffer_read_only, storage_buffer_read_only_sized,
                storage_buffer_sized, uniform_buffer,
            },
            *,
        },
        renderer::{RenderContext, RenderDevice, RenderGraph, RenderGraphSystems, RenderQueue},
        sync_world::{MainEntity, RenderEntity, TemporaryRenderEntity},
        texture::GpuImage,
        view::{
            ExtractedView, RenderVisibleEntities, ViewUniform, ViewUniformOffset, ViewUniforms,
        },
        Extract, MainWorld,
    },
};
#[cfg(feature = "3d")]
use bevy::{
    core_pipeline::{
        core_3d::{
            AlphaMask3d, Opaque3d, Opaque3dBatchSetKey, Opaque3dBinKey, Transparent3d,
            CORE_3D_DEPTH_FORMAT,
        },
        prepass::{OpaqueNoLightmap3dBatchSetKey, OpaqueNoLightmap3dBinKey},
    },
    render::{
        mesh::allocator::MeshSlabs,
        render_phase::{BinnedPhaseItem, ViewBinnedRenderPhases},
    },
};
use bitflags::bitflags;
use bytemuck::{Pod, Zeroable};
use effect_cache::{CachedEffect, EffectSlice, SlabState};
use event::{CachedChildInfo, CachedEffectEvents, CachedParentInfo, GpuChildInfo};
use fixedbitset::FixedBitSet;
use gpu_buffer::GpuBuffer;

use crate::{
    asset::{DefaultMesh, EffectAsset},
    calc_func_id,
    render::{
        batch::{BatchInput, EffectDrawBatch, EffectSorter, InitAndUpdatePipelineIds},
        effect_cache::{AnyDrawIndirectArgs, CachedDrawIndirectArgs, SlabId},
    },
    AlphaMode, Attribute, CompiledParticleEffect, EffectProperties, EffectShaders,
    EffectSimulation, EffectSpawner, EffectVisibilityClass, HanabiMainCamera, ParticleLayout,
    PropertyLayout, SimulationCondition, TextureLayout,
};

mod aligned_buffer_vec;
mod batch;
mod buffer_table;
mod effect_cache;
mod event;
mod gpu_buffer;
#[cfg(test)]
mod headless_batching_tests;
mod property;
mod shader_cache;
#[cfg(all(test, feature = "gpu_tests"))]
mod shader_contract_tests;
mod sort;

use aligned_buffer_vec::AlignedBufferVec;
use batch::BatchSpawnInfo;
pub(crate) use batch::Batcher;
use buffer_table::{BufferTable, BufferTableId};
pub(crate) use effect_cache::EffectCache;
pub(crate) use event::{allocate_events, on_remove_cached_effect_events, EventCache};
pub(crate) use property::{
    allocate_properties, on_remove_cached_properties, prepare_property_buffers, PropertyBindGroups,
    PropertyCache,
};
use property::{CachedEffectProperties, PropertyBindGroupKey};
pub use shader_cache::ShaderCache;
pub(crate) use sort::SortBindGroups;

use self::batch::EffectBatch;

/// Plugin to add systems related to Hanabi rendering.
#[derive(Debug, Clone, Copy)]
pub struct HanabiRenderPlugin;

impl Plugin for HanabiRenderPlugin {
    fn name(&self) -> &str {
        "hanabi:render"
    }

    fn build(&self, app: &mut App) {
        let Some(render_app) = app.get_sub_app_mut(bevy::render::RenderApp) else {
            return;
        };

        render_app.add_systems(
            RenderGraph,
            simulate
                .in_set(RenderGraphSystems::Render)
                .before(bevy::core_pipeline::schedule::camera_driver),
        );
    }
}

/// GPU representation of a single entry in the indirect index buffer of
/// particles.
#[repr(C)]
#[derive(Debug, Copy, Clone, Pod, Zeroable, ShaderType)]
struct GpuIndirectIndex {
    /// Ping-pong indirection index for particles (ping).
    pub ping: u32,
    /// Ping-pong indirection index for particles (pong).
    pub pong: u32,
    /// Indirection index for dead particles, for recycling.
    pub dead: u32,
}

/// Helper to calculate a hash of a given hashable value.
fn calc_hash<H: Hash>(value: &H) -> u64 {
    let mut hasher = DefaultHasher::default();
    value.hash(&mut hasher);
    hasher.finish()
}

/// Source data (buffer and range inside the buffer) to create a buffer binding.
#[derive(Debug, Clone)]
pub(crate) struct BufferBindingSource {
    buffer: Buffer,
    offset: u32,
    size: NonZeroU32,
}

impl BufferBindingSource {
    /// Get a binding over the source data.
    pub fn as_binding(&self) -> BindingResource<'_> {
        BindingResource::Buffer(BufferBinding {
            buffer: &self.buffer,
            offset: self.offset as u64 * 4,
            size: Some(self.size.into()),
        })
    }
}

impl PartialEq for BufferBindingSource {
    fn eq(&self, other: &Self) -> bool {
        self.buffer.id() == other.buffer.id()
            && self.offset == other.offset
            && self.size == other.size
    }
}

impl<'a> From<&'a BufferBindingSource> for BufferBinding<'a> {
    fn from(value: &'a BufferBindingSource) -> Self {
        BufferBinding {
            buffer: &value.buffer,
            offset: value.offset as u64,
            size: Some(value.size.into()),
        }
    }
}

/// Simulation parameters, available to all shaders of all effects.
#[derive(Debug, Default, Clone, Copy, Resource)]
pub(crate) struct SimParams {
    /// Current effect system simulation time since startup, in seconds.
    /// This is based on the [`Time<EffectSimulation>`](EffectSimulation) clock.
    time: f64,
    /// Delta time, in seconds, since last effect system update.
    delta_time: f32,

    /// Current virtual time since startup, in seconds.
    /// This is based on the [`Time<Virtual>`](Virtual) clock.
    virtual_time: f64,
    /// Virtual delta time, in seconds, since last effect system update.
    virtual_delta_time: f32,

    /// Current real time since startup, in seconds.
    /// This is based on the [`Time<Real>`](Real) clock.
    real_time: f64,
    /// Real delta time, in seconds, since last effect system update.
    real_delta_time: f32,
}

/// GPU representation of [`SimParams`], as well as additional per-frame
/// effect-independent values.
#[repr(C)]
#[repr(align(16))] // vec4<f32>
#[derive(Debug, Copy, Clone, Pod, Zeroable, ShaderType)]
struct GpuSimParams {
    /// Main view/camera frustum planes, for things like frustum culling. Each
    /// plane is encoded as (normal, distance) over 4 components, in world
    /// space.
    frustum: [Vec4; 6],
    /// World space position of the main view/camera, for things like
    /// distance-based sorting. The W component is unused (but required for
    /// padding in WGSL).
    camera_position: Vec4,
    /// Delta time, in seconds, since last effect system update.
    delta_time: f32,
    /// Current effect system simulation time since startup, in seconds.
    ///
    /// This is a lower-precision variant of [`SimParams::time`].
    time: f32,
    /// Virtual delta time, in seconds, since last effect system update.
    virtual_delta_time: f32,
    /// Current virtual time since startup, in seconds.
    ///
    /// This is a lower-precision variant of [`SimParams::time`].
    virtual_time: f32,
    /// Real delta time, in seconds, since last effect system update.
    real_delta_time: f32,
    /// Current real time since startup, in seconds.
    ///
    /// This is a lower-precision variant of [`SimParams::time`].
    real_time: f32,
    /// Total number of effects to update this frame. Used by the indirect
    /// compute pipeline to cap the compute thread to the actual number of
    /// effects to process.
    ///
    /// This is only used by the `vfx_indirect` compute shader.
    num_effects: u32,

    pad0: u32,
}

impl Default for GpuSimParams {
    fn default() -> Self {
        Self {
            frustum: [Vec4::ZERO; 6],
            camera_position: Vec4::ZERO,
            delta_time: 0.04,
            time: 0.0,
            virtual_delta_time: 0.04,
            virtual_time: 0.0,
            real_delta_time: 0.04,
            real_time: 0.0,
            num_effects: 0,
            pad0: 0,
        }
    }
}

impl From<SimParams> for GpuSimParams {
    #[inline]
    fn from(src: SimParams) -> Self {
        Self::from(&src)
    }
}

impl From<&SimParams> for GpuSimParams {
    fn from(src: &SimParams) -> Self {
        Self {
            delta_time: src.delta_time,
            time: src.time as f32,
            virtual_delta_time: src.virtual_delta_time,
            virtual_time: src.virtual_time as f32,
            real_delta_time: src.real_delta_time,
            real_time: src.real_time as f32,
            ..default()
        }
    }
}

/// Compressed representation of a transform for GPU transfer.
///
/// The transform is stored as the three first rows of a transposed [`Mat4`],
/// assuming the last row is the unit row [`Vec4::W`]. The transposing ensures
/// that the three values are [`Vec4`] types which are naturally aligned and
/// without padding when used in WGSL. Without this, storing only the first
/// three components of each column would introduce padding, and would use the
/// same storage size on GPU as a full [`Mat4`].
#[repr(C)]
#[repr(align(16))] // alignof(mat3x4)
#[derive(Debug, Default, Clone, Copy, Pod, Zeroable, ShaderType)]
pub(crate) struct GpuCompressedTransform {
    pub x_row: [f32; 4],
    pub y_row: [f32; 4],
    pub z_row: [f32; 4],
}

impl From<Mat4> for GpuCompressedTransform {
    fn from(value: Mat4) -> Self {
        let tr = value.transpose();
        #[cfg(test)]
        crate::test_utils::assert_approx_eq!(tr.w_axis, Vec4::W);
        Self {
            x_row: tr.x_axis.to_array(),
            y_row: tr.y_axis.to_array(),
            z_row: tr.z_axis.to_array(),
        }
    }
}

impl From<&Mat4> for GpuCompressedTransform {
    fn from(value: &Mat4) -> Self {
        let tr = value.transpose();
        #[cfg(test)]
        crate::test_utils::assert_approx_eq!(tr.w_axis, Vec4::W);
        Self {
            x_row: tr.x_axis.to_array(),
            y_row: tr.y_axis.to_array(),
            z_row: tr.z_axis.to_array(),
        }
    }
}

impl GpuCompressedTransform {
    /// Returns the translation as represented by this transform.
    #[allow(dead_code)]
    pub fn translation(&self) -> Vec3 {
        Vec3 {
            x: self.x_row[3],
            y: self.y_row[3],
            z: self.z_row[3],
        }
    }
}

/// Extension trait for shader types stored in a WGSL storage buffer.
pub(crate) trait StorageType {
    /// Get the aligned size, in bytes, of this type such that it aligns to the
    /// given alignment, in bytes.
    ///
    /// This is mainly used to align GPU types to device requirements.
    fn aligned_size(alignment: u32) -> NonZeroU64;

    /// Get the WGSL padding code to append to the GPU struct to align it.
    ///
    /// This is useful if the struct needs to be bound directly with a dynamic
    /// bind group offset, which requires the offset to be a multiple of a GPU
    /// device specific alignment value.
    fn padding_code(alignment: u32) -> String;
}

impl<T: ShaderType> StorageType for T {
    fn aligned_size(alignment: u32) -> NonZeroU64 {
        NonZeroU64::new(T::min_size().get().next_multiple_of(alignment as u64)).unwrap()
    }

    fn padding_code(alignment: u32) -> String {
        let aligned_size = T::aligned_size(alignment);
        trace!(
            "Aligning {} to {} bytes as device limits requires. Orignal size: {} bytes. Aligned size: {} bytes.",
            std::any::type_name::<T>(),
            alignment,
            T::min_size().get(),
            aligned_size
        );

        // We need to pad the Spawner WGSL struct based on the device padding so that we
        // can use it as an array element but also has a direct struct binding.
        if T::min_size() != aligned_size {
            let padding_size = aligned_size.get() - T::min_size().get();
            assert!(padding_size % 4 == 0);
            format!("padding: array<u32, {}>", padding_size / 4)
        } else {
            "".to_string()
        }
    }
}

/// GPU representation of spawner parameters.
#[repr(C)]
#[derive(Debug, Default, Clone, Copy, Pod, Zeroable, ShaderType)]
pub(crate) struct GpuSpawnerParams {
    /// Transform of the effect (origin of the emitter). This is either added to
    /// emitted particles at spawn time, if the effect simulated in world
    /// space, or to all simulated particles during rendering if the effect is
    /// simulated in local space.
    transform: GpuCompressedTransform,
    /// Inverse of [`transform`], stored with the same convention.
    ///
    /// [`transform`]: Self::transform
    inverse_transform: GpuCompressedTransform,
    /// Number of particles to spawn this frame.
    spawn: i32,
    /// Spawn seed, for randomized modifiers.
    seed: u32,
    /// Index of the pong (read) buffer for indirect indices, used by the render
    /// shader to fetch particles and render them. Only temporarily stored
    /// between indirect and render passes, and overwritten each frame by CPU
    /// upload. This is mostly a hack to transfer a value between those 2
    /// compute passes.
    render_pong: u32,
    /// Index of the [`GpuEffectMetadata`] for this effect.
    effect_metadata_index: u32,
    /// Index of the [`GpuDrawIndirect`] or [`GpuDrawIndexedIndirect`] for this
    /// effect.
    draw_indirect_index: u32,
    /// Start offset of the particles and indirect indices into the effect's
    /// slab, in number of particles (row index).
    slab_offset: u32,
    /// Start offset of the particles and indirect indices into the parent
    /// effect's slab (if the effect has a parent effect), in number of
    /// particles (row index). This is ignored if the effect has no parent.
    parent_slab_offset: u32,
    // For Pod
    _padding0: u32,
}

impl GpuSpawnerParams {
    /// Create a new [`GpuSpawnerParams`] from gathered CPU data.
    pub fn new(
        global_transform: &GlobalTransform,
        spawn_count: u32,
        prng_seed: u32,
        slab_offset: u32,
        parent_slab_offset: Option<u32>,
        effect_metadata_buffer_table_id: BufferTableId,
        maybe_cached_draw_indirect_args: Option<&CachedDrawIndirectArgs>,
    ) -> Self {
        let transform = global_transform.to_matrix().into();
        let inverse_transform = Mat4::from(
            // Inverse the Affine3A first, then convert to Mat4. This is a lot more
            // efficient than inversing the Mat4.
            global_transform.affine().inverse(),
        )
        .into();
        Self {
            transform,
            inverse_transform,
            spawn: spawn_count as i32,
            seed: prng_seed,
            effect_metadata_index: effect_metadata_buffer_table_id.0,
            draw_indirect_index: maybe_cached_draw_indirect_args
                .map(|cdia| cdia.get_row().0)
                .unwrap_or_default(),
            slab_offset,
            parent_slab_offset: parent_slab_offset.unwrap_or(u32::MAX),
            ..default()
        }
    }
}

/// GPU representation of an indirect compute dispatch input.
///
/// Note that unlike most other data structure, this doesn't need to be aligned
/// (except for the default 4-byte align for most GPU types) to any uniform or
/// storage buffer offset alignment, because the buffer storing this is only
/// ever used as input to indirect dispatch commands, and never bound as a
/// shader resource.
///
/// See https://docs.rs/wgpu/latest/wgpu/util/struct.DispatchIndirectArgs.html.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Pod, Zeroable, ShaderType)]
pub struct GpuDispatchIndirectArgs {
    pub x: u32,
    pub y: u32,
    pub z: u32,
}

impl Default for GpuDispatchIndirectArgs {
    fn default() -> Self {
        Self { x: 0, y: 1, z: 1 }
    }
}

/// GPU representation of an indirect (non-indexed) render input.
///
/// Note that unlike most other data structure, this doesn't need to be aligned
/// (except for the default 4-byte align for most GPU types) to any uniform or
/// storage buffer offset alignment, because the buffer storing this is only
/// ever used as input to indirect render commands, and never bound as a shader
/// resource.
///
/// See https://docs.rs/wgpu/latest/wgpu/util/struct.DrawIndirectArgs.html.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Pod, Zeroable, ShaderType)]
pub struct GpuDrawIndirectArgs {
    pub vertex_count: u32,
    pub instance_count: u32,
    pub first_vertex: u32,
    pub first_instance: u32,
}

impl Default for GpuDrawIndirectArgs {
    fn default() -> Self {
        Self {
            vertex_count: 0,
            instance_count: 1,
            first_vertex: 0,
            first_instance: 0,
        }
    }
}

/// GPU representation of an indirect indexed render input.
///
/// Note that unlike most other data structure, this doesn't need to be aligned
/// (except for the default 4-byte align for most GPU types) to any uniform or
/// storage buffer offset alignment, because the buffer storing this is only
/// ever used as input to indirect render commands, and never bound as a shader
/// resource.
///
/// See https://docs.rs/wgpu/latest/wgpu/util/struct.DrawIndexedIndirectArgs.html.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Pod, Zeroable, ShaderType)]
pub struct GpuDrawIndexedIndirectArgs {
    pub index_count: u32,
    pub instance_count: u32,
    pub first_index: u32,
    pub base_vertex: i32,
    pub first_instance: u32,
}

impl Default for GpuDrawIndexedIndirectArgs {
    fn default() -> Self {
        Self {
            index_count: 0,
            instance_count: 1,
            first_index: 0,
            base_vertex: 0,
            first_instance: 0,
        }
    }
}

/// Shared info for a single batch (single shader invocation).
#[repr(C)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Pod, Zeroable, ShaderType)]
pub struct GpuBatchInfo {
    /// Total number of CPU particles to spawn. This is the sum of all `spawn`
    /// counts of all effect instances in this batch which spawn CPU-based
    /// particles. This is uploaded from CPU each frame.
    pub total_spawn_count: u32,
    /// Total number of CPU particles to update. This is the sum of all
    /// `alive_count` of all effect instances in this batch. This is calculated
    /// on GPU each frame, between the init and update passes.
    pub total_update_count: u32,
    /// Start index of the slice of [`GpuSpawnerInfo`] for this batch, into the
    /// [`EffectsMeta::spawner_buffer`]. The slice length is equal to the number
    /// of effects instances batched together.
    pub spawner_base: u32,
    /// Offset to apply to the workgroup thread index to determine the global
    /// particle index in the currently bound slab. This is often (and ideally)
    /// zero, but may be > 0 if the entire slab cannot be processed with a
    /// single batch.
    pub base_particle: u32,
    /// Offset, in u32 count (4 bytes), into the prefix sum buffer where the
    /// first value for this batch is located.
    pub prefix_sum_offset: u32,
    /// Number of consecutive values in the prefix sum buffer for this batch.
    /// This is the number of effects batches together in the batch.
    pub prefix_sum_count: u32,
}

/// Metadata describing a single effect instance.
///
/// The metadata describes various effect settings, as well we the location in
/// GPU buffers of the piece of data associated with that instance.
///
/// This is the CPU representation equivalent to the WGSL structure on GPU
/// `EffectMetadata`.
#[repr(C)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Pod, Zeroable, ShaderType)]
pub struct GpuEffectMetadata {
    //
    // Some runtime variables modified on GPU only (capacity is constant)
    /// Effect capacity, in number of particles.
    pub capacity: u32,
    // Additional data not part of the required draw indirect args
    /// Number of alive particles.
    pub alive_count: u32,
    /// Cached value of `alive_count` to cap threads in update pass.
    pub max_update: u32,
    /// Cached value of `dead_count` to cap threads in init pass.
    pub max_spawn: u32,
    /// Index of the ping buffer for particle indices. Init and update compute
    /// passes always write into the ping buffer and read from the pong buffer.
    /// The buffers are swapped (ping = 1 - ping) during the indirect dispatch.
    pub indirect_write_index: u32,

    /// Index of the [`GpuDrawIndirect`] or [`GpuDrawIndexedIndirect`] struct
    /// inside the global [`EffectsMeta::draw_indirect_buffer`] or
    /// [`EffectsMeta::draw_indexed_indirect_buffer`]. The actual buffer depends
    /// on whether the mesh is indexed or not, which is stored in
    /// [`CachedMeshLocation`].
    pub indirect_draw_index: u32,
    /// Offset (in u32 count) of the init indirect dispatch struct inside its
    /// buffer. This avoids having to align those 16-byte structs to the GPU
    /// alignment (at least 32 bytes, even 256 bytes on some).
    pub init_indirect_dispatch_index: u32,
    /// Array index of the property block for this
    /// effect. This is ignored if the effect doesn't use properties.
    pub properties_array_index: u32,
    /// Index of this effect into its parent's ChildInfo array
    /// ([`EffectChildren::effect_cache_ids`] and its associated GPU
    /// array). This starts at zero for the first child of each effect, and is
    /// only unique per parent, not globally. Only available if this effect is a
    /// child of another effect (i.e. if it has a parent).
    pub local_child_index: u32,
    /// For children, global index of the ChildInfo into the shared array.
    pub global_child_index: u32,
    /// For parents, base index of the their first ChildInfo into the shared
    /// array.
    pub base_child_index: u32,

    /// Particle stride, in number of u32.
    pub particle_stride: u32,
    /// Offset from the particle start to the first sort key, in number of u32.
    pub sort_key_offset: u32,
    /// Offset from the particle start to the second sort key, in number of u32.
    pub sort_key2_offset: u32,

    //
    // Again some runtime-only GPU-mutated data
    /// Atomic counter incremented each time a particle spawns. Useful for
    /// things like RIBBON_ID or any other use where a unique value is needed.
    /// The value loops back after some time, but unless some particle lives
    /// forever there's little chance of repetition.
    pub particle_counter: u32,
}

/// Single init fill dispatch item in an [`InitFillDispatchQueue`].
#[derive(Debug)]
pub(super) struct InitFillDispatchItem {
    /// Index of the source [`GpuChildInfo`] entry to read the event count from.
    pub global_child_index: u32,
    /// Index of the [`GpuDispatchIndirect`] entry to write the workgroup count
    /// to.
    pub dispatch_indirect_index: u32,
}

/// Queue of fill dispatch operations for the init indirect pass.
///
/// The queue stores the init fill dispatch operations for the current frame,
/// without the reference to the source and destination buffers, which may be
/// reallocated later in the frame. This allows enqueuing operations during the
/// prepare rendering phase, while deferring GPU buffer (re-)allocation to a
/// later stage.
#[derive(Debug, Default, Resource)]
pub(super) struct InitFillDispatchQueue {
    queue: Vec<InitFillDispatchItem>,
    submitted_queue_index: Option<u32>,
}

impl InitFillDispatchQueue {
    /// Clear the queue.
    #[inline]
    pub fn clear(&mut self) {
        self.queue.clear();
        self.submitted_queue_index = None;
    }

    /// Check if the queue is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.queue.is_empty()
    }

    /// Enqueue a new operation.
    #[inline]
    pub fn enqueue(&mut self, global_child_index: u32, dispatch_indirect_index: u32) {
        assert!(global_child_index != u32::MAX);
        self.queue.push(InitFillDispatchItem {
            global_child_index,
            dispatch_indirect_index,
        });
    }

    /// Submit pending operations for this frame.
    pub fn submit(
        &mut self,
        src_buffer: &Buffer,
        dst_buffer: &Buffer,
        gpu_buffer_operations: &mut GpuBufferOperations,
    ) {
        if self.queue.is_empty() {
            return;
        }

        // Sort by source. We can only batch if the destination is also contiguous, so
        // we can check with a linear walk if the source is already sorted.
        self.queue
            .sort_unstable_by_key(|item| item.global_child_index);

        let mut fill_queue = GpuBufferOperationQueue::new();

        // Batch and schedule all init indirect dispatch operations
        assert!(
            self.queue[0].global_child_index != u32::MAX,
            "Global child index not initialized"
        );
        let mut src_start = self.queue[0].global_child_index;
        let mut dst_start = self.queue[0].dispatch_indirect_index;
        let mut src_end = src_start + 1;
        let mut dst_end = dst_start + 1;
        let src_stride = GpuChildInfo::min_size().get() as u32 / 4;
        let dst_stride = GpuDispatchIndirectArgs::SHADER_SIZE.get() as u32 / 4;
        for i in 1..self.queue.len() {
            let InitFillDispatchItem {
                global_child_index: src,
                dispatch_indirect_index: dst,
            } = self.queue[i];
            if src != src_end || dst != dst_end {
                let count = src_end - src_start;
                debug_assert_eq!(count, dst_end - dst_start);
                let args = GpuBufferOperationArgs {
                    src_offset: src_start * src_stride + 1,
                    src_stride,
                    dst_offset: dst_start * dst_stride,
                    dst_stride,
                    count,
                    thread_group_size: 1,
                };
                trace!(
                "enqueue_init_fill(): src:global_child_index={} dst:init_indirect_dispatch_index={} args={:?} src_buffer={:?} dst_buffer={:?}",
                src_start,
                dst_start,
                args,
                src_buffer.id(),
                dst_buffer.id(),
            );
                fill_queue.enqueue(
                    GpuBufferOperationType::FillDispatchArgs,
                    args,
                    src_buffer.clone(),
                    None,
                    dst_buffer.clone(),
                    None,
                );
                src_start = src;
                dst_start = dst;
            }
            src_end = src + 1;
            dst_end = dst + 1;
        }
        if src_start != src_end || dst_start != dst_end {
            let count = src_end - src_start;
            debug_assert_eq!(count, dst_end - dst_start);
            let args = GpuBufferOperationArgs {
                src_offset: src_start * src_stride + 1,
                src_stride,
                dst_offset: dst_start * dst_stride,
                dst_stride,
                count,
                thread_group_size: 1,
            };
            trace!(
            "IFDA::submit(): src:global_child_index={} dst:init_indirect_dispatch_index={} args={:?} src_buffer={:?} dst_buffer={:?}",
            src_start,
            dst_start,
            args,
            src_buffer.id(),
            dst_buffer.id(),
        );
            fill_queue.enqueue(
                GpuBufferOperationType::FillDispatchArgs,
                args,
                src_buffer.clone(),
                None,
                dst_buffer.clone(),
                None,
            );
        }

        debug_assert!(self.submitted_queue_index.is_none());
        if !fill_queue.operation_queue.is_empty() {
            self.submitted_queue_index = Some(gpu_buffer_operations.submit(fill_queue));
        }
    }
}

/// Single sort fill dispatch item in a [`SortFillDispatchQueue`].
#[derive(Debug)]
pub(super) struct SortFillDispatchItem {
    /// Metadata table entry of the ribbon effect batch to read the
    /// [`GpuEffectMetadata::alive_count`] from.
    pub metadata_table_id: BufferTableId,
    /// Index into the prefix sum buffer where the post-update alive count is
    /// copied before calculating the sort prefix sum.
    pub prefix_sum_index: u32,
    /// Index of the [`GpuDispatchIndirect`] entry to write the workgroup count
    /// to, for the sort-fill pass (1 thread per particle).
    pub sort_fill_indirect_dispatch_index: u32,
    /// Index of the [`GpuDispatchIndirect`] entry to write the workgroup count
    /// to, for the sort pass (1 thread per 16-particle group).
    pub sort_indirect_dispatch_index: u32,
}

/// Indices of the GPU operation queues needed to prepare a ribbon sort.
#[derive(Debug, Default)]
pub(super) struct SortFillDispatchQueueIndices {
    /// Queue which copies post-update alive counts into the prefix sum buffer.
    pub alive_count_copy: Option<u32>,
    /// Queue which creates the indirect sort-fill dispatch arguments. This uses
    /// 1 thread per particle, total alive_count threads.
    pub fill_dispatch: Option<u32>,
    /// Queue which creates the indirect sort dispatch arguments. This uses 1
    /// thread per 16 particles, total alive_count.div_ceil(16) threads.
    pub sort_dispatch: Option<u32>,
}

/// Queue of fill dispatch operations for the ribbon particle sort pass.
///
/// The queue stores the sort fill dispatch operations for the current frame,
/// without the reference to the effect metadata buffer, which may be
/// reallocated later in the frame. This allows enqueuing operations during
/// batching, while deferring the buffer capture to after the metadata buffer is
/// (re-)allocated. Mirrors [`InitFillDispatchQueue`].
#[derive(Debug, Default, Resource)]
pub(super) struct SortFillDispatchQueue {
    queue: Vec<SortFillDispatchItem>,
}

impl SortFillDispatchQueue {
    /// Clear the queue.
    #[inline]
    pub fn clear(&mut self) {
        self.queue.clear();
    }

    /// Check if no ribbon sort dispatch operations were queued this frame.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.queue.is_empty()
    }

    /// Enqueue a new operation.
    #[inline]
    pub fn enqueue(
        &mut self,
        metadata_table_id: BufferTableId,
        prefix_sum_index: u32,
        sort_fill_indirect_dispatch_index: u32,
        sort_indirect_dispatch_index: u32,
    ) {
        self.queue.push(SortFillDispatchItem {
            metadata_table_id,
            prefix_sum_index,
            sort_fill_indirect_dispatch_index,
            sort_indirect_dispatch_index,
        });
    }

    /// Submit the queued operations, returning the index of the submitted queue
    /// (or `None` if there was nothing to submit). Must be called after
    /// `effect_metadata_buffer` has been (re-)allocated for the frame.
    pub fn submit(
        &self,
        effect_metadata_buffer: &BufferTable<GpuEffectMetadata>,
        prefix_sum_buffer: &Buffer,
        sort_bind_groups: &SortBindGroups,
        gpu_buffer_operations: &mut GpuBufferOperations,
    ) -> SortFillDispatchQueueIndices {
        if self.queue.is_empty() {
            return default();
        }

        let Some(src_buffer) = effect_metadata_buffer.buffer() else {
            error!("Failed to find effect metadata buffer. This is a bug.");
            return default();
        };
        let Some(dst_buffer) = sort_bind_groups.indirect_args_buffer() else {
            error!("Missing indirect dispatch buffer for sorting, cannot schedule particle sort for ribbons. This is a bug.");
            return default();
        };

        let alive_count_offset = std::mem::offset_of!(GpuEffectMetadata, alive_count) as u32 / 4;
        debug_assert_eq!(
            alive_count_offset, 1,
            "GpuEffectMetadata changed, update this assert."
        );
        let src_stride = GpuEffectMetadata::SHADER_SIZE.get() as u32 / 4;
        let dst_stride = GpuDispatchIndirectArgs::SHADER_SIZE.get() as u32 / 4;

        let mut alive_count_copy_queue = GpuBufferOperationQueue::new();
        let mut fill_queue = GpuBufferOperationQueue::new();
        let mut sort_queue = GpuBufferOperationQueue::new();
        for item in &self.queue {
            let src_binding_offset = effect_metadata_buffer.dynamic_offset(item.metadata_table_id);
            debug_assert_eq!(
                src_binding_offset % 4,
                0,
                "Effect metadata offset must be u32-aligned."
            );
            let src_offset = src_binding_offset / 4 + alive_count_offset;
            trace!(
                "queue_sort_alive_count_copy(): src#{:?}@+{}B -> dst#{:?}[{}]",
                src_buffer.id(),
                src_binding_offset,
                prefix_sum_buffer.id(),
                item.prefix_sum_index,
            );

            // Enqueue an op to copy alive_count into the prefix sum buffer
            alive_count_copy_queue.enqueue(
                GpuBufferOperationType::Copy,
                GpuBufferOperationArgs {
                    src_offset,
                    src_stride,
                    dst_offset: item.prefix_sum_index,
                    dst_stride: 1,
                    count: 1,
                    thread_group_size: 1,
                },
                src_buffer.clone(),
                None,
                prefix_sum_buffer.clone(),
                None,
            );

            // Enqueue an op to calculate from alive_count the number of workgroups for the
            // sort-fill pass
            let dst_offset = sort_bind_groups
                .get_indirect_args_byte_offset(item.sort_fill_indirect_dispatch_index)
                / 4;
            fill_queue.enqueue(
                GpuBufferOperationType::FillDispatchArgs,
                GpuBufferOperationArgs {
                    src_offset,
                    src_stride,
                    dst_offset: dst_offset as u32,
                    dst_stride,
                    count: 1,
                    thread_group_size: 1,
                },
                src_buffer.clone(),
                None,
                dst_buffer.clone(),
                None,
            );

            // Enqueue an op to calculate from alive_count the number of workgroups for the
            // sort pass
            let dst_offset = sort_bind_groups
                .get_indirect_args_byte_offset(item.sort_indirect_dispatch_index)
                / 4;
            sort_queue.enqueue(
                GpuBufferOperationType::FillDispatchArgs,
                GpuBufferOperationArgs {
                    src_offset,
                    src_stride,
                    dst_offset: dst_offset as u32,
                    dst_stride,
                    count: 1,
                    thread_group_size: 16, // numItemPerThread=16; see shader
                },
                src_buffer.clone(),
                None,
                dst_buffer.clone(),
                None,
            );

            trace!(
                "queue_sort_fill_dispatch(): src#{:?}@+{}B ({}B) -> dst#{:?}@+{}B (whole)",
                src_buffer.id(),
                src_binding_offset,
                src_stride,
                dst_buffer.id(),
                dst_offset * 4,
            );
        }

        let alive_count_copy = if alive_count_copy_queue.operation_queue.is_empty() {
            None
        } else {
            Some(gpu_buffer_operations.submit(alive_count_copy_queue))
        };
        let fill_dispatch = if fill_queue.operation_queue.is_empty() {
            None
        } else {
            Some(gpu_buffer_operations.submit(fill_queue))
        };
        let sort_dispatch = if sort_queue.operation_queue.is_empty() {
            None
        } else {
            Some(gpu_buffer_operations.submit(sort_queue))
        };
        SortFillDispatchQueueIndices {
            alive_count_copy,
            fill_dispatch,
            sort_dispatch,
        }
    }
}

/// Compute pipeline to run the `vfx_indirect` dispatch workgroup calculation
/// shader.
#[derive(Resource)]
pub(crate) struct DispatchIndirectPipeline {
    /// Layout of bind group sim_params@0.
    sim_params_bind_group_layout_desc: BindGroupLayoutDescriptor,
    /// Layout of bind group effect_metadata@1.
    effect_metadata_bind_group_layout_desc: BindGroupLayoutDescriptor,
    /// Layout of bind group spawner@2.
    spawner_bind_group_layout_desc: BindGroupLayoutDescriptor,
    /// Layout of bind group child_infos@3.
    child_infos_bind_group_layout_desc: BindGroupLayoutDescriptor,
    /// Shader when no GPU events are used (no bind group @3).
    indirect_shader_noevent: Handle<Shader>,
    /// Shader when GPU events are used (bind group @3 present).
    indirect_shader_events: Handle<Shader>,
}

impl FromWorld for DispatchIndirectPipeline {
    fn from_world(world: &mut World) -> Self {
        let render_device = world.get_resource::<RenderDevice>().unwrap();

        // Copy the indirect pipeline shaders to self, because we can't access anything
        // else during pipeline specialization.
        let (indirect_shader_noevent, indirect_shader_events) = {
            let effects_meta = world.get_resource::<EffectsMeta>().unwrap();
            (
                effects_meta.indirect_shader_noevent.clone(),
                effects_meta.indirect_shader_events.clone(),
            )
        };

        let storage_alignment = render_device.limits().min_storage_buffer_offset_alignment;
        let spawner_min_binding_size = GpuSpawnerParams::aligned_size(storage_alignment);

        // @group(0) @binding(0) var<uniform> sim_params : SimParams;
        trace!("GpuSimParams: min_size={}", GpuSimParams::min_size());
        let sim_params_bind_group_layout_desc = BindGroupLayoutDescriptor::new(
            "hanabi:bgl:dispatch_indirect:sim_params",
            &BindGroupLayoutEntries::single(
                ShaderStages::COMPUTE,
                uniform_buffer::<GpuSimParams>(false),
            ),
        );

        trace!(
            "GpuEffectMetadata: min_size={}",
            GpuEffectMetadata::min_size()
        );
        let effect_metadata_bind_group_layout_desc = BindGroupLayoutDescriptor::new(
            "hanabi:bgl:dispatch_indirect:effect_metadata@1",
            &BindGroupLayoutEntries::sequential(
                ShaderStages::COMPUTE,
                (
                    // @group(1) @binding(0) var<storage, read_write> effect_metadatas :
                    // array<EffectMetadata>;
                    storage_buffer::<GpuEffectMetadata>(false),
                    // @group(1) @binding(1) var<storage, read_write> draw_indirect_buffer :
                    // array<u32>;
                    storage_buffer::<GpuDrawIndexedIndirectArgs>(false),
                ),
            ),
        );

        trace!(
            "GpuSpawnerParams: min_size={} spawner_min_binding_size={}",
            GpuSpawnerParams::min_size(),
            spawner_min_binding_size
        );
        let spawner_bind_group_layout_desc = BindGroupLayoutDescriptor::new(
            "hanabi:bgl:dispatch_indirect:spawner@2",
            &BindGroupLayoutEntries::sequential(
                ShaderStages::COMPUTE,
                (
                    // @group(2) @binding(0) var<storage, read_write> spawner_buffer :
                    // array<Spawner>;
                    //storage_buffer::<GpuSpawnerParams>(false), // TODO - vfx_sort_fill still
                    // needs aligned
                    storage_buffer_sized(false, Some(spawner_min_binding_size)),
                    // @group(2) @binding(1) var<storage, read_write> prefix_sum : array<u32>;
                    storage_buffer::<u32>(false),
                ),
            ),
        );

        trace!("GpuChildInfo: min_size={}", GpuChildInfo::min_size());
        let child_infos_bind_group_layout_desc = BindGroupLayoutDescriptor::new(
            "hanabi:bgl:dispatch_indirect:child_infos@3",
            &BindGroupLayoutEntries::single(
                ShaderStages::COMPUTE,
                // @group(3) @binding(0) var<storage, read_write> child_info_buffer :
                // ChildInfoBuffer;
                storage_buffer::<GpuChildInfo>(false),
            ),
        );

        Self {
            sim_params_bind_group_layout_desc,
            effect_metadata_bind_group_layout_desc,
            spawner_bind_group_layout_desc,
            child_infos_bind_group_layout_desc,
            indirect_shader_noevent,
            indirect_shader_events,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct DispatchIndirectPipelineKey {
    /// True if any allocated effect uses GPU spawn events. In that case, the
    /// pipeline is specialized to clear all GPU events each frame after the
    /// indirect init pass consumed them to spawn particles, and before the
    /// update pass optionally produce more events.
    /// Key: HAS_GPU_SPAWN_EVENTS
    has_events: bool,
}

impl SpecializedComputePipeline for DispatchIndirectPipeline {
    type Key = DispatchIndirectPipelineKey;

    fn specialize(&self, key: Self::Key) -> ComputePipelineDescriptor {
        trace!(
            "Specializing indirect pipeline (has_events={})",
            key.has_events
        );

        let mut shader_defs = Vec::with_capacity(2);
        if key.has_events {
            shader_defs.push("HAS_GPU_SPAWN_EVENTS".into());
        }

        let mut layout = Vec::with_capacity(4);
        layout.push(self.sim_params_bind_group_layout_desc.clone());
        layout.push(self.effect_metadata_bind_group_layout_desc.clone());
        layout.push(self.spawner_bind_group_layout_desc.clone());
        if key.has_events {
            layout.push(self.child_infos_bind_group_layout_desc.clone());
        }

        let label = format!(
            "hanabi:compute_pipeline:dispatch_indirect{}",
            if key.has_events {
                "_events"
            } else {
                "_noevent"
            }
        );

        ComputePipelineDescriptor {
            constants: default(),
            label: Some(label.into()),
            layout,
            immediate_size: 0,
            shader: if key.has_events {
                self.indirect_shader_events.clone()
            } else {
                self.indirect_shader_noevent.clone()
            },
            shader_defs,
            entry_point: Some("main".into()),
            zero_initialize_workgroup_memory: false,
        }
    }
}

/// Compute pipeline to run the `vfx_prefix_sum` dispatch workgroup calculation
/// shader.
#[derive(Resource)]
pub(crate) struct PrefixSumPipeline {
    /// Layout of the (only) bind group.
    bind_group_layout_desc: BindGroupLayoutDescriptor,
    /// Shader.
    compute_shader: Handle<Shader>,
}

impl FromWorld for PrefixSumPipeline {
    fn from_world(world: &mut World) -> Self {
        // FIXME - we don't really need to keep this in EffectsMeta, could move here,
        // and replace the FromWorld with Default
        let compute_shader = {
            let effects_meta = world.get_resource::<EffectsMeta>().unwrap();
            effects_meta.prefix_sum_shader.clone()
        };
        let min_storage_buffer_offset_alignment = {
            let render_device = world.get_resource::<RenderDevice>().unwrap();
            render_device.limits().min_storage_buffer_offset_alignment
        };

        let bind_group_layout_desc = BindGroupLayoutDescriptor::new(
            "hanabi:bgl:prefix_sum:@0",
            &BindGroupLayoutEntries::sequential(
                ShaderStages::COMPUTE,
                (
                    // @group(0) @binding(0) var<storage, read_write> batch_infos :
                    // array<BatchInfo>;
                    storage_buffer_sized(
                        false,
                        Some(GpuBatchInfo::aligned_size(
                            min_storage_buffer_offset_alignment,
                        )),
                    ),
                    // @group(0) @binding(1) var<storage, read_write> prefix_sum : array<u32>;
                    storage_buffer::<u32>(false),
                    // @group(0) @binding(2) var<storage, read_write> dispatch_indirect_buffer :
                    // array<DispatchIndirectArgs>;
                    storage_buffer::<GpuDispatchIndirectArgs>(false),
                ),
            ),
        );

        Self {
            bind_group_layout_desc,
            compute_shader,
        }
    }
}

impl PrefixSumPipeline {
    pub fn make_pipeline_desc(&self) -> ComputePipelineDescriptor {
        trace!("Create prefix sum pipeline");

        ComputePipelineDescriptor {
            constants: default(),
            label: Some("hanabi:compute_pipeline:prefix_sum".into()),
            layout: vec![self.bind_group_layout_desc.clone()],
            immediate_size: 0,
            shader: self.compute_shader.clone(),
            shader_defs: vec![],
            entry_point: Some("main".into()),
            zero_initialize_workgroup_memory: false,
        }
    }
}

/// Type of GPU buffer operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum GpuBufferOperationType {
    /// Clear the destination buffer to zero.
    ///
    /// The source parameters [`src_offset`] and [`src_stride`] are ignored.
    ///
    /// [`src_offset`]: crate::GpuBufferOperationArgs::src_offset
    /// [`src_stride`]: crate::GpuBufferOperationArgs::src_stride
    #[allow(dead_code)]
    Zero,
    /// Copy a source buffer into a destination buffer.
    ///
    /// The source can have a stride between each `u32` copied. The destination
    /// is always a contiguous buffer.
    #[allow(dead_code)]
    Copy,
    /// Fill the arguments for a later indirect dispatch call.
    ///
    /// This is similar to a copy, but will round up the source value to the
    /// number of threads per workgroup (64) before writing it into the
    /// destination.
    FillDispatchArgs,
    /// Fill the arguments for a later indirect dispatch call.
    ///
    /// This is the same as [`FillDispatchArgs`], but the source element count
    /// is read from the fourth entry in the destination buffer directly,
    /// and the source buffer and source arguments are unused.
    #[allow(dead_code)]
    FillDispatchArgsSelf,
}

/// GPU representation of the arguments of a block operation on a buffer.
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Pod, Zeroable, ShaderType)]
pub(super) struct GpuBufferOperationArgs {
    /// Offset, as u32 count, where the operation starts in the source buffer.
    src_offset: u32,
    /// Stride, as u32 count, between elements in the source buffer.
    src_stride: u32,
    /// Offset, as u32 count, where the operation starts in the destination
    /// buffer.
    dst_offset: u32,
    /// Stride, as u32 count, between elements in the destination buffer.
    dst_stride: u32,
    /// Number of u32 elements to process for this operation.
    count: u32,
    /// Thread group size.
    thread_group_size: u32,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct QueuedOperationBindGroupKey {
    src_buffer: BufferId,
    src_binding_size: Option<NonZeroU32>,
    dst_buffer: BufferId,
    dst_binding_size: Option<NonZeroU32>,
}

/// A single GPU operation queued on the CPU.
///
/// The operation may involve multiple reads/writes; see
/// [`GpuBufferOperationArgs::count`]. However it is guaranteed to be performed
/// as a single transactional compute dispatch.
#[derive(Debug, Clone)]
struct QueuedOperation {
    /// Type of GPU operation. This determines which compute shader is running.
    op: GpuBufferOperationType,
    /// Index into the shared arguments buffer of the arguments for this single
    /// operation.
    args_index: u32,
    /// Source buffer the operation reads from.
    src_buffer: Buffer,
    /// Binding size of the source buffer, or `None` to bind the whole buffer.
    src_binding_size: Option<NonZeroU32>,
    /// Destination buffer the operation writes to.
    dst_buffer: Buffer,
    /// Binding size of the destination buffer, or `None` to bind the whole
    /// buffer.
    dst_binding_size: Option<NonZeroU32>,
}

impl QueuedOperation {
    /// Get the buffer binding for the source buffer.
    pub fn src_binding(&self) -> BindingResource<'_> {
        BindingResource::Buffer(BufferBinding {
            buffer: &self.src_buffer,
            offset: 0,
            size: self.src_binding_size.map(Into::into),
        })
    }

    /// Get the buffer binding for the destination buffer.
    pub fn dst_binding(&self) -> BindingResource<'_> {
        BindingResource::Buffer(BufferBinding {
            buffer: &self.dst_buffer,
            offset: 0,
            size: self.dst_binding_size.map(Into::into),
        })
    }
}

impl From<&QueuedOperation> for QueuedOperationBindGroupKey {
    fn from(value: &QueuedOperation) -> Self {
        Self {
            src_buffer: value.src_buffer.id(),
            src_binding_size: value.src_binding_size,
            dst_buffer: value.dst_buffer.id(),
            dst_binding_size: value.dst_binding_size,
        }
    }
}

/// Queue of GPU buffer operations.
///
/// The queue records a series of ordered operations on GPU buffers. It can be
/// submitted for this frame via [`GpuBufferOperations::submit()`], and
/// subsequently dispatched as a compute pass via
/// [`GpuBufferOperations::dispatch()`].
pub struct GpuBufferOperationQueue {
    /// Operation arguments, one per queued operation. The meaning depends on
    /// the [`GpuBufferOperationType`].
    args: Vec<GpuBufferOperationArgs>,
    /// Queued operations.
    operation_queue: Vec<QueuedOperation>,
}

impl GpuBufferOperationQueue {
    /// Create a new empty queue.
    pub fn new() -> Self {
        Self {
            args: vec![],
            operation_queue: vec![],
        }
    }

    /// Enqueue a generic operation.
    pub fn enqueue(
        &mut self,
        op: GpuBufferOperationType,
        args: GpuBufferOperationArgs,
        src_buffer: Buffer,
        src_binding_size: Option<NonZeroU32>,
        dst_buffer: Buffer,
        dst_binding_size: Option<NonZeroU32>,
    ) -> u32 {
        assert!(args.thread_group_size > 0);
        trace!(
            "Queue {:?} op: args={:?} src_buffer={:?} src_binding_size={:?} dst_buffer={:?} dst_binding_size={:?}",
            op,
            args,
            src_buffer,
            src_binding_size,
            dst_buffer,
            dst_binding_size,
        );
        let args_index = self.args.len() as u32;
        self.args.push(args);
        self.operation_queue.push(QueuedOperation {
            op,
            args_index,
            src_buffer,
            src_binding_size,
            dst_buffer,
            dst_binding_size,
        });
        args_index
    }
}

/// GPU buffer operations for this frame.
///
/// This resource contains a list of submitted [`GpuBufferOperationQueue`] for
/// the current frame, and ensures the bind groups for those operations are up
/// to date.
#[derive(Resource)]
pub(super) struct GpuBufferOperations {
    /// Arguments for the buffer operations submitted this frame.
    args_buffer: AlignedBufferVec<GpuBufferOperationArgs>,

    /// Bind groups for the submitted operations.
    bind_groups: HashMap<QueuedOperationBindGroupKey, BindGroup>,

    /// Submitted queues for this frame.
    queues: Vec<Vec<QueuedOperation>>,
}

impl FromWorld for GpuBufferOperations {
    fn from_world(world: &mut World) -> Self {
        let render_device = world.get_resource::<RenderDevice>().unwrap();
        let align = render_device.limits().min_uniform_buffer_offset_alignment;
        Self::new(align)
    }
}

impl GpuBufferOperations {
    pub fn new(align: u32) -> Self {
        let args_buffer = AlignedBufferVec::new(
            BufferUsages::UNIFORM,
            Some(NonZeroU64::new(align as u64).unwrap()),
            Some("hanabi:buffer:gpu_operation_args".to_string()),
        );
        Self {
            args_buffer,
            bind_groups: default(),
            queues: vec![],
        }
    }

    /// Clear the queue and begin recording operations for a new frame.
    pub fn begin_frame(&mut self) {
        self.args_buffer.clear();
        self.bind_groups.clear(); // for now; might consider caching frame-to-frame
        self.queues.clear();
    }

    /// Submit a recorded queue.
    ///
    /// # Panics
    ///
    /// Panics if the queue submitted is empty.
    pub fn submit(&mut self, mut queue: GpuBufferOperationQueue) -> u32 {
        assert!(!queue.operation_queue.is_empty());
        let queue_index = self.queues.len() as u32;
        for qop in &mut queue.operation_queue {
            qop.args_index = self.args_buffer.push(queue.args[qop.args_index as usize]) as u32;
        }
        self.queues.push(queue.operation_queue);
        queue_index
    }

    /// Finish recording operations for this frame, and schedule buffer writes
    /// to GPU.
    pub fn end_frame(&mut self, device: &RenderDevice, render_queue: &RenderQueue) {
        assert_eq!(
            self.args_buffer.len(),
            self.queues.iter().fold(0, |len, q| len + q.len())
        );

        // Upload to GPU buffer
        if self.args_buffer.write_buffer(device, render_queue) {
            // Buffer was reallocated; clear all bind groups depending on it
            self.bind_groups.clear();
        }
    }

    /// Create all necessary bind groups for all queued operations.
    pub fn create_bind_groups(
        &mut self,
        render_device: &RenderDevice,
        utils_pipeline: &UtilsPipeline,
    ) {
        trace!(
            "Creating bind groups for {} operation queues...",
            self.queues.len()
        );
        for queue in &self.queues {
            for qop in queue {
                let key: QueuedOperationBindGroupKey = qop.into();
                self.bind_groups.entry(key).or_insert_with(|| {
                    let src_id: NonZeroU32 = qop.src_buffer.id().into();
                    let dst_id: NonZeroU32 = qop.dst_buffer.id().into();
                    let label = format!(
                        "hanabi:bg:util_{:?}_{}_{}",
                        qop.op,
                        src_id.get(),
                        dst_id.get()
                    );
                    let use_dynamic_offset = matches!(
                        qop.op,
                        GpuBufferOperationType::FillDispatchArgs | GpuBufferOperationType::Copy
                    );
                    let bind_group_layout: &BindGroupLayout =
                        utils_pipeline.bind_group_layout(qop.op, use_dynamic_offset);
                    trace!(
                        "-> Creating new bind group '{}': src#{} ({:?}B) -> dst#{} ({:?}B)",
                        label,
                        src_id,
                        qop.src_binding_size,
                        dst_id,
                        qop.dst_binding_size,
                    );
                    render_device.create_bind_group(
                        Some(&label[..]),
                        bind_group_layout,
                        &BindGroupEntries::sequential((
                            BindingResource::Buffer(BufferBinding {
                                buffer: self.args_buffer.buffer().unwrap(),
                                offset: 0,
                                // We always bind exactly 1 row of arguments
                                size: Some(
                                    NonZeroU64::new(self.args_buffer.aligned_size() as u64)
                                        .unwrap(),
                                ),
                            }),
                            qop.src_binding(),
                            qop.dst_binding(),
                        )),
                    )
                });
            }
        }
    }

    /// Dispatch a submitted queue by index.
    ///
    /// This creates a new, optionally labelled, compute pass, and records to
    /// the render context a series of compute workgroup dispatch, one for each
    /// enqueued operation.
    ///
    /// The compute pipeline(s) used for each operation are fetched from the
    /// [`UtilsPipeline`], and the associated bind groups are used from a
    /// previous call to [`Self::create_bind_groups()`].
    pub fn dispatch(
        &self,
        index: u32,
        render_context: &mut RenderContext,
        utils_pipeline: &UtilsPipeline,
        compute_pass_label: Option<&str>,
    ) {
        let queue = &self.queues[index as usize];
        trace!(
            "Recording GPU commands for queue #{} ({} ops)...",
            index,
            queue.len(),
        );

        if queue.is_empty() {
            return;
        }

        let mut compute_pass =
            render_context
                .command_encoder()
                .begin_compute_pass(&ComputePassDescriptor {
                    label: compute_pass_label,
                    timestamp_writes: None,
                });

        let mut prev_op = None;
        let mut prev_key = None;
        let mut prev_dynamic_args_index = None;
        for qop in queue {
            trace!("qop={:?}", qop);

            let pipeline_changed = Some(qop.op) != prev_op;
            if pipeline_changed {
                compute_pass.set_pipeline(utils_pipeline.get_pipeline(qop.op));
                prev_op = Some(qop.op);
            }

            let key: QueuedOperationBindGroupKey = qop.into();
            let use_dynamic_offset = matches!(
                qop.op,
                GpuBufferOperationType::FillDispatchArgs | GpuBufferOperationType::Copy
            );
            let bind_group_changed = Some(key) != prev_key;
            let requires_bind_group_rebind = pipeline_changed
                || bind_group_changed
                || (use_dynamic_offset && Some(qop.args_index) != prev_dynamic_args_index);
            if requires_bind_group_rebind {
                if let Some(bind_group) = self.bind_groups.get(&key) {
                    if use_dynamic_offset {
                        let args_offset = self.args_buffer.dynamic_offset(qop.args_index as usize);
                        compute_pass.set_bind_group(0, bind_group, &[args_offset]);
                        trace!(
                            "set bind group with dynamic offsets: op={:?} args=+{}B",
                            qop.op,
                            args_offset,
                        );
                    } else {
                        compute_pass.set_bind_group(0, bind_group, &[]);
                        trace!("set bind group without dynamic offsets: op={:?}", qop.op);
                    }
                } else {
                    error!("GPU fill dispatch buffer operation bind group not found for buffers src#{:?} dst#{:?}", qop.src_buffer.id(), qop.dst_buffer.id());
                    continue;
                }
                prev_key = Some(key);
                prev_dynamic_args_index = use_dynamic_offset.then_some(qop.args_index);
            }

            // Dispatch the operations for this buffer
            const WORKGROUP_SIZE: u32 = 64;
            let num_ops = 1u32; // TODO - batching!
            let workgroup_count = num_ops.div_ceil(WORKGROUP_SIZE);
            compute_pass.dispatch_workgroups(workgroup_count, 1, 1);
            trace!(
                "-> fill dispatch compute dispatched: num_ops={} workgroup_count={}",
                num_ops,
                workgroup_count
            );
        }
    }
}

/// Compute pipeline to run the `vfx_utils` shader.
#[derive(Resource)]
pub(crate) struct UtilsPipeline {
    #[allow(dead_code)]
    bind_group_layout: BindGroupLayout,
    bind_group_layout_dyn: BindGroupLayout,
    bind_group_layout_no_src: BindGroupLayout,
    pipelines: [ComputePipeline; 4],
}

impl FromWorld for UtilsPipeline {
    fn from_world(world: &mut World) -> Self {
        let render_device = world.get_resource::<RenderDevice>().unwrap();

        let bind_group_layout = render_device.create_bind_group_layout(
            "hanabi:bgl:utils",
            &BindGroupLayoutEntries::sequential(
                ShaderStages::COMPUTE,
                (
                    uniform_buffer::<GpuBufferOperationArgs>(false),
                    storage_buffer_read_only::<u32>(false),
                    storage_buffer::<u32>(false),
                ),
            ),
        );

        let pipeline_layout = render_device.create_pipeline_layout(&PipelineLayoutDescriptor {
            label: Some("hanabi:pipeline_layout:utils"),
            bind_group_layouts: &[Some(&bind_group_layout)],
            immediate_size: 0,
        });

        let bind_group_layout_dyn = render_device.create_bind_group_layout(
            "hanabi:bgl:utils_dyn",
            &BindGroupLayoutEntries::sequential(
                ShaderStages::COMPUTE,
                (
                    uniform_buffer::<GpuBufferOperationArgs>(true),
                    storage_buffer_read_only::<u32>(false),
                    storage_buffer::<u32>(false),
                ),
            ),
        );

        let pipeline_layout_dyn = render_device.create_pipeline_layout(&PipelineLayoutDescriptor {
            label: Some("hanabi:pipeline_layout:utils_dyn"),
            bind_group_layouts: &[Some(&bind_group_layout_dyn)],
            immediate_size: 0,
        });

        let bind_group_layout_no_src = render_device.create_bind_group_layout(
            "hanabi:bind_group_layout:utils_no_src",
            &BindGroupLayoutEntries::with_indices(
                ShaderStages::COMPUTE,
                (
                    (0, uniform_buffer::<GpuBufferOperationArgs>(false)),
                    (2, storage_buffer::<u32>(false)),
                ),
            ),
        );

        let pipeline_layout_no_src =
            render_device.create_pipeline_layout(&PipelineLayoutDescriptor {
                label: Some("hanabi:pipeline_layout:utils_no_src"),
                bind_group_layouts: &[Some(&bind_group_layout_no_src)],
                immediate_size: 0,
            });

        let shader_code = include_str!("vfx_utils.wgsl");

        let shader_source = ShaderSource::Wgsl(Cow::Borrowed(shader_code));

        debug!("Create utils shader module:\n{}", shader_code);
        #[allow(unsafe_code)]
        let shader_module = unsafe {
            // no colon in a shader module's label: DX12's DXC takes it as the source's file name, and on Windows
            // "hanabi:shader:utils" names no file, so every Bevy app failed there wherever DXC loads (raea #196)
            render_device.create_shader_module(ShaderModuleDescriptor {
                label: Some("hanabi_shader_utils"),
                source: shader_source,
            })
        };

        trace!("Create vfx_utils pipelines...");
        let zero_pipeline = render_device.create_compute_pipeline(&RawComputePipelineDescriptor {
            label: Some("hanabi:compute_pipeline:zero_buffer"),
            layout: Some(&pipeline_layout),
            module: &shader_module,
            entry_point: Some("zero_buffer"),
            compilation_options: PipelineCompilationOptions {
                constants: &[],
                zero_initialize_workgroup_memory: false,
            },
            cache: None,
        });
        let copy_pipeline = render_device.create_compute_pipeline(&RawComputePipelineDescriptor {
            label: Some("hanabi:compute_pipeline:copy_buffer"),
            layout: Some(&pipeline_layout_dyn),
            module: &shader_module,
            entry_point: Some("copy_buffer"),
            compilation_options: PipelineCompilationOptions {
                constants: &[],
                zero_initialize_workgroup_memory: false,
            },
            cache: None,
        });
        let fill_dispatch_args_pipeline =
            render_device.create_compute_pipeline(&RawComputePipelineDescriptor {
                label: Some("hanabi:compute_pipeline:fill_dispatch_args"),
                layout: Some(&pipeline_layout_dyn),
                module: &shader_module,
                entry_point: Some("fill_dispatch_args"),
                compilation_options: PipelineCompilationOptions {
                    constants: &[],
                    zero_initialize_workgroup_memory: false,
                },
                cache: None,
            });
        let fill_dispatch_args_self_pipeline =
            render_device.create_compute_pipeline(&RawComputePipelineDescriptor {
                label: Some("hanabi:compute_pipeline:fill_dispatch_args_self"),
                layout: Some(&pipeline_layout_no_src),
                module: &shader_module,
                entry_point: Some("fill_dispatch_args_self"),
                compilation_options: PipelineCompilationOptions {
                    constants: &[],
                    zero_initialize_workgroup_memory: false,
                },
                cache: None,
            });

        Self {
            bind_group_layout,
            bind_group_layout_dyn,
            bind_group_layout_no_src,
            pipelines: [
                zero_pipeline,
                copy_pipeline,
                fill_dispatch_args_pipeline,
                fill_dispatch_args_self_pipeline,
            ],
        }
    }
}

impl UtilsPipeline {
    fn get_pipeline(&self, op: GpuBufferOperationType) -> &ComputePipeline {
        match op {
            GpuBufferOperationType::Zero => &self.pipelines[0],
            GpuBufferOperationType::Copy => &self.pipelines[1],
            GpuBufferOperationType::FillDispatchArgs => &self.pipelines[2],
            GpuBufferOperationType::FillDispatchArgsSelf => &self.pipelines[3],
        }
    }

    fn bind_group_layout(
        &self,
        op: GpuBufferOperationType,
        with_dynamic_offsets: bool,
    ) -> &BindGroupLayout {
        if op == GpuBufferOperationType::FillDispatchArgsSelf {
            assert!(
                !with_dynamic_offsets,
                "FillDispatchArgsSelf op cannot use dynamic offset (not implemented)"
            );
            &self.bind_group_layout_no_src
        } else if with_dynamic_offsets {
            &self.bind_group_layout_dyn
        } else {
            &self.bind_group_layout
        }
    }
}

#[derive(Resource)]
pub(crate) struct ParticlesInitPipeline {
    sim_params_layout_desc: BindGroupLayoutDescriptor,
}

impl Default for ParticlesInitPipeline {
    fn default() -> Self {
        let sim_params_layout_desc = BindGroupLayoutDescriptor::new(
            "hanabi:bgl:vfx_init:sim_params@0",
            // @group(0) @binding(0) var<uniform> sim_params: SimParams;
            &BindGroupLayoutEntries::single(
                ShaderStages::COMPUTE,
                uniform_buffer::<GpuSimParams>(false),
            ),
        );

        Self {
            sim_params_layout_desc,
        }
    }
}

bitflags! {
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub struct ParticleInitPipelineKeyFlags: u8 {
        //const CLONE = (1u8 << 0); // DEPRECATED
        const ATTRIBUTE_PREV = (1u8 << 1);
        const ATTRIBUTE_NEXT = (1u8 << 2);
        const CONSUME_GPU_SPAWN_EVENTS = (1u8 << 3);
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct ParticleInitPipelineKey {
    /// Compute shader, with snippets applied, but not preprocessed yet.
    shader: Handle<Shader>,
    /// Minimum binding size in bytes for the particle layout buffer.
    particle_layout_min_binding_size: NonZeroU32,
    /// Minimum binding size in bytes for the particle layout buffer of the
    /// parent effect, if any.
    /// Key: READ_PARENT_PARTICLE
    parent_particle_layout_min_binding_size: Option<NonZeroU32>,
    /// Pipeline flags.
    flags: ParticleInitPipelineKeyFlags,
    /// Layout of the particle@1 bind group this pipeline was specialized with.
    particle_bind_group_layout_desc: BindGroupLayoutDescriptor,
    /// Layout of the spawner@2 bind group this pipeline was specialized with.
    spawner_bind_group_layout_desc: BindGroupLayoutDescriptor,
    /// Layout of the metadata@3 bind group this pipeline was specialized with.
    metadata_bind_group_layout_desc: BindGroupLayoutDescriptor,
}

impl SpecializedComputePipeline for ParticlesInitPipeline {
    type Key = ParticleInitPipelineKey;

    fn specialize(&self, key: Self::Key) -> ComputePipelineDescriptor {
        // We use the hash to correlate the key content with the GPU resource name
        let hash = calc_hash(&key);
        trace!("Specializing init pipeline {hash:016X} with key {key:?}");

        let mut shader_defs = Vec::with_capacity(4);
        if key
            .flags
            .contains(ParticleInitPipelineKeyFlags::ATTRIBUTE_PREV)
        {
            shader_defs.push("ATTRIBUTE_PREV".into());
        }
        if key
            .flags
            .contains(ParticleInitPipelineKeyFlags::ATTRIBUTE_NEXT)
        {
            shader_defs.push("ATTRIBUTE_NEXT".into());
        }
        let consume_gpu_spawn_events = key
            .flags
            .contains(ParticleInitPipelineKeyFlags::CONSUME_GPU_SPAWN_EVENTS);
        if consume_gpu_spawn_events {
            shader_defs.push("CONSUME_GPU_SPAWN_EVENTS".into());
        }
        // FIXME - for now this needs to keep in sync with consume_gpu_spawn_events
        if key.parent_particle_layout_min_binding_size.is_some() {
            assert!(consume_gpu_spawn_events);
            shader_defs.push("READ_PARENT_PARTICLE".into());
        } else {
            assert!(!consume_gpu_spawn_events);
        }

        let label = format!("hanabi:pipeline:init_{hash:016X}");
        trace!(
            "-> creating pipeline '{}' with shader defs:{}",
            label,
            shader_defs
                .iter()
                .fold(String::new(), |acc, x| acc + &format!(" {x:?}"))
        );

        ComputePipelineDescriptor {
            constants: default(),
            label: Some(label.into()),
            layout: vec![
                self.sim_params_layout_desc.clone(),
                key.particle_bind_group_layout_desc.clone(),
                key.spawner_bind_group_layout_desc.clone(),
                key.metadata_bind_group_layout_desc.clone(),
            ],
            immediate_size: 0,
            shader: key.shader,
            shader_defs,
            entry_point: Some("main".into()),
            zero_initialize_workgroup_memory: false,
        }
    }
}

#[derive(Resource)]
pub(crate) struct ParticlesUpdatePipeline {
    sim_params_layout_desc: BindGroupLayoutDescriptor,
}

impl Default for ParticlesUpdatePipeline {
    fn default() -> Self {
        let sim_params_layout_desc = BindGroupLayoutDescriptor::new(
            "hanabi:bgl:vfx_update:sim_params@0",
            &BindGroupLayoutEntries::sequential(
                ShaderStages::COMPUTE,
                (
                    // @group(0) @binding(0) var<uniform> sim_params : SimParams;
                    uniform_buffer::<GpuSimParams>(false),
                    // @group(0) @binding(1) var<storage, read_write> draw_indirect_buffer :
                    // array<DrawIndexedIndirectArgs>;
                    storage_buffer::<GpuDrawIndexedIndirectArgs>(false),
                ),
            ),
        );

        Self {
            sim_params_layout_desc,
        }
    }
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub(crate) struct ParticleUpdatePipelineKey {
    /// Compute shader, with snippets applied, but not preprocessed yet.
    shader: Handle<Shader>,
    /// Particle layout.
    particle_layout: ParticleLayout,
    /// Key: LOCAL_SPACE_SIMULATION
    /// The effect is simulated in local space. The particle positions are
    /// stored in simulation space.
    local_space_simulation: bool,
    /// Minimum binding size in bytes for the particle layout buffer of the
    /// parent effect, if any.
    /// Key: READ_PARENT_PARTICLE
    parent_particle_layout_min_binding_size: Option<NonZeroU32>,
    /// Key: EMITS_GPU_SPAWN_EVENTS
    num_event_buffers: u32,
    /// Layout of the particle@1 bind group this pipeline was specialized with.
    particle_bind_group_layout_desc: BindGroupLayoutDescriptor,
    /// Layout of the spawner@2 bind group this pipeline was specialized with.
    spawner_bind_group_layout_desc: BindGroupLayoutDescriptor,
    /// Layout of the metadata@3 bind group this pipeline was specialized with.
    metadata_bind_group_layout_desc: BindGroupLayoutDescriptor,
}

impl SpecializedComputePipeline for ParticlesUpdatePipeline {
    type Key = ParticleUpdatePipelineKey;

    fn specialize(&self, key: Self::Key) -> ComputePipelineDescriptor {
        // We use the hash to correlate the key content with the GPU resource name
        let hash = calc_hash(&key);
        trace!("Specializing update pipeline {hash:016X} with key {key:?}");

        let mut shader_defs = Vec::with_capacity(6);
        shader_defs.push("EM_MAX_SPAWN_ATOMIC".into());
        // ChildInfo needs atomic event_count because all threads append to the event
        // buffer(s) in parallel.
        shader_defs.push("CHILD_INFO_EVENT_COUNT_IS_ATOMIC".into());
        if key.particle_layout.contains(Attribute::PREV) {
            shader_defs.push("ATTRIBUTE_PREV".into());
        }
        if key.particle_layout.contains(Attribute::NEXT) {
            shader_defs.push("ATTRIBUTE_NEXT".into());
        }
        if key.parent_particle_layout_min_binding_size.is_some() {
            shader_defs.push("READ_PARENT_PARTICLE".into());
        }
        if key.num_event_buffers > 0 {
            shader_defs.push("EMITS_GPU_SPAWN_EVENTS".into());
        }
        if key.local_space_simulation {
            shader_defs.push("LOCAL_SPACE_SIMULATION".into());
        }

        let hash = calc_func_id(&key);
        let label = format!("hanabi:pipeline:update_{hash:016X}");
        trace!(
            "-> creating pipeline '{}' with shader defs:{}",
            label,
            shader_defs
                .iter()
                .fold(String::new(), |acc, x| acc + &format!(" {x:?}"))
        );

        ComputePipelineDescriptor {
            constants: default(),
            label: Some(label.into()),
            layout: vec![
                self.sim_params_layout_desc.clone(),
                key.particle_bind_group_layout_desc.clone(),
                key.spawner_bind_group_layout_desc.clone(),
                key.metadata_bind_group_layout_desc.clone(),
            ],
            immediate_size: 0,
            shader: key.shader,
            shader_defs,
            entry_point: Some("main".into()),
            zero_initialize_workgroup_memory: false,
        }
    }
}

#[derive(Resource)]
pub(crate) struct ParticlesRenderPipeline {
    view_layout_desc: BindGroupLayoutDescriptor,
}

impl FromWorld for ParticlesRenderPipeline {
    fn from_world(_world: &mut World) -> Self {
        let view_layout_desc = BindGroupLayoutDescriptor::new(
            "hanabi:bgl:render:view@0",
            &BindGroupLayoutEntries::sequential(
                ShaderStages::VERTEX_FRAGMENT,
                (
                    // @group(0) @binding(0) var<uniform> view: View;
                    uniform_buffer::<ViewUniform>(true),
                    // @group(0) @binding(1) var<uniform> sim_params : SimParams;
                    uniform_buffer::<GpuSimParams>(false),
                ),
            ),
        );

        Self { view_layout_desc }
    }
}

#[cfg(all(feature = "2d", feature = "3d"))]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
enum PipelineMode {
    Camera2d,
    Camera3d,
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub(crate) struct ParticleRenderPipelineKey {
    /// Render shader, with snippets applied, but not preprocessed yet.
    shader: Handle<Shader>,
    /// Particle layout.
    particle_layout: ParticleLayout,
    mesh_layout: Option<MeshVertexBufferLayoutRef>,
    /// Texture layout.
    texture_layout: TextureLayout,
    /// Key: LOCAL_SPACE_SIMULATION
    /// The effect is simulated in local space, and during rendering all
    /// particles are transformed by the effect's [`GlobalTransform`].
    local_space_simulation: bool,
    /// Key: USE_ALPHA_MASK, OPAQUE
    /// The particle's alpha masking behavior.
    alpha_mask: ParticleRenderAlphaMaskPipelineKey,
    /// The effect needs Alpha blend.
    alpha_mode: AlphaMode,
    /// Key: FLIPBOOK
    /// The effect is rendered with flipbook texture animation based on the
    /// sprite index of each particle.
    flipbook: bool,
    /// Key: NEEDS_UV
    /// The effect needs UVs.
    needs_uv: bool,
    /// Key: NEEDS_NORMAL
    /// The effect needs normals.
    needs_normal: bool,
    /// Key: NEEDS_PARTICLE_IN_FRAGMENT
    /// The effect needs access to the particle index and buffer in the fragment
    /// shader.
    needs_particle_fragment: bool,
    /// Key: RIBBONS
    /// The effect has ribbons.
    ribbons: bool,
    /// For dual-mode configurations only, the actual mode of the current render
    /// pipeline. Otherwise the mode is implicitly determined by the active
    /// feature.
    #[cfg(all(feature = "2d", feature = "3d"))]
    pipeline_mode: PipelineMode,
    /// MSAA sample count.
    msaa_samples: u32,
    /// Camera output format.
    target_format: TextureFormat,
    /// Layout of the spawner@2 bind group this pipeline was specialized with.
    spawner_bind_group_layout_desc: BindGroupLayoutDescriptor,
}

#[derive(Clone, Copy, Default, Hash, PartialEq, Eq, Debug)]
pub(crate) enum ParticleRenderAlphaMaskPipelineKey {
    #[default]
    Blend,
    /// Key: USE_ALPHA_MASK
    /// The effect is rendered with alpha masking.
    AlphaMask,
    /// Key: OPAQUE
    /// The effect is rendered fully-opaquely.
    Opaque,
}

impl SpecializedRenderPipeline for ParticlesRenderPipeline {
    type Key = ParticleRenderPipelineKey;

    fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor {
        trace!("Specializing render pipeline for key: {key:?}");

        trace!("Creating layout for bind group particle@1 of render pass");
        // FIXME - duplicated in ParticleSlab::new()
        let particle_bind_group_layout_desc = BindGroupLayoutDescriptor::new(
            "hanabi:bgl:render:particle@1",
            &BindGroupLayoutEntries::sequential(
                ShaderStages::VERTEX,
                (
                    // @group(1) @binding(0) var<storage, read> particle_buffer : ParticleBuffer;
                    storage_buffer_read_only_sized(
                        false,
                        Some(key.particle_layout.min_binding_size()),
                    )
                    .visibility(ShaderStages::VERTEX_FRAGMENT),
                    // @group(1) @binding(1) var<storage, read> indirect_buffer : IndirectBuffer;
                    storage_buffer_read_only::<GpuIndirectIndex>(false),
                ),
            ),
        );

        let layout = vec![
            self.view_layout_desc.clone(),
            particle_bind_group_layout_desc,
            key.spawner_bind_group_layout_desc.clone(),
        ];

        let mut shader_defs = vec![];
        if !key.texture_layout.layout.is_empty() {
            shader_defs.push("HAS_MATERIAL".into());
        }

        let vertex_buffer_layout = key.mesh_layout.as_ref().and_then(|mesh_layout| {
            mesh_layout
                .0
                .get_layout(&[
                    Mesh::ATTRIBUTE_POSITION.at_shader_location(0),
                    Mesh::ATTRIBUTE_UV_0.at_shader_location(1),
                    Mesh::ATTRIBUTE_NORMAL.at_shader_location(2),
                ])
                .ok()
        });

        // Key: LOCAL_SPACE_SIMULATION
        if key.local_space_simulation {
            shader_defs.push("LOCAL_SPACE_SIMULATION".into());
        }

        match key.alpha_mask {
            ParticleRenderAlphaMaskPipelineKey::Blend => {}
            ParticleRenderAlphaMaskPipelineKey::AlphaMask => {
                // Key: USE_ALPHA_MASK
                shader_defs.push("USE_ALPHA_MASK".into())
            }
            ParticleRenderAlphaMaskPipelineKey::Opaque => {
                // Key: OPAQUE
                shader_defs.push("OPAQUE".into())
            }
        }

        // Key: FLIPBOOK
        if key.flipbook {
            shader_defs.push("FLIPBOOK".into());
        }

        // Key: NEEDS_UV
        if key.needs_uv {
            shader_defs.push("NEEDS_UV".into());
        }

        // Key: NEEDS_NORMAL
        if key.needs_normal {
            shader_defs.push("NEEDS_NORMAL".into());
        }

        if key.needs_particle_fragment {
            shader_defs.push("NEEDS_PARTICLE_FRAGMENT".into());
        }

        // Key: RIBBONS
        if key.ribbons {
            shader_defs.push("RIBBONS".into());
        }

        #[cfg(feature = "2d")]
        let depth_stencil_2d = DepthStencilState {
            format: CORE_2D_DEPTH_FORMAT,
            // Use depth buffer with alpha-masked particles, not with transparent ones
            depth_write_enabled: Some(false), // TODO - opaque/alphamask 2d
            // Bevy uses reverse-Z, so GreaterEqual really means closer
            depth_compare: Some(CompareFunction::GreaterEqual),
            stencil: StencilState::default(),
            bias: DepthBiasState::default(),
        };

        #[cfg(feature = "3d")]
        let depth_stencil_3d = DepthStencilState {
            format: CORE_3D_DEPTH_FORMAT,
            // Use depth buffer with alpha-masked or opaque particles, not
            // with transparent ones
            depth_write_enabled: Some(matches!(
                key.alpha_mask,
                ParticleRenderAlphaMaskPipelineKey::AlphaMask
                    | ParticleRenderAlphaMaskPipelineKey::Opaque
            )),
            // Bevy uses reverse-Z, so GreaterEqual really means closer
            depth_compare: Some(CompareFunction::GreaterEqual),
            stencil: StencilState::default(),
            bias: DepthBiasState::default(),
        };

        #[cfg(all(feature = "2d", feature = "3d"))]
        assert_eq!(CORE_2D_DEPTH_FORMAT, CORE_3D_DEPTH_FORMAT);
        #[cfg(all(feature = "2d", feature = "3d"))]
        let depth_stencil = match key.pipeline_mode {
            PipelineMode::Camera2d => Some(depth_stencil_2d),
            PipelineMode::Camera3d => Some(depth_stencil_3d),
        };

        #[cfg(all(feature = "2d", not(feature = "3d")))]
        let depth_stencil = Some(depth_stencil_2d);

        #[cfg(all(feature = "3d", not(feature = "2d")))]
        let depth_stencil = Some(depth_stencil_3d);

        let format = key.target_format;

        let hash = calc_func_id(&key);
        let label = format!("hanabi:pipeline:render_{hash:016X}");
        trace!(
            "-> creating pipeline '{}' with shader defs:{}",
            label,
            shader_defs
                .iter()
                .fold(String::new(), |acc, x| acc + &format!(" {x:?}"))
        );

        RenderPipelineDescriptor {
            label: Some(label.into()),
            layout,
            immediate_size: 0,
            vertex: VertexState {
                constants: default(),
                shader: key.shader.clone(),
                entry_point: Some("vertex".into()),
                shader_defs: shader_defs.clone(),
                buffers: vec![vertex_buffer_layout.expect("Vertex buffer layout not present")],
            },
            fragment: Some(FragmentState {
                constants: default(),
                shader: key.shader,
                shader_defs,
                entry_point: Some("fragment".into()),
                targets: vec![Some(ColorTargetState {
                    format,
                    blend: Some(key.alpha_mode.into()),
                    write_mask: ColorWrites::ALL,
                })],
            }),
            primitive: PrimitiveState {
                front_face: FrontFace::Ccw,
                cull_mode: None,
                unclipped_depth: false,
                polygon_mode: PolygonMode::Fill,
                conservative: false,
                topology: PrimitiveTopology::TriangleList,
                strip_index_format: None,
            },
            depth_stencil,
            multisample: MultisampleState {
                count: key.msaa_samples,
                mask: !0,
                alpha_to_coverage_enabled: false,
            },
            zero_initialize_workgroup_memory: false,
        }
    }
}

/// A single effect instance extracted from a [`ParticleEffect`] as a
/// render world item.
///
/// [`ParticleEffect`]: crate::ParticleEffect
#[derive(Debug, Clone, PartialEq, Component)]
#[require(CachedPipelines, CachedReadyState, CachedEffectMetadata)]
pub(crate) struct ExtractedEffect {
    /// Handle to the effect asset this instance is based on.
    /// The handle is weak to prevent refcount cycles and gracefully handle
    /// assets unloaded or destroyed after a draw call has been submitted.
    pub handle: Handle<EffectAsset>,
    /// Particle layout for the effect.
    pub particle_layout: ParticleLayout,
    /// Effect capacity, in number of particles.
    pub capacity: u32,
    /// Layout flags.
    pub layout_flags: LayoutFlags,
    /// Texture layout.
    pub texture_layout: TextureLayout,
    /// Textures.
    pub textures: Vec<Handle<Image>>,
    /// Alpha mode.
    pub alpha_mode: AlphaMode,
    /// Effect shaders.
    pub effect_shaders: EffectShaders,
    /// Condition under which the effect is simulated.
    pub simulation_condition: SimulationCondition,
}

/// Extracted data for the [`GpuSpawnerParams`].
///
/// This contains all data which may change each frame during the regular usage
/// of the effect, but doesn't require any particular GPU resource update
/// (except re-uploading that new data to GPU, of course).
#[derive(Debug, Clone, PartialEq, Component)]
pub(crate) struct ExtractedSpawner {
    /// Number of particles to spawn this frame.
    ///
    /// This is ignored if the effect is a child effect consuming GPU spawn
    /// events.
    pub spawn_count: u32,
    /// PRNG seed.
    pub prng_seed: u32,
    /// Global transform of the effect origin.
    pub transform: GlobalTransform,
    /// Is the effect visible this frame?
    pub is_visible: bool,
}

/// Cache info for the metadata of the effect.
///
/// This manages the GPU allocation of the [`GpuEffectMetadata`] for this
/// effect.
#[derive(Debug, Default, Component)]
pub(crate) struct CachedEffectMetadata {
    /// Allocation ID.
    pub table_id: BufferTableId,
    /// Current metadata values, cached on CPU for change detection.
    pub metadata: GpuEffectMetadata,
}

/// Extracted parent information for a child effect.
///
/// This component is present on the [`RenderEntity`] of an extracted effect if
/// the effect has a parent effect. Otherwise, it's removed.
///
/// This components forms an ECS relationship with [`ChildrenEffects`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Component)]
#[relationship(relationship_target = ChildrenEffects)]
pub(crate) struct ChildEffectOf {
    /// Render entity of the parent.
    pub parent: Entity,
}

/// Extracted children information for a parent effect.
///
/// This component is present on the [`RenderEntity`] of an extracted effect if
/// the effect is a parent effect for one or more child effects. Otherwise, it's
/// removed.
///
/// This components forms an ECS relationship with [`ChildEffectOf`]. Note that
/// we don't use `linked_spawn` because:
/// 1. This would fight with the `SyncToRenderWorld` as the main world
///    parent-child hierarchy is by design not an ECS relationship (it's a lose
///    declarative coupling).
/// 2. The components on the render entity often store GPU resources or other
///    data we need to clean-up manually, and not all of them currently use
///    lifecycle hooks, so we want to manage despawning manually to prevent
///    leaks.
#[derive(Debug, Clone, PartialEq, Eq, Component)]
#[relationship_target(relationship = ChildEffectOf)]
pub(crate) struct ChildrenEffects(Vec<Entity>);

impl<'a> IntoIterator for &'a ChildrenEffects {
    type Item = <Self::IntoIter as Iterator>::Item;

    type IntoIter = std::slice::Iter<'a, Entity>;

    #[inline(always)]
    fn into_iter(self) -> Self::IntoIter {
        self.0.iter()
    }
}

impl Deref for ChildrenEffects {
    type Target = [Entity];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// Extracted data for an effect's properties, if any.
///
/// This component is present on the [`RenderEntity`] of an extracted effect if
/// that effect has properties. It optionally contains new CPU data to
/// (re-)upload this frame. If the effect has no property, this component is
/// removed.
#[derive(Debug, Component)]
pub(crate) struct ExtractedProperties {
    /// Property layout for the effect.
    pub property_layout: PropertyLayout,
    /// Values of properties written in a binary blob according to
    /// [`property_layout`].
    ///
    /// This is `Some(blob)` if the data needs to be (re)uploaded to GPU, or
    /// `None` if nothing needs to be done for this frame.
    ///
    /// [`property_layout`]: crate::render::ExtractedEffect::property_layout
    pub property_data: Option<Vec<u8>>,
}

#[derive(Default, Resource)]
pub(crate) struct EffectAssetEvents {
    pub images: Vec<AssetEvent<Image>>,
}

/// System extracting all the asset events for the [`Image`] assets to enable
/// dynamic update of images bound to any effect.
///
/// This system runs in parallel of [`extract_effects`].
pub(crate) fn extract_effect_events(
    mut events: ResMut<EffectAssetEvents>,
    mut image_events: Extract<MessageReader<AssetEvent<Image>>>,
) {
    #[cfg(feature = "trace")]
    let _span = bevy::log::info_span!("extract_effect_events").entered();
    trace!("extract_effect_events()");

    let EffectAssetEvents { ref mut images } = *events;
    *images = image_events.read().copied().collect();
}

/// Debugging settings.
///
/// Settings used to debug Hanabi. These have no effect on the actual behavior
/// of Hanabi, but may affect its performance.
///
/// # Example
///
/// ```
/// # use bevy::prelude::*;
/// # use bevy_hanabi::*;
/// fn startup(mut debug_settings: ResMut<DebugSettings>) {
///     // Each time a new effect is spawned, capture 2 frames
///     debug_settings.start_capture_on_new_effect = true;
///     debug_settings.capture_frame_count = 2;
/// }
/// ```
#[derive(Debug, Default, Clone, Copy, Resource)]
pub struct DebugSettings {
    /// Enable automatically starting a GPU debugger capture as soon as this
    /// frame starts rendering (extract phase).
    ///
    /// Enable this feature to automatically capture one or more GPU frames when
    /// the `extract_effects()` system runs next. This instructs any attached
    /// GPU debugger to start a capture; this has no effect if no debugger
    /// is attached.
    ///
    /// If a capture is already on-going this has no effect; the on-going
    /// capture needs to be terminated first. Note however that a capture can
    /// stop and another start in the same frame.
    ///
    /// This value is not reset automatically. If you set this to `true`, you
    /// should set it back to `false` on next frame to avoid capturing forever.
    pub start_capture_this_frame: bool,

    /// Enable automatically starting a GPU debugger capture when one or more
    /// effects are spawned.
    ///
    /// Enable this feature to automatically capture one or more GPU frames when
    /// a new effect is spawned (as detected by ECS change detection). This
    /// instructs any attached GPU debugger to start a capture; this has no
    /// effect if no debugger is attached.
    pub start_capture_on_new_effect: bool,

    /// Number of frames to capture with a GPU debugger.
    ///
    /// By default this value is zero, and a GPU debugger capture runs for a
    /// single frame. If a non-zero frame count is specified here, the capture
    /// will instead stop once the specified number of frames has been recorded.
    ///
    /// You should avoid setting this to a value too large, to prevent the
    /// capture size from getting out of control. A typical value is 1 to 3
    /// frames, or possibly more (up to 10) for exceptional contexts. Some GPU
    /// debuggers or graphics APIs might further limit this value on their own,
    /// so there's no guarantee the graphics API will honor this value.
    pub capture_frame_count: u32,
}

#[derive(Debug, Default, Clone, Copy, Resource)]
pub(crate) struct RenderDebugSettings {
    /// Is a GPU debugger capture on-going?
    is_capturing: bool,
    /// Start time of any on-going GPU debugger capture.
    capture_start: Duration,
    /// Number of frames captured so far for on-going GPU debugger capture.
    captured_frames: u32,
}

/// Manage GPU debug capture start/stop.
///
/// If any GPU debug capture is configured to start or stop in
/// [`DebugSettings`], they do so during this system's run. This ensures
/// that all GPU commands produced by Hanabi are recorded (but may miss some
/// from Bevy itself, if another Bevy system runs before this one).
///
/// We do this during extract to try and capture as close as possible to an
/// entire GPU frame.
pub(crate) fn start_stop_gpu_debug_capture(
    real_time: Extract<Res<Time<Real>>>,
    render_device: Res<RenderDevice>,
    debug_settings: Extract<Res<DebugSettings>>,
    mut render_debug_settings: ResMut<RenderDebugSettings>,
    q_added_effects: Extract<Query<(), Added<CompiledParticleEffect>>>,
) {
    #[cfg(feature = "trace")]
    let _span = bevy::log::info_span!("start_stop_debug_capture").entered();
    trace!("start_stop_debug_capture()");

    // Stop any pending capture if needed
    if render_debug_settings.is_capturing {
        render_debug_settings.captured_frames += 1;

        if render_debug_settings.captured_frames >= debug_settings.capture_frame_count {
            #[expect(unsafe_code, reason = "Debugging only")]
            unsafe {
                render_device.wgpu_device().stop_graphics_debugger_capture();
            }
            render_debug_settings.is_capturing = false;
            warn!(
                "Stopped GPU debug capture after {} frames, at t={}s.",
                render_debug_settings.captured_frames,
                real_time.elapsed().as_secs_f64()
            );
        }
    }

    // If no pending capture, consider starting a new one
    if !render_debug_settings.is_capturing
        && (debug_settings.start_capture_this_frame
            || (debug_settings.start_capture_on_new_effect && !q_added_effects.is_empty()))
    {
        #[expect(unsafe_code, reason = "Debugging only")]
        unsafe {
            render_device
                .wgpu_device()
                .start_graphics_debugger_capture();
        }
        render_debug_settings.is_capturing = true;
        render_debug_settings.capture_start = real_time.elapsed();
        render_debug_settings.captured_frames = 0;
        warn!(
            "Started GPU debug capture of {} frames at t={}s.",
            debug_settings.capture_frame_count,
            render_debug_settings.capture_start.as_secs_f64()
        );
    }
}

/// Write the ready state of all render world effects back into their source
/// effect in the main world.
pub(crate) fn report_ready_state(
    mut main_world: ResMut<MainWorld>,
    q_ready_state: Query<&CachedReadyState>,
) {
    let mut q_effects = main_world.query::<(RenderEntity, &mut CompiledParticleEffect)>();
    for (render_entity, mut compiled_particle_effect) in q_effects.iter_mut(&mut main_world) {
        if let Ok(cached_ready_state) = q_ready_state.get(render_entity) {
            compiled_particle_effect.is_ready = cached_ready_state.is_ready();
        }
    }
}

/// System extracting data for rendering of all active [`ParticleEffect`]
/// components.
///
/// [`ParticleEffect`]: crate::ParticleEffect
pub(crate) fn extract_effects(
    mut commands: Commands,
    effects: Extract<Res<Assets<EffectAsset>>>,
    default_mesh: Extract<Res<DefaultMesh>>,
    // Main world effects to extract
    q_effects: Extract<
        Query<(
            Entity,
            RenderEntity,
            Option<&InheritedVisibility>,
            Option<&ViewVisibility>,
            &EffectSpawner,
            &CompiledParticleEffect,
            Option<Ref<EffectProperties>>,
            &GlobalTransform,
        )>,
    >,
    // Render world effects extracted from a previous frame, if any
    mut q_extracted_effects: Query<(
        &mut ExtractedEffect,
        Option<&mut ExtractedSpawner>,
        Option<&ChildEffectOf>, // immutable, because of relationship
        Option<&mut ExtractedEffectMesh>,
        Option<&mut ExtractedProperties>,
    )>,
) {
    #[cfg(feature = "trace")]
    let _span = bevy::log::info_span!("extract_effects").entered();
    trace!("extract_effects()");

    // Loop over all existing effects to extract them
    trace!("Extracting {} effects...", q_effects.iter().len());
    for (
        main_entity,
        render_entity,
        maybe_inherited_visibility,
        maybe_view_visibility,
        effect_spawner,
        compiled_effect,
        maybe_properties,
        transform,
    ) in q_effects.iter()
    {
        // Check if shaders are configured
        let Some(effect_shaders) = compiled_effect.get_configured_shaders() else {
            trace!("Effect {:?}: no configured shader, skipped.", main_entity);
            continue;
        };

        // Check if asset is available, otherwise silently ignore
        let Some(asset) = effects.get(&compiled_effect.asset) else {
            trace!(
                "Effect {:?}: EffectAsset not ready, skipped. asset:{:?}",
                main_entity,
                compiled_effect.asset
            );
            continue;
        };

        let is_visible = maybe_inherited_visibility
            .map(|cv| cv.get())
            .unwrap_or(true)
            && maybe_view_visibility.map(|cv| cv.get()).unwrap_or(true);

        let mut cmd = commands.entity(render_entity);

        // Fetch the existing extraction compoennts, if any, which we need to update.
        // Because we use SyncToRenderWorld, there's always a render entity, but it may
        // miss all components. And because we can't query only optional components
        // (that would match all entities in the entire world), we force querying
        // ExtractedEffect, which means we get a miss if it's the first extraction and
        // it's not spawned yet. That's OK, we'll spawn it below.
        let (
            maybe_extracted_effect,
            maybe_extracted_spawner,
            maybe_child_of,
            maybe_extracted_mesh,
            maybe_extracted_properties,
        ) = q_extracted_effects
            .get_mut(render_entity)
            .map(|(extracted_effect, b, c, d, e)| (Some(extracted_effect), b, c, d, e))
            .unwrap_or((None, None, None, None, None));

        // Validate material
        let texture_layout = asset.module().texture_layout();
        let num_expected_tex = texture_layout.layout.len();
        if compiled_effect.textures.len() < num_expected_tex {
            error!(
                "Instance of effect '{}' on entity {:?} is missing some textures. Layout expected {} textures, but CompiledParticleEffect got {} instead.",
                asset.name,
                main_entity,
                num_expected_tex,
                compiled_effect.textures.len()
            );
            continue;
        }
        let textures = if compiled_effect.textures.len() > num_expected_tex {
            trace!(
                "Instance of effect '{}' on entity {:?} has too many textures. Layout expected {} textures, but CompiledParticleEffect got {} instead. Ignoring the extra ones.",
                asset.name,
                main_entity,
                num_expected_tex,
                compiled_effect.textures.len()
            );
            compiled_effect.textures[..num_expected_tex].to_vec()
        } else {
            compiled_effect.textures.clone()
        };

        // Extract general effect data
        let layout_flags = compiled_effect.layout_flags;
        let alpha_mode = compiled_effect.alpha_mode;
        trace!(
            "Extracted instance of effect '{}' on entity {:?} (render entity {:?}): texture_layout_count={} texture_count={} layout_flags={:?}",
            asset.name,
            main_entity,
            render_entity,
            texture_layout.layout.len(),
            textures.len(),
            layout_flags,
        );
        let new_extracted_effect = ExtractedEffect {
            handle: compiled_effect.asset.clone(),
            particle_layout: asset.particle_layout().clone(),
            capacity: asset.capacity(),
            layout_flags,
            texture_layout,
            textures,
            alpha_mode,
            effect_shaders: effect_shaders.clone(),
            simulation_condition: asset.simulation_condition,
        };
        if let Some(mut extracted_effect) = maybe_extracted_effect {
            extracted_effect.set_if_neq(new_extracted_effect);
        } else {
            trace!(
                "Inserting new ExtractedEffect component on {:?}",
                render_entity
            );
            cmd.insert(new_extracted_effect);
        }

        // Extract the spawner data
        let new_spawner = ExtractedSpawner {
            spawn_count: effect_spawner.spawn_count,
            prng_seed: compiled_effect.prng_seed,
            transform: *transform,
            is_visible,
        };
        trace!(
            "[Effect {}] spawn_count={} prng_seed={}",
            render_entity,
            new_spawner.spawn_count,
            new_spawner.prng_seed
        );
        if let Some(mut extracted_spawner) = maybe_extracted_spawner {
            extracted_spawner.set_if_neq(new_spawner);
        } else {
            trace!(
                "Inserting new ExtractedSpawner component on {}",
                render_entity
            );
            cmd.insert(new_spawner);
        }

        // Extract the effect mesh
        let mesh = compiled_effect
            .mesh
            .clone()
            .unwrap_or(default_mesh.0.clone());
        let new_mesh = ExtractedEffectMesh { mesh: mesh.id() };
        if let Some(mut extracted_mesh) = maybe_extracted_mesh {
            extracted_mesh.set_if_neq(new_mesh);
        } else {
            trace!(
                "Inserting new ExtractedEffectMesh component on {:?}",
                render_entity
            );
            cmd.insert(new_mesh);
        }

        // Extract the parent, if any, and resolve its render entity
        let parent_render_entity = if let Some(main_entity) = compiled_effect.parent {
            let Ok((_, render_entity, _, _, _, _, _, _)) = q_effects.get(main_entity) else {
                error!(
                    "Failed to resolve render entity of parent with main entity {:?}.",
                    main_entity
                );
                cmd.remove::<ChildEffectOf>();
                // TODO - prevent extraction altogether here, instead of just de-parenting?
                continue;
            };
            Some(render_entity)
        } else {
            None
        };
        if let Some(render_entity) = parent_render_entity {
            let new_child_of = ChildEffectOf {
                parent: render_entity,
            };
            // If there's already an ExtractedParent component, ensure we overwrite only if
            // different, to not trigger ECS change detection that we rely on.
            if let Some(child_effect_of) = maybe_child_of {
                // The relationship makes ChildEffectOf immutable, so re-insert to mutate
                if *child_effect_of != new_child_of {
                    cmd.insert(new_child_of);
                }
            } else {
                trace!(
                    "Inserting new ChildEffectOf component on {:?}",
                    render_entity
                );
                cmd.insert(new_child_of);
            }
        } else {
            cmd.remove::<ChildEffectOf>();
        }

        // Extract property data
        let property_layout = asset.property_layout();
        if property_layout.is_empty() {
            cmd.remove::<ExtractedProperties>();
        } else {
            // Re-extract CPU property data if any. Note that this data is not a "new value"
            // but instead a "value that must be uploaded this frame", and therefore is
            // empty when there's no change (as opposed to, having a constant value
            // frame-to-frame).
            let property_data = if let Some(properties) = maybe_properties {
                if properties.is_changed() {
                    trace!("Detected property change, re-serializing...");
                    Some(properties.serialize(&property_layout))
                } else {
                    None
                }
            } else {
                None
            };

            let new_properties = ExtractedProperties {
                property_layout,
                property_data,
            };
            trace!("new_properties = {new_properties:?}");

            if let Some(mut extracted_properties) = maybe_extracted_properties {
                // Always mutate if there's new CPU data to re-upload. Otherwise check for any
                // other change.
                if new_properties.property_data.is_some()
                    || (extracted_properties.property_layout != new_properties.property_layout)
                {
                    trace!(
                        "Updating existing ExtractedProperties (was: {:?})",
                        extracted_properties.as_ref()
                    );
                    *extracted_properties = new_properties;
                }
            } else {
                trace!(
                    "Inserting new ExtractedProperties component on {:?}",
                    render_entity
                );
                cmd.insert(new_properties);
            }
        }
    }
}

pub(crate) fn extract_sim_params(
    real_time: Extract<Res<Time<Real>>>,
    virtual_time: Extract<Res<Time<Virtual>>>,
    time: Extract<Res<Time<EffectSimulation>>>,
    mut sim_params: ResMut<SimParams>,
) {
    #[cfg(feature = "trace")]
    let _span = bevy::log::info_span!("extract_sim_params").entered();
    trace!("extract_sim_params()");

    // Save simulation params into render world
    sim_params.time = time.elapsed_secs_f64();
    sim_params.delta_time = time.delta_secs();
    sim_params.virtual_time = virtual_time.elapsed_secs_f64();
    sim_params.virtual_delta_time = virtual_time.delta_secs();
    sim_params.real_time = real_time.elapsed_secs_f64();
    sim_params.real_delta_time = real_time.delta_secs();
    trace!(
        "SimParams: time={} delta_time={} vtime={} delta_vtime={} rtime={} delta_rtime={}",
        sim_params.time,
        sim_params.delta_time,
        sim_params.virtual_time,
        sim_params.virtual_delta_time,
        sim_params.real_time,
        sim_params.real_delta_time,
    );
}

/// Extracted [`HanabiMainCamera`], with additional extracted data.
///
/// This component is added inside the render world to the render view
/// corresponding to the extracted camera of the main camera holding the
/// [`HanabiMainCamera`] component, if any.
#[derive(Debug, Component)]
pub(crate) struct HanabiRenderCamera {
    /// Main entity the camera/view was extracted from, which contains the
    /// [`Camera`], the [`HanabiMainCamera`], and the original data extracted.
    #[allow(dead_code)]
    pub view: MainEntity,
    /// Camera position in world space.
    pub position: Vec3,
    /// Frustum planes of the camera.
    pub frustum: [Vec4; 6],
}

/// Extract camera related data for the main Hanabi view.
///
/// The main Hanabi view is the view of the [`Camera`] tagged with the
/// [`HanabiMainCamera`], if any. That camera and its view data are used for
/// various features that require a single camera.
pub(crate) fn extract_main_view(
    mut commands: Commands,
    q_views: Extract<
        Query<
            (Entity, RenderEntity, &GlobalTransform, &Frustum),
            (With<Camera>, With<HanabiMainCamera>),
        >,
    >,
    q_render_view: Query<Entity, With<HanabiRenderCamera>>,
) {
    #[cfg(feature = "trace")]
    let _span = bevy::log::info_span!("extract_main_view").entered();
    trace!("extract_main_view()");

    // Get the unique main view from the main world.
    let Ok((entity, render_entity, global_transform, frustum)) = q_views.single() else {
        trace!("No HanabiMainCamera component found (or multiple; this is not allowed).");
        // Delete any previous (now invalid) render world camera
        for entity in q_render_view.iter() {
            commands.entity(entity).remove::<HanabiRenderCamera>();
        }
        return;
    };

    // Ensure uniqueness; delete any previous camera if it's on a different entity
    // that the expected one.
    q_render_view.iter().for_each(|e| {
        if e != render_entity {
            commands.entity(e).remove::<HanabiRenderCamera>();
        }
    });

    let render_camera = HanabiRenderCamera {
        view: entity.into(),
        position: global_transform.translation(),
        frustum: frustum.half_spaces.map(|h| h.normal_d()),
    };
    trace!("HanabiRenderCamera: {render_camera:?}");

    let mut commands = commands.entity(render_entity);
    commands.insert(render_camera);
}

/// Various GPU limits and aligned sizes computed once and cached.
struct GpuLimits {
    /// Value of [`WgpuLimits::min_storage_buffer_offset_alignment`].
    ///
    /// [`WgpuLimits::min_storage_buffer_offset_alignment`]: bevy::render::settings::WgpuLimits::min_storage_buffer_offset_alignment
    storage_buffer_align: NonZeroU32,

    /// Size of [`GpuSpawnerParams`] aligned to the contraint of
    /// [`WgpuLimits::min_storage_buffer_offset_alignment`].
    ///
    /// [`WgpuLimits::min_storage_buffer_offset_alignment`]: bevy::render::settings::WgpuLimits::min_storage_buffer_offset_alignment
    #[allow(dead_code)]
    spawner_params_aligned_size: NonZeroU32,
}

impl GpuLimits {
    pub fn from_device(render_device: &RenderDevice) -> Self {
        let storage_buffer_align =
            render_device.limits().min_storage_buffer_offset_alignment as u64;

        let spawner_params_aligned_size = NonZeroU32::new(
            GpuSpawnerParams::min_size()
                .get()
                .next_multiple_of(storage_buffer_align) as u32,
        )
        .unwrap();

        trace!(
            "GPU-aligned sizes (align: {} B):\n- GpuSpawnerParams: {} B -> {} B",
            storage_buffer_align,
            GpuSpawnerParams::min_size().get(),
            spawner_params_aligned_size.get(),
        );

        Self {
            storage_buffer_align: NonZeroU32::new(storage_buffer_align as u32).unwrap(),
            spawner_params_aligned_size,
        }
    }

    /// Byte alignment for any storage buffer binding.
    pub fn storage_buffer_align(&self) -> NonZeroU32 {
        self.storage_buffer_align
    }
}

/// Global render world resource containing the GPU data to draw all the
/// particle effects in all views.
///
/// The resource is populated by [`prepare_effects()`] with all the effects to
/// render for the current frame, for all views in the frame, and consumed by
/// [`queue_effects()`] to actually enqueue the drawning commands to draw those
/// effects.
#[derive(Resource)]
pub struct EffectsMeta {
    /// Bind group for the camera view, containing the camera projection and
    /// other uniform values related to the camera.
    view_bind_group: Option<BindGroup>,
    /// Bind group #0 of the vfx_update shader, for the simulation parameters
    /// like the current time and frame delta time.
    update_sim_params_bind_group: Option<BindGroup>,
    /// Bind group #0 of the vfx_init and vfx_indirect shaders, for the
    /// simulation parameters like the current time and frame delta time.
    init_and_indirect_sim_params_bind_group: Option<BindGroup>,
    /// Bind group #1 of the vfx_indirect shader, containing both the indirect
    /// compute dispatch and render buffers.
    indirect_metadata_bind_group: Option<BindGroup>,
    /// Bind group #2 of the vfx_indirect shader, containing the spawners.
    indirect_spawner_bind_group: Option<BindGroup>,
    /// Global shared GPU uniform buffer storing the simulation parameters,
    /// uploaded each frame from CPU to GPU.
    sim_params_uniforms: UniformBuffer<GpuSimParams>,
    /// Global shared GPU buffer storing the various spawner parameter structs
    /// for the active effect instances.
    // Note: we're still binding individual spawners in vfx_sort_fill.
    spawner_buffer: AlignedBufferVec<GpuSpawnerParams>,
    /// Global shared GPU buffer storing the various indirect dispatch args
    /// structs for the indirect dispatch of the Update pass.
    dispatch_indirect_args_buffer: GpuBuffer<GpuDispatchIndirectArgs>,
    /// Global shared GPU buffer storing the various indirect draw args structs
    /// for the indirect Render pass. Note that we use
    /// [`GpuDrawIndexedIndirectArgs`] as the largest of the two variants (the
    /// other being [`GpuDrawIndirectArgs`]). For non-indexed entries, we ignore
    /// the last `u32` value.
    draw_indirect_args_buffer: BufferTable<GpuDrawIndexedIndirectArgs>,
    /// Global shared GPU buffer storing the various `EffectMetadata`
    /// structs for the active effect instances.
    effect_metadata_buffer: BufferTable<GpuEffectMetadata>,
    /// Various GPU limits and aligned sizes lazily allocated and cached for
    /// convenience.
    #[allow(dead_code)]
    gpu_limits: GpuLimits,
    indirect_shader_noevent: Handle<Shader>,
    indirect_shader_events: Handle<Shader>,
    /// Pipeline cache ID of the two indirect dispatch pass pipelines (the
    /// -noevent and -events variants).
    indirect_pipeline_ids: [CachedComputePipelineId; 2],
    /// Pipeline cache ID of the active indirect dispatch pass pipeline, which
    /// is either the -noevent or -events variant depending on whether there's
    /// any child effect with GPU events currently active.
    active_indirect_pipeline_id: CachedComputePipelineId,

    prefix_sum_shader: Handle<Shader>,
    prefix_sum_bind_group: Option<BindGroup>,
    prefix_sum_pipeline_id: CachedComputePipelineId,
}

impl EffectsMeta {
    pub fn new(
        device: RenderDevice,
        indirect_shader_noevent: Handle<Shader>,
        indirect_shader_events: Handle<Shader>,
        prefix_sum_shader: Handle<Shader>,
    ) -> Self {
        let gpu_limits = GpuLimits::from_device(&device);

        // Ensure individual GpuSpawnerParams elements are properly aligned so they can
        // be addressed individually by the computer shaders.
        let item_align = gpu_limits.storage_buffer_align();
        trace!(
            "Aligning storage buffers to {} bytes as device limits requires.",
            item_align.get()
        );

        Self {
            view_bind_group: None,
            update_sim_params_bind_group: None,
            init_and_indirect_sim_params_bind_group: None,
            indirect_metadata_bind_group: None,
            indirect_spawner_bind_group: None,
            sim_params_uniforms: UniformBuffer::default(),
            spawner_buffer: AlignedBufferVec::new(
                BufferUsages::STORAGE,
                Some(item_align.into()),
                Some("hanabi:buffer:spawner".to_string()),
            ),
            dispatch_indirect_args_buffer: GpuBuffer::new(
                BufferUsages::STORAGE | BufferUsages::INDIRECT,
                Some("hanabi:buffer:dispatch_indirect_args".to_string()),
            ),
            draw_indirect_args_buffer: BufferTable::new(
                BufferUsages::STORAGE | BufferUsages::INDIRECT,
                Some(GpuDrawIndexedIndirectArgs::SHADER_SIZE),
                Some("hanabi:buffer:draw_indirect_args".to_string()),
            ),
            effect_metadata_buffer: BufferTable::new(
                BufferUsages::STORAGE | BufferUsages::INDIRECT,
                None,
                Some("hanabi:buffer:effect_metadata".to_string()),
            ),
            gpu_limits,
            indirect_shader_noevent,
            indirect_shader_events,
            indirect_pipeline_ids: [
                CachedComputePipelineId::INVALID,
                CachedComputePipelineId::INVALID,
            ],
            active_indirect_pipeline_id: CachedComputePipelineId::INVALID,
            prefix_sum_shader,
            prefix_sum_bind_group: None,
            prefix_sum_pipeline_id: CachedComputePipelineId::INVALID,
        }
    }

    /// Allocate a [`GpuSpawnerParams`] entry into the global shared GPU buffer.
    ///
    /// The entry is allocated for a single render frame. It's automatically
    /// deallocated at the end of this render frame.
    ///
    /// # Returns
    ///
    /// This returns the index of the entry into the global GPU buffer.
    pub fn allocate_spawner(&mut self, gpu_spawner_params: GpuSpawnerParams) -> u32 {
        let spawner_base = self.spawner_buffer.len() as u32;
        self.spawner_buffer.push(gpu_spawner_params);
        spawner_base
    }

    pub fn allocate_draw_indirect(
        &mut self,
        draw_args: &AnyDrawIndirectArgs,
    ) -> CachedDrawIndirectArgs {
        let row = self
            .draw_indirect_args_buffer
            .insert(draw_args.bitcast_to_row_entry());
        CachedDrawIndirectArgs {
            row,
            args: *draw_args,
        }
    }

    pub fn update_draw_indirect(&mut self, row_index: &CachedDrawIndirectArgs) {
        self.draw_indirect_args_buffer
            .update(row_index.get_row(), row_index.args.bitcast_to_row_entry());
    }

    pub fn free_draw_indirect(&mut self, row_index: &CachedDrawIndirectArgs) {
        self.draw_indirect_args_buffer.remove(row_index.get_row());
    }
}

bitflags! {
    /// Effect flags.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct LayoutFlags: u32 {
        /// No flags.
        const NONE = 0;
        // DEPRECATED - The effect uses an image texture.
        //const PARTICLE_TEXTURE = (1 << 0);
        /// The effect is simulated in local space.
        const LOCAL_SPACE_SIMULATION = (1 << 2);
        /// The effect uses alpha masking instead of alpha blending. Only used for 3D.
        const USE_ALPHA_MASK = (1 << 3);
        /// The effect is rendered with flipbook texture animation based on the
        /// [`Attribute::SPRITE_INDEX`] of each particle.
        const FLIPBOOK = (1 << 4);
        /// The effect needs UVs.
        const NEEDS_UV = (1 << 5);
        /// The effect has ribbons.
        const RIBBONS = (1 << 6);
        /// The effects needs normals.
        const NEEDS_NORMAL = (1 << 7);
        /// The effect is fully-opaque.
        const OPAQUE = (1 << 8);
        /// The (update) shader emits GPU spawn events to instruct another effect to spawn particles.
        const EMIT_GPU_SPAWN_EVENTS = (1 << 9);
        /// The (init) shader spawns particles by consuming GPU spawn events, instead of
        /// a single CPU spawn count.
        const CONSUME_GPU_SPAWN_EVENTS = (1 << 10);
        /// The (init or update) shader needs access to its parent particle. This allows
        /// a particle init or update pass to read the data of a parent particle, for
        /// example to inherit some of the attributes.
        const READ_PARENT_PARTICLE = (1 << 11);
        /// The effect access to the particle data in the fragment shader.
        const NEEDS_PARTICLE_FRAGMENT = (1 << 12);
    }
}

impl Default for LayoutFlags {
    fn default() -> Self {
        Self::NONE
    }
}

// raea: blended particles drawn in a fixed order, so a frame is the same on every run (raea #52). The draw list is
// built with atomics, in an order that differs from run to run, which only opaque and alpha-masked particles' depth
// test hides; so blended effects are sorted like ribbons, by view depth from the main camera, far first (as Bevy sorts
// its transparent phase). The sort key's offset carries two flags for vfx_sort_fill.wesl: the key is the depth of the
// vec3 at that offset, and in local space (the effect's transform applies).
pub(crate) const RAEA_SORT_BY_DISTANCE: u32 = 1 << 31;
pub(crate) const RAEA_SORT_LOCAL: u32 = 1 << 30;

impl LayoutFlags {
    /// raea: the effect's particles are sorted before drawing: ribbons (upstream) and blended effects.
    pub(crate) fn raea_sorted(&self) -> bool {
        self.contains(LayoutFlags::RIBBONS) || !self.intersects(LayoutFlags::OPAQUE | LayoutFlags::USE_ALPHA_MASK)
    }
}

/// Observer raised when the [`CachedEffect`] component is removed, which
/// indicates that the effect instance was despawned.
pub(crate) fn on_remove_cached_effect(
    trigger: On<Remove<CachedEffect>>,
    query: Query<(Entity, &MainEntity, &CachedEffect)>,
    mut effect_cache: ResMut<EffectCache>,
    mut effect_bind_groups: ResMut<EffectBindGroups>,
) {
    #[cfg(feature = "trace")]
    let _span = bevy::log::info_span!("on_remove_cached_effect").entered();

    // FIXME - review this Observer pattern; this triggers for each event one by
    // one, which could kill performance if many effects are removed.

    // Fecth the components of the effect being destroyed. Note that the despawn
    // command above is not yet applied, so this query should always succeed.
    let Ok((render_entity, main_entity, cached_effect)) = query.get(trigger.event().entity) else {
        return;
    };

    // Deallocate the effect slice in the GPU effect buffer, and if this was the
    // last slice, also deallocate the GPU buffer itself.
    trace!(
        "=> ParticleEffect on render entity {:?} associated with main entity {:?}, removing...",
        render_entity,
        main_entity,
    );
    let Ok(SlabState::Free) = effect_cache.remove(cached_effect) else {
        // Buffer was not affected, so all bind groups are still valid. Nothing else to
        // do.
        return;
    };

    // Clear bind groups associated with the removed buffer
    trace!(
        "=> GPU particle slab #{} gone, destroying its bind groups...",
        cached_effect.slab_id.index()
    );
    effect_bind_groups
        .particle_slabs
        .remove(&cached_effect.slab_id);
}

/// Observer raised when the [`CachedEffectMetadata`] component is removed, to
/// deallocate the GPU resources associated with the indirect draw args.
pub(crate) fn on_remove_cached_metadata(
    trigger: On<Remove<CachedEffectMetadata>>,
    query: Query<&CachedEffectMetadata>,
    mut effects_meta: ResMut<EffectsMeta>,
) {
    #[cfg(feature = "trace")]
    let _span = bevy::log::info_span!("on_remove_cached_metadata").entered();

    if let Ok(cached_metadata) = query.get(trigger.event().entity) {
        if cached_metadata.table_id.is_valid() {
            effects_meta
                .effect_metadata_buffer
                .remove(cached_metadata.table_id);
        }
    };
}

/// Observer raised when the [`CachedDrawIndirectArgs`] component is removed, to
/// deallocate the GPU resources associated with the indirect draw args.
pub(crate) fn on_remove_cached_draw_indirect_args(
    trigger: On<Remove<CachedDrawIndirectArgs>>,
    query: Query<&CachedDrawIndirectArgs>,
    mut effects_meta: ResMut<EffectsMeta>,
) {
    #[cfg(feature = "trace")]
    let _span = bevy::log::info_span!("on_remove_cached_draw_indirect_args").entered();

    if let Ok(cached_draw_args) = query.get(trigger.event().entity) {
        effects_meta.free_draw_indirect(cached_draw_args);
    };
}

/// Clear pending GPU resources left from previous frame.
///
/// Those generally are source buffers for buffer-to-buffer copies on capacity
/// growth, which need the source buffer to be alive until the copy is done,
/// then can be discarded here.
pub(crate) fn clear_previous_frame_resizes(
    mut effects_meta: ResMut<EffectsMeta>,
    mut sort_bind_groups: ResMut<SortBindGroups>,
    mut init_fill_dispatch_queue: ResMut<InitFillDispatchQueue>,
    mut sort_fill_dispatch_queue: ResMut<SortFillDispatchQueue>,
) {
    #[cfg(feature = "trace")]
    let _span = bevy::log::info_span!("clear_previous_frame_resizes").entered();
    trace!("clear_previous_frame_resizes");

    init_fill_dispatch_queue.clear();
    sort_fill_dispatch_queue.clear();

    // Clear last frame's buffer resizes which may have occured during last frame,
    // during `simulate()` while the `BufferTable` could not be mutated. This is
    // the first point at which we can do that where we're not blocking the main
    // world (so, excluding the extract system).
    effects_meta
        .dispatch_indirect_args_buffer
        .clear_previous_frame_resizes();
    effects_meta
        .draw_indirect_args_buffer
        .clear_previous_frame_resizes();
    effects_meta
        .effect_metadata_buffer
        .clear_previous_frame_resizes();
    sort_bind_groups.clear_previous_frame_resizes();
}

// Fixup the [`CachedChildInfo::global_child_index`] once all child infos have
// been allocated.
pub fn fixup_parents(
    q_changed_parents: Query<(Entity, Ref<CachedParentInfo>)>,
    mut q_children: Query<&mut CachedChildInfo>,
) {
    #[cfg(feature = "trace")]
    let _span = bevy::log::info_span!("fixup_parents").entered();
    trace!("fixup_parents");

    // Once all parents are (re-)allocated, fix up the global index of all
    // children if the parent base index changed.
    trace!(
        "Updating the global index of children of parent effects whose child list just changed..."
    );
    for (parent_entity, cached_parent_info) in q_changed_parents.iter() {
        let base_index =
            cached_parent_info.byte_range.start / GpuChildInfo::SHADER_SIZE.get() as u32;
        let parent_changed = cached_parent_info.is_changed();
        trace!(
            "Updating {} children of parent effect {:?} with base child index {} (parent_changed:{})...",
            cached_parent_info.children.len(),
            parent_entity,
            base_index,
            parent_changed
        );
        for (child_entity, _) in &cached_parent_info.children {
            let Ok(mut cached_child_info) = q_children.get_mut(*child_entity) else {
                error!(
                    "Cannot find child {:?} declared by parent {:?}",
                    *child_entity, parent_entity
                );
                continue;
            };
            if !cached_child_info.is_changed() && !parent_changed {
                continue;
            }
            cached_child_info.global_child_index = base_index + cached_child_info.local_child_index;
            trace!(
                "+ Updated global index for child ID {:?} of parent {:?}: local={}, global={}",
                child_entity,
                parent_entity,
                cached_child_info.local_child_index,
                cached_child_info.global_child_index
            );
        }
    }
}

/// Allocate the GPU resources for all extracted effects.
///
/// This adds the [`CachedEffect`] component as needed, and update it with the
/// allocation in the [`EffectCache`].
pub fn allocate_effects(
    mut commands: Commands,
    mut q_extracted_effects: Query<
        (
            Entity,
            &ExtractedEffect,
            Has<ChildEffectOf>,
            Option<&mut CachedEffect>,
        ),
        Changed<ExtractedEffect>,
    >,
    mut effect_cache: ResMut<EffectCache>,
) {
    #[cfg(feature = "trace")]
    let _span = bevy::log::info_span!("allocate_effects").entered();
    trace!("allocate_effects");

    for (entity, extracted_effect, has_parent, maybe_cached_effect) in &mut q_extracted_effects {
        // Insert or update the effect into the EffectCache
        if let Some(mut cached_effect) = maybe_cached_effect {
            trace!("Updating EffectCache entry for entity {entity:?}...");
            let _ = effect_cache.remove(cached_effect.as_ref());
            *cached_effect = effect_cache.insert(
                extracted_effect.handle.clone(),
                extracted_effect.capacity,
                &extracted_effect.particle_layout,
            );
        } else {
            trace!("Allocating new entry in EffectCache for entity {entity:?}...");
            let cached_effect = effect_cache.insert(
                extracted_effect.handle.clone(),
                extracted_effect.capacity,
                &extracted_effect.particle_layout,
            );
            commands.entity(entity).insert(cached_effect);
        }

        // Ensure the particle@1 bind group layout exists for the given configuration of
        // particle layout. We do this here only for effects without a parent; for those
        // with a parent, we'll do it after we resolved that parent.
        if !has_parent {
            let parent_min_binding_size = None;
            effect_cache.ensure_particle_bind_group_layout_desc(
                extracted_effect.particle_layout.min_binding_size32(),
                parent_min_binding_size,
            );
        }

        // Ensure the metadata@3 bind group layout exists for the init pass.
        {
            let consume_gpu_spawn_events = extracted_effect
                .layout_flags
                .contains(LayoutFlags::CONSUME_GPU_SPAWN_EVENTS);
            effect_cache.ensure_metadata_init_bind_group_layout_desc(consume_gpu_spawn_events);
        }
    }
}

/// Update any cached mesh info based on any relocation done by Bevy itself.
///
/// Bevy will merge small meshes into larger GPU buffers automatically. When
/// this happens, the mesh location changes, and we need to update our
/// references to it in order to know how to issue the draw commands.
///
/// This system updates both the [`CachedMeshLocation`] and the
/// [`CachedIndirectDrawArgs`] components.
pub fn update_mesh_locations(
    mut commands: Commands,
    mut effects_meta: ResMut<EffectsMeta>,
    mesh_allocator: Res<MeshAllocator>,
    render_meshes: Res<RenderAssets<RenderMesh>>,
    mut q_cached_effects: Query<(
        Entity,
        &ExtractedEffectMesh,
        Option<&mut CachedMeshLocation>,
        Option<&mut CachedDrawIndirectArgs>,
    )>,
) {
    #[cfg(feature = "trace")]
    let _span = bevy::log::info_span!("update_mesh_locations").entered();
    trace!("update_mesh_locations");

    for (entity, extracted_mesh, maybe_cached_mesh_location, maybe_cached_draw_indirect_args) in
        &mut q_cached_effects
    {
        let mut cmds = commands.entity(entity);

        // Resolve the render mesh
        let Some(render_mesh) = render_meshes.get(extracted_mesh.mesh) else {
            warn!(
                "Cannot find render mesh of particle effect instance on entity {:?}, despite applying default mesh. Invalid asset handle: {:?}",
                entity, extracted_mesh.mesh
            );
            cmds.remove::<CachedMeshLocation>();
            continue;
        };

        // Find the location where the render mesh was allocated. This is handled by
        // Bevy itself in the allocate_and_free_meshes() system. Bevy might
        // re-batch the vertex and optional index data of meshes together at any point,
        // so we need to confirm that the location data we may have cached is still
        // valid.
        let Some(mesh_vertex_buffer_slice) = mesh_allocator.mesh_vertex_slice(&extracted_mesh.mesh)
        else {
            trace!(
                "Effect main_entity {:?}: cannot find vertex slice of render mesh {:?}",
                entity,
                extracted_mesh.mesh
            );
            cmds.remove::<CachedMeshLocation>();
            continue;
        };
        let mesh_index_buffer_slice = mesh_allocator.mesh_index_slice(&extracted_mesh.mesh);
        let indexed =
            if let RenderMeshBufferInfo::Indexed { index_format, .. } = render_mesh.buffer_info {
                if let Some(ref slice) = mesh_index_buffer_slice {
                    Some(MeshIndexSlice {
                        format: index_format,
                        buffer: slice.buffer.clone(),
                        range: slice.range.clone(),
                    })
                } else {
                    trace!(
                        "Effect main_entity {:?}: cannot find index slice of render mesh {:?}",
                        entity,
                        extracted_mesh.mesh
                    );
                    cmds.remove::<CachedMeshLocation>();
                    continue;
                }
            } else {
                None
            };

        // Calculate the new draw args and mesh location based on Bevy's info
        let new_draw_args = AnyDrawIndirectArgs::from_slices(
            &mesh_vertex_buffer_slice,
            mesh_index_buffer_slice.as_ref(),
        );
        let new_mesh_location = match &mesh_index_buffer_slice {
            // Indexed mesh rendering
            Some(mesh_index_buffer_slice) => CachedMeshLocation {
                vertex_buffer: mesh_vertex_buffer_slice.buffer.id(),
                vertex_or_index_count: mesh_index_buffer_slice.range.len() as u32,
                first_index_or_vertex_offset: mesh_index_buffer_slice.range.start,
                vertex_offset_or_base_instance: mesh_vertex_buffer_slice.range.start as i32,
                indexed,
            },
            // Non-indexed mesh rendering
            None => CachedMeshLocation {
                vertex_buffer: mesh_vertex_buffer_slice.buffer.id(),
                vertex_or_index_count: mesh_vertex_buffer_slice.range.len() as u32,
                first_index_or_vertex_offset: mesh_vertex_buffer_slice.range.start,
                vertex_offset_or_base_instance: 0,
                indexed: None,
            },
        };

        // We don't allocate the draw indirect args ahead of time because we need to
        // select the indexed vs. non-indexed buffer. Now that we know whether the mesh
        // is indexed, we can allocate it (or reallocate it if indexing mode changed).
        if let Some(mut cached_draw_indirect) = maybe_cached_draw_indirect_args {
            assert!(cached_draw_indirect.row.is_valid());

            // If the GPU draw args changed, re-upload to GPU.
            if new_draw_args != cached_draw_indirect.args {
                debug!(
                    "Indirect draw args changed for asset {:?}\nold:{:?}\nnew:{:?}",
                    entity, cached_draw_indirect.args, new_draw_args
                );
                cached_draw_indirect.args = new_draw_args;
                effects_meta.update_draw_indirect(cached_draw_indirect.as_ref());
            }
        } else {
            cmds.insert(effects_meta.allocate_draw_indirect(&new_draw_args));
        }

        // Compare to any cached data and update if necessary, or insert if missing.
        // This will trigger change detection in the ECS, which will in turn trigger
        // GpuEffectMetadata re-upload.
        if let Some(mut old_mesh_location) = maybe_cached_mesh_location {
            if *old_mesh_location != new_mesh_location {
                debug!(
                    "Mesh location changed for asset {:?}\nold:{:?}\nnew:{:?}",
                    entity, old_mesh_location, new_mesh_location
                );
                *old_mesh_location = new_mesh_location;
            }
        } else {
            cmds.insert(new_mesh_location);
        }
    }
}

/// Allocate an entry in the GPU table for any [`CachedEffectMetadata`] missing
/// one.
///
/// This system does NOT take care of (re-)uploading recent CPU data to GPU.
/// This is done much later in the frame, after batching and once all data for
/// it is ready. But it's necessary to ensure the allocation is determined
/// already ahead of time, in order to do batching of contiguous metadata
/// blocks (TODO; not currently used, also may end up using binary search in
/// shader, in which case we won't need continguous-ness and can maybe remove
/// this system).
// TODO - consider using observer OnAdd instead?
pub fn allocate_metadata(
    mut effects_meta: ResMut<EffectsMeta>,
    mut q_metadata: Query<&mut CachedEffectMetadata>,
) {
    for mut metadata in &mut q_metadata {
        if !metadata.table_id.is_valid() {
            metadata.table_id = effects_meta
                .effect_metadata_buffer
                .insert(metadata.metadata);
        } else {
            // Unless this is the first time we allocate the GPU entry (above),
            // we should never reach the beginning of this frame
            // with a changed metadata which has not
            // been re-uploaded last frame.
            // NO! We can only detect the change *since last run of THIS system*
            // so wont' see that a latter system the data.
            // assert!(!metadata.is_changed());
        }
    }
}

/// Update the [`CachedParentInfo`] of parent effects and the
/// [`CachedChildInfo`] of child effects.
pub fn allocate_parent_child_infos(
    mut commands: Commands,
    mut effect_cache: ResMut<EffectCache>,
    mut event_cache: ResMut<EventCache>,
    // All extracted child effects. May or may not already have a CachedChildInfo. If not, this
    // will be spawned below.
    mut q_child_effects: Query<(
        Entity,
        &ExtractedEffect,
        &ChildEffectOf,
        &CachedEffectEvents,
        Option<&mut CachedChildInfo>,
    )>,
    // All parent effects from a previous frame (already have CachedParentInfo), which can be
    // updated in-place without spawning a new CachedParentInfo.
    mut q_parent_effects: Query<(
        Entity,
        &ExtractedEffect,
        &CachedEffect,
        &ChildrenEffects,
        Option<&mut CachedParentInfo>,
    )>,
) {
    #[cfg(feature = "trace")]
    let _span = bevy::log::info_span!("allocate_child_infos").entered();
    trace!("allocate_child_infos");

    // Loop on all child effects and ensure their CachedChildInfo is up-to-date.
    for (child_entity, _, child_effect_of, cached_effect_events, maybe_cached_child_info) in
        &mut q_child_effects
    {
        // Fetch the parent effect
        let parent_entity = child_effect_of.parent;
        let Ok((_, _, parent_cached_effect, children_effects, _)) =
            q_parent_effects.get(parent_entity)
        else {
            warn!("Unknown parent #{parent_entity:?} on child entity {child_entity:?}, removing CachedChildInfo.");
            if maybe_cached_child_info.is_some() {
                commands.entity(child_entity).remove::<CachedChildInfo>();
            }
            continue;
        };

        // Find the index of this child entity in its parent's storage
        let Some(local_child_index) = children_effects.0.iter().position(|e| *e == child_entity)
        else {
            warn!("Cannot find child entity {child_entity:?} in the children collection of parent entity {parent_entity:?}. Relationship desync?");
            if maybe_cached_child_info.is_some() {
                commands.entity(child_entity).remove::<CachedChildInfo>();
            }
            continue;
        };
        let local_child_index = local_child_index as u32;

        // Fetch the effect buffer of the parent effect
        let Some(parent_buffer_binding_source) = effect_cache
            .get_slab(&parent_cached_effect.slab_id)
            .map(|effect_buffer| effect_buffer.max_binding_source())
        else {
            warn!(
                "Unknown parent slab #{} on parent entity {:?}, removing CachedChildInfo.",
                parent_cached_effect.slab_id.index(),
                parent_entity
            );
            if maybe_cached_child_info.is_some() {
                commands.entity(child_entity).remove::<CachedChildInfo>();
            }
            continue;
        };

        let new_cached_child_info = CachedChildInfo {
            parent_slab_id: parent_cached_effect.slab_id,
            parent_slab_offset: parent_cached_effect.slice.range().start,
            parent_particle_layout: parent_cached_effect.slice.particle_layout.clone(),
            parent_buffer_binding_source,
            local_child_index,
            global_child_index: u32::MAX, // fixed up later by fixup_parents()
            init_indirect_dispatch_index: cached_effect_events.init_indirect_dispatch_index,
        };
        if let Some(mut cached_child_info) = maybe_cached_child_info {
            if !cached_child_info.is_locally_equal(&new_cached_child_info) {
                *cached_child_info = new_cached_child_info;
            }
        } else {
            commands.entity(child_entity).insert(new_cached_child_info);
        }
    }

    // Loop on all parent effects and ensure their CachedParentInfo is up-to-date.
    for (parent_entity, parent_extracted_effect, _, children_effects, maybe_cached_parent_info) in
        &mut q_parent_effects
    {
        let parent_min_binding_size = parent_extracted_effect.particle_layout.min_binding_size32();

        // Loop over children and gather GpuChildInfo
        let mut new_children = Vec::with_capacity(children_effects.0.len());
        let mut new_child_infos = Vec::with_capacity(children_effects.0.len());
        for child_entity in children_effects.0.iter() {
            // Fetch the child's event buffer allocation info
            let Ok((_, child_extracted_effect, _, cached_effect_events, _)) =
                q_child_effects.get(*child_entity)
            else {
                warn!("Child entity {child_entity:?} from parent entity {parent_entity:?} didnt't resolve to a child instance. The parent effect cannot be processed.");
                if maybe_cached_parent_info.is_some() {
                    commands.entity(parent_entity).remove::<CachedParentInfo>();
                }
                break;
            };

            // Fetch the GPU event buffer of the child
            let Some(event_buffer) = event_cache.get_buffer(cached_effect_events.buffer_index)
            else {
                warn!("Child entity {child_entity:?} from parent entity {parent_entity:?} doesn't have an allocated GPU event buffer. The parent effect cannot be processed.");
                break;
            };

            let buffer_binding_source = BufferBindingSource {
                buffer: event_buffer.clone(),
                offset: cached_effect_events.range.start,
                size: NonZeroU32::new(cached_effect_events.range.len() as u32).unwrap(),
            };
            new_children.push((*child_entity, buffer_binding_source));

            new_child_infos.push(GpuChildInfo {
                event_count: 0,
                init_indirect_dispatch_index: cached_effect_events.init_indirect_dispatch_index,
            });

            // Ensure the particle@1 bind group layout exists for the given configuration of
            // particle layout. We do this here only for effects with a parent; for those
            // without a parent, we already did this in allocate_effects().
            effect_cache.ensure_particle_bind_group_layout_desc(
                child_extracted_effect.particle_layout.min_binding_size32(),
                Some(parent_min_binding_size),
            );
        }

        // If we don't have all children, just abort this effect. We don't try to have
        // partial relationships, this is too complex for shader bindings.
        debug_assert_eq!(new_children.len(), new_child_infos.len());
        if (new_children.len() < children_effects.len()) && maybe_cached_parent_info.is_some() {
            warn!("One or more child effect(s) on parent effect {parent_entity:?} failed to configure. The parent effect cannot be processed.");
            commands.entity(parent_entity).remove::<CachedParentInfo>();
            continue;
        }

        // Insert or update the CachedParentInfo component of the parent effect
        if let Some(mut cached_parent_info) = maybe_cached_parent_info {
            if cached_parent_info.children != new_children {
                // FIXME - missing way to just update in-place without changing the allocation
                // size!
                // if cached_parent_info.children.len() == new_children.len() {
                //} else {
                event_cache.reallocate_child_infos(
                    parent_entity,
                    new_children,
                    &new_child_infos[..],
                    cached_parent_info.as_mut(),
                );
                //}
            }
        } else {
            let cached_parent_info =
                event_cache.allocate_child_infos(parent_entity, new_children, &new_child_infos[..]);
            commands.entity(parent_entity).insert(cached_parent_info);
        }
    }
}

/// Prepare the init and update compute pipelines for an effect.
///
/// This caches the pipeline IDs once resolved, and their compiling state when
/// it changes, to determine when an effect is ready to be used.
///
/// Note that we do that proactively even if the effect will be skipped this
/// frame (for example because it's not visible). This ensures we queue pipeline
/// compilations ASAP, as they can take a long time (10+ frames). We also use
/// the pipeline compiling state, which we query here, to inform whether the
/// effect is ready for this frame. So in general if this is a new pipeline, it
/// won't be ready this frame.
pub fn prepare_init_update_pipelines(
    mut q_effects: Query<(
        Entity,
        &ExtractedEffect,
        &CachedEffect,
        Option<&CachedChildInfo>,
        Option<&CachedParentInfo>,
        Option<&CachedEffectProperties>,
        &mut CachedPipelines,
    )>,
    // FIXME - need mut for bind group layout creation; shouldn't be create there though
    mut effect_cache: ResMut<EffectCache>,
    pipeline_cache: Res<PipelineCache>,
    property_cache: ResMut<PropertyCache>,
    init_pipeline: Res<ParticlesInitPipeline>,
    update_pipeline: Res<ParticlesUpdatePipeline>,
    mut specialized_init_pipelines: ResMut<SpecializedComputePipelines<ParticlesInitPipeline>>,
    mut specialized_update_pipelines: ResMut<SpecializedComputePipelines<ParticlesUpdatePipeline>>,
) {
    #[cfg(feature = "trace")]
    let _span = bevy::log::info_span!("prepare_init_update_pipelines").entered();
    trace!("prepare_init_update_pipelines");

    // Note: As of Bevy 0.16 we can't evict old pipelines from the cache. They're
    // inserted forever. https://github.com/bevyengine/bevy/issues/19925

    for (
        entity,
        extracted_effect,
        cached_effect,
        maybe_cached_child_info,
        maybe_cached_parent_info,
        maybe_cached_properties,
        mut cached_pipelines,
    ) in &mut q_effects
    {
        trace!(
            "Preparing pipelines for effect {:?}... (flags: {:?})",
            entity,
            cached_pipelines.flags
        );

        let particle_layout = &cached_effect.slice.particle_layout;
        let particle_layout_min_binding_size = particle_layout.min_binding_size32();
        let has_event_buffer = maybe_cached_child_info.is_some();
        let parent_particle_layout_min_binding_size = maybe_cached_child_info
            .as_ref()
            .map(|cci| cci.parent_particle_layout.min_binding_size32());

        let Some(particle_bind_group_layout_desc) = effect_cache.particle_bind_group_layout_desc(
            particle_layout_min_binding_size,
            parent_particle_layout_min_binding_size,
        ) else {
            error!("Failed to find particle sim bind group @1 for min_binding_size={} parent_min_binding_size={:?}",
                particle_layout_min_binding_size, parent_particle_layout_min_binding_size);
            continue;
        };
        let particle_bind_group_layout_desc = particle_bind_group_layout_desc.clone();

        // This should always exist by the time we reach this point, because we should
        // have inserted any property in the cache, which would have allocated the
        // proper bind group layout (or the default no-property one).
        let property_layout_min_binding_size =
            maybe_cached_properties.map(|cp| cp.property_layout.min_binding_size());
        let spawner_bind_group_layout_desc = property_cache
            .bind_group_layout_desc(
                property_layout_min_binding_size,
                true,
                &extracted_effect.texture_layout,
            )
            .unwrap_or_else(|| {
                panic!(
                    "Failed to find spawner@2 bind group layout for property binding size {:?} and texture layout {:?}",
                    property_layout_min_binding_size,
                    extracted_effect.texture_layout,
                )
            });
        trace!(
            "Retrieved spawner@2 bind group layout desc for property binding size {}:  {:?}.",
            property_layout_min_binding_size
                .as_ref()
                .map(|size| size.get())
                .unwrap_or(0),
            spawner_bind_group_layout_desc,
        );

        // Resolve the init pipeline
        let init_pipeline_id = if let Some(init_pipeline_id) = cached_pipelines.init.as_ref() {
            *init_pipeline_id
        } else {
            // Clear flag just in case, to ensure consistency.
            cached_pipelines
                .flags
                .remove(CachedPipelineFlags::INIT_PIPELINE_READY);

            // Fetch the metadata@3 bind group layout from the cache
            let metadata_bind_group_layout_desc = effect_cache
                .metadata_init_bind_group_layout_desc(has_event_buffer)
                .unwrap()
                .clone();

            let init_pipeline_key_flags = {
                let mut flags = ParticleInitPipelineKeyFlags::empty();
                flags.set(
                    ParticleInitPipelineKeyFlags::ATTRIBUTE_PREV,
                    particle_layout.contains(Attribute::PREV),
                );
                flags.set(
                    ParticleInitPipelineKeyFlags::ATTRIBUTE_NEXT,
                    particle_layout.contains(Attribute::NEXT),
                );
                flags.set(
                    ParticleInitPipelineKeyFlags::CONSUME_GPU_SPAWN_EVENTS,
                    has_event_buffer,
                );
                flags
            };

            let init_pipeline_id: CachedComputePipelineId = specialized_init_pipelines.specialize(
                pipeline_cache.as_ref(),
                &init_pipeline,
                ParticleInitPipelineKey {
                    shader: extracted_effect.effect_shaders.init.clone(),
                    particle_layout_min_binding_size,
                    parent_particle_layout_min_binding_size,
                    flags: init_pipeline_key_flags,
                    particle_bind_group_layout_desc: particle_bind_group_layout_desc.clone(),
                    spawner_bind_group_layout_desc: spawner_bind_group_layout_desc.clone(),
                    metadata_bind_group_layout_desc,
                },
            );
            trace!("Init pipeline specialized: id={:?}", init_pipeline_id);

            cached_pipelines.init = Some(init_pipeline_id);
            init_pipeline_id
        };

        // Resolve the update pipeline
        let update_pipeline_id = if let Some(update_pipeline_id) = cached_pipelines.update.as_ref()
        {
            *update_pipeline_id
        } else {
            // Clear flag just in case, to ensure consistency.
            cached_pipelines
                .flags
                .remove(CachedPipelineFlags::UPDATE_PIPELINE_READY);

            let num_event_buffers = maybe_cached_parent_info
                .as_ref()
                .map(|p| p.children.len() as u32)
                .unwrap_or_default();

            let local_space_simulation = extracted_effect
                .layout_flags
                .contains(LayoutFlags::LOCAL_SPACE_SIMULATION);

            // FIXME: currently don't hava a way to determine when this is needed, because
            // we know the number of children per parent only after resolving
            // all parents, but by that point we forgot if this is a newly added
            // effect or not. So since we need to re-ensure for all effects, not
            // only new ones, might as well do here...
            effect_cache.ensure_metadata_update_bind_group_layout_desc(num_event_buffers);

            // Fetch the bind group layouts from the cache
            let metadata_bind_group_layout_desc = effect_cache
                .metadata_update_bind_group_layout_desc(num_event_buffers)
                .unwrap()
                .clone();

            let update_pipeline_id = specialized_update_pipelines.specialize(
                pipeline_cache.as_ref(),
                &update_pipeline,
                ParticleUpdatePipelineKey {
                    shader: extracted_effect.effect_shaders.update.clone(),
                    particle_layout: particle_layout.clone(),
                    local_space_simulation,
                    parent_particle_layout_min_binding_size,
                    num_event_buffers,
                    particle_bind_group_layout_desc: particle_bind_group_layout_desc.clone(),
                    spawner_bind_group_layout_desc: spawner_bind_group_layout_desc.clone(),
                    metadata_bind_group_layout_desc,
                },
            );
            trace!("Update pipeline specialized: id={:?}", update_pipeline_id);

            cached_pipelines.update = Some(update_pipeline_id);
            update_pipeline_id
        };

        // Never batch an effect with a pipeline not available; this will prevent its
        // init/update pass from running, but the vfx_indirect pass will run
        // nonetheless, which causes desyncs and leads to bugs.
        if pipeline_cache
            .get_compute_pipeline(init_pipeline_id)
            .is_none()
        {
            match pipeline_cache.get_compute_pipeline_state(init_pipeline_id)
            {
                CachedPipelineState::Err(err) => error!("Skipping effect from render entity {:?} due to missing or not ready init pipeline (error: {:?})", entity, err),
                other => trace!(
                    "Skipping effect from render entity {:?} due to missing or not ready init pipeline (status: {:?})",
                    entity,
                    other
                ),
            }
            cached_pipelines
                .flags
                .remove(CachedPipelineFlags::INIT_PIPELINE_READY);
            continue;
        }

        // PipelineCache::get_compute_pipeline() only returns a value if the pipeline is
        // ready
        cached_pipelines
            .flags
            .insert(CachedPipelineFlags::INIT_PIPELINE_READY);
        trace!("[Effect {:?}] Init pipeline ready.", entity);

        // Never batch an effect with a pipeline not available; this will prevent its
        // init/update pass from running, but the vfx_indirect pass will run
        // nonetheless, which causes desyncs and leads to bugs.
        if pipeline_cache
            .get_compute_pipeline(update_pipeline_id)
            .is_none()
        {
            trace!(
                "Skipping effect from render entity {:?} due to missing or not ready update pipeline (status: {:?})",
                entity,
                pipeline_cache.get_compute_pipeline_state(update_pipeline_id)
            );
            cached_pipelines
                .flags
                .remove(CachedPipelineFlags::UPDATE_PIPELINE_READY);
            continue;
        }

        // PipelineCache::get_compute_pipeline() only returns a value if the pipeline is
        // ready
        cached_pipelines
            .flags
            .insert(CachedPipelineFlags::UPDATE_PIPELINE_READY);
        trace!("[Effect {:?}] Update pipeline ready.", entity);
    }
}

pub fn prepare_indirect_pipeline(
    event_cache: Res<EventCache>,
    mut effects_meta: ResMut<EffectsMeta>,
    pipeline_cache: Res<PipelineCache>,
    indirect_pipeline: Res<DispatchIndirectPipeline>,
    mut specialized_indirect_pipelines: ResMut<
        SpecializedComputePipelines<DispatchIndirectPipeline>,
    >,
    prefix_sum_pipeline: Res<PrefixSumPipeline>,
) {
    // Queue the vfx_prefix_sum pipeline if not already ready
    // TODO - not really related to this system, should probably be moved?
    if effects_meta.prefix_sum_pipeline_id == CachedComputePipelineId::INVALID {
        effects_meta.prefix_sum_pipeline_id =
            pipeline_cache.queue_compute_pipeline(prefix_sum_pipeline.make_pipeline_desc())
    }

    // Ensure the 2 variants of the indirect pipelines are created.
    // TODO - move that elsewhere in some one-time setup?
    if effects_meta.indirect_pipeline_ids[0] == CachedComputePipelineId::INVALID {
        effects_meta.indirect_pipeline_ids[0] = specialized_indirect_pipelines.specialize(
            pipeline_cache.as_ref(),
            &indirect_pipeline,
            DispatchIndirectPipelineKey { has_events: false },
        );
    }
    if effects_meta.indirect_pipeline_ids[1] == CachedComputePipelineId::INVALID {
        effects_meta.indirect_pipeline_ids[1] = specialized_indirect_pipelines.specialize(
            pipeline_cache.as_ref(),
            &indirect_pipeline,
            DispatchIndirectPipelineKey { has_events: true },
        );
    }

    // Select the active one depending on whether there's any child info to consume
    let is_empty = event_cache.child_infos().is_empty();
    if effects_meta.active_indirect_pipeline_id == CachedComputePipelineId::INVALID {
        if is_empty {
            effects_meta.active_indirect_pipeline_id = effects_meta.indirect_pipeline_ids[0];
        } else {
            effects_meta.active_indirect_pipeline_id = effects_meta.indirect_pipeline_ids[1];
        }
    } else {
        // If this is the first time we insert an event buffer, we need to switch the
        // indirect pass from non-event to event mode. That is, we need to re-allocate
        // the pipeline with the child infos buffer binding. Conversely, if there's no
        // more effect using GPU spawn events, we can deallocate.
        let was_empty =
            effects_meta.active_indirect_pipeline_id == effects_meta.indirect_pipeline_ids[0];
        if was_empty && !is_empty {
            trace!("First event buffer inserted; switching indirect pass to event mode...");
            effects_meta.active_indirect_pipeline_id = effects_meta.indirect_pipeline_ids[1];
        } else if is_empty && !was_empty {
            trace!("Last event buffer removed; switching indirect pass to no-event mode...");
            effects_meta.active_indirect_pipeline_id = effects_meta.indirect_pipeline_ids[0];
        }
    }
}

// TEMP - Mark all cached effects as invalid for this frame until another system
// explicitly marks them as valid. Otherwise we early out in some parts, and
// reuse by mistake the previous frame's extraction.
pub fn clear_transient_batch_inputs(
    mut commands: Commands,
    mut q_cached_effects: Query<Entity, With<BatchInput>>,
) {
    for entity in &mut q_cached_effects {
        if let Ok(mut cmd) = commands.get_entity(entity) {
            cmd.remove::<BatchInput>();
        }
    }
}

/// Effect mesh extracted from the main world.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Component)]
pub(crate) struct ExtractedEffectMesh {
    /// Asset of the effect mesh to draw.
    pub mesh: AssetId<Mesh>,
}

/// Indexed mesh metadata for [`CachedMesh`].
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub(crate) struct MeshIndexSlice {
    /// Index format.
    pub format: IndexFormat,
    /// GPU buffer containing the indices.
    pub buffer: Buffer,
    /// Range inside [`Self::buffer`] where the indices are.
    pub range: Range<u32>,
}

impl PartialEq for MeshIndexSlice {
    fn eq(&self, other: &Self) -> bool {
        self.format == other.format
            && self.buffer.id() == other.buffer.id()
            && self.range == other.range
    }
}

impl Eq for MeshIndexSlice {}

/// Cached info about a mesh location in a Bevy buffer. This information is
/// uploaded to GPU into [`GpuEffectMetadata`] for indirect rendering, but is
/// also kept CPU side in this component to detect when Bevy relocated a mesh,
/// so we can invalidate that GPU data.
#[derive(Debug, Clone, PartialEq, Eq, Component)]
pub(crate) struct CachedMeshLocation {
    /// Vertex buffer.
    pub vertex_buffer: BufferId,
    /// See [`GpuEffectMetadata::vertex_or_index_count`].
    pub vertex_or_index_count: u32,
    /// See [`GpuEffectMetadata::first_index_or_vertex_offset`].
    pub first_index_or_vertex_offset: u32,
    /// See [`GpuEffectMetadata::vertex_offset_or_base_instance`].
    pub vertex_offset_or_base_instance: i32,
    /// Indexed rendering metadata.
    pub indexed: Option<MeshIndexSlice>,
}

bitflags! {
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub struct CachedPipelineFlags: u8 {
        const NONE = 0;
        /// The init pipeline for this effect is ready for use. This means the compute pipeline is compiled and cached.
        const INIT_PIPELINE_READY = (1u8 << 0);
        /// The update pipeline for this effect is ready for use. This means the compute pipeline is compiled and cached.
        const UPDATE_PIPELINE_READY = (1u8 << 1);
    }
}

impl Default for CachedPipelineFlags {
    fn default() -> Self {
        Self::NONE
    }
}

/// Render world cached shader pipelines for a [`CachedEffect`].
///
/// This is updated with the IDs of the pipelines when they are queued for
/// compiling, and with the state of those pipelines to detect when the effect
/// is ready to be used.
///
/// This component is always auto-inserted alongside [`ExtractedEffect`] as soon
/// as a new effect instance is spawned, because it contains the readiness state
/// of those pipelines, which we want to query each frame. The pipelines are
/// also mandatory, so this component is always needed.
#[derive(Debug, Default, Component)]
pub(crate) struct CachedPipelines {
    /// Caching flags indicating the pipelines readiness.
    pub flags: CachedPipelineFlags,
    /// ID of the cached init pipeline. This is valid once the pipeline is
    /// queued for compilation, but this doesn't mean the pipeline is ready for
    /// use. Readiness is encoded in [`Self::flags`].
    pub init: Option<CachedComputePipelineId>,
    /// ID of the cached update pipeline. This is valid once the pipeline is
    /// queued for compilation, but this doesn't mean the pipeline is ready for
    /// use. Readiness is encoded in [`Self::flags`].
    pub update: Option<CachedComputePipelineId>,
}

impl CachedPipelines {
    /// Check if all pipelines for this effect are ready.
    #[inline]
    pub fn is_ready(&self) -> bool {
        self.flags.contains(
            CachedPipelineFlags::INIT_PIPELINE_READY | CachedPipelineFlags::UPDATE_PIPELINE_READY,
        )
    }
}

/// Ready state for this effect.
///
/// An effect is ready if:
/// - Its init and update pipelines are ready, as reported by
///   [`CachedPipelines::is_ready()`].
///
/// This components holds the calculated ready state propagated from all
/// ancestor effects, if any. That propagation is done by the
/// [`propagate_ready_state()`] system.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Component)]
pub(crate) struct CachedReadyState {
    is_ready: bool,
}

impl CachedReadyState {
    #[inline(always)]
    pub fn new(is_ready: bool) -> Self {
        Self { is_ready }
    }

    #[inline(always)]
    pub fn and(mut self, ancestors_ready: bool) -> Self {
        self.and_with(ancestors_ready);
        self
    }

    #[inline(always)]
    pub fn and_with(&mut self, ancestors_ready: bool) {
        self.is_ready = self.is_ready && ancestors_ready;
    }

    #[inline(always)]
    pub fn is_ready(&self) -> bool {
        self.is_ready
    }
}

#[derive(SystemParam)]
pub struct PrepareEffectsReadOnlyParams<'w, 's> {
    sim_params: Res<'w, SimParams>,
    marker: PhantomData<&'s usize>,
}

/// Update the ready state of all effects, and propagate recursively to
/// children.
pub(crate) fn propagate_ready_state(
    mut q_root_effects: Query<
        (
            Entity,
            Option<&ChildrenEffects>,
            Ref<CachedPipelines>,
            &mut CachedReadyState,
        ),
        Without<ChildEffectOf>,
    >,
    mut orphaned: RemovedComponents<ChildEffectOf>,
    q_ready_state: Query<
        (
            Ref<CachedPipelines>,
            &mut CachedReadyState,
            Option<&ChildrenEffects>,
        ),
        With<ChildEffectOf>,
    >,
    q_child_effects: Query<(Entity, Ref<ChildEffectOf>), With<CachedReadyState>>,
    mut orphaned_entities: Local<Vec<Entity>>,
) {
    #[cfg(feature = "trace")]
    let _span = bevy::log::info_span!("propagate_ready_state").entered();
    trace!("propagate_ready_state");

    // Update orphaned list for this frame, and sort it so we can efficiently binary
    // search it
    orphaned_entities.clear();
    orphaned_entities.extend(orphaned.read());
    orphaned_entities.sort_unstable();

    // Iterate in parallel over all root effects (those without any parent). This is
    // the most common case, so should take care of the heavy lifting of propagating
    // to most effects. For child effects, we then descend recursively.
    q_root_effects.par_iter_mut().for_each(
        |(entity, maybe_children, cached_pipelines, mut cached_ready_state)| {
            // Update the ready state of this root effect
            let changed = cached_pipelines.is_changed() || cached_ready_state.is_added() || orphaned_entities.binary_search(&entity).is_ok();
            trace!("[Entity {}] changed={} cached_pipelines={} ready_state={}", entity, changed, cached_pipelines.is_ready(), cached_ready_state.is_ready);
            if changed {
                // Root effects by default are ready since they have no ancestors to check. After that we check the ready conditions for this effect alone.
                let new_ready_state = CachedReadyState::new(cached_pipelines.is_ready());
                if *cached_ready_state != new_ready_state {
                    debug!(
                        "[Entity {}] Changed ready to: {}",
                        entity,
                        new_ready_state.is_ready()
                    );
                    *cached_ready_state = new_ready_state;
                }
            }

            // Recursively update the ready state of its descendants
            if let Some(children) = maybe_children {
                for (child, child_of) in q_child_effects.iter_many(children).flatten() {
                    assert_eq!(
                        child_of.parent, entity,
                        "Malformed hierarchy. This probably means that your hierarchy has been improperly maintained, or contains a cycle"
                    );
                    // SAFETY:
                    // - `child` must have consistent parentage, or the above assertion would panic.
                    //   Since `child` is parented to a root entity, the entire hierarchy leading to it
                    //   is consistent.
                    // - We may operate as if all descendants are consistent, since
                    //   `propagate_ready_state_recursive` will panic before continuing to propagate if it
                    //   encounters an entity with inconsistent parentage.
                    // - Since each root entity is unique and the hierarchy is consistent and
                    //   forest-like, other root entities' `propagate_ready_state_recursive` calls will not conflict
                    //   with this one.
                    // - Since this is the only place where `transform_query` gets used, there will be
                    //   no conflicting fetches elsewhere.
                    #[expect(unsafe_code, reason = "`propagate_ready_state_recursive()` is unsafe due to its use of `Query::get_unchecked()`.")]
                    unsafe {
                        propagate_ready_state_recursive(
                            &cached_ready_state,
                            &q_ready_state,
                            &q_child_effects,
                            child,
                            changed || child_of.is_changed(),
                        );
                    }
                }
            }
        },
    );
}

#[expect(
    unsafe_code,
    reason = "This function uses `Query::get_unchecked()`, which can result in multiple mutable references if the preconditions are not met."
)]
unsafe fn propagate_ready_state_recursive(
    parent_state: &CachedReadyState,
    q_ready_state: &Query<
        (
            Ref<CachedPipelines>,
            &mut CachedReadyState,
            Option<&ChildrenEffects>,
        ),
        With<ChildEffectOf>,
    >,
    q_child_of: &Query<(Entity, Ref<ChildEffectOf>), With<CachedReadyState>>,
    entity: Entity,
    mut changed: bool,
) {
    // Update this effect in-place by checking its own state and the state of its
    // parent (which has already been propagated from all the parent's ancestors, so
    // is correct for this frame).
    let (cached_ready_state, maybe_children) = {
        let Ok((cached_pipelines, mut cached_ready_state, maybe_children)) =
        // SAFETY: Copied from Bevy's transform propagation, same reasoning
        (unsafe { q_ready_state.get_unchecked(entity) }) else {
            return;
        };

        changed |= cached_pipelines.is_changed() || cached_ready_state.is_added();
        if changed {
            let new_ready_state =
                CachedReadyState::new(parent_state.is_ready()).and(cached_pipelines.is_ready());
            // Ensure we don't trigger ECS change detection here if state didn't change, so
            // we can avoid this effect branch on next iteration.
            if *cached_ready_state != new_ready_state {
                debug!(
                    "[Entity {}] Changed ready to: {}",
                    entity,
                    new_ready_state.is_ready()
                );
                *cached_ready_state = new_ready_state;
            }
        }
        (cached_ready_state, maybe_children)
    };

    // Recurse into descendants
    let Some(children) = maybe_children else {
        return;
    };
    for (child, child_of) in q_child_of.iter_many(children).flatten() {
        assert_eq!(
        child_of.parent, entity,
        "Malformed hierarchy. This probably means that your hierarchy has been improperly maintained, or contains a cycle"
    );
        // SAFETY: The caller guarantees that `transform_query` will not be fetched for
        // any descendants of `entity`, so it is safe to call
        // `propagate_recursive` for each child.
        //
        // The above assertion ensures that each child has one and only one unique
        // parent throughout the entire hierarchy.
        unsafe {
            propagate_ready_state_recursive(
                cached_ready_state.as_ref(),
                q_ready_state,
                q_child_of,
                child,
                changed || child_of.is_changed(),
            );
        }
    }
}

/// Once all effects are extracted and all cached components are updated, it's
/// time to prepare for sorting and batching. Collect all relevant data and
/// insert/update the [`BatchInput`] for each effect.
pub(crate) fn prepare_batch_inputs(
    render_device: Res<RenderDevice>,
    mut commands: Commands,
    read_only_params: PrepareEffectsReadOnlyParams,
    pipeline_cache: Res<PipelineCache>,
    mut effects_meta: ResMut<EffectsMeta>,
    q_cached_effects: Query<(
        MainEntity,
        Entity,
        &ExtractedEffect,
        &ExtractedSpawner,
        &CachedEffect,
        &CachedEffectMetadata,
        &CachedReadyState,
        &CachedPipelines,
        Option<&CachedDrawIndirectArgs>,
        Option<&CachedParentInfo>,
        Option<&ChildEffectOf>,
        Option<&CachedChildInfo>,
        Option<&CachedEffectEvents>,
    )>,
    mut sort_bind_groups: ResMut<SortBindGroups>,
    q_render_camera: Query<&HanabiRenderCamera>,
) {
    #[cfg(feature = "trace")]
    let _span = bevy::log::info_span!("prepare_batch_inputs").entered();
    trace!("prepare_batch_inputs");

    // Workaround for too many params in system (TODO: refactor to split work?)
    let sim_params = read_only_params.sim_params.into_inner();

    // Clear per-instance buffers, which are filled below and re-uploaded each frame
    effects_meta.spawner_buffer.clear();
    effects_meta.dispatch_indirect_args_buffer.clear();

    // Build batcher inputs from extracted effects, updating all cached components
    // for each effect on the fly.
    let mut extracted_effect_count = 0;
    let mut prepared_effect_count = 0;
    for (
        main_entity,
        render_entity,
        extracted_effect,
        extracted_spawner,
        cached_effect,
        cached_effect_metadata,
        cached_ready_state,
        cached_pipelines,
        maybe_cached_draw_indirect_args,
        maybe_cached_parent_info,
        maybe_child_effect_of,
        maybe_cached_child_info,
        maybe_cached_effect_events,
    ) in &q_cached_effects
    {
        extracted_effect_count += 1;

        // Skip this effect if not ready
        if !cached_ready_state.is_ready() {
            trace!("Pipelines not ready for effect {}, skipped.", render_entity);
            continue;
        }

        // Skip this effect if not visible and not simulating when hidden
        if !extracted_spawner.is_visible
            && (extracted_effect.simulation_condition == SimulationCondition::WhenVisible)
        {
            trace!(
                "Effect {} not visible, and simulation condition is WhenVisible, so skipped.",
                render_entity
            );
            continue;
        }

        // Fetch the init and update pipelines.
        // SAFETY: If is_ready() returns true, this means the pipelines are cached and
        // ready, so the IDs must be valid.
        let init_and_update_pipeline_ids = InitAndUpdatePipelineIds {
            init: cached_pipelines.init.unwrap(),
            update: cached_pipelines.update.unwrap(),
        };

        let effect_slice = EffectSlice {
            slice: cached_effect.slice.range(),
            slab_id: cached_effect.slab_id,
            particle_layout: cached_effect.slice.particle_layout.clone(),
        };

        // Fetch the bind group layouts from the cache
        trace!("child_effect_of={:?}", maybe_child_effect_of);
        let parent_slab_id = if let Some(child_effect_of) = maybe_child_effect_of {
            let Ok((_, _, _, _, parent_cached_effect, _, _, _, _, _, _, _, _)) =
                q_cached_effects.get(child_effect_of.parent)
            else {
                // At this point we should have discarded invalid effects with a missing parent,
                // so if the parent is not found this is a bug.
                error!(
                    "Effect main_entity {:?}: parent render entity {:?} not found.",
                    main_entity, child_effect_of.parent
                );
                continue;
            };
            Some(parent_cached_effect.slab_id)
        } else {
            None
        };

        // For ribbons, we need the sorting pipeline to be ready to sort the ribbon's
        // particles by age in order to build a contiguous mesh.
        if extracted_effect.layout_flags.raea_sorted() { // raea: and blended effects (#52)
            // Ensure the bind group layout for sort-fill is ready. This will also ensure
            // the pipeline is created and queued if needed.
            if let Err(err) = sort_bind_groups.ensure_sort_fill_bind_group_layout_desc(
                &render_device,
                &pipeline_cache,
                &extracted_effect.particle_layout,
            ) {
                error!(
                    "Failed to create bind group for ribbon effect sorting: {:?}",
                    err
                );
                continue;
            }

            // Check sort pipelines are ready, otherwise we might desync some buffers if
            // running only some of them but not all.
            if !sort_bind_groups
                .is_pipeline_ready(&extracted_effect.particle_layout, &pipeline_cache)
            {
                trace!(
                    "Sort pipeline not ready for effect on main entity {:?}; skipped.",
                    main_entity
                );
                continue;
            }
        }

        // Output some debug info
        trace!("init_shader = {:?}", extracted_effect.effect_shaders.init);
        trace!(
            "update_shader = {:?}",
            extracted_effect.effect_shaders.update
        );
        trace!(
            "render_shader = {:?}",
            extracted_effect.effect_shaders.render
        );
        trace!("layout_flags = {:?}", extracted_effect.layout_flags);
        trace!("particle_layout = {:?}", effect_slice.particle_layout);

        let parent_slab_offset = maybe_cached_child_info.map(|cci| cci.parent_slab_offset);

        assert!(cached_effect_metadata.table_id.is_valid());
        let gpu_spawner_params = GpuSpawnerParams::new(
            &extracted_spawner.transform,
            extracted_spawner.spawn_count,
            extracted_spawner.prng_seed,
            cached_effect.slice.range().start,
            parent_slab_offset,
            cached_effect_metadata.table_id,
            maybe_cached_draw_indirect_args,
        );

        trace!("Updating cached effect at entity {render_entity:?}...");
        let mut cmd = commands.entity(render_entity);
        // Inserting the BatchInput component marks the effect as ready for this frame
        cmd.insert(BatchInput {
            effect_slice,
            init_and_update_pipeline_ids,
            parent_slab_id,
            event_buffer_index: maybe_cached_effect_events.map(|cee| cee.buffer_index),
            child_effects: maybe_cached_parent_info
                .as_ref()
                .map(|cp| cp.children.clone())
                .unwrap_or_default(),
            gpu_spawner_params,
            init_indirect_dispatch_index: maybe_cached_child_info
                .as_ref()
                .map(|cc| cc.init_indirect_dispatch_index),
        });

        prepared_effect_count += 1;
    }
    trace!("Prepared {prepared_effect_count}/{extracted_effect_count} extracted effect(s)");

    // Update simulation parameters, including the total effect count for this frame
    {
        let dummy = HanabiRenderCamera {
            view: Entity::PLACEHOLDER.into(),
            frustum: default(),
            position: default(),
        };
        let render_camera = q_render_camera.single().unwrap_or(&dummy);

        let mut gpu_sim_params: GpuSimParams = sim_params.into();
        gpu_sim_params.frustum = render_camera.frustum;
        gpu_sim_params.camera_position = render_camera.position.extend(0.0);
        gpu_sim_params.num_effects = prepared_effect_count;
        trace!(
            "Simulation parameters: time={} delta_time={} virtual_time={} \
                virtual_delta_time={} real_time={} real_delta_time={} num_effects={}",
            gpu_sim_params.time,
            gpu_sim_params.delta_time,
            gpu_sim_params.virtual_time,
            gpu_sim_params.virtual_delta_time,
            gpu_sim_params.real_time,
            gpu_sim_params.real_delta_time,
            gpu_sim_params.num_effects,
        );
        effects_meta.sim_params_uniforms.set(gpu_sim_params);
    }
}

/// Batch compatible effects together into a single pass.
///
/// For all effects marked as ready for this frame (have a BatchInput
/// component), sort the effects by grouping compatible effects together, then
/// batch those groups together. Each batch can be updated and rendered with a
/// single compute dispatch or draw call.
pub(crate) fn batch_effects(
    render_device: Res<RenderDevice>,
    render_queue: Res<RenderQueue>,
    mut commands: Commands,
    mut effects_meta: ResMut<EffectsMeta>,
    mut sort_bind_groups: ResMut<SortBindGroups>,
    mut q_cached_effects: Query<(
        Entity,
        &MainEntity,
        &ExtractedEffect,
        &ExtractedSpawner,
        &ExtractedEffectMesh,
        &CachedDrawIndirectArgs,
        &CachedEffectMetadata,
        Option<&CachedEffectEvents>,
        Option<&ChildEffectOf>,
        Option<&CachedChildInfo>,
        Option<&CachedEffectProperties>,
        // The presence of BatchInput ensure the effect is ready
        &mut BatchInput,
    )>,
    mut batcher: ResMut<Batcher>,
    mut gpu_buffer_operations: ResMut<GpuBufferOperations>,
    mut effect_bind_groups: ResMut<EffectBindGroups>,
    mut property_bind_groups: ResMut<PropertyBindGroups>,
    mut sort_fill_dispatch_queue: ResMut<SortFillDispatchQueue>,
) {
    #[cfg(feature = "trace")]
    let _span = bevy::log::info_span!("batch_effects").entered();
    trace!("batch_effects");

    // Sort effects in batching order, so that we can batch by simply doing a linear
    // scan of the effects in this order. Currently compatible effects mean:
    // - same effect slab (so we can bind the buffers once for all batched effects)
    // - in order of increasing sub-allocation inside those buffers (to make the
    //   sort stable)
    // - with parents before their children, to ensure ???? FIXME don't we need to
    //   opposite?!!!
    let mut effect_sorter = EffectSorter::new();
    for (entity, _, _, _, _, _, _, _, child_of, _, _, input) in &q_cached_effects {
        effect_sorter.insert(
            entity,
            input.effect_slice.slab_id,
            input.effect_slice.slice.start,
            child_of.map(|co| co.parent),
        );
    }
    effect_sorter.sort();

    // For now we re-create that buffer each frame. Since there's no CPU -> GPU
    // transfer, this is pretty cheap in practice.
    sort_bind_groups.clear_indirect_args_buffer();

    // Loop on all extracted effects in sorted order, and batch compatible
    // effects together.
    trace!("Batching {} effects...", q_cached_effects.iter().len());
    batcher.clear();
    for entity in effect_sorter.iter() {
        let Ok((
            entity,
            main_entity,
            extracted_effect,
            extracted_spawner,
            extracted_effect_mesh,
            cached_draw_indirect_args,
            cached_effect_metadata,
            cached_effect_events,
            _,
            cached_child_info,
            cached_properties,
            mut input,
        )) = q_cached_effects.get_mut(entity)
        else {
            continue;
        };

        let translation = extracted_spawner.transform.translation();
        let instance_spawn_count = if input.event_buffer_index.is_some() {
            // GPU spawn based effects are not currently batched.
            0
        } else {
            extracted_spawner.spawn_count
        };

        // Now that the effects are sorted in batching order, we can allocate the
        // GpuSpawnerParams in the GPU buffer, such that they're contiguous for a single
        // batch.
        let spawner_index = effects_meta.allocate_spawner(input.gpu_spawner_params);

        let texture_layout = extracted_effect.texture_layout.clone();
        let property_key = if let Some(cp) = cached_properties.as_ref() {
            PropertyBindGroupKey::new(cp, texture_layout)
        } else {
            PropertyBindGroupKey::texture_only(texture_layout)
        };

        // Create a single-effect batch candidate. It can be merged with the previous
        // batch if fully compatible.
        let mut effect_batch = EffectBatch::from_input(
            main_entity.id(),
            extracted_effect,
            extracted_spawner,
            extracted_effect_mesh,
            cached_effect_events,
            cached_child_info,
            spawner_index,
            &mut input,
            cached_draw_indirect_args.row,
            cached_effect_metadata.table_id,
            property_key,
        );

        // If the batch has ribbons, we need to sort the particles by RIBBON_ID and AGE
        // for ribbon meshing, in order to avoid gaps when some particles in the middle
        // of the ribbon die (since we can't guarantee a linear lifetime through the
        // ribbon).
        let sort_indirect_dispatch_indices =
            if extracted_effect.layout_flags.raea_sorted() { // raea: and blended effects (#52)
                // Allocate a GpuDispatchIndirect entry
                let sort_fill_indirect_dispatch_index = sort_bind_groups.allocate_indirect_args();
                effect_batch.effect_data[0].sort_fill_indirect_dispatch_index =
                    Some(sort_fill_indirect_dispatch_index);

                let sort_indirect_dispatch_index = sort_bind_groups.allocate_indirect_args();
                effect_batch.effect_data[0].sort_indirect_dispatch_index =
                    Some(sort_indirect_dispatch_index);

                // Ensure sort merge passes are allocated, if needed
                sort_bind_groups.ensure_mergesort_passes(extracted_effect.capacity);

                Some((
                    sort_fill_indirect_dispatch_index,
                    sort_indirect_dispatch_index,
                ))
            } else {
                None
            };

        // Append to sorted compute batches; this may merge with the previous one.
        let (new_effect_batch_index, sort_prefix_sum_index) =
            batcher.push(effect_batch, instance_spawn_count);
        if let Some(new_effect_batch_index) = new_effect_batch_index {
            trace!(
                "Spawned new effect batch #{:?} from cached instance on entity {:?}.",
                new_effect_batch_index,
                entity,
            );

            // Spawn an EffectDrawBatch to drive rendering for that batch. Note that
            // technically nothing imposes we use the same batching for compute init/update
            // and for rendering, so we could (and probably should) re-batch specifically
            // for rendering. For now we just use the same batches.
            commands
                .spawn(EffectDrawBatch {
                    effect_batch_index: new_effect_batch_index,
                    translation,
                    main_entity: *main_entity,
                })
                .insert(TemporaryRenderEntity::default());
        }
        if let Some((sort_fill_indirect_dispatch_index, sort_indirect_dispatch_index)) =
            sort_indirect_dispatch_indices
        {
            sort_fill_dispatch_queue.enqueue(
                cached_effect_metadata.table_id,
                sort_prefix_sum_index,
                sort_fill_indirect_dispatch_index,
                sort_indirect_dispatch_index,
            );
        }

        // Ensure first_instance remains zero (required without INDIRECT_FIRST_INSTANCE
        // support).
        let mut draw_args = cached_draw_indirect_args.args;
        match &mut draw_args {
            AnyDrawIndirectArgs::NonIndexed(args) => args.first_instance = 0,
            AnyDrawIndirectArgs::Indexed(args) => args.first_instance = 0,
        }

        effects_meta.draw_indirect_args_buffer.update(
            cached_draw_indirect_args.row,
            draw_args.bitcast_to_row_entry(),
        );
    }

    batcher.try_end_batch();

    // Allocate dispatch indirect args entries for all batches, so that we can
    // dispatch the update pass. They are indexed by the same batch info index, so
    // much map 1:1 with the GpuBatchInfo indices.
    if !batcher.is_empty() {
        let di_base = effects_meta
            .dispatch_indirect_args_buffer
            .allocate_slice(batcher.len() as u32);
        // We cleared the Batcher and just enqueued the first batches
        assert_eq!(di_base, 0);
    }

    // Once all batching for init/update is one, allocate the render "batches"
    // (currently no batching; this allocates one entry per effect instance).
    batcher.allocate_render_batches();

    // Begin the GpuBufferOperations frame here; the matching submit()s happen in
    // queue_sort_fill_dispatch_ops() and queue_init_fill_dispatch_ops(), and
    // end_frame() at the tail of the latter. Render set ordering guarantees
    // this begin precedes all submits.
    gpu_buffer_operations.begin_frame();
    debug_assert!(batcher.fill_dispatch_queue_index.is_none());
    debug_assert!(batcher.sort_dispatch_queue_index.is_none());

    // Write the entire spawner buffer for this frame, for all effects combined
    if effects_meta
        .spawner_buffer
        .write_buffer(&render_device, &render_queue)
    {
        // Buffer was reallocated; clear all bind groups referencing the old buffer
        effect_bind_groups.particle_slabs.clear();
        property_bind_groups.clear();
        effects_meta.indirect_spawner_bind_group = None;
    }

    // Write the entire batch info buffer for this frame, for all batches combined
    if batcher.write_batch_info_buffer(&render_device, &render_queue) {
        // Buffer was reallocated; clear all bind groups referencing the old buffer
        effect_bind_groups.particle_slabs.clear();
        property_bind_groups.clear();
        effects_meta.indirect_spawner_bind_group = None;
        effects_meta.prefix_sum_bind_group = None;
    }

    // Write the entire prefix sum buffer for this frame, for all batches combined
    if batcher.write_prefix_sum_buffer(&render_device, &render_queue) {
        // Buffer was reallocated; clear all bind groups referencing the old buffer
        effect_bind_groups.particle_slabs.clear();
        property_bind_groups.clear();
        effects_meta.indirect_spawner_bind_group = None;
        effects_meta.prefix_sum_bind_group = None;
    }

    let _ = sort_bind_groups.write_merge_params(&render_device, &render_queue);
}

/// Per-buffer bind groups for a GPU effect buffer.
///
/// This contains all bind groups specific to a single [`EffectBuffer`].
///
/// [`EffectBuffer`]: crate::render::effect_cache::EffectBuffer
pub(crate) struct BufferBindGroups {
    /// Bind group for the render shader.
    ///
    /// ```wgsl
    /// @binding(0) var<storage, read> particle_buffer : ParticleBuffer;
    /// @binding(1) var<storage, read> indirect_buffer : IndirectBuffer;
    /// ```
    render: BindGroup,
    // /// Bind group for filling the indirect dispatch arguments of any child init
    // /// pass.
    // ///
    // /// This bind group is optional; it's only created if the current effect has
    // /// a GPU spawn event buffer, irrelevant of whether it has child effects
    // /// (although normally the event buffer is not created if there's no
    // /// children).
    // ///
    // /// The source buffer is always the current effect's event buffer. The
    // /// destination buffer is the global shared buffer for indirect fill args
    // /// operations owned by the [`EffectCache`]. The uniform buffer of operation
    // /// args contains the data to index the relevant part of the global shared
    // /// buffer for this effect buffer; it may contain multiple entries in case
    // /// multiple effects are batched inside the current effect buffer.
    // ///
    // /// ```wgsl
    // /// @group(0) @binding(0) var<uniform> args : BufferOperationArgs;
    // /// @group(0) @binding(1) var<storage, read> src_buffer : array<u32>;
    // /// @group(0) @binding(2) var<storage, read_write> dst_buffer : array<u32>;
    // /// ```
    // init_fill_dispatch: Option<BindGroup>,
}

/// Combination of a texture layout and the bound textures.
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
struct Material {
    layout: TextureLayout,
    textures: Vec<AssetId<Image>>,
}

impl Material {
    /// Get the bind group entries to create a bind group.
    pub fn append_binding_entries<'a, 'b>(
        &'b self,
        binding_start: u32,
        gpu_images: &'a RenderAssets<GpuImage>,
        entries: &'b mut Vec<BindGroupEntry<'a>>,
    ) -> bool {
        if self.layout.layout.len() != self.textures.len() {
            return false;
        }

        if self.textures.is_empty() {
            return true;
        }

        let mut index = binding_start;
        for (slot, asset_id) in self.layout.layout.iter().zip(self.textures.iter()) {
            let Some(gpu_image) = gpu_images.get(*asset_id) else {
                return false;
            };
            if gpu_image.texture_descriptor.sample_count > 1 {
                return false;
            }
            if !slot.accepts(
                gpu_image.texture_descriptor.dimension,
                gpu_image.texture_descriptor.array_layer_count(),
                gpu_image
                    .texture_descriptor
                    .format
                    .is_depth_stencil_format(),
            ) {
                return false;
            }
            entries.push(BindGroupEntry {
                binding: index,
                resource: BindingResource::TextureView(&gpu_image.texture_view),
            });
            entries.push(BindGroupEntry {
                binding: index + 1,
                resource: BindingResource::Sampler(&gpu_image.sampler),
            });
            index += 2;
        }
        true
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct BindingKey {
    pub buffer_id: BufferId,
    pub offset: u32,
    pub size: NonZeroU32,
}

impl<'a> From<BufferSlice<'a>> for BindingKey {
    fn from(value: BufferSlice<'a>) -> Self {
        Self {
            buffer_id: value.buffer.id(),
            offset: value.offset,
            size: value.size,
        }
    }
}

impl<'a> From<&BufferSlice<'a>> for BindingKey {
    fn from(value: &BufferSlice<'a>) -> Self {
        Self {
            buffer_id: value.buffer.id(),
            offset: value.offset,
            size: value.size,
        }
    }
}

impl From<&BufferBindingSource> for BindingKey {
    fn from(value: &BufferBindingSource) -> Self {
        Self {
            buffer_id: value.buffer.id(),
            offset: value.offset,
            size: value.size,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct ConsumeEventKey {
    child_infos_buffer_id: BufferId,
    events: BindingKey,
}

impl From<&ConsumeEventBuffers<'_>> for ConsumeEventKey {
    fn from(value: &ConsumeEventBuffers) -> Self {
        Self {
            child_infos_buffer_id: value.child_infos_buffer.id(),
            events: value.events.into(),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct InitMetadataBindGroupKey {
    pub slab_id: SlabId,
    pub effect_metadata_buffer: BufferId,
    pub consume_event_key: Option<ConsumeEventKey>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct UpdateMetadataBindGroupKey {
    pub slab_id: SlabId,
    pub effect_metadata_buffer: BufferId,
    pub child_info_buffer_id: Option<BufferId>,
    pub event_buffers_keys: Vec<BindingKey>,
}

/// Bind group cached with an associated key.
///
/// The cached bind group is associated with the given key representing the
/// inputs that the bind group depends on. When those inputs change, the key
/// should change, indicating the bind group needs to be recreated.
///
/// This object manages a single bind group and its key.
struct CachedBindGroup<K: Eq> {
    /// Key the bind group was created from. Each time the key changes, the bind
    /// group should be re-created.
    key: K,
    /// Bind group created from the key.
    bind_group: BindGroup,
}

#[derive(Debug, Clone, Copy)]
struct BufferSlice<'a> {
    pub buffer: &'a Buffer,
    pub offset: u32,
    pub size: NonZeroU32,
}

impl<'a> From<BufferSlice<'a>> for BufferBinding<'a> {
    fn from(value: BufferSlice<'a>) -> Self {
        Self {
            buffer: value.buffer,
            offset: value.offset.into(),
            size: Some(value.size.into()),
        }
    }
}

impl<'a> From<&BufferSlice<'a>> for BufferBinding<'a> {
    fn from(value: &BufferSlice<'a>) -> Self {
        Self {
            buffer: value.buffer,
            offset: value.offset.into(),
            size: Some(value.size.into()),
        }
    }
}

impl<'a> From<&'a BufferBindingSource> for BufferSlice<'a> {
    fn from(value: &'a BufferBindingSource) -> Self {
        Self {
            buffer: &value.buffer,
            offset: value.offset,
            size: value.size,
        }
    }
}

/// Optional input to [`EffectBindGroups::get_or_create_init_metadata()`] when
/// the init pass consumes GPU events as a mechanism to spawn particles.
struct ConsumeEventBuffers<'a> {
    /// Entire buffer containing the [`GpuChildInfo`] entries for all effects.
    /// This is dynamically indexed inside the shader.
    child_infos_buffer: &'a Buffer,
    /// Slice of the [`EventBuffer`] where the GPU spawn events are stored.
    events: BufferSlice<'a>,
}

#[derive(Default, Resource)]
pub struct EffectBindGroups {
    /// Map from a slab ID to the bind groups shared among all effects that
    /// use that particle slab.
    particle_slabs: HashMap<SlabId, BufferBindGroups>,
    /// Map of bind groups for image assets used as particle textures.
    images: HashMap<AssetId<Image>, BindGroup>,
    /// Map from buffer index to its metadata bind group (group 3) for the init
    /// pass.
    // FIXME - doesn't work with batching; this should be the instance ID
    init_metadata_bind_groups: HashMap<SlabId, CachedBindGroup<InitMetadataBindGroupKey>>,
    /// Map from buffer index to its metadata bind group (group 3) for the
    /// update pass.
    // FIXME - doesn't work with batching; this should be the instance ID
    update_metadata_bind_groups: HashMap<SlabId, CachedBindGroup<UpdateMetadataBindGroupKey>>,
}

impl EffectBindGroups {
    pub fn particle_render(&self, slab_id: &SlabId) -> Option<&BindGroup> {
        self.particle_slabs.get(slab_id).map(|bg| &bg.render)
    }

    /// Retrieve the metadata@3 bind group for the init pass, creating it if
    /// needed.
    pub(self) fn get_or_create_init_metadata(
        &mut self,
        effect_batch: &EffectBatch,
        render_device: &RenderDevice,
        layout: &BindGroupLayout,
        effect_metadata_buffer: &Buffer,
        consume_event_buffers: Option<ConsumeEventBuffers>,
    ) -> Result<&BindGroup, ()> {
        assert!(!effect_batch.effect_data.is_empty());
        assert!(effect_batch.effect_data[0].metadata_table_id.is_valid());

        let key = InitMetadataBindGroupKey {
            slab_id: effect_batch.slab_id,
            effect_metadata_buffer: effect_metadata_buffer.id(),
            consume_event_key: consume_event_buffers.as_ref().map(Into::into),
        };

        let make_entry = || {
            let mut entries = Vec::with_capacity(3);
            entries.push(
                // @group(3) @binding(0) var<storage, read_write> effect_metadatas :
                // array<EffectMetadata>;
                BindGroupEntry {
                    binding: 0,
                    resource: effect_metadata_buffer.as_entire_binding(),
                },
            );
            if let Some(consume_event_buffers) = consume_event_buffers.as_ref() {
                entries.push(
                    // @group(3) @binding(1) var<storage, read> child_info_buffer :
                    // ChildInfoBuffer;
                    BindGroupEntry {
                        binding: 1,
                        resource: BindingResource::Buffer(BufferBinding {
                            buffer: consume_event_buffers.child_infos_buffer,
                            offset: 0,
                            size: None,
                        }),
                    },
                );
                entries.push(
                    // @group(3) @binding(2) var<storage, read> event_buffer : EventBuffer;
                    BindGroupEntry {
                        binding: 2,
                        resource: BindingResource::Buffer(consume_event_buffers.events.into()),
                    },
                );
            }

            let bind_group =
                render_device.create_bind_group("hanabi:bg:init:metadata@3", layout, &entries[..]);

            trace!(
                    "Created new metadata@3 bind group for init pass and buffer index {}: effect_metadata=#{}",
                    effect_batch.slab_id.index(),
                    effect_batch.effect_data[0].metadata_table_id.0,
                );

            bind_group
        };

        Ok(&self
            .init_metadata_bind_groups
            .entry(effect_batch.slab_id)
            .and_modify(|cbg| {
                if cbg.key != key {
                    trace!(
                        "Bind group key changed for init metadata@3, re-creating bind group... old={:?} new={:?}",
                        cbg.key,
                        key
                    );
                    cbg.key = key;
                    cbg.bind_group = make_entry();
                }
            })
            .or_insert_with(|| {
                trace!("Inserting new bind group for init metadata@3 with key={:?}", key);
                CachedBindGroup {
                    key,
                    bind_group: make_entry(),
                }
            })
            .bind_group)
    }

    /// Retrieve the metadata@3 bind group for the update pass, creating it if
    /// needed.
    pub(self) fn get_or_create_update_metadata(
        &mut self,
        effect_batch: &EffectBatch,
        render_device: &RenderDevice,
        layout: &BindGroupLayout,
        effect_metadata_buffer: &Buffer,
        child_info_buffer: Option<&Buffer>,
        event_buffers: &[(Entity, BufferBindingSource)],
    ) -> Result<&BindGroup, ()> {
        assert!(!effect_batch.effect_data.is_empty());
        assert!(effect_batch.effect_data[0].metadata_table_id.is_valid());

        // Check arguments consistency
        assert_eq!(effect_batch.child_event_buffers.len(), event_buffers.len());
        let emits_gpu_spawn_events = !event_buffers.is_empty();
        let child_info_buffer_id = if emits_gpu_spawn_events {
            child_info_buffer.as_ref().map(|buffer| buffer.id())
        } else {
            // Note: child_info_buffer can be Some() if allocated, but we only consider it
            // if relevant, that is if the effect emits GPU spawn events.
            None
        };
        assert_eq!(emits_gpu_spawn_events, child_info_buffer_id.is_some());

        let event_buffers_keys = event_buffers
            .iter()
            .map(|(_, buffer_binding_source)| buffer_binding_source.into())
            .collect::<Vec<_>>();

        let key = UpdateMetadataBindGroupKey {
            slab_id: effect_batch.slab_id,
            effect_metadata_buffer: effect_metadata_buffer.id(),
            child_info_buffer_id,
            event_buffers_keys,
        };

        let make_entry = || {
            let mut entries = Vec::with_capacity(2 + event_buffers.len());
            // @group(3) @binding(0) var<storage, read_write> effect_metadatas :
            // array<EffectMetadata>;
            entries.push(BindGroupEntry {
                binding: 0,
                resource: effect_metadata_buffer.as_entire_binding(),
            });
            if emits_gpu_spawn_events {
                let child_info_buffer = child_info_buffer.unwrap();

                // @group(3) @binding(1) var<storage, read_write> child_info_buffer :
                // ChildInfoBuffer;
                entries.push(BindGroupEntry {
                    binding: 1,
                    resource: BindingResource::Buffer(BufferBinding {
                        buffer: child_info_buffer,
                        offset: 0,
                        size: None,
                    }),
                });

                for (index, (_, buffer_binding_source)) in event_buffers.iter().enumerate() {
                    // @group(3) @binding(2+N) var<storage, read_write> event_buffer_N :
                    // EventBuffer;
                    // FIXME - BufferBindingSource originally was for Events, counting in u32, but
                    // then moved to counting in bytes, so now need some conversion. Need to review
                    // all of this...
                    let mut buffer_binding: BufferBinding = buffer_binding_source.into();
                    buffer_binding.offset *= 4;
                    buffer_binding.size = buffer_binding
                        .size
                        .map(|sz| NonZeroU64::new(sz.get() * 4).unwrap());
                    entries.push(BindGroupEntry {
                        binding: 2 + index as u32,
                        resource: BindingResource::Buffer(buffer_binding),
                    });
                }
            }

            let bind_group = render_device.create_bind_group(
                "hanabi:bg:update:metadata@3",
                layout,
                &entries[..],
            );

            trace!(
                "Created new metadata@3 bind group for update pass and slab ID {}",
                effect_batch.slab_id.index(),
            );

            bind_group
        };

        Ok(&self
            .update_metadata_bind_groups
            .entry(effect_batch.slab_id)
            .and_modify(|cbg| {
                if cbg.key != key {
                    trace!(
                        "Bind group key changed for update metadata@3, re-creating bind group... old={:?} new={:?}",
                        cbg.key,
                        key
                    );
                    cbg.key = key.clone();
                    cbg.bind_group = make_entry();
                }
            })
            .or_insert_with(|| {
                trace!(
                    "Inserting new bind group for update metadata@3 with key={:?}",
                    key
                );
                CachedBindGroup {
                    key: key.clone(),
                    bind_group: make_entry(),
                }
            })
            .bind_group)
    }
}

#[derive(SystemParam)]
pub struct QueueEffectsReadOnlyParams<'w, 's> {
    effects_meta: Res<'w, EffectsMeta>,
    property_cache: Res<'w, PropertyCache>,
    pipeline_cache: Res<'w, PipelineCache>,
    #[cfg(feature = "2d")]
    draw_functions_2d: Res<'w, DrawFunctions<Transparent2d>>,
    #[cfg(feature = "3d")]
    draw_functions_3d: Res<'w, DrawFunctions<Transparent3d>>,
    #[cfg(feature = "3d")]
    draw_functions_alpha_mask: Res<'w, DrawFunctions<AlphaMask3d>>,
    #[cfg(feature = "3d")]
    draw_functions_opaque: Res<'w, DrawFunctions<Opaque3d>>,
    marker: PhantomData<&'s usize>,
}

fn emit_sorted_draw<T, F>(
    views: &Query<(&RenderVisibleEntities, &ExtractedView, &Msaa)>,
    render_phases: &mut ResMut<ViewSortedRenderPhases<T>>,
    view_entities: &mut FixedBitSet,
    batcher: &Batcher,
    effect_draw_batches: &Query<(Entity, &mut EffectDrawBatch)>,
    render_pipeline: &mut ParticlesRenderPipeline,
    mut specialized_render_pipelines: Mut<SpecializedRenderPipelines<ParticlesRenderPipeline>>,
    property_cache: &PropertyCache,
    render_meshes: &RenderAssets<RenderMesh>,
    pipeline_cache: &PipelineCache,
    make_phase_item: F,
    #[cfg(all(feature = "2d", feature = "3d"))] pipeline_mode: PipelineMode,
) where
    T: SortedPhaseItem,
    F: Fn(CachedRenderPipelineId, (Entity, MainEntity), &EffectDrawBatch, &ExtractedView) -> T,
{
    trace!("emit_sorted_draw() {} views", views.iter().len());

    for (visible_entities, view, msaa) in views.iter() {
        let Some(visible_effect_entities) = visible_entities.get::<EffectVisibilityClass>() else {
            continue;
        };
        trace!(
            "Process new sorted view with {} visible particle effect entities",
            visible_effect_entities.iter_visible().count()
        );

        let Some(render_phase) = render_phases.get_mut(&view.retained_view_entity) else {
            continue;
        };

        {
            #[cfg(feature = "trace")]
            let _span = bevy::log::info_span!("collect_view_entities").entered();

            view_entities.clear();
            view_entities.extend(
                visible_effect_entities
                    .iter_visible()
                    .map(|(_, main_entity)| main_entity.index_u32() as usize),
            );
        }

        // For each view, loop over all the effect batches to determine if the effect
        // needs to be rendered for that view, and enqueue a view-dependent
        // batch if so.
        for (draw_entity, draw_batch) in effect_draw_batches.iter() {
            #[cfg(feature = "trace")]
            let _span_draw = bevy::log::info_span!("draw_batch").entered();

            trace!(
                "Process draw batch: draw_entity={:?} effect_batch_index={:?}",
                draw_entity,
                draw_batch.effect_batch_index,
            );

            // Get the EffectBatches this EffectDrawBatch is part of.
            let Some(effect_batch) = batcher.get(draw_batch.effect_batch_index) else {
                continue;
            };

            trace!(
                "-> EffectBach: slab_id={} spawner_base={} layout_flags={:?}",
                effect_batch.slab_id.index(),
                effect_batch.spawner_base,
                effect_batch.layout_flags,
            );

            // AlphaMask is a binned draw, so no sorted draw can possibly use it
            if effect_batch
                .layout_flags
                .intersects(LayoutFlags::USE_ALPHA_MASK | LayoutFlags::OPAQUE)
            {
                trace!("Non-transparent batch. Skipped.");
                continue;
            }

            // Check if batch contains any entity visible in the current view. Otherwise we
            // can skip the entire batch. Note: This is O(n^2) but (unlike
            // the Sprite renderer this is inspired from) we don't expect more than
            // a handful of particle effect instances, so would rather not pay the memory
            // cost of a FixedBitSet for the sake of an arguable speed-up.
            // TODO - Profile to confirm.
            #[cfg(feature = "trace")]
            let _span_check_vis = bevy::log::info_span!("check_visibility").entered();
            let has_visible_entity = effect_batch
                .effect_data
                .iter()
                .any(|effect_data| view_entities.contains(effect_data.entity as usize));
            if !has_visible_entity {
                trace!("No visible entity for view, not emitting any draw call.");
                continue;
            }
            #[cfg(feature = "trace")]
            _span_check_vis.exit();

            // FIXME - We draw the entire batch, but part of it may not be visible in this
            // view! We should re-batch for the current view specifically!

            let local_space_simulation = effect_batch
                .layout_flags
                .contains(LayoutFlags::LOCAL_SPACE_SIMULATION);
            let alpha_mask = ParticleRenderAlphaMaskPipelineKey::from(effect_batch.layout_flags);
            let flipbook = effect_batch.layout_flags.contains(LayoutFlags::FLIPBOOK);
            let needs_uv = effect_batch.layout_flags.contains(LayoutFlags::NEEDS_UV);
            let needs_normal = effect_batch
                .layout_flags
                .contains(LayoutFlags::NEEDS_NORMAL);
            let needs_particle_fragment = effect_batch
                .layout_flags
                .contains(LayoutFlags::NEEDS_PARTICLE_FRAGMENT);
            let ribbons = effect_batch.layout_flags.contains(LayoutFlags::RIBBONS);
            let image_count = effect_batch.texture_layout.layout.len() as u8;

            // FIXME - Maybe it's better to copy the mesh layout into the batch, instead of
            // re-querying here...?
            let Some(render_mesh) = render_meshes.get(effect_batch.mesh) else {
                trace!("Batch has no render mesh, skipped.");
                continue;
            };
            let mesh_layout = render_mesh.layout.clone();

            // Specialize the render pipeline based on the effect batch
            trace!(
                "Specializing render pipeline: render_shader={:?} image_count={} alpha_mask={:?} flipbook={:?} target_format={:?}",
                effect_batch.render_shader,
                image_count,
                alpha_mask,
                flipbook,
                view.target_format
            );

            // Add a draw pass for the effect batch
            trace!("Emitting individual draw for batch");

            let alpha_mode = effect_batch.alpha_mode;

            // This should always exist by the time we reach this point, because we should
            // have inserted any property in the cache, which would have allocated the
            // proper bind group layout (or the default no-property one).
            let has_multi_draw = false; // TODO?
            let property_layout_min_binding_size =
                NonZeroU64::new(effect_batch.property_key.binding_size as u64);
            let spawner_bind_group_layout_desc = property_cache
                .bind_group_layout_desc(
                    property_layout_min_binding_size,
                    has_multi_draw,
                    &effect_batch.texture_layout,
                )
                .unwrap_or_else(|| {
                    panic!(
                        "Failed to find spawner@2 bind group layout for property binding size {:?}",
                        property_layout_min_binding_size,
                    )
                });

            #[cfg(feature = "trace")]
            let _span_specialize = bevy::log::info_span!("specialize").entered();
            let render_pipeline_id = specialized_render_pipelines.specialize(
                pipeline_cache,
                render_pipeline,
                ParticleRenderPipelineKey {
                    shader: effect_batch.render_shader.clone(),
                    mesh_layout: Some(mesh_layout),
                    particle_layout: effect_batch.particle_layout.clone(),
                    texture_layout: effect_batch.texture_layout.clone(),
                    local_space_simulation,
                    alpha_mask,
                    alpha_mode,
                    flipbook,
                    needs_uv,
                    needs_normal,
                    needs_particle_fragment,
                    ribbons,
                    #[cfg(all(feature = "2d", feature = "3d"))]
                    pipeline_mode,
                    msaa_samples: msaa.samples(),
                    target_format: view.target_format,
                    spawner_bind_group_layout_desc: spawner_bind_group_layout_desc.clone(),
                },
            );
            #[cfg(feature = "trace")]
            _span_specialize.exit();

            trace!("+ Render pipeline specialized: id={:?}", render_pipeline_id,);
            trace!(
                "+ Add Transparent for batch on draw_entity {:?}: slab_id={} \
                spawner_base={} handle={:?}",
                draw_entity,
                effect_batch.slab_id.index(),
                effect_batch.spawner_base,
                effect_batch.handle
            );
            render_phase.add_transient(make_phase_item(
                render_pipeline_id,
                (draw_entity, draw_batch.main_entity),
                draw_batch,
                view,
            ));
        }
    }
}

#[cfg(feature = "3d")]
fn emit_binned_draw<T, F, G>(
    views: &Query<(&RenderVisibleEntities, &ExtractedView, &Msaa)>,
    render_phases: &mut ResMut<ViewBinnedRenderPhases<T>>,
    view_entities: &mut FixedBitSet,
    batcher: &Batcher,
    effect_draw_batches: &Query<(Entity, &mut EffectDrawBatch)>,
    render_pipeline: &mut ParticlesRenderPipeline,
    mut specialized_render_pipelines: Mut<SpecializedRenderPipelines<ParticlesRenderPipeline>>,
    property_cache: &PropertyCache,
    pipeline_cache: &PipelineCache,
    render_meshes: &RenderAssets<RenderMesh>,
    mesh_allocator: &MeshAllocator,
    make_batch_set_key: F,
    make_bin_key: G,
    #[cfg(all(feature = "2d", feature = "3d"))] pipeline_mode: PipelineMode,
    alpha_mask: ParticleRenderAlphaMaskPipelineKey,
) where
    T: BinnedPhaseItem,
    F: Fn(CachedRenderPipelineId, &EffectDrawBatch, &ExtractedView, MeshSlabs) -> T::BatchSetKey,
    G: Fn() -> T::BinKey,
{
    use bevy::render::render_phase::{BinnedRenderPhaseType, InputUniformIndex};

    trace!("emit_binned_draw() {} views", views.iter().len());

    for (visible_entities, view, msaa) in views.iter() {
        let Some(visible_effect_entities) = visible_entities.get::<EffectVisibilityClass>() else {
            continue;
        };
        trace!("Process new binned view (alpha_mask={:?})", alpha_mask);

        let Some(render_phase) = render_phases.get_mut(&view.retained_view_entity) else {
            continue;
        };

        {
            #[cfg(feature = "trace")]
            let _span = bevy::log::info_span!("collect_view_entities").entered();

            view_entities.clear();
            view_entities.extend(
                visible_effect_entities
                    .iter_visible()
                    .map(|(_, main_entity)| main_entity.index_u32() as usize),
            );
        }

        // For each view, loop over all the effect batches to determine if the effect
        // needs to be rendered for that view, and enqueue a view-dependent
        // batch if so.
        for (draw_entity, draw_batch) in effect_draw_batches.iter() {
            #[cfg(feature = "trace")]
            let _span_draw = bevy::log::info_span!("draw_batch").entered();

            trace!(
                "Process draw batch: draw_entity={:?} effect_batch_index={:?}",
                draw_entity,
                draw_batch.effect_batch_index,
            );

            // Get the EffectBatches this EffectDrawBatch is part of.
            let Some(effect_batch) = batcher.get(draw_batch.effect_batch_index) else {
                continue;
            };

            trace!(
                "-> EffectBaches: slab_id={} spawner_base={} layout_flags={:?}",
                effect_batch.slab_id.index(),
                effect_batch.spawner_base,
                effect_batch.layout_flags,
            );

            if ParticleRenderAlphaMaskPipelineKey::from(effect_batch.layout_flags) != alpha_mask {
                trace!(
                    "Mismatching alpha mask pipeline key (batches={:?}, expected={:?}). Skipped.",
                    effect_batch.layout_flags,
                    alpha_mask
                );
                continue;
            }

            // Check if batch contains any entity visible in the current view. Otherwise we
            // can skip the entire batch. Note: This is O(n^2) but (unlike
            // the Sprite renderer this is inspired from) we don't expect more than
            // a handful of particle effect instances, so would rather not pay the memory
            // cost of a FixedBitSet for the sake of an arguable speed-up.
            // TODO - Profile to confirm.
            #[cfg(feature = "trace")]
            let _span_check_vis = bevy::log::info_span!("check_visibility").entered();
            let has_visible_entity = effect_batch
                .effect_data
                .iter()
                .any(|effect_data| view_entities.contains(effect_data.entity as usize));
            if !has_visible_entity {
                trace!("No visible entity for view, not emitting any draw call.");
                continue;
            }
            #[cfg(feature = "trace")]
            _span_check_vis.exit();

            // FIXME - We draw the entire batch, but part of it may not be visible in this
            // view! We should re-batch for the current view specifically!

            let local_space_simulation = effect_batch
                .layout_flags
                .contains(LayoutFlags::LOCAL_SPACE_SIMULATION);
            let alpha_mask = ParticleRenderAlphaMaskPipelineKey::from(effect_batch.layout_flags);
            let flipbook = effect_batch.layout_flags.contains(LayoutFlags::FLIPBOOK);
            let needs_uv = effect_batch.layout_flags.contains(LayoutFlags::NEEDS_UV);
            let needs_normal = effect_batch
                .layout_flags
                .contains(LayoutFlags::NEEDS_NORMAL);
            let needs_particle_fragment = effect_batch
                .layout_flags
                .contains(LayoutFlags::NEEDS_PARTICLE_FRAGMENT);
            let ribbons = effect_batch.layout_flags.contains(LayoutFlags::RIBBONS);
            let image_count = effect_batch.texture_layout.layout.len() as u8;
            let render_mesh = render_meshes.get(effect_batch.mesh);

            // Specialize the render pipeline based on the effect batch
            trace!(
                "Specializing render pipeline: render_shaders={:?} image_count={} alpha_mask={:?} flipbook={:?} target_format={:?}",
                effect_batch.render_shader,
                image_count,
                alpha_mask,
                flipbook,
                view.target_format
            );

            // Add a draw pass for the effect batch
            trace!("Emitting individual draw for batch");

            let alpha_mode = effect_batch.alpha_mode;

            let Some(mesh_layout) = render_mesh.map(|gpu_mesh| gpu_mesh.layout.clone()) else {
                trace!("Missing mesh vertex buffer layout. Skipped.");
                continue;
            };

            // This should always exist by the time we reach this point, because we should
            // have inserted any property in the cache, which would have allocated the
            // proper bind group layout (or the default no-property one).
            let has_multi_draw = false; // TODO?
            let property_layout_min_binding_size =
                NonZeroU64::new(effect_batch.property_key.binding_size as u64);
            let spawner_bind_group_layout_desc = property_cache
                .bind_group_layout_desc(
                    property_layout_min_binding_size,
                    has_multi_draw,
                    &effect_batch.texture_layout,
                )
                .unwrap_or_else(|| {
                    panic!(
                        "Failed to find spawner@2 bind group layout for property binding size {:?}",
                        property_layout_min_binding_size,
                    )
                });

            #[cfg(feature = "trace")]
            let _span_specialize = bevy::log::info_span!("specialize").entered();
            let render_pipeline_id = specialized_render_pipelines.specialize(
                pipeline_cache,
                render_pipeline,
                ParticleRenderPipelineKey {
                    shader: effect_batch.render_shader.clone(),
                    mesh_layout: Some(mesh_layout),
                    particle_layout: effect_batch.particle_layout.clone(),
                    texture_layout: effect_batch.texture_layout.clone(),
                    local_space_simulation,
                    alpha_mask,
                    alpha_mode,
                    flipbook,
                    needs_uv,
                    needs_normal,
                    needs_particle_fragment,
                    ribbons,
                    #[cfg(all(feature = "2d", feature = "3d"))]
                    pipeline_mode,
                    msaa_samples: msaa.samples(),
                    target_format: view.target_format,
                    spawner_bind_group_layout_desc: spawner_bind_group_layout_desc.clone(),
                },
            );
            #[cfg(feature = "trace")]
            _span_specialize.exit();

            trace!("+ Render pipeline specialized: id={:?}", render_pipeline_id,);
            trace!(
                "+ Add Transparent for batch on draw_entity {:?}: slab_id={} \
                spawner_base={} handle={:?}",
                draw_entity,
                effect_batch.slab_id.index(),
                effect_batch.spawner_base,
                effect_batch.handle
            );
            let slabs = mesh_allocator
                .mesh_slabs(&effect_batch.mesh)
                .unwrap_or_default();
            render_phase.add(
                make_batch_set_key(render_pipeline_id, draw_batch, view, slabs),
                make_bin_key(),
                (draw_entity, draw_batch.main_entity),
                InputUniformIndex::default(),
                BinnedRenderPhaseType::NonMesh,
            );
        }
    }
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn queue_effects(
    views: Query<(&RenderVisibleEntities, &ExtractedView, &Msaa)>,
    mut render_pipeline: ResMut<ParticlesRenderPipeline>,
    mut specialized_render_pipelines: ResMut<SpecializedRenderPipelines<ParticlesRenderPipeline>>,
    mut effect_bind_groups: ResMut<EffectBindGroups>,
    batcher: Res<Batcher>,
    effect_draw_batches: Query<(Entity, &mut EffectDrawBatch)>,
    events: Res<EffectAssetEvents>,
    render_meshes: Res<RenderAssets<RenderMesh>>,
    #[cfg(feature = "3d")] mesh_allocator: Res<MeshAllocator>,
    read_params: QueueEffectsReadOnlyParams,
    mut view_entities: Local<FixedBitSet>,
    #[cfg(feature = "2d")] mut transparent_2d_render_phases: ResMut<
        ViewSortedRenderPhases<Transparent2d>,
    >,
    #[cfg(feature = "3d")] mut transparent_3d_render_phases: ResMut<
        ViewSortedRenderPhases<Transparent3d>,
    >,
    #[cfg(feature = "3d")] (mut opaque_3d_render_phases, mut alpha_mask_3d_render_phases): (
        ResMut<ViewBinnedRenderPhases<Opaque3d>>,
        ResMut<ViewBinnedRenderPhases<AlphaMask3d>>,
    ),
    mut change_tick: Local<Tick>,
) {
    #[cfg(feature = "trace")]
    let _span = bevy::log::info_span!("hanabi:queue_effects").entered();

    trace!("queue_effects");

    let effects_meta = read_params.effects_meta.into_inner();
    let property_cache = read_params.property_cache.into_inner();
    let pipeline_cache = read_params.pipeline_cache.into_inner();

    // Bump the change tick so that Bevy is forced to rebuild the binned render
    // phase bins. We don't use the built-in caching so we don't want Bevy to
    // reuse stale data.
    let next_change_tick = change_tick.get() + 1;
    change_tick.set(next_change_tick);

    // If an image has changed, the GpuImage has (probably) changed
    for event in &events.images {
        match event {
            AssetEvent::Added { .. } => (),
            AssetEvent::LoadedWithDependencies { .. } => (),
            AssetEvent::Unused { .. } => (),
            AssetEvent::Modified { id } => {
                if effect_bind_groups.images.remove(id).is_some() {
                    trace!("Destroyed bind group of modified image asset {:?}", id);
                }
            }
            AssetEvent::Removed { id } => {
                if effect_bind_groups.images.remove(id).is_some() {
                    trace!("Destroyes bind group of removed image asset {:?}", id);
                }
            }
        };
    }

    if effects_meta.spawner_buffer.buffer().is_none() || effects_meta.spawner_buffer.is_empty() {
        // No spawners are active
        return;
    }

    // Loop over all 2D cameras/views that need to render effects
    #[cfg(feature = "2d")]
    {
        #[cfg(feature = "trace")]
        let _span_draw = bevy::log::info_span!("draw_2d").entered();

        let draw_effects_function_2d = read_params
            .draw_functions_2d
            .read()
            .get_id::<DrawEffects>()
            .unwrap();

        // Effects with full alpha blending
        if !views.is_empty() {
            trace!("Emit effect draw calls for alpha blended 2D views...");
            emit_sorted_draw(
                &views,
                &mut transparent_2d_render_phases,
                &mut view_entities,
                &batcher,
                &effect_draw_batches,
                &mut render_pipeline,
                specialized_render_pipelines.reborrow(),
                property_cache,
                &render_meshes,
                pipeline_cache,
                |id, entity, draw_batch, _view| Transparent2d {
                    sort_key: FloatOrd(draw_batch.translation.z),
                    entity,
                    pipeline: id,
                    draw_function: draw_effects_function_2d,
                    batch_range: 0..1,
                    extracted_index: 0, // ???
                    extra_index: PhaseItemExtraIndex::None,
                    indexed: true, // ???
                },
                #[cfg(feature = "3d")]
                PipelineMode::Camera2d,
            );
        }
    }

    // Loop over all 3D cameras/views that need to render effects
    #[cfg(feature = "3d")]
    {
        #[cfg(feature = "trace")]
        let _span_draw = bevy::log::info_span!("draw_3d").entered();

        // Effects with full alpha blending
        if !views.is_empty() {
            use bevy::core_pipeline::core_3d::TransparentSortingInfo3d;

            trace!("Emit effect draw calls for alpha blended 3D views...");

            let draw_effects_function_3d = read_params
                .draw_functions_3d
                .read()
                .get_id::<DrawEffects>()
                .unwrap();

            emit_sorted_draw(
                &views,
                &mut transparent_3d_render_phases,
                &mut view_entities,
                &batcher,
                &effect_draw_batches,
                &mut render_pipeline,
                specialized_render_pipelines.reborrow(),
                property_cache,
                &render_meshes,
                pipeline_cache,
                |id, entity, batch, view| Transparent3d {
                    sorting_info: TransparentSortingInfo3d::Sorted {
                        mesh_center: batch.translation,
                        depth_bias: 0.0,
                    },
                    distance: view.rangefinder3d().distance(&batch.translation),
                    pipeline: id,
                    entity,
                    draw_function: draw_effects_function_3d,
                    batch_range: 0..1,
                    extra_index: PhaseItemExtraIndex::None,
                    indexed: true, // ???
                },
                #[cfg(feature = "2d")]
                PipelineMode::Camera3d,
            );
        }

        // Effects with alpha mask
        if !views.is_empty() {
            #[cfg(feature = "trace")]
            let _span_draw = bevy::log::info_span!("draw_alphamask").entered();

            trace!("Emit effect draw calls for alpha masked 3D views...");

            let draw_effects_function_alpha_mask = read_params
                .draw_functions_alpha_mask
                .read()
                .get_id::<DrawEffects>()
                .unwrap();

            emit_binned_draw(
                &views,
                &mut alpha_mask_3d_render_phases,
                &mut view_entities,
                &batcher,
                &effect_draw_batches,
                &mut render_pipeline,
                specialized_render_pipelines.reborrow(),
                property_cache,
                pipeline_cache,
                &render_meshes,
                &mesh_allocator,
                |id, _batch, _view, slabs| OpaqueNoLightmap3dBatchSetKey {
                    pipeline: id,
                    draw_function: draw_effects_function_alpha_mask,
                    material_bind_group_index: None,
                    slabs,
                },
                // Unused for now
                || OpaqueNoLightmap3dBinKey {
                    asset_id: AssetId::<Mesh>::default().untyped(),
                },
                #[cfg(feature = "2d")]
                PipelineMode::Camera3d,
                ParticleRenderAlphaMaskPipelineKey::AlphaMask,
            );
        }

        // Opaque particles
        if !views.is_empty() {
            #[cfg(feature = "trace")]
            let _span_draw = bevy::log::info_span!("draw_opaque").entered();

            trace!("Emit effect draw calls for opaque 3D views...");

            let draw_effects_function_opaque = read_params
                .draw_functions_opaque
                .read()
                .get_id::<DrawEffects>()
                .unwrap();

            emit_binned_draw(
                &views,
                &mut opaque_3d_render_phases,
                &mut view_entities,
                &batcher,
                &effect_draw_batches,
                &mut render_pipeline,
                specialized_render_pipelines.reborrow(),
                property_cache,
                pipeline_cache,
                &render_meshes,
                &mesh_allocator,
                |id, _batch, _view, slabs| Opaque3dBatchSetKey {
                    pipeline: id,
                    draw_function: draw_effects_function_opaque,
                    material_bind_group_index: None,
                    slabs,
                    lightmap_slab: None,
                },
                // Unused for now
                || Opaque3dBinKey {
                    asset_id: AssetId::<Mesh>::default().untyped(),
                },
                #[cfg(feature = "2d")]
                PipelineMode::Camera3d,
                ParticleRenderAlphaMaskPipelineKey::Opaque,
            );
        }
    }
}

/// Once a child effect is batched, and therefore passed validations to be
/// updated and rendered this frame, dispatch a new GPU operation to fill the
/// indirect dispatch args of its init pass based on the number of GPU events
/// emitted in the previous frame and stored in its event buffer.
pub fn queue_init_indirect_workgroup_update(
    q_cached_effects: Query<(
        Entity,
        &CachedChildInfo,
        &CachedEffectEvents,
        &CachedReadyState,
    )>,
    mut init_fill_dispatch_queue: ResMut<InitFillDispatchQueue>,
) {
    debug_assert_eq!(
        GpuChildInfo::min_size().get() % 4,
        0,
        "Invalid GpuChildInfo alignment."
    );

    // Schedule some GPU buffer operation to update the number of workgroups to
    // dispatch during the indirect init pass of this effect based on the number of
    // GPU spawn events written in its buffer.
    for (entity, cached_child_info, cached_effect_events, cached_ready_state) in &q_cached_effects {
        if !cached_ready_state.is_ready() {
            trace!(
                "[Effect {:?}] Skipping init_fill_dispatch.enqueue() because effect is not ready.",
                entity
            );
            continue;
        }
        let init_indirect_dispatch_index = cached_effect_events.init_indirect_dispatch_index;
        let global_child_index = cached_child_info.global_child_index;
        trace!(
            "[Effect {:?}] init_fill_dispatch.enqueue(): src:global_child_index={} dst:init_indirect_dispatch_index={}",
            entity,
            global_child_index,
            init_indirect_dispatch_index,
        );
        assert!(global_child_index != u32::MAX);
        init_fill_dispatch_queue.enqueue(global_child_index, init_indirect_dispatch_index);
    }
}

/// Prepare GPU resources for effect rendering.
///
/// This system runs in the [`RenderSet::PrepareResources`] render set, after
/// Bevy has updated the [`ViewUniforms`], which need to be referenced to get
/// access to the current camera view.
pub(crate) fn prepare_gpu_resources(
    mut effects_meta: ResMut<EffectsMeta>,
    //mut effect_cache: ResMut<EffectCache>,
    mut event_cache: ResMut<EventCache>,
    mut effect_bind_groups: ResMut<EffectBindGroups>,
    mut sort_bind_groups: ResMut<SortBindGroups>,
    render_device: Res<RenderDevice>,
    render_queue: Res<RenderQueue>,
    view_uniforms: Res<ViewUniforms>,
    render_pipeline: Res<ParticlesRenderPipeline>,
    pipeline_cache: Res<PipelineCache>,
) {
    // Get the binding for the ViewUniform, the uniform data structure containing
    // the Camera data for the current view. If not available, we cannot render
    // anything.
    let Some(view_binding) = view_uniforms.uniforms.binding() else {
        return;
    };

    // Upload simulation parameters for this frame
    let prev_buffer_id = effects_meta.sim_params_uniforms.buffer().map(|b| b.id());
    effects_meta
        .sim_params_uniforms
        .write_buffer(&render_device, &render_queue);
    if prev_buffer_id != effects_meta.sim_params_uniforms.buffer().map(|b| b.id()) {
        // Buffer changed, invalidate bind groups
        effects_meta.update_sim_params_bind_group = None;
        effects_meta.init_and_indirect_sim_params_bind_group = None;
    }

    // Create the bind group for the camera/view parameters
    // FIXME - Not here!
    effects_meta.view_bind_group = Some(render_device.create_bind_group(
        "hanabi:bg:camera_view",
        &pipeline_cache.get_bind_group_layout(&render_pipeline.view_layout_desc),
        &BindGroupEntries::sequential((
            view_binding,
            effects_meta.sim_params_uniforms.binding().unwrap(),
        )),
    ));

    // Re-/allocate the draw indirect args buffer if needed
    if effects_meta
        .draw_indirect_args_buffer
        .allocate_gpu(&render_device, &render_queue)
    {
        // All those bind groups use the buffer so need to be re-created
        trace!("*** Draw indirect args buffer re-allocated; clearing all bind groups using it.");
        effects_meta.update_sim_params_bind_group = None;
        effects_meta.indirect_metadata_bind_group = None;
    }

    // Re-/allocate any GPU buffer if needed
    //effect_cache.prepare_buffers(&render_device, &render_queue, &mut
    // effect_bind_groups);
    event_cache.prepare_buffers(&render_device, &render_queue, &mut effect_bind_groups);
    sort_bind_groups.prepare_buffers(&render_device);
    if effects_meta
        .dispatch_indirect_args_buffer
        .prepare_buffers(&render_device)
    {
        // All those bind groups use the buffer so need to be re-created
        trace!("*** Dispatch indirect buffer for update pass re-allocated; clearing all bind groups using it.");
        effect_bind_groups.particle_slabs.clear();
    }
}

/// Update the [`GpuEffectMetadata`] of all the effects queued for update/render
/// this frame.
///
/// By this point, all effects should have a [`CachedEffectMetadata`] with a
/// valid allocation in the GPU table for a [`GpuEffectMetadata`] entry. This
/// system actually synchronize the CPU value with the GPU one in case of
/// change.
pub(crate) fn prepare_effect_metadata(
    render_device: Res<RenderDevice>,
    render_queue: Res<RenderQueue>,
    mut q_effects: Query<(
        MainEntity,
        Ref<ExtractedEffect>,
        Ref<CachedEffect>,
        Option<Ref<CachedChildInfo>>,
        Option<Ref<CachedParentInfo>>,
        Option<Ref<CachedDrawIndirectArgs>>,
        Option<Ref<CachedEffectEvents>>,
        Option<Ref<CachedEffectProperties>>,
        &mut CachedEffectMetadata,
    )>,
    mut effects_meta: ResMut<EffectsMeta>,
    mut effect_bind_groups: ResMut<EffectBindGroups>,
) {
    #[cfg(feature = "trace")]
    let _span = bevy::log::info_span!("prepare_effect_metadata").entered();
    trace!("prepare_effect_metadata");

    for (
        main_entity,
        extracted_effect,
        cached_effect,
        maybe_cached_child_info,
        maybe_cached_parent_info,
        maybe_cached_draw_indirect_args,
        maybe_cached_effect_events,
        maybe_cached_effect_properties,
        mut cached_effect_metadata,
    ) in &mut q_effects
    {
        // Check if anything relevant to GpuEffectMetadata changed this frame; otherwise
        // early out and skip this effect.
        let is_changed_ee = extracted_effect.is_changed();
        let is_changed_ce = cached_effect.is_changed();
        let is_changed_cci = maybe_cached_child_info
            .as_ref()
            .map(|cci| cci.is_changed())
            .unwrap_or(false);
        let is_changed_cpi = maybe_cached_parent_info
            .as_ref()
            .map(|cpi| cpi.is_changed())
            .unwrap_or(false);
        let is_changed_cdia = maybe_cached_draw_indirect_args
            .as_ref()
            .map(|cdia| cdia.is_changed())
            .unwrap_or(false);
        let is_changed_cee = maybe_cached_effect_events
            .as_ref()
            .map(|cee| cee.is_changed())
            .unwrap_or(false);
        let is_changed_cep = maybe_cached_effect_properties
            .as_ref()
            .map(|cep| cep.is_changed())
            .unwrap_or(false);
        trace!(
            "Preparting GpuEffectMetadata for effect {:?}: is_changed[] = {} {} {} {} {} {} {}",
            main_entity,
            is_changed_ee,
            is_changed_ce,
            is_changed_cci,
            is_changed_cpi,
            is_changed_cdia,
            is_changed_cee,
            is_changed_cep
        );
        if !is_changed_ee
            && !is_changed_ce
            && !is_changed_cci
            && !is_changed_cpi
            && !is_changed_cdia
            && !is_changed_cee
            && !is_changed_cep
        {
            continue;
        }

        let capacity = cached_effect.slice.len();

        // Global and local indices of this effect as a child of another (parent) effect
        let (global_child_index, local_child_index) = maybe_cached_child_info
            .map(|cci| (cci.global_child_index, cci.local_child_index))
            .unwrap_or((u32::MAX, u32::MAX));

        // Base index of all children of this (parent) effect
        let base_child_index = maybe_cached_parent_info
            .map(|cpi| {
                debug_assert_eq!(
                    cpi.byte_range.start % GpuChildInfo::SHADER_SIZE.get() as u32,
                    0
                );
                cpi.byte_range.start / GpuChildInfo::SHADER_SIZE.get() as u32
            })
            .unwrap_or(u32::MAX);

        let particle_stride = extracted_effect.particle_layout.min_binding_size32().get() / 4;
        let sort_key_offset = extracted_effect
            .particle_layout
            .byte_offset(Attribute::RIBBON_ID)
            .map(|byte_offset| byte_offset / 4)
            .unwrap_or(u32::MAX);
        // raea: a blended effect (no ribbons) sorts by its particles' view depth from the main camera, far first
        // (vfx_sort_fill.wesl reads the flags; raea #52)
        let sort_key_offset = match extracted_effect.layout_flags.raea_sorted()
            && !extracted_effect.layout_flags.contains(LayoutFlags::RIBBONS)
        {
            true => extracted_effect
                .particle_layout
                .byte_offset(Attribute::POSITION)
                .map(|byte_offset| {
                    let local = extracted_effect
                        .layout_flags
                        .contains(LayoutFlags::LOCAL_SPACE_SIMULATION);
                    byte_offset / 4 | RAEA_SORT_BY_DISTANCE | if local { RAEA_SORT_LOCAL } else { 0 }
                })
                .unwrap_or(u32::MAX),
            false => sort_key_offset,
        };
        let sort_key2_offset = extracted_effect
            .particle_layout
            .byte_offset(Attribute::AGE)
            .map(|byte_offset| byte_offset / 4)
            .unwrap_or(u32::MAX);

        let gpu_effect_metadata = GpuEffectMetadata {
            capacity,
            alive_count: 0,
            max_update: 0,
            max_spawn: capacity,
            indirect_write_index: 0,
            indirect_draw_index: maybe_cached_draw_indirect_args
                .map(|cdia| cdia.get_row().0)
                .unwrap_or(u32::MAX),
            init_indirect_dispatch_index: maybe_cached_effect_events
                .map(|cee| cee.init_indirect_dispatch_index)
                .unwrap_or(u32::MAX),
            properties_array_index: maybe_cached_effect_properties
                .map(|cep| cep.array_index)
                .unwrap_or(u32::MAX),
            local_child_index,
            global_child_index,
            base_child_index,
            particle_stride,
            sort_key_offset,
            sort_key2_offset,
            ..default()
        };

        // Insert of update entry in GPU buffer table
        assert!(cached_effect_metadata.table_id.is_valid());
        if gpu_effect_metadata != cached_effect_metadata.metadata {
            effects_meta
                .effect_metadata_buffer
                .update(cached_effect_metadata.table_id, gpu_effect_metadata);

            cached_effect_metadata.metadata = gpu_effect_metadata;

            // This triggers on all new spawns and annoys everyone; silence until we can at
            // least warn only on non-first-spawn, and ideally split indirect data from that
            // struct so we don't overwrite it and solve the issue.
            debug!(
                "Updated metadata entry {} for effect {:?}, this will reset it.",
                cached_effect_metadata.table_id.0, main_entity
            );
        }
    }

    // Once all EffectMetadata values are written, schedule a GPU upload
    if effects_meta
        .effect_metadata_buffer
        .allocate_gpu(render_device.as_ref(), render_queue.as_ref())
    {
        // All those bind groups use the buffer so need to be re-created
        trace!("*** Effect metadata buffer re-allocated; clearing all bind groups using it.");
        effects_meta.indirect_metadata_bind_group = None;
        effect_bind_groups.init_metadata_bind_groups.clear();
        effect_bind_groups.update_metadata_bind_groups.clear();
    }
}

/// Read the queued ribbon sort fill dispatch operations and enqueue the
/// corresponding GPU buffer fill dispatch operations.
///
/// This system runs after [`prepare_effect_metadata()`] has (re-)allocated the
/// effect metadata buffer to this frame's effect count, so that it captures the
/// correctly-sized buffer and computes in-bounds dynamic offsets. It must run
/// before [`queue_init_fill_dispatch_ops()`], which uploads the shared
/// [`GpuBufferOperations`] args buffer to the GPU at the end of the frame.
pub(crate) fn queue_sort_fill_dispatch_ops(
    effects_meta: Res<EffectsMeta>,
    sort_bind_groups: Res<SortBindGroups>,
    sort_fill_dispatch_queue: Res<SortFillDispatchQueue>,
    mut gpu_buffer_operations: ResMut<GpuBufferOperations>,
    mut batcher: ResMut<Batcher>,
) {
    #[cfg(feature = "trace")]
    let _span = bevy::log::info_span!("queue_sort_fill_dispatch_ops").entered();
    trace!("queue_sort_fill_dispatch_ops");

    if sort_fill_dispatch_queue.is_empty() {
        return;
    }

    debug_assert!(batcher.sort_fill_prefix_sum_queue_index.is_none());
    debug_assert!(batcher.fill_dispatch_queue_index.is_none());
    debug_assert!(batcher.sort_dispatch_queue_index.is_none());
    let Some(prefix_sum_buffer) = batcher.prefix_sum_buffer() else {
        error!("Missing prefix sum buffer for ribbon sort. This is a bug.");
        return;
    };
    let queue_indices = sort_fill_dispatch_queue.submit(
        &effects_meta.effect_metadata_buffer,
        prefix_sum_buffer,
        &sort_bind_groups,
        &mut gpu_buffer_operations,
    );
    batcher.sort_fill_prefix_sum_queue_index = queue_indices.alive_count_copy;
    batcher.fill_dispatch_queue_index = queue_indices.fill_dispatch;
    batcher.sort_dispatch_queue_index = queue_indices.sort_dispatch;
}

/// Read the queued init fill dispatch operations, batch them together by
/// contiguous source and destination entries in the buffers, and enqueue
/// corresponding GPU buffer fill dispatch operations for all batches.
///
/// This system runs after the GPU buffers have been (re-)allocated in
/// [`prepare_gpu_resources()`], so that it can read the new buffer IDs and
/// reference them from the generic [`GpuBufferOperationQueue`].
pub(crate) fn queue_init_fill_dispatch_ops(
    event_cache: Res<EventCache>,
    render_device: Res<RenderDevice>,
    render_queue: Res<RenderQueue>,
    mut init_fill_dispatch_queue: ResMut<InitFillDispatchQueue>,
    mut gpu_buffer_operations: ResMut<GpuBufferOperations>,
) {
    // Submit all queued init fill dispatch operations with the proper buffers
    if !init_fill_dispatch_queue.is_empty() {
        let src_buffer = event_cache.child_infos().buffer();
        let dst_buffer = event_cache.init_indirect_dispatch_buffer();
        if let (Some(src_buffer), Some(dst_buffer)) = (src_buffer, dst_buffer) {
            init_fill_dispatch_queue.submit(src_buffer, dst_buffer, &mut gpu_buffer_operations);
        } else {
            if src_buffer.is_none() {
                warn!("Event cache has no allocated GpuChildInfo buffer, but there's {} init fill dispatch operation(s) queued. Ignoring those operations. This will prevent child particles from spawning.", init_fill_dispatch_queue.queue.len());
            }
            if dst_buffer.is_none() {
                warn!("Event cache has no allocated GpuDispatchIndirect buffer, but there's {} init fill dispatch operation(s) queued. Ignoring those operations. This will prevent child particles from spawning.", init_fill_dispatch_queue.queue.len());
            }
        }
    }

    // End the GpuBufferOperations frame and upload the args buffer to GPU, after
    // every submit() for the frame (the sort submit in
    // queue_sort_fill_dispatch_ops() is ordered before this system,
    // the init submit is just above). See begin_frame() in batch_effects() for the
    // full lifecycle.
    gpu_buffer_operations.end_frame(&render_device, &render_queue);
}

#[derive(SystemParam)]
pub struct PipelineParams<'w, 's> {
    dispatch_indirect_pipeline: Res<'w, DispatchIndirectPipeline>,
    prefix_sum_pipeline: Res<'w, PrefixSumPipeline>,
    utils_pipeline: Res<'w, UtilsPipeline>,
    init_pipeline: Res<'w, ParticlesInitPipeline>,
    update_pipeline: Res<'w, ParticlesUpdatePipeline>,
    marker: PhantomData<&'s usize>,
}

pub(crate) fn prepare_bind_groups(
    mut effects_meta: ResMut<EffectsMeta>,
    mut effect_cache: ResMut<EffectCache>,
    mut event_cache: ResMut<EventCache>,
    mut effect_bind_groups: ResMut<EffectBindGroups>,
    mut property_bind_groups: ResMut<PropertyBindGroups>,
    mut sort_bind_groups: ResMut<SortBindGroups>,
    property_cache: Res<PropertyCache>,
    batcher: Res<Batcher>,
    render_device: Res<RenderDevice>,
    pipeline_cache: Res<PipelineCache>,
    pipelines: PipelineParams,
    gpu_images: Res<RenderAssets<GpuImage>>,
    mut gpu_buffer_operation_queue: ResMut<GpuBufferOperations>,
) {
    // We can't simulate nor render anything without at least the spawner buffer
    if effects_meta.spawner_buffer.is_empty() {
        return;
    }
    let Some(spawner_buffer) = effects_meta.spawner_buffer.buffer().cloned() else {
        error!("Missing spawner GPU buffer!");
        return;
    };
    let Some(prefix_sum_buffer) = batcher.prefix_sum_buffer().cloned() else {
        error!("Missing prefix sum GPU buffer!");
        return;
    };
    let Some(batch_info_buffer) = batcher.batch_info_buffer().cloned() else {
        error!("Missing batch info GPU buffer!");
        return;
    };
    let Some(dispatch_indirect_args_buffer) =
        effects_meta.dispatch_indirect_args_buffer.buffer().cloned()
    else {
        error!("Missing dispatch indirect GPU buffer!");
        return;
    };

    // Workaround for too many params in system (TODO: refactor to split work?)
    let dispatch_indirect_pipeline = pipelines.dispatch_indirect_pipeline.into_inner();
    let prefix_sum_pipeline = pipelines.prefix_sum_pipeline.into_inner();
    let utils_pipeline = pipelines.utils_pipeline.into_inner();
    let init_pipeline = pipelines.init_pipeline.into_inner();
    let update_pipeline = pipelines.update_pipeline.into_inner();

    // Ensure child_infos@3 bind group for the indirect pass is available if needed.
    // This returns `None` if the buffer is not ready, either because it's not
    // created yet or because it's not needed (no child effect).
    event_cache.ensure_indirect_child_info_buffer_bind_group(&render_device);

    {
        #[cfg(feature = "trace")]
        let _span = bevy::log::info_span!("shared_bind_groups").entered();

        // Create the sim_params@0 bind group for the global simulation parameters,
        // which is shared by the init and update passes.
        if effects_meta.update_sim_params_bind_group.is_none() {
            if let Some(draw_indirect_buffer) = effects_meta.draw_indirect_args_buffer.buffer() {
                effects_meta.update_sim_params_bind_group = Some(render_device.create_bind_group(
                    "hanabi:bg:vfx_update:sim_params@0",
                    &pipeline_cache.get_bind_group_layout(&update_pipeline.sim_params_layout_desc),
                    &BindGroupEntries::sequential((
                        // @group(0) @binding(0) var<uniform> sim_params : SimParams;
                        effects_meta.sim_params_uniforms.binding().unwrap(),
                        // @group(0) @binding(1) var<storage, read_write> draw_indirect_buffer :
                        // array<DrawIndexedIndirectArgs>;
                        draw_indirect_buffer.as_entire_binding(),
                    )),
                ));
            } else {
                debug!("Cannot allocate bind group for vfx_update:sim_params@0 - draw_indirect_buffer not ready");
            }
        }
        if effects_meta
            .init_and_indirect_sim_params_bind_group
            .is_none()
        {
            debug!("Re-creating init_and_indirect_sim_params_bind_group...");
            effects_meta.init_and_indirect_sim_params_bind_group =
                Some(render_device.create_bind_group(
                    "hanabi:bg:vfx_indirect:sim_params@0",
                    &pipeline_cache.get_bind_group_layout(&init_pipeline.sim_params_layout_desc), // FIXME - Shared with init
                    // @group(0) @binding(0) var<uniform> sim_params : SimParams;
                    &BindGroupEntries::single(effects_meta.sim_params_uniforms.binding().unwrap()),
                ));
        }

        // Create the @1 bind group for the indirect dispatch preparation pass of all
        // effects at once
        effects_meta.indirect_metadata_bind_group = match (
            effects_meta.effect_metadata_buffer.buffer(),
            effects_meta.draw_indirect_args_buffer.buffer(),
        ) {
            (Some(effect_metadata_buffer), Some(draw_indirect_buffer)) => {
                // Base bind group for indirect pass
                Some(render_device.create_bind_group(
                    "hanabi:bg:vfx_indirect:metadata@1",
                    &pipeline_cache.get_bind_group_layout(
                        &dispatch_indirect_pipeline.effect_metadata_bind_group_layout_desc,
                    ),
                    &BindGroupEntries::sequential((
                        // @group(1) @binding(0) var<storage, read_write> effect_metadatas :
                        // array<EffectMetadata>;
                        effect_metadata_buffer.as_entire_binding(),
                        // @group(1) @binding(1) var<storage, read_write> draw_indirect_buffer :
                        // array<u32>;
                        draw_indirect_buffer.as_entire_binding(),
                    )),
                ))
            }

            // Some buffer is not yet available, can't create the bind group
            _ => None,
        };

        // Create the @2 bind group for the indirect dispatch preparation pass of all
        // effects at once
        if effects_meta.indirect_spawner_bind_group.is_none() {
            let bind_group = render_device.create_bind_group(
                "hanabi:bg:vfx_indirect:spawner@2",
                &pipeline_cache.get_bind_group_layout(
                    &dispatch_indirect_pipeline.spawner_bind_group_layout_desc,
                ),
                &BindGroupEntries::sequential((
                    // @group(2) @binding(0) var<storage, read> spawner_buffer : array<Spawner>;
                    spawner_buffer.as_entire_binding(),
                    // @group(2) @binding(1) var<storage, read_write> prefix_sum : array<u32>;
                    prefix_sum_buffer.as_entire_binding(),
                )),
            );

            effects_meta.indirect_spawner_bind_group = Some(bind_group);
        }

        // Create the @0 bind group for the prefix sum pass of all batches at once
        if effects_meta.prefix_sum_bind_group.is_none() {
            effects_meta.prefix_sum_bind_group = Some(render_device.create_bind_group(
                "hanabi:bind_group:vfx_prefix_sum:@0",
                &pipeline_cache.get_bind_group_layout(&prefix_sum_pipeline.bind_group_layout_desc),
                &BindGroupEntries::sequential((
                    // @group(0) @binding(0) var<storage, read> batch_infos :
                    // array<BatchInfo>;
                    batch_info_buffer.as_entire_binding(),
                    // @group(0) @binding(1) var<storage, read_write> prefix_sum :
                    // array<u32>;
                    prefix_sum_buffer.as_entire_binding(),
                    // @group(0) @binding(2) var<storage, read_write>
                    // dispatch_indirect_buffer : array<DispatchIndirectArgs>;
                    dispatch_indirect_args_buffer.as_entire_binding(),
                )),
            ));
        }
    }

    // Create the per-slab bind groups
    trace!("Create per-slab bind groups...");
    for (slab_index, particle_slab) in effect_cache.slabs().iter().enumerate() {
        #[cfg(feature = "trace")]
        let _span_buffer = bevy::log::info_span!("create_buffer_bind_groups").entered();

        let Some(particle_slab) = particle_slab else {
            trace!(
                "Particle slab index #{} has no allocated EffectBuffer, skipped.",
                slab_index
            );
            continue;
        };

        // Ensure all effects in this batch have a bind group for the entire buffer of
        // the group, since the update phase runs on an entire group/buffer at once,
        // with all the effect instances in it batched together.
        trace!("effect particle slab_index=#{}", slab_index);
        effect_bind_groups
            .particle_slabs
            .entry(SlabId::new(slab_index as u32))
            .or_insert_with(|| {
                // Bind group particle@1 for render pass
                trace!("Creating particle@1 bind group for buffer #{slab_index} in render pass");
                let render = render_device.create_bind_group(
                    &format!("hanabi:bg:render:particles@1:vfx{slab_index}")[..],
                    particle_slab.render_particles_buffer_layout(),
                    &BindGroupEntries::sequential((
                        // @group(1) @binding(0) var<storage, read> particle_buffer :
                        // ParticleBuffer;
                        particle_slab.as_entire_binding_particle(),
                        // @group(1) @binding(1) var<storage, read> indirect_buffer :
                        // IndirectBuffer;
                        particle_slab.as_entire_binding_indirect(),
                    )),
                );

                BufferBindGroups { render }
            });
    }

    // Create bind groups for queued GPU buffer operations
    gpu_buffer_operation_queue.create_bind_groups(&render_device, utils_pipeline);

    // Create the per-effect-batch bind groups
    for effect_batch in batcher.iter() {
        #[cfg(feature = "trace")]
        let _span_buffer = bevy::log::info_span!("create_batch_bind_groups").entered();

        // Create the property bind group @2 if needed
        if let Err(err) = property_bind_groups.ensure_exists(
            &effect_batch.property_key,
            &property_cache,
            &spawner_buffer,
            &prefix_sum_buffer,
            &effect_batch.texture_layout,
            &effect_batch.textures[..],
            &batch_info_buffer,
            &render_device,
            &pipeline_cache,
            &gpu_images,
        ) {
            error!("Failed to create property bind group for effect batch: {err:?}");
            continue;
        }

        // Bind group particle@1 for the simulate compute shaders (init and udpate) to
        // simulate particles.
        if effect_cache
            .create_particle_sim_bind_group(
                &effect_batch.slab_id,
                &render_device,
                effect_batch.particle_layout.min_binding_size32(),
                effect_batch.parent_min_binding_size,
                effect_batch.parent_binding_source.as_ref(),
                &pipeline_cache,
            )
            .is_err()
        {
            error!("No particle buffer allocated for effect batch.");
            continue;
        }

        // Bind group @3 of init pass
        // FIXME - this is instance-dependent, not buffer-dependent
        {
            let consume_gpu_spawn_events = effect_batch
                .layout_flags
                .contains(LayoutFlags::CONSUME_GPU_SPAWN_EVENTS);
            let consume_event_buffers = if let BatchSpawnInfo::GpuSpawner { .. } =
                effect_batch.spawn_info
            {
                assert!(consume_gpu_spawn_events);
                let cached_effect_events = effect_batch.cached_effect_events.as_ref().unwrap();
                Some(ConsumeEventBuffers {
                    child_infos_buffer: event_cache.child_infos_buffer().unwrap(),
                    events: BufferSlice {
                        buffer: event_cache
                            .get_buffer(cached_effect_events.buffer_index)
                            .unwrap(),
                        // Note: event range is in u32 count, not bytes
                        offset: cached_effect_events.range.start * 4,
                        size: NonZeroU32::new(cached_effect_events.range.len() as u32 * 4).unwrap(),
                    },
                })
            } else {
                assert!(!consume_gpu_spawn_events);
                None
            };
            let Some(init_metadata_layout_desc) =
                effect_cache.metadata_init_bind_group_layout_desc(consume_gpu_spawn_events)
            else {
                continue;
            };
            if effect_bind_groups
                .get_or_create_init_metadata(
                    effect_batch,
                    &render_device,
                    &pipeline_cache.get_bind_group_layout(init_metadata_layout_desc),
                    effects_meta.effect_metadata_buffer.buffer().unwrap(),
                    consume_event_buffers,
                )
                .is_err()
            {
                continue;
            }
        }

        // Bind group @3 of update pass
        // FIXME - this is instance-dependent, not buffer-dependent#
        {
            let num_event_buffers = effect_batch.child_event_buffers.len() as u32;

            let Some(update_metadata_layout_desc) =
                effect_cache.metadata_update_bind_group_layout_desc(num_event_buffers)
            else {
                continue;
            };
            if effect_bind_groups
                .get_or_create_update_metadata(
                    effect_batch,
                    &render_device,
                    &pipeline_cache.get_bind_group_layout(update_metadata_layout_desc),
                    effects_meta.effect_metadata_buffer.buffer().unwrap(),
                    event_cache.child_infos_buffer(),
                    &effect_batch.child_event_buffers[..],
                )
                .is_err()
            {
                continue;
            }
        }

        if effect_batch.layout_flags.raea_sorted() { // raea: and blended effects (#52)
            let effect_buffer = effect_cache.get_slab(&effect_batch.slab_id).unwrap();

            // Bind group @0 of sort-fill pass
            let particle_buffer = effect_buffer.particle_buffer();
            let indirect_index_buffer = effect_buffer.indirect_index_buffer();
            let effect_metadata_buffer = effects_meta.effect_metadata_buffer.buffer().unwrap();
            if let Err(err) = sort_bind_groups.ensure_sort_fill_bind_group(
                &render_device,
                &effect_batch.particle_layout,
                particle_buffer,
                indirect_index_buffer,
                effect_metadata_buffer,
                &spawner_buffer,
                effects_meta.sim_params_uniforms.buffer().unwrap(), // raea: (#52)
                &pipeline_cache,
            ) {
                error!(
                    "Failed to create sort-fill bind group @0 for ribbon effect: {:?}",
                    err
                );
                continue;
            }

            // Bind group @0 of sort pass
            sort_bind_groups.ensure_sort_bind_group(&render_device, &pipeline_cache);
            sort_bind_groups.ensure_sort_merge_bind_group(&render_device, &pipeline_cache);

            // Bind group @0 of sort-copy pass
            let indirect_index_buffer = effect_buffer.indirect_index_buffer();
            if let Err(err) = sort_bind_groups.ensure_sort_copy_bind_group(
                &render_device,
                indirect_index_buffer,
                effect_metadata_buffer,
                &spawner_buffer,
                &pipeline_cache,
            ) {
                error!(
                    "Failed to create sort-copy bind group @0 for ribbon effect: {:?}",
                    err
                );
                continue;
            }
        }
    }
}

type DrawEffectsSystemState = SystemState<(
    SRes<EffectsMeta>,
    SRes<EffectBindGroups>,
    SRes<PropertyBindGroups>,
    SRes<PipelineCache>,
    SRes<RenderAssets<RenderMesh>>,
    SRes<MeshAllocator>,
    SQuery<Read<ViewUniformOffset>>,
    SRes<Batcher>,
    SQuery<Read<EffectDrawBatch>>,
)>;

/// Draw function for rendering all active effects for the current frame.
///
/// Effects are rendered in the [`Transparent2d`] phase of the main 2D pass,
/// and the [`Transparent3d`] phase of the main 3D pass.
pub(crate) struct DrawEffects {
    params: DrawEffectsSystemState,
}

impl DrawEffects {
    pub fn new(world: &mut World) -> Self {
        Self {
            params: SystemState::new(world),
        }
    }
}

/// Draw all particles of a single effect in view, in 2D or 3D.
///
/// FIXME: use pipeline ID to look up which group index it is.
fn draw<'w>(
    world: &'w World,
    pass: &mut TrackedRenderPass<'w>,
    view: Entity,
    entity: (Entity, MainEntity),
    pipeline_id: CachedRenderPipelineId,
    params: &mut DrawEffectsSystemState,
) {
    let (
        effects_meta,
        effect_bind_groups,
        property_bind_groups,
        pipeline_cache,
        meshes,
        mesh_allocator,
        views,
        batcher,
        effect_draw_batches,
    ) = params.get(world).unwrap();
    let view_uniform = views.get(view).unwrap();
    let effects_meta = effects_meta.into_inner();
    let effect_bind_groups = effect_bind_groups.into_inner();
    let property_bind_groups = property_bind_groups.into_inner();
    let meshes = meshes.into_inner();
    let mesh_allocator = mesh_allocator.into_inner();
    let effect_draw_batch = effect_draw_batches.get(entity.0).unwrap();
    let effect_batch = batcher.get(effect_draw_batch.effect_batch_index).unwrap();

    let Some(pipeline) = pipeline_cache.into_inner().get_render_pipeline(pipeline_id) else {
        trace!("ERROR: Failed to find render pipeline ID {:?}", pipeline_id);
        return;
    };

    trace!("render pass");

    pass.set_render_pipeline(pipeline);

    let Some(render_mesh): Option<&RenderMesh> = meshes.get(effect_batch.mesh) else {
        trace!("Missing render mesh; can't draw. Skipped.");
        return;
    };
    let Some(vertex_buffer_slice) = mesh_allocator.mesh_vertex_slice(&effect_batch.mesh) else {
        trace!("Missing vertex buffer slice; can't draw. Skipped.");
        return;
    };

    // Vertex buffer containing the particle model to draw. Generally a quad.
    // FIXME - need to upload "vertex_buffer_slice.range.start as i32" into
    // "base_vertex" in the indirect struct...
    pass.set_vertex_buffer(0, vertex_buffer_slice.buffer.slice(..));

    // View properties (camera matrix, etc.)
    pass.set_bind_group(
        0,
        effects_meta.view_bind_group.as_ref().unwrap(),
        &[view_uniform.offset],
    );

    // Particles buffer
    pass.set_bind_group(
        1,
        effect_bind_groups
            .particle_render(&effect_batch.slab_id)
            .unwrap(),
        &[],
    );

    let Some(indirect_buffer) = effects_meta.draw_indirect_args_buffer.buffer() else {
        trace!(
            "The draw indirect buffer containing the indirect draw args is not ready for batch slab_id=#{}. Skipping draw call.",
            effect_batch.slab_id.index(),
        );
        return;
    };

    let spawner_size = effects_meta.spawner_buffer.aligned_size() as u32;
    assert!(spawner_size >= GpuSpawnerParams::min_size().get() as u32);

    match render_mesh.buffer_info {
        RenderMeshBufferInfo::Indexed { index_format, .. } => {
            let Some(index_buffer_slice) = mesh_allocator.mesh_index_slice(&effect_batch.mesh)
            else {
                trace!(
                    "The index buffer for indexed rendering is not ready for batch slab_id=#{}. Skipping draw call.",
                    effect_batch.slab_id.index(),
                );
                return;
            };

            pass.set_index_buffer(index_buffer_slice.buffer.slice(..), index_format);
            // Note: multi_draw_indexed_indirect() only works if first_instance is
            // available, which doesn't work without validation in wgpu. This is due to a
            // limitation in DX12. We could have multidraw in Vulkan only though, but that
            // requires dynamically switching.
            // pass.multi_draw_indexed_indirect(
            //     indirect_buffer,
            //     draw_indirect_offset,
            //     effect_batch.effect_count,
            // );
            let with_prefix_sum = false; // per-effect, no draw batching
            trace!(
                "Emit non-batched draw_indexed_indirect() for {} effect instances...",
                effect_batch.effect_data.len()
            );
            for effect_data in &effect_batch.effect_data {
                pass.set_bind_group(
                    2,
                    property_bind_groups
                        .get(&effect_batch.property_key, with_prefix_sum)
                        .unwrap(),
                    &[effect_data.render_batch_info_offset],
                );
                let draw_indirect_index = effect_data.draw_indirect_buffer_row_index.0;
                assert_eq!(GpuDrawIndexedIndirectArgs::SHADER_SIZE.get(), 20);
                let draw_indirect_offset =
                    draw_indirect_index as u64 * GpuDrawIndexedIndirectArgs::SHADER_SIZE.get();
                trace!(
                    "+ batch-info @+{}B (row #{}), draw_indirect_offset=+{}B (row #{})",
                    effect_data.render_batch_info_offset,
                    effect_data.render_batch_info_offset / GpuBatchInfo::SHADER_SIZE.get() as u32,
                    draw_indirect_offset,
                    draw_indirect_index,
                );
                pass.draw_indexed_indirect(indirect_buffer, draw_indirect_offset);
            }
        }
        RenderMeshBufferInfo::NonIndexed => {
            // Note: multi_draw_indexed_indirect() only works if first_instance is
            // available, which doesn't work without validation in wgpu. This is due to a
            // limitation in DX12. We could have multidraw in Vulkan only though, but that
            // requires dynamically switching.
            // pass.multi_draw_indirect(
            //     indirect_buffer,
            //     draw_indirect_offset,
            //     effect_batch.effect_count,
            // );
            let with_prefix_sum = false; // per-effect, no draw batching
            trace!(
                "Emit non-batched draw_indirect() for {} effect instances...",
                effect_batch.effect_data.len()
            );
            for effect_data in &effect_batch.effect_data {
                pass.set_bind_group(
                    2,
                    property_bind_groups
                        .get(&effect_batch.property_key, with_prefix_sum)
                        .unwrap(),
                    &[effect_data.render_batch_info_offset],
                );
                let draw_indirect_index = effect_data.draw_indirect_buffer_row_index.0;
                assert_eq!(GpuDrawIndexedIndirectArgs::SHADER_SIZE.get(), 20);
                let draw_indirect_offset =
                    draw_indirect_index as u64 * GpuDrawIndexedIndirectArgs::SHADER_SIZE.get();
                pass.draw_indirect(indirect_buffer, draw_indirect_offset);
            }
        }
    }
}

#[cfg(feature = "2d")]
impl Draw<Transparent2d> for DrawEffects {
    fn draw<'w>(
        &mut self,
        world: &'w World,
        pass: &mut TrackedRenderPass<'w>,
        view: Entity,
        item: &Transparent2d,
    ) -> Result<(), DrawError> {
        trace!("Draw<Transparent2d>: view={:?}", view);
        draw(
            world,
            pass,
            view,
            item.entity,
            item.pipeline,
            &mut self.params,
        );
        Ok(())
    }
}

#[cfg(feature = "3d")]
impl Draw<Transparent3d> for DrawEffects {
    fn draw<'w>(
        &mut self,
        world: &'w World,
        pass: &mut TrackedRenderPass<'w>,
        view: Entity,
        item: &Transparent3d,
    ) -> Result<(), DrawError> {
        trace!("Draw<Transparent3d>: view={:?}", view);
        draw(
            world,
            pass,
            view,
            item.entity,
            item.pipeline,
            &mut self.params,
        );
        Ok(())
    }
}

#[cfg(feature = "3d")]
impl Draw<AlphaMask3d> for DrawEffects {
    fn draw<'w>(
        &mut self,
        world: &'w World,
        pass: &mut TrackedRenderPass<'w>,
        view: Entity,
        item: &AlphaMask3d,
    ) -> Result<(), DrawError> {
        trace!("Draw<AlphaMask3d>: view={:?}", view);
        draw(
            world,
            pass,
            view,
            item.representative_entity,
            item.batch_set_key.pipeline,
            &mut self.params,
        );
        Ok(())
    }
}

#[cfg(feature = "3d")]
impl Draw<Opaque3d> for DrawEffects {
    fn draw<'w>(
        &mut self,
        world: &'w World,
        pass: &mut TrackedRenderPass<'w>,
        view: Entity,
        item: &Opaque3d,
    ) -> Result<(), DrawError> {
        trace!("Draw<Opaque3d>: view={:?}", view);
        draw(
            world,
            pass,
            view,
            item.representative_entity,
            item.batch_set_key.pipeline,
            &mut self.params,
        );
        Ok(())
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum HanabiPipelineId {
    Invalid,
    Cached(CachedComputePipelineId),
}

#[derive(Debug)]
pub(crate) enum ComputePipelineError {
    Queued,
    Creating,
    Error,
}

impl From<&CachedPipelineState> for ComputePipelineError {
    fn from(value: &CachedPipelineState) -> Self {
        match value {
            CachedPipelineState::Queued => Self::Queued,
            CachedPipelineState::Creating(_) => Self::Creating,
            CachedPipelineState::Err(_) => Self::Error,
            _ => panic!("Trying to convert Ok state to error."),
        }
    }
}

pub(crate) struct HanabiComputePass<'a> {
    /// Pipeline cache to fetch cached compute pipelines by ID.
    pipeline_cache: &'a PipelineCache,
    /// WGPU compute pass.
    compute_pass: ComputePass<'a>,
    /// Current pipeline (cached).
    pipeline_id: HanabiPipelineId,
}

impl<'a> Deref for HanabiComputePass<'a> {
    type Target = ComputePass<'a>;

    fn deref(&self) -> &Self::Target {
        &self.compute_pass
    }
}

impl DerefMut for HanabiComputePass<'_> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.compute_pass
    }
}

impl<'a> HanabiComputePass<'a> {
    pub fn new(
        label: &str,
        pipeline_cache: &'a PipelineCache,
        render_context: &'a mut RenderContext,
    ) -> Self {
        let compute_pass =
            render_context
                .command_encoder()
                .begin_compute_pass(&ComputePassDescriptor {
                    label: Some(label),
                    timestamp_writes: None,
                });
        Self {
            pipeline_cache,
            compute_pass,
            pipeline_id: HanabiPipelineId::Invalid,
        }
    }

    pub fn set_cached_compute_pipeline(
        &mut self,
        pipeline_id: CachedComputePipelineId,
    ) -> Result<(), ComputePipelineError> {
        if HanabiPipelineId::Cached(pipeline_id) == self.pipeline_id {
            trace!("set_cached_compute_pipeline() id={pipeline_id:?} -> already set; skipped");
            return Ok(());
        }
        trace!("set_cached_compute_pipeline() id={pipeline_id:?}");
        let Some(pipeline) = self.pipeline_cache.get_compute_pipeline(pipeline_id) else {
            let state = self.pipeline_cache.get_compute_pipeline_state(pipeline_id);
            if let CachedPipelineState::Err(err) = state {
                error!(
                    "Failed to find compute pipeline #{}: {:?}",
                    pipeline_id.id(),
                    err
                );
            } else {
                debug!("Compute pipeline not ready #{}", pipeline_id.id());
            }
            return Err(state.into());
        };
        self.compute_pass.set_pipeline(pipeline);
        self.pipeline_id = HanabiPipelineId::Cached(pipeline_id);
        Ok(())
    }
}

fn simulate(
    mut render_context: RenderContext,
    pipeline_cache: Res<PipelineCache>,
    effects_meta: Res<EffectsMeta>,
    effect_bind_groups: Res<EffectBindGroups>,
    property_bind_groups: Res<PropertyBindGroups>,
    sort_bind_groups: Res<SortBindGroups>,
    utils_pipeline: Res<UtilsPipeline>,
    effect_cache: Res<EffectCache>,
    event_cache: Res<EventCache>,
    gpu_buffer_operations: Res<GpuBufferOperations>,
    batcher: Res<Batcher>,
    init_fill_dispatch_queue: Res<InitFillDispatchQueue>,
) {
    trace!("simulate()");

    // Make sure to schedule any buffer copy before accessing their content later in
    // the GPU commands below.
    {
        let command_encoder = render_context.command_encoder();
        effects_meta
            .dispatch_indirect_args_buffer
            .write_buffers(command_encoder);
        effects_meta
            .draw_indirect_args_buffer
            .write_buffer(command_encoder);
        effects_meta
            .effect_metadata_buffer
            .write_buffer(command_encoder);
        event_cache.write_buffers(command_encoder);
        sort_bind_groups.write_buffers(command_encoder);
    }

    // Compute init fill dispatch pass - Fill the indirect dispatch structs for any
    // upcoming init pass of this frame, based on the GPU spawn events emitted by
    // the update pass of their parent effect during the previous frame.
    if let Some(queue_index) = init_fill_dispatch_queue.submitted_queue_index.as_ref() {
        gpu_buffer_operations.dispatch(
            *queue_index,
            &mut render_context,
            &utils_pipeline,
            Some("hanabi:init_indirect_fill_dispatch"),
        );
    }

    // If there's no batch, there's nothing more to do. Avoid continuing because
    // some GPU resources are missing, which is expected when there's no effect but
    // is an error (and will log warnings/errors) otherwise.
    if batcher.is_empty() {
        return;
    }

    // Validate that the main buffers and other core resources are available. Avoid
    // dispatching partial work if not all passes will complete, as this generally
    // desynchronizes the buffers.
    let (
        Some(_),
        true,
        Some(indirect_metadata_bind_group),
        Some(init_and_indirect_sim_params_bind_group),
        Some(indirect_spawner_bind_group),
    ) = (
        effects_meta.spawner_buffer.buffer(),
        !effects_meta.spawner_buffer.is_empty(),
        &effects_meta.indirect_metadata_bind_group,
        &effects_meta.init_and_indirect_sim_params_bind_group,
        &effects_meta.indirect_spawner_bind_group,
    )
    else {
        trace!("WARN - Some resource not ready, can't simulate this frame.");
        return;
    };
    let Some(prefix_sum_bind_group) = &effects_meta.prefix_sum_bind_group else {
        trace!("WARN - Prefix sum bind group not ready, can't simulate this frame.");
        return;
    };
    let Some(indirect_buffer) = effects_meta.dispatch_indirect_args_buffer.buffer() else {
        trace!("WARN - Missing indirect buffer for update pass, can't simulate this frame.");
        // FIXME - Bevy doesn't allow returning custom errors here...
        return;
    };

    // Compute init pass
    {
        trace!("init: loop over effect batches...");

        let mut compute_pass =
            HanabiComputePass::new("hanabi:init", &pipeline_cache, &mut render_context);

        // Bind group simparams@0 is common to everything, only set once per init pass
        compute_pass.set_bind_group(
            0,
            effects_meta
                .init_and_indirect_sim_params_bind_group
                .as_ref()
                .unwrap(),
            &[],
        );

        // Dispatch init compute jobs for all batches
        for effect_batch in batcher.iter() {
            // Do not dispatch any init work if there's nothing to spawn this frame for the
            // batch. Note that this hopefully should have been skipped earlier.
            {
                let use_indirect_dispatch = effect_batch
                    .layout_flags
                    .contains(LayoutFlags::CONSUME_GPU_SPAWN_EVENTS);
                match effect_batch.spawn_info {
                    BatchSpawnInfo::CpuSpawner { total_spawn_count } => {
                        assert!(!use_indirect_dispatch);
                        if total_spawn_count == 0 {
                            continue;
                        }
                    }
                    BatchSpawnInfo::GpuSpawner { .. } => {
                        assert!(use_indirect_dispatch);
                    }
                }
            }

            // Fetch bind group particle@1
            let Some(particle_bind_group) =
                effect_cache.particle_sim_bind_group(&effect_batch.slab_id)
            else {
                error!(
                    "Failed to find init particle@1 bind group for slab #{}",
                    effect_batch.slab_id.index()
                );
                continue;
            };

            // Fetch bind group metadata@3
            let Some(metadata_bind_group) = effect_bind_groups
                .init_metadata_bind_groups
                .get(&effect_batch.slab_id)
            else {
                error!(
                    "Failed to find init metadata@3 bind group for slab #{}",
                    effect_batch.slab_id.index()
                );
                continue;
            };

            if compute_pass
                .set_cached_compute_pipeline(effect_batch.init_and_update_pipeline_ids.init)
                .is_err()
            {
                error!("Failed to set init pipeline");
                continue;
            }

            // Compute dynamic offsets
            let spawner_base = effect_batch.spawner_base;
            let spawner_aligned_size = effects_meta.spawner_buffer.aligned_size();
            debug_assert!(spawner_aligned_size >= GpuSpawnerParams::min_size().get() as usize);
            let spawner_offset = spawner_base * spawner_aligned_size as u32;
            let batch_info_aligned_size = batcher.batch_info_buffer_aligned_size();
            let batch_info_offset = effect_batch.batch_info_id * batch_info_aligned_size;

            // Setup init pass
            compute_pass.set_bind_group(1, particle_bind_group, &[]);
            compute_pass.set_bind_group(
                2,
                property_bind_groups
                    .get(&effect_batch.property_key, true)
                    .unwrap(),
                &[batch_info_offset],
            );
            compute_pass.set_bind_group(3, &metadata_bind_group.bind_group, &[]);

            // Dispatch init job
            match effect_batch.spawn_info {
                // Indirect dispatch via GPU spawn events
                BatchSpawnInfo::GpuSpawner {
                    init_indirect_dispatch_index,
                    ..
                } => {
                    assert!(effect_batch
                        .layout_flags
                        .contains(LayoutFlags::CONSUME_GPU_SPAWN_EVENTS));

                    // Note: the indirect offset of a dispatch workgroup only needs
                    // 4-byte alignment
                    assert_eq!(GpuDispatchIndirectArgs::min_size().get(), 12);
                    let indirect_offset = init_indirect_dispatch_index as u64 * 12;

                    trace!(
                        "record commands for indirect init pipeline of effect {:?} \
                                init_indirect_dispatch_index={} \
                                indirect_offset={} \
                                property_key={:?}...",
                        effect_batch.handle,
                        init_indirect_dispatch_index,
                        indirect_offset,
                        effect_batch.property_key,
                    );

                    compute_pass.dispatch_workgroups_indirect(
                        event_cache.init_indirect_dispatch_buffer().unwrap(),
                        indirect_offset,
                    );
                }

                // Direct dispatch via CPU spawn count
                BatchSpawnInfo::CpuSpawner {
                    total_spawn_count: spawn_count,
                } => {
                    assert!(!effect_batch
                        .layout_flags
                        .contains(LayoutFlags::CONSUME_GPU_SPAWN_EVENTS));

                    const WORKGROUP_SIZE: u32 = 64;
                    let workgroup_count = spawn_count.div_ceil(WORKGROUP_SIZE);

                    trace!(
                        "record commands for init pipeline of effect {:?} \
                                (spawn {} particles => {} workgroups) spawner_base={} \
                                spawner_offset={} \
                                property_key={:?}...",
                        effect_batch.handle,
                        spawn_count,
                        workgroup_count,
                        spawner_base,
                        spawner_offset,
                        effect_batch.property_key,
                    );

                    compute_pass.dispatch_workgroups(workgroup_count, 1, 1);
                }
            }

            trace!("init compute dispatched");
        }
    }

    // Indirect dispatch compute pass
    {
        // Only start a compute pass if there's an effect; makes things clearer in
        // debugger.
        let mut compute_pass = HanabiComputePass::new(
            "hanabi:indirect_dispatch",
            &pipeline_cache,
            &mut render_context,
        );

        // Dispatch indirect dispatch compute job
        trace!("record commands for indirect dispatch pipeline...");

        let has_gpu_spawn_events = !event_cache.child_infos().is_empty();
        if has_gpu_spawn_events {
            if let Some(indirect_child_info_buffer_bind_group) =
                event_cache.indirect_child_info_buffer_bind_group()
            {
                assert!(has_gpu_spawn_events);
                compute_pass.set_bind_group(3, indirect_child_info_buffer_bind_group, &[]);
            } else {
                error!("Missing child_info_buffer@3 bind group for the vfx_indirect pass.");
                compute_pass.insert_debug_marker("ERROR:MissingIndirectBindGroup3");
                // FIXME - Bevy doesn't allow returning custom errors here...
                return;
            }
        }

        if compute_pass
            .set_cached_compute_pipeline(effects_meta.active_indirect_pipeline_id)
            .is_err()
        {
            compute_pass.insert_debug_marker("ERROR:FailedToSetIndirectComputePipeline");
            // FIXME - Bevy doesn't allow returning custom errors here...
            return;
        }

        //error!("FIXME - effect_metadata_buffer has gaps!!!! this won't work. len() is
        // the size exluding gaps!");
        const WORKGROUP_SIZE: u32 = 64;
        //let total_effect_count = effects_meta.effect_metadata_buffer.len();
        let total_effect_count = effects_meta.spawner_buffer.len() as u32;
        let workgroup_count = total_effect_count.div_ceil(WORKGROUP_SIZE);

        // Setup vfx_indirect pass
        compute_pass.set_bind_group(0, init_and_indirect_sim_params_bind_group, &[]);
        compute_pass.set_bind_group(1, indirect_metadata_bind_group, &[]);
        compute_pass.set_bind_group(2, indirect_spawner_bind_group, &[]);
        compute_pass.dispatch_workgroups(workgroup_count, 1, 1);
        trace!(
            "indirect dispatch compute dispatched: total_effect_count={} workgroup_count={}",
            total_effect_count,
            workgroup_count
        );
    }

    // Update prefix sum compute pass
    //
    // Compute the prefix sum of alive particles before the update pass, so that we
    // can dispatch batches of effects in the update pass. The alive count is
    // copied into the prefix sum buffer during the indirect dispatch pass just
    // before.
    {
        let mut compute_pass = HanabiComputePass::new(
            "hanabi:update_prefix_sum",
            &pipeline_cache,
            &mut render_context,
        );

        trace!("record commands for update prefix sum pipeline...");

        if compute_pass
            .set_cached_compute_pipeline(effects_meta.prefix_sum_pipeline_id)
            .is_err()
        {
            // FIXME - Bevy doesn't allow returning custom errors here...
            trace!("ERROR - Failed to set prefix sum compute pipeline. Simulation aborted.");
            return;
        }

        // Dispatch one thread per effect batch, to calculate the prefix sum of the
        // number of particles to update per effect in that batch.
        const WORKGROUP_SIZE: u32 = 64;
        let total_batch_count = batcher.len() as u32;
        let workgroup_count = total_batch_count.div_ceil(WORKGROUP_SIZE);

        // Setup vfx_prefix_sum pass
        compute_pass.set_bind_group(0, prefix_sum_bind_group, &[]);
        compute_pass.dispatch_workgroups(workgroup_count, 1, 1);
        trace!(
            "prefix sum dispatched: total_batch_count={} workgroup_count={}",
            total_batch_count,
            workgroup_count
        );
    }

    // Update compute pass
    //
    // Simulate all alive particles.
    let mut needs_sort = false;
    {
        let mut compute_pass =
            HanabiComputePass::new("hanabi:update", &pipeline_cache, &mut render_context);

        // Bind group simparams@0 is common to everything, only set once per update pass
        compute_pass.set_bind_group(
            0,
            effects_meta.update_sim_params_bind_group.as_ref().unwrap(),
            &[],
        );

        // Dispatch update compute jobs
        for effect_batch in batcher.iter() {
            // Remember if we need to sort any effect, for next pass
            if effect_batch.layout_flags.raea_sorted() { // raea: and blended effects (#52)
                needs_sort = true;
            }

            // Fetch bind group particle@1
            let Some(particle_bind_group) =
                effect_cache.particle_sim_bind_group(&effect_batch.slab_id)
            else {
                error!(
                    "Failed to find update particle@1 bind group for slab #{}",
                    effect_batch.slab_id.index()
                );
                compute_pass.insert_debug_marker("ERROR:MissingParticleSimBindGroup");
                continue;
            };

            // Fetch bind group metadata@3
            let Some(metadata_bind_group) = effect_bind_groups
                .update_metadata_bind_groups
                .get(&effect_batch.slab_id)
            else {
                error!(
                    "Failed to find update metadata@3 bind group for slab #{}",
                    effect_batch.slab_id.index()
                );
                compute_pass.insert_debug_marker("ERROR:MissingMetadataBindGroup");
                continue;
            };

            // Fetch compute pipeline
            if let Err(err) = compute_pass
                .set_cached_compute_pipeline(effect_batch.init_and_update_pipeline_ids.update)
            {
                compute_pass.insert_debug_marker(&format!(
                    "ERROR:FailedToSetCachedUpdatePipeline:{:?}",
                    err
                ));
                continue;
            }

            // Compute dynamic offsets
            let batch_info_aligned_size = batcher.batch_info_buffer_aligned_size();
            let batch_info_offset = effect_batch.batch_info_id * batch_info_aligned_size;
            trace!(
                    "record commands for update pipeline of effect {:?} spawner_base={} batch_info_id={} batch_info_offset={}B",
                    effect_batch.handle,
                    effect_batch.spawner_base,
                    effect_batch.batch_info_id,
                    batch_info_offset,
                );

            // Setup update pass
            compute_pass.set_bind_group(1, particle_bind_group, &[]);
            compute_pass.set_bind_group(
                2,
                property_bind_groups
                    .get(&effect_batch.property_key, true)
                    .unwrap(),
                &[batch_info_offset],
            );
            compute_pass.set_bind_group(3, &metadata_bind_group.bind_group, &[]);

            // Dispatch update job
            let dispatch_indirect_offset =
                effect_batch.batch_info_id * GpuDispatchIndirectArgs::SHADER_SIZE.get() as u32;
            trace!(
                "dispatch_workgroups_indirect: buffer={:?} offset={}B",
                indirect_buffer,
                dispatch_indirect_offset,
            );
            compute_pass
                .dispatch_workgroups_indirect(indirect_buffer, dispatch_indirect_offset as u64);

            trace!("update compute dispatched");
        }
    }

    // Do sorting for ribbons
    if needs_sort {
        // Copy alive counts for sort prefix sum compute pass
        //
        // After the update pass (atomically) updated the particle alive count per
        // effect instance, copy that number into the prefix sum buffer, so that
        // the prefix sum pass can calculate the prefix sum for sorting.
        let Some(queue_index) = batcher.sort_fill_prefix_sum_queue_index else {
            warn!("Missing alive count copy queue for ribbon sorting.");
            return;
        };
        gpu_buffer_operations.dispatch(
            queue_index,
            &mut render_context,
            &utils_pipeline,
            Some("hanabi:sort_fill_prefix_sum"),
        );

        // Sort prefix sum compute pass
        //
        // Calculate the prefix sum of the number of alive particles to sort.
        {
            let mut compute_pass = HanabiComputePass::new(
                "hanabi:sort_prefix_sum",
                &pipeline_cache,
                &mut render_context,
            );

            trace!("record commands for sort prefix sum pass...");

            if compute_pass
                .set_cached_compute_pipeline(effects_meta.prefix_sum_pipeline_id)
                .is_err()
            {
                // FIXME - Bevy doesn't allow returning custom errors here...
                trace!("ERROR - Failed to set prefix sum compute pipeline. Simulation aborted.");
                return;
            }

            // Dispatch one thread per init effect batch.
            const WORKGROUP_SIZE: u32 = 64;
            // Note: This only works because we use the same batches for the init and update
            // passes. A priori there's no reason why we couldn't split them, since likely
            // the batches would be different.
            let total_batch_count = batcher.len() as u32;
            let workgroup_count = total_batch_count.div_ceil(WORKGROUP_SIZE);

            // Setup vfx_prefix_sum pass
            compute_pass.set_bind_group(0, prefix_sum_bind_group, &[]);
            compute_pass.dispatch_workgroups(workgroup_count, 1, 1);
            trace!(
                "prefix sum dispatched: total_batch_count={} workgroup_count={}",
                total_batch_count,
                workgroup_count
            );
        }

        // Compute sort fill dispatch pass - Fill the indirect dispatch structs for any
        // batch of particles which needs sorting, based on the actual number of alive
        // particles in the batch after their update in the compute update pass. Since
        // particles may die during update, this may be different from the number of
        // particles updated.
        let Some(queue_index) = batcher.fill_dispatch_queue_index else {
            warn!("Missing indirect dispatch queue for ribbon sorting (fill).");
            return;
        };
        gpu_buffer_operations.dispatch(
            queue_index,
            &mut render_context,
            &utils_pipeline,
            Some("hanabi:sort_fill_dispatch"),
        );

        // Compute sort sort dispatch pass - Fill the indirect dispatch structs for any
        // batch of particles which needs sorting, based on the actual number of alive
        // particles in the batch after their update in the compute update pass. Since
        // particles may die during update, this may be different from the number of
        // particles updated.
        let Some(queue_index) = batcher.sort_dispatch_queue_index else {
            warn!("Missing indirect dispatch queue for ribbon sorting (sort).");
            return;
        };
        gpu_buffer_operations.dispatch(
            queue_index,
            &mut render_context,
            &utils_pipeline,
            Some("hanabi:sort_sort_dispatch"),
        );

        // Compute sort pass
        {
            let effect_metadata_buffer = effects_meta.effect_metadata_buffer.buffer().unwrap();
            let indirect_args_buffer = sort_bind_groups.indirect_args_buffer().unwrap();

            // Loop on batches and find those which need sorting
            for effect_batch in batcher.iter() {
                if !effect_batch.layout_flags.raea_sorted() { // raea: and blended effects (#52)
                    continue;
                }
                trace!("Processing RIBBONS effect batch for sorting...");
                // raea: a blended effect sorts by POSITION and needs neither (#52)
                if effect_batch.layout_flags.contains(LayoutFlags::RIBBONS) {
                assert!(effect_batch.particle_layout.contains(Attribute::RIBBON_ID));
                assert!(effect_batch.particle_layout.contains(Attribute::AGE)); // or is that optional?
                }

                let Some(effect_buffer) = effect_cache.get_slab(&effect_batch.slab_id) else {
                    warn!("Missing sort-fill effect buffer.");
                    continue;
                };

                // Loop on individual effects instances in the batch. Currently we can't sort an
                // entire batch at once, only a single instance. This is in part because each
                // instance already requires 2 keys to sort (RIBBON_ID + AGE), and it's not
                // clear that adding a third key is faster than doing multiple dispatches.
                for (effect_index, effect_data) in effect_batch.effect_data.iter().enumerate() {
                    let Some(fill_indirect_dispatch_index) =
                        effect_data.sort_fill_indirect_dispatch_index
                    else {
                        continue;
                    };
                    let Some(sort_indirect_dispatch_index) =
                        effect_data.sort_indirect_dispatch_index
                    else {
                        continue;
                    };
                    let fill_indirect_offset = sort_bind_groups
                        .get_indirect_args_byte_offset(fill_indirect_dispatch_index);
                    let sort_indirect_offset = sort_bind_groups
                        .get_indirect_args_byte_offset(sort_indirect_dispatch_index);

                    // Fill the sort buffer with the key-value pairs to sort
                    {
                        let mut compute_pass = HanabiComputePass::new(
                            "hanabi:sort_fill",
                            &pipeline_cache,
                            &mut render_context,
                        );
                        compute_pass.push_debug_group("hanabi:sort_fill");

                        let Some(pipeline_id) = sort_bind_groups
                            .get_sort_fill_pipeline_id(&effect_batch.particle_layout)
                        else {
                            warn!("Missing sort-fill pipeline.");
                            compute_pass.insert_debug_marker("ERROR:MissingSortFillPipeline");
                            continue;
                        };
                        if compute_pass
                            .set_cached_compute_pipeline(pipeline_id)
                            .is_err()
                        {
                            compute_pass.insert_debug_marker("ERROR:FailedToSetSortFillPipeline");
                            compute_pass.pop_debug_group();
                            return;
                        }

                        let spawner_aligned_size = effects_meta.spawner_buffer.aligned_size();
                        assert!(
                            spawner_aligned_size >= GpuSpawnerParams::min_size().get() as usize
                        );
                        let spawner_offset = (effect_batch.spawner_base + effect_index as u32)
                            * spawner_aligned_size as u32;

                        let particle_buffer = effect_buffer.particle_buffer();
                        let indirect_index_buffer = effect_buffer.indirect_index_buffer();
                        let Some(spawner_buffer) = effects_meta.spawner_buffer.buffer() else {
                            warn!("Missing spawner buffer for sort-fill.");
                            compute_pass.insert_debug_marker("ERROR:MissingSortFillSpawnerBuffer");
                            continue;
                        };
                        let Some(bind_group) = sort_bind_groups.sort_fill_bind_group(
                            particle_buffer.id(),
                            indirect_index_buffer.id(),
                            effect_metadata_buffer.id(),
                            spawner_buffer.id(),
                            effects_meta.sim_params_uniforms.buffer().unwrap().id(), // raea: (#52)
                        ) else {
                            warn!("Missing sort-fill bind group.");
                            compute_pass.insert_debug_marker("ERROR:MissingSortFillBindGroup");
                            continue;
                        };
                        compute_pass.set_bind_group(0, bind_group, &[spawner_offset]);

                        compute_pass.dispatch_workgroups_indirect(
                            indirect_args_buffer,
                            fill_indirect_offset,
                        );
                        compute_pass.pop_debug_group();
                    }

                    // Block-sort particles
                    {
                        let mut compute_pass = HanabiComputePass::new(
                            "hanabi:sort",
                            &pipeline_cache,
                            &mut render_context,
                        );
                        compute_pass.push_debug_group("hanabi:sort");

                        if compute_pass
                            .set_cached_compute_pipeline(sort_bind_groups.sort_pipeline_id())
                            .is_err()
                        {
                            compute_pass.insert_debug_marker("ERROR:FailedToSetSortPipeline");
                            compute_pass.pop_debug_group();
                            return;
                        }

                        let Some(bind_group) = sort_bind_groups.sort_bind_group() else {
                            warn!("Missing sort bind group.");
                            compute_pass.insert_debug_marker("ERROR:MissingSortBindGroup");
                            continue;
                        };
                        compute_pass.set_bind_group(0, bind_group, &[]);
                        compute_pass.dispatch_workgroups_indirect(
                            indirect_args_buffer,
                            sort_indirect_offset,
                        );

                        compute_pass.pop_debug_group();
                    }

                    // If the number of particles is larger than the block size (1024), we need to
                    // dispatch mergesort passes to merge pairs of lists until the full list is
                    // sorted. Since we don't know (from CPU) the number of particles, and so the
                    // number of required passes, we over-estimate from the total effect capacity.
                    let block_size = 1024; // see shader
                    let num_blocks = effect_batch.capacity.div_ceil(block_size);
                    let num_passes = if num_blocks > 1 {
                        32 - (num_blocks - 1).leading_zeros() // log2(n-1) rounded up
                    } else {
                        0
                    };
                    if num_passes > 0 {
                        let pipeline_id = sort_bind_groups.sort_merge_pipeline_id();
                        let Some(bind_group) = sort_bind_groups.sort_merge_bind_group() else {
                            warn!("Missing sort bind group.");
                            return;
                        };

                        let mut num_lists = num_blocks;
                        for ipass in 0..num_passes {
                            // A separate compute pass provides the storage-buffer visibility
                            // guarantee required before the next ping-pong merge stage reads
                            // this stage's output.
                            let mut compute_pass = HanabiComputePass::new(
                                "hanabi:sort_merge",
                                &pipeline_cache,
                                &mut render_context,
                            );
                            compute_pass.push_debug_group("hanabi:sort_merge");
                            if compute_pass
                                .set_cached_compute_pipeline(pipeline_id)
                                .is_err()
                            {
                                compute_pass
                                    .insert_debug_marker("ERROR:FailedToSetSortMergePipeline");
                                compute_pass.pop_debug_group();
                                return;
                            }

                            let offset = sort_bind_groups.merge_params_buffer().dynamic_offset(
                                SortBindGroups::COPY_SOURCE_PARAM_COUNT + ipass as usize,
                            );
                            compute_pass.set_bind_group(0, bind_group, &[offset]);

                            // For now, each thread serially merges a pair of lists. A final
                            // unpaired list is copied to the other ping-pong half.
                            let num_threads = num_lists.div_ceil(2);
                            let num_workgroups = num_threads.div_ceil(64);
                            compute_pass.dispatch_workgroups(num_workgroups, 1, 1);

                            compute_pass.pop_debug_group();
                            num_lists = num_lists.div_ceil(2);
                        }
                    }

                    // Copy the sorted indices back into the indirect index buffer.
                    {
                        let mut compute_pass = HanabiComputePass::new(
                            "hanabi:sort_copy",
                            &pipeline_cache,
                            &mut render_context,
                        );
                        compute_pass.push_debug_group("hanabi:copy_sorted_indices");

                        let pipeline_id = sort_bind_groups.sort_copy_pipeline_id();
                        if compute_pass
                            .set_cached_compute_pipeline(pipeline_id)
                            .is_err()
                        {
                            compute_pass.insert_debug_marker("ERROR:FailedToSetSortCopyPipeline");
                            compute_pass.pop_debug_group();
                            return;
                        }

                        let spawner_aligned_size = effects_meta.spawner_buffer.aligned_size();
                        assert!(
                            spawner_aligned_size >= GpuSpawnerParams::min_size().get() as usize
                        );
                        let spawner_offset = (effect_batch.spawner_base + effect_index as u32)
                            * spawner_aligned_size as u32;

                        let indirect_index_buffer = effect_buffer.indirect_index_buffer();
                        let Some(spawner_buffer) = effects_meta.spawner_buffer.buffer() else {
                            warn!("Missing spawner buffer for sort-copy.");
                            compute_pass.insert_debug_marker("ERROR:MissingSortCopySpawnerBuffer");
                            continue;
                        };
                        let Some(bind_group) = sort_bind_groups.sort_copy_bind_group(
                            indirect_index_buffer.id(),
                            effect_metadata_buffer.id(),
                            spawner_buffer.id(),
                        ) else {
                            warn!("Missing sort-copy bind group.");
                            compute_pass.insert_debug_marker("ERROR:MissingSortCopyBindGroup");
                            continue;
                        };
                        let copy_source_offset = sort_bind_groups
                            .merge_params_buffer()
                            .dynamic_offset(num_passes as usize % 2);
                        compute_pass.set_bind_group(
                            0,
                            bind_group,
                            &[spawner_offset, copy_source_offset],
                        );

                        compute_pass.dispatch_workgroups_indirect(
                            indirect_args_buffer,
                            fill_indirect_offset,
                        );

                        compute_pass.pop_debug_group();
                    }

                    // The sort-fill pass uses this counter as an atomic append index. It must
                    // remain valid through the block-sort, merge, and copy passes, then be
                    // cleared before sorting the next effect instance.
                    render_context.command_encoder().clear_buffer(
                        sort_bind_groups.sort_buffer(),
                        0,
                        Some(4),
                    );
                }
            }
        }
    }
}

impl From<LayoutFlags> for ParticleRenderAlphaMaskPipelineKey {
    fn from(layout_flags: LayoutFlags) -> Self {
        if layout_flags.contains(LayoutFlags::USE_ALPHA_MASK) {
            ParticleRenderAlphaMaskPipelineKey::AlphaMask
        } else if layout_flags.contains(LayoutFlags::OPAQUE) {
            ParticleRenderAlphaMaskPipelineKey::Opaque
        } else {
            ParticleRenderAlphaMaskPipelineKey::Blend
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn layout_flags() {
        let flags = LayoutFlags::default();
        assert_eq!(flags, LayoutFlags::NONE);
    }

    #[cfg(feature = "gpu_tests")]
    #[test]
    fn gpu_ops_ifda() {
        use crate::test_utils::MockRenderer;

        let renderer = MockRenderer::new();
        let device = renderer.device();
        let render_queue = renderer.queue();

        let mut world = World::new();
        world.insert_resource(device.clone());
        let mut buffer_ops = GpuBufferOperations::from_world(&mut world);

        let src_buffer = device.create_buffer(&BufferDescriptor {
            label: None,
            size: 256,
            usage: BufferUsages::STORAGE,
            mapped_at_creation: false,
        });
        let dst_buffer = device.create_buffer(&BufferDescriptor {
            label: None,
            size: 256,
            usage: BufferUsages::STORAGE,
            mapped_at_creation: false,
        });

        // Two consecutive ops can be merged. This includes having contiguous slices
        // both in source and destination.
        buffer_ops.begin_frame();
        {
            let mut q = InitFillDispatchQueue::default();
            q.enqueue(0, 0);
            assert_eq!(q.queue.len(), 1);
            q.enqueue(1, 1);
            // Ops are not batched yet
            assert_eq!(q.queue.len(), 2);
            // On submit, the ops get batched together
            q.submit(&src_buffer, &dst_buffer, &mut buffer_ops);
            assert_eq!(buffer_ops.args_buffer.len(), 1);
        }
        buffer_ops.end_frame(&device, &render_queue);

        // Even if out of order, the init fill dispatch ops are batchable. Here the
        // offsets are enqueued inverted.
        buffer_ops.begin_frame();
        {
            let mut q = InitFillDispatchQueue::default();
            q.enqueue(1, 1);
            assert_eq!(q.queue.len(), 1);
            q.enqueue(0, 0);
            // Ops are not batched yet
            assert_eq!(q.queue.len(), 2);
            // On submit, the ops get batched together
            q.submit(&src_buffer, &dst_buffer, &mut buffer_ops);
            assert_eq!(buffer_ops.args_buffer.len(), 1);
        }
        buffer_ops.end_frame(&device, &render_queue);

        // However, both the source and destination need to be contiguous at the same
        // time. Here they are mixed so we can't batch.
        buffer_ops.begin_frame();
        {
            let mut q = InitFillDispatchQueue::default();
            q.enqueue(0, 1);
            assert_eq!(q.queue.len(), 1);
            q.enqueue(1, 0);
            // Ops are not batched yet
            assert_eq!(q.queue.len(), 2);
            // On submit, the ops cannot get batched together
            q.submit(&src_buffer, &dst_buffer, &mut buffer_ops);
            assert_eq!(buffer_ops.args_buffer.len(), 2);
        }
        buffer_ops.end_frame(&device, &render_queue);
    }
}