Skip to content

Commit 41871eb

Browse files
committed
[update] the pipeline to support vertex buffers & attributes.
1 parent f9bdb39 commit 41871eb

6 files changed

Lines changed: 85 additions & 19 deletions

File tree

crates/lambda-platform/src/gfx/assembler.rs

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,77 @@
11
//! Primitive assembly for the graphics pipeline.
22
3-
use gfx_hal::pso;
3+
use gfx_hal::pso::{
4+
self,
5+
AttributeDesc,
6+
Element,
7+
VertexBufferDesc,
8+
};
9+
10+
use super::{
11+
buffer::Buffer,
12+
surface::ColorFormat,
13+
};
14+
15+
/// Attributes for a vertex.
16+
pub struct VertexAttribute {
17+
pub location: u32,
18+
pub offset: u32,
19+
pub element: Element<ColorFormat>,
20+
}
421

522
/// PrimitiveAssemblerBuilder for preparing PrimitiveAssemblers to use in the
623
/// lambda-platform Rendering pipeline.
7-
pub struct PrimitiveAssemblerBuilder {}
24+
pub struct PrimitiveAssemblerBuilder {
25+
buffer_descriptions: Vec<VertexBufferDesc>,
26+
attribute_descriptions: Vec<AttributeDesc>,
27+
}
828

929
impl PrimitiveAssemblerBuilder {
1030
pub fn new() -> Self {
11-
return Self {};
31+
return Self {
32+
buffer_descriptions: Vec::new(),
33+
attribute_descriptions: Vec::new(),
34+
};
1235
}
1336

1437
/// Build a primitive assembler given the lambda-platform vertex shader
15-
/// module.
38+
/// module. Buffers & attributes do not have to be tied to
1639
pub fn build<'shader, RenderBackend: gfx_hal::Backend>(
17-
self,
40+
&'shader mut self,
1841
vertex_shader: &'shader super::shader::ShaderModule<RenderBackend>,
42+
buffers: Option<&Vec<Buffer<RenderBackend>>>,
43+
attributes: Option<&Vec<VertexAttribute>>,
1944
) -> PrimitiveAssembler<'shader, RenderBackend> {
45+
let binding = self.buffer_descriptions.len() as u32;
46+
47+
match (buffers, attributes) {
48+
(Some(buffers), Some(attributes)) => {
49+
self.buffer_descriptions = buffers
50+
.iter()
51+
.map(|buffer| VertexBufferDesc {
52+
binding,
53+
stride: buffer.stride() as u32,
54+
rate: pso::VertexInputRate::Vertex,
55+
})
56+
.collect();
57+
58+
self.attribute_descriptions = attributes
59+
.iter()
60+
.map(|attribute| {
61+
return AttributeDesc {
62+
location: attribute.location,
63+
binding,
64+
element: attribute.element,
65+
};
66+
})
67+
.collect();
68+
}
69+
_ => {}
70+
}
71+
2072
let primitive_assembler = pso::PrimitiveAssemblerDesc::Vertex {
21-
buffers: &[],
22-
attributes: &[],
73+
buffers: self.buffer_descriptions.as_slice(),
74+
attributes: self.attribute_descriptions.as_slice(),
2375
input_assembler: pso::InputAssemblerDesc::new(
2476
pso::Primitive::TriangleList,
2577
),

crates/lambda-platform/src/gfx/buffer.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ use super::gpu::Gpu;
99
pub type Usage = gfx_hal::buffer::Usage;
1010
pub type Properties = gfx_hal::memory::Properties;
1111

12-
/// A buffer is a block of memory that can be used to store data that is accessed
12+
/// The type of buffers that can be allocated on the GPU.
1313
#[derive(Debug, Clone, Copy)]
1414
pub enum BufferType {
1515
Vertex,
1616
Index,
17+
Uniform,
18+
Storage,
1719
}
1820

1921
/// A buffer is a block of memory that can be used to store data that can be
@@ -26,7 +28,11 @@ pub struct Buffer<RenderBackend: super::internal::Backend> {
2628
buffer_type: BufferType,
2729
}
2830

29-
impl<RenderBackend: super::internal::Backend> Buffer<RenderBackend> {}
31+
impl<RenderBackend: super::internal::Backend> Buffer<RenderBackend> {
32+
pub fn stride(&self) -> usize {
33+
return self.stride;
34+
}
35+
}
3036

3137
pub struct BufferBuilder {
3238
buffer_length: usize,

crates/lambda-platform/src/gfx/pipeline.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,14 @@ impl<RenderBackend: internal::Backend> RenderPipelineBuilder<RenderBackend> {
106106
)
107107
};
108108

109-
let primitive_assembler =
110-
super::assembler::PrimitiveAssemblerBuilder::new().build(vertex_shader);
109+
// TODO(vmarcella): The primitive assembler should be configurable through
110+
// the RenderPipelineBuilder so that buffers & attributes can be bound.
111+
let mut builder = super::assembler::PrimitiveAssemblerBuilder::new();
112+
let primitive_assembler = builder.build(vertex_shader, None, None);
111113

112114
let fragment_entry = match fragment_shader {
113115
Some(shader) => Some(internal::EntryPoint::<RenderBackend> {
114-
entry: "main",
116+
entry: shader.entry(),
115117
module: super::internal::module_for(shader),
116118
specialization: gfx_hal::pso::Specialization::default(),
117119
}),

lambda/src/core/render/buffer.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
mod internal {
2+
// Placed these in an internal module to avoid a name collision with the
3+
// high level Buffer & BufferBuilder types in the parent module.
24
pub use lambda_platform::gfx::buffer::{
35
Buffer,
46
BufferBuilder,
@@ -23,6 +25,9 @@ pub struct Buffer {
2325
buffer_type: BufferType,
2426
}
2527

28+
/// A buffer is a block of memory that can be used to store data that can be
29+
/// accessed by the GPU. The buffer is created with a length, usage, and
30+
/// properties that determine how the buffer can be used.
2631
pub struct BufferBuilder {
2732
buffer_builder: internal::BufferBuilder,
2833
buffer_type: BufferType,
@@ -36,28 +41,32 @@ impl BufferBuilder {
3641
};
3742
}
3843

44+
/// Sets the length of the buffer (In bytes).
3945
pub fn with_length(&mut self, size: usize) -> &mut Self {
4046
self.buffer_builder.with_length(size);
4147
return self;
4248
}
4349

50+
/// Sets the type of buffer to create.
4451
pub fn with_buffer_type(&mut self, buffer_type: BufferType) -> &mut Self {
4552
self.buffer_type = buffer_type;
4653
self.buffer_builder.with_buffer_type(buffer_type);
4754
return self;
4855
}
4956

57+
/// Sets the usage of the buffer.
5058
pub fn with_usage(&mut self, usage: Usage) -> &mut Self {
5159
self.buffer_builder.with_usage(usage);
5260
return self;
5361
}
5462

63+
/// Sets the properties of the buffer.
5564
pub fn with_properties(&mut self, properties: Properties) -> &mut Self {
5665
self.buffer_builder.with_properties(properties);
5766
return self;
5867
}
5968

60-
/// Build a buffer from the render context.
69+
/// Build a buffer utilizing the current render context
6170
pub fn build<Data: Sized>(
6271
&self,
6372
render_context: &mut RenderContext,

lambda/src/core/render/mesh.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use super::{
1515
#[derive(Debug)]
1616
pub struct Mesh {
1717
vertices: Vec<Vertex>,
18-
indices: Vec<u32>,
1918
buffer: Buffer,
2019
}
2120

@@ -26,15 +25,13 @@ pub struct Mesh {
2625
pub struct MeshBuilder {
2726
capacity: usize,
2827
vertices: Vec<Vertex>,
29-
indices: Vec<u32>,
3028
}
3129

3230
impl MeshBuilder {
3331
pub fn new() -> Self {
3432
return Self {
3533
capacity: 0,
3634
vertices: vec![],
37-
indices: vec![],
3835
};
3936
}
4037

@@ -72,7 +69,6 @@ impl MeshBuilder {
7269
Ok(buffer) => {
7370
return Ok(Mesh {
7471
vertices: self.vertices.clone(),
75-
indices: self.indices.clone(),
7672
buffer,
7773
});
7874
}
@@ -90,7 +86,6 @@ mod tests {
9086
let mut mesh = super::MeshBuilder::new();
9187

9288
assert_eq!(mesh.vertices.len(), 0);
93-
assert_eq!(mesh.indices.len(), 0);
9489
}
9590

9691
// TODO(vmarcella): Add more tests for mesh building once the render context

lambda/src/core/render/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ impl RenderContext {
212212
return self.frame_buffer.as_ref().unwrap().clone();
213213
}
214214

215-
/// Allocates a command buffer and records commands to the GPU.
215+
/// Allocates a command buffer and records commands to the GPU. This is the
216+
/// primary entry point for submitting commands to the GPU and where rendering
217+
/// will occur.
216218
pub fn render(&mut self, commands: Vec<RenderCommand>) {
217219
let (width, height) = self
218220
.surface

0 commit comments

Comments
 (0)