mln-rs-webgpu 0.0.11

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
Documentation
// Ports include/mln/webgpu/renderer_backend.hpp and src/mln/webgpu/renderer_backend.cpp
//
// The C++ backend stores raw `WGPUDevice`/`WGPUQueue`/instance handles in its
// `Impl` and every resource reaches them through `Context& -> getBackend()`.
// Here the `Impl` is shared (`Rc<BackendImpl>`) by the backend and the
// context, and resources hold an `Rc<Context>`.
//
// What the backend draws into is a target built on it (`OffscreenTarget`,
// `SurfaceTarget`), whose draw targets give the render passes their views.

use std::any::Any;
use std::cell::{Cell, RefCell};
use std::rc::Rc;

use mln_gfx::context::Context as GfxContext;
use mln_gfx::renderer_backend::{self, RendererBackendBase};
use mln_gfx::shader_registry::ShaderRegistry;
use mln_gfx::shaders::program_parameters::ProgramParameters;
use mln_gfx::shaders::shader_source::BuiltIn;

use crate::context::Context;
use crate::device::Device;
use crate::shaders::shader_group::ShaderGroup;
use crate::shaders::shader_source::shader_source;
use crate::writes::Writes;

/// `RendererBackend::Impl`
#[derive(Default)]
pub struct BackendImpl {
    instance: RefCell<Option<wgpu::Instance>>,
    device: RefCell<Option<wgpu::Device>>,
    queue: RefCell<Option<wgpu::Queue>>,
    /// `wgpu::TextureFormat::Undefined` is `None`.
    depth_stencil_format: Cell<Option<wgpu::TextureFormat>>,
    color_format: Cell<Option<wgpu::TextureFormat>>,
    writes: RefCell<Writes>,
    /// The page that small uniform buffers are taken from, and how many of
    /// its bytes are taken.
    uniform_page: RefCell<Option<(wgpu::Buffer, u64)>>,
}

/// The size of the buffers that small uniform buffers share.
const UNIFORM_PAGE: u64 = 16 << 10;

impl BackendImpl {
    pub fn set_instance(&self, instance: Option<wgpu::Instance>) {
        *self.instance.borrow_mut() = instance;
    }
    pub fn set_device(&self, device: Option<wgpu::Device>) {
        *self.device.borrow_mut() = device;
        *self.writes.borrow_mut() = Writes::default();
        *self.uniform_page.borrow_mut() = None;
    }
    pub fn set_queue(&self, queue: Option<wgpu::Queue>) {
        *self.queue.borrow_mut() = queue;
    }
    pub fn get_instance(&self) -> Option<wgpu::Instance> {
        self.instance.borrow().clone()
    }
    pub fn get_device(&self) -> Option<wgpu::Device> {
        self.device.borrow().clone()
    }
    pub fn get_queue(&self) -> Option<wgpu::Queue> {
        self.queue.borrow().clone()
    }
    pub fn set_depth_stencil_format(&self, format: Option<wgpu::TextureFormat>) {
        self.depth_stencil_format.set(format);
    }
    pub fn get_depth_stencil_format(&self) -> Option<wgpu::TextureFormat> {
        self.depth_stencil_format.get()
    }
    pub fn set_color_format(&self, format: Option<wgpu::TextureFormat>) {
        self.color_format.set(format);
    }
    pub fn get_color_format(&self) -> Option<wgpu::TextureFormat> {
        self.color_format.get()
    }

    /// Port-only: writes `data` at `offset` in `buffer` ahead of the next
    /// [`submit`](Self::submit)'s commands, as `Queue::write_buffer` does,
    /// but small writes are gathered and copied into place together.
    pub(crate) fn write_buffer(&self, buffer: &wgpu::Buffer, offset: u64, data: &[u8]) {
        if let (Some(device), Some(queue)) = (self.device.borrow().as_ref(), self.queue.borrow().as_ref()) {
            self.writes.borrow_mut().write(device, queue, buffer, offset, data);
        }
    }

    /// Port-only: [`write_buffer`](Self::write_buffer) where the bytes after
    /// `data` up to `padded` bytes from `offset` already hold zeros
    /// (`Writes::write_padded`).
    pub(crate) fn write_buffer_padded(&self, buffer: &wgpu::Buffer, offset: u64, data: &[u8], padded: u64) {
        if let (Some(device), Some(queue)) = (self.device.borrow().as_ref(), self.queue.borrow().as_ref()) {
            self.writes.borrow_mut().write_padded(device, queue, buffer, offset, data, padded);
        }
    }

    /// Port-only: `size` bytes (a multiple of 256) of a new uniform buffer,
    /// zero filled: the buffer and their offset in it. Small ones are taken
    /// in turn from a shared page, so that UBOs made together sit together
    /// and their writes join one copy. A page is freed with the last of its
    /// UBOs (and the bind groups and commands that use them).
    pub(crate) fn allocate_uniform_buffer(&self, device: &wgpu::Device, size: u64) -> (wgpu::Buffer, u64) {
        let create = |size| {
            device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("Uniform Buffer"),
                size,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            })
        };
        if size > UNIFORM_PAGE / 4 {
            return (create(size), 0);
        }
        let mut page = self.uniform_page.borrow_mut();
        let (buffer, taken) = match &mut *page {
            Some((buffer, taken)) if *taken + size <= UNIFORM_PAGE => (buffer, taken),
            _ => {
                let (buffer, taken) = page.insert((create(UNIFORM_PAGE), 0));
                (buffer, taken)
            }
        };
        let offset = *taken;
        *taken += size;
        (buffer.clone(), offset)
    }

    /// Port-only: writes the bytes of `data` at `offset` in `buffer`
    /// straight from the JS array they're in (`Writes::write_from_host`).
    /// `offset` is a multiple of 4; a length that isn't is padded with
    /// zeros.
    #[cfg(target_family = "wasm")]
    pub(crate) fn write_buffer_from_host(
        &self,
        buffer: &wgpu::Buffer,
        offset: u64,
        data: &mln_util::host_bytes::HostBytes,
    ) {
        let (Some(device), Some(queue)) = (self.device.borrow().clone(), self.queue.borrow().clone()) else {
            return;
        };
        let len = data.len() as u64;
        let aligned = len & !(wgpu::COPY_BUFFER_ALIGNMENT - 1);
        let mut writes = self.writes.borrow_mut();
        writes.write_from_host(&device, &queue, buffer, offset, data, aligned);
        if aligned < len
            && let Some(array) = data.array()
        {
            // writeBuffer takes whole 4-byte words.
            let mut tail = [0u8; wgpu::COPY_BUFFER_ALIGNMENT as usize];
            array.subarray(aligned as u32, len as u32).copy_to(&mut tail[..(len - aligned) as usize]);
            writes.write(&device, &queue, buffer, offset + aligned, &tail);
        }
    }

    /// Port-only: submits `command_buffer` after the copies of the writes
    /// gathered by [`write_buffer`](Self::write_buffer).
    pub(crate) fn submit(&self, command_buffer: wgpu::CommandBuffer) {
        let (Some(device), Some(queue)) = (self.device.borrow().clone(), self.queue.borrow().clone()) else {
            return;
        };
        let writes = self.writes.borrow_mut().encode(&device, &queue);
        queue.submit(writes.into_iter().chain([command_buffer]));
    }
}

/// `webgpu::RendererBackend`: a renderer's context on a [`Device`].
pub struct RendererBackend {
    base: RendererBackendBase,
    impl_: Rc<BackendImpl>,
    device: Device,
}

/// The shader types registered by `initShaders`, in the C++ order, with the
/// instanced fill extrusion shaders after their plain ones, as on Metal.
const REGISTERED_SHADERS: &[BuiltIn] = &[
    BuiltIn::BackgroundShader,
    BuiltIn::BackgroundPatternShader,
    BuiltIn::SkyShader,
    BuiltIn::AtmosphereShader,
    BuiltIn::CircleShader,
    BuiltIn::ClippingMaskProgram,
    BuiltIn::CollisionBoxShader,
    BuiltIn::CollisionCircleShader,
    BuiltIn::DebugShader,
    BuiltIn::FillShader,
    BuiltIn::FillOutlineShader,
    BuiltIn::FillPatternShader,
    BuiltIn::FillOutlinePatternShader,
    BuiltIn::FillOutlineTriangulatedShader,
    BuiltIn::FillExtrusionShader,
    BuiltIn::FillExtrusionInstancedShader,
    BuiltIn::FillExtrusionPatternShader,
    BuiltIn::FillExtrusionPatternInstancedShader,
    BuiltIn::HeatmapShader,
    BuiltIn::HeatmapTextureShader,
    BuiltIn::HillshadeShader,
    BuiltIn::HillshadePrepareShader,
    BuiltIn::ColorReliefShader,
    BuiltIn::LineShader,
    BuiltIn::LineGradientShader,
    BuiltIn::LinePatternShader,
    BuiltIn::LineSDFShader,
    BuiltIn::LineGradientSDFShader,
    BuiltIn::RasterShader,
    BuiltIn::SymbolIconShader,
    BuiltIn::SymbolSDFShader,
    BuiltIn::SymbolTextAndIconShader,
    BuiltIn::WideVectorShader,
    BuiltIn::GlobeDepthShader,
];

impl RendererBackend {
    /// A backend on `device`. Backends on one device render independently,
    /// each submitting to the device's queue.
    #[must_use]
    pub fn new(device: &Device) -> Self {
        let impl_ = BackendImpl::default();
        impl_.set_instance(device.instance().cloned());
        impl_.set_device(Some(device.device().clone()));
        impl_.set_queue(Some(device.queue().clone()));
        RendererBackend { base: RendererBackendBase::new(), impl_: Rc::new(impl_), device: device.clone() }
    }

    /// The device the backend renders on.
    pub fn device(&self) -> &Device {
        &self.device
    }

    /// The backend's context, made on first use. Call inside a
    /// `BackendScope`.
    pub fn webgpu_context(&self) -> Option<Rc<Context>> {
        let context = renderer_backend::RendererBackend::get_context(self);
        context.as_any().downcast_ref::<Context>().map(Context::rc)
    }

    pub fn backend_impl(&self) -> &Rc<BackendImpl> {
        &self.impl_
    }

    /// `context.reset()`, which also releases the buffers the context caches
    /// (`Context::release_cached_buffers`).
    pub(crate) fn reset_context(&mut self) {
        if let Some(context) = self.base.reset_context()
            && let Some(context) = context.as_any().downcast_ref::<Context>()
        {
            context.release_cached_buffers();
        }
    }

    /// `createContext()`
    pub fn create_context(&self) -> Rc<Context> {
        Context::new(self.impl_.clone())
    }

    /// `initShaders`: register one `webgpu::ShaderGroup` per built-in shader.
    ///
    /// C++ registers all 31 types; a type whose WGSL is not part of this
    /// build is skipped (TODO(port): all sources once the
    /// `@maplibre//:webgpu-shader-headers` filegroup feeds the extractor).
    /// A duplicate registration is an error, as in C++ (which throws).
    pub fn init_shaders(&self, registry: &ShaderRegistry, program_parameters: &ProgramParameters) {
        for &shader_id in REGISTERED_SHADERS {
            let Some(source) = shader_source(shader_id) else {
                continue;
            };
            let group = Rc::new(ShaderGroup::new(shader_id, program_parameters.clone()));
            if !registry.register_shader_group(group, source.name) {
                panic!("Failed to register {} with shader registry!", source.name);
            }
        }
    }

    pub fn get_instance(&self) -> Option<wgpu::Instance> {
        self.impl_.get_instance()
    }
    pub fn get_device(&self) -> Option<wgpu::Device> {
        self.impl_.get_device()
    }
    pub fn get_queue(&self) -> Option<wgpu::Queue> {
        self.impl_.get_queue()
    }
    pub fn set_depth_stencil_format(&self, format: Option<wgpu::TextureFormat>) {
        self.impl_.set_depth_stencil_format(format);
    }
    pub fn get_depth_stencil_format(&self) -> Option<wgpu::TextureFormat> {
        self.impl_.get_depth_stencil_format()
    }
    pub fn set_color_format(&self, format: Option<wgpu::TextureFormat>) {
        self.impl_.set_color_format(format);
    }
    pub fn get_color_format(&self) -> Option<wgpu::TextureFormat> {
        self.impl_.get_color_format()
    }
}

impl renderer_backend::RendererBackend for RendererBackend {
    fn renderer_backend_base(&self) -> &RendererBackendBase {
        &self.base
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn init_shaders(&self, registry: &ShaderRegistry, parameters: &ProgramParameters) {
        RendererBackend::init_shaders(self, registry, parameters);
    }

    fn create_context(&self) -> Rc<dyn GfxContext> {
        RendererBackend::create_context(self)
    }

    /// WebGPU has no context to make current.
    fn activate(&self) {}
    fn deactivate(&self) {}
}

impl Drop for RendererBackend {
    fn drop(&mut self) {
        self.reset_context();
    }
}