diff options
| author | HampusM <hampus@hampusmat.com> | 2026-04-06 20:48:36 +0200 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-04-06 20:48:36 +0200 |
| commit | 5853d458ae55443c79976893f822aaed508de013 (patch) | |
| tree | 02555763fda13d9dfb4fee00802dad205265144d | |
| parent | 942df064017258a92eee1a14cd613c6aec983dc8 (diff) | |
refactor(engine): improve names of reflection types & items
| -rw-r--r-- | engine-macros/src/lib.rs | 38 | ||||
| -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 |
4 files changed, 51 insertions, 85 deletions
diff --git a/engine-macros/src/lib.rs b/engine-macros/src/lib.rs index b87acf8..b7bbd43 100644 --- a/engine-macros/src/lib.rs +++ b/engine-macros/src/lib.rs @@ -63,49 +63,55 @@ pub fn reflection_derive(input: TokenStream) -> TokenStream byte_offset: std::mem::offset_of!(Self, #field_ident), type_id: std::any::TypeId::of::<#field_type>(), type_name: #field_type_name, - get_reflection: #engine_crate_path::reflection::FnWithDebug::new(|| { + get_type: #engine_crate_path::reflection::FnWithDebug::new(|| { struct SpecializationTarget<Field>(std::marker::PhantomData<Field>); trait FieldHasReflection { - fn field_reflection(&self) -> Option<&'static #engine_crate_path::reflection::Reflection>; + fn field_type_reflection(&self) + -> Option<&'static #engine_crate_path::reflection::Type>; } trait FieldDoesNotHaveReflection { - fn field_reflection(&self) -> Option<&'static #engine_crate_path::reflection::Reflection>; + fn field_type_reflection(&self) + -> Option<&'static #engine_crate_path::reflection::Type>; } impl<Field> FieldDoesNotHaveReflection for &SpecializationTarget<Field> { - fn field_reflection(&self) -> Option<&'static #engine_crate_path::reflection::Reflection> + fn field_type_reflection(&self) + -> Option<&'static #engine_crate_path::reflection::Type> { None } } - impl<Field: #engine_crate_path::reflection::With> FieldHasReflection for SpecializationTarget<Field> + impl<Field> FieldHasReflection for SpecializationTarget<Field> + where + Field: #engine_crate_path::reflection::Reflection { - fn field_reflection(&self) -> Option<&'static #engine_crate_path::reflection::Reflection> + fn field_type_reflection(&self) + -> Option<&'static #engine_crate_path::reflection::Type> { - Some(Field::reflection()) + Some(Field::type_reflection()) } } (&SpecializationTarget::<#field_type>(std::marker::PhantomData)) - .field_reflection() + .field_type_reflection() }) } } }); quote! { - impl #impl_generics #engine_crate_path::reflection::With for + impl #impl_generics #engine_crate_path::reflection::Reflection for #input_ident #type_generics #where_clause { - const REFLECTION: &#engine_crate_path::reflection::Reflection = + const TYPE_REFLECTION: &#engine_crate_path::reflection::Type = &const { - #engine_crate_path::reflection::Reflection::Struct( + #engine_crate_path::reflection::Type::Struct( #engine_crate_path::reflection::Struct { fields: &[ #(#fields),* @@ -113,16 +119,6 @@ pub fn reflection_derive(input: TokenStream) -> TokenStream } ) }; - - fn reflection() -> &'static #engine_crate_path::reflection::Reflection - { - Self::REFLECTION - } - - fn get_reflection(&self) -> &'static #engine_crate_path::reflection::Reflection - { - Self::reflection() - } } } .into() 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); |
