begin et end frame dans context
This commit is contained in:
+31
-1
@@ -64,7 +64,12 @@ impl Context {
|
||||
/// 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> {
|
||||
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
|
||||
@@ -95,4 +100,29 @@ impl Context {
|
||||
self.surface.configure(&self.device, &config);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Acquires the next surface texture for rendering this frame. Returns an error variant
|
||||
/// describing why acquisition failed (timeout, occlusion, surface lost, etc.).
|
||||
/// Typically called by the orchestrator (main.rs) at the start of each frame loop iteration.
|
||||
pub fn begin_frame(&self) -> Result<wgpu::SurfaceTexture, WsgError> {
|
||||
// In wgpu 30, get_current_texture() returns CurrentSurfaceTexture enum directly (not Result).
|
||||
// All variants are matched to provide explicit error handling instead of panicking.
|
||||
match self.surface.get_current_texture() {
|
||||
wgpu::CurrentSurfaceTexture::Success(texture) => Ok(texture),
|
||||
wgpu::CurrentSurfaceTexture::Suboptimal(_texture) => Err(WsgError::SuboptimalTexture),
|
||||
wgpu::CurrentSurfaceTexture::Timeout => Err(WsgError::FrameTimeout),
|
||||
wgpu::CurrentSurfaceTexture::Occluded => Err(WsgError::Occluded),
|
||||
wgpu::CurrentSurfaceTexture::Outdated => Err(WsgError::Outdated),
|
||||
wgpu::CurrentSurfaceTexture::Lost => Err(WsgError::Lost),
|
||||
wgpu::CurrentSurfaceTexture::Validation => Err(WsgError::Validation),
|
||||
}
|
||||
}
|
||||
|
||||
/// Presents the rendered frame by submitting the acquired surface texture to the GPU queue.
|
||||
/// The frame must have been obtained via begin_frame(); calling present() twice on the same
|
||||
/// texture is undefined behavior. Called by the orchestrator after rendering commands are complete.
|
||||
pub fn end_frame(&self, frame: wgpu::SurfaceTexture) {
|
||||
// In wgpu 30, present() moved from SurfaceTexture::present() to Queue::present(frame).
|
||||
self.queue.present(frame);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,4 +50,29 @@ pub enum WsgError {
|
||||
/// 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,
|
||||
|
||||
/// A timeout was encountered while acquiring a surface texture. Skip this frame and retry.
|
||||
#[error("Frame acquisition timed out")]
|
||||
FrameTimeout,
|
||||
|
||||
/// The window is occluded (minimized or behind another window). Skip until visible.
|
||||
#[error("Window is occluded")]
|
||||
Occluded,
|
||||
|
||||
/// The underlying surface changed — call configure() before retrying.
|
||||
#[error("Surface configuration outdated; reconfigure required")]
|
||||
Outdated,
|
||||
|
||||
/// The surface has been lost and needs to be recreated.
|
||||
#[error("Surface lost")]
|
||||
Lost,
|
||||
|
||||
/// A validation error inside get_current_texture() was raised.
|
||||
#[error("Validation error during frame acquisition")]
|
||||
Validation,
|
||||
|
||||
/// Successfully acquired the surface texture but it no longer matches the surface properties.
|
||||
/// Reconfigure recommended for optimal performance.
|
||||
#[error("Acquired suboptimal surface texture; reconfigure recommended")]
|
||||
SuboptimalTexture,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user