Skip to content

Commit 550eb5b

Browse files
committed
Remove the need for the function
1 parent 2162a77 commit 550eb5b

File tree

6 files changed

+157
-168
lines changed

6 files changed

+157
-168
lines changed

crates/bevy_color/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ bevy_reflect = { path = "../bevy_reflect", version = "0.14.0-dev", features = [
1616
bytemuck = "1"
1717
serde = "1.0"
1818
thiserror = "1.0"
19+
<<<<<<< Updated upstream
1920
wgpu = { version = "0.19.3", default-features = false }
2021
encase = { version = "0.7", default-features = false }
22+
=======
23+
wgpu = { version = "0.19.1", default-features = false }
24+
encase = { git = "https://github.com/james7132/encase", branch = "direct-copy-specialization", default_features = false }
25+
>>>>>>> Stashed changes
2126

2227
[lints]
2328
workspace = true

crates/bevy_color/src/linear_rgba.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ impl encase::ShaderType for LinearRgba {
336336
alignment,
337337
has_uniform_min_alignment: false,
338338
min_size: size,
339+
has_internal_padding: false,
339340
extra: (),
340341
}
341342
};

crates/bevy_ecs/src/bundle.rs

Lines changed: 144 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
prelude::World,
1616
query::DebugCheckedUnwrap,
1717
storage::{SparseSetIndex, SparseSets, Storages, Table, TableRow},
18-
world::{unsafe_world_cell::UnsafeWorldCell, DeferredWorld},
18+
world::{unsafe_world_cell::UnsafeWorldCell},
1919
};
2020
use bevy_ptr::{ConstNonNull, OwningPtr};
2121
use bevy_utils::all_tuples;
@@ -637,205 +637,183 @@ impl<'w> BundleInserter<'w> {
637637
location: EntityLocation,
638638
bundle: T,
639639
) -> EntityLocation {
640-
#[inline]
641-
fn trigger_hooks(
642-
entity: Entity,
643-
bundle_info: &BundleInfo,
644-
add_bundle: &AddBundle,
645-
archetype: &Archetype,
646-
mut world: DeferredWorld,
647-
) {
648-
if archetype.has_on_add() {
649-
// SAFETY: All components in the bundle are guarenteed to exist in the World
650-
// as they must be initialized before creating the BundleInfo.
651-
unsafe {
652-
world.trigger_on_add(
653-
entity,
654-
bundle_info
655-
.iter_components()
656-
.zip(add_bundle.bundle_status.iter())
657-
.filter(|(_, &status)| status == ComponentStatus::Added)
658-
.map(|(id, _)| id),
659-
);
660-
}
661-
}
662-
if archetype.has_on_insert() {
663-
// SAFETY: All components in the bundle are guarenteed to exist in the World
664-
// as they must be initialized before creating the BundleInfo.
665-
unsafe { world.trigger_on_insert(entity, bundle_info.iter_components()) }
666-
}
667-
}
668-
669640
let bundle_info = self.bundle_info.as_ref();
670641
let add_bundle = self.add_bundle.as_ref();
671642
let table = self.table.as_mut();
672643
let archetype = self.archetype.as_mut();
673644

674-
match &mut self.result {
645+
let new_location = match &mut self.result {
675646
InsertBundleResult::SameArchetype => {
676-
{
677-
// SAFETY: Mutable references do not alias and will be dropped after this block
678-
let sparse_sets = {
679-
let world = self.world.world_mut();
680-
&mut world.storages.sparse_sets
681-
};
647+
// SAFETY: Mutable references do not alias and will be dropped after this block
648+
let sparse_sets = {
649+
let world = self.world.world_mut();
650+
&mut world.storages.sparse_sets
651+
};
652+
653+
bundle_info.write_components(
654+
table,
655+
sparse_sets,
656+
add_bundle,
657+
entity,
658+
location.table_row,
659+
self.change_tick,
660+
bundle,
661+
);
682662

683-
bundle_info.write_components(
684-
table,
685-
sparse_sets,
686-
add_bundle,
687-
entity,
688-
location.table_row,
689-
self.change_tick,
690-
bundle,
691-
);
692-
}
693-
// SAFETY: We have no outstanding mutable references to world as they were dropped
694-
let deferred_world = unsafe { self.world.into_deferred() };
695-
trigger_hooks(entity, bundle_info, add_bundle, archetype, deferred_world);
696663
location
697664
}
698665
InsertBundleResult::NewArchetypeSameTable { new_archetype } => {
699666
let new_archetype = new_archetype.as_mut();
700667

701-
let new_location = {
702-
// SAFETY: Mutable references do not alias and will be dropped after this block
703-
let (sparse_sets, entities) = {
704-
let world = self.world.world_mut();
705-
(&mut world.storages.sparse_sets, &mut world.entities)
706-
};
707-
708-
let result = archetype.swap_remove(location.archetype_row);
709-
if let Some(swapped_entity) = result.swapped_entity {
710-
let swapped_location =
711-
// SAFETY: If the swap was successful, swapped_entity must be valid.
712-
unsafe { entities.get(swapped_entity).debug_checked_unwrap() };
713-
entities.set(
714-
swapped_entity.index(),
715-
EntityLocation {
716-
archetype_id: swapped_location.archetype_id,
717-
archetype_row: location.archetype_row,
718-
table_id: swapped_location.table_id,
719-
table_row: swapped_location.table_row,
720-
},
721-
);
722-
}
723-
let new_location = new_archetype.allocate(entity, result.table_row);
724-
entities.set(entity.index(), new_location);
725-
bundle_info.write_components(
726-
table,
727-
sparse_sets,
728-
add_bundle,
729-
entity,
730-
result.table_row,
731-
self.change_tick,
732-
bundle,
733-
);
734-
new_location
668+
// SAFETY: Mutable references do not alias and will be dropped after this block
669+
let (sparse_sets, entities) = {
670+
let world = self.world.world_mut();
671+
(&mut world.storages.sparse_sets, &mut world.entities)
735672
};
736673

737-
// SAFETY: We have no outstanding mutable references to world as they were dropped
738-
let deferred_world = unsafe { self.world.into_deferred() };
739-
trigger_hooks(entity, bundle_info, add_bundle, archetype, deferred_world);
674+
let result = archetype.swap_remove(location.archetype_row);
675+
if let Some(swapped_entity) = result.swapped_entity {
676+
let swapped_location =
677+
// SAFETY: If the swap was successful, swapped_entity must be valid.
678+
unsafe { entities.get(swapped_entity).debug_checked_unwrap() };
679+
entities.set(
680+
swapped_entity.index(),
681+
EntityLocation {
682+
archetype_id: swapped_location.archetype_id,
683+
archetype_row: location.archetype_row,
684+
table_id: swapped_location.table_id,
685+
table_row: swapped_location.table_row,
686+
},
687+
);
688+
}
740689

690+
let new_location = new_archetype.allocate(entity, result.table_row);
691+
entities.set(entity.index(), new_location);
692+
bundle_info.write_components(
693+
table,
694+
sparse_sets,
695+
add_bundle,
696+
entity,
697+
result.table_row,
698+
self.change_tick,
699+
bundle,
700+
);
741701
new_location
742702
}
743703
InsertBundleResult::NewArchetypeNewTable {
744704
new_archetype,
745705
new_table,
746706
} => {
747707
let new_table = new_table.as_mut();
708+
let new_archetype = new_archetype.as_mut();
748709

749-
let new_location = {
750-
// SAFETY: Mutable references do not alias and will be dropped after this block
751-
let (archetypes_ptr, sparse_sets, entities) = {
752-
let world = self.world.world_mut();
753-
let archetype_ptr: *mut Archetype =
754-
world.archetypes.archetypes.as_mut_ptr();
755-
(
756-
archetype_ptr,
757-
&mut world.storages.sparse_sets,
758-
&mut world.entities,
759-
)
760-
};
761-
let new_archetype = new_archetype.as_mut();
762-
let result = archetype.swap_remove(location.archetype_row);
763-
if let Some(swapped_entity) = result.swapped_entity {
764-
let swapped_location =
765-
// SAFETY: If the swap was successful, swapped_entity must be valid.
766-
unsafe { entities.get(swapped_entity).debug_checked_unwrap() };
767-
entities.set(
768-
swapped_entity.index(),
769-
EntityLocation {
770-
archetype_id: swapped_location.archetype_id,
771-
archetype_row: location.archetype_row,
772-
table_id: swapped_location.table_id,
773-
table_row: swapped_location.table_row,
774-
},
710+
// SAFETY: Mutable references do not alias and will be dropped after this block
711+
let (archetypes_ptr, sparse_sets, entities) = {
712+
let world = self.world.world_mut();
713+
let archetype_ptr: *mut Archetype =
714+
world.archetypes.archetypes.as_mut_ptr();
715+
(
716+
archetype_ptr,
717+
&mut world.storages.sparse_sets,
718+
&mut world.entities,
719+
)
720+
};
721+
722+
let result = archetype.swap_remove(location.archetype_row);
723+
if let Some(swapped_entity) = result.swapped_entity {
724+
let swapped_location =
725+
// SAFETY: If the swap was successful, swapped_entity must be valid.
726+
unsafe { entities.get(swapped_entity).debug_checked_unwrap() };
727+
entities.set(
728+
swapped_entity.index(),
729+
EntityLocation {
730+
archetype_id: swapped_location.archetype_id,
731+
archetype_row: location.archetype_row,
732+
table_id: swapped_location.table_id,
733+
table_row: swapped_location.table_row,
734+
},
735+
);
736+
}
737+
// PERF: store "non bundle" components in edge, then just move those to avoid
738+
// redundant copies
739+
let move_result = table.move_to_superset_unchecked(result.table_row, new_table);
740+
let new_location = new_archetype.allocate(entity, move_result.new_row);
741+
entities.set(entity.index(), new_location);
742+
743+
// if an entity was moved into this entity's table spot, update its table row
744+
if let Some(swapped_entity) = move_result.swapped_entity {
745+
let swapped_location =
746+
// SAFETY: If the swap was successful, swapped_entity must be valid.
747+
unsafe { entities.get(swapped_entity).debug_checked_unwrap() };
748+
749+
entities.set(
750+
swapped_entity.index(),
751+
EntityLocation {
752+
archetype_id: swapped_location.archetype_id,
753+
archetype_row: swapped_location.archetype_row,
754+
table_id: swapped_location.table_id,
755+
table_row: result.table_row,
756+
},
757+
);
758+
759+
if archetype.id() == swapped_location.archetype_id {
760+
archetype.set_entity_table_row(
761+
swapped_location.archetype_row,
762+
result.table_row,
775763
);
776-
}
777-
// PERF: store "non bundle" components in edge, then just move those to avoid
778-
// redundant copies
779-
let move_result = table.move_to_superset_unchecked(result.table_row, new_table);
780-
let new_location = new_archetype.allocate(entity, move_result.new_row);
781-
entities.set(entity.index(), new_location);
782-
783-
// if an entity was moved into this entity's table spot, update its table row
784-
if let Some(swapped_entity) = move_result.swapped_entity {
785-
let swapped_location =
786-
// SAFETY: If the swap was successful, swapped_entity must be valid.
787-
unsafe { entities.get(swapped_entity).debug_checked_unwrap() };
788-
789-
entities.set(
790-
swapped_entity.index(),
791-
EntityLocation {
792-
archetype_id: swapped_location.archetype_id,
793-
archetype_row: swapped_location.archetype_row,
794-
table_id: swapped_location.table_id,
795-
table_row: result.table_row,
796-
},
764+
} else if new_archetype.id() == swapped_location.archetype_id {
765+
new_archetype.set_entity_table_row(
766+
swapped_location.archetype_row,
767+
result.table_row,
797768
);
798-
799-
if archetype.id() == swapped_location.archetype_id {
800-
archetype.set_entity_table_row(
769+
} else {
770+
// SAFETY: the only two borrowed archetypes are above and we just did collision checks
771+
(*archetypes_ptr.add(swapped_location.archetype_id.index()))
772+
.set_entity_table_row(
801773
swapped_location.archetype_row,
802774
result.table_row,
803775
);
804-
} else if new_archetype.id() == swapped_location.archetype_id {
805-
new_archetype.set_entity_table_row(
806-
swapped_location.archetype_row,
807-
result.table_row,
808-
);
809-
} else {
810-
// SAFETY: the only two borrowed archetypes are above and we just did collision checks
811-
(*archetypes_ptr.add(swapped_location.archetype_id.index()))
812-
.set_entity_table_row(
813-
swapped_location.archetype_row,
814-
result.table_row,
815-
);
816-
}
817776
}
777+
}
818778

819-
bundle_info.write_components(
820-
new_table,
821-
sparse_sets,
822-
add_bundle,
823-
entity,
824-
move_result.new_row,
825-
self.change_tick,
826-
bundle,
827-
);
828-
829-
new_location
830-
};
831-
832-
// SAFETY: We have no outstanding mutable references to world as they were dropped
833-
let deferred_world = unsafe { self.world.into_deferred() };
834-
trigger_hooks(entity, bundle_info, add_bundle, archetype, deferred_world);
779+
bundle_info.write_components(
780+
new_table,
781+
sparse_sets,
782+
add_bundle,
783+
entity,
784+
move_result.new_row,
785+
self.change_tick,
786+
bundle,
787+
);
835788

836789
new_location
837790
}
791+
};
792+
793+
// SAFETY: We have no outstanding mutable references to world as they were dropped
794+
let mut deferred_world = unsafe { self.world.into_deferred() };
795+
796+
if archetype.has_on_add() {
797+
// SAFETY: All components in the bundle are guarenteed to exist in the World
798+
// as they must be initialized before creating the BundleInfo.
799+
unsafe {
800+
deferred_world.trigger_on_add(
801+
entity,
802+
bundle_info
803+
.iter_components()
804+
.zip(add_bundle.bundle_status.iter())
805+
.filter(|(_, &status)| status == ComponentStatus::Added)
806+
.map(|(id, _)| id),
807+
);
808+
}
838809
}
810+
if archetype.has_on_insert() {
811+
// SAFETY: All components in the bundle are guarenteed to exist in the World
812+
// as they must be initialized before creating the BundleInfo.
813+
unsafe { deferred_world.trigger_on_insert(entity, bundle_info.iter_components()) }
814+
}
815+
816+
new_location
839817
}
840818

841819
#[inline]

crates/bevy_encase_derive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ proc-macro = true
1313

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

1818
[lints]
1919
workspace = true

crates/bevy_render/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ flate2 = { version = "1.0.22", optional = true }
9090
ruzstd = { version = "0.5.0", optional = true }
9191
# For transcoding of UASTC/ETC1S universal formats, and for .basis file support
9292
basis-universal = { version = "0.3.0", optional = true }
93-
encase = { version = "0.7", features = ["glam"] }
93+
encase = { git = "https://github.com/james7132/encase", branch = "direct-copy-specialization", features = ["glam"] }
9494
# For wgpu profiling using tracing. Use `RUST_LOG=info` to also capture the wgpu spans.
9595
profiling = { version = "1", features = [
9696
"profile-with-tracing",

0 commit comments

Comments
 (0)