mln-rs-renderer 0.0.13

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
Documentation
use mln_gfx::ShaderRegistry;
use mln_gfx::shaders::segment::{MAX_SEGMENT_VERTICES, SegmentBase};
use mln_util::constants;

use crate::buckets::fill_extrusion_bucket::FillExtrusionStaticVertex;

/// The most walls whose quads a segment draws: as many as its 16-bit
/// indices reach.
const FILL_EXTRUSION_SEGMENT_WALLS: usize = MAX_SEGMENT_VERTICES / 4;

pub(crate) struct RenderStaticData {
    pub(crate) has_3d: bool,
    pub(crate) uploaded: std::cell::Cell<bool>,
    pub(crate) shaders: Box<ShaderRegistry>,
}

impl RenderStaticData {
    #[must_use]
    pub(crate) fn new(shaders: Box<ShaderRegistry>) -> Self {
        RenderStaticData { has_3d: false, uploaded: std::cell::Cell::new(false), shaders }
    }

    /// Uploads the static data. Takes `&self` (the flag is a `Cell`): the
    /// renderer calls it while `PaintParameters` borrows the static data.
    pub(crate) fn upload(&self, _context: &crate::gfx::context::Context) {
        if !self.uploaded.get() {
            // these are still used by stencil buffer rendering
            self.uploaded.set(true);
        }
    }

    /// The tile's four corners, as position-only vertices (`a_pos`).
    #[must_use]
    pub(crate) fn tile_vertices() -> Vec<[i16; 2]> {
        let extent = constants::EXTENT as i16;
        vec![[0, 0], [extent, 0], [0, extent], [extent, extent]]
    }

    #[must_use]
    pub(crate) fn quad_triangle_indices() -> Vec<u16> {
        vec![0, 1, 2, 1, 2, 3]
    }

    #[must_use]
    pub(crate) fn raster_vertices() -> Vec<crate::buckets::raster_bucket::RasterLayoutVertex> {
        use geometry::Point;

        use crate::buckets::raster_bucket::RasterBucket;
        let e = constants::EXTENT as i16;
        let t = constants::EXTENT as u16;
        vec![
            RasterBucket::layout_vertex(Point::new(0, 0), Point::new(0, 0)),
            RasterBucket::layout_vertex(Point::new(e, 0), Point::new(t, 0)),
            RasterBucket::layout_vertex(Point::new(0, e), Point::new(0, t)),
            RasterBucket::layout_vertex(Point::new(e, e), Point::new(t, t)),
        ]
    }

    #[must_use]
    pub(crate) fn raster_segments() -> Vec<SegmentBase> {
        vec![SegmentBase::with_lengths(0, 0, 4, 6)]
    }

    #[must_use]
    pub(crate) fn fill_extrusion_vertices() -> Vec<FillExtrusionStaticVertex> {
        vec![[1, 0], [1, 1], [0, 0], [0, 1]]
    }

    #[must_use]
    pub(crate) fn fill_extrusion_triangle_indices() -> Vec<u16> {
        // ┌──────┐
        // │ 0  1 │ Counter-Clockwise winding order.
        // │      │ Triangle 1: 0 => 2 => 1
        // │ 2  3 │ Triangle 2: 1 => 2 => 3
        // └──────┘
        vec![0, 2, 1, 1, 2, 3]
    }

    /// A segment that draws `instance_count` fill-extrusion walls.
    #[must_use]
    pub(crate) fn fill_extrusion_segment(instance_count: usize) -> SegmentBase {
        SegmentBase { instance_count, ..SegmentBase::with_lengths(0, 0, 4, 6) }
    }

    /// The triangles of the walls that a segment of
    /// `fill_extrusion_wall_segments` draws, each wall's quad four vertices
    /// after the last's, as `fill_extrusion_triangle_indices` draws the quad.
    #[must_use]
    pub(crate) fn fill_extrusion_wall_indices() -> Vec<u16> {
        let quad = Self::fill_extrusion_triangle_indices();
        (0..FILL_EXTRUSION_SEGMENT_WALLS as u16)
            .flat_map(|wall| quad.iter().map(move |&index| 4 * wall + index))
            .collect()
    }

    /// The segments that draw `walls` fill-extrusion walls whose quads are in
    /// the vertex buffer, one after the other, from the indices of
    /// `fill_extrusion_wall_indices`.
    #[must_use]
    pub(crate) fn fill_extrusion_wall_segments(walls: usize) -> Vec<SegmentBase> {
        (0..walls)
            .step_by(FILL_EXTRUSION_SEGMENT_WALLS)
            .map(|first| {
                let count = FILL_EXTRUSION_SEGMENT_WALLS.min(walls - first);
                SegmentBase::with_lengths(4 * first, 0, 4 * count, 6 * count)
            })
            .collect()
    }

    /// The corners of the quad that an instanced fill outline draws a
    /// segment with: x picks the segment's start (0) or end (1), y its side.
    #[must_use]
    pub(crate) fn fill_outline_vertices() -> Vec<[i16; 2]> {
        vec![[0, 0], [0, 1], [1, 0], [1, 1]]
    }

    /// A segment that draws `instance_count` fill outline segments, as
    /// quads of `fill_outline_vertices` and `quad_triangle_indices`.
    #[must_use]
    pub(crate) fn fill_outline_segment(instance_count: usize) -> SegmentBase {
        SegmentBase { instance_count, ..SegmentBase::with_lengths(0, 0, 4, 6) }
    }

    #[must_use]
    pub(crate) fn tile_triangle_segments() -> Vec<SegmentBase> {
        vec![SegmentBase::with_lengths(0, 0, 4, 6)]
    }

    // dem-heatmap
    #[must_use]
    pub(crate) fn heatmap_texture_vertices() -> Vec<crate::buckets::heatmap_bucket::HeatmapTextureLayoutVertex> {
        use geometry::Point;

        use crate::buckets::heatmap_bucket::HeatmapBucket;
        vec![
            HeatmapBucket::texture_vertex(Point::new(0, 0)),
            HeatmapBucket::texture_vertex(Point::new(1, 0)),
            HeatmapBucket::texture_vertex(Point::new(0, 1)),
            HeatmapBucket::texture_vertex(Point::new(1, 1)),
        ]
    }

    #[must_use]
    pub(crate) fn heatmap_texture_segments() -> Vec<SegmentBase> {
        vec![SegmentBase::with_lengths(0, 0, 4, 6)]
    }
}