@@ -28,17 +28,22 @@ pub(crate) enum TextureHandle {
2828pub ( crate ) struct TexturesContext {
2929 textures : TextureIdSlotMap ,
3030 removed : Vec < TextureSlotId > ,
31+ removed_render_passes : Vec < miniquad:: RenderPass > ,
3132}
3233impl TexturesContext {
3334 pub fn new ( ) -> TexturesContext {
3435 TexturesContext {
3536 textures : TextureIdSlotMap :: new ( ) ,
3637 removed : Vec :: with_capacity ( 200 ) ,
38+ removed_render_passes : Vec :: with_capacity ( 10 ) ,
3739 }
3840 }
3941 fn schedule_removed ( & mut self , texture : TextureSlotId ) {
4042 self . removed . push ( texture) ;
4143 }
44+ fn schedule_render_pass_removed ( & mut self , pass : miniquad:: RenderPass ) {
45+ self . removed_render_passes . push ( pass) ;
46+ }
4247 fn store_texture ( & mut self , texture : miniquad:: TextureId ) -> TextureHandle {
4348 TextureHandle :: Managed ( Arc :: new ( TextureSlotGuarded ( self . textures . insert ( texture) ) ) )
4449 }
@@ -52,6 +57,11 @@ impl TexturesContext {
5257 self . textures . len ( )
5358 }
5459 pub fn garbage_collect ( & mut self , ctx : & mut miniquad:: Context ) {
60+ // Delete RenderPasses first, then textures (safer for attachments/FBOs)
61+ for pass in self . removed_render_passes . drain ( 0 ..) {
62+ ctx. delete_render_pass ( pass) ;
63+ }
64+
5565 for texture in self . removed . drain ( 0 ..) {
5666 if let Some ( texture) = self . textures . get ( texture) {
5767 ctx. delete_texture ( texture) ;
@@ -376,9 +386,11 @@ impl RenderPass {
376386
377387impl Drop for RenderPass {
378388 fn drop ( & mut self ) {
389+ // Safety: if strong_count < 2, this is the last strong ref.
390+ // No new strong references can be created after this point.
379391 if Arc :: strong_count ( & self . render_pass ) < 2 {
380- let context = get_quad_context ( ) ;
381- context . delete_render_pass ( * self . render_pass ) ;
392+ let ctx = get_context ( ) ;
393+ ctx . textures . schedule_render_pass_removed ( * self . render_pass ) ;
382394 }
383395 }
384396}
0 commit comments