Merge pull request #2107 from kacpherek/fix/1959-pack-reload-surface

🤖 Agent: Gate check by Ludovic's AI agent (Claude). Checked: the renderer that replaces this one shares its instance, adapter and device, so it can take the configured surface instead of creating a second one for the same window. Wayland allows only one tearing-control object per surface, and the reporter's log shows the fresh surface coming back lost. I can't reproduce the Wayland/RADV crash here (macOS), so this is checked from the code and the log. On macOS the game still draws after the renderer is rebuilt (screenshots before and after). Fixes #1959. Main's HDR output conflicted in recreate_surface, so I merged main into the branch: the reused surface now goes through main's Renderer::configure too. Thanks @kacpherek! Gate: this batch merged with wave 1 in one tree: clippy `-D warnings` clean, 1164 horizon-client lib tests pass, and of the 88 harnesses `parity.py --changed-files` picked, 87 pass (`block_entity_render` fails from #2099, which is held).
This commit is contained in:
Ludovic Marie
2026-10-26 18:21:17 +01:00
committed by GitHub
2 changed files with 34 additions and 7 deletions
+1 -1
View File
@@ -163,7 +163,7 @@ impl Game<'_> {
self.pack.context.biomes.clone(),
self.pack.context.light.clone(),
)?;
// Keep the working GPU resources, but release its exclusive window surface.
// Keep the working GPU resources, and hand its window surface over.
if let Some(renderer) = &mut self.renderer {
renderer.release_surface();
}
+33 -6
View File
@@ -334,6 +334,10 @@ pub struct Renderer {
adapter: wgpu::Adapter,
/// The window's swapchain; `None` when rendering offscreen (`--offscreen`).
surface: Option<wgpu::Surface<'static>>,
/// The surface `release_surface` set aside for the renderer that replaces
/// this one on the same device: reused, not created again, as a new
/// surface for the window can come back lost (Wayland on Mesa).
spare_surface: std::sync::Mutex<Option<wgpu::Surface<'static>>>,
/// The offscreen colour target, sized like `config`.
offscreen: Option<wgpu::Texture>,
/// Offscreen submissions still on the GPU: at most two frames run ahead.
@@ -1044,7 +1048,15 @@ impl Renderer {
surface: if offscreen {
None
} else {
Some(renderer.instance.create_surface(window.clone())?)
let spare = renderer
.spare_surface
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.take();
Some(match spare {
Some(surface) => surface,
None => renderer.instance.create_surface(window.clone())?,
})
},
instance: renderer.instance.clone(),
adapter: renderer.adapter.clone(),
@@ -2320,6 +2332,7 @@ impl Renderer {
instance,
adapter,
surface,
spare_surface: std::sync::Mutex::new(None),
offscreen,
in_flight: std::collections::VecDeque::new(),
first_frame: if previous.is_none() {
@@ -2499,18 +2512,32 @@ impl Renderer {
view_formats: &config.view_formats,
})
}
/// Release the window surface before another renderer uses the same window.
/// Hand the window surface over to the renderer that replaces this one on
/// the same device (it takes `spare_surface`).
pub fn release_surface(&mut self) {
self.surface = None;
*self
.spare_surface
.get_mut()
.unwrap_or_else(std::sync::PoisonError::into_inner) = self.surface.take();
}
/// 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())?;
// The surface set aside for a replacement that failed, else a new one
// (Wayland allows only one tearing-control object per window surface,
// so the old one goes first).
let spare = self
.spare_surface
.get_mut()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.take();
self.surface = None;
let surface = match spare {
Some(surface) => surface,
None => self.instance.create_surface(self.window.clone())?,
};
Self::configure(
&self.device,
&surface,