• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

djeedai / bevy_hanabi / 11474564617

23 Oct 2024 06:47AM UTC coverage: 57.582% (-1.5%) from 59.035%
11474564617

Pull #377

github

web-flow
Merge 5f7b37c8d into 75f07d778
Pull Request #377: Allow particles to be arbitrary meshes.

33 of 190 new or added lines in 8 files covered. (17.37%)

31 existing lines in 2 files now uncovered.

3482 of 6047 relevant lines covered (57.58%)

23.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/render/batch.rs
1
use std::{
2
    fmt::Debug,
3
    ops::{Index, Range},
4
};
5

6
#[cfg(feature = "2d")]
7
use bevy::math::FloatOrd;
8
use bevy::{
9
    prelude::*,
10
    render::render_resource::{Buffer, CachedComputePipelineId},
11
};
12

13
use super::{
14
    effect_cache::{DispatchBufferIndices, EffectSlices},
15
    EffectCacheId, GpuCompressedTransform, LayoutFlags,
16
};
17
use crate::{AlphaMode, EffectAsset, EffectShader, ParticleLayout, PropertyLayout, TextureLayout};
18

19
/// Data needed to render all batches pertaining to a specific effect.
20
#[derive(Debug, Component)]
21
pub(crate) struct EffectBatches {
22
    /// Handle of the underlying effect asset describing the effect.
23
    pub handle: Handle<EffectAsset>,
24
    /// One batch per particle group.
25
    pub group_batches: Vec<EffectBatch>,
26
    /// Index of the buffer.
27
    pub buffer_index: u32,
28
    /// Index of the first Spawner of the effects in the batch.
29
    pub spawner_base: u32,
30
    /// Number of particles to spawn/init this frame.
31
    pub spawn_count: u32,
32
    /// The effect cache ID.
33
    pub effect_cache_id: EffectCacheId,
34
    /// The indices within the various indirect dispatch buffers.
35
    pub dispatch_buffer_indices: DispatchBufferIndices,
36
    /// The index of the first [`GpuParticleGroup`] structure in the global
37
    /// [`EffectsMeta::particle_group_buffer`] buffer. The buffer is currently
38
    /// re-created each frame, so the rows for multiple groups of an effect are
39
    /// guaranteed to be contiguous.
40
    pub first_particle_group_buffer_index: u32,
41
    /// Particle layout.
42
    pub particle_layout: ParticleLayout,
43
    /// Flags describing the render layout.
44
    pub layout_flags: LayoutFlags,
45
    /// The mesh to draw.
46
    pub mesh: Handle<Mesh>,
47
    /// Texture layout.
48
    pub texture_layout: TextureLayout,
49
    /// Textures.
50
    pub textures: Vec<Handle<Image>>,
51
    /// Alpha mode.
52
    pub alpha_mode: AlphaMode,
53
    /// Entities holding the source [`ParticleEffect`] instances which were
54
    /// batched into this single batch. Used to determine visibility per view.
55
    ///
56
    /// [`ParticleEffect`]: crate::ParticleEffect
57
    pub entities: Vec<u32>,
58
    /// Configured shaders used for the particle rendering of this batch.
59
    /// Note that we don't need to keep the init/update shaders alive because
60
    /// their pipeline specialization is doing it via the specialization key.
61
    pub render_shaders: Vec<Handle<Shader>>,
62
    /// Init compute pipeline specialized for this batch.
63
    pub init_pipeline_id: CachedComputePipelineId,
64
    /// Update compute pipeline specialized for this batch.
65
    pub update_pipeline_ids: Vec<CachedComputePipelineId>,
66
}
67

68
impl Index<u32> for EffectBatches {
69
    type Output = EffectBatch;
70

71
    fn index(&self, index: u32) -> &Self::Output {
×
72
        &self.group_batches[index as usize]
×
73
    }
74
}
75

76
/// Single effect batch to drive rendering.
77
///
78
/// This component is spawned into the render world during the prepare phase
79
/// ([`prepare_effects()`]), once per effect batch per group. In turns it
80
/// references an [`EffectBatches`] component containing all the shared data for
81
/// all the groups of the effect.
82
#[derive(Debug, Component)]
83
pub(crate) struct EffectDrawBatch {
84
    /// Group index of the batch.
85
    pub group_index: u32,
86
    /// Entity holding the [`EffectBatches`] this batch is part of.
87
    pub batches_entity: Entity,
88
    /// For 2D rendering, the Z coordinate used as the sort key. Ignored for 3D
89
    /// rendering.
90
    #[cfg(feature = "2d")]
91
    pub z_sort_key_2d: FloatOrd,
92
    /// For 3d rendering, the position of the emitter so we can compute distance
93
    /// to camera. Ignored for 2D rendering.
94
    #[cfg(feature = "3d")]
95
    pub translation_3d: Vec3,
96
}
97

98
/// Batch data specific to a single particle group.
99
#[derive(Debug)]
100
pub(crate) struct EffectBatch {
101
    /// Slice of particles in the GPU effect buffer for the entire batch.
102
    pub slice: Range<u32>,
103
}
104

105
impl EffectBatches {
106
    /// Create a new batch from a single input.
107
    pub fn from_input(
×
108
        input: BatchesInput,
109
        spawner_base: u32,
110
        effect_cache_id: EffectCacheId,
111
        init_pipeline_id: CachedComputePipelineId,
112
        update_pipeline_ids: Vec<CachedComputePipelineId>,
113
        dispatch_buffer_indices: DispatchBufferIndices,
114
        first_particle_group_buffer_index: u32,
115
    ) -> EffectBatches {
116
        EffectBatches {
117
            buffer_index: input.effect_slices.buffer_index,
×
118
            spawner_base,
119
            spawn_count: input.spawn_count,
×
120
            particle_layout: input.effect_slices.particle_layout,
×
121
            effect_cache_id,
122
            dispatch_buffer_indices,
123
            first_particle_group_buffer_index,
124
            group_batches: input
×
125
                .effect_slices
126
                .slices
127
                .windows(2)
128
                .map(|range| EffectBatch {
129
                    slice: range[0]..range[1],
130
                })
131
                .collect(),
132
            handle: input.handle,
×
133
            layout_flags: input.layout_flags,
×
NEW
134
            mesh: input.mesh.clone(),
×
135
            texture_layout: input.texture_layout,
×
136
            textures: input.textures,
×
137
            alpha_mode: input.alpha_mode,
×
138
            render_shaders: input.effect_shader.render,
×
139
            init_pipeline_id,
140
            update_pipeline_ids,
141
            entities: vec![input.entity.index()],
×
142
        }
143
    }
144
}
145

146
/// Effect batching input, obtained from extracted effects.
147
#[derive(Debug)]
148
pub(crate) struct BatchesInput {
149
    /// Handle of the underlying effect asset describing the effect.
150
    pub handle: Handle<EffectAsset>,
151
    /// Entity index excluding generation ([`Entity::index()`]). This is
152
    /// transient for a single frame, so the generation is useless.
153
    pub entity: Entity,
154
    /// Effect slices.
155
    pub effect_slices: EffectSlices,
156
    /// Layout of the effect properties.
157
    pub property_layout: PropertyLayout,
158
    /// Effect shader.
159
    pub effect_shader: EffectShader,
160
    /// Various flags related to the effect.
161
    pub layout_flags: LayoutFlags,
162
    pub mesh: Handle<Mesh>,
163
    /// Texture layout.
164
    pub texture_layout: TextureLayout,
165
    /// Textures.
166
    pub textures: Vec<Handle<Image>>,
167
    /// Alpha mode.
168
    pub alpha_mode: AlphaMode,
169
    /// Number of particles to spawn for this effect.
170
    pub spawn_count: u32,
171
    /// Emitter transform.
172
    pub transform: GpuCompressedTransform,
173
    /// Emitter inverse transform.
174
    pub inverse_transform: GpuCompressedTransform,
175
    /// GPU buffer where properties for this batch need to be written.
176
    pub property_buffer: Option<Buffer>,
177
    /// Serialized property data.
178
    // FIXME - Contains a single effect's data; should handle multiple ones.
179
    pub property_data: Option<Vec<u8>>,
180
    /// Sort key, for 2D only.
181
    #[cfg(feature = "2d")]
182
    pub z_sort_key_2d: FloatOrd,
183
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc