Skip to content

Commit db53485

Browse files
committed
[update] gfx pipelines to take in push constants.
1 parent fbded39 commit db53485

3 files changed

Lines changed: 63 additions & 11 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ pub enum Command<RenderBackend: gfx_hal::Backend> {
9090
Draw {
9191
vertices: Range<u32>,
9292
},
93+
PushConstants {
94+
pipeline: Rc<RenderPipeline<RenderBackend>>,
95+
stage: super::pipeline::PipelineStage,
96+
offset: u32,
97+
bytes: &'static [u32],
98+
},
9399
EndRecording,
94100
}
95101

@@ -167,6 +173,17 @@ impl<'command_pool, RenderBackend: gfx_hal::Backend>
167173
)
168174
}
169175
Command::EndRenderPass => self.command_buffer.end_render_pass(),
176+
Command::PushConstants {
177+
pipeline,
178+
stage,
179+
offset,
180+
bytes,
181+
} => self.command_buffer.push_graphics_constants(
182+
super::pipeline::internal::pipeline_layout_for(pipeline.as_ref()),
183+
stage,
184+
offset,
185+
bytes,
186+
),
170187
Command::Draw { vertices } => self.command_buffer.draw(vertices, 0..1),
171188
Command::EndRecording => self.command_buffer.finish(),
172189
}

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,16 @@ pub mod internal {
2323
) -> &RenderBackend::GraphicsPipeline {
2424
return &pipeline.pipeline;
2525
}
26+
27+
pub fn pipeline_layout_for<RenderBackend: gfx_hal::Backend>(
28+
pipeline: &super::RenderPipeline<RenderBackend>,
29+
) -> &RenderBackend::PipelineLayout {
30+
return &pipeline.pipeline_layout;
31+
}
2632
}
2733

34+
use std::ops::Range;
35+
2836
use gfx_hal::device::Device;
2937

3038
use super::{
@@ -35,14 +43,41 @@ use super::{
3543
/// Builder for a gfx-hal backed render pipeline.
3644
pub struct RenderPipelineBuilder<RenderBackend: internal::Backend> {
3745
pipeline_layout: Option<RenderBackend::PipelineLayout>,
46+
push_constants: Vec<PushConstantUpload>,
3847
}
3948

49+
pub type PipelineStage = gfx_hal::pso::ShaderStageFlags;
50+
51+
pub type PushConstantUpload = (PipelineStage, Range<u32>);
52+
4053
impl<RenderBackend: internal::Backend> RenderPipelineBuilder<RenderBackend> {
4154
pub fn new() -> Self {
4255
return Self {
4356
pipeline_layout: None,
57+
push_constants: Vec::new(),
4458
};
4559
}
60+
61+
/// Adds a push constant to the render pipeline at the set PipelineStage(s)
62+
pub fn with_push_constant(
63+
mut self,
64+
stage: PipelineStage,
65+
bytes: u32,
66+
) -> Self {
67+
self.push_constants.push((stage, 0..bytes));
68+
return self;
69+
}
70+
71+
/// Adds multiple push constants to the render pipeline at their
72+
/// set PipelineStage(s)
73+
pub fn with_push_constants(
74+
mut self,
75+
push_constants: Vec<PushConstantUpload>,
76+
) -> Self {
77+
self.push_constants.extend(push_constants);
78+
return self;
79+
}
80+
4681
/// Builds a render pipeline based on your builder configuration. You can
4782
/// configure a render pipeline to be however you'd like it to be.
4883
pub fn build(
@@ -57,7 +92,10 @@ impl<RenderBackend: internal::Backend> RenderPipelineBuilder<RenderBackend> {
5792
let pipeline_layout = unsafe {
5893
use internal::Device;
5994
super::internal::logical_device_for(gpu)
60-
.create_pipeline_layout(vec![].into_iter(), vec![].into_iter())
95+
.create_pipeline_layout(
96+
vec![].into_iter(),
97+
self.push_constants.into_iter(),
98+
)
6199
.expect(
62100
"The GPU does not have enough memory to allocate a pipeline layout",
63101
)

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,16 +224,13 @@ impl<'builder> RenderPassBuilder<'builder> {
224224
};
225225

226226
let render_pass = unsafe {
227-
super::internal::logical_device_for(gpu)
228-
.create_render_pass(
229-
attachments.into_iter(),
230-
subpasses.into_iter(),
231-
vec![].into_iter(),
232-
)
233-
.expect(
234-
"The GPU does not have enough memory to allocate a render pass.",
235-
)
236-
};
227+
super::internal::logical_device_for(gpu).create_render_pass(
228+
attachments.into_iter(),
229+
subpasses.into_iter(),
230+
vec![].into_iter(),
231+
)
232+
}
233+
.expect("The GPU does not have enough memory to allocate a render pass.");
237234

238235
return RenderPass { render_pass };
239236
}

0 commit comments

Comments
 (0)