configure context

This commit is contained in:
Jérôme Bousquié
2026-07-03 21:11:43 +02:00
parent 3d6112833d
commit 4fd39d32c6
4 changed files with 114 additions and 1 deletions
+36
View File
@@ -59,4 +59,40 @@ impl Context {
queue,
})
}
/// Configures the surface with a render format and alpha mode for rendering.
/// Inputs: adapter (GPU capabilities), width/height (surface resolution).
/// Returns Ok(()) on success or SurfaceIncompatible if no SRGB format + alpha mode exist.
/// Typically called by the renderer when window size changes.
pub fn configure(&self, adapter: &wgpu::Adapter, width: u32, height: u32) -> Result<(), WsgError> {
let caps = self.surface.get_capabilities(adapter);
// Prefer SRGB format for color accuracy; fall back to first available if none (technical point documented above).
let format = caps
.formats
.iter()
.copied()
.find(|f| f.is_srgb())
.or(caps.formats.first().copied())
.ok_or(WsgError::SurfaceIncompatible)?;
let alpha_mode = caps
.alpha_modes
.first()
.copied()
.ok_or(WsgError::SurfaceIncompatible)?;
let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format,
width,
height,
present_mode: wgpu::PresentMode::Fifo, // V-Sync activated
alpha_mode, // first supported alpha mode
view_formats: vec![],
color_space: wgpu::SurfaceColorSpace::Srgb,
desired_maximum_frame_latency: 2,
};
self.surface.configure(&self.device, &config);
Ok(())
}
}