diff --git a/examples/testbed/src/free_fly_camera.rs b/examples/testbed/src/free_fly_camera.rs index aa3dc4f..d1c0ddc 100644 --- a/examples/testbed/src/free_fly_camera.rs +++ b/examples/testbed/src/free_fly_camera.rs @@ -29,6 +29,7 @@ impl Default for FreeFlyCamera { } impl FreeFlyCamera { + #[allow(dead_code)] pub fn new(speed: f32, slow_speed_factor: f32, look_speed: f32, mouse_sensitivity: f32, look_with_keys: bool) -> Self { Self { speed, @@ -83,8 +84,8 @@ impl System for FreeFlyCameraPlugin { } } - for (mut cam, mut fly) in world - .view_iter::<(&mut CameraComponent, &mut FreeFlyCamera)>() + for (mut cam, fly) in world + .view_iter::<(&mut CameraComponent, &FreeFlyCamera)>() { let forward = cam.transform.forward(); let left = cam.transform.left(); diff --git a/examples/testbed/src/main.rs b/examples/testbed/src/main.rs index 93d8031..f621764 100644 --- a/examples/testbed/src/main.rs +++ b/examples/testbed/src/main.rs @@ -1,6 +1,6 @@ use std::ptr::NonNull; -use lyra_engine::{math::{self, Vec3}, math::Transform, input::{KeyCode, InputButtons, MouseMotion, ActionHandler, Layout, Action, ActionKind, LayoutId, ActionMapping, Binding, ActionSource, ActionMappingId, InputActionPlugin, ActionState}, game::Game, plugin::Plugin, render::{window::{CursorGrabMode, WindowOptions}, light::{PointLight, directional::DirectionalLight, SpotLight}}, change_tracker::Ct, ecs::{system::{Criteria, CriteriaSchedule, BatchedSystem, IntoSystem}, world::World, Component}, DeltaTime, scene::{TransformComponent, ModelComponent, CameraComponent}}; +use lyra_engine::{math::{self, Vec3}, math::Transform, input::{KeyCode, ActionHandler, Action, ActionKind, LayoutId, ActionMapping, ActionSource, ActionMappingId, InputActionPlugin}, game::Game, render::{window::{CursorGrabMode, WindowOptions}, light::{PointLight, directional::DirectionalLight, SpotLight}}, change_tracker::Ct, ecs::{system::{Criteria, CriteriaSchedule, BatchedSystem, IntoSystem}, world::World, Component}, DeltaTime, scene::{TransformComponent, ModelComponent, CameraComponent}}; use lyra_engine::assets::{ResourceManager, Model}; mod free_fly_camera; @@ -86,7 +86,7 @@ async fn main() { )); { - let mut cube_tran = Transform::from_xyz(-3.5, 0.0, -8.0); + let cube_tran = Transform::from_xyz(-3.5, 0.0, -8.0); //cube_tran.rotate_y(math::Angle::Degrees(180.0)); world.spawn(( TransformComponent::from(cube_tran), diff --git a/lyra-ecs/src/archetype.rs b/lyra-ecs/src/archetype.rs index 27db880..582a0d2 100644 --- a/lyra-ecs/src/archetype.rs +++ b/lyra-ecs/src/archetype.rs @@ -28,17 +28,6 @@ impl Drop for ComponentColumn { #[allow(dead_code)] impl ComponentColumn { - /// Creates an invalid component column. Do not attempt to use it. - /* pub unsafe fn dangling() -> Self { - ComponentColumn { - data: RefCell:: - capacity: 0, - info: ComponentInfo::new::<()>(), - entry_size: 0, - len: 0, - } - } */ - pub unsafe fn alloc(component_layout: Layout, capacity: usize) -> NonNull { let new_layout = Layout::from_size_align( component_layout.size().checked_mul(capacity).unwrap(), @@ -53,7 +42,7 @@ impl ComponentColumn { } pub unsafe fn new(info: ComponentInfo, capacity: usize) -> Self { - let data = ComponentColumn::alloc(info.layout.into_layout_unchecked(), capacity); + let data = ComponentColumn::alloc(info.layout.into_layout().unwrap(), capacity); Self { data: RefCell::new(data), @@ -69,7 +58,7 @@ impl ComponentColumn { /// # Safety /// /// This column must have space to fit the component, if it does not have room it will panic. - pub unsafe fn set_at(&mut self, entity_index: usize, comp_src: NonNull, tick: Tick) { + pub unsafe fn set_at(&mut self, entity_index: usize, comp_src: NonNull, is_dynamic: bool, tick: Tick) { assert!(entity_index < self.capacity); let mut data = self.data.borrow_mut(); @@ -78,9 +67,11 @@ impl ComponentColumn { let dest = NonNull::new_unchecked(data.as_ptr().add(entity_index * self.info.layout.size)); ptr::copy_nonoverlapping(comp_src.as_ptr(), dest.as_ptr(), self.info.layout.size); - unsafe { - let layout = self.info.layout.into_layout_unchecked(); - //std::alloc::dealloc(comp_src.as_ptr(), layout); + if is_dynamic { + unsafe { + let layout = self.info.layout.into_layout().unwrap(); + std::alloc::dealloc(comp_src.as_ptr(), layout); + } } // check if a component spot is being set twice and that the entity's tick is @@ -113,7 +104,7 @@ impl ComponentColumn { /// /// This column must have the entity. pub unsafe fn get_mut(&mut self, entity_index: usize, tick: &Tick) -> &mut T { - self.entity_ticks[entity_index].tick_to(&tick); + self.entity_ticks[entity_index].tick_to(tick); let mut data = self.data.borrow_mut(); let data = data.deref_mut(); @@ -139,7 +130,7 @@ impl ComponentColumn { let mut data = self.data.borrow_mut(); - let mut new_ptr = Self::alloc(self.info.layout.into_layout_unchecked(), new_capacity); + let mut new_ptr = Self::alloc(self.info.layout.into_layout().unwrap(), new_capacity); if self.len > 0 { ptr::copy_nonoverlapping(data.as_ptr(), new_ptr.as_ptr(), self.len * self.info.layout.size); @@ -163,7 +154,7 @@ impl ComponentColumn { /// Removes a component from the column, freeing it, and returning the old index of the entity that took its place in the column. pub unsafe fn remove_component(&mut self, entity_index: usize, tick: &Tick) -> Option { - //self.entity_ticks[entity_index].tick_to(&tick); + let _ = tick; // may be used at some point let mut data = self.data.borrow_mut(); let data = data.deref_mut(); @@ -192,19 +183,19 @@ impl ComponentColumn { moved_index } - pub fn borrow_ptr<'a>(&'a self) -> Ref<'a, NonNull> { + pub fn borrow_ptr(&self) -> Ref> { self.data.borrow() } - pub fn borrow_mut_ptr<'a>(&'a self) -> RefMut<'a, NonNull> { + pub fn borrow_mut_ptr(&self) -> RefMut> { self.data.borrow_mut() } - pub fn try_borrow_ptr<'a>(&'a self) -> Result>, BorrowError> { + pub fn try_borrow_ptr(&self) -> Result>, BorrowError> { self.data.try_borrow() } - pub fn try_borrow_mut_ptr<'a>(&'a self) -> Result>, BorrowMutError> { + pub fn try_borrow_mut_ptr(&self) -> Result>, BorrowMutError> { self.data.try_borrow_mut() } } @@ -264,10 +255,11 @@ impl Archetype { let entity_index = self.entities.len(); self.entities.insert(entity, ArchetypeEntityId(entity_index as u64)); + let is_dynamic = bundle.is_dynamic(); bundle.take(|data, type_id, _size| { let col = self.get_column_mut(type_id).unwrap(); - unsafe { col.set_at(entity_index, data, tick.clone()); } + unsafe { col.set_at(entity_index, data, is_dynamic, *tick); } col.len += 1; }); @@ -425,7 +417,7 @@ mod tests { use rand::Rng; - use crate::{ArchetypeId, tests::{Vec2, Vec3}, world::{Entity, EntityId}, bundle::Bundle, ComponentInfo, MemoryLayout, DynTypeId, DynamicBundle, Tick}; + use crate::{tests::{Vec2, Vec3}, world::{Entity, EntityId}, bundle::Bundle, ComponentInfo, MemoryLayout, DynTypeId, DynamicBundle, Tick}; use super::Archetype; diff --git a/lyra-ecs/src/bundle.rs b/lyra-ecs/src/bundle.rs index 735a48e..bdbc659 100644 --- a/lyra-ecs/src/bundle.rs +++ b/lyra-ecs/src/bundle.rs @@ -1,4 +1,4 @@ -use std::{any::{TypeId, Any}, ptr::NonNull, mem::size_of, alloc::Layout}; +use std::{ptr::NonNull, mem::size_of, alloc::Layout}; use crate::{component::Component, component_info::ComponentInfo, DynTypeId}; @@ -12,6 +12,9 @@ pub trait Bundle { /// Take the bundle by calling the closure with pointers to each component, its type and size. /// The closure is expected to take ownership of the pointer. fn take(self, f: impl FnMut(NonNull, DynTypeId, usize)); + + /// Returns a boolean indicating if this Bundle is dynamic. See [`DynamicBundle`] + fn is_dynamic(&self) -> bool; } // The macro below can implement this for us, but this is here for development @@ -29,6 +32,10 @@ impl Bundle for (C1,) { f(NonNull::from(&c1).cast(), DynTypeId::of::(), size_of::()); } + + fn is_dynamic(&self) -> bool { + false + } } macro_rules! impl_bundle_tuple { @@ -50,6 +57,10 @@ macro_rules! impl_bundle_tuple { $(f(NonNull::from(&$name).cast(), DynTypeId::of::<$name>(), size_of::<$name>());)+ } + + fn is_dynamic(&self) -> bool { + false + } } ); } @@ -87,6 +98,10 @@ impl DynamicBundle { Self::default() } + pub fn is_empty(&self) -> bool { + self.bundle.len() == 0 + } + pub fn len(&self) -> usize { self.bundle.len() } @@ -125,12 +140,16 @@ impl Bundle for DynamicBundle { } fn info(&self) -> Vec { - self.bundle.iter().map(|b| b.1.clone()).collect() + self.bundle.iter().map(|b| b.1).collect() } fn take(self, mut f: impl FnMut(NonNull, DynTypeId, usize)) { for (data, info) in self.bundle.iter() { - f(data.clone(), info.type_id, info.layout.size); + f(*data, info.type_id, info.layout.size); } } + + fn is_dynamic(&self) -> bool { + true + } } \ No newline at end of file diff --git a/lyra-ecs/src/component_info.rs b/lyra-ecs/src/component_info.rs index 179e24c..7437e16 100644 --- a/lyra-ecs/src/component_info.rs +++ b/lyra-ecs/src/component_info.rs @@ -1,4 +1,4 @@ -use std::{any::{TypeId, type_name}, alloc::{Layout, LayoutError}}; +use std::{any::TypeId, alloc::{Layout, LayoutError}}; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct MemoryLayout { @@ -31,8 +31,9 @@ impl MemoryLayout { } } - pub unsafe fn into_layout_unchecked(self) -> Layout { - Layout::from_size_align_unchecked(self.size, self.alignment) + /// Converts self into a Layout. + pub fn into_layout(self) -> Result { + Layout::from_size_align(self.size, self.alignment) } } @@ -43,15 +44,15 @@ pub enum DynTypeId { Unknown(u128), } -impl Into for u128 { - fn into(self) -> DynTypeId { - DynTypeId::Unknown(self) +impl From for DynTypeId { + fn from(val: u128) -> Self { + DynTypeId::Unknown(val) } } -impl Into for TypeId { - fn into(self) -> DynTypeId { - DynTypeId::Rust(self) +impl From for DynTypeId { + fn from(val: TypeId) -> Self { + DynTypeId::Rust(val) } } @@ -68,7 +69,7 @@ impl DynTypeId { pub fn is(&self) -> bool { match self { - DynTypeId::Rust(tyid) => tyid.clone() == TypeId::of::(), + DynTypeId::Rust(tyid) => *tyid == TypeId::of::(), DynTypeId::Unknown(_) => false, } } @@ -81,7 +82,7 @@ impl DynTypeId { /// Force self into a rust TypeId, will panic if this type is not a Rust type. pub fn as_rust(&self) -> TypeId { match self { - DynTypeId::Rust(t) => t.clone(), + DynTypeId::Rust(t) => *t, DynTypeId::Unknown(_) => panic!("This type is unknown to rust, cannot construct a TypeId from it!"), } } @@ -108,6 +109,7 @@ impl ComponentInfo { where D: Into, { + let _ = name; // would be used at some point Self { type_id: type_id.into(), //name: name.to_string(), diff --git a/lyra-ecs/src/lib.rs b/lyra-ecs/src/lib.rs index 4196373..f42b377 100644 --- a/lyra-ecs/src/lib.rs +++ b/lyra-ecs/src/lib.rs @@ -1,8 +1,6 @@ -use crate::world::World; - extern crate self as lyra_ecs; -//mod lyra_ecs { pub use super::*; } +#[allow(unused_imports)] pub(crate) mod lyra_engine { pub(crate) mod ecs { pub use super::super::*; diff --git a/lyra-ecs/src/query/borrow.rs b/lyra-ecs/src/query/borrow.rs index 60dedd0..059a889 100644 --- a/lyra-ecs/src/query/borrow.rs +++ b/lyra-ecs/src/query/borrow.rs @@ -1,4 +1,4 @@ -use std::{marker::PhantomData, any::TypeId, ptr::NonNull, cell::{Ref, RefCell, RefMut}}; +use std::{marker::PhantomData, ptr::NonNull, cell::{Ref, RefMut}}; use crate::{world::World, ComponentColumn, DynTypeId, Tick}; @@ -45,6 +45,15 @@ pub struct QueryBorrow { _phantom: PhantomData } +impl Default for QueryBorrow { + fn default() -> Self { + Self { + type_id: DynTypeId::of::(), + _phantom: PhantomData, + } + } +} + impl Copy for QueryBorrow {} impl Clone for QueryBorrow { @@ -55,10 +64,7 @@ impl Clone for QueryBorrow { impl QueryBorrow { pub fn new() -> Self { - Self { - type_id: DynTypeId::of::(), - _phantom: PhantomData, - } + Self::default() } } @@ -79,6 +85,7 @@ where } unsafe fn fetch<'a>(&self, _world: &'a World, archetype: &'a crate::archetype::Archetype, tick: crate::Tick) -> Self::Fetch<'a> { + let _ = tick; let col = archetype.columns.iter().find(|c| c.info.type_id == self.type_id) .expect("You ignored 'can_visit_archetype'!"); @@ -143,6 +150,15 @@ pub struct QueryBorrowMut { _phantom: PhantomData } +impl Default for QueryBorrowMut { + fn default() -> Self { + Self { + type_id: DynTypeId::of::(), + _phantom: PhantomData, + } + } +} + impl Copy for QueryBorrowMut {} impl Clone for QueryBorrowMut { @@ -153,10 +169,7 @@ impl Clone for QueryBorrowMut { impl QueryBorrowMut { pub fn new() -> Self { - Self { - type_id: DynTypeId::of::(), - _phantom: PhantomData, - } + Self::default() } } @@ -182,13 +195,13 @@ where let col = archetype.columns.iter().find(|c| c.info.type_id == self.type_id) .expect("You ignored 'can_visit_archetype'!"); let layout_size = col.info.layout.size; - let mut col = NonNull::from(col); + let col = NonNull::from(col); // TODO: find a way to get the component column mutable with a borrowed archetype so its tick can be updated. // the fetcher needs to tick the entities tick in the archetype FetchBorrowMut { - col: NonNull::from(col), + col, size: layout_size, tick, _phantom: PhantomData, @@ -206,7 +219,7 @@ impl AsQuery for &mut T { #[cfg(test)] mod tests { - use std::{any::TypeId, mem::size_of, marker::PhantomData, ptr::NonNull}; + use std::{mem::size_of, marker::PhantomData, ptr::NonNull}; use crate::{world::{World, Entity, EntityId}, archetype::{Archetype, ArchetypeId}, query::{View, Fetch}, tests::Vec2, bundle::Bundle, DynTypeId, Tick}; diff --git a/lyra-ecs/src/query/dynamic/mod.rs b/lyra-ecs/src/query/dynamic/mod.rs index 6e285ea..729021d 100644 --- a/lyra-ecs/src/query/dynamic/mod.rs +++ b/lyra-ecs/src/query/dynamic/mod.rs @@ -1,4 +1,4 @@ -use std::{ptr::NonNull, cell::Ref}; +use std::ptr::NonNull; use crate::{world::World, ComponentColumn, ComponentInfo}; diff --git a/lyra-ecs/src/query/dynamic/view.rs b/lyra-ecs/src/query/dynamic/view.rs index aff9ae4..fb88b85 100644 --- a/lyra-ecs/src/query/dynamic/view.rs +++ b/lyra-ecs/src/query/dynamic/view.rs @@ -83,7 +83,7 @@ impl<'a> Iterator for DynamicViewIter<'a> { self.next_archetype += 1; let arch = unsafe { self.archetypes.get_unchecked(arch_id) }; - if arch.entities.len() == 0 { + if arch.entities.is_empty() { continue; } diff --git a/lyra-ecs/src/query/entities.rs b/lyra-ecs/src/query/entities.rs index 914cf19..b1c28df 100644 --- a/lyra-ecs/src/query/entities.rs +++ b/lyra-ecs/src/query/entities.rs @@ -1,4 +1,4 @@ -use crate::{world::{Entity, World}, archetype::{Archetype, ArchetypeId}}; +use crate::{world::{Entity, World}, archetype::Archetype}; use super::{Fetch, Query, AsQuery}; diff --git a/lyra-ecs/src/query/mod.rs b/lyra-ecs/src/query/mod.rs index c295865..1149fbc 100644 --- a/lyra-ecs/src/query/mod.rs +++ b/lyra-ecs/src/query/mod.rs @@ -1,4 +1,4 @@ -use crate::{archetype::{Archetype, ArchetypeId}, world::{ArchetypeEntityId, World}, Tick}; +use crate::{archetype::Archetype, world::{ArchetypeEntityId, World}, Tick}; pub mod view; pub use view::*; @@ -66,9 +66,7 @@ pub trait Query: Copy { unsafe fn fetch<'a>(&self, world: &'a World, archetype: &'a Archetype, tick: Tick) -> Self::Fetch<'a>; - /// Attempt to fetch only from the world. - /// - /// This is used in [`QueryResource`]. + /// Returns a fetcher that doesn't fetch from an archetype. unsafe fn fetch_world<'a>(&self, world: &'a World) -> Option> { let _ = world; None diff --git a/lyra-ecs/src/query/resource.rs b/lyra-ecs/src/query/resource.rs index fb622b7..dcba6ea 100644 --- a/lyra-ecs/src/query/resource.rs +++ b/lyra-ecs/src/query/resource.rs @@ -1,4 +1,4 @@ -use std::{marker::PhantomData, any::TypeId, ptr::NonNull, cell::{Ref, RefMut}}; +use std::{marker::PhantomData, cell::{Ref, RefMut}}; use crate::{world::World, resource::ResourceObject}; @@ -62,6 +62,7 @@ impl Query for QueryResource { } unsafe fn fetch<'a>(&self, world: &'a World, _archetype: &'a crate::archetype::Archetype, tick: crate::Tick) -> Self::Fetch<'a> { + let _ = tick; self.fetch_world(world).unwrap() } @@ -136,6 +137,7 @@ impl Query for QueryResourceMut { } unsafe fn fetch<'a>(&self, world: &'a World, _archetype: &'a crate::archetype::Archetype, tick: crate::Tick) -> Self::Fetch<'a> { + let _ = tick; self.fetch_world(world).unwrap() } diff --git a/lyra-ecs/src/query/tick.rs b/lyra-ecs/src/query/tick.rs index 30b5b63..f2f4376 100644 --- a/lyra-ecs/src/query/tick.rs +++ b/lyra-ecs/src/query/tick.rs @@ -52,6 +52,15 @@ pub struct QueryTickOf { _phantom: PhantomData } +impl Default for QueryTickOf { + fn default() -> Self { + Self { + type_id: DynTypeId::of::(), + _phantom: PhantomData, + } + } +} + // manually implemented to avoid a Copy bound on T impl Copy for QueryTickOf {} @@ -64,10 +73,7 @@ impl Clone for QueryTickOf { impl QueryTickOf { pub fn new() -> Self { - Self { - type_id: DynTypeId::of::(), - _phantom: PhantomData, - } + Self::default() } } diff --git a/lyra-ecs/src/query/view.rs b/lyra-ecs/src/query/view.rs index bae2d3d..3377efb 100644 --- a/lyra-ecs/src/query/view.rs +++ b/lyra-ecs/src/query/view.rs @@ -1,6 +1,6 @@ use std::ops::Range; -use crate::{archetype::{Archetype, ArchetypeId}, world::{ArchetypeEntityId, World}, EntityId, TickTracker, Tick}; +use crate::{archetype::Archetype, world::{ArchetypeEntityId, World}, EntityId, Tick}; use super::{Query, Fetch}; @@ -96,7 +96,7 @@ where self.next_archetype += 1; let arch = unsafe { self.archetypes.get_unchecked(arch_id) }; - if arch.entities.len() == 0 { + if arch.entities.is_empty() { continue; } diff --git a/lyra-ecs/src/resource.rs b/lyra-ecs/src/resource.rs index 54551b4..55dd47b 100644 --- a/lyra-ecs/src/resource.rs +++ b/lyra-ecs/src/resource.rs @@ -1,4 +1,4 @@ -use std::{any::{TypeId, Any}, alloc::Layout, cell::{RefCell, Ref, RefMut}, ptr::NonNull, alloc}; +use std::{any::{TypeId, Any}, cell::{RefCell, Ref, RefMut}}; /// Shorthand for `Send + Sync + 'static`, so it never needs to be implemented manually. pub trait ResourceObject: Send + Sync + 'static {} @@ -30,7 +30,7 @@ impl ResourceData { /// /// * If the data is already borrowed mutably, this will panic. /// * If the type of `T` is not the same as the resource type. - pub fn get<'a, T: 'static>(&'a self) -> Ref<'a, T> { + pub fn get(&self) -> Ref { Ref::map(self.data.borrow(), |a| a.downcast_ref().unwrap()) } @@ -40,7 +40,7 @@ impl ResourceData { /// /// * If the data is already borrowed mutably, this will panic. /// * If the type of `T` is not the same as the resource type. - pub fn get_mut<'a, T: 'static>(&'a self) -> RefMut<'a, T> { + pub fn get_mut(&self) -> RefMut { RefMut::map(self.data.borrow_mut(), |a| a.downcast_mut().unwrap()) } @@ -49,7 +49,7 @@ impl ResourceData { /// # Panics /// /// * If the type of `T` is not the same as the resource type. - pub fn try_get<'a, T: 'static>(&'a self) -> Option> { + pub fn try_get(&self) -> Option> { self.data.try_borrow() .map(|r| Ref::map(r, |a| a.downcast_ref().unwrap())) @@ -61,7 +61,7 @@ impl ResourceData { /// # Panics /// /// * If the type of `T` is not the same as the resource type. - pub fn try_get_mut<'a, T: 'static>(&'a self) -> Option> { + pub fn try_get_mut(&self) -> Option> { self.data.try_borrow_mut() .map(|r| RefMut::map(r, |a| a.downcast_mut().unwrap())) .ok() diff --git a/lyra-ecs/src/system/batched.rs b/lyra-ecs/src/system/batched.rs index 58b656a..295270a 100644 --- a/lyra-ecs/src/system/batched.rs +++ b/lyra-ecs/src/system/batched.rs @@ -7,6 +7,7 @@ use super::{System, Criteria}; /// A system that executes a batch of systems in order that they were given. /// You can optionally add criteria that must pass before the systems are /// executed. +#[derive(Default)] pub struct BatchedSystem { systems: Vec>, criteria: Vec>, @@ -16,11 +17,7 @@ pub struct BatchedSystem { impl BatchedSystem { /// Create a new BatchedSystem pub fn new() -> Self { - Self { - systems: Vec::new(), - criteria: Vec::new(), - criteria_checks: 0, - } + Self::default() } pub fn with_system(&mut self, system: S) -> &mut Self @@ -55,11 +52,10 @@ impl System for BatchedSystem { for criteria in self.criteria.iter_mut() { match criteria.can_run(world, self.criteria_checks) { - super::CriteriaSchedule::Yes => can_run = can_run && true, + super::CriteriaSchedule::Yes => {}, super::CriteriaSchedule::No => can_run = false, super::CriteriaSchedule::YesAndLoop => { - can_run = can_run && true; - check_again = can_run && true; + check_again = can_run; }, super::CriteriaSchedule::NoAndLoop => { can_run = false; diff --git a/lyra-ecs/src/system/graph.rs b/lyra-ecs/src/system/graph.rs index 6aeee00..b7150ca 100644 --- a/lyra-ecs/src/system/graph.rs +++ b/lyra-ecs/src/system/graph.rs @@ -28,15 +28,14 @@ pub struct GraphSystem { /// A system executor that represents the systems in a graph to handle dependencies. /// /// The graph uses an adjacency list. +#[derive(Default)] pub struct GraphExecutor { systems: HashMap, } impl GraphExecutor { pub fn new() -> Self { - Self { - systems: HashMap::new(), - } + Self::default() } /// Inserts a system into the Graph. @@ -84,7 +83,7 @@ impl GraphExecutor { Ok(possible_errors) } - fn topological_sort<'a, 'b>(&'a self, stack: &'b mut VecDeque, visited: &mut HashSet<&'a str>, node: &'a GraphSystem) -> Result<(), GraphExecutorError> { + fn topological_sort<'a>(&'a self, stack: &mut VecDeque, visited: &mut HashSet<&'a str>, node: &'a GraphSystem) -> Result<(), GraphExecutorError> { if !visited.contains(node.name.as_str()) { visited.insert(&node.name); diff --git a/lyra-ecs/src/system/mod.rs b/lyra-ecs/src/system/mod.rs index b2d3706..e8386ff 100644 --- a/lyra-ecs/src/system/mod.rs +++ b/lyra-ecs/src/system/mod.rs @@ -21,6 +21,7 @@ pub trait System { /// A setup step of the System, called before `execute` ever runs. fn setup(&self, world: NonNull) -> anyhow::Result<()> { + let _ = world; Ok(()) } } @@ -39,10 +40,14 @@ pub trait FnArgFetcher { /// Return the appropriate world access if this fetcher gets the world directly. /// Return [`Access::None`] if you're only fetching components, or resources. fn world_access(&self) -> Access { - Access::None + Access::Read } /// Get the arg from the world + /// + /// # Safety + /// The system executor must ensure that on execution of the system, it will be safe to + /// borrow the world. unsafe fn get<'a>(&mut self, world: NonNull) -> Self::Arg<'a>; } @@ -52,6 +57,7 @@ pub trait FnArg { pub struct FnSystem { inner: F, + #[allow(dead_code)] args: Args, } @@ -356,6 +362,7 @@ mod tests { test_system.into_system().execute(NonNull::from(&world)).unwrap(); + #[allow(dead_code)] fn test_system(world: &mut World) -> anyhow::Result<()> { let mut counter = world.get_resource_mut::(); counter.0 += 10; diff --git a/lyra-ecs/src/world.rs b/lyra-ecs/src/world.rs index 232a4ed..697f74f 100644 --- a/lyra-ecs/src/world.rs +++ b/lyra-ecs/src/world.rs @@ -1,6 +1,6 @@ use std::{collections::{HashMap, VecDeque}, any::TypeId, cell::{Ref, RefMut}, ptr::NonNull}; -use crate::{archetype::{ArchetypeId, Archetype}, bundle::Bundle, component::Component, query::{Query, ViewIter, View, AsQuery}, resource::ResourceData, query::{dynamic::{DynamicViewIter, QueryDynamicType, DynamicView}, ViewOne}, ComponentInfo, DynTypeId, TickTracker, Tick}; +use crate::{archetype::{ArchetypeId, Archetype}, bundle::Bundle, query::{Query, ViewIter, View, AsQuery}, resource::ResourceData, query::{dynamic::DynamicView, ViewOne}, ComponentInfo, DynTypeId, TickTracker, Tick}; #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub struct EntityId(pub u64); @@ -157,28 +157,29 @@ impl World { let mut col_types: Vec = current_arch.columns.iter().map(|c| c.info.type_id).collect(); let orig_col = col_types.clone(); - col_types.extend(bundle.type_ids().into_iter()); + col_types.extend(bundle.type_ids()); let mut col_infos: Vec = current_arch.columns.iter().map(|c| c.info).collect(); - col_infos.extend(bundle.info().into_iter()); + col_infos.extend(bundle.info()); let col_ptrs: Vec<(NonNull, ComponentInfo)> = current_arch.columns.iter().map(|c| unsafe { (NonNull::new_unchecked(c.borrow_ptr().as_ptr()), c.info) }).collect(); if let Some(arch) = self.archetypes.values_mut().find(|a| a.is_archetype_for(&col_types)) { let res_index = arch.reserve_one(entity); + let is_dynamic = bundle.is_dynamic(); for (col_type, (col_ptr, col_info)) in orig_col.into_iter().zip(col_ptrs.into_iter()) { unsafe { let ptr = NonNull::new_unchecked(col_ptr.as_ptr() .add(res_index.0 as usize * col_info.layout.size)); let col = arch.get_column_mut(col_type).unwrap(); - col.set_at(res_index.0 as _, ptr, tick); + col.set_at(res_index.0 as _, ptr, is_dynamic, tick); } } bundle.take(|data, type_id, _size| { let col = arch.get_column_mut(type_id).unwrap(); - unsafe { col.set_at(res_index.0 as _, data, tick); } + unsafe { col.set_at(res_index.0 as _, data, is_dynamic, tick); } col.len += 1; }); @@ -210,7 +211,7 @@ impl World { } /// View into the world for a set of entities that satisfy the queries. - pub fn view_iter<'a, T: 'static + AsQuery>(&'a self) -> ViewIter<'a, T::Query> { + pub fn view_iter(&self) -> ViewIter { let archetypes = self.archetypes.values().collect(); let v = View::new(self, T::Query::new(), archetypes); v.into_iter() @@ -220,7 +221,7 @@ impl World { DynamicView::new(self) } - pub fn view_one<'a, T: 'static + AsQuery>(&'a self, entity: Entity) -> ViewOne<'a, T::Query> { + pub fn view_one(&self, entity: Entity) -> ViewOne { ViewOne::new(self, entity.id, T::Query::new()) } @@ -245,7 +246,7 @@ impl World { /// /// Will panic if the resource is not in the world. See [`try_get_resource`] for /// a function that returns an option. - pub fn get_resource<'a, T: 'static>(&'a self) -> Ref<'a, T> { + pub fn get_resource(&self) -> Ref { self.resources.get(&TypeId::of::()).unwrap() .get() } @@ -253,17 +254,16 @@ impl World { /// Attempts to get a resource from the World. /// /// Returns `None` if the resource was not found. - pub fn try_get_resource<'a, T: 'static>(&'a self) -> Option> { + pub fn try_get_resource(&self) -> Option> { self.resources.get(&TypeId::of::()) - .map(|r| r.try_get()) - .flatten() + .and_then(|r| r.try_get()) } /// Gets a mutable borrow of a resource from the World. /// /// Will panic if the resource is not in the world. See [`try_get_resource_mut`] for /// a function that returns an option. - pub fn get_resource_mut<'a, T: 'static>(&'a self) -> RefMut<'a, T> { + pub fn get_resource_mut(&self) -> RefMut { self.resources.get(&TypeId::of::()).unwrap() .get_mut() } @@ -271,10 +271,9 @@ impl World { /// Attempts to get a mutable borrow of a resource from the World. /// /// Returns `None` if the resource was not found. - pub fn try_get_resource_mut<'a, T: 'static>(&'a self) -> Option> { + pub fn try_get_resource_mut(&self) -> Option> { self.resources.get(&TypeId::of::()) - .map(|r| r.try_get_mut()) - .flatten() + .and_then(|r| r.try_get_mut()) } /// Increments the TickTracker which is used for tracking changes to components. @@ -298,8 +297,6 @@ impl World { #[cfg(test)] mod tests { - use std::ops::Deref; - use crate::{tests::{Vec2, Vec3}, query::TickOf}; use super::World; diff --git a/lyra-resource/src/loader/model.rs b/lyra-resource/src/loader/model.rs index e365cbc..c9caf4b 100644 --- a/lyra-resource/src/loader/model.rs +++ b/lyra-resource/src/loader/model.rs @@ -60,7 +60,7 @@ impl ModelLoader { } } */ - fn process_node(&self, buffers: &Vec>, materials: &Vec, node: gltf::Node<'_>) -> Vec { + fn process_node(buffers: &Vec>, materials: &Vec, node: gltf::Node<'_>) -> Vec { let mut meshes = vec![]; if let Some(mesh) = node.mesh() { for prim in mesh.primitives() { @@ -110,14 +110,13 @@ impl ModelLoader { let mat = materials.get(prim.material().index().unwrap()).unwrap(); new_mesh.set_material(mat.clone()); - //prim.material(). meshes.push(new_mesh); } } for child in node.children() { - let mut child_meshes = self.process_node(buffers, materials, child); + let mut child_meshes = ModelLoader::process_node(buffers, materials, child); meshes.append(&mut child_meshes); } @@ -186,7 +185,7 @@ impl ResourceLoader for ModelLoader { .map(|mat| Material::from_gltf(&mut context, mat)).collect(); let meshes: Vec = scene.nodes() - .flat_map(|node| self.process_node(&buffers, &materials, node)) + .flat_map(|node| ModelLoader::process_node(&buffers, &materials, node)) .collect(); debug!("Loaded {} meshes, and {} materials from '{}'", meshes.len(), materials.len(), path); diff --git a/lyra-resource/src/material.rs b/lyra-resource/src/material.rs index 7bccf46..1c3c357 100644 --- a/lyra-resource/src/material.rs +++ b/lyra-resource/src/material.rs @@ -1,6 +1,4 @@ -use std::{collections::{hash_map::DefaultHasher, HashMap}, hash::{Hash, Hasher}}; - -use tracing::debug; +use std::{collections::hash_map::DefaultHasher, hash::{Hash, Hasher}}; use crate::{Texture, ResHandle, util, loader::model::GltfLoadContext}; @@ -112,7 +110,7 @@ pub struct Specular { } impl Specular { - pub fn from_gltf(context: &mut GltfLoadContext, value: gltf::material::Specular) -> Self { + pub(crate) fn from_gltf(context: &mut GltfLoadContext, value: gltf::material::Specular) -> Self { let color_texture = value.specular_color_texture() .map(|t| Material::load_texture(context, t)); diff --git a/src/delta_time.rs b/src/delta_time.rs index 50f3e84..dcf3314 100644 --- a/src/delta_time.rs +++ b/src/delta_time.rs @@ -1,5 +1,3 @@ -use std::borrow::BorrowMut; - use instant::Instant; use lyra_ecs::{Component, world::World, system::IntoSystem}; diff --git a/src/game.rs b/src/game.rs index 882a074..f214024 100755 --- a/src/game.rs +++ b/src/game.rs @@ -3,7 +3,7 @@ use std::{sync::Arc, collections::VecDeque, ptr::NonNull}; use async_std::task::block_on; use lyra_ecs::{world::World, system::{GraphExecutor, System}}; -use tracing::{info, error, Level, debug}; +use tracing::{info, error, Level}; use tracing_appender::non_blocking; use tracing_subscriber::{ layer::SubscriberExt, @@ -290,7 +290,7 @@ impl Game { plugin.as_ref().setup(self); } - let mut world = self.world.take().unwrap_or_default(); + let world = self.world.take().unwrap_or_default(); // run startup systems while let Some(mut startup) = self.startup_systems.pop_front() { diff --git a/src/input/action.rs b/src/input/action.rs index 7b9aa3a..91dbbf2 100644 --- a/src/input/action.rs +++ b/src/input/action.rs @@ -1,19 +1,21 @@ -use std::{collections::HashMap, cell::RefCell, borrow::BorrowMut, ops::Deref}; +use std::{collections::HashMap, ops::Deref}; use lyra_ecs::{world::World, system::IntoSystem}; -use crate::{castable_any::CastableAny, plugin::Plugin}; +use crate::plugin::Plugin; use super::{Button, KeyCode, InputButtons}; +#[allow(dead_code)] #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] -enum GamepadFormat { +pub enum GamepadFormat { DualAxis, Joystick, } +#[allow(dead_code)] #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] -enum GamepadButton { +pub enum GamepadButton { FaceBottom, FaceLeft, FaceRight, @@ -35,8 +37,9 @@ enum GamepadButton { RSpecial, } +#[allow(dead_code)] #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] -enum GamepadAxis { +pub enum GamepadAxis { LThumbstickX, LThumbstickY, RThumbstickX, @@ -45,12 +48,14 @@ enum GamepadAxis { RTrigger, } +#[allow(dead_code)] #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] -enum GamepadInput { +pub enum GamepadInput { Button(GamepadButton), Axis(GamepadAxis), } +#[allow(dead_code)] #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] pub enum ActionSource { Keyboard(KeyCode), @@ -132,7 +137,7 @@ impl Action { } } -#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct LayoutId(u32); impl From for LayoutId { @@ -141,13 +146,7 @@ impl From for LayoutId { } } -impl Default for LayoutId { - fn default() -> Self { - Self(0) - } -} - -#[derive(Clone)] +#[derive(Clone, Default)] pub struct Layout { mappings: HashMap, active_mapping: ActionMappingId, @@ -155,10 +154,7 @@ pub struct Layout { impl Layout { pub fn new() -> Self { - Self { - mappings: HashMap::new(), - active_mapping: Default::default(), - } + Self::default() } pub fn add_mapping(&mut self, mapping: ActionMapping) -> &mut Self { @@ -168,7 +164,7 @@ impl Layout { } } -#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct ActionMappingId(u32); impl From for ActionMappingId { @@ -177,12 +173,6 @@ impl From for ActionMappingId { } } -impl Default for ActionMappingId { - fn default() -> Self { - Self(0) - } -} - #[derive(Clone)] pub struct ActionMapping { layout: LayoutId, @@ -209,8 +199,7 @@ impl ActionMapping { pub fn bind(mut self, action_label: &str, bindings: &[Binding]) -> Self { let mut bindings = bindings.to_vec(); - let action_binds = self.action_binds.entry(action_label.to_string()) - .or_insert_with(|| Vec::new()); + let action_binds = self.action_binds.entry(action_label.to_string()).or_default(); action_binds.append(&mut bindings); self @@ -226,7 +215,7 @@ impl ActionMapping { /* pub fn add_bindings(&mut self, action_label: String, bindings: &[Binding]) -> &mut Self { let mut bindings = bindings.to_vec(); let action_binds = self.action_binds.entry(action_label) - .or_insert_with(|| Vec::new()); + .or_insert_with(Vec::new); action_binds.append(&mut bindings); self @@ -237,7 +226,7 @@ impl ActionMapping { } } -#[derive(Clone)] +#[derive(Clone, Default)] pub struct ActionHandler { pub actions: HashMap, pub layouts: HashMap, @@ -247,12 +236,7 @@ pub struct ActionHandler { impl ActionHandler { pub fn new() -> Self { - Self { - actions: HashMap::new(), - layouts: HashMap::new(), - current_layout: LayoutId::default(), - current_mapping: ActionMappingId::default(), - } + Self::default() } pub fn add_layout(mut self, id: LayoutId) -> Self { @@ -262,8 +246,6 @@ impl ActionHandler { } pub fn add_action(mut self, label: &str, action: Action) -> Self { - /* let layout = self.layouts.get_mut(&layout_id).unwrap(); - layout.actions.insert(label.to_string(), action); */ self.actions.insert(label.to_string(), action); self @@ -281,12 +263,9 @@ impl ActionHandler { /// This will panic if the action name does not correspond to an action. pub fn is_action_pressed(&self, action_name: &str) -> bool { let action = self.actions.get(action_name) - .expect(&format!("Action {action_name} was not found")); + .unwrap_or_else(|| panic!("Action {action_name} was not found")); - match action.state { - ActionState::Pressed(_) | ActionState::JustPressed(_) => true, - _ => false - } + matches!(action.state, ActionState::Pressed(_) | ActionState::JustPressed(_)) } /// Returns true if the action was just pressed. @@ -294,12 +273,9 @@ impl ActionHandler { /// This will panic if the action name does not correspond to an action. pub fn was_action_just_pressed(&self, action_name: &str) -> bool { let action = self.actions.get(action_name) - .expect(&format!("Action {action_name} was not found")); + .unwrap_or_else(|| panic!("Action {action_name} was not found")); - match action.state { - ActionState::JustPressed(_) => true, - _ => false - } + matches!(action.state, ActionState::JustPressed(_)) } /// Returns true if the action was just released. @@ -307,12 +283,9 @@ impl ActionHandler { /// This will panic if the action name does not correspond to an action. pub fn was_action_just_released(&self, action_name: &str) -> bool { let action = self.actions.get(action_name) - .expect(&format!("Action {action_name} was not found")); + .unwrap_or_else(|| panic!("Action {action_name} was not found")); - match action.state { - ActionState::JustReleased => true, - _ => false - } + matches!(action.state, ActionState::JustReleased) } /// Returns an action's state. @@ -320,7 +293,7 @@ impl ActionHandler { /// This will panic if the action name does not correspond to an action. pub fn get_action_state(&self, action_name: &str) -> ActionState { let action = self.actions.get(action_name) - .expect(&format!("Action {action_name} was not found")); + .unwrap_or_else(|| panic!("Action {action_name} was not found")); action.state } @@ -331,7 +304,7 @@ impl ActionHandler { /// This will panic if the action name does not correspond to an action. pub fn get_pressed_modifier(&self, action_name: &str) -> Option { let action = self.actions.get(action_name) - .expect(&format!("Action {action_name} was not found")); + .unwrap_or_else(|| panic!("Action {action_name} was not found")); match action.state { ActionState::Pressed(v) | ActionState::JustPressed(v) => Some(v), @@ -345,7 +318,7 @@ impl ActionHandler { /// This will panic if the action name does not correspond to an action. pub fn get_just_pressed_modifier(&self, action_name: &str) -> Option { let action = self.actions.get(action_name) - .expect(&format!("Action {action_name} was not found")); + .unwrap_or_else(|| panic!("Action {action_name} was not found")); match action.state { ActionState::JustPressed(v) => Some(v), @@ -359,7 +332,7 @@ impl ActionHandler { /// This will panic if the action name does not correspond to an action. pub fn get_axis_modifier(&self, action_name: &str) -> Option { let action = self.actions.get(action_name) - .expect(&format!("Action {action_name} was not found")); + .unwrap_or_else(|| panic!("Action {action_name} was not found")); match action.state { ActionState::Axis(v) => Some(v), @@ -376,7 +349,6 @@ fn actions_system(world: &mut World) -> anyhow::Result<()> { let mut handler = world.try_get_resource_mut::() .expect("No Input Action handler was created in the world!"); - //handler let layout = handler.layouts.get(&handler.current_layout).expect("No active layout"); let mapping = layout.mappings.get(&layout.active_mapping).expect("No active mapping"); @@ -405,13 +377,9 @@ fn actions_system(world: &mut World) -> anyhow::Result<()> { } else { match action.state { ActionState::Idle => {}, - //ActionState::Pressed(_) => todo!(), - //ActionState::JustPressed(_) => todo!(), ActionState::JustReleased => action.state = ActionState::Idle, - //ActionState::Released => action.state = ActionState::I, _ => action.state = ActionState::JustReleased, } - //action.state = ActionState::JustReleased; } } } diff --git a/src/math/transform.rs b/src/math/transform.rs index 2b5be2b..9e02ae1 100755 --- a/src/math/transform.rs +++ b/src/math/transform.rs @@ -94,16 +94,14 @@ impl Transform { pub fn lerp(&self, rhs: Transform, alpha: f32) -> Self { if alpha.is_finite() { - let mut res = self.clone(); + let mut res = *self; res.translation = self.translation.lerp(rhs.translation, alpha); // normalize rotation here to avoid panics res.rotation = self.rotation.lerp(rhs.rotation.normalize(), alpha); res.scale = self.scale.lerp(rhs.scale, alpha); res } else { - self.clone() + *self } - - } } \ No newline at end of file diff --git a/src/render/light/mod.rs b/src/render/light/mod.rs index f22db94..d89cbd2 100644 --- a/src/render/light/mod.rs +++ b/src/render/light/mod.rs @@ -4,14 +4,11 @@ pub mod spotlight; use lyra_ecs::{Entity, Tick, world::World, query::{Entities, TickOf}}; pub use point::*; -pub use directional::*; pub use spotlight::*; -use std::{collections::{VecDeque, HashMap}, num::{NonZeroU64, NonZeroU32}, marker::PhantomData}; +use std::{collections::{VecDeque, HashMap}, marker::PhantomData}; -pub use point::*; -use tracing::{debug, Instrument}; -use wgpu::util::DeviceExt; +use tracing::debug; use std::mem; @@ -39,9 +36,9 @@ pub struct LightBuffer { } impl LightBuffer { - pub fn new(layout: &wgpu::BindGroupLayout, max_count: usize) -> Self { + pub fn new(max_count: usize) -> Self { Self { - _phantom: PhantomData::default(), + _phantom: PhantomData, max_count, buffer_count: 0, used_indexes: HashMap::new(), @@ -104,7 +101,7 @@ impl LightBuffer { } } -pub struct LightUniformBuffers { +pub(crate) struct LightUniformBuffers { pub buffer: wgpu::Buffer, pub bindgroup_layout: wgpu::BindGroupLayout, pub bindgroup: wgpu::BindGroup, @@ -157,8 +154,8 @@ impl LightUniformBuffers { label: Some("BG_Lights"), }); - let point_lights = LightBuffer::new(&bindgroup_layout, MAX_LIGHT_COUNT); - let spot_lights = LightBuffer::new(&bindgroup_layout, MAX_LIGHT_COUNT); + let point_lights = LightBuffer::new(MAX_LIGHT_COUNT); + let spot_lights = LightBuffer::new(MAX_LIGHT_COUNT); Self { buffer, @@ -310,7 +307,7 @@ impl DirectionalLightUniform { #[repr(C)] #[derive(Default, Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] -struct SpotLightUniform { +pub(crate) struct SpotLightUniform { pub position: glam::Vec3, pub _padding: u32, pub direction: glam::Vec3, diff --git a/src/render/material.rs b/src/render/material.rs index 154ba76..162c561 100755 --- a/src/render/material.rs +++ b/src/render/material.rs @@ -42,7 +42,7 @@ impl Material { let diffuse_texture = value.base_color_texture.as_ref().map(|t| &t.data.as_ref().unwrap().image) .map(|i| RenderTexture::from_image(device, queue, bg_layout.clone(), i, None).unwrap()); - let specular = value.specular.as_ref().map(|s| MaterialSpecular::from_resource(device, queue, bg_layout.clone(), &s)); + let specular = value.specular.as_ref().map(|s| MaterialSpecular::from_resource(device, queue, bg_layout.clone(), s)); Self { shader_id: value.shader_uuid.unwrap_or(0), diff --git a/src/render/render_buffer.rs b/src/render/render_buffer.rs index e0b0e08..74d5e6d 100755 --- a/src/render/render_buffer.rs +++ b/src/render/render_buffer.rs @@ -133,10 +133,7 @@ impl BufferWrapperBuilder { } fn format_label(&self, prefix: &str) -> Option { - match self.label_prefix.as_ref() { - Some(v) => Some(format!("{}{}", prefix, v)), - None => None - } + self.label_prefix.as_ref().map(|v| format!("{}{}", prefix, v)) } /// Finish building the BufferWrapper diff --git a/src/render/render_job.rs b/src/render/render_job.rs index 5b220e7..9c7ec1c 100755 --- a/src/render/render_job.rs +++ b/src/render/render_job.rs @@ -1,10 +1,7 @@ - use lyra_ecs::Entity; use crate::math::Transform; -use super::material::Material; - pub struct RenderJob { pub entity: Entity, pub shader_id: u64, diff --git a/src/render/renderer.rs b/src/render/renderer.rs index b9aad0d..9e250ee 100755 --- a/src/render/renderer.rs +++ b/src/render/renderer.rs @@ -1,6 +1,4 @@ use std::collections::{HashMap, VecDeque, HashSet}; -use std::mem; -use std::num::NonZeroU64; use std::sync::Arc; use std::borrow::Cow; @@ -11,23 +9,22 @@ use lyra_ecs::Entity; use lyra_ecs::query::{Entities, TickOf}; use lyra_ecs::world::World; use tracing::{debug, warn}; -use wgpu::{BindGroup, BindGroupLayout, Limits}; +use wgpu::{BindGroupLayout, Limits}; use wgpu::util::DeviceExt; use winit::window::Window; -use crate::math::{Transform, self}; -use crate::render::light::PointLightUniform; +use crate::math::Transform; use crate::render::material::MaterialUniform; -use crate::render::render_buffer::{BufferWrapperBuilder, BindGroupPair}; +use crate::render::render_buffer::BufferWrapperBuilder; use crate::scene::{ModelComponent, TransformComponent, CameraComponent}; use super::camera::{RenderCamera, CameraUniform}; use super::desc_buf_lay::DescVertexBufferLayout; -use super::light::{PointLight, LightUniformBuffers}; +use super::light::LightUniformBuffers; use super::material::Material; use super::render_buffer::BufferWrapper; use super::texture::RenderTexture; -use super::transform_buffer_storage::{TransformBufferIndices, TransformBuffers}; +use super::transform_buffer_storage::TransformBuffers; use super::vertex::Vertex; use super::{render_pipeline::FullRenderPipeline, render_buffer::BufferStorage, render_job::RenderJob}; @@ -49,7 +46,6 @@ struct MeshBufferStorage { //#[allow(dead_code)] //render_texture: Option, material: Option, - diffuse_texture: Option, // The index of the transform for this entity. // The tuple is structured like this: (transform index, index of transform inside the buffer) @@ -128,9 +124,10 @@ impl BasicRenderer { limits: if cfg!(target_arch = "wasm32") { wgpu::Limits::downlevel_webgl2_defaults() } else { - let mut v = wgpu::Limits::default(); - v.max_bind_groups = 8; - v + wgpu::Limits { + max_bind_groups: 8, + ..Default::default() + } }, label: None, }, @@ -354,7 +351,7 @@ impl BasicRenderer { ( vertex_buffer, indices ) } - fn create_mesh_buffers(&mut self, mesh: &Mesh, transform_indices: TransformBufferIndices) -> MeshBufferStorage { + fn create_mesh_buffers(&mut self, mesh: &Mesh) -> MeshBufferStorage { let (vertex_buffer, buffer_indices) = self.create_vertex_index_buffers(mesh); let material = Material::from_resource(&self.device, &self.queue, self.bgl_texture.clone(), &mesh.material()); @@ -366,7 +363,6 @@ impl BasicRenderer { buffer_vertex: vertex_buffer, buffer_indices, material: Some(material), - diffuse_texture: None, } } @@ -376,13 +372,13 @@ impl BasicRenderer { self.transform_buffers.expand_buffers(&self.device); } - let indices = self.transform_buffers.update_or_insert(&self.queue, &self.render_limits, + self.transform_buffers.update_or_insert(&self.queue, &self.render_limits, entity, || ( transform.calculate_mat4(), glam::Mat3::from_quat(transform.rotation) )); #[allow(clippy::map_entry)] if !self.mesh_buffers.contains_key(&mesh.uuid) { // create the mesh's buffers - let buffers = self.create_mesh_buffers(mesh, indices); + let buffers = self.create_mesh_buffers(mesh); self.mesh_buffers.insert(mesh.uuid, buffers); self.entity_meshes.insert(entity, mesh.uuid); @@ -475,7 +471,7 @@ impl Renderer for BasicRenderer { warn!("Missing camera!"); } - self.light_buffers.update_lights(&self.queue, last_epoch, &main_world); + self.light_buffers.update_lights(&self.queue, last_epoch, main_world); } fn render(&mut self) -> Result<(), wgpu::SurfaceError> { @@ -524,15 +520,15 @@ impl Renderer for BasicRenderer { .and_then(|m| m.diffuse_texture.as_ref()) { render_pass.set_bind_group(0, tex.bind_group(), &[]); } else { - render_pass.set_bind_group(0, &self.default_texture.bind_group(), &[]); + render_pass.set_bind_group(0, self.default_texture.bind_group(), &[]); } if let Some(tex) = buffers.material.as_ref() .and_then(|m| m.specular.as_ref()) - .and_then(|s| s.texture.as_ref().or_else(|| s.color_texture.as_ref())) { + .and_then(|s| s.texture.as_ref().or(s.color_texture.as_ref())) { render_pass.set_bind_group(5, tex.bind_group(), &[]); } else { - render_pass.set_bind_group(5, &self.default_texture.bind_group(), &[]); + render_pass.set_bind_group(5, self.default_texture.bind_group(), &[]); } // Get the bindgroup for job's transform and bind to it using an offset. diff --git a/src/render/texture.rs b/src/render/texture.rs index b178005..c1b019f 100755 --- a/src/render/texture.rs +++ b/src/render/texture.rs @@ -147,8 +147,8 @@ impl RenderTexture { Ok(Self { inner_texture: texture, - view: view, - sampler: sampler, + view, + sampler, bindgroup_pair: Some(bgp), }) } @@ -217,8 +217,8 @@ impl RenderTexture { Self { inner_texture: texture, - view: view, - sampler: sampler, + view, + sampler, bindgroup_pair: None, } } diff --git a/src/render/transform_buffer_storage.rs b/src/render/transform_buffer_storage.rs index 74f4694..36e9a1d 100644 --- a/src/render/transform_buffer_storage.rs +++ b/src/render/transform_buffer_storage.rs @@ -1,7 +1,6 @@ use std::{collections::{VecDeque, HashMap}, num::NonZeroU64}; use lyra_ecs::Entity; -use tracing::debug; use wgpu::Limits; use std::mem; @@ -165,10 +164,6 @@ impl TransformBuffers { .map(|entry| &entry.bindgroup) } - pub fn entity_bind_group(&self, entity: Entity) -> Option<&wgpu::BindGroup> { - self.entity_indices(entity).and_then(|i| self.bind_group(*i)) - } - /// Expand the Transform buffers by adding another uniform buffer binding pub fn expand_buffers(&mut self, device: &wgpu::Device) { let limits = device.limits(); diff --git a/src/render/window.rs b/src/render/window.rs index 3388125..4b591d2 100644 --- a/src/render/window.rs +++ b/src/render/window.rs @@ -1,4 +1,4 @@ -use std::{sync::Arc, collections::VecDeque}; +use std::sync::Arc; use glam::{Vec2, IVec2}; use lyra_ecs::{world::World, system::IntoSystem};