bevy_asset/render_asset.rs
1use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize};
2use serde::{Deserialize, Serialize};
3
4bitflags::bitflags! {
5 /// Defines where the asset will be used.
6 ///
7 /// If an asset is set to the `RENDER_WORLD` but not the `MAIN_WORLD`, the asset data (pixel data,
8 /// mesh vertex data, etc) will be removed from the cpu-side asset once it's been extracted and prepared
9 /// in the render world. The asset will remain in the assets collection, but with only metadata.
10 ///
11 /// Unloading the asset data saves on memory, as for most cases it is no longer necessary to keep
12 /// it in RAM once it's been uploaded to the GPU's VRAM. However, this means you cannot access the
13 /// asset data from the CPU (via the `Assets<T>` resource) once unloaded (without re-loading it).
14 ///
15 /// If you never need access to the asset from the CPU past the first frame it's loaded on,
16 /// or only need very infrequent access, then set this to `RENDER_WORLD`. Otherwise, set this to
17 /// `RENDER_WORLD | MAIN_WORLD`.
18 ///
19 /// If you have an asset that doesn't actually need to end up in the render world, like an Image
20 /// that will be decoded into another Image asset, use `MAIN_WORLD` only.
21 ///
22 /// ## Platform-specific
23 ///
24 /// On Wasm, it is not possible for now to free reserved memory. To control memory usage, load assets
25 /// in sequence and unload one before loading the next. See this
26 /// [discussion about memory management](https://github.com/WebAssembly/design/issues/1397) for more
27 /// details.
28 #[repr(transparent)]
29 #[derive(Serialize, Deserialize, Hash, Clone, Copy, PartialEq, Eq, Debug, Reflect)]
30 #[reflect(opaque)]
31 #[reflect(Serialize, Deserialize, Hash, Clone, PartialEq, Debug)]
32 pub struct RenderAssetUsages: u8 {
33 /// The bit flag for the main world.
34 const MAIN_WORLD = 1 << 0;
35 /// The bit flag for the render world.
36 const RENDER_WORLD = 1 << 1;
37 }
38}
39
40impl Default for RenderAssetUsages {
41 /// Returns the default render asset usage flags:
42 /// `RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD`
43 ///
44 /// This default configuration ensures the asset persists in the main world, even after being prepared for rendering.
45 ///
46 /// If your asset does not change, consider using `RenderAssetUsages::RENDER_WORLD` exclusively. This will cause
47 /// the asset to be unloaded from the main world once it has been prepared for rendering. If the asset does not need
48 /// to reach the render world at all, use `RenderAssetUsages::MAIN_WORLD` exclusively.
49 fn default() -> Self {
50 RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD
51 }
52}