deltanedas 2 лет назад
Родитель
Сommit
0223355986

+ 1 - 1
Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "chair"
-version = "0.3.1"
+version = "0.4.0"
 edition = "2021"
 description = "An efficient binary mesh format which is both small and extremely fast to read."
 readme = "README.md"

+ 1 - 1
examples/viewer/Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "viewer"
-version = "0.2.0"
+version = "0.4.0"
 edition = "2021"
 
 [dependencies]

+ 0 - 1
examples/viewer/src/render.rs

@@ -4,6 +4,5 @@ mod depth;
 mod object;
 mod pipeline;
 mod renderer;
-mod vertex;
 
 pub use renderer::Renderer;

+ 2 - 3
examples/viewer/src/render/pipeline.rs

@@ -1,10 +1,9 @@
 use crate::render::{
 	depth::*,
-	object::*,
-	vertex::*
+	object::*
 };
 
-use chair::Vertex;
+use chair::{TexturedVertex, Vertex};
 use wgpu::*;
 
 pub fn mesh(device: &Device, config: &SurfaceConfiguration, bind_layouts: &[&BindGroupLayout]) -> RenderPipeline {

+ 2 - 3
examples/viewer/src/render/renderer.rs

@@ -3,8 +3,7 @@ use crate::render::{
 	camera::*,
 	depth::*,
 	object::*,
-	pipeline,
-	vertex::*
+	pipeline
 };
 
 use std::{
@@ -14,7 +13,7 @@ use std::{
 };
 
 use bytemuck::cast_slice;
-use chair::{Mesh, MeshReader};
+use chair::{Mesh, MeshReader, TexturedVertex};
 use wgpu::{
 	util::{BufferInitDescriptor, DeviceExt},
 	*

+ 0 - 31
examples/viewer/src/render/vertex.rs

@@ -1,31 +0,0 @@
-use chair::*;
-use wgpu::*;
-
-#[derive(Clone, Copy, Debug, Pod, Zeroable)]
-#[repr(C)]
-pub struct TexturedVertex {
-	pub pos: [f32; 3],
-	pub uvs: [f32; 2]
-}
-
-const ATTRIBS: &[VertexAttribute] = &vertex_attr_array![
-	0 => Float32x3,
-	1 => Float32x2
-];
-
-impl Vertex for TexturedVertex {
-	fn new(pos: [f32; 3], _: [f32; 4], _: [f32; 3], uvs: [f32; 2]) -> Self {
-		Self {
-			pos,
-			uvs
-		}
-	}
-
-	fn features() -> MeshFeatures {
-		features!(Textured)
-	}
-
-	fn attribs() -> &'static [VertexAttribute] {
-		ATTRIBS
-	}
-}

+ 24 - 10
src/mesh.rs

@@ -42,19 +42,33 @@ impl Mesh {
 		}
 	}
 
-	/// Draw a single instance of this mesh.
-	/// Drawing indexed or indirectly is prefered.
-	pub fn draw<'a>(&'a self, pass: &mut RenderPass<'a>) {
-		pass.set_vertex_buffer(0, self.vertices.slice(..));
-		pass.set_index_buffer(self.indices.slice(..), INDEX_FORMAT);
-		pass.draw_indexed(0..self.index_count, 0, 0..1);
+	/// Draw a single instance of this mesh, using slot 0 for the vertex buffer.
+	/// Ideally use `draw_instanced`.
+	pub fn draw<'m: 'p, 'p>(&'m self, pass: &mut RenderPass<'p>) {
+		self.draw_instanced(pass, 0..1);
 	}
 
 	/// Draw multiple instances of this mesh, the instance buffer must be bound already.
-	/// Drawing indirectly is prefered.
-	pub fn draw_instanced<'a>(&'a self, pass: &mut RenderPass<'a>, instances: Range<u32>) {
-		pass.set_vertex_buffer(0, self.vertices.slice(..));
-		pass.set_index_buffer(self.indices.slice(..), INDEX_FORMAT);
+	/// Slot 0 is used for the vertex buffer
+	/// Drawing indirectly can have greater performance, use `bind` and call whichever indirect drawing function you need.
+	pub fn draw_instanced<'m: 'p, 'p>(&'m self, pass: &mut RenderPass<'p>, instances: Range<u32>) {
+		self.bind(pass, 0);
 		pass.draw_indexed(0..self.index_count, 0, instances);
 	}
+
+	/// Bind this mesh's buffers for drawing
+	pub fn bind<'m: 'p, 'p>(&'m self, pass: &mut RenderPass<'p>, slot: u32) {
+		pass.set_vertex_buffer(slot, self.vertices.slice(..));
+		pass.set_index_buffer(self.indices.slice(..), INDEX_FORMAT);
+	}
+
+	/// Returns a reference to the vertex buffer
+	pub fn vertices(&self) -> &Buffer {
+		&self.vertices
+	}
+
+	/// Returns a reference to the index buffer
+	pub fn indices(&self) -> &Buffer {
+		&self.indices
+	}
 }

+ 49 - 8
src/vertex/impls.rs

@@ -13,6 +13,13 @@ pub struct BasicVertex {
 	pub pos: [f32; 3]
 }
 
+#[cfg(feature = "mesh")]
+impl BasicVertex {
+	const ATTRIBS: &'static [VertexAttribute] = &vertex_attr_array![
+		0 => Float32x3
+	];
+}
+
 impl Vertex for BasicVertex {
 	fn new(pos: [f32; 3], _: [f32; 4], _: [f32; 3], _: [f32; 2]) -> Self {
 		Self {
@@ -27,9 +34,7 @@ impl Vertex for BasicVertex {
 
 	#[cfg(feature = "mesh")]
 	fn attribs() -> &'static [VertexAttribute] {
-		&vertex_attr_array![
-			0 => Float32x3
-		]
+		Self::ATTRIBS
 	}
 }
 
@@ -41,6 +46,14 @@ pub struct ColouredVertex {
 	pub col: [f32; 4]
 }
 
+#[cfg(feature = "mesh")]
+impl ColouredVertex {
+	const ATTRIBS: &'static [VertexAttribute] = &vertex_attr_array![
+		0 => Float32x3,
+		1 => Float32x4
+	];
+}
+
 impl Vertex for ColouredVertex {
 	fn new(pos: [f32; 3], col: [f32; 4], _: [f32; 3], _: [f32; 2]) -> Self {
 		Self {
@@ -55,12 +68,40 @@ impl Vertex for ColouredVertex {
 
 	#[cfg(feature = "mesh")]
 	fn attribs() -> &'static [VertexAttribute] {
-		ATTRIBS
+		Self::ATTRIBS
 	}
 }
 
+/// A vertex with position and texture coordinates
+#[derive(Clone, Copy, Debug, Pod, Zeroable)]
+#[repr(C)]
+pub struct TexturedVertex {
+	pub pos: [f32; 3],
+	pub uvs: [f32; 2]
+}
+
 #[cfg(feature = "mesh")]
-const ATTRIBS: &[VertexAttribute] = &vertex_attr_array![
-	0 => Float32x3,
-	1 => Float32x4
-];
+impl TexturedVertex {
+	const ATTRIBS: &'static [VertexAttribute] = &vertex_attr_array![
+		0 => Float32x3,
+		1 => Float32x2
+	];
+}
+
+impl Vertex for TexturedVertex {
+	fn new(pos: [f32; 3], _: [f32; 4], _: [f32; 3], uvs: [f32; 2]) -> Self {
+		Self {
+			pos,
+			uvs
+		}
+	}
+
+	fn features() -> MeshFeatures {
+		features!(Textured)
+	}
+
+	#[cfg(feature = "mesh")]
+	fn attribs() -> &'static [VertexAttribute] {
+		Self::ATTRIBS
+	}
+}