From f08f00108145879c9a8001cb085f61d289385b77 Mon Sep 17 00:00:00 2001 From: Fedor Logachev Date: Thu, 30 May 2024 19:17:04 -0600 Subject: [PATCH] graphics/gl: Fix glBindTexture with already deleted textures bug delete_texture was not clearing textures cache, therefore next apply_bindings tried to re-bind already dead texture. It was (hopefully) not affecting anything except WebGL validation layer complains. --- src/graphics/gl.rs | 20 +++++++++----------- src/lib.rs | 4 ++-- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/graphics/gl.rs b/src/graphics/gl.rs index 83960d57..26bb3e1b 100644 --- a/src/graphics/gl.rs +++ b/src/graphics/gl.rs @@ -261,12 +261,6 @@ impl Texture { } } - pub fn delete(&self) { - unsafe { - glDeleteTextures(1, &self.raw as *const _); - } - } - pub fn resize(&mut self, ctx: &mut GlContext, width: u32, height: u32, source: Option<&[u8]>) { ctx.cache.store_texture_binding(0); ctx.cache.bind_texture(0, self.params.kind.into(), self.raw); @@ -814,9 +808,14 @@ impl RenderingBackend for GlContext { self.textures.0.push(texture); TextureId(TextureIdInner::Managed(self.textures.0.len() - 1)) } + fn delete_texture(&mut self, texture: TextureId) { + self.cache.clear_texture_bindings(); + let t = self.textures.get(texture); - t.delete(); + unsafe { + glDeleteTextures(1, &t.raw as *const _); + } } fn delete_shader(&mut self, program: ShaderId) { @@ -988,17 +987,16 @@ impl RenderingBackend for GlContext { fn delete_render_pass(&mut self, render_pass: RenderPass) { let pass_id = render_pass.0; - let render_pass = &self.passes[pass_id]; + let render_pass = self.passes.remove(pass_id); unsafe { glDeleteFramebuffers(1, &render_pass.gl_fb as *const _) } for color_texture in &render_pass.color_textures { - self.textures.get(*color_texture).delete(); + self.delete_texture(*color_texture); } if let Some(depth_texture) = render_pass.depth_texture { - self.textures.get(depth_texture).delete(); + self.delete_texture(depth_texture); } - self.passes.remove(pass_id); } fn new_pipeline( diff --git a/src/lib.rs b/src/lib.rs index 7b1588ae..16df4172 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,9 +40,9 @@ impl ResourceManager { self.id - 1 } - pub fn remove(&mut self, id: usize) { + pub fn remove(&mut self, id: usize) -> T { // Let it crash if the resource is not found - self.resources.remove(&id).unwrap(); + self.resources.remove(&id).unwrap() } }