Skip to content

Commit 2600d0d

Browse files
committed
[add] a resize call to the render api.
1 parent d50c09f commit 2600d0d

5 files changed

Lines changed: 32 additions & 8 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Primitive assembly for the graphics pipeline.
2+
13
use gfx_hal::pso;
24

35
/// PrimitiveAssemblerBuilder for preparing PrimitiveAssemblers to use in the

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ impl SurfaceBuilder {
2424
return Self { name: None };
2525
}
2626

27+
/// Set the name of the surface.
2728
pub fn with_name(mut self, name: &str) -> Self {
2829
self.name = Some(name.to_string());
2930
return self;
@@ -161,6 +162,7 @@ impl SwapchainBuilder {
161162
return Self { size: (480, 360) };
162163
}
163164

165+
/// Set the size of the swapchain for the surface image.
164166
pub fn with_size(mut self, width: u32, height: u32) -> Self {
165167
self.size = (width, height);
166168
return self;
@@ -298,20 +300,14 @@ pub mod internal {
298300
return surface.frame_buffer_attachment.clone();
299301
}
300302

301-
pub fn surface_for<RenderBackend: gfx_hal::Backend>(
302-
surface: &mut super::Surface<RenderBackend>,
303-
) -> &mut RenderBackend::Surface {
304-
return &mut surface.gfx_hal_surface;
305-
}
306-
307303
/// Borrow the surface and take the image. This internal function is used for
308304
/// rendering and composes surface_for + take image.
309305
pub fn borrow_surface_and_take_image<RenderBackend: gfx_hal::Backend>(
310306
surface: &mut super::Surface<RenderBackend>,
311307
) -> (&mut RenderBackend::Surface, <RenderBackend::Surface as PresentationSurface<RenderBackend>>::SwapchainImage){
312308
return (
313309
&mut surface.gfx_hal_surface,
314-
surface.image.take().expect(""),
310+
surface.image.take().expect("Surface image is not present"),
315311
);
316312
}
317313
}

lambda/src/core/render/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,15 @@ impl RenderContext {
323323
None => {}
324324
}
325325
}
326+
327+
pub fn resize(&mut self, width: u32, height: u32) {
328+
let swapchain = SwapchainBuilder::new()
329+
.with_size(width, height)
330+
.build(&self.gpu, &self.surface);
331+
Rc::get_mut(&mut self.surface)
332+
.expect("Failed to acquire the surface to resize during")
333+
.apply_swapchain(&self.gpu, swapchain, 1_000_000_000);
334+
}
326335
}
327336

328337
type PlatformRenderCommand = Command<internal::RenderBackend>;

lambda/src/runtimes/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ impl Runtime for GenericRuntime {
153153
});
154154
}
155155
WinitWindowEvent::Resized(dims) => {
156+
active_render_api
157+
.as_mut()
158+
.unwrap()
159+
.resize(dims.width, dims.height);
160+
156161
publisher.publish_event(Events::Window {
157162
event: WindowEvent::Resize {
158163
width: dims.width,
@@ -162,6 +167,11 @@ impl Runtime for GenericRuntime {
162167
})
163168
}
164169
WinitWindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
170+
active_render_api
171+
.as_mut()
172+
.unwrap()
173+
.resize(new_inner_size.width, new_inner_size.height);
174+
165175
publisher.publish_event(Events::Window {
166176
event: WindowEvent::Resize {
167177
width: new_inner_size.width,

tools/lambda_rs_demo/src/main.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ pub struct DemoComponent {
4141
vertex_shader: Shader,
4242
render_pass: Option<Rc<RenderPass>>,
4343
render_pipeline: Option<Rc<RenderPipeline>>,
44+
width: u32,
45+
height: u32,
4446
}
4547

4648
impl Component<Events> for DemoComponent {
@@ -61,6 +63,8 @@ impl Component<Events> for DemoComponent {
6163
Events::Window { event, issued_at } => match event {
6264
WindowEvent::Resize { width, height } => {
6365
println!("Window resized to {}x{}", width, height);
66+
self.width = *width;
67+
self.height = *height;
6468
}
6569
WindowEvent::Close => {
6670
println!("Window closed");
@@ -134,7 +138,8 @@ impl RenderableComponent<Events> for DemoComponent {
134138
_render_context: &mut lambda::core::render::RenderContext,
135139
_last_render: &std::time::Duration,
136140
) -> Vec<RenderCommand> {
137-
let viewport = viewport::ViewportBuilder::new().build(800, 600);
141+
let viewport =
142+
viewport::ViewportBuilder::new().build(self.width, self.height);
138143

139144
// This array of commands will be executed in linear order
140145
return vec![
@@ -206,6 +211,8 @@ impl Default for DemoComponent {
206211
triangle_vertex: fs,
207212
render_pass: None,
208213
render_pipeline: None,
214+
width: 800,
215+
height: 600,
209216
};
210217
}
211218
}

0 commit comments

Comments
 (0)