Skip to content
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_hanabi"
version = "0.18.0"
version = "0.19.0-dev"
authors = ["Jerome Humbert <djeedai@gmail.com>"]
edition = "2021"
rust-version = "1.89.0"
Expand Down
8 changes: 3 additions & 5 deletions src/render/aligned_buffer_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,7 @@ impl<T: Pod + ShaderSize> AlignedBufferVec<T> {
// we wouldn't be calling the xxx_bindings() helpers, we'd have earlied out
// before.
let buffer = self.buffer()?;
Some(BindingResource::Buffer(BufferBinding {
buffer,
offset: 0,
size: None, // entire buffer
}))
Some(buffer.as_entire_binding())
}

/// Get a binding for a subset of the elements of the buffer.
Expand Down Expand Up @@ -201,13 +197,15 @@ impl<T: Pod + ShaderSize> AlignedBufferVec<T> {
///
/// [`aligned_size()`]: crate::AlignedBufferVec::aligned_size
#[inline]
#[must_use]
pub fn dynamic_offset(&self, index: usize) -> u32 {
let offset = self.aligned_size * index;
assert!(offset <= u32::MAX as usize);
u32::try_from(offset).expect("AlignedBufferVec index out of bounds")
}

#[inline]
#[must_use]
#[allow(dead_code)]
pub fn is_empty(&self) -> bool {
self.values.is_empty()
Expand Down
114 changes: 38 additions & 76 deletions src/render/effect_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ use bevy::{
ecs::{component::Component, resource::Resource},
log::{trace, warn},
platform::collections::HashMap,
render::{mesh::allocator::MeshBufferSlice, render_resource::*, renderer::RenderDevice},
render::{
mesh::allocator::MeshBufferSlice,
render_resource::{
binding_types::{storage_buffer_read_only, storage_buffer_read_only_sized},
*,
},
renderer::RenderDevice,
},
utils::default,
};
use bytemuck::cast_slice_mut;
Expand All @@ -19,7 +26,7 @@ use crate::{
asset::EffectAsset,
render::{
calc_hash, event::GpuChildInfo, GpuDrawIndexedIndirectArgs, GpuDrawIndirectArgs,
GpuEffectMetadata, GpuSpawnerParams, StorageType as _, INDIRECT_INDEX_SIZE,
GpuEffectMetadata, GpuIndirectIndex, GpuSpawnerParams, StorageType as _,
},
ParticleLayout,
};
Expand Down Expand Up @@ -318,52 +325,22 @@ impl ParticleSlab {
let spawner_params_size = GpuSpawnerParams::aligned_size(
render_device.limits().min_storage_buffer_offset_alignment,
);
let entries = [
// @group(1) @binding(0) var<storage, read> particle_buffer : ParticleBuffer;
BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::VERTEX_FRAGMENT,
ty: BindingType::Buffer {
ty: BufferBindingType::Storage { read_only: true },
has_dynamic_offset: false,
min_binding_size: Some(particle_layout.min_binding_size()),
},
count: None,
},
// @group(1) @binding(1) var<storage, read> indirect_buffer : IndirectBuffer;
BindGroupLayoutEntry {
binding: 1,
visibility: ShaderStages::VERTEX,
ty: BindingType::Buffer {
ty: BufferBindingType::Storage { read_only: true },
has_dynamic_offset: false,
min_binding_size: Some(NonZeroU64::new(INDIRECT_INDEX_SIZE as u64).unwrap()),
},
count: None,
},
// @group(1) @binding(2) var<storage, read> spawner : Spawner;
BindGroupLayoutEntry {
binding: 2,
visibility: ShaderStages::VERTEX,
ty: BindingType::Buffer {
ty: BufferBindingType::Storage { read_only: true },
has_dynamic_offset: true,
min_binding_size: Some(spawner_params_size),
},
count: None,
},
];
let label = format!(
"hanabi:bind_group_layout:render:particles@1:slab{}",
slab_id.0
);
trace!(
"Creating particles@1 layout '{}' for render pass with {} entries",
label,
entries.len(),
let label = format!("hanabi:bgl:render:particles@1:slab{}", slab_id.0);
let render_particles_buffer_layout = render_device.create_bind_group_layout(
&label[..],
&BindGroupLayoutEntries::sequential(
ShaderStages::VERTEX,
(
// @group(1) @binding(0) var<storage, read> particle_buffer : ParticleBuffer;
storage_buffer_read_only_sized(false, Some(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),
// @group(1) @binding(2) var<storage, read> spawner : Spawner;
storage_buffer_read_only_sized(true, Some(spawner_params_size)),
),
),
);
let render_particles_buffer_layout =
render_device.create_bind_group_layout(&label[..], &entries[..]);

Self {
particle_buffer,
Expand Down Expand Up @@ -445,35 +422,20 @@ impl ParticleSlab {
return;
}

let label = format!("hanabi:bind_group:sim:particle@1:vfx{}", slab_id.index());
let label = format!("hanabi:bg:sim:particle@1:vfx{}", slab_id.index());
let entries: &[BindGroupEntry] = if let Some(parent_binding) =
parent_binding_source.as_ref().map(|bbs| bbs.as_binding())
{
&[
BindGroupEntry {
binding: 0,
resource: self.as_entire_binding_particle(),
},
BindGroupEntry {
binding: 1,
resource: self.as_entire_binding_indirect(),
},
BindGroupEntry {
binding: 2,
resource: parent_binding,
},
]
&BindGroupEntries::sequential((
self.as_entire_binding_particle(),
self.as_entire_binding_indirect(),
parent_binding,
))
} else {
&[
BindGroupEntry {
binding: 0,
resource: self.as_entire_binding_particle(),
},
BindGroupEntry {
binding: 1,
resource: self.as_entire_binding_indirect(),
},
]
&BindGroupEntries::sequential((
self.as_entire_binding_particle(),
self.as_entire_binding_indirect(),
))
};

trace!(
Expand Down Expand Up @@ -1143,7 +1105,7 @@ fn create_particle_sim_bind_group_layout_desc(
ty: BindingType::Buffer {
ty: BufferBindingType::Storage { read_only: false },
has_dynamic_offset: false,
min_binding_size: Some(NonZeroU64::new(INDIRECT_INDEX_SIZE as _).unwrap()),
min_binding_size: Some(GpuIndirectIndex::SHADER_SIZE),
},
count: None,
});
Expand All @@ -1164,7 +1126,7 @@ fn create_particle_sim_bind_group_layout_desc(
}

let hash = calc_hash(&entries);
let label = format!("hanabi:bind_group_layout:sim:particles_{:016X}", hash);
let label = format!("hanabi:bgl:sim:particles_{:016X}", hash);
trace!(
"Creating particle bind group layout '{}' for init pass with {} entries. (parent_buffer:{})",
label,
Expand Down Expand Up @@ -1227,7 +1189,7 @@ fn create_metadata_init_bind_group_layout_desc(

let hash = calc_hash(&entries);
let label = format!(
"hanabi:bind_group_layout:init:metadata@3_{}{:016X}",
"hanabi:bgl:init:metadata@3_{}{:016X}",
if consume_gpu_spawn_events {
"events"
} else {
Expand Down Expand Up @@ -1301,7 +1263,7 @@ fn create_metadata_update_bind_group_layout_desc(
}

let hash = calc_hash(&entries);
let label = format!("hanabi:bind_group_layout:update:metadata_{:016X}", hash);
let label = format!("hanabi:bgl:update:metadata_{:016X}", hash);
trace!(
"Creating particle bind group layout '{}' for init update with {} entries. (num_event_buffers:{})",
label,
Expand Down
37 changes: 12 additions & 25 deletions src/render/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use bevy::{
log::{error, trace},
prelude::{Component, Entity, ResMut, Resource},
render::{
render_resource::{BindGroup, BindGroupLayout, Buffer, ShaderSize as _, ShaderType},
render_resource::{
binding_types::storage_buffer, BindGroup, BindGroupEntries, BindGroupLayout,
BindGroupLayoutEntries, Buffer, ShaderSize as _, ShaderType,
},
renderer::{RenderDevice, RenderQueue},
},
};
Expand All @@ -20,10 +23,7 @@ use thiserror::Error;
use wgpu::util::BufferInitDescriptor;
#[cfg(not(debug_assertions))]
use wgpu::BufferDescriptor;
use wgpu::{
BindGroupEntry, BindGroupLayoutEntry, BindingResource, BindingType, BufferBinding,
BufferBindingType, BufferUsages, CommandEncoder, ShaderStages,
};
use wgpu::{BufferUsages, CommandEncoder, ShaderStages};

use super::{
aligned_buffer_vec::HybridAlignedBufferVec, effect_cache::SlabState, gpu_buffer::GpuBuffer,
Expand Down Expand Up @@ -345,17 +345,11 @@ impl EventCache {
);

let child_infos_bind_group_layout = device.create_bind_group_layout(
"hanabi:bind_group_layout:indirect:child_infos@3",
&[BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::COMPUTE,
ty: BindingType::Buffer {
ty: BufferBindingType::Storage { read_only: false },
has_dynamic_offset: false,
min_binding_size: Some(GpuChildInfo::min_size()),
},
count: None,
}],
"hanabi:bgl:indirect:child_infos@3",
&BindGroupLayoutEntries::single(
ShaderStages::COMPUTE,
storage_buffer::<GpuChildInfo>(false),
),
);

Self {
Expand Down Expand Up @@ -641,16 +635,9 @@ impl EventCache {
let buffer = self.child_infos_buffer()?;
// TODO - stop re-creating each frame...
self.indirect_child_info_buffer_bind_group = Some(device.create_bind_group(
"hanabi:bind_group:indirect:child_infos@3",
"hanabi:bg:indirect:child_infos@3",
&self.indirect_child_info_buffer_bind_group_layout,
&[BindGroupEntry {
binding: 0,
resource: BindingResource::Buffer(BufferBinding {
buffer,
offset: 0,
size: None,
}),
}],
&BindGroupEntries::single(buffer.as_entire_binding()),
));
self.indirect_child_info_buffer_bind_group.as_ref()
}
Expand Down
Loading
Loading