From 7e6d817ce47f88157f64a2943feb2d3d73dcce8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Fri, 5 Jan 2024 06:53:47 +0100 Subject: [PATCH] assets should be kept on CPU by default (#11212) # Objective - Since #10520, assets are unloaded from RAM by default. This breaks a number of scenario: - using `load_folder` - loading a gltf, then going through its mesh to transform them / compute a collider / ... - any assets/subassets scenario should be `Keep` as you can't know what the user will do with the assets - android suspension, where GPU memory is unloaded - Alternative to #11202 ## Solution - Keep assets on CPU memory by default --- crates/bevy_gltf/src/loader.rs | 8 ++++---- crates/bevy_render/src/mesh/shape/capsule.rs | 2 +- crates/bevy_render/src/mesh/shape/cylinder.rs | 2 +- crates/bevy_render/src/mesh/shape/icosphere.rs | 2 +- crates/bevy_render/src/mesh/shape/mod.rs | 6 +++--- crates/bevy_render/src/mesh/shape/regular_polygon.rs | 2 +- crates/bevy_render/src/mesh/shape/torus.rs | 2 +- crates/bevy_render/src/mesh/shape/uvsphere.rs | 2 +- crates/bevy_render/src/render_asset.rs | 2 +- crates/bevy_render/src/texture/image.rs | 2 +- crates/bevy_render/src/texture/image_loader.rs | 2 +- crates/bevy_sprite/src/texture_atlas_builder.rs | 2 +- 12 files changed, 17 insertions(+), 17 deletions(-) diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index 8864e5383c45e..178a4d0285eb1 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -353,7 +353,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, RenderAssetPersistencePolicy::Unload); + let mut mesh = Mesh::new(primitive_topology, RenderAssetPersistencePolicy::Keep); // Read vertex attributes for (semantic, accessor) in primitive.attributes() { @@ -397,7 +397,7 @@ async fn load_gltf<'a, 'b, 'c>( let morph_target_image = MorphTargetImage::new( morph_target_reader.map(PrimitiveMorphAttributesIter), mesh.count_vertices(), - RenderAssetPersistencePolicy::Unload, + RenderAssetPersistencePolicy::Keep, )?; let handle = load_context.add_labeled_asset(morph_targets_label, morph_target_image.0); @@ -688,7 +688,7 @@ async fn load_image<'a, 'b>( supported_compressed_formats, is_srgb, ImageSampler::Descriptor(sampler_descriptor), - RenderAssetPersistencePolicy::Unload, + RenderAssetPersistencePolicy::Keep, )?; Ok(ImageOrPath::Image { image, @@ -710,7 +710,7 @@ async fn load_image<'a, 'b>( supported_compressed_formats, is_srgb, ImageSampler::Descriptor(sampler_descriptor), - RenderAssetPersistencePolicy::Unload, + RenderAssetPersistencePolicy::Keep, )?, label: texture_label(&gltf_texture), }) diff --git a/crates/bevy_render/src/mesh/shape/capsule.rs b/crates/bevy_render/src/mesh/shape/capsule.rs index 00dc9fd28542c..865997a9b624f 100644 --- a/crates/bevy_render/src/mesh/shape/capsule.rs +++ b/crates/bevy_render/src/mesh/shape/capsule.rs @@ -369,7 +369,7 @@ impl From for Mesh { Mesh::new( PrimitiveTopology::TriangleList, - RenderAssetPersistencePolicy::Unload, + RenderAssetPersistencePolicy::Keep, ) .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, vs) .with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, vns) diff --git a/crates/bevy_render/src/mesh/shape/cylinder.rs b/crates/bevy_render/src/mesh/shape/cylinder.rs index 7f959628bc796..be08ea22ede91 100644 --- a/crates/bevy_render/src/mesh/shape/cylinder.rs +++ b/crates/bevy_render/src/mesh/shape/cylinder.rs @@ -123,7 +123,7 @@ impl From for Mesh { Mesh::new( PrimitiveTopology::TriangleList, - RenderAssetPersistencePolicy::Unload, + RenderAssetPersistencePolicy::Keep, ) .with_indices(Some(Indices::U32(indices))) .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions) diff --git a/crates/bevy_render/src/mesh/shape/icosphere.rs b/crates/bevy_render/src/mesh/shape/icosphere.rs index 0ee936d218b2e..af8158d5bd19a 100644 --- a/crates/bevy_render/src/mesh/shape/icosphere.rs +++ b/crates/bevy_render/src/mesh/shape/icosphere.rs @@ -108,7 +108,7 @@ impl TryFrom for Mesh { Ok(Mesh::new( PrimitiveTopology::TriangleList, - RenderAssetPersistencePolicy::Unload, + RenderAssetPersistencePolicy::Keep, ) .with_indices(Some(indices)) .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, points) diff --git a/crates/bevy_render/src/mesh/shape/mod.rs b/crates/bevy_render/src/mesh/shape/mod.rs index 0c421c41a2a9a..f7c7ce730959a 100644 --- a/crates/bevy_render/src/mesh/shape/mod.rs +++ b/crates/bevy_render/src/mesh/shape/mod.rs @@ -124,7 +124,7 @@ impl From for Mesh { Mesh::new( PrimitiveTopology::TriangleList, - RenderAssetPersistencePolicy::Unload, + RenderAssetPersistencePolicy::Keep, ) .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions) .with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals) @@ -179,7 +179,7 @@ impl From for Mesh { Mesh::new( PrimitiveTopology::TriangleList, - RenderAssetPersistencePolicy::Unload, + RenderAssetPersistencePolicy::Keep, ) .with_indices(Some(indices)) .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions) @@ -263,7 +263,7 @@ impl From for Mesh { Mesh::new( PrimitiveTopology::TriangleList, - RenderAssetPersistencePolicy::Unload, + RenderAssetPersistencePolicy::Keep, ) .with_indices(Some(Indices::U32(indices))) .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions) diff --git a/crates/bevy_render/src/mesh/shape/regular_polygon.rs b/crates/bevy_render/src/mesh/shape/regular_polygon.rs index f7f169429c696..d6228b0aa99b8 100644 --- a/crates/bevy_render/src/mesh/shape/regular_polygon.rs +++ b/crates/bevy_render/src/mesh/shape/regular_polygon.rs @@ -58,7 +58,7 @@ impl From for Mesh { Mesh::new( PrimitiveTopology::TriangleList, - RenderAssetPersistencePolicy::Unload, + RenderAssetPersistencePolicy::Keep, ) .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions) .with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals) diff --git a/crates/bevy_render/src/mesh/shape/torus.rs b/crates/bevy_render/src/mesh/shape/torus.rs index dc3664f626bbc..19a58a259f68f 100644 --- a/crates/bevy_render/src/mesh/shape/torus.rs +++ b/crates/bevy_render/src/mesh/shape/torus.rs @@ -89,7 +89,7 @@ impl From for Mesh { Mesh::new( PrimitiveTopology::TriangleList, - RenderAssetPersistencePolicy::Unload, + RenderAssetPersistencePolicy::Keep, ) .with_indices(Some(Indices::U32(indices))) .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions) diff --git a/crates/bevy_render/src/mesh/shape/uvsphere.rs b/crates/bevy_render/src/mesh/shape/uvsphere.rs index dd3e29fd0f19d..c596e1823a359 100644 --- a/crates/bevy_render/src/mesh/shape/uvsphere.rs +++ b/crates/bevy_render/src/mesh/shape/uvsphere.rs @@ -85,7 +85,7 @@ impl From for Mesh { Mesh::new( PrimitiveTopology::TriangleList, - RenderAssetPersistencePolicy::Unload, + RenderAssetPersistencePolicy::Keep, ) .with_indices(Some(Indices::U32(indices))) .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, vertices) diff --git a/crates/bevy_render/src/render_asset.rs b/crates/bevy_render/src/render_asset.rs index da0adac338dac..2ea2caaf5e779 100644 --- a/crates/bevy_render/src/render_asset.rs +++ b/crates/bevy_render/src/render_asset.rs @@ -55,8 +55,8 @@ pub trait RenderAsset: Asset + Clone { /// or only need very infrequent access, then set this to Unload. Otherwise, set this to Keep. #[derive(Reflect, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Default, Debug)] pub enum RenderAssetPersistencePolicy { - #[default] Unload, + #[default] Keep, } diff --git a/crates/bevy_render/src/texture/image.rs b/crates/bevy_render/src/texture/image.rs index b7fe241429749..02566083e8556 100644 --- a/crates/bevy_render/src/texture/image.rs +++ b/crates/bevy_render/src/texture/image.rs @@ -465,7 +465,7 @@ impl Default for Image { }, sampler: ImageSampler::Default, texture_view_descriptor: None, - cpu_persistent_access: RenderAssetPersistencePolicy::Unload, + cpu_persistent_access: RenderAssetPersistencePolicy::Keep, } } } diff --git a/crates/bevy_render/src/texture/image_loader.rs b/crates/bevy_render/src/texture/image_loader.rs index 284b085896a02..68e68803ad6cd 100644 --- a/crates/bevy_render/src/texture/image_loader.rs +++ b/crates/bevy_render/src/texture/image_loader.rs @@ -67,7 +67,7 @@ impl Default for ImageLoaderSettings { format: ImageFormatSetting::default(), is_srgb: true, sampler: ImageSampler::Default, - cpu_persistent_access: RenderAssetPersistencePolicy::Unload, + cpu_persistent_access: RenderAssetPersistencePolicy::Keep, } } } diff --git a/crates/bevy_sprite/src/texture_atlas_builder.rs b/crates/bevy_sprite/src/texture_atlas_builder.rs index 07a5e911e69f9..3fd655642ac97 100644 --- a/crates/bevy_sprite/src/texture_atlas_builder.rs +++ b/crates/bevy_sprite/src/texture_atlas_builder.rs @@ -209,7 +209,7 @@ impl TextureAtlasBuilder { self.format.pixel_size() * (current_width * current_height) as usize ], self.format, - RenderAssetPersistencePolicy::Unload, + RenderAssetPersistencePolicy::Keep, ); Some(rect_placements) }