Skip to main content

pebble/rendering/
graphics_plugin.rs

1use crate::{
2    prelude::{
3        Backend, Commands, GPUSurfaceHandle, Plugin, PresentableWindow, Res, ResMut, SystemStage,
4        WindowResource,
5    },
6    rendering::{async_init::PendingBackend, sync::init_channel},
7};
8
9/// Plugin that initialises the GPU backend asynchronously and handles window
10/// resize events.
11///
12/// On startup it calls [`Backend::init`] with the window handle and a one-shot
13/// sender, then polls the receiver every [`PreRender`](SystemStage::PreRender)
14/// tick until the backend arrives. Once available it also forwards window size
15/// changes to [`Backend::resize`].
16pub struct GraphicsPlugin<B, W> {
17    _marker: std::marker::PhantomData<(B, W)>,
18}
19
20impl<B: Backend, W: PresentableWindow> GraphicsPlugin<B, W>
21where
22    W::Handle: GPUSurfaceHandle,
23{
24    pub fn new() -> Self {
25        Self {
26            _marker: std::marker::PhantomData,
27        }
28    }
29}
30
31impl<B: Backend, W: PresentableWindow> Plugin for GraphicsPlugin<B, W>
32where
33    W::Handle: GPUSurfaceHandle,
34{
35    fn build(&self, app: &mut crate::prelude::App) {
36        // B arrives asynchronously (see poll_backend_ready) — mark it so a
37        // system elsewhere with a hard `Res<B>` requirement waits quietly
38        // instead of App treating it as a missing/misconfigured resource.
39        app.provides::<B>();
40        app.add_system(SystemStage::Startup, setup_gpu_async::<B, W>)
41            .add_system(SystemStage::PreRender, poll_backend_ready::<B>)
42            .add_system(SystemStage::PreRender, handle_resize_async::<B, W>);
43    }
44}
45
46struct LastWindowSize(u32, u32);
47
48/// Startup system: kick off backend initialisation and store the pending receiver.
49fn setup_gpu_async<B: Backend, W>(mut commands: Commands, window: Res<WindowResource<W>>)
50where
51    W: PresentableWindow,
52    W::Handle: GPUSurfaceHandle,
53{
54    let (w, h) = W::size(&window.handle);
55    let (sender, receiver) = init_channel::<B>();
56    B::init(window.handle.clone(), w, h, sender);
57    commands.insert_resource(PendingBackend::<B> {
58        receiver: std::sync::Mutex::new(receiver),
59    });
60}
61
62/// PreRender system: poll the one-shot channel; promote the backend to a
63/// resource and remove the pending marker once it arrives.
64fn poll_backend_ready<B: Backend>(mut commands: Commands, pending: Option<Res<PendingBackend<B>>>) {
65    if let Some(p) = pending {
66        let mut guard = match p.receiver.lock() {
67            Ok(g) => g,
68            Err(poisoned) => poisoned.into_inner(),
69        };
70
71        if let Ok(backend) = guard.try_recv() {
72            commands.insert_resource(backend);
73            commands.remove_resource::<PendingBackend<B>>();
74        }
75    }
76}
77
78/// PreRender system: forward the current window size to the backend so it can
79/// recreate the swapchain when the window is resized.
80fn handle_resize_async<B: Backend, W: PresentableWindow>(
81    backend: Option<ResMut<B>>,
82    window: Res<WindowResource<W>>,
83) where
84    W::Handle: GPUSurfaceHandle,
85{
86    let Some(mut backend) = backend else { return };
87    let (w, h) = W::size(&window.handle);
88    if w > 0 && h > 0 {
89        backend.resize(w, h);
90    }
91}