Skip to content

Commit 84d86f7

Browse files
committed
[update] mesh building to not require cloning vertices and attributes which can get expensive for larger meshes
1 parent ab6f5d9 commit 84d86f7

File tree

14 files changed

+88
-99
lines changed

14 files changed

+88
-99
lines changed

crates/lambda-rs/src/render/mesh.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl MeshBuilder {
6464

6565
/// Allocates memory for the given number of vertices and fills
6666
/// the mesh with empty vertices.
67-
pub fn with_capacity(&mut self, size: usize) -> &mut Self {
67+
pub fn with_capacity(mut self, size: usize) -> Self {
6868
self.vertices.resize(
6969
size,
7070
Vertex {
@@ -77,34 +77,32 @@ impl MeshBuilder {
7777
}
7878

7979
/// Adds a vertex to the mesh.
80-
pub fn with_vertex(&mut self, vertex: Vertex) -> &mut Self {
80+
pub fn with_vertex(mut self, vertex: Vertex) -> Self {
8181
self.vertices.push(vertex);
8282
return self;
8383
}
8484

8585
/// Specify the attributes of the mesh. This is used to map the vertex data to
8686
/// the input of the vertex shader.
87-
pub fn with_attributes(
88-
&mut self,
89-
attributes: Vec<VertexAttribute>,
90-
) -> &mut Self {
87+
pub fn with_attributes(mut self, attributes: Vec<VertexAttribute>) -> Self {
9188
self.attributes = attributes;
9289
return self;
9390
}
9491

9592
/// Builds a mesh from the vertices and indices that have been added to the
9693
/// builder and allocates the memory for the mesh on the GPU.
97-
pub fn build(&self) -> Mesh {
94+
pub fn build(self) -> Mesh {
9895
return Mesh {
99-
vertices: self.vertices.clone(),
100-
attributes: self.attributes.clone(),
96+
vertices: self.vertices,
97+
attributes: self.attributes,
10198
};
10299
}
103100

104101
/// Builds a mesh from the vertices of an OBJ file. The mesh will have the same
105102
/// attributes as the OBJ file and can be allocated on to the GPU with
106103
/// `BufferBuilder::build_from_mesh`.
107-
pub fn build_from_obj(&self, file_path: &str) -> Mesh {
104+
pub fn build_from_obj(self, file_path: &str) -> Mesh {
105+
let _ = self;
108106
let obj = load_textured_obj_from_file(file_path);
109107

110108
let vertices = obj
@@ -170,17 +168,16 @@ mod tests {
170168
/// the built mesh.
171169
#[test]
172170
fn mesh_builder_capacity_and_attributes_are_applied() {
173-
let mut builder = MeshBuilder::new();
174-
builder.with_capacity(2);
171+
let builder = MeshBuilder::new().with_capacity(2);
175172
assert_eq!(builder.vertices.len(), 2);
176173

177-
builder.with_vertex(Vertex {
178-
position: [1.0, 2.0, 3.0],
179-
normal: [0.0, 1.0, 0.0],
180-
color: [0.5, 0.5, 0.5],
181-
});
182-
183-
let mesh = builder.build();
174+
let mesh = builder
175+
.with_vertex(Vertex {
176+
position: [1.0, 2.0, 3.0],
177+
normal: [0.0, 1.0, 0.0],
178+
color: [0.5, 0.5, 0.5],
179+
})
180+
.build();
184181
assert_eq!(mesh.vertices().len(), 3);
185182
}
186183

demos/physics/src/bin/physics_falling_quad.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,12 @@ impl Component<ComponentResult, String> for FallingQuadDemo {
175175
.build(),
176176
];
177177

178-
let mut mesh_builder = MeshBuilder::new();
179-
vertices.iter().for_each(|vertex| {
180-
mesh_builder.with_vertex(*vertex);
181-
});
182-
183-
let mesh = mesh_builder
178+
let mesh = vertices
179+
.iter()
180+
.copied()
181+
.fold(MeshBuilder::new(), |builder, vertex| {
182+
return builder.with_vertex(vertex);
183+
})
184184
.with_attributes(vec![
185185
VertexAttribute {
186186
location: 0,

demos/physics/src/bin/physics_rigid_bodies_2d.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,12 @@ impl Component<ComponentResult, String> for RigidBodies2DDemo {
350350
.build(),
351351
];
352352

353-
let mut mesh_builder = MeshBuilder::new();
354-
vertices.iter().for_each(|vertex| {
355-
mesh_builder.with_vertex(*vertex);
356-
});
357-
358-
let mesh = mesh_builder
353+
let mesh = vertices
354+
.iter()
355+
.copied()
356+
.fold(MeshBuilder::new(), |builder, vertex| {
357+
return builder.with_vertex(vertex);
358+
})
359359
.with_attributes(vec![
360360
VertexAttribute {
361361
location: 0,

demos/render/src/bin/immediates.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,12 @@ impl Component<ComponentResult, String> for ImmediatesExample {
149149
.build(),
150150
];
151151

152-
let mut mesh_builder = MeshBuilder::new();
153-
vertices.iter().for_each(|vertex| {
154-
mesh_builder.with_vertex(*vertex);
155-
});
156-
157-
let mesh = mesh_builder
152+
let mesh = vertices
153+
.iter()
154+
.copied()
155+
.fold(MeshBuilder::new(), |builder, vertex| {
156+
return builder.with_vertex(vertex);
157+
})
158158
.with_attributes(vec![
159159
VertexAttribute {
160160
location: 0,

demos/render/src/bin/offscreen_post.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,11 @@ impl OffscreenPostExample {
338338
.build(),
339339
];
340340

341-
let mut mesh_builder = MeshBuilder::new();
342-
for v in vertices {
343-
mesh_builder.with_vertex(v);
344-
}
345-
346-
return mesh_builder
341+
return vertices
342+
.into_iter()
343+
.fold(MeshBuilder::new(), |builder, vertex| {
344+
return builder.with_vertex(vertex);
345+
})
347346
.with_attributes(vec![
348347
VertexAttribute {
349348
location: 0,

demos/render/src/bin/reflective_room.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -870,11 +870,11 @@ fn build_unit_cube_mesh() -> Mesh {
870870
[(h, -h, -h), (-h, -h, -h), (-h, h, -h), (h, h, -h)],
871871
);
872872

873-
let mut mesh_builder = MeshBuilder::new();
874-
for v in verts.into_iter() {
875-
mesh_builder.with_vertex(v);
876-
}
877-
let mesh = mesh_builder
873+
let mesh = verts
874+
.into_iter()
875+
.fold(MeshBuilder::new(), |builder, vertex| {
876+
return builder.with_vertex(vertex);
877+
})
878878
.with_attributes(vec![
879879
VertexAttribute {
880880
location: 0,
@@ -914,18 +914,16 @@ fn build_floor_quad_mesh(extent: f32) -> Mesh {
914914
let p2 = v(h, h);
915915
let p3 = v(-h, h);
916916

917-
let mut mesh_builder = MeshBuilder::new();
918917
// Tri winding flipped to face +Y (avoid back-face cull)
919-
// Tri 1
920-
mesh_builder.with_vertex(p0);
921-
mesh_builder.with_vertex(p2);
922-
mesh_builder.with_vertex(p1);
923-
// Tri 2
924-
mesh_builder.with_vertex(p0);
925-
mesh_builder.with_vertex(p3);
926-
mesh_builder.with_vertex(p2);
927-
928-
let mesh = mesh_builder
918+
let mesh = MeshBuilder::new()
919+
// Tri 1
920+
.with_vertex(p0)
921+
.with_vertex(p2)
922+
.with_vertex(p1)
923+
// Tri 2
924+
.with_vertex(p0)
925+
.with_vertex(p3)
926+
.with_vertex(p2)
929927
.with_attributes(vec![
930928
VertexAttribute {
931929
location: 0,

demos/render/src/bin/textured_cube.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,11 @@ impl Component<ComponentResult, String> for TexturedCubeExample {
262262
[(h, -h, -h), (-h, -h, -h), (-h, h, -h), (h, h, -h)],
263263
);
264264

265-
let mut mesh_builder = MeshBuilder::new();
266-
for v in verts.into_iter() {
267-
mesh_builder.with_vertex(v);
268-
}
269-
let mesh = mesh_builder
265+
let mesh = verts
266+
.into_iter()
267+
.fold(MeshBuilder::new(), |builder, vertex| {
268+
return builder.with_vertex(vertex);
269+
})
270270
.with_attributes(vec![
271271
VertexAttribute {
272272
location: 0,

demos/render/src/bin/textured_quad.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,12 @@ impl Component<ComponentResult, String> for TexturedQuadExample {
163163
.build(), // uv (0,1)
164164
];
165165

166-
let mut mesh_builder = MeshBuilder::new();
167-
vertices.iter().for_each(|v| {
168-
mesh_builder.with_vertex(*v);
169-
});
170-
let mesh = mesh_builder
166+
let mesh = vertices
167+
.iter()
168+
.copied()
169+
.fold(MeshBuilder::new(), |builder, vertex| {
170+
return builder.with_vertex(vertex);
171+
})
171172
.with_attributes(vec![
172173
VertexAttribute {
173174
location: 0,

demos/render/src/bin/uniform_buffer_triangle.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,12 @@ impl Component<ComponentResult, String> for UniformBufferExample {
148148
.build(),
149149
];
150150

151-
let mut mesh_builder = MeshBuilder::new();
152-
vertices.iter().for_each(|vertex| {
153-
mesh_builder.with_vertex(*vertex);
154-
});
155-
156-
let mesh = mesh_builder
151+
let mesh = vertices
152+
.iter()
153+
.copied()
154+
.fold(MeshBuilder::new(), |builder, vertex| {
155+
return builder.with_vertex(vertex);
156+
})
157157
.with_attributes(vec![
158158
VertexAttribute {
159159
location: 0,

docs/tutorials/physics/basics/falling-quad-kinematic.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -468,12 +468,10 @@ fn on_attach(
468468
.build(),
469469
];
470470

471-
let mut mesh_builder = MeshBuilder::new();
472-
vertices.iter().for_each(|vertex| {
473-
mesh_builder.with_vertex(*vertex);
474-
});
475-
476-
let mesh = mesh_builder
471+
let mesh = vertices
472+
.iter()
473+
.copied()
474+
.fold(MeshBuilder::new(), |builder, vertex| builder.with_vertex(vertex))
477475
.with_attributes(vec![
478476
VertexAttribute {
479477
location: 0,

0 commit comments

Comments
 (0)