Skip to content

Commit

Permalink
graphics/gl: Fix glBindTexture with already deleted textures bug
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
not-fl3 committed May 31, 2024
1 parent 73c0bec commit f08f001
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
20 changes: 9 additions & 11 deletions src/graphics/gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ impl<T> ResourceManager<T> {
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()
}
}

Expand Down

0 comments on commit f08f001

Please sign in to comment.