diff --git a/crates/bevy_asset/src/asset_server.rs b/crates/bevy_asset/src/asset_server.rs index c1656368b5818..2c53065174dc7 100644 --- a/crates/bevy_asset/src/asset_server.rs +++ b/crates/bevy_asset/src/asset_server.rs @@ -7,8 +7,9 @@ use crate::{ use anyhow::Result; use bevy_ecs::system::{Res, ResMut}; use bevy_log::warn; +use bevy_reflect::UniqueAssetId; use bevy_tasks::TaskPool; -use bevy_utils::{Entry, HashMap, Uuid}; +use bevy_utils::{Entry, HashMap}; use crossbeam_channel::TryRecvError; use parking_lot::{Mutex, RwLock}; use std::{path::Path, sync::Arc}; @@ -52,7 +53,7 @@ pub struct AssetServerInternal { pub(crate) asset_io: Box, pub(crate) asset_ref_counter: AssetRefCounter, pub(crate) asset_sources: Arc>>, - pub(crate) asset_lifecycles: Arc>>>, + pub(crate) asset_lifecycles: Arc>>>, loaders: RwLock>>, extension_to_loader_index: RwLock>, handle_to_path: Arc>>>, @@ -880,7 +881,7 @@ mod test { assert!(server.get_handle_path(&handle).is_none()); // invalid HandleId - let invalid_id = HandleId::new(Uuid::new_v4(), 42); + let invalid_id = HandleId::new(0, 42); assert!(server.get_handle_path(invalid_id).is_none()); // invalid AssetPath diff --git a/crates/bevy_asset/src/diagnostic/asset_count_diagnostics_plugin.rs b/crates/bevy_asset/src/diagnostic/asset_count_diagnostics_plugin.rs index 8224edd3893da..34d58e5abd6d5 100644 --- a/crates/bevy_asset/src/diagnostic/asset_count_diagnostics_plugin.rs +++ b/crates/bevy_asset/src/diagnostic/asset_count_diagnostics_plugin.rs @@ -2,6 +2,7 @@ use crate::{Asset, Assets}; use bevy_app::prelude::*; use bevy_diagnostic::{Diagnostic, DiagnosticId, Diagnostics, MAX_DIAGNOSTIC_NAME_WIDTH}; use bevy_ecs::system::{Res, ResMut}; +use bevy_reflect::Uuid; /// Adds "asset count" diagnostic to an App pub struct AssetCountDiagnosticsPlugin { @@ -25,7 +26,7 @@ impl Plugin for AssetCountDiagnosticsPlugin { impl AssetCountDiagnosticsPlugin { pub fn diagnostic_id() -> DiagnosticId { - DiagnosticId(T::TYPE_UUID) + DiagnosticId(Uuid::from_u128(T::TYPE_UUID as u128)) } pub fn setup_system(mut diagnostics: ResMut) { diff --git a/crates/bevy_asset/src/handle.rs b/crates/bevy_asset/src/handle.rs index 06fdee6907a37..91d03cff0606d 100644 --- a/crates/bevy_asset/src/handle.rs +++ b/crates/bevy_asset/src/handle.rs @@ -10,8 +10,7 @@ use crate::{ Asset, Assets, }; use bevy_ecs::{component::Component, reflect::ReflectComponent}; -use bevy_reflect::{FromReflect, Reflect, ReflectDeserialize}; -use bevy_utils::Uuid; +use bevy_reflect::{FromReflect, Reflect, ReflectDeserialize, UniqueAssetId}; use crossbeam_channel::{Receiver, Sender}; use serde::{Deserialize, Serialize}; @@ -32,7 +31,7 @@ use serde::{Deserialize, Serialize}; )] #[reflect_value(Serialize, Deserialize, PartialEq, Hash)] pub enum HandleId { - Id(Uuid, u64), + Id(UniqueAssetId, u64), AssetPathId(AssetPathId), } @@ -60,7 +59,7 @@ impl HandleId { } #[inline] - pub const fn new(type_uuid: Uuid, id: u64) -> Self { + pub const fn new(type_uuid: UniqueAssetId, id: u64) -> Self { HandleId::Id(type_uuid, id) } } @@ -306,7 +305,7 @@ pub struct HandleUntyped { } impl HandleUntyped { - pub const fn weak_from_u64(uuid: Uuid, id: u64) -> Self { + pub const fn weak_from_u64(uuid: UniqueAssetId, id: u64) -> Self { Self { id: HandleId::new(uuid, id), handle_type: HandleType::Weak, diff --git a/crates/bevy_asset/src/info.rs b/crates/bevy_asset/src/info.rs index 6005e92def189..c3d9b1014e0d9 100644 --- a/crates/bevy_asset/src/info.rs +++ b/crates/bevy_asset/src/info.rs @@ -1,5 +1,6 @@ use crate::{path::AssetPath, LabelId}; -use bevy_utils::{HashMap, HashSet, Uuid}; +use bevy_reflect::UniqueAssetId; +use bevy_utils::{HashMap, HashSet}; use serde::{Deserialize, Serialize}; use std::path::PathBuf; @@ -12,7 +13,7 @@ pub struct SourceMeta { pub struct AssetMeta { pub label: Option, pub dependencies: Vec>, - pub type_uuid: Uuid, + pub type_uuid: UniqueAssetId, } /// Info about a specific asset, such as its path and its current load state @@ -20,7 +21,7 @@ pub struct AssetMeta { pub struct SourceInfo { pub meta: Option, pub path: PathBuf, - pub asset_types: HashMap, + pub asset_types: HashMap, pub load_state: LoadState, pub committed_assets: HashSet, pub version: usize, @@ -33,7 +34,7 @@ impl SourceInfo { }) } - pub fn get_asset_type(&self, label_id: LabelId) -> Option { + pub fn get_asset_type(&self, label_id: LabelId) -> Option { self.asset_types.get(&label_id).cloned() } } diff --git a/crates/bevy_pbr/src/material.rs b/crates/bevy_pbr/src/material.rs index d01e4f79a557c..9cfbc5b828e3c 100644 --- a/crates/bevy_pbr/src/material.rs +++ b/crates/bevy_pbr/src/material.rs @@ -290,7 +290,7 @@ impl EntityRenderCommand for SetMaterial pass: &mut TrackedRenderPass<'w>, ) -> RenderCommandResult { let material_handle = query.get(item).unwrap(); - let material = materials.into_inner().get(material_handle).unwrap(); + let material = materials.into_inner().get(&material_handle.id).unwrap(); pass.set_bind_group( I, M::bind_group(material), @@ -344,8 +344,8 @@ pub fn queue_material_meshes( if let Ok((material_handle, mesh_handle, mesh_uniform)) = material_meshes.get(*visible_entity) { - if let Some(material) = render_materials.get(material_handle) { - if let Some(mesh) = render_meshes.get(mesh_handle) { + if let Some(material) = render_materials.get(&material_handle.id) { + if let Some(mesh) = render_meshes.get(&mesh_handle.id) { let mut mesh_key = MeshPipelineKey::from_primitive_topology(mesh.primitive_topology) | msaa_key; diff --git a/crates/bevy_pbr/src/render/light.rs b/crates/bevy_pbr/src/render/light.rs index 562c9b6d2031c..5f714d27f1102 100644 --- a/crates/bevy_pbr/src/render/light.rs +++ b/crates/bevy_pbr/src/render/light.rs @@ -1070,7 +1070,7 @@ pub fn queue_shadows( // so no meshes will be queued for entity in visible_entities.iter().copied() { if let Ok(mesh_handle) = casting_meshes.get(entity) { - if let Some(mesh) = render_meshes.get(mesh_handle) { + if let Some(mesh) = render_meshes.get(&mesh_handle.id) { let key = ShadowPipelineKey::from_primitive_topology(mesh.primitive_topology); let pipeline_id = pipelines.specialize( diff --git a/crates/bevy_pbr/src/render/mesh.rs b/crates/bevy_pbr/src/render/mesh.rs index 4c34a462eb0f7..aea8c93bfface 100644 --- a/crates/bevy_pbr/src/render/mesh.rs +++ b/crates/bevy_pbr/src/render/mesh.rs @@ -349,7 +349,7 @@ impl MeshPipeline { handle_option: &Option>, ) -> Option<(&'a TextureView, &'a Sampler)> { if let Some(handle) = handle_option { - let gpu_image = gpu_images.get(handle)?; + let gpu_image = gpu_images.get(&handle.id)?; Some((&gpu_image.texture_view, &gpu_image.sampler)) } else { Some(( @@ -669,7 +669,7 @@ impl EntityRenderCommand for DrawMesh { pass: &mut TrackedRenderPass<'w>, ) -> RenderCommandResult { let mesh_handle = mesh_query.get(item).unwrap(); - if let Some(gpu_mesh) = meshes.into_inner().get(mesh_handle) { + if let Some(gpu_mesh) = meshes.into_inner().get(&mesh_handle.id) { pass.set_vertex_buffer(0, gpu_mesh.vertex_buffer.slice(..)); match &gpu_mesh.buffer_info { GpuBufferInfo::Indexed { diff --git a/crates/bevy_pbr/src/wireframe.rs b/crates/bevy_pbr/src/wireframe.rs index 7a65ae267ef99..714ae164520b6 100644 --- a/crates/bevy_pbr/src/wireframe.rs +++ b/crates/bevy_pbr/src/wireframe.rs @@ -129,7 +129,7 @@ fn queue_wireframes( let add_render_phase = |(entity, mesh_handle, mesh_uniform): (Entity, &Handle, &MeshUniform)| { - if let Some(mesh) = render_meshes.get(mesh_handle) { + if let Some(mesh) = render_meshes.get(&mesh_handle.id) { let key = msaa_key | MeshPipelineKey::from_primitive_topology(mesh.primitive_topology); let pipeline_id = specialized_pipelines.specialize( diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/type_uuid.rs b/crates/bevy_reflect/bevy_reflect_derive/src/type_uuid.rs index 77c95b4a3debd..e3fde14a13dde 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/type_uuid.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/type_uuid.rs @@ -1,5 +1,7 @@ extern crate proc_macro; +use std::{collections::hash_map::DefaultHasher, hash::Hasher}; + use bevy_macro_utils::BevyManifest; use quote::{quote, ToTokens}; use syn::*; @@ -50,17 +52,14 @@ pub fn type_uuid_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStre let uuid = uuid.expect("No `#[uuid = \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"` attribute found."); - let bytes = uuid - .as_bytes() - .iter() - .map(|byte| format!("{:#X}", byte)) - .map(|byte_str| syn::parse_str::(&byte_str).unwrap()); + + let mut s = DefaultHasher::new(); + std::hash::Hash::hash(&uuid, &mut s); + let unique_id = s.finish(); let gen = quote! { impl #bevy_reflect_path::TypeUuid for #name { - const TYPE_UUID: #bevy_reflect_path::Uuid = #bevy_reflect_path::Uuid::from_bytes([ - #( #bytes ),* - ]); + const TYPE_UUID: #bevy_reflect_path::UniqueAssetId = #unique_id; } }; gen.into() diff --git a/crates/bevy_reflect/src/type_uuid.rs b/crates/bevy_reflect/src/type_uuid.rs index 545ab97bb594b..59cb135afb655 100644 --- a/crates/bevy_reflect/src/type_uuid.rs +++ b/crates/bevy_reflect/src/type_uuid.rs @@ -1,14 +1,16 @@ pub use bevy_reflect_derive::TypeUuid; pub use bevy_utils::Uuid; +pub type UniqueAssetId = u64; + /// A trait for types with a statically associated UUID. pub trait TypeUuid { - const TYPE_UUID: Uuid; + const TYPE_UUID: UniqueAssetId; } /// A trait for types with an associated UUID. pub trait TypeUuidDynamic { - fn type_uuid(&self) -> Uuid; + fn type_uuid(&self) -> UniqueAssetId; fn type_name(&self) -> &'static str; } @@ -17,7 +19,7 @@ where T: TypeUuid, { /// Returns the UUID associated with this value's type. - fn type_uuid(&self) -> Uuid { + fn type_uuid(&self) -> UniqueAssetId { Self::TYPE_UUID } diff --git a/crates/bevy_render/src/camera/camera.rs b/crates/bevy_render/src/camera/camera.rs index 07e748fe8211f..22ebe42d75671 100644 --- a/crates/bevy_render/src/camera/camera.rs +++ b/crates/bevy_render/src/camera/camera.rs @@ -57,9 +57,9 @@ impl RenderTarget { RenderTarget::Window(window_id) => windows .get(window_id) .and_then(|window| window.swap_chain_texture.as_ref()), - RenderTarget::Image(image_handle) => { - images.get(image_handle).map(|image| &image.texture_view) - } + RenderTarget::Image(image_handle) => images + .get(&image_handle.id) + .map(|image| &image.texture_view), } } pub fn get_physical_size(&self, windows: &Windows, images: &Assets) -> Option { diff --git a/crates/bevy_render/src/render_asset.rs b/crates/bevy_render/src/render_asset.rs index 12861ae6f4e64..d86a72974f6bc 100644 --- a/crates/bevy_render/src/render_asset.rs +++ b/crates/bevy_render/src/render_asset.rs @@ -1,6 +1,6 @@ use crate::{RenderApp, RenderStage}; use bevy_app::{App, Plugin}; -use bevy_asset::{Asset, AssetEvent, Assets, Handle}; +use bevy_asset::{Asset, AssetEvent, Assets, HandleId}; use bevy_ecs::{ prelude::*, system::{lifetimeless::*, RunSystem, SystemParam, SystemParamItem}, @@ -68,8 +68,8 @@ impl Plugin for RenderAssetPlugin { /// Temporarily stores the extracted and removed assets of the current frame. pub struct ExtractedAssets { - extracted: Vec<(Handle, A::ExtractedAsset)>, - removed: Vec>, + extracted: Vec<(HandleId, A::ExtractedAsset)>, + removed: Vec, } impl Default for ExtractedAssets { @@ -83,7 +83,7 @@ impl Default for ExtractedAssets { /// Stores all GPU representations ([`RenderAsset::PreparedAssets`](RenderAsset::PreparedAsset)) /// of [`RenderAssets`](RenderAsset) as long as they exist. -pub type RenderAssets = HashMap, ::PreparedAsset>; +pub type RenderAssets = HashMap::PreparedAsset>; /// This system extracts all crated or modified assets of the corresponding [`RenderAsset`] type /// into the "render world". @@ -97,14 +97,14 @@ fn extract_render_asset( for event in events.iter() { match event { AssetEvent::Created { handle } => { - changed_assets.insert(handle); + changed_assets.insert(handle.id); } AssetEvent::Modified { handle } => { - changed_assets.insert(handle); + changed_assets.insert(handle.id); } AssetEvent::Removed { handle } => { - changed_assets.remove(handle); - removed.push(handle.clone_weak()); + changed_assets.remove(&handle.id); + removed.push(handle.id); } } } @@ -112,11 +112,11 @@ fn extract_render_asset( let mut extracted_assets = Vec::new(); for handle in changed_assets.drain() { if let Some(asset) = assets.get(handle) { - extracted_assets.push((handle.clone_weak(), asset.extract_asset())); + extracted_assets.push((handle, asset.extract_asset())); } } - commands.insert_resource(ExtractedAssets { + commands.insert_resource(ExtractedAssets:: { extracted: extracted_assets, removed, }); @@ -133,7 +133,7 @@ pub type RenderAssetParams = ( // TODO: consider storing inside system? /// All assets that should be prepared next frame. pub struct PrepareNextFrameAssets { - assets: Vec<(Handle, A::ExtractedAsset)>, + assets: Vec<(HandleId, A::ExtractedAsset)>, } impl Default for PrepareNextFrameAssets { diff --git a/crates/bevy_sprite/src/mesh2d/material.rs b/crates/bevy_sprite/src/mesh2d/material.rs index 630af30155cb0..ba844c0945480 100644 --- a/crates/bevy_sprite/src/mesh2d/material.rs +++ b/crates/bevy_sprite/src/mesh2d/material.rs @@ -276,7 +276,7 @@ impl EntityRenderCommand pass: &mut TrackedRenderPass<'w>, ) -> RenderCommandResult { let material2d_handle = query.get(item).unwrap(); - let material2d = materials.into_inner().get(material2d_handle).unwrap(); + let material2d = materials.into_inner().get(&material2d_handle.id).unwrap(); pass.set_bind_group( I, M::bind_group(material2d), @@ -313,8 +313,8 @@ pub fn queue_material2d_meshes( if let Ok((material2d_handle, mesh2d_handle, mesh2d_uniform)) = material2d_meshes.get(*visible_entity) { - if let Some(material2d) = render_materials.get(material2d_handle) { - if let Some(mesh) = render_meshes.get(&mesh2d_handle.0) { + if let Some(material2d) = render_materials.get(&material2d_handle.id) { + if let Some(mesh) = render_meshes.get(&mesh2d_handle.0.id) { let mesh_key = msaa_key | Mesh2dPipelineKey::from_primitive_topology(mesh.primitive_topology); diff --git a/crates/bevy_sprite/src/mesh2d/mesh.rs b/crates/bevy_sprite/src/mesh2d/mesh.rs index 5f9a54c565325..04cebd01926f9 100644 --- a/crates/bevy_sprite/src/mesh2d/mesh.rs +++ b/crates/bevy_sprite/src/mesh2d/mesh.rs @@ -214,7 +214,7 @@ impl Mesh2dPipeline { handle_option: &Option>, ) -> Option<(&'a TextureView, &'a Sampler)> { if let Some(handle) = handle_option { - let gpu_image = gpu_images.get(handle)?; + let gpu_image = gpu_images.get(&handle.id)?; Some((&gpu_image.texture_view, &gpu_image.sampler)) } else { Some(( @@ -441,7 +441,7 @@ impl EntityRenderCommand for DrawMesh2d { pass: &mut TrackedRenderPass<'w>, ) -> RenderCommandResult { let mesh_handle = &mesh2d_query.get(item).unwrap().0; - if let Some(gpu_mesh) = meshes.into_inner().get(mesh_handle) { + if let Some(gpu_mesh) = meshes.into_inner().get(&mesh_handle.id) { pass.set_vertex_buffer(0, gpu_mesh.vertex_buffer.slice(..)); match &gpu_mesh.buffer_info { GpuBufferInfo::Indexed { diff --git a/crates/bevy_sprite/src/render/mod.rs b/crates/bevy_sprite/src/render/mod.rs index 1243ded649a55..69df0eb74f0f7 100644 --- a/crates/bevy_sprite/src/render/mod.rs +++ b/crates/bevy_sprite/src/render/mod.rs @@ -12,7 +12,6 @@ use bevy_ecs::{ system::{lifetimeless::*, SystemParamItem}, }; use bevy_math::{const_vec2, Vec2}; -use bevy_reflect::Uuid; use bevy_render::{ color::Color, render_asset::RenderAssets, @@ -409,7 +408,7 @@ pub fn queue_sprites( // Impossible starting values that will be replaced on the first iteration let mut current_batch = SpriteBatch { - image_handle_id: HandleId::Id(Uuid::nil(), u64::MAX), + image_handle_id: HandleId::Id(u64::MAX, u64::MAX), colored: false, }; let mut current_batch_entity = Entity::from_raw(u32::MAX); @@ -426,9 +425,7 @@ pub fn queue_sprites( }; if new_batch != current_batch { // Set-up a new possible batch - if let Some(gpu_image) = - gpu_images.get(&Handle::weak(new_batch.image_handle_id)) - { + if let Some(gpu_image) = gpu_images.get(&new_batch.image_handle_id) { current_batch = new_batch; current_image_size = Vec2::new(gpu_image.size.width, gpu_image.size.height); current_batch_entity = commands.spawn_bundle((current_batch,)).id(); diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index 3644d1037f197..77ade3cc3f35b 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -448,7 +448,7 @@ pub fn queue_uinodes( .values .entry(batch.image.clone_weak()) .or_insert_with(|| { - let gpu_image = gpu_images.get(&batch.image).unwrap(); + let gpu_image = gpu_images.get(&batch.image.id).unwrap(); render_device.create_bind_group(&BindGroupDescriptor { entries: &[ BindGroupEntry { diff --git a/examples/2d/mesh2d_manual.rs b/examples/2d/mesh2d_manual.rs index 6fec365bb617f..3da1f4532e00f 100644 --- a/examples/2d/mesh2d_manual.rs +++ b/examples/2d/mesh2d_manual.rs @@ -333,7 +333,7 @@ pub fn queue_colored_mesh2d( if let Ok((mesh2d_handle, mesh2d_uniform)) = colored_mesh2d.get(*visible_entity) { // Get our specialized pipeline let mut mesh2d_key = mesh_key; - if let Some(mesh) = render_meshes.get(&mesh2d_handle.0) { + if let Some(mesh) = render_meshes.get(&mesh2d_handle.0.id) { mesh2d_key |= Mesh2dPipelineKey::from_primitive_topology(mesh.primitive_topology); } diff --git a/examples/shader/animate_shader.rs b/examples/shader/animate_shader.rs index b4266bcc8f00d..3ecfc014e2a28 100644 --- a/examples/shader/animate_shader.rs +++ b/examples/shader/animate_shader.rs @@ -115,7 +115,7 @@ fn queue_custom( let view_matrix = view.transform.compute_matrix(); let view_row_2 = view_matrix.row(2); for (entity, mesh_uniform, mesh_handle) in material_meshes.iter() { - if let Some(mesh) = render_meshes.get(mesh_handle) { + if let Some(mesh) = render_meshes.get(&mesh_handle.id) { let pipeline = pipelines .specialize(&mut pipeline_cache, &custom_pipeline, key, &mesh.layout) .unwrap(); diff --git a/examples/shader/compute_shader_game_of_life.rs b/examples/shader/compute_shader_game_of_life.rs index 9ad3db0b5bfe1..01f44435d3216 100644 --- a/examples/shader/compute_shader_game_of_life.rs +++ b/examples/shader/compute_shader_game_of_life.rs @@ -87,7 +87,7 @@ fn queue_bind_group( game_of_life_image: Res, render_device: Res, ) { - let view = &gpu_images[&game_of_life_image.0]; + let view = &gpu_images[&game_of_life_image.0.id]; let bind_group = render_device.create_bind_group(&BindGroupDescriptor { label: None, layout: &pipeline.texture_bind_group_layout, diff --git a/examples/shader/shader_defs.rs b/examples/shader/shader_defs.rs index 187ec8e51d49d..58df4ca3b1e34 100644 --- a/examples/shader/shader_defs.rs +++ b/examples/shader/shader_defs.rs @@ -152,7 +152,7 @@ fn queue_custom( let view_matrix = view.transform.compute_matrix(); let view_row_2 = view_matrix.row(2); for (entity, mesh_handle, mesh_uniform, is_red) in material_meshes.iter() { - if let Some(mesh) = render_meshes.get(mesh_handle) { + if let Some(mesh) = render_meshes.get(&mesh_handle.id) { let key = msaa_key | MeshPipelineKey::from_primitive_topology(mesh.primitive_topology); let pipeline = pipelines diff --git a/examples/shader/shader_instancing.rs b/examples/shader/shader_instancing.rs index f0544c0234c5c..996f1506efeb1 100644 --- a/examples/shader/shader_instancing.rs +++ b/examples/shader/shader_instancing.rs @@ -120,7 +120,7 @@ fn queue_custom( let view_matrix = view.transform.compute_matrix(); let view_row_2 = view_matrix.row(2); for (entity, mesh_uniform, mesh_handle) in material_meshes.iter() { - if let Some(mesh) = meshes.get(mesh_handle) { + if let Some(mesh) = meshes.get(&mesh_handle.id) { let key = msaa_key | MeshPipelineKey::from_primitive_topology(mesh.primitive_topology); let pipeline = pipelines @@ -242,7 +242,7 @@ impl EntityRenderCommand for DrawMeshInstanced { let mesh_handle = mesh_query.get(item).unwrap(); let instance_buffer = instance_buffer_query.get(item).unwrap(); - let gpu_mesh = match meshes.into_inner().get(mesh_handle) { + let gpu_mesh = match meshes.into_inner().get(&mesh_handle.id) { Some(gpu_mesh) => gpu_mesh, None => return RenderCommandResult::Failure, };