mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 20:53:53 +00:00
44424391fe
# Objective - No point in keeping Meshes/Images in RAM once they're going to be sent to the GPU, and kept in VRAM. This saves a _significant_ amount of memory (several GBs) on scenes like bistro. - References - https://github.com/bevyengine/bevy/pull/1782 - https://github.com/bevyengine/bevy/pull/8624 ## Solution - Augment RenderAsset with the capability to unload the underlying asset after extracting to the render world. - Mesh/Image now have a cpu_persistent_access field. If this field is RenderAssetPersistencePolicy::Unload, the asset will be unloaded from Assets<T>. - A new AssetEvent is sent upon dropping the last strong handle for the asset, which signals to the RenderAsset to remove the GPU version of the asset. --- ## Changelog - Added `AssetEvent::NoLongerUsed` and `AssetEvent::is_no_longer_used()`. This event is sent when the last strong handle of an asset is dropped. - Rewrote the API for `RenderAsset` to allow for unloading the asset data from the CPU. - Added `RenderAssetPersistencePolicy`. - Added `Mesh::cpu_persistent_access` for memory savings when the asset is not needed except for on the GPU. - Added `Image::cpu_persistent_access` for memory savings when the asset is not needed except for on the GPU. - Added `ImageLoaderSettings::cpu_persistent_access`. - Added `ExrTextureLoaderSettings`. - Added `HdrTextureLoaderSettings`. ## Migration Guide - Asset loaders (GLTF, etc) now load meshes and textures without `cpu_persistent_access`. These assets will be removed from `Assets<Mesh>` and `Assets<Image>` once `RenderAssets<Mesh>` and `RenderAssets<Image>` contain the GPU versions of these assets, in order to reduce memory usage. If you require access to the asset data from the CPU in future frames after the GLTF asset has been loaded, modify all dependent `Mesh` and `Image` assets and set `cpu_persistent_access` to `RenderAssetPersistencePolicy::Keep`. - `Mesh` now requires a new `cpu_persistent_access` field. Set it to `RenderAssetPersistencePolicy::Keep` to mimic the previous behavior. - `Image` now requires a new `cpu_persistent_access` field. Set it to `RenderAssetPersistencePolicy::Keep` to mimic the previous behavior. - `MorphTargetImage::new()` now requires a new `cpu_persistent_access` parameter. Set it to `RenderAssetPersistencePolicy::Keep` to mimic the previous behavior. - `DynamicTextureAtlasBuilder::add_texture()` now requires that the `TextureAtlas` you pass has an `Image` with `cpu_persistent_access: RenderAssetPersistencePolicy::Keep`. Ensure you construct the image properly for the texture atlas. - The `RenderAsset` trait has significantly changed, and requires adapting your existing implementations. - The trait now requires `Clone`. - The `ExtractedAsset` associated type has been removed (the type itself is now extracted). - The signature of `prepare_asset()` is slightly different - A new `persistence_policy()` method is now required (return RenderAssetPersistencePolicy::Unload to match the previous behavior). - Match on the new `NoLongerUsed` variant for exhaustive matches of `AssetEvent`.
125 lines
3.7 KiB
Rust
125 lines
3.7 KiB
Rust
//! This example demonstrates the built-in 3d shapes in Bevy.
|
|
//! The scene includes a patterned texture and a rotation for visualizing the normals and UVs.
|
|
|
|
use std::f32::consts::PI;
|
|
|
|
use bevy::{
|
|
prelude::*,
|
|
render::{
|
|
render_asset::RenderAssetPersistencePolicy,
|
|
render_resource::{Extent3d, TextureDimension, TextureFormat},
|
|
},
|
|
};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
|
|
.add_systems(Startup, setup)
|
|
.add_systems(Update, rotate)
|
|
.run();
|
|
}
|
|
|
|
/// A marker component for our shapes so we can query them separately from the ground plane
|
|
#[derive(Component)]
|
|
struct Shape;
|
|
|
|
const X_EXTENT: f32 = 14.5;
|
|
|
|
fn setup(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut images: ResMut<Assets<Image>>,
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
) {
|
|
let debug_material = materials.add(StandardMaterial {
|
|
base_color_texture: Some(images.add(uv_debug_texture())),
|
|
..default()
|
|
});
|
|
|
|
let shapes = [
|
|
meshes.add(shape::Cube::default().into()),
|
|
meshes.add(shape::Box::default().into()),
|
|
meshes.add(shape::Capsule::default().into()),
|
|
meshes.add(shape::Torus::default().into()),
|
|
meshes.add(shape::Cylinder::default().into()),
|
|
meshes.add(shape::Icosphere::default().try_into().unwrap()),
|
|
meshes.add(shape::UVSphere::default().into()),
|
|
];
|
|
|
|
let num_shapes = shapes.len();
|
|
|
|
for (i, shape) in shapes.into_iter().enumerate() {
|
|
commands.spawn((
|
|
PbrBundle {
|
|
mesh: shape,
|
|
material: debug_material.clone(),
|
|
transform: Transform::from_xyz(
|
|
-X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * X_EXTENT,
|
|
2.0,
|
|
0.0,
|
|
)
|
|
.with_rotation(Quat::from_rotation_x(-PI / 4.)),
|
|
..default()
|
|
},
|
|
Shape,
|
|
));
|
|
}
|
|
|
|
commands.spawn(PointLightBundle {
|
|
point_light: PointLight {
|
|
intensity: 9000.0,
|
|
range: 100.,
|
|
shadows_enabled: true,
|
|
..default()
|
|
},
|
|
transform: Transform::from_xyz(8.0, 16.0, 8.0),
|
|
..default()
|
|
});
|
|
|
|
// ground plane
|
|
commands.spawn(PbrBundle {
|
|
mesh: meshes.add(shape::Plane::from_size(50.0).into()),
|
|
material: materials.add(Color::SILVER.into()),
|
|
..default()
|
|
});
|
|
|
|
commands.spawn(Camera3dBundle {
|
|
transform: Transform::from_xyz(0.0, 6., 12.0).looking_at(Vec3::new(0., 1., 0.), Vec3::Y),
|
|
..default()
|
|
});
|
|
}
|
|
|
|
fn rotate(mut query: Query<&mut Transform, With<Shape>>, time: Res<Time>) {
|
|
for mut transform in &mut query {
|
|
transform.rotate_y(time.delta_seconds() / 2.);
|
|
}
|
|
}
|
|
|
|
/// Creates a colorful test pattern
|
|
fn uv_debug_texture() -> Image {
|
|
const TEXTURE_SIZE: usize = 8;
|
|
|
|
let mut palette: [u8; 32] = [
|
|
255, 102, 159, 255, 255, 159, 102, 255, 236, 255, 102, 255, 121, 255, 102, 255, 102, 255,
|
|
198, 255, 102, 198, 255, 255, 121, 102, 255, 255, 236, 102, 255, 255,
|
|
];
|
|
|
|
let mut texture_data = [0; TEXTURE_SIZE * TEXTURE_SIZE * 4];
|
|
for y in 0..TEXTURE_SIZE {
|
|
let offset = TEXTURE_SIZE * y * 4;
|
|
texture_data[offset..(offset + TEXTURE_SIZE * 4)].copy_from_slice(&palette);
|
|
palette.rotate_right(4);
|
|
}
|
|
|
|
Image::new_fill(
|
|
Extent3d {
|
|
width: TEXTURE_SIZE as u32,
|
|
height: TEXTURE_SIZE as u32,
|
|
depth_or_array_layers: 1,
|
|
},
|
|
TextureDimension::D2,
|
|
&texture_data,
|
|
TextureFormat::Rgba8UnormSrgb,
|
|
RenderAssetPersistencePolicy::Unload,
|
|
)
|
|
}
|