summaryrefslogtreecommitdiff
path: root/engine/src/mesh/vertex_buffer.rs
diff options
context:
space:
mode:
Diffstat (limited to 'engine/src/mesh/vertex_buffer.rs')
-rw-r--r--engine/src/mesh/vertex_buffer.rs51
1 files changed, 29 insertions, 22 deletions
diff --git a/engine/src/mesh/vertex_buffer.rs b/engine/src/mesh/vertex_buffer.rs
index 9d77270..4df3ffc 100644
--- a/engine/src/mesh/vertex_buffer.rs
+++ b/engine/src/mesh/vertex_buffer.rs
@@ -30,9 +30,9 @@ impl<const LEN: usize> VertexAttrValue for [f32; LEN]
}
#[derive(Debug)]
-pub struct NamedVertexAttr<'name, Value: VertexAttrValue>
+pub struct NamedVertexAttr<Value: VertexAttrValue>
{
- pub name: &'name str,
+ pub label: VertexLabel,
pub value: Value,
}
@@ -40,7 +40,7 @@ pub struct NamedVertexAttr<'name, Value: VertexAttrValue>
#[non_exhaustive]
pub struct VertexAttrProperties
{
- pub name: Cow<'static, str>,
+ pub label: VertexLabel,
pub ty: VertexAttrType,
pub layout: Layout,
pub byte_offset: usize,
@@ -49,10 +49,20 @@ pub struct VertexAttrProperties
#[derive(Debug)]
pub struct VertexAttrInfo
{
- pub name: Cow<'static, str>,
+ pub label: VertexLabel,
pub ty: VertexAttrType,
}
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub enum VertexLabel
+{
+ Position,
+ Normal,
+ Uv,
+ Color,
+ Other(Cow<'static, str>),
+}
+
#[derive(Debug, Clone, Default)]
pub struct VertexBuffer
{
@@ -67,8 +77,8 @@ impl VertexBuffer
{
let mut vertex_attr_props = vertex_attrs
.iter()
- .map(|VertexAttrInfo { name, ty }| VertexAttrProperties {
- name: name.clone(),
+ .map(|VertexAttrInfo { label, ty }| VertexAttrProperties {
+ label: label.clone(),
ty: ty.clone(),
layout: ty.layout(),
byte_offset: 0,
@@ -78,10 +88,9 @@ impl VertexBuffer
let mut vertex_layout = Layout::new::<()>();
for VertexAttrProperties {
- name: _,
- ty: _,
layout: vertex_attr_layout,
byte_offset: vertex_attr_byte_offset,
+ ..
} in &mut vertex_attr_props
{
let (new_struct_layout, byte_offset) =
@@ -100,10 +109,7 @@ impl VertexBuffer
}
}
- pub fn push<'name, VertexAttrs: NamedVertexAttrs<'name>>(
- &mut self,
- vertex: VertexAttrs,
- )
+ pub fn push<VertexAttrs: NamedVertexAttrs>(&mut self, vertex: VertexAttrs)
{
assert_eq!(
self.vertex_attr_props.len(),
@@ -119,11 +125,11 @@ impl VertexBuffer
let vertex_attrs = vertex.vertex_attrs();
- for (vertex_attr_name, vertex_attr_bytes, vertex_attr_ty) in vertex_attrs {
+ for (vertex_label, vertex_attr_bytes, vertex_attr_ty) in vertex_attrs {
let vertex_attr_props = self
.vertex_attr_props
.iter()
- .find(|vertex_attr_props| vertex_attr_props.name == vertex_attr_name)
+ .find(|vertex_attr_props| &vertex_attr_props.label == vertex_label)
.unwrap();
assert_eq!(vertex_attr_ty, vertex_attr_props.ty);
@@ -170,13 +176,13 @@ impl VertexBuffer
pub fn iter<VertexAttr: VertexAttrValue>(
&self,
- vertex_attr_name: &str,
+ vertex_label: VertexLabel,
) -> Iter<'_, VertexAttr>
{
let vertex_attr_props = self
.vertex_attr_props
.iter()
- .find(|vertex_attr_props| vertex_attr_props.name == vertex_attr_name)
+ .find(|vertex_attr_props| vertex_attr_props.label == vertex_label)
.unwrap();
assert_eq!(VertexAttr::ty(), vertex_attr_props.ty);
@@ -217,18 +223,19 @@ impl<'a, VertexAttr: VertexAttrValue> Iterator for Iter<'a, VertexAttr>
}
}
-pub trait NamedVertexAttrs<'name>
+pub trait NamedVertexAttrs
{
fn vertex_attr_cnt(&self) -> usize;
- fn vertex_attrs(&self) -> impl Iterator<Item = (&'name str, &[u8], VertexAttrType)>;
+ fn vertex_attrs(&self)
+ -> impl Iterator<Item = (&VertexLabel, &[u8], VertexAttrType)>;
}
macro_rules! impl_named_vertex_attrs {
($cnt: tt) => {
seq!(I in 0..$cnt {
- impl<'name, #(VertexAttr~I: VertexAttrValue,)*>
- NamedVertexAttrs<'name> for (#(NamedVertexAttr<'name, VertexAttr~I>,)*)
+ impl<#(VertexAttr~I: VertexAttrValue,)*>
+ NamedVertexAttrs for (#(NamedVertexAttr<VertexAttr~I>,)*)
{
fn vertex_attr_cnt(&self) -> usize
{
@@ -236,10 +243,10 @@ macro_rules! impl_named_vertex_attrs {
}
fn vertex_attrs(&self)
- -> impl Iterator<Item = (&'name str, &[u8], VertexAttrType)>
+ -> impl Iterator<Item = (&VertexLabel, &[u8], VertexAttrType)>
{
[#(
- (self.I.name, self.I.value.as_bytes(), VertexAttr~I::ty()),
+ (&self.I.label, self.I.value.as_bytes(), VertexAttr~I::ty()),
)*].into_iter()
}
}