Skip to main content

mln_webgpu/
renderer_backend.rs

1use std::cell::{RefCell, RefMut};
2use std::rc::Rc;
3
4use mln_gfx::gpu::Gpu;
5use mln_gfx::renderer_backend;
6
7use crate::device::Device;
8use crate::gpu::WebGpu;
9
10/// A renderer's backend on a [`Device`]: the [`WebGpu`] that its resources
11/// belong to, and that the targets built on it import their textures into.
12pub struct RendererBackend {
13    gpu: Rc<RefCell<WebGpu>>,
14    device: Device,
15}
16
17impl RendererBackend {
18    /// A backend on `device`. Backends on one device render independently,
19    /// each submitting to the device's queue.
20    #[must_use]
21    pub fn new(device: &Device) -> Self {
22        let gpu = Rc::new(RefCell::new(WebGpu::new(device)));
23        RendererBackend { gpu, device: device.clone() }
24    }
25
26    /// The device the backend renders on.
27    #[must_use]
28    pub fn device(&self) -> &Device {
29        &self.device
30    }
31
32    pub(crate) fn gpu(&self) -> RefMut<'_, WebGpu> {
33        self.gpu.borrow_mut()
34    }
35}
36
37impl renderer_backend::RendererBackend for RendererBackend {
38    fn gpu(&self) -> RefMut<'_, dyn Gpu> {
39        RefMut::map(self.gpu.borrow_mut(), |gpu| -> &mut dyn Gpu { gpu })
40    }
41}