Skip to content

Commit 4eb2d72

Browse files
committed
[update] the renderer implementation to be within the Runnable and use the new surface, instance, and gpu APIs.
1 parent 0522768 commit 4eb2d72

5 files changed

Lines changed: 110 additions & 240 deletions

File tree

lambda/src/components/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
mod renderer;
2-
pub use renderer::{
3-
RenderPlan,
4-
Renderer,
5-
};
62

73
mod component_stack;
84
pub use component_stack::ComponentStack;

lambda/src/components/renderer.rs

Lines changed: 1 addition & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -1,182 +1 @@
1-
use core::mem::swap;
2-
use std::collections::HashMap;
3-
4-
use lambda_platform::{
5-
gfx,
6-
gfx::gfx_hal_exports,
7-
};
8-
9-
use crate::{
10-
components::Window,
11-
core::{
12-
component::Component,
13-
events::Event,
14-
render::shader::ShaderKind,
15-
},
16-
};
17-
18-
/// Enums for How to load shaders into the
19-
pub enum Shader {
20-
FromFile {
21-
name: Option<String>,
22-
path: String,
23-
kind: ShaderKind,
24-
},
25-
FromString {
26-
name: Option<String>,
27-
source: String,
28-
kind: ShaderKind,
29-
},
30-
}
31-
32-
pub enum Resource {
33-
Shader(Shader),
34-
}
35-
36-
pub enum OnAttachSteps {
37-
UploadShader(Shader),
38-
}
39-
40-
pub enum OnDetachSteps {}
41-
pub enum OnEventSteps {}
42-
pub enum OnUpdateSteps {}
43-
44-
pub struct RenderPlan {
45-
on_attach: Vec<OnAttachSteps>,
46-
on_detach: Vec<OnDetachSteps>,
47-
on_event: Vec<OnEventSteps>,
48-
on_update: Vec<OnUpdateSteps>,
49-
}
50-
51-
pub struct RendererBase<B: gfx_hal_exports::Backend> {
52-
instance: gfx::GfxInstance<B>,
53-
gpu: gfx::gpu::GfxGpu<B>,
54-
format: gfx_hal_exports::Format,
55-
extent: gfx_hal_exports::Extent2D,
56-
fences: HashMap<String, Fences<B>>,
57-
surfaces: HashMap<String, B::Surface>,
58-
59-
graphic_pipelines: Vec<B::GraphicsPipeline>,
60-
pipeline_layouts: Vec<B::PipelineLayout>,
61-
render_passes: Vec<B::RenderPass>,
62-
}
63-
64-
type PlatformAPI = gfx::api::RenderingAPI::Backend;
65-
66-
struct Fences<B: gfx_hal_exports::Backend> {
67-
submission_fence: B::Fence,
68-
rendering_semaphore: B::Semaphore,
69-
}
70-
71-
type PlatformFences = Fences<PlatformAPI>;
72-
73-
/// The Renderer component utilizing the current platforms rendering backend
74-
/// provided by
75-
pub type Renderer = RendererBase<PlatformAPI>;
76-
77-
impl Renderer {
78-
pub fn new(name: &str, window: &Window) -> Self {
79-
let instance = gfx::GfxInstance::<PlatformAPI>::new(name);
80-
let mut surface = instance.create_surface(window.window_handle());
81-
let mut gpu = instance
82-
.open_primary_gpu(Some(&surface))
83-
.with_command_pool();
84-
85-
// Create the image extent and initial frame buffer attachment description for rendering.
86-
let format = gpu.find_supported_color_format(&surface);
87-
let dimensions = window.dimensions();
88-
let (extent, _frame_buffer_attachment) = gpu
89-
.configure_swapchain_and_update_extent(
90-
&mut surface,
91-
format,
92-
[dimensions[0], dimensions[1]],
93-
);
94-
95-
let mut surfaces = HashMap::new();
96-
surfaces.insert("Primary".to_string(), surface);
97-
let mut fences = HashMap::<String, PlatformFences>::new();
98-
99-
let (submission_fence, rendering_semaphore) = gpu.create_access_fences();
100-
let fence_set = PlatformFences {
101-
submission_fence,
102-
rendering_semaphore,
103-
};
104-
105-
fences.insert("Primary".to_string(), fence_set);
106-
107-
return Self {
108-
instance,
109-
gpu,
110-
format,
111-
surfaces,
112-
extent,
113-
fences,
114-
graphic_pipelines: vec![],
115-
pipeline_layouts: vec![],
116-
render_passes: vec![],
117-
};
118-
}
119-
120-
pub fn upload_render_plan(&mut self, plan: RenderPlan) {
121-
todo!();
122-
}
123-
}
124-
125-
impl Component for Renderer {
126-
fn on_attach(&mut self) {
127-
println!("The rendering API has been attached to the current Runnable.")
128-
}
129-
130-
/// When detaching the Renderer, it will deallocate all GPU resources that have been created.
131-
fn on_detach(&mut self) {
132-
println!("Destroying GPU resources allocated during run.");
133-
134-
let mut empty_fences = HashMap::new();
135-
swap(&mut empty_fences, &mut self.fences);
136-
137-
for (name, fence) in empty_fences.into_iter() {
138-
self.gpu.destroy_access_fences(
139-
fence.submission_fence,
140-
fence.rendering_semaphore,
141-
);
142-
}
143-
144-
let mut pipeline_layouts = vec![];
145-
swap(&mut pipeline_layouts, &mut self.pipeline_layouts);
146-
147-
for pipeline_layout in pipeline_layouts.into_iter() {
148-
self.gpu.destroy_pipeline_layout(pipeline_layout);
149-
}
150-
151-
let mut render_passes = vec![];
152-
swap(&mut render_passes, &mut self.render_passes);
153-
154-
for render_pass in render_passes.into_iter() {
155-
self.gpu.destroy_render_pass(render_pass);
156-
}
157-
158-
let mut graphics_pipelines = vec![];
159-
swap(&mut graphics_pipelines, &mut self.graphic_pipelines);
160-
for pipeline in graphics_pipelines.into_iter() {
161-
self.gpu.destroy_graphics_pipeline(pipeline);
162-
}
163-
164-
// Destroy command pool allocated on the GPU.
165-
self.gpu.destroy_command_pool();
166-
let mut surfaces = HashMap::new();
167-
swap(&mut surfaces, &mut self.surfaces);
168-
169-
for (_surface_name, mut surface) in surfaces.into_iter() {
170-
// Unconfigure the swapchain and destroy the surface context.
171-
println!("Destroying Surface: {}", _surface_name);
172-
self.gpu.unconfigure_swapchain(&mut surface);
173-
self.instance.destroy_surface(surface);
174-
}
175-
176-
println!("Destroyed all GPU resources");
177-
}
178-
179-
fn on_event(&mut self, event: &Event) {}
180-
181-
fn on_update(&mut self, last_frame: &std::time::Duration) {}
182-
}
1+
pub struct Renderer;

lambda/src/core/render/mod.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,6 @@ impl<B: gfx_hal_exports::Backend> Component for LambdaRenderer<B> {
168168
self.gpu.destroy_command_pool();
169169
let mut surface = self.surface.take().unwrap();
170170

171-
// Unconfigure the swapchain and destroy the surface context.
172-
self.gpu.unconfigure_swapchain(&mut surface);
173-
self.instance.destroy_surface(surface);
174-
175171
println!("Destroyed all GPU resources");
176172
}
177173

@@ -180,16 +176,7 @@ impl<B: gfx_hal_exports::Backend> Component for LambdaRenderer<B> {
180176
Event::Resized {
181177
new_width,
182178
new_height,
183-
} => {
184-
let (extent, frame_buffer_attachment) =
185-
self.gpu.configure_swapchain_and_update_extent(
186-
self.surface.as_mut().unwrap(),
187-
self.format,
188-
[*new_width, *new_height],
189-
);
190-
self.extent = Some(extent);
191-
self.frame_buffer_attachment = Some(frame_buffer_attachment);
192-
}
179+
} => {}
193180
_ => (),
194181
};
195182
}

0 commit comments

Comments
 (0)