Release Wayland surface before replacing the pack renderer

This commit is contained in:
Cosmic
2026-10-16 22:41:33 -04:00
parent 5515cbab33
commit e0d6e56281
2 changed files with 36 additions and 5 deletions
+19 -2
View File
@@ -130,11 +130,28 @@ impl Game<'_> {
self.pack.context.biomes.clone(),
self.pack.context.light.clone(),
)?;
let renderer = self
// Keep the working GPU resources, but release its exclusive window surface.
if let Some(renderer) = &mut self.renderer {
renderer.release_surface();
}
let renderer = match self
.window
.clone()
.map(|window| self.renderer_for(window, &pack, &atlas))
.transpose()?;
.transpose()
{
Ok(renderer) => renderer,
Err(error) => {
if let Some(renderer) = &mut self.renderer {
renderer.recreate_surface().map_err(|restore| {
format!(
"{error}; could not restore the previous rendering surface: {restore}"
)
})?;
}
return Err(error);
}
};
// Everything loaded: switch over.
self.random_tick_sections.clear();
self.options.resources = root.to_path_buf();
+17 -3
View File
@@ -1656,6 +1656,22 @@ impl Renderer {
pub fn is_offscreen(&self) -> bool {
self.surface.is_none()
}
/// Release the window surface before another renderer uses the same window.
pub fn release_surface(&mut self) {
self.surface = None;
}
/// Reattach a window after surface loss or a failed renderer replacement.
pub fn recreate_surface(&mut self) -> Result<()> {
if self.offscreen.is_some() {
return Ok(());
}
// Wayland allows only one tearing-control object per window surface.
self.release_surface();
let surface = self.instance.create_surface(self.window.clone())?;
surface.configure(&self.device, &self.config);
self.surface = Some(surface);
Ok(())
}
fn depth(device: &wgpu::Device, config: &wgpu::SurfaceConfiguration) -> wgpu::TextureView {
device
.create_texture(&wgpu::TextureDescriptor {
@@ -2039,9 +2055,7 @@ impl Renderer {
return Ok(false);
}
Some(wgpu::CurrentSurfaceTexture::Lost) => {
let surface = self.instance.create_surface(self.window.clone())?;
surface.configure(&self.device, &self.config);
self.surface = Some(surface);
self.recreate_surface()?;
return Ok(false);
}
Some(wgpu::CurrentSurfaceTexture::Validation) => {