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

Dynamic queries and builder API #9774

Merged
merged 57 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
b56e1c7
Fully dynamic term based query builder
james-j-obrien Sep 11, 2023
5917065
System installation of dynamically built queries
james-j-obrien Sep 11, 2023
b7f04df
Fix change detection, add Changed and Added
james-j-obrien Sep 11, 2023
07dc116
Add additional tests, fix bugs and clean up
james-j-obrien Sep 11, 2023
a12b5dd
Add trivial examples
james-j-obrien Sep 11, 2023
f6382c1
Performance tweaks
james-j-obrien Sep 12, 2023
25623dc
Add dynamic reference methods
james-j-obrien Sep 12, 2023
c16e0ec
Fix rebase issue
james-j-obrien Sep 12, 2023
799b318
Remove examples so github is less angry, rename builder methods to be…
james-j-obrien Sep 12, 2023
711653b
Better builder method names
james-j-obrien Sep 12, 2023
ff1e33a
Major perf improvements, add additional dynamic builder methods
james-j-obrien Sep 12, 2023
40bd8c5
Remove closures from hot loop, remove more runtime checks
james-j-obrien Sep 12, 2023
d0736f5
Remove allocations from hot loop
james-j-obrien Sep 12, 2023
c3d37da
Minor cleanup
james-j-obrien Sep 12, 2023
353c248
Remove #[inline(always)] where it doesn't improve or degrades perform…
james-j-obrien Sep 13, 2023
40c1c62
Flatten terms
james-j-obrien Sep 13, 2023
7667c0a
Fix AnyOf issue
james-j-obrien Sep 13, 2023
031779c
Reorder impls to be more intuitive
james-j-obrien Sep 13, 2023
5494b28
Add dynamic_query example
james-j-obrien Sep 14, 2023
688459d
First docs pass, bug fixes and minor improvements
james-j-obrien Sep 15, 2023
666edb3
Improve documentation, add additional utility functions, add benches
james-j-obrien Sep 18, 2023
5ac0e50
Remove temporary test
james-j-obrien Sep 18, 2023
9d6382f
Minor cleanup
james-j-obrien Sep 18, 2023
f7abf12
First CI pass
james-j-obrien Sep 18, 2023
5c880e9
Second CI pass
james-j-obrien Sep 18, 2023
7680abd
Third times the charm
james-j-obrien Sep 18, 2023
7bd46ab
Add comments
james-j-obrien Sep 18, 2023
db48ac8
Flatten terms, remove branching where possible
james-j-obrien Sep 29, 2023
02091d9
Fix example
james-j-obrien Sep 29, 2023
9a3f5b3
Static iteration, remove FetchedTerm and FetchBuffer
james-j-obrien Sep 30, 2023
88ba5e3
Cleanup component ptr access
james-j-obrien Sep 30, 2023
2cf8f1e
Tidy docs slightly
james-j-obrien Sep 30, 2023
9c65312
Swap to constants to speed up dense iteration, minor API cleanup
james-j-obrien Sep 30, 2023
e9c7861
Consistency pass
james-j-obrien Sep 30, 2023
c674c4a
Move to pointers from iterators, split fetch and filter terms
james-j-obrien Oct 4, 2023
5952756
Add From<Mut<T>> For MutUntyped
james-j-obrien Oct 7, 2023
a76370c
FilteredAccess based implementation
james-j-obrien Nov 14, 2023
323cc74
Merge branch 'dynamic-term-builder' into query-builder
james-j-obrien Nov 14, 2023
ce0ea41
Merge branch 'bevyengine:main' into dynamic-term-builder
james-j-obrien Nov 14, 2023
89f1eac
Doc fixes
james-j-obrien Nov 14, 2023
bf9cee7
Merge branch 'dynamic-term-builder' of https://github.com/james-j-obr…
james-j-obrien Nov 14, 2023
32a6796
Add compile fail tests, add safety comments to example
james-j-obrien Nov 20, 2023
289ca79
Merge main
james-j-obrien Nov 22, 2023
52e0391
Learn how to spell required correctly
james-j-obrien Nov 22, 2023
fecd6bc
Merge main, incorporate feedback
james-j-obrien Nov 30, 2023
04d88cc
Fix doc test
james-j-obrien Nov 30, 2023
3bfcc3f
Fix docs and compile fail tests
james-j-obrien Nov 30, 2023
60c006f
Fix trailing newline
james-j-obrien Nov 30, 2023
b59bb38
Fix trailing whitespace...
james-j-obrien Nov 30, 2023
cf951bb
Merge branch 'main' into dynamic-term-builder
james-j-obrien Dec 2, 2023
2e8a197
Remove unused import
james-j-obrien Dec 2, 2023
5c98ef4
Improve safety comments
james-j-obrien Dec 15, 2023
6f6a0cc
Merge main
james-j-obrien Dec 15, 2023
63f97a3
Address remaining feedback
james-j-obrien Jan 16, 2024
056ff2a
Merge branch 'main' into dynamic-term-builder
james-j-obrien Jan 16, 2024
a3ab076
Fix panic strings
james-j-obrien Jan 16, 2024
8c1bbb0
Fix doc test
james-j-obrien Jan 16, 2024
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
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,17 @@ description = "Groups commonly used compound queries and query filters into a si
category = "ECS (Entity Component System)"
wasm = false

[[example]]
name = "dynamic"
path = "examples/ecs/dynamic.rs"
doc-scrape-examples = true

[package.metadata.example.dynamic]
name = "Dynamic ECS"
description = "Dynamically create components, spawn entities with those components and query those components"
category = "ECS (Entity Component System)"
wasm = false

[[example]]
name = "event"
path = "examples/ecs/event.rs"
Expand Down
16 changes: 6 additions & 10 deletions crates/bevy_ecs/macros/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,22 +336,18 @@ pub fn derive_world_query_impl(input: TokenStream) -> TokenStream {
#( <#field_types>::update_component_access(&state.#named_field_idents, _access); )*
}

fn update_archetype_component_access(
state: &Self::State,
_archetype: &#path::archetype::Archetype,
_access: &mut #path::query::Access<#path::archetype::ArchetypeComponentId>
) {
#(
<#field_types>::update_archetype_component_access(&state.#named_field_idents, _archetype, _access);
)*
}

fn init_state(world: &mut #path::world::World) -> #state_struct_name #user_ty_generics {
#state_struct_name {
#(#named_field_idents: <#field_types>::init_state(world),)*
}
}

fn get_state(world: &#path::world::World) -> Option<#state_struct_name #user_ty_generics> {
Some(#state_struct_name {
#(#named_field_idents: <#field_types>::get_state(world)?,)*
})
}

fn matches_component_set(state: &Self::State, _set_contains_id: &impl Fn(#path::component::ComponentId) -> bool) -> bool {
true #(&& <#field_types>::matches_component_set(&state.#named_field_idents, _set_contains_id))*
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub mod prelude {
component::Component,
entity::Entity,
event::{Event, EventReader, EventWriter, Events},
query::{Added, AnyOf, Changed, Has, Or, QueryState, With, Without},
query::{Added, AnyOf, Changed, Has, Or, QueryBuilder, QueryState, With, Without},
removal_detection::RemovedComponents,
schedule::{
apply_deferred, apply_state_transition, common_conditions::*, Condition,
Expand Down
60 changes: 55 additions & 5 deletions crates/bevy_ecs/src/query/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ impl<T: SparseSetIndex> Access<T> {
self.writes_all
}

/// Removes all writes.
pub fn clear_writes(&mut self) {
james-j-obrien marked this conversation as resolved.
Show resolved Hide resolved
self.writes_all = false;
self.writes.clear();
}

/// Removes all accesses.
pub fn clear(&mut self) {
self.reads_all = false;
Expand Down Expand Up @@ -198,6 +204,33 @@ impl<T: SparseSetIndex> Access<T> {
&& other.writes.is_disjoint(&self.reads_and_writes)
}

/// Returns `true` if the set is a subset of another, i.e. `other` contains
/// at least all the values in `self`.
pub fn is_subset(&self, other: &Access<T>) -> bool {
if self.writes_all {
return other.writes_all;
}

if other.writes_all {
return true;
}

if self.reads_all {
return other.reads_all;
}

if other.reads_all {
return self.writes.is_subset(&other.writes);
}

let reads = self
.reads_and_writes
.difference(&self.writes)
.collect::<FixedBitSet>();

reads.is_subset(&other.reads_and_writes) && self.writes.is_subset(&other.writes)
james-j-obrien marked this conversation as resolved.
Show resolved Hide resolved
}

/// Returns a vector of elements that the access and `other` cannot access at the same time.
pub fn get_conflicts(&self, other: &Access<T>) -> Vec<T> {
let mut conflicts = FixedBitSet::default();
Expand Down Expand Up @@ -267,16 +300,18 @@ impl<T: SparseSetIndex> Access<T> {
/// See comments the [`WorldQuery`](super::WorldQuery) impls of [`AnyOf`](super::AnyOf)/`Option`/[`Or`](super::Or) for more information.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct FilteredAccess<T: SparseSetIndex> {
access: Access<T>,
pub(crate) access: Access<T>,
pub(crate) required: FixedBitSet,
james-j-obrien marked this conversation as resolved.
Show resolved Hide resolved
// An array of filter sets to express `With` or `Without` clauses in disjunctive normal form, for example: `Or<(With<A>, With<B>)>`.
// Filters like `(With<A>, Or<(With<B>, Without<C>)>` are expanded into `Or<((With<A>, With<B>), (With<A>, Without<C>))>`.
filter_sets: Vec<AccessFilters<T>>,
pub(crate) filter_sets: Vec<AccessFilters<T>>,
}

impl<T: SparseSetIndex> Default for FilteredAccess<T> {
fn default() -> Self {
Self {
access: Access::default(),
required: FixedBitSet::default(),
filter_sets: vec![AccessFilters::default()],
}
}
Expand Down Expand Up @@ -306,15 +341,23 @@ impl<T: SparseSetIndex> FilteredAccess<T> {
/// Adds access to the element given by `index`.
pub fn add_read(&mut self, index: T) {
self.access.add_read(index.clone());
self.add_required(index.clone());
self.and_with(index);
}

/// Adds exclusive access to the element given by `index`.
pub fn add_write(&mut self, index: T) {
self.access.add_write(index.clone());
self.add_required(index.clone());
self.and_with(index);
}

fn add_required(&mut self, index: T) {
let index = index.sparse_set_index();
self.required.grow(index + 1);
self.required.insert(index);
}

/// Adds a `With` filter: corresponds to a conjunction (AND) operation.
///
/// Suppose we begin with `Or<(With<A>, With<B>)>`, which is represented by an array of two `AccessFilter` instances.
Expand Down Expand Up @@ -391,6 +434,7 @@ impl<T: SparseSetIndex> FilteredAccess<T> {
/// `Or<((With<A>, With<C>), (With<A>, Without<D>), (Without<B>, With<C>), (Without<B>, Without<D>))>`.
pub fn extend(&mut self, other: &FilteredAccess<T>) {
self.access.extend(&other.access);
self.required.union_with(&other.required);

// We can avoid allocating a new array of bitsets if `other` contains just a single set of filters:
// in this case we can short-circuit by performing an in-place union for each bitset.
Expand Down Expand Up @@ -423,12 +467,18 @@ impl<T: SparseSetIndex> FilteredAccess<T> {
pub fn write_all(&mut self) {
self.access.write_all();
}

/// Returns `true` if the set is a subset of another, i.e. `other` contains
/// at least all the values in `self`.
pub fn is_subset(&self, other: &FilteredAccess<T>) -> bool {
self.required.is_subset(&other.required) && self.access().is_subset(other.access())
}
}

#[derive(Clone, Eq, PartialEq)]
struct AccessFilters<T> {
with: FixedBitSet,
without: FixedBitSet,
pub(crate) struct AccessFilters<T> {
pub(crate) with: FixedBitSet,
pub(crate) without: FixedBitSet,
_index_type: PhantomData<T>,
}

Expand Down
Loading
Loading