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

Add AssetChanged query filter #16810

Merged
merged 27 commits into from
Dec 17, 2024
Merged

Conversation

tychedelia
Copy link
Contributor

@tychedelia tychedelia commented Dec 13, 2024

Objective

Implement a new AssetChanged query filter that allows users to query for entities whose related assets may have changed.

  • Closes A query filter for Asset updates #5069
  • Unblocks Cold specialization #16420. Currently, cold-specialization, a key rendering optimization for unlocking ancillary benefits of the retained render world, is blocked on being unable detect all scenarios in which an entity's mesh/material changes using events and observers. An AssetChanged filter will drastically simplify our implementation and be more robust to future changes.

Originally implemented by @nicopap in #5080.

Solution

  • Adds a new AssetChanged query filter that initializes a AssetChanges<A> resource that tracks changed assets and ticks in asset_events.
  • Reverts constrain WorldQuery::get_state to only use &Components #13343 and changes the api of get_state to accept impl Into<UnsafeWorldCell<'w>> to allow accessing the AssetChanges<A> resource.
  • Adds a AsAssetId trait used for newtype handle wrappers (e.g. Mesh3d) that allows associating a component with the underlying Asset it represents.

Testing

  • Tests are added for AssetChanged.
  • TBD on performance. We are going to add this Mesh3d and MeshMaterial3d (etc) in the renderer. Long term wins in render performance this unblocks should swamp tracking overhead for any realistic workload.

Migration Guide

  • The asset_events system is no longer public. Users should order their systems relative to the AssetEvents system set.

@tychedelia tychedelia added A-ECS Entities, components, systems, and events X-Contentious There are nontrivial implications that should be thought through S-Needs-Review Needs reviewer attention (from anyone!) to move forward S-Needs-SME Decision or review from an SME is required labels Dec 13, 2024
@tychedelia tychedelia changed the title Changed handle Add AssetChanged query filter Dec 13, 2024
@Victoronz
Copy link
Contributor

Victoronz commented Dec 13, 2024

Why does this revert the get_state PR? In the get_state impl of this PR, I only see a call to resource_id on world, which is already available on &Components.

@ItsDoot ItsDoot added the C-Usability A targeted quality-of-life change that makes Bevy easier to use label Dec 13, 2024
@tychedelia
Copy link
Contributor Author

Why does this revert the get_state PR? In the get_state impl of this PR, I only see a call to resource_id on world, which is already available on &Components.

Because I didn't know that. Will change it back!

@Victoronz
Copy link
Contributor

Victoronz commented Dec 13, 2024

Separately, how should the resource access in init_fetch be reasoned about?
A type being private means it cannot be mentioned in user code, however you do not have to mention a type to obtain it via public API! In this case, this is possible via id-based resource fetching.
UnsafeWorldCell::get_resource gives its caller the responsibility to ensure it has access to that resource, however this safety contract cannot be passed on to users of the filter.
For both users, libraries and other in-engine areas, how should we communicate about this?

@Victoronz Victoronz added the P-Unsound A bug that results in undefined compiler behavior label Dec 13, 2024
crates/bevy_asset/src/lib.rs Outdated Show resolved Hide resolved

/// Filter that selects entities with a `A` for an asset that changed
/// after the system last ran, where `A` is a component that implements
/// [`AsAssetId`].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about components that store handles to multiple assets of the same type? We don't need to do that right now, but we might have to revisit it later. In my app I have components with handles to dozens of AnimationClips for example. I think you could maybe just have AsAssetId's method return -> impl Iterator<Item = AssetId>.

This isn't blocking, just a suggestion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bigger issue is how to handle the associated type. We might have to use some kind of indirection via UntypedAssetId instead to be able to handle multiple kinds of assets.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I also misunderstood you. Multiple assets of the same type should be much easier to handle. I'd been hung up on if, for example we decided to merge mesh and material as has been discussed before.

crates/bevy_asset/src/asset_changed.rs Show resolved Hide resolved
crates/bevy_asset/src/asset_changed.rs Outdated Show resolved Hide resolved
crates/bevy_asset/src/lib.rs Outdated Show resolved Hide resolved
crates/bevy_asset/src/lib.rs Outdated Show resolved Hide resolved
crates/bevy_ecs/src/query/fetch.rs Outdated Show resolved Hide resolved
@tychedelia
Copy link
Contributor Author

In this case, this is possible via id-based resource fetching.

How would the user forge an id if they do not have access to the underlying type? UnsafeWorldCell::get_resource already documents the aliasing requirements as an unsafe API. If users are iterating or creating random opaque ComponentIds and getting mutable access they trivially violate that contract by not knowing what resource they are touching.

Co-authored-by: Patrick Walton <pcwalton@mimiga.net>
@pcwalton
Copy link
Contributor

Overall this looks fine to me, but as I mentioned we should have an ECS expert look over the get_state signature change.

@alice-i-cecile alice-i-cecile added this to the 0.16 milestone Dec 13, 2024
@alice-i-cecile alice-i-cecile added the M-Needs-Release-Note Work that should be called out in the blog due to impact label Dec 14, 2024
@@ -583,6 +583,16 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
{
access.add_component_write(archetype_component_id);
}
if self.component_access.access.has_resource_read(component_id) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, this is an existing soundness bug. Re-adding the tag.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes have been removed in favor of @chescock's.

@alice-i-cecile alice-i-cecile added the P-Unsound A bug that results in undefined compiler behavior label Dec 14, 2024
tychedelia and others added 4 commits December 13, 2024 22:33
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
github-merge-queue bot pushed a commit that referenced this pull request Dec 17, 2024
# Objective

Allow resources to be accessed soundly by `QueryData` and `QueryFilter`
implementations.

This mostly works today, and is used in `bevy-trait-query` and will be
used by #16810. The problem is that the access is not made visible to
the executor, so it would be possible for a system with resource access
in a query to run concurrently with a system that accesses the resource
with `ResMut`, resulting in Undefined Behavior.

## Solution

Define calling `add_resource_read` or `add_resource_write` in
`WorldQuery::update_component_access` to be a supported way to declare
resource access in a query.
Modify `QueryState::new_with_access` to check for resource access and
report it in `archetype_component_acccess`.
Modify `FilteredAccess::is_compatible` to consider resource access
conflicting even on queries with disjoint filters.
Copy link
Contributor

@pcwalton pcwalton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine, with the caveat that as usual I'm not an expert on the ECS guts.

/// This resource is automatically managed by the [`AssetEvents`](crate::AssetEvents) schedule and
/// should not be exposed to the user in order to maintain safety guarantees. Any additional uses of
/// this resource should be carefully audited to ensure that they do not introduce any safety
/// issues.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you mean "should not be exposed", what do you mean? Changing the fields?

I ask because usually safety invariants are scoped to be module-private, not crate-private.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mostly is meant to mean don't make this pub because some asks. It's crate scoped mostly due to code organization, having the filter live in its own file is I think preferable to including all of this code in the root lib file where the asset event system lives simply for the sake of keeping it module private.

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Dec 17, 2024
Merged via the queue into bevyengine:main with commit df14443 Dec 17, 2024
28 checks passed
ecoskey pushed a commit to ecoskey/bevy that referenced this pull request Jan 6, 2025
# Objective

Allow resources to be accessed soundly by `QueryData` and `QueryFilter`
implementations.

This mostly works today, and is used in `bevy-trait-query` and will be
used by bevyengine#16810. The problem is that the access is not made visible to
the executor, so it would be possible for a system with resource access
in a query to run concurrently with a system that accesses the resource
with `ResMut`, resulting in Undefined Behavior.

## Solution

Define calling `add_resource_read` or `add_resource_write` in
`WorldQuery::update_component_access` to be a supported way to declare
resource access in a query.
Modify `QueryState::new_with_access` to check for resource access and
report it in `archetype_component_acccess`.
Modify `FilteredAccess::is_compatible` to consider resource access
conflicting even on queries with disjoint filters.
ecoskey pushed a commit to ecoskey/bevy that referenced this pull request Jan 6, 2025
# Objective

Implement a new `AssetChanged` query filter that allows users to query
for entities whose related assets may have changed.

- Closes bevyengine#5069
- Unblocks bevyengine#16420. Currently, `cold-specialization`, a key rendering
optimization for unlocking ancillary benefits of the retained render
world, is blocked on being unable detect all scenarios in which an
entity's mesh/material changes using events and observers. An
`AssetChanged` filter will drastically simplify our implementation and
be more robust to future changes.

Originally implemented by @nicopap in bevyengine#5080.

## Solution

- Adds a new `AssetChanged` query filter that initializes a
`AssetChanges<A>` resource that tracks changed assets and ticks in
`asset_events`.
- ~Reverts bevyengine#13343 and changes the api of `get_state` to accept `impl
Into<UnsafeWorldCell<'w>>` to allow accessing the `AssetChanges<A>`
resource.~
- Adds a `AsAssetId` trait used for newtype handle wrappers (e.g.
`Mesh3d`) that allows associating a component with the underlying
`Asset` it represents.

## Testing

- Tests are added for `AssetChanged`.
- TBD on performance. We are going to add this `Mesh3d` and
`MeshMaterial3d` (etc) in the renderer. Long term wins in render
performance this unblocks should swamp tracking overhead for any
realistic workload.

## Migration Guide

- The `asset_events` system is no longer public. Users should order
their systems relative to the `AssetEvents` system set.

---------

Co-authored-by: Nicola Papale <nico@nicopap.ch>
Co-authored-by: Patrick Walton <pcwalton@mimiga.net>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: Chris Russell <8494645+chescock@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Unsafe Touches with unsafe code in some way M-Needs-Release-Note Work that should be called out in the blog due to impact P-Unsound A bug that results in undefined compiler behavior S-Needs-Review Needs reviewer attention (from anyone!) to move forward X-Contentious There are nontrivial implications that should be thought through
Projects
None yet
Development

Successfully merging this pull request may close these issues.

A query filter for Asset updates
7 participants