Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retain mesh and property into separate components #410

Merged
merged 2 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ use crate::{
compile_effects, gather_removed_effects,
properties::EffectProperties,
render::{
add_remove_effects, extract_effect_events, extract_effects, prepare_bind_groups,
prepare_effects, prepare_gpu_resources, queue_effects, DispatchIndirectPipeline,
DrawEffects, EffectAssetEvents, EffectBindGroups, EffectCache, EffectsMeta,
ExtractedEffects, GpuDispatchIndirect, GpuParticleGroup, GpuRenderEffectMetadata,
GpuRenderGroupIndirect, GpuSpawnerParams, ParticlesInitPipeline, ParticlesRenderPipeline,
ParticlesUpdatePipeline, ShaderCache, SimParams, StorageType as _, VfxSimulateDriverNode,
VfxSimulateNode,
add_remove_effects, batch_effects, extract_effect_events, extract_effects,
prepare_bind_groups, prepare_effects, prepare_gpu_resources, queue_effects,
DispatchIndirectPipeline, DrawEffects, EffectAssetEvents, EffectBindGroups, EffectCache,
EffectsMeta, ExtractedEffects, GpuDispatchIndirect, GpuParticleGroup,
GpuRenderEffectMetadata, GpuRenderGroupIndirect, GpuSpawnerParams, ParticlesInitPipeline,
ParticlesRenderPipeline, ParticlesUpdatePipeline, ShaderCache, SimParams, StorageType as _,
VfxSimulateDriverNode, VfxSimulateNode,
},
spawn::{self, Random},
tick_initializers,
Expand Down Expand Up @@ -312,14 +312,14 @@ impl Plugin for HanabiPlugin {
.add_systems(
Render,
(
(add_remove_effects, prepare_effects)
(add_remove_effects, prepare_effects, batch_effects)
.chain()
.in_set(EffectSystems::PrepareEffectAssets)
// Ensure we run after Bevy prepared the render Mesh
.after(allocate_and_free_meshes),
queue_effects
.in_set(EffectSystems::QueueEffects)
.after(prepare_effects),
.after(batch_effects),
prepare_gpu_resources
.in_set(EffectSystems::PrepareEffectGpuResources)
.after(prepare_view_uniforms),
Expand Down
65 changes: 31 additions & 34 deletions src/render/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ use bevy::{

use super::{
effect_cache::{DispatchBufferIndices, EffectSlices},
GpuCompressedTransform, LayoutFlags,
CachedMesh, LayoutFlags,
};
use crate::{
spawn::EffectInitializer, AlphaMode, EffectAsset, EffectShader, ParticleLayout, PropertyLayout,
TextureLayout,
spawn::EffectInitializer, AlphaMode, EffectAsset, EffectShader, ParticleLayout, TextureLayout,
};

/// Data needed to render all batches pertaining to a specific effect.
Expand All @@ -46,8 +45,8 @@ pub(crate) struct EffectBatches {
pub particle_layout: ParticleLayout,
/// Flags describing the render layout.
pub layout_flags: LayoutFlags,
/// Asset handle of the effect mesh to draw.
pub mesh: Handle<Mesh>,
/// Asset ID of the effect mesh to draw.
pub mesh: AssetId<Mesh>,
/// GPU buffer storing the [`mesh`] of the effect.
pub mesh_buffer: Buffer,
/// Slice inside the GPU buffer for the effect mesh.
Expand Down Expand Up @@ -113,7 +112,8 @@ pub(crate) struct EffectBatch {
impl EffectBatches {
/// Create a new batch from a single input.
pub fn from_input(
input: BatchesInput,
cached_mesh: &CachedMesh,
input: &mut BatchesInput,
spawner_base: u32,
init_and_update_pipeline_ids: Vec<InitAndUpdatePipelineIds>,
dispatch_buffer_indices: DispatchBufferIndices,
Expand All @@ -123,7 +123,7 @@ impl EffectBatches {
buffer_index: input.effect_slices.buffer_index,
spawner_base,
initializers: input.initializers.clone(),
particle_layout: input.effect_slices.particle_layout,
particle_layout: input.effect_slices.particle_layout.clone(),
dispatch_buffer_indices,
first_particle_group_buffer_index,
group_batches: input
Expand All @@ -134,13 +134,13 @@ impl EffectBatches {
slice: range[0]..range[1],
})
.collect(),
handle: input.handle,
handle: input.handle.clone(),
layout_flags: input.layout_flags,
mesh: input.mesh.clone(),
mesh_buffer: input.mesh_buffer,
mesh_slice: input.mesh_slice,
texture_layout: input.texture_layout,
textures: input.textures,
mesh: cached_mesh.mesh,
mesh_buffer: cached_mesh.buffer.clone(),
mesh_slice: cached_mesh.range.clone(),
texture_layout: input.texture_layout.clone(),
textures: input.textures.clone(),
alpha_mode: input.alpha_mode,
render_shaders: input
.effect_shaders
Expand All @@ -149,61 +149,58 @@ impl EffectBatches {
.collect(),
init_and_update_pipeline_ids,
entities: vec![input.main_entity.id().index()],
group_order: input.group_order,
group_order: input.group_order.clone(),
}
}
}

/// Effect batching input, obtained from extracted effects.
#[derive(Debug)]
#[derive(Debug, Component)]
pub(crate) struct BatchesInput {
/// Handle of the underlying effect asset describing the effect.
pub handle: Handle<EffectAsset>,
/// Main entity of the [`ParticleEffect`], used for visibility.
pub main_entity: MainEntity,
/// Render entity of the [`CachedEffect`]. FIXME - doesn't work with
/// batching!
#[allow(dead_code)]
pub entity: Entity,
/// Effect slices.
pub effect_slices: EffectSlices,
/// Layout of the effect properties.
pub property_layout: PropertyLayout,
/// Effect shaders.
pub effect_shaders: Vec<EffectShader>,
/// Various flags related to the effect.
pub layout_flags: LayoutFlags,
/// Asset handle of the effect mesh to draw.
pub mesh: Handle<Mesh>,
/// GPU buffer storing the [`mesh`] of the effect.
pub mesh_buffer: Buffer,
/// Slice inside the GPU buffer for the effect mesh.
pub mesh_slice: Range<u32>,
/// Texture layout.
pub texture_layout: TextureLayout,
/// Textures.
pub textures: Vec<Handle<Image>>,
/// Alpha mode.
pub alpha_mode: AlphaMode,
#[allow(dead_code)]
pub particle_layout: ParticleLayout,
pub initializers: Vec<EffectInitializer>,
/// The order in which we evaluate groups.
pub group_order: Vec<u32>,
/// Emitter transform.
pub transform: GpuCompressedTransform,
/// Emitter inverse transform.
pub inverse_transform: GpuCompressedTransform,
/// GPU buffer where properties for this batch need to be written.
pub property_buffer: Option<Buffer>,
/// Serialized property data.
// FIXME - Contains a single effect's data; should handle multiple ones.
pub property_data: Option<Vec<u8>>,
/// Emitter position.
#[cfg(feature = "3d")]
pub position: Vec3,
/// Sort key, for 2D only.
#[cfg(feature = "2d")]
pub z_sort_key_2d: FloatOrd,
}

#[derive(Debug)]
pub(crate) struct InitAndUpdatePipelineIds {
pub(crate) init: CachedComputePipelineId,
pub(crate) update: CachedComputePipelineId,
pub init: CachedComputePipelineId,
pub update: CachedComputePipelineId,
}

#[derive(Debug, Component)]
pub(crate) struct CachedGroups {
pub spawner_base: u32,
pub first_particle_group_buffer_index: Option<u32>,
// Note: Stolen each frame, so invalid if not re-extracted each frame. This is how we tell if
// an effect is active for the current frame.
pub init_and_update_pipeline_ids: Vec<InitAndUpdatePipelineIds>,
}
Loading
Loading