Skip to main content

mln_engine/draw/
mod.rs

1//! What a layer draws with: the other half of the [layer
2//! contract](crate::layer).
3//!
4//! A layer makes its GPU objects through the [`Resources`] its hooks get and
5//! keeps their handles, from vertices, indexes and textures in the
6//! [`Context`]'s stores, the meshes the layers share
7//! ([`RenderStaticData`], [`globe`]) and the quads of a tile's mask
8//! ([`StoredQuads`]): [`Mesh`]es, which draw a program over geometry with
9//! textures, and [`Target`]s, textures of the layer's own that its draws
10//! draw into ahead of the frame and that its other draws sample. An object
11//! goes once its last handle drops and the GPU is done with it.
12//!
13//! Each frame, a layer pushes its draws into its [`Draws`]: into its main
14//! [`Group`], which draws at its place in the layers' order and depth, and
15//! into the groups of its targets. A [`Draw`] of a mesh says where it is
16//! among the group's draws, whether it may draw in the opaque pass, how it
17//! tests depth ([`Depth`]) and whether it clips to its tile, and comes with
18//! the values of its program's blocks of each draw and, for a draw of a
19//! tile, the tile's projection. The engine writes the blocks once for the
20//! group, uploads the meshes, and draws the group in the frame's passes.
21//!
22//! A layer sees its frame as a [`LayerFrame`]: the map's, or a drape's. A
23//! layer that draws with the backend's own commands asks the frame to lend
24//! them ([`Draws::lend`]), at a time in the frame ([`LendAt`]), and gets them
25//! with the frame and where it draws the map ([`Lent`]). What it makes with
26//! them, the engine doesn't know of: the layer frees it once the GPU is done
27//! with it, through work it queues for after the GPU ([`AfterGpu`]).
28//!
29//! The objects and their handles belong to the renderer's thread.
30//!
31//! Inside the engine, the renderer's graphics state over its backend's `Gpu`
32//! (`context`) keeps the stores of what draws share, and the queue that
33//! releases it in order.
34
35pub(crate) mod after_gpu;
36pub(crate) mod atlas_binding;
37pub(crate) mod buffer;
38pub(crate) mod context;
39pub(crate) mod draws;
40pub(crate) mod dynamic_texture;
41pub(crate) mod dynamic_texture_atlas;
42pub(crate) mod identity;
43pub(crate) mod indexes;
44pub(crate) mod mesh;
45pub(crate) mod objects;
46pub(crate) mod owned;
47pub(crate) mod quads;
48pub(crate) mod release;
49pub(crate) mod resources;
50pub(crate) mod segment;
51pub(crate) mod store;
52pub(crate) mod target;
53pub(crate) mod texture2d;
54pub(crate) mod tile_render_data;
55pub(crate) mod uniform_buffer;
56pub(crate) mod upload;
57pub(crate) mod vertex_attribute;
58pub(crate) mod vertices;
59
60/// `m` as the column-major `f32`s that a block's matrix holds.
61#[must_use]
62pub fn cast_matrix(m: &glam::DMat4) -> [f32; 16] {
63    m.as_mat4().to_cols_array()
64}
65
66/// Copies again into `mesh`'s paint attributes, through `refresh`, the
67/// values of its bucket's paint properties that `changes` says feature
68/// state rewrote: a layer's [`prepare`](crate::layer::Layer::prepare) calls
69/// it with what its tile's entry took (`TileEntry::take_changes`).
70pub fn refresh_paint_attributes(
71    mesh: &Mesh,
72    context: &mut Context,
73    changes: crate::layer::BucketChanges,
74    refresh: impl FnOnce(&mut Context, &mut VertexAttributeArray),
75) {
76    if !changes.paint {
77        return;
78    }
79    mesh.update(context, |objects, context| refresh(context, objects.paint_attributes_mut()));
80}
81
82/// The size of a texture of an image of `size`.
83#[must_use]
84pub fn texture_size(size: mln_style::size::Size) -> mln_gfx::Size {
85    mln_gfx::Size::new(size.width, size.height)
86}
87
88pub use crate::draw::after_gpu::{AfterGpu, GpuEnd};
89pub use crate::draw::atlas_binding::AtlasBinding;
90pub use crate::draw::context::Context;
91pub use crate::draw::draws::{Depth, Draw, Draws, Group, LayerFrame, LendAt, Lent};
92pub use crate::draw::identity::SimpleIdentity;
93pub use crate::draw::indexes::{DrawIndexes, Indexes};
94pub use crate::draw::mesh::{Indexed, add_point_quads};
95pub use crate::draw::objects::{DrawGeometry, Mesh, Textures, copy_shared_attributes, raw_vertices};
96pub use crate::draw::quads::{RasterLayoutVertex, StoredQuads, mask_quads};
97pub use crate::draw::resources::Resources;
98pub use crate::draw::segment::{MAX_SEGMENT_VERTICES, SegmentBase};
99pub use crate::draw::store::{SharedIndexes, SharedTexture};
100pub use crate::draw::target::Target;
101pub use crate::draw::texture2d::Texture2D;
102pub use crate::draw::vertex_attribute::{SharedAttribute, Uniforms, VertexAttributeArray};
103pub use crate::draw::vertices::{SharedVertices, TypedVertices, Vertices};
104pub use crate::render::frame_projection::zero_to_one_clip_z;
105pub use crate::render::layer_composite::Composite;
106pub use crate::render::light::EvaluatedLight;
107pub use crate::render::pass::RenderPass;
108pub use crate::render::static_data::RenderStaticData;
109pub use crate::render::tile::RenderTile;
110pub use crate::render::tile_matrix::{globe_extrude_scale, projection_data, to_projection_ubo};
111pub use crate::text::atlas::{ImagePosition, ImagePositions, RectU16};
112
113/// The globe's tile meshes, which a layer that draws whole tiles draws on
114/// the globe, and the rows of the poles' vertices.
115pub mod globe {
116    pub use crate::render::globe::globe_tile_mesh::{GlobeTileMeshCache, RawGlobeTileMesh, raw_globe_tile_mesh};
117    pub use crate::render::globe::{NORTH_POLE_Y, SOUTH_POLE_Y};
118}
119
120/// What a program of a layer that stands on terrain or hides behind it
121/// declares. A program of a layer whose type stands on terrain
122/// ([`OnTerrain::Stands`](crate::layer::OnTerrain::Stands)) declares the
123/// attribute [`GROUND_HEIGHT`](crate::draw::ground::GROUND_HEIGHT), which the engine fills, for a draw of its
124/// terrain variant, with the height of the ground under each of the draw's
125/// points; one whose type hides behind the ground in front of it declares the
126/// texture [`GROUND_DEPTH`](crate::draw::ground::GROUND_DEPTH), which the engine binds to the depth of the
127/// frame's ground.
128pub mod ground {
129    pub use crate::render::terrain::programs::{GROUND_DEM, GROUND_DEPTH, GROUND_HEIGHT, TERRAIN_SURFACE};
130    pub use crate::render::terrain::{
131        FrameGround, GroundHeights, StandingDraw, TerrainElevation, TileGround, height_attribute, point_center,
132    };
133
134    /// The GLSL snippets of the ground: `ground_height!()`, which a program
135    /// that stands on terrain reads the ground's height with,
136    /// `ground_visibility!()`, which a program that hides behind the ground
137    /// declares, `hillshade_shading!()`, the hillshade's shading of its
138    /// slopes, which the terrain's surface shades a lit hillshade's drapes
139    /// with, and `terrain_fog!()` and `terrain_props!()`, the terrain
140    /// surface's vertex prelude, for a program that reads the surface as the
141    /// terrain's programs do.
142    pub mod glsl {
143        pub use crate::{
144            terrain_glsl_ground_height as ground_height, terrain_glsl_ground_visibility as ground_visibility,
145            terrain_glsl_hillshade_shading as hillshade_shading, terrain_glsl_terrain_fog as terrain_fog,
146            terrain_glsl_terrain_props as terrain_props,
147        };
148    }
149
150    /// [`glsl`]'s snippets in GLSL ES, but the fog, which is in its
151    /// `terrain_props!()`, and the names of the terrain's blocks.
152    pub mod glsl_es {
153        pub use crate::render::terrain::glsl_es::TERRAIN_NAMES;
154        pub use crate::{
155            terrain_glsl_es_ground_height as ground_height, terrain_glsl_es_ground_visibility as ground_visibility,
156            terrain_glsl_es_hillshade_shading as hillshade_shading, terrain_glsl_es_terrain_props as terrain_props,
157        };
158    }
159
160    /// [`glsl`]'s snippets in MSL, but the surface's prelude.
161    pub mod msl {
162        pub use crate::{
163            terrain_msl_ground_height as ground_height, terrain_msl_ground_visibility as ground_visibility,
164            terrain_msl_hillshade_shading as hillshade_shading,
165        };
166    }
167
168    /// [`glsl`]'s snippets in WGSL, with the surface's prelude as one text.
169    pub mod wgsl {
170        pub use crate::render::terrain::wgsl::TERRAIN_PRELUDE;
171        pub use crate::{
172            terrain_wgsl_ground_height as ground_height, terrain_wgsl_ground_visibility as ground_visibility,
173            terrain_wgsl_hillshade_shading as hillshade_shading,
174        };
175    }
176}