Skip to content

Commit

Permalink
assets should be kept on CPU by default (bevyengine#11212)
Browse files Browse the repository at this point in the history
# Objective

- Since bevyengine#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 bevyengine#11202 

## Solution

- Keep assets on CPU memory by default
  • Loading branch information
mockersf authored and Maximetinu committed Feb 12, 2024
1 parent 9b4fbb8 commit b23cb42
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 17 deletions.
8 changes: 4 additions & 4 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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),
})
Expand Down
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 @@ -369,7 +369,7 @@ impl From<Capsule> for Mesh {

Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetPersistencePolicy::Unload,
RenderAssetPersistencePolicy::Keep,
)
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, vs)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, vns)
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 @@ -123,7 +123,7 @@ impl From<Cylinder> for Mesh {

Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetPersistencePolicy::Unload,
RenderAssetPersistencePolicy::Keep,
)
.with_indices(Some(Indices::U32(indices)))
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
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 @@ -108,7 +108,7 @@ impl TryFrom<Icosphere> for Mesh {

Ok(Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetPersistencePolicy::Unload,
RenderAssetPersistencePolicy::Keep,
)
.with_indices(Some(indices))
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, points)
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 @@ -124,7 +124,7 @@ impl From<Box> for Mesh {

Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetPersistencePolicy::Unload,
RenderAssetPersistencePolicy::Keep,
)
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
Expand Down Expand Up @@ -179,7 +179,7 @@ impl From<Quad> for Mesh {

Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetPersistencePolicy::Unload,
RenderAssetPersistencePolicy::Keep,
)
.with_indices(Some(indices))
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
Expand Down Expand Up @@ -263,7 +263,7 @@ impl From<Plane> for Mesh {

Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetPersistencePolicy::Unload,
RenderAssetPersistencePolicy::Keep,
)
.with_indices(Some(Indices::U32(indices)))
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
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 @@ -58,7 +58,7 @@ impl From<RegularPolygon> for Mesh {

Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetPersistencePolicy::Unload,
RenderAssetPersistencePolicy::Keep,
)
.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/torus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl From<Torus> for Mesh {

Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetPersistencePolicy::Unload,
RenderAssetPersistencePolicy::Keep,
)
.with_indices(Some(Indices::U32(indices)))
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
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 @@ -85,7 +85,7 @@ impl From<UVSphere> for Mesh {

Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetPersistencePolicy::Unload,
RenderAssetPersistencePolicy::Keep,
)
.with_indices(Some(Indices::U32(indices)))
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, vertices)
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/render_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/texture/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/texture/image_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_sprite/src/texture_atlas_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit b23cb42

Please sign in to comment.