diff options
Diffstat (limited to 'engine/src')
| -rw-r--r-- | engine/src/reflection.rs | 82 | ||||
| -rw-r--r-- | engine/src/renderer/opengl/graphics_mesh.rs | 10 | ||||
| -rw-r--r-- | engine/src/shader.rs | 6 |
3 files changed, 34 insertions, 64 deletions
diff --git a/engine/src/reflection.rs b/engine/src/reflection.rs index 7c5ee5c..13384fe 100644 --- a/engine/src/reflection.rs +++ b/engine/src/reflection.rs @@ -4,20 +4,26 @@ use std::fmt::Debug; pub use engine_macros::Reflection; -pub trait With: 'static +pub trait Reflection: 'static { - const REFLECTION: &Reflection; + const TYPE_REFLECTION: &Type; - fn reflection() -> &'static Reflection + fn type_reflection() -> &'static Type where - Self: Sized; + Self: Sized, + { + Self::TYPE_REFLECTION + } - fn get_reflection(&self) -> &'static Reflection; + fn get_type_reflection(&self) -> &'static Type + { + Self::TYPE_REFLECTION + } } #[derive(Debug)] #[non_exhaustive] -pub enum Reflection +pub enum Type { Struct(Struct), Array(Array), @@ -25,7 +31,7 @@ pub enum Reflection Literal(Literal), } -impl Reflection +impl Type { pub const fn as_struct(&self) -> Option<&Struct> { @@ -51,28 +57,28 @@ pub struct StructField pub byte_offset: usize, pub type_id: TypeId, pub type_name: &'static str, - pub get_reflection: FnWithDebug<Option<&'static Reflection>>, + pub get_type: FnWithDebug<Option<&'static Type>>, } impl StructField { - pub fn reflection(&self) -> Option<&'static Reflection> + pub fn type_reflection(&self) -> Option<&'static Type> { - self.get_reflection.get() + self.get_type.get() } } #[derive(Debug, Clone)] pub struct Array { - pub item_reflection: &'static Reflection, + pub item_reflection: &'static Type, pub length: usize, } #[derive(Debug, Clone)] pub struct Slice { - pub item_reflection: &'static Reflection, + pub item_reflection: &'static Type, } #[derive(Debug)] @@ -86,25 +92,13 @@ pub struct Literal macro_rules! impl_with_for_literals { ($($literal: ty),*) => { $( - impl With for $literal + impl Reflection for $literal { - const REFLECTION: &Reflection = &Reflection::Literal(Literal { + const TYPE_REFLECTION: &Type = &Type::Literal(Literal { layout: Layout::new::<$literal>(), type_id: TypeId::of::<$literal>(), type_name: || type_name::<$literal>() }); - - fn reflection() -> &'static Reflection - where - Self: Sized - { - Self::REFLECTION - } - - fn get_reflection(&self) -> &'static Reflection - { - Self::reflection() - } } )* }; @@ -128,42 +122,18 @@ impl_with_for_literals!( &'static str ); -impl<T: With, const LEN: usize> With for [T; LEN] +impl<T: Reflection, const LEN: usize> Reflection for [T; LEN] { - const REFLECTION: &Reflection = &Reflection::Array(Array { - item_reflection: T::REFLECTION, + const TYPE_REFLECTION: &Type = &Type::Array(Array { + item_reflection: T::TYPE_REFLECTION, length: LEN, }); - - fn reflection() -> &'static Reflection - where - Self: Sized, - { - Self::REFLECTION - } - - fn get_reflection(&self) -> &'static Reflection - { - Self::reflection() - } } -impl<T: With> With for &'static [T] +impl<T: Reflection> Reflection for &'static [T] { - const REFLECTION: &Reflection = - &Reflection::Slice(Slice { item_reflection: T::REFLECTION }); - - fn reflection() -> &'static Reflection - where - Self: Sized, - { - Self::REFLECTION - } - - fn get_reflection(&self) -> &'static Reflection - { - Self::reflection() - } + const TYPE_REFLECTION: &Type = + &Type::Slice(Slice { item_reflection: T::TYPE_REFLECTION }); } #[derive(Clone)] diff --git a/engine/src/renderer/opengl/graphics_mesh.rs b/engine/src/renderer/opengl/graphics_mesh.rs index d62cd3e..cdd0e8d 100644 --- a/engine/src/renderer/opengl/graphics_mesh.rs +++ b/engine/src/renderer/opengl/graphics_mesh.rs @@ -12,7 +12,7 @@ use opengl_bindings::vertex_array::{ use zerocopy::IntoBytes; use crate::mesh::Mesh; -use crate::reflection::Reflection; +use crate::reflection::Type; use crate::shader::VertexSubset as ShaderVertexSubset; #[derive(Debug)] @@ -105,8 +105,8 @@ impl GraphicsMesh vertex_arr.set_attrib_format( current_context, attrib_index, - match vertex_subset_field.reflection.reflection() { - Some(Reflection::Literal(_)) => { + match vertex_subset_field.reflection.type_reflection() { + Some(Type::Literal(_)) => { if vertex_subset_field.reflection.type_id != TypeId::of::<f32>() { panic!("Unsupported vertex field data type"); } @@ -118,8 +118,8 @@ impl GraphicsMesh offset: vertex_subset_field.offset.try_into().unwrap(), } } - Some(Reflection::Array(array_vertex_field)) => { - let Reflection::Literal(array_vertex_field_item) = + Some(Type::Array(array_vertex_field)) => { + let Type::Literal(array_vertex_field_item) = array_vertex_field.item_reflection else { panic!("Unsupported array item type in vertex field"); diff --git a/engine/src/shader.rs b/engine/src/shader.rs index 2b16c47..f23a366 100644 --- a/engine/src/shader.rs +++ b/engine/src/shader.rs @@ -34,9 +34,9 @@ use crate::asset::{ use crate::builder; use crate::mesh::Vertex; use crate::reflection::{ + Reflection, Struct as StructReflection, StructField as StructFieldReflection, - With, }; use crate::shader::default::{ ASSET_LABEL, @@ -738,7 +738,7 @@ pub struct VertexSubset { pub layout: Layout, pub fields: [Option<VertexSubsetField>; const { - Vertex::REFLECTION.as_struct().unwrap().fields.len() + Vertex::TYPE_REFLECTION.as_struct().unwrap().fields.len() }], } @@ -749,7 +749,7 @@ impl VertexSubset ) -> Result<Self, VertexSubsetError> { const VERTEX_REFLECTION: &StructReflection = - const { Vertex::REFLECTION.as_struct().unwrap() }; + const { Vertex::TYPE_REFLECTION.as_struct().unwrap() }; if vs_entrypoint.stage() != Stage::Vertex { return Err(VertexSubsetError::EntrypointNotInVertexStage); |
