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

Unload render assets from RAM #10520

Merged
merged 48 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
e3b4936
Unload render assets from RAM
JMS55 Nov 12, 2023
9e6a6e2
Rename, docs
JMS55 Nov 12, 2023
0e0cf83
Doc clarification
JMS55 Nov 12, 2023
d7796cf
Refactor
JMS55 Nov 12, 2023
bb1b9f1
Merge commit 'ea01c3e387d80994662aa54c2b8f70dd678903c3' into unload_r…
JMS55 Nov 22, 2023
22588ad
Send asset removal event on last strong handle drop, even if the asse…
JMS55 Nov 22, 2023
d28a695
Revert previous dumb commit
JMS55 Nov 22, 2023
3111a57
Add TODO
JMS55 Nov 22, 2023
63055d8
Remove Assets::remove
JMS55 Nov 22, 2023
29b1c71
Clarify doc
JMS55 Nov 22, 2023
5ce91e1
Add NoLongerUsed event
JMS55 Nov 22, 2023
6fe128d
Fix docs
JMS55 Nov 22, 2023
b91ddd7
Fix docs
JMS55 Nov 23, 2023
2d1a5c5
Read asset info for skip
JMS55 Nov 23, 2023
624fe80
Fixes
JMS55 Nov 23, 2023
9d7d16f
Fix tests
JMS55 Nov 23, 2023
1b1eb32
Misc
JMS55 Nov 27, 2023
18ffd40
Fix examples
JMS55 Nov 28, 2023
239b6a3
Ignore clippy
JMS55 Nov 28, 2023
e3de7ce
Merge commit 'a902ea6f85079777e61ea8627d86277c0d9abaf1' into unload_r…
JMS55 Nov 28, 2023
d00e845
Missed a clippy lint
JMS55 Nov 28, 2023
9829eec
Missed more clippy lints again
JMS55 Nov 28, 2023
9b47b6b
Remove unused import
JMS55 Nov 28, 2023
9478355
Fixes
JMS55 Nov 28, 2023
9c0f3bc
Add missed image loader
JMS55 Nov 28, 2023
aa538e2
Fix doc links
JMS55 Nov 28, 2023
19cd2bd
Fix examples
JMS55 Nov 28, 2023
23cf06f
Fix more examples
JMS55 Nov 28, 2023
69061f8
Merge remote-tracking branch 'bevy/gh-readonly-queue/main/pr-10785-0f…
JMS55 Nov 29, 2023
ae035b5
Add image loader persistence setting
JMS55 Dec 3, 2023
03c33de
Fixes
JMS55 Dec 3, 2023
d083e30
Appease clippy
JMS55 Dec 3, 2023
400f324
Merge commit '4d42713e7713bd8abaa3fe3164574b5b6895f40e' into unload_r…
JMS55 Dec 3, 2023
8b863af
Merge commit '11065974d487ca4b9680ae683a9d356f2f1b6c36' into unload_r…
JMS55 Dec 14, 2023
2c9c529
Merge commit '8067e46049f222d37ac394745805bad98979980f' into unload_r…
JMS55 Dec 28, 2023
e1f78c9
Use enum instead of bool
JMS55 Dec 28, 2023
8c92266
Misc changes
JMS55 Dec 28, 2023
6acc91d
Format
JMS55 Dec 28, 2023
6bcc067
Fix example imports
JMS55 Dec 28, 2023
9bb0f46
Fix tests and docs
JMS55 Dec 29, 2023
fb45b81
Rename
JMS55 Dec 30, 2023
ccb8afe
Tweak doc comment
JMS55 Dec 30, 2023
9088603
Merge commit '786abbf3f5e5be4b89c6b53d2d03162079f8e1f4' into unload_r…
JMS55 Dec 30, 2023
ea5af0d
Add missing import
JMS55 Dec 30, 2023
fec04d1
Fix doc
JMS55 Dec 30, 2023
8538833
Rename to Unused
JMS55 Jan 3, 2024
16e320e
Finish rename
JMS55 Jan 3, 2024
6efbb67
Fix another name I missed
JMS55 Jan 3, 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
1 change: 1 addition & 0 deletions crates/bevy_core_pipeline/src/tonemapping/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,5 +412,6 @@ pub fn lut_placeholder() -> Image {
},
sampler: ImageSampler::Default,
texture_view_descriptor: None,
mutable: false,
}
}
4 changes: 4 additions & 0 deletions crates/bevy_gizmos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ impl RenderAsset for LineGizmo {
self.clone()
}

fn unload_after_extract(_extracted_asset: &Self::ExtractedAsset) -> bool {
false
}

fn prepare_asset(
line_gizmo: Self::ExtractedAsset,
render_device: &mut SystemParamItem<Self::Param>,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ async fn load_gltf<'a, 'b, 'c>(
let primitive_label = primitive_label(&gltf_mesh, &primitive);
let primitive_topology = get_primitive_topology(primitive.mode())?;

let mut mesh = Mesh::new(primitive_topology);
let mut mesh = Mesh::new(primitive_topology, false);
Copy link
Contributor

Choose a reason for hiding this comment

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

can we make this a flag in the GltfLoaderSettings (or make settings.load_meshes into an enum maybe)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd rather not, I think. Users should instead find the specific meshes/textures after loading, and set those ones specifically to Keep.


// Read vertex attributes
for (semantic, accessor) in primitive.attributes() {
Expand Down
10 changes: 8 additions & 2 deletions crates/bevy_render/src/mesh/mesh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ pub struct Mesh {
indices: Option<Indices>,
morph_targets: Option<Handle<Image>>,
morph_target_names: Option<Vec<String>>,
pub mutable: bool,
}

impl Mesh {
Expand Down Expand Up @@ -180,13 +181,14 @@ impl Mesh {
/// Construct a new mesh. You need to provide a [`PrimitiveTopology`] so that the
/// renderer knows how to treat the vertex data. Most of the time this will be
/// [`PrimitiveTopology::TriangleList`].
pub fn new(primitive_topology: PrimitiveTopology) -> Self {
pub fn new(primitive_topology: PrimitiveTopology, mutable: bool) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this would be nicer as a separate method instead, like with_cpu_access(), similar to the existing methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure how I feel. I wanted users to be aware of this. I'm worried if we make it a separate method they'll not realize, and then be confused why their asset is "missing" in their systems later on.

Mesh {
primitive_topology,
attributes: Default::default(),
indices: None,
morph_targets: None,
morph_target_names: None,
mutable,
}
}

Expand Down Expand Up @@ -1061,6 +1063,10 @@ impl RenderAsset for Mesh {
self.clone()
}

fn unload_after_extract(extracted_asset: &Self::ExtractedAsset) -> bool {
!extracted_asset.mutable
}

/// Converts the extracted mesh a into [`GpuMesh`].
fn prepare_asset(
mesh: Self::ExtractedAsset,
Expand Down Expand Up @@ -1237,7 +1243,7 @@ mod tests {
#[test]
#[should_panic]
fn panic_invalid_format() {
let _mesh = Mesh::new(PrimitiveTopology::TriangleList)
let _mesh = Mesh::new(PrimitiveTopology::TriangleList, false)
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, vec![[0.0, 0.0, 0.0]]);
}
}
2 changes: 1 addition & 1 deletion crates/bevy_render/src/mesh/shape/capsule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ impl From<Capsule> for Mesh {
assert_eq!(vs.len(), vert_len);
assert_eq!(tris.len(), fs_len);

Mesh::new(PrimitiveTopology::TriangleList)
Mesh::new(PrimitiveTopology::TriangleList, false)
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, vs)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, vns)
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, vts)
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/mesh/shape/cylinder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl From<Cylinder> for Mesh {
build_cap(true);
build_cap(false);

Mesh::new(PrimitiveTopology::TriangleList)
Mesh::new(PrimitiveTopology::TriangleList, false)
.with_indices(Some(Indices::U32(indices)))
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/mesh/shape/icosphere.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl TryFrom<Icosphere> for Mesh {

let indices = Indices::U32(indices);

Ok(Mesh::new(PrimitiveTopology::TriangleList)
Ok(Mesh::new(PrimitiveTopology::TriangleList, false)
.with_indices(Some(indices))
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, points)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_render/src/mesh/shape/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl From<Box> for Mesh {
20, 21, 22, 22, 23, 20, // bottom
]);

Mesh::new(PrimitiveTopology::TriangleList)
Mesh::new(PrimitiveTopology::TriangleList, false)
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs)
Expand Down Expand Up @@ -172,7 +172,7 @@ impl From<Quad> for Mesh {
let normals: Vec<_> = vertices.iter().map(|(_, n, _)| *n).collect();
let uvs: Vec<_> = vertices.iter().map(|(_, _, uv)| *uv).collect();

Mesh::new(PrimitiveTopology::TriangleList)
Mesh::new(PrimitiveTopology::TriangleList, false)
.with_indices(Some(indices))
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
Expand Down Expand Up @@ -253,7 +253,7 @@ impl From<Plane> for Mesh {
}
}

Mesh::new(PrimitiveTopology::TriangleList)
Mesh::new(PrimitiveTopology::TriangleList, false)
.with_indices(Some(Indices::U32(indices)))
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/mesh/shape/regular_polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl From<RegularPolygon> for Mesh {
indices.extend_from_slice(&[0, i + 1, i]);
}

Mesh::new(PrimitiveTopology::TriangleList)
Mesh::new(PrimitiveTopology::TriangleList, false)
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs)
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/mesh/shape/torus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl From<Torus> for Mesh {
}
}

Mesh::new(PrimitiveTopology::TriangleList)
Mesh::new(PrimitiveTopology::TriangleList, false)
.with_indices(Some(Indices::U32(indices)))
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/mesh/shape/uvsphere.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl From<UVSphere> for Mesh {
}
}

Mesh::new(PrimitiveTopology::TriangleList)
Mesh::new(PrimitiveTopology::TriangleList, false)
.with_indices(Some(Indices::U32(indices)))
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, vertices)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
Expand Down
48 changes: 32 additions & 16 deletions crates/bevy_render/src/render_asset.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{Extract, ExtractSchedule, Render, RenderApp, RenderSet};
use crate::{ExtractSchedule, MainWorld, Render, RenderApp, RenderSet};
use bevy_app::{App, Plugin};
use bevy_asset::{Asset, AssetEvent, AssetId, Assets};
use bevy_ecs::{
prelude::*,
prelude::{Commands, EventReader, IntoSystemConfigs, ResMut, Resource},
schedule::SystemConfigs,
system::{StaticSystemParam, SystemParam, SystemParamItem},
system::{StaticSystemParam, SystemParam, SystemParamItem, SystemState},
};
use bevy_utils::{thiserror::Error, HashMap, HashSet};
use std::marker::PhantomData;
Expand Down Expand Up @@ -32,8 +32,18 @@ pub trait RenderAsset: Asset {
/// Specifies all ECS data required by [`RenderAsset::prepare_asset`].
/// For convenience use the [`lifetimeless`](bevy_ecs::system::lifetimeless) [`SystemParam`].
type Param: SystemParam;

/// Converts the asset into a [`RenderAsset::ExtractedAsset`].
fn extract_asset(&self) -> Self::ExtractedAsset;

/// Whether or not to unload the asset after extracting.
///
/// Unloading the asset saves on memory, as you no longer need to keep it in RAM once
/// it's been uploaded to the GPU's VRAM. However, you can no longer modify the asset
/// once unloaded (without re-loading it), making this a bad choice for frequently modified
/// assets.
fn unload_after_extract(extracted_asset: &Self::ExtractedAsset) -> bool;

/// Prepares the `extracted asset` for the GPU by transforming it into
/// a [`RenderAsset::PreparedAsset`]. Therefore ECS data may be accessed via the `param`.
fn prepare_asset(
Expand Down Expand Up @@ -160,11 +170,11 @@ impl<A: RenderAsset> RenderAssets<A> {

/// This system extracts all created or modified assets of the corresponding [`RenderAsset`] type
/// into the "render world".
fn extract_render_asset<A: RenderAsset>(
mut commands: Commands,
mut events: Extract<EventReader<AssetEvent<A>>>,
assets: Extract<Res<Assets<A>>>,
) {
fn extract_render_asset<A: RenderAsset>(mut commands: Commands, mut main_world: ResMut<MainWorld>) {
let mut system_state: SystemState<(EventReader<AssetEvent<A>>, ResMut<Assets<A>>)> =
SystemState::new(&mut main_world);
let (mut events, mut assets) = system_state.get_mut(&mut main_world);

let mut changed_assets = HashSet::default();
let mut removed = Vec::new();
for event in events.read() {
Expand All @@ -185,7 +195,13 @@ fn extract_render_asset<A: RenderAsset>(
let mut extracted_assets = Vec::new();
for id in changed_assets.drain() {
if let Some(asset) = assets.get(id) {
extracted_assets.push((id, asset.extract_asset()));
let extracted_asset = asset.extract_asset();

if A::unload_after_extract(&extracted_asset) {
assets.remove_untracked(id);
}

extracted_assets.push((id, extracted_asset));
}
}

Expand All @@ -212,16 +228,16 @@ impl<A: RenderAsset> Default for PrepareNextFrameAssets<A> {

/// This system prepares all assets of the corresponding [`RenderAsset`] type
/// which where extracted this frame for the GPU.
pub fn prepare_assets<R: RenderAsset>(
mut extracted_assets: ResMut<ExtractedAssets<R>>,
mut render_assets: ResMut<RenderAssets<R>>,
mut prepare_next_frame: ResMut<PrepareNextFrameAssets<R>>,
param: StaticSystemParam<<R as RenderAsset>::Param>,
pub fn prepare_assets<A: RenderAsset>(
mut extracted_assets: ResMut<ExtractedAssets<A>>,
mut render_assets: ResMut<RenderAssets<A>>,
mut prepare_next_frame: ResMut<PrepareNextFrameAssets<A>>,
param: StaticSystemParam<<A as RenderAsset>::Param>,
) {
let mut param = param.into_inner();
let queued_assets = std::mem::take(&mut prepare_next_frame.assets);
for (id, extracted_asset) in queued_assets {
match R::prepare_asset(extracted_asset, &mut param) {
match A::prepare_asset(extracted_asset, &mut param) {
Ok(prepared_asset) => {
render_assets.insert(id, prepared_asset);
}
Expand All @@ -236,7 +252,7 @@ pub fn prepare_assets<R: RenderAsset>(
}

for (id, extracted_asset) in std::mem::take(&mut extracted_assets.extracted) {
match R::prepare_asset(extracted_asset, &mut param) {
match A::prepare_asset(extracted_asset, &mut param) {
Ok(prepared_asset) => {
render_assets.insert(id, prepared_asset);
}
Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_render/src/texture/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub struct Image {
/// The [`ImageSampler`] to use during rendering.
pub sampler: ImageSampler,
pub texture_view_descriptor: Option<wgpu::TextureViewDescriptor<'static>>,
pub mutable: bool,
}

/// Used in [`Image`], this determines what image sampler to use when rendering. The default setting,
Expand Down Expand Up @@ -464,6 +465,7 @@ impl Default for Image {
},
sampler: ImageSampler::Default,
texture_view_descriptor: None,
mutable: false,
}
}
}
Expand Down Expand Up @@ -813,6 +815,10 @@ impl RenderAsset for Image {
self.clone()
}

fn unload_after_extract(extracted_asset: &Self::ExtractedAsset) -> bool {
!extracted_asset.mutable
}

/// Converts the extracted image into a [`GpuImage`].
fn prepare_asset(
image: Self::ExtractedAsset,
Expand Down