mln-rs-engine 0.0.21

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
//! The engine's programs: its own ([`programs`]), the blocks they read
//! ([`ubo`]) and their text in each language, and every program it draws
//! with ([`PROGRAMS`]): those and the terrain's ([`terrain`]). A layer
//! type's programs are in its own module.

// Every built-in program has text in WGSL, MSL, GLSL and GLSL ES. Each
// reads the vertex layouts and the blocks (`ubo`) that the renderer makes,
// and they all draw the same.
//
// The GLSL compiles with naga's GLSL front end:
// - The stages read what their programs declare (`mln_glsl`'s declarations)
//   by its names there.
// - A texture and its sampler are two bindings, `<name>_texture` and
//   `<name>_sampler`, combined where they are sampled.
// - Symbols read a quad's atlas origin and height offset as the vertex
//   attributes `tex_origin` and `height_offset`.
// - A fill extrusion wall reads its outline vertex and the next one as
//   instance attributes. The pulled walls and outlines have no vertex
//   inputs: they read the bucket's vertices from storage buffers in the
//   drawable set, by `gl_InstanceIndex` plus `gl_VertexIndex` / 4.
// - Atlas positions go through `atlas_pos`, and the pattern rectangles reach
//   the fragment stage flat.
// - On the globe, a symbol vertex stage puts a screen-aligned label whose
//   anchor is behind the horizon outside the clip volume, unless the label
//   is raised.
// - On the globe, points on the sphere are offsets from the tile's anchor,
//   which the projection matrix carries, so float32 keeps them exact close
//   in (`projectToSphere`).
// - What naga doesn't take is written another way: no precision qualifiers,
//   `int` for the `bool` members of buffers, no sampler parameters, and
//   `flat` on an integer output.
// - Clip space is y-up, as in OpenGL, Metal, D3D and naga's IR: no stage
//   flips Y; the SPIR-V back end does for Vulkan.
//
// The GLSL ES:
// - `atlas_pos` (the fragment prelude) places the samples of an atlas image,
//   in the pattern and symbol shaders, and the pattern rectangles and the
//   symbols' atlas origins reach the fragment shaders `flat`.
// - The symbol shaders read `a_tex_origin`, the atlas origin of a quad's
//   image, and `a_height_offset`, the symbol's `symbol-height-offset`, as
//   GL JS's do.
// - On the globe, the symbol shaders drop a screen-aligned label whose anchor
//   is behind the horizon, unless the label is raised.
// - The fill extrusion shaders declare `gl_Position` invariant. Their vertex
//   shaders draw the roofs, and the instanced ones the walls: an instance of
//   a quad per outline vertex, as the WGSL does.
// - The instanced fill outline shaders draw an outline where the context
//   draws no 2 px lines: a quad per segment, two pixels wide as a wide line
//   is, with the line shaders' fragment shaders.
// - The fill, fill outline, fill extrusion and line pattern fragment shaders
//   set `precision highp float` on GL ES, as GL JS's do: overzoomed past a
//   source's maxzoom, their pattern positions outgrow a 16-bit mediump
//   float. The background pattern draws at the current zoom, so its
//   positions stay small.
// - The clipping mask program projects through the projection block as the
//   other shaders do, and has a globe variant.
// - On the globe, points on the sphere are offsets from the tile's anchor,
//   which the projection matrix carries, so float32 keeps them exact close
//   in (`projectToSphere`).
// - The terrain shaders apply the WGSL's height rule.

#[rustfmt::skip]
mod glsl;
#[rustfmt::skip]
mod glsl_es;
#[rustfmt::skip]
mod msl;
pub mod programs;
pub mod ubo;
mod wgsl;

/// The terrain's programs and the blocks they read, which the terrain keeps
/// with its text, and its surface's prelude in each language, for a program
/// that reads the surface as the terrain's programs do: `ground_height!()`,
/// with `terrain_fog!()` and `terrain_props!()` in GLSL, the names of the
/// terrain's blocks in GLSL ES, and the prelude as one text in WGSL.
pub mod terrain {
    pub use crate::render::terrain::{programs, ubo};

    pub mod glsl {
        pub use crate::{
            terrain_glsl_ground_height as ground_height, terrain_glsl_terrain_fog as terrain_fog,
            terrain_glsl_terrain_props as terrain_props,
        };
    }

    pub mod glsl_es {
        pub use crate::render::terrain::glsl_es::TERRAIN_NAMES;
        pub use crate::{
            terrain_glsl_es_ground_height as ground_height, terrain_glsl_es_terrain_props as terrain_props,
        };
    }

    pub mod msl {
        pub use crate::terrain_msl_ground_height as ground_height;
    }

    pub mod wgsl {
        pub use crate::render::terrain::wgsl::TERRAIN_PRELUDE;
    }
}

use mln_gfx::projection_variant::ProjectionVariant;
use mln_gfx::shaders::program::Program;

/// The programs the engine draws with: its own and the terrain's. Each
/// crate of layer types lists its own (in `mln_builtins`,
/// `mln_builtins::shaders::PROGRAMS`).
pub static PROGRAMS: &[&Program] = &[
    &programs::CLIPPING_MASK,
    &programs::GLOBE_DEPTH,
    &programs::SKY,
    &programs::ATMOSPHERE,
    &programs::LAYER_COMPOSITE,
    &crate::render::terrain::programs::TERRAIN,
    &crate::render::terrain::programs::TERRAIN_DEPTH,
];

/// The projection variants of a program: those that stand on terrain too,
/// for one that `stands`, a program of a layer type that stands on terrain
/// ([`OnTerrain::Stands`](crate::layer::OnTerrain::Stands)).
#[must_use]
pub fn projection_variants(stands: bool) -> &'static [ProjectionVariant] {
    const ALL: [ProjectionVariant; 4] = [
        ProjectionVariant::Mercator,
        ProjectionVariant::Globe,
        ProjectionVariant::Terrain,
        ProjectionVariant::GlobeTerrain,
    ];
    if stands { &ALL } else { &ALL[..2] }
}