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

djeedai / bevy_hanabi / 11543837292

27 Oct 2024 09:10PM UTC coverage: 57.849% (-1.2%) from 59.035%
11543837292

Pull #387

github

web-flow
Merge a72c10537 into 75f07d778
Pull Request #387: Unify the clone modifier and spawners, and fix races.

114 of 621 new or added lines in 7 files covered. (18.36%)

23 existing lines in 5 files now uncovered.

3534 of 6109 relevant lines covered (57.85%)

23.02 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::ops::{Index, Range};
2

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

10
use super::{
11
    effect_cache::{DispatchBufferIndices, EffectSlices},
12
    EffectCacheId, GpuCompressedTransform, LayoutFlags,
13
};
14
use crate::{
15
    spawn::EffectInitializer, AlphaMode, EffectAsset, EffectShader, ParticleLayout, PropertyLayout,
16
    TextureLayout,
17
};
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
    /// The initializer (spawner or cloner) for each particle group.
31
    pub initializers: Vec<EffectInitializer>,
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
    /// Texture layout.
46
    pub texture_layout: TextureLayout,
47
    /// Textures.
48
    pub textures: Vec<Handle<Image>>,
49
    /// Alpha mode.
50
    pub alpha_mode: AlphaMode,
51
    /// Entities holding the source [`ParticleEffect`] instances which were
52
    /// batched into this single batch. Used to determine visibility per view.
53
    ///
54
    /// [`ParticleEffect`]: crate::ParticleEffect
55
    pub entities: Vec<u32>,
56
    /// Configured shaders used for the particle rendering of this batch.
57
    /// Note that we don't need to keep the init/update shaders alive because
58
    /// their pipeline specialization is doing it via the specialization key.
59
    pub render_shaders: Vec<Handle<Shader>>,
60
    /// Init and update compute pipelines specialized for this batch.
61
    pub init_and_update_pipeline_ids: Vec<InitAndUpdatePipelineIds>,
62
    /// The order in which we evaluate groups.
63
    pub group_order: Vec<u32>,
64
}
65

66
impl Index<u32> for EffectBatches {
67
    type Output = EffectBatch;
68

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

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

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

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

146
/// Effect batching input, obtained from extracted effects.
147
#[derive(Debug, Clone)]
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 shaders.
159
    pub effect_shaders: Vec<EffectShader>,
160
    /// Various flags related to the effect.
161
    pub layout_flags: LayoutFlags,
162
    /// Texture layout.
163
    pub texture_layout: TextureLayout,
164
    /// Textures.
165
    pub textures: Vec<Handle<Image>>,
166
    /// Alpha mode.
167
    pub alpha_mode: AlphaMode,
168
    pub particle_layout: ParticleLayout,
169
    pub initializers: Vec<EffectInitializer>,
170
    /// The order in which we evaluate groups.
171
    pub group_order: Vec<u32>,
172
    /// Emitter transform.
173
    pub transform: GpuCompressedTransform,
174
    /// Emitter inverse transform.
175
    pub inverse_transform: GpuCompressedTransform,
176
    /// GPU buffer where properties for this batch need to be written.
177
    pub property_buffer: Option<Buffer>,
178
    /// Serialized property data.
179
    // FIXME - Contains a single effect's data; should handle multiple ones.
180
    pub property_data: Option<Vec<u8>>,
181
    /// Sort key, for 2D only.
182
    #[cfg(feature = "2d")]
183
    pub z_sort_key_2d: FloatOrd,
184
}
185

186
#[derive(Debug)]
187
pub(crate) struct InitAndUpdatePipelineIds {
188
    pub(crate) init: CachedComputePipelineId,
189
    pub(crate) update: CachedComputePipelineId,
190
}
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

© 2025 Coveralls, Inc