configure context
This commit is contained in:
@@ -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(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,4 +45,9 @@ pub enum WsgError {
|
||||
/// Caller: `Context::new()` after `Instance::create_surface()`.
|
||||
#[error("Failed to create rendering surface")]
|
||||
SurfaceCreation(wgpu::CreateSurfaceError),
|
||||
|
||||
/// The surface is incompatible — no SRGB texture format and alpha mode are available on the device.
|
||||
/// Caller: `Context::configure()` when selecting a render format/alpha mode from surface capabilities.
|
||||
#[error("No compatible render format or alpha mode found for the surface")]
|
||||
SurfaceIncompatible,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user