Skip to content

Commit f9bdb39

Browse files
committed
[add] high level buffer implementation & introduce buffer types for use in the render pipeline.
1 parent 80bab08 commit f9bdb39

6 files changed

Lines changed: 133 additions & 22 deletions

File tree

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
use gfx_hal::{
2-
memory::{
3-
Segment,
4-
SparseFlags,
5-
},
6-
Backend,
1+
use gfx_hal::memory::{
2+
Segment,
3+
SparseFlags,
74
};
85

96
use super::gpu::Gpu;
@@ -12,12 +9,21 @@ use super::gpu::Gpu;
129
pub type Usage = gfx_hal::buffer::Usage;
1310
pub type Properties = gfx_hal::memory::Properties;
1411

12+
/// A buffer is a block of memory that can be used to store data that is accessed
13+
#[derive(Debug, Clone, Copy)]
14+
pub enum BufferType {
15+
Vertex,
16+
Index,
17+
}
18+
1519
/// A buffer is a block of memory that can be used to store data that can be
1620
/// accessed by the GPU.
1721
#[derive(Debug, Clone, Copy)]
1822
pub struct Buffer<RenderBackend: super::internal::Backend> {
1923
buffer: RenderBackend::Buffer,
2024
memory: RenderBackend::Memory,
25+
stride: usize,
26+
buffer_type: BufferType,
2127
}
2228

2329
impl<RenderBackend: super::internal::Backend> Buffer<RenderBackend> {}
@@ -26,14 +32,16 @@ pub struct BufferBuilder {
2632
buffer_length: usize,
2733
usage: Usage,
2834
properties: Properties,
35+
buffer_type: BufferType,
2936
}
3037

3138
impl BufferBuilder {
3239
pub fn new() -> Self {
3340
return Self {
3441
buffer_length: 0,
35-
usage: gfx_hal::buffer::Usage::empty(),
36-
properties: gfx_hal::memory::Properties::empty(),
42+
usage: Usage::empty(),
43+
properties: Properties::empty(),
44+
buffer_type: BufferType::Vertex,
3745
};
3846
}
3947

@@ -48,6 +56,12 @@ impl BufferBuilder {
4856
}
4957

5058
pub fn with_properties(&mut self, properties: Properties) -> &mut Self {
59+
self.properties = properties;
60+
return self;
61+
}
62+
63+
pub fn with_buffer_type(&mut self, buffer_type: BufferType) -> &mut Self {
64+
self.buffer_type = buffer_type;
5165
return self;
5266
}
5367

@@ -160,6 +174,8 @@ impl BufferBuilder {
160174
return Ok(Buffer {
161175
buffer,
162176
memory: buffer_memory,
177+
stride: std::mem::size_of::<Data>(),
178+
buffer_type: self.buffer_type,
163179
});
164180
}
165181
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use std::ops::Range;
3636
use gfx_hal::device::Device;
3737

3838
use super::{
39+
buffer::Buffer,
3940
gpu::Gpu,
4041
shader::ShaderModule,
4142
};
@@ -58,6 +59,10 @@ impl<RenderBackend: internal::Backend> RenderPipelineBuilder<RenderBackend> {
5859
};
5960
}
6061

62+
pub fn with_buffer(&mut self, buffer: &Buffer<RenderBackend>) -> &mut Self {
63+
todo!()
64+
}
65+
6166
/// Adds a push constant to the render pipeline at the set PipelineStage(s)
6267
pub fn with_push_constant(
6368
mut self,

lambda/src/core/render/buffer.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
mod internal {
2+
pub use lambda_platform::gfx::buffer::{
3+
Buffer,
4+
BufferBuilder,
5+
};
6+
}
7+
8+
// publicly use Properties and Usage from buffer.rs
9+
pub use lambda_platform::gfx::buffer::{
10+
BufferType,
11+
Properties,
12+
Usage,
13+
};
14+
15+
use super::{
16+
internal::mut_gpu_from_context,
17+
RenderContext,
18+
};
19+
20+
#[derive(Debug)]
21+
pub struct Buffer {
22+
buffer: internal::Buffer<super::internal::RenderBackend>,
23+
buffer_type: BufferType,
24+
}
25+
26+
pub struct BufferBuilder {
27+
buffer_builder: internal::BufferBuilder,
28+
buffer_type: BufferType,
29+
}
30+
31+
impl BufferBuilder {
32+
pub fn new() -> Self {
33+
return Self {
34+
buffer_builder: internal::BufferBuilder::new(),
35+
buffer_type: BufferType::Vertex,
36+
};
37+
}
38+
39+
pub fn with_length(&mut self, size: usize) -> &mut Self {
40+
self.buffer_builder.with_length(size);
41+
return self;
42+
}
43+
44+
pub fn with_buffer_type(&mut self, buffer_type: BufferType) -> &mut Self {
45+
self.buffer_type = buffer_type;
46+
self.buffer_builder.with_buffer_type(buffer_type);
47+
return self;
48+
}
49+
50+
pub fn with_usage(&mut self, usage: Usage) -> &mut Self {
51+
self.buffer_builder.with_usage(usage);
52+
return self;
53+
}
54+
55+
pub fn with_properties(&mut self, properties: Properties) -> &mut Self {
56+
self.buffer_builder.with_properties(properties);
57+
return self;
58+
}
59+
60+
/// Build a buffer from the render context.
61+
pub fn build<Data: Sized>(
62+
&self,
63+
render_context: &mut RenderContext,
64+
data: Vec<Data>,
65+
) -> Result<Buffer, &'static str> {
66+
let buffer_allocation = self
67+
.buffer_builder
68+
.build(&mut mut_gpu_from_context(render_context), data);
69+
70+
match buffer_allocation {
71+
Ok(buffer) => {
72+
return Ok(Buffer {
73+
buffer,
74+
buffer_type: self.buffer_type,
75+
});
76+
}
77+
Err(error) => {
78+
return Err(error);
79+
}
80+
}
81+
}
82+
}

lambda/src/core/render/mesh.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
use lambda_platform::gfx::buffer::{
2-
Buffer,
3-
BufferBuilder,
4-
Properties,
5-
Usage,
6-
};
7-
81
use super::{
2+
buffer::{
3+
Buffer,
4+
BufferBuilder,
5+
Properties,
6+
Usage,
7+
},
98
vertex::Vertex,
109
RenderContext,
1110
};
12-
use crate::core::render::internal::mut_gpu_from_context;
1311

1412
// ---------------------------------- Mesh ------------------------------------
1513

@@ -18,7 +16,7 @@ use crate::core::render::internal::mut_gpu_from_context;
1816
pub struct Mesh {
1917
vertices: Vec<Vertex>,
2018
indices: Vec<u32>,
21-
buffer: Buffer<super::internal::RenderBackend>,
19+
buffer: Buffer,
2220
}
2321

2422
// ------------------------------ MeshBuilder ---------------------------------
@@ -68,7 +66,7 @@ impl MeshBuilder {
6866
.with_length(gpu_memory_required)
6967
.with_usage(Usage::VERTEX)
7068
.with_properties(Properties::CPU_VISIBLE | Properties::COHERENT)
71-
.build(mut_gpu_from_context(render_context), self.vertices.clone());
69+
.build(render_context, self.vertices.clone());
7270

7371
match buffer_allocation {
7472
Ok(buffer) => {

lambda/src/core/render/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! windowing.
33
44
// Module Exports
5+
pub mod buffer;
56
pub mod command;
67
pub mod mesh;
78
pub mod pipeline;

lambda/src/core/render/pipeline.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use lambda_platform::gfx::shader::{
66
};
77

88
use super::{
9+
buffer::Buffer,
910
internal::{
1011
gpu_from_context,
1112
mut_gpu_from_context,
@@ -45,27 +46,35 @@ use lambda_platform::gfx::pipeline::PushConstantUpload;
4546

4647
pub struct RenderPipelineBuilder {
4748
push_constants: Vec<PushConstantUpload>,
49+
buffers: Vec<Buffer>,
4850
}
4951

5052
impl RenderPipelineBuilder {
5153
pub fn new() -> Self {
5254
return Self {
5355
push_constants: Vec::new(),
56+
buffers: Vec::new(),
5457
};
5558
}
5659

60+
/// Adds a buffer to the render pipeline.
61+
pub fn with_buffer(&mut self, buffer: Buffer) -> &mut Self {
62+
self.buffers.push(buffer);
63+
return self;
64+
}
65+
5766
pub fn with_push_constant(
58-
mut self,
67+
&mut self,
5968
stage: PipelineStage,
6069
bytes: u32,
61-
) -> Self {
70+
) -> &mut Self {
6271
self.push_constants.push((stage, 0..bytes));
6372
return self;
6473
}
6574

6675
/// Builds a render pipeline based on your builder configuration.
6776
pub fn build(
68-
self,
77+
&self,
6978
render_context: &mut RenderContext,
7079
render_pass: &super::render_pass::RenderPass,
7180
vertex_shader: &Shader,
@@ -88,7 +97,7 @@ impl RenderPipelineBuilder {
8897

8998
let render_pipeline =
9099
lambda_platform::gfx::pipeline::RenderPipelineBuilder::new()
91-
.with_push_constants(self.push_constants)
100+
.with_push_constants(self.push_constants.clone())
92101
.build(
93102
gpu_from_context(render_context),
94103
&platform_render_pass_from_render_pass(render_pass),

0 commit comments

Comments
 (0)