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        app.add_system(SystemStage::Startup, setup_gpu_async::<B, W>)
37            .add_system(SystemStage::PreRender, poll_backend_ready::<B>)
38            .add_system(SystemStage::PreRender, handle_resize_async::<B, W>);
39    }
40}
41
42struct LastWindowSize(u32, u32);
43
44/// Startup system: kick off backend initialisation and store the pending receiver.
45fn setup_gpu_async<B: Backend, W>(mut commands: Commands, window: Res<WindowResource<W>>)
46where
47    W: PresentableWindow,
48    W::Handle: GPUSurfaceHandle,
49{
50    let (w, h) = W::size(&window.handle);
51    let (sender, receiver) = init_channel::<B>();
52    B::init(window.handle.clone(), w, h, sender);
53    commands.insert_resource(PendingBackend::<B> {
54        receiver: std::sync::Mutex::new(receiver),
55    });
56}
57
58/// PreRender system: poll the one-shot channel; promote the backend to a
59/// resource and remove the pending marker once it arrives.
60fn poll_backend_ready<B: Backend>(mut commands: Commands, pending: Option<Res<PendingBackend<B>>>) {
61    if let Some(p) = pending {
62        let mut guard = match p.receiver.lock() {
63            Ok(g) => g,
64            Err(poisoned) => poisoned.into_inner(),
65        };
66
67        if let Ok(backend) = guard.try_recv() {
68            commands.insert_resource(backend);
69            commands.remove_resource::<PendingBackend<B>>();
70        }
71    }
72}
73
74/// PreRender system: forward the current window size to the backend so it can
75/// recreate the swapchain when the window is resized.
76fn handle_resize_async<B: Backend, W: PresentableWindow>(
77    backend: Option<ResMut<B>>,
78    window: Res<WindowResource<W>>,
79) where
80    W::Handle: GPUSurfaceHandle,
81{
82    let Some(mut backend) = backend else { return };
83    let (w, h) = W::size(&window.handle);
84    if w > 0 && h > 0 {
85        backend.resize(w, h);
86    }
87}