docs: add window field to State struct for Outdated recovery

This commit is contained in:
2026-05-30 20:02:59 -05:00
parent 23a7e3b151
commit 4a4be8b307

View File

@@ -217,6 +217,7 @@ struct State {
device: wgpu::Device, device: wgpu::Device,
queue: wgpu::Queue, queue: wgpu::Queue,
config: wgpu::SurfaceConfiguration, config: wgpu::SurfaceConfiguration,
window: Arc<Window>,
pipeline: wgpu::RenderPipeline, pipeline: wgpu::RenderPipeline,
vertex_buffer: wgpu::Buffer, vertex_buffer: wgpu::Buffer,
} }
@@ -235,6 +236,12 @@ struct State {
- **`config`** — holds the surface's current width, height, pixel format, and - **`config`** — holds the surface's current width, height, pixel format, and
[present mode](concepts/GLOSSARY.md#present-mode). When the window is resized, [present mode](concepts/GLOSSARY.md#present-mode). When the window is resized,
we reconfigure the surface with updated dimensions. we reconfigure the surface with updated dimensions.
- **`window`** — shared reference to the winit window. Stored as an `Arc` so
the `resize()` method and the `SurfaceError::Outdated` recovery handler can
access the window's current dimensions. When the surface becomes outdated
(e.g., after a compositor restart or display hotplug), recovery requires
reconfiguring the swapchain with the window's live size — and that requires
holding a reference to the window itself.
- **`pipeline`** — the compiled [render pipeline](concepts/GLOSSARY.md#render-pipeline). - **`pipeline`** — the compiled [render pipeline](concepts/GLOSSARY.md#render-pipeline).
A render pipeline is an immutable configuration combining a shader, a vertex A render pipeline is an immutable configuration combining a shader, a vertex
buffer layout, a primitive topology, and a [color target](concepts/GLOSSARY.md#color-target) buffer layout, a primitive topology, and a [color target](concepts/GLOSSARY.md#color-target)
@@ -389,6 +396,7 @@ impl State {
device, device,
queue, queue,
config, config,
window: Arc::clone(&window),
pipeline, pipeline,
vertex_buffer, vertex_buffer,
}) })
@@ -964,6 +972,11 @@ fn render(&mut self) {
}; };
self.resize(size); self.resize(size);
} }
// The window field stored in State enables Outdated recovery:
// when the compositor or display server invalidates the surface,
// we can reconfigure the swapchain using the window's current
// dimensions (self.window.inner_size()) without needing a
// separate reference to the window from the event loop.
wgpu::SurfaceError::Lost => { wgpu::SurfaceError::Lost => {
log::error!("Surface error: Lost — cannot recover without re-creating State"); log::error!("Surface error: Lost — cannot recover without re-creating State");
} }