begin et end frame dans context

This commit is contained in:
Jérôme Bousquié
2026-07-04 14:57:29 +02:00
parent 4fd39d32c6
commit 4b7a1b05e5
3 changed files with 98 additions and 1 deletions
+31 -1
View File
@@ -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);
}
}