@@ -22,7 +22,6 @@ use lambda_platform::gfx::{
2222 } ,
2323 framebuffer:: FramebufferBuilder ,
2424 surface:: SwapchainBuilder ,
25- viewport:: ViewPort ,
2625} ;
2726
2827use self :: {
@@ -104,7 +103,6 @@ impl RenderContextBuilder {
104103 submission_fence : Some ( submission_fence) ,
105104 render_semaphore : Some ( render_semaphore) ,
106105 command_pool : Some ( command_pool) ,
107- viewports : vec ! [ ] ,
108106 render_passes : vec ! [ ] ,
109107 render_pipelines : vec ! [ ] ,
110108 } ;
@@ -123,12 +121,29 @@ pub struct RenderContext {
123121 Option < internal:: RenderSubmissionFence < internal:: RenderBackend > > ,
124122 render_semaphore : Option < internal:: RenderSemaphore < internal:: RenderBackend > > ,
125123 command_pool : Option < internal:: CommandPool < internal:: RenderBackend > > ,
126- render_passes : Vec < Option < RenderPass > > ,
127- render_pipelines : Vec < Option < RenderPipeline > > ,
128- viewports : Vec < ViewPort > ,
124+ render_passes : Vec < RenderPass > ,
125+ render_pipelines : Vec < RenderPipeline > ,
129126}
130127
128+ pub type ResourceId = usize ;
129+
131130impl RenderContext {
131+ /// Permanently transfer a render pipeline to the render context in exchange
132+ /// for a resource ID that you can use in render commands.
133+ pub fn attach_pipeline ( & mut self , pipeline : RenderPipeline ) -> ResourceId {
134+ let index = self . render_pipelines . len ( ) ;
135+ self . render_pipelines . push ( pipeline) ;
136+ return index;
137+ }
138+
139+ /// Permanently transfer a render pipeline to the render context in exchange
140+ /// for a resource ID that you can use in render commands.
141+ pub fn attach_render_pass ( & mut self , render_pass : RenderPass ) -> ResourceId {
142+ let index = self . render_passes . len ( ) ;
143+ self . render_passes . push ( render_pass) ;
144+ return index;
145+ }
146+
132147 /// destroys the RenderContext and all associated resources.
133148 pub fn destroy ( mut self ) {
134149 println ! ( "{} will now start destroying resources." , self . name) ;
@@ -147,30 +162,32 @@ impl RenderContext {
147162 . expect ( "Couldn't take the rendering semaphore from the context and destroy it." )
148163 . destroy ( & self . gpu ) ;
149164
165+ self
166+ . command_pool
167+ . as_mut ( )
168+ . unwrap ( )
169+ . deallocate_command_buffer ( "primary" ) ;
170+
171+ self
172+ . command_pool
173+ . take ( )
174+ . expect ( "Couldn't take the command pool from the context and destroy it." )
175+ . destroy ( & self . gpu ) ;
176+
150177 // Destroy render passes.
151178 let mut render_passes = vec ! [ ] ;
152179 swap ( & mut self . render_passes , & mut render_passes) ;
153180
154- for render_pass in & mut render_passes {
155- render_pass
156- . take ( )
157- . expect (
158- "Couldn't take the render pass from the context and destroy it." ,
159- )
160- . destroy ( & self ) ;
181+ for render_pass in render_passes {
182+ render_pass. destroy ( & self ) ;
161183 }
162184
163185 // Destroy render pipelines.
164186 let mut render_pipelines = vec ! [ ] ;
165187 swap ( & mut self . render_pipelines , & mut render_pipelines) ;
166188
167- for render_pipeline in & mut render_pipelines {
168- render_pipeline
169- . take ( )
170- . expect (
171- "Couldn't take the render pipeline from the context and destroy it." ,
172- )
173- . destroy ( & self ) ;
189+ for render_pipeline in render_pipelines {
190+ render_pipeline. destroy ( & self ) ;
174191 }
175192
176193 // Takes the inner surface and destroys it.
@@ -232,7 +249,6 @@ impl RenderContext {
232249 command_buffer. issue_commands ( platform_command_list) ;
233250 command_buffer. issue_command ( PlatformRenderCommand :: EndRecording ) ;
234251
235- println ! ( "[INFO] {} will now submit commands to the GPU." , self . name) ;
236252 self . gpu . submit_command_buffer (
237253 & mut command_buffer,
238254 vec ! [ self . render_semaphore. as_ref( ) . unwrap( ) ] ,
@@ -242,7 +258,6 @@ impl RenderContext {
242258 . expect ( "Failed to get mutable reference to submission fence." ) ,
243259 ) ;
244260
245- println ! ( "[INFO] {} will now render to the surface." , self . name) ;
246261 self
247262 . gpu
248263 . render_to_surface (
@@ -252,15 +267,10 @@ impl RenderContext {
252267 )
253268 . expect ( "Failed to render to the surface" ) ;
254269
255- println ! (
256- "[INFO] {} will now wait for the GPU to finish rendering." ,
257- self . name
258- ) ;
259270 match self . frame_buffer {
260271 Some ( _) => {
261272 Rc :: try_unwrap ( self . frame_buffer . take ( ) . unwrap ( ) )
262- . ok ( )
263- . unwrap ( )
273+ . expect ( "Failed to unwrap the frame buffer." )
264274 . destroy ( & self . gpu ) ;
265275 }
266276 None => { }
@@ -276,6 +286,14 @@ impl RenderContext {
276286 . apply_swapchain ( & self . gpu , swapchain, 1_000_000_000 )
277287 . expect ( "Failed to apply the swapchain to the surface while attempting to resize." ) ;
278288 }
289+
290+ pub fn get_render_pass ( & self , id : ResourceId ) -> & RenderPass {
291+ return & self . render_passes [ id] ;
292+ }
293+
294+ pub fn get_render_pipeline ( & mut self , id : ResourceId ) -> & RenderPipeline {
295+ return & self . render_pipelines [ id] ;
296+ }
279297}
280298
281299type PlatformRenderCommand = Command < internal:: RenderBackend > ;
0 commit comments