Skip to content

Commit

Permalink
Remove the need for the function
Browse files Browse the repository at this point in the history
  • Loading branch information
james7132 committed Mar 3, 2024
1 parent 2162a77 commit 550eb5b
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 168 deletions.
5 changes: 5 additions & 0 deletions crates/bevy_color/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ bevy_reflect = { path = "../bevy_reflect", version = "0.14.0-dev", features = [
bytemuck = "1"
serde = "1.0"
thiserror = "1.0"
<<<<<<< Updated upstream
wgpu = { version = "0.19.3", default-features = false }
encase = { version = "0.7", default-features = false }
=======
wgpu = { version = "0.19.1", default-features = false }
encase = { git = "https://github.com/james7132/encase", branch = "direct-copy-specialization", default_features = false }
>>>>>>> Stashed changes

[lints]
workspace = true
1 change: 1 addition & 0 deletions crates/bevy_color/src/linear_rgba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ impl encase::ShaderType for LinearRgba {
alignment,
has_uniform_min_alignment: false,
min_size: size,
has_internal_padding: false,
extra: (),
}
};
Expand Down
310 changes: 144 additions & 166 deletions crates/bevy_ecs/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
prelude::World,
query::DebugCheckedUnwrap,
storage::{SparseSetIndex, SparseSets, Storages, Table, TableRow},
world::{unsafe_world_cell::UnsafeWorldCell, DeferredWorld},
world::{unsafe_world_cell::UnsafeWorldCell},
};
use bevy_ptr::{ConstNonNull, OwningPtr};
use bevy_utils::all_tuples;
Expand Down Expand Up @@ -637,205 +637,183 @@ impl<'w> BundleInserter<'w> {
location: EntityLocation,
bundle: T,
) -> EntityLocation {
#[inline]
fn trigger_hooks(
entity: Entity,
bundle_info: &BundleInfo,
add_bundle: &AddBundle,
archetype: &Archetype,
mut world: DeferredWorld,
) {
if archetype.has_on_add() {
// SAFETY: All components in the bundle are guarenteed to exist in the World
// as they must be initialized before creating the BundleInfo.
unsafe {
world.trigger_on_add(
entity,
bundle_info
.iter_components()
.zip(add_bundle.bundle_status.iter())
.filter(|(_, &status)| status == ComponentStatus::Added)
.map(|(id, _)| id),
);
}
}
if archetype.has_on_insert() {
// SAFETY: All components in the bundle are guarenteed to exist in the World
// as they must be initialized before creating the BundleInfo.
unsafe { world.trigger_on_insert(entity, bundle_info.iter_components()) }
}
}

let bundle_info = self.bundle_info.as_ref();
let add_bundle = self.add_bundle.as_ref();
let table = self.table.as_mut();
let archetype = self.archetype.as_mut();

match &mut self.result {
let new_location = match &mut self.result {
InsertBundleResult::SameArchetype => {
{
// SAFETY: Mutable references do not alias and will be dropped after this block
let sparse_sets = {
let world = self.world.world_mut();
&mut world.storages.sparse_sets
};
// SAFETY: Mutable references do not alias and will be dropped after this block
let sparse_sets = {
let world = self.world.world_mut();
&mut world.storages.sparse_sets
};

bundle_info.write_components(
table,
sparse_sets,
add_bundle,
entity,
location.table_row,
self.change_tick,
bundle,
);

bundle_info.write_components(
table,
sparse_sets,
add_bundle,
entity,
location.table_row,
self.change_tick,
bundle,
);
}
// SAFETY: We have no outstanding mutable references to world as they were dropped
let deferred_world = unsafe { self.world.into_deferred() };
trigger_hooks(entity, bundle_info, add_bundle, archetype, deferred_world);
location
}
InsertBundleResult::NewArchetypeSameTable { new_archetype } => {
let new_archetype = new_archetype.as_mut();

let new_location = {
// SAFETY: Mutable references do not alias and will be dropped after this block
let (sparse_sets, entities) = {
let world = self.world.world_mut();
(&mut world.storages.sparse_sets, &mut world.entities)
};

let result = archetype.swap_remove(location.archetype_row);
if let Some(swapped_entity) = result.swapped_entity {
let swapped_location =
// SAFETY: If the swap was successful, swapped_entity must be valid.
unsafe { entities.get(swapped_entity).debug_checked_unwrap() };
entities.set(
swapped_entity.index(),
EntityLocation {
archetype_id: swapped_location.archetype_id,
archetype_row: location.archetype_row,
table_id: swapped_location.table_id,
table_row: swapped_location.table_row,
},
);
}
let new_location = new_archetype.allocate(entity, result.table_row);
entities.set(entity.index(), new_location);
bundle_info.write_components(
table,
sparse_sets,
add_bundle,
entity,
result.table_row,
self.change_tick,
bundle,
);
new_location
// SAFETY: Mutable references do not alias and will be dropped after this block
let (sparse_sets, entities) = {
let world = self.world.world_mut();
(&mut world.storages.sparse_sets, &mut world.entities)
};

// SAFETY: We have no outstanding mutable references to world as they were dropped
let deferred_world = unsafe { self.world.into_deferred() };
trigger_hooks(entity, bundle_info, add_bundle, archetype, deferred_world);
let result = archetype.swap_remove(location.archetype_row);
if let Some(swapped_entity) = result.swapped_entity {
let swapped_location =
// SAFETY: If the swap was successful, swapped_entity must be valid.
unsafe { entities.get(swapped_entity).debug_checked_unwrap() };
entities.set(
swapped_entity.index(),
EntityLocation {
archetype_id: swapped_location.archetype_id,
archetype_row: location.archetype_row,
table_id: swapped_location.table_id,
table_row: swapped_location.table_row,
},
);
}

let new_location = new_archetype.allocate(entity, result.table_row);
entities.set(entity.index(), new_location);
bundle_info.write_components(
table,
sparse_sets,
add_bundle,
entity,
result.table_row,
self.change_tick,
bundle,
);
new_location
}
InsertBundleResult::NewArchetypeNewTable {
new_archetype,
new_table,
} => {
let new_table = new_table.as_mut();
let new_archetype = new_archetype.as_mut();

let new_location = {
// SAFETY: Mutable references do not alias and will be dropped after this block
let (archetypes_ptr, sparse_sets, entities) = {
let world = self.world.world_mut();
let archetype_ptr: *mut Archetype =
world.archetypes.archetypes.as_mut_ptr();
(
archetype_ptr,
&mut world.storages.sparse_sets,
&mut world.entities,
)
};
let new_archetype = new_archetype.as_mut();
let result = archetype.swap_remove(location.archetype_row);
if let Some(swapped_entity) = result.swapped_entity {
let swapped_location =
// SAFETY: If the swap was successful, swapped_entity must be valid.
unsafe { entities.get(swapped_entity).debug_checked_unwrap() };
entities.set(
swapped_entity.index(),
EntityLocation {
archetype_id: swapped_location.archetype_id,
archetype_row: location.archetype_row,
table_id: swapped_location.table_id,
table_row: swapped_location.table_row,
},
// SAFETY: Mutable references do not alias and will be dropped after this block
let (archetypes_ptr, sparse_sets, entities) = {
let world = self.world.world_mut();
let archetype_ptr: *mut Archetype =
world.archetypes.archetypes.as_mut_ptr();
(
archetype_ptr,
&mut world.storages.sparse_sets,
&mut world.entities,
)
};

let result = archetype.swap_remove(location.archetype_row);
if let Some(swapped_entity) = result.swapped_entity {
let swapped_location =
// SAFETY: If the swap was successful, swapped_entity must be valid.
unsafe { entities.get(swapped_entity).debug_checked_unwrap() };
entities.set(
swapped_entity.index(),
EntityLocation {
archetype_id: swapped_location.archetype_id,
archetype_row: location.archetype_row,
table_id: swapped_location.table_id,
table_row: swapped_location.table_row,
},
);
}
// PERF: store "non bundle" components in edge, then just move those to avoid
// redundant copies
let move_result = table.move_to_superset_unchecked(result.table_row, new_table);
let new_location = new_archetype.allocate(entity, move_result.new_row);
entities.set(entity.index(), new_location);

// if an entity was moved into this entity's table spot, update its table row
if let Some(swapped_entity) = move_result.swapped_entity {
let swapped_location =
// SAFETY: If the swap was successful, swapped_entity must be valid.
unsafe { entities.get(swapped_entity).debug_checked_unwrap() };

entities.set(
swapped_entity.index(),
EntityLocation {
archetype_id: swapped_location.archetype_id,
archetype_row: swapped_location.archetype_row,
table_id: swapped_location.table_id,
table_row: result.table_row,
},
);

if archetype.id() == swapped_location.archetype_id {
archetype.set_entity_table_row(
swapped_location.archetype_row,
result.table_row,
);
}
// PERF: store "non bundle" components in edge, then just move those to avoid
// redundant copies
let move_result = table.move_to_superset_unchecked(result.table_row, new_table);
let new_location = new_archetype.allocate(entity, move_result.new_row);
entities.set(entity.index(), new_location);

// if an entity was moved into this entity's table spot, update its table row
if let Some(swapped_entity) = move_result.swapped_entity {
let swapped_location =
// SAFETY: If the swap was successful, swapped_entity must be valid.
unsafe { entities.get(swapped_entity).debug_checked_unwrap() };

entities.set(
swapped_entity.index(),
EntityLocation {
archetype_id: swapped_location.archetype_id,
archetype_row: swapped_location.archetype_row,
table_id: swapped_location.table_id,
table_row: result.table_row,
},
} else if new_archetype.id() == swapped_location.archetype_id {
new_archetype.set_entity_table_row(
swapped_location.archetype_row,
result.table_row,
);

if archetype.id() == swapped_location.archetype_id {
archetype.set_entity_table_row(
} else {
// SAFETY: the only two borrowed archetypes are above and we just did collision checks
(*archetypes_ptr.add(swapped_location.archetype_id.index()))
.set_entity_table_row(
swapped_location.archetype_row,
result.table_row,
);
} else if new_archetype.id() == swapped_location.archetype_id {
new_archetype.set_entity_table_row(
swapped_location.archetype_row,
result.table_row,
);
} else {
// SAFETY: the only two borrowed archetypes are above and we just did collision checks
(*archetypes_ptr.add(swapped_location.archetype_id.index()))
.set_entity_table_row(
swapped_location.archetype_row,
result.table_row,
);
}
}
}

bundle_info.write_components(
new_table,
sparse_sets,
add_bundle,
entity,
move_result.new_row,
self.change_tick,
bundle,
);

new_location
};

// SAFETY: We have no outstanding mutable references to world as they were dropped
let deferred_world = unsafe { self.world.into_deferred() };
trigger_hooks(entity, bundle_info, add_bundle, archetype, deferred_world);
bundle_info.write_components(
new_table,
sparse_sets,
add_bundle,
entity,
move_result.new_row,
self.change_tick,
bundle,
);

new_location
}
};

// SAFETY: We have no outstanding mutable references to world as they were dropped
let mut deferred_world = unsafe { self.world.into_deferred() };

if archetype.has_on_add() {
// SAFETY: All components in the bundle are guarenteed to exist in the World

Check warning on line 797 in crates/bevy_ecs/src/bundle.rs

View workflow job for this annotation

GitHub Actions / typos

"guarenteed" should be "guaranteed".
// as they must be initialized before creating the BundleInfo.
unsafe {
deferred_world.trigger_on_add(
entity,
bundle_info
.iter_components()
.zip(add_bundle.bundle_status.iter())
.filter(|(_, &status)| status == ComponentStatus::Added)
.map(|(id, _)| id),
);
}
}
if archetype.has_on_insert() {
// SAFETY: All components in the bundle are guarenteed to exist in the World

Check warning on line 811 in crates/bevy_ecs/src/bundle.rs

View workflow job for this annotation

GitHub Actions / typos

"guarenteed" should be "guaranteed".
// as they must be initialized before creating the BundleInfo.
unsafe { deferred_world.trigger_on_insert(entity, bundle_info.iter_components()) }
}

new_location
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_encase_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ proc-macro = true

[dependencies]
bevy_macro_utils = { path = "../bevy_macro_utils", version = "0.14.0-dev" }
encase_derive_impl = "0.7"
encase_derive_impl = { git = "https://github.com/james7132/encase", branch = "direct-copy-specialization" }

[lints]
workspace = true
2 changes: 1 addition & 1 deletion crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ flate2 = { version = "1.0.22", optional = true }
ruzstd = { version = "0.5.0", optional = true }
# For transcoding of UASTC/ETC1S universal formats, and for .basis file support
basis-universal = { version = "0.3.0", optional = true }
encase = { version = "0.7", features = ["glam"] }
encase = { git = "https://github.com/james7132/encase", branch = "direct-copy-specialization", features = ["glam"] }
# For wgpu profiling using tracing. Use `RUST_LOG=info` to also capture the wgpu spans.
profiling = { version = "1", features = [
"profile-with-tracing",
Expand Down
Loading

0 comments on commit 550eb5b

Please sign in to comment.