From 8b8019234730951f5bb4757a84c089ed8cb55f40 Mon Sep 17 00:00:00 2001 From: mars Date: Thu, 12 May 2022 08:42:33 -0600 Subject: [PATCH] Move predefined mesh attrs to dedicated struct --- src/main.rs | 5 +++-- src/pass/mesh.rs | 57 +++++++++++++++++++++++++++--------------------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8fa2164..8554747 100644 --- a/src/main.rs +++ b/src/main.rs @@ -68,16 +68,17 @@ fn main() { ]; let mesh_pass = resources.get::>().unwrap(); + let attributes = mesh_pass.get_attributes(); let example_vertices = AttrBuffer { - id: mesh_pass.get_vertex_attr_id(), + id: attributes.vertex, count: example_vertices.len(), data: bytemuck::cast_slice(&example_vertices).to_vec(), }; let example_indices: Vec = vec![0, 1, 2, 1, 2, 3, 2, 3, 0, 0, 2, 3]; let example_indices = AttrBuffer { - id: mesh_pass.get_index_attr_id(), + id: attributes.index, count: example_indices.len(), data: bytemuck::cast_slice(&example_indices).to_vec(), }; diff --git a/src/pass/mesh.rs b/src/pass/mesh.rs index 880955b..6f0027f 100644 --- a/src/pass/mesh.rs +++ b/src/pass/mesh.rs @@ -41,6 +41,27 @@ impl Vertex { pub type Index = u32; +#[derive(Clone)] +pub struct Attributes { + pub vertex: AttrId, + pub index: AttrId, +} + +impl Attributes { + pub fn new(attr_store: impl AsRef) -> Self { + Self { + vertex: attr_store.as_ref().get_type::(), + index: attr_store.as_ref().add(AttrInfo { + layout: AttrLayout { + size: std::mem::size_of::(), + }, + usages: wgpu::BufferUsages::INDEX, + default_pool_size: 1_000_000, + }) + } + } +} + #[derive(Clone, Debug)] pub struct TransformedMesh { pub mesh: MeshHandle, @@ -86,8 +107,7 @@ pub struct MeshPass { attr_store: Arc, shader_info: ShaderInfo, mesh_pool: Arc, - vertex_attr_id: AttrId, - index_attr_id: AttrId, + attributes: Attributes, mesh_layout_id: MeshLayoutId, skinning_bind_group_layout: wgpu::BindGroupLayout, skinning_pipeline: wgpu::ComputePipeline, @@ -106,19 +126,11 @@ impl MeshPass { ) -> Self { let attr_store = AttrStore::new(); let mesh_pool = MeshPool::new(device.clone(), attr_store.to_owned()); - - let vertex_attr_id = attr_store.get_type::(); - let index_attr_id = attr_store.add(AttrInfo { - layout: AttrLayout { - size: std::mem::size_of::(), - }, - usages: wgpu::BufferUsages::INDEX, - default_pool_size: 1_000_000, - }); + let attributes = Attributes::new(&attr_store); let mut mesh_layout = MeshLayoutDesc::new(); - mesh_layout.insert(vertex_attr_id, ()); - mesh_layout.insert(index_attr_id, ()); + mesh_layout.insert(attributes.vertex, ()); + mesh_layout.insert(attributes.index, ()); let mesh_layout_id = mesh_pool.add_layout(mesh_layout).unwrap(); let render_pipeline_layout = @@ -248,8 +260,7 @@ impl MeshPass { attr_store, shader_info, mesh_pool, - index_attr_id, - vertex_attr_id, + attributes, mesh_layout_id, skinning_bind_group_layout, skinning_pipeline, @@ -264,12 +275,8 @@ impl MeshPass { &self.mesh_pool } - pub fn get_vertex_attr_id(&self) -> AttrId { - self.vertex_attr_id - } - - pub fn get_index_attr_id(&self) -> AttrId { - self.index_attr_id + pub fn get_attributes(&self) -> &Attributes{ + &self.attributes } pub fn add_transformed_meshes(&self, meshes: &[TransformedMesh]) { @@ -330,7 +337,7 @@ impl RenderPass for MeshPass { } in mesh_bindings.iter() { let pools = self.mesh_pool.get_bindings(bindings.clone()); - let vertices_pool = pools.get(self.vertex_attr_id).unwrap(); + let vertices_pool = pools.get(self.attributes.vertex).unwrap(); // TODO defer bind group creation into separate Vec after GpuVecs have been written let bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor { @@ -377,8 +384,8 @@ impl RenderPass for MeshPass { }; for (mesh, infos) in instances { - let vertices = infos.iter().find(|i| i.0 == self.vertex_attr_id).unwrap().1; - let indices = infos.iter().find(|i| i.0 == self.index_attr_id).unwrap().1; + let vertices = infos.iter().find(|i| i.0 == self.attributes.vertex).unwrap().1; + let indices = infos.iter().find(|i| i.0 == self.attributes.index).unwrap().1; group.meshes.push(MeshCommand { vertex_offset: vertices.offset, @@ -482,7 +489,7 @@ impl RenderPass for MeshPass { cmds.set_bind_group(0, data.bind_viewport, &[]); for (bindings, meshes) in mesh_bindings.iter() { - let indices_pool = bindings.get(self.index_attr_id).unwrap(); + let indices_pool = bindings.get(self.attributes.index).unwrap(); cmds.set_vertex_buffer(0, data.frame_data.skinned_vertices.as_ref().slice(..)); cmds.set_index_buffer(