mln-rs-engine 0.0.18

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! The terrain's draws: one per terrain tile and stack of draped layers, of
//! the shared grids, each with the tile's projection, where it reads its
//! DEM and its drape's light in entries of its stack's storage buffers,
//! raised by the DEM's texture and sampling the tile's drape of the stack,
//! which they shade with the stack's light, if it has one, and cover with
//! the frame's fog; one per terrain tile of the surface's depth, where 3D
//! layers draw before the surface; and one per terrain tile of the ground's
//! depth into its texture, where layers hide behind the ground.

use std::mem::size_of;

use mln_gfx::color_mode::ColorMode;
use mln_gfx::cull_face_mode::CullFaceMode;
use mln_gfx::gpu::{ClipDepthRange, Gpu, ShaderKey};
use mln_gfx::projection_variant::ProjectionVariant;
use mln_gfx::shaders::bindings::PROJECTION_ID;
use mln_gfx::shaders::layer_ubo::ProjectionUBO;
use mln_gfx::{AttributeDataType, DepthMaskType, DrawMode};
use mln_style::tile_id::CanonicalTileID;

use super::mesh::{TerrainMesh, TerrainVertex};
use super::{DemBindings, DrapeStacks, Drapes, Surface, TerrainCover};
use crate::camera::View;
use crate::draw::context::Context;
use crate::draw::indexes::Indexes;
use crate::draw::objects::{DrawGeometry, DrawObjects, Textures};
use crate::draw::segment::SegmentBase;
use crate::draw::store::SharedIndexes;
use crate::draw::vertex_attribute::{SharedAttribute, Uniforms, VertexAttributeArray};
use crate::draw::vertices::SharedVertices;
use crate::layer::layer_drawing::{
    DrawDepth, DrawList, FrameDraw, KeptDraw, LayerDraws, TileMap, finish_tiles, tile_objects,
};
use crate::layers::render_hillshade_layer::lat_range;
use crate::log_target;
use crate::render::frame_projection::zero_to_one_clip_z;
use crate::render::paint_parameters::PaintParameters;
use crate::render::terrain::programs;
use crate::render::terrain::ubo::{HillshadeTilePropsUBO, TerrainDrawableUBO, TerrainFog, TerrainSurfaceUBO};
use crate::render::tile_matrix::to_projection_ubo;

/// The ids of the buffers that the draws bind.
const DRAWABLE_ID: usize = programs::TERRAIN.buffer_id("drawableVector");
const LIGHT_TILE_ID: usize = programs::TERRAIN.buffer_id("lightVector");
const SURFACE_ID: usize = programs::TERRAIN.buffer_id("terrain");
const LIGHT_ID: usize = programs::TERRAIN.buffer_id("props");

/// The slots of the programs' textures that the draws bind.
const GROUND_DEM_SLOT: usize = programs::TERRAIN.texture_slot("ground_dem");
const DRAPE_SLOT: usize = programs::TERRAIN.texture_slot("drape");

/// A grid, in the context's stores.
struct SharedMesh {
    /// Whether it has skirts.
    skirts: bool,
    /// Whether it has a cap to the north and to the south pole.
    poles: [bool; 2],
    vertices: SharedVertices,
    vertex_count: usize,
    indexes: SharedIndexes,
    index_count: usize,
}

/// What a tile's draw reads of its own: where it reads its DEM, its
/// projection, and its drape's light.
#[derive(Clone, Copy)]
struct TileUniforms {
    drawable: TerrainDrawableUBO,
    projection: ProjectionUBO,
    light: HillshadeTilePropsUBO,
}

/// The draws of the terrain's tiles for one stack of draped layers.
#[derive(Default)]
struct StackDraws {
    /// The GPU objects of each tile's draw.
    tiles: TileMap<KeptDraw>,
    /// The frame's draws of them.
    frame: LayerDraws,
    /// The frame's draws as the update makes them, with their uniforms.
    draw_list: DrawList<TileUniforms>,
}

/// How the draws were made: the projection variant of their shader,
/// whether their grids have skirts, and the triangles they cull, those
/// facing away, whose winding a flipped viewport turns round.
type BuiltFor = (ProjectionVariant, bool, CullFaceMode);

/// The draws of the terrain's tiles, a set for each stack, which draw in the
/// translucent pass, a set of the surface's depth and one of the ground's
/// depth, where the frame draws them, nearest the center first,
/// testing and writing the 3D depth range. The sets share the grids, the
/// DEM textures and each tile's projection and DEM coordinates.
#[derive(Default)]
pub(crate) struct TerrainDraws {
    /// The draws of each of the frame's stacks.
    stacks: Vec<StackDraws>,
    /// The draws of the surface's depth, transparent, which draw before the
    /// frame's layers ([`TerrainDraws::render_depth`]): the same draws as
    /// the stacks', so the same depth.
    depth: Option<StackDraws>,
    /// The draws of the ground's depth into its texture
    /// ([`TerrainDraws::render_ground`]).
    ground: Option<StackDraws>,
    /// The surface's shader and the ground depth's, with the variant they
    /// are of.
    shaders: Option<(ProjectionVariant, [ShaderKey; 2])>,
    /// The grids the draws hold: on the globe, the tiles of the first and
    /// last rows draw grids with caps to the poles.
    meshes: Vec<SharedMesh>,
    /// How the draws and the grids were made.
    built_for: Option<BuiltFor>,
    /// Whether the backend failed to make the surface's shader.
    failed: bool,
}

impl TerrainDraws {
    /// Makes the frame's draws of the tiles of `cover`, one for each of the
    /// stacks of `drapes`, which read the DEM textures of `dems`, sample the
    /// tile's drape of their stack, shade it with the stack's light of
    /// `stacks` if it has one, cover it with `fog`, and draw as `surface`
    /// says, and, if `depth_first`, one of the surface's depth, and, if
    /// `ground`, one of the ground's depth; or drops the draws and what they
    /// share for `None`, a frame without terrain, or where there is nothing
    /// to draw. Each tile projects as the frame's view does, onto the globe
    /// on a globe.
    pub(crate) fn update(
        &mut self,
        gpu: &mut dyn Gpu,
        parameters: &mut PaintParameters<'_>,
        cover: Option<(&TerrainCover, &DemBindings)>,
        (surface, fog): (Surface, TerrainFog),
        (drapes, lights): (&Drapes, &DrapeStacks),
        (depth_first, ground): (bool, bool),
    ) {
        let stacks = drapes.stacks();
        let draws = stacks > 0 || ground;
        let shaders = cover.filter(|_| draws).and_then(|_| self.shaders(gpu, parameters.context, parameters.view));
        let (Some((cover, dems)), Some([shader, depth_shader])) = (cover, shaders) else {
            self.stacks.clear();
            self.depth = None;
            self.ground = None;
            self.meshes.clear();
            self.built_for = None;
            return;
        };

        let cull_face_mode = parameters.cull_back_faces();
        let (view, proj_matrix) = (parameters.view, &parameters.transform_params.proj_matrix);
        let context = &mut *parameters.context;
        let built_for = (variant(view), surface.skirts, cull_face_mode);
        if self.built_for != Some(built_for) {
            // The draws hold the other variant's shader, the other grids, or
            // cull the other way.
            self.stacks.clear();
            self.depth = None;
            self.ground = None;
            self.meshes.clear();
            self.built_for = Some(built_for);
        }

        // A backend whose clip depth runs from -1 writes the depth of one
        // whose runs from 0, as fill extrusions do. The globe projects in its
        // shaders.
        let remap_clip_z = context.caps().clip_depth == ClipDepthRange::NegativeOneToOne && !view.is_globe();
        self.stacks.resize_with(stacks, StackDraws::default);
        for (set, wanted) in [(&mut self.depth, depth_first), (&mut self.ground, ground)] {
            if !wanted {
                *set = None;
            } else if set.is_none() {
                *set = Some(StackDraws::default());
            }
        }
        for stack in self.stacks.iter_mut().chain(&mut self.depth).chain(&mut self.ground) {
            stack.tiles.set_tiles(cover.tiles.iter().map(|tile| tile.id));
        }
        for (index, tile) in cover.tiles.iter().enumerate() {
            let bound = tile.dem.and_then(|(id, coords)| Some((dems.dem(&id)?, coords)));
            let (dem_texture, dem_coords) = match bound {
                Some(((texture, dim), coords)) => {
                    (texture, [coords.scale, coords.offset[0], coords.offset[1], dim as f32])
                }
                None => (dems.flat(), [0.0; 4]),
            };
            let mut projection = view.projection_data_with(&tile.id.to_unwrapped(), proj_matrix);
            if remap_clip_z {
                zero_to_one_clip_z(&mut projection.main_matrix);
                zero_to_one_clip_z(&mut projection.fallback_matrix);
            }
            let uniforms = TileUniforms {
                drawable: TerrainDrawableUBO { dem_coords },
                projection: to_projection_ubo(&projection),
                light: HillshadeTilePropsUBO::default(),
            };
            let latrange = lat_range(&tile.id.to_unwrapped());
            // The ground's depth is the surface's without the skirts, which
            // fill the cracks between tiles: a skirt that the camera sees
            // edge-on draws the depth of no ground, as a rasterizer makes it.
            let poles = poles(view, tile.id.canonical);
            let surface_mesh = mesh(&mut self.meshes, context, surface.skirts, poles);
            let ground_mesh = self.ground.is_some().then(|| mesh(&mut self.meshes, context, false, poles));
            let meshes = &self.meshes;
            for (set, draws) in sets(&mut self.stacks, &mut self.depth, &mut self.ground) {
                let Some(entry) = draws.tiles.entry(&tile.id) else {
                    continue;
                };
                // Where a stack draws nothing in the tile, it has no drape
                // and no draw, but the first stack's draw writes the depth
                // the stacks above test, so it draws transparent there. The
                // surface depth's draws draw so everywhere.
                let draw = if let Set::Ground = set {
                    let Some(mesh) = ground_mesh.map(|index| &meshes[index]) else {
                        continue;
                    };
                    let objects = || mesh_objects(&depth_shader, ColorMode::unblended(), mesh, cull_face_mode);
                    entry.value.get_or_insert_with(|| KeptDraw::new(objects()))
                } else {
                    let drape = match set {
                        Set::Stack(stack) => {
                            drapes.texture(stack, &tile.id).or_else(|| (stack == 0).then(|| dems.flat()))
                        }
                        Set::Depth | Set::Ground => Some(dems.flat()),
                    };
                    let Some(drape) = drape.cloned() else {
                        *entry.value = None;
                        continue;
                    };
                    let mesh = &meshes[surface_mesh];
                    let objects = || mesh_objects(&shader, ColorMode::alpha_blended(), mesh, cull_face_mode);
                    let draw = entry.value.get_or_insert_with(|| KeptDraw::new(objects()));
                    draw.objects.set_texture(DRAPE_SLOT, Some(&drape));
                    draw
                };
                draw.objects.set_texture(GROUND_DEM_SLOT, Some(dem_texture));
                let frame_draw = FrameDraw {
                    priority: index as i64,
                    depth: DrawDepth::Volume { mask: DepthMaskType::ReadWrite },
                    ..draw.frame_draw(Some(tile.id))
                };
                // A stack without a light, and the surface depth's draws,
                // draw their drapes' colors.
                let light = set
                    .stack()
                    .and_then(|stack| lights.light(stack))
                    .map(|light| HillshadeTilePropsUBO { latrange, ..light.tile });
                draws.draw_list.push(frame_draw, TileUniforms { light: light.unwrap_or_default(), ..uniforms });
            }
        }
        let surface_ubo = TerrainSurfaceUBO::new(&surface.props, &fog);
        for (set, StackDraws { tiles, frame, draw_list }) in sets(&mut self.stacks, &mut self.depth, &mut self.ground) {
            let light = set.stack().and_then(|stack| lights.light(stack)).map(|light| light.props).unwrap_or_default();
            finish_tiles(draw_list, frame, tiles, gpu, context, |writer| {
                writer.storage(DRAWABLE_ID, |uniforms| uniforms.drawable);
                writer.storage(PROJECTION_ID, |uniforms| uniforms.projection);
                writer.storage(LIGHT_TILE_ID, |uniforms| uniforms.light);
                writer.uniform(SURFACE_ID, &surface_ubo);
                writer.uniform(LIGHT_ID, &light);
            });
        }
    }

    /// Whether the backend failed to make the surface's shader, which then
    /// draws nothing until the shaders are made again.
    pub(crate) fn failed(&self) -> bool {
        self.failed
    }

    /// The surface's shader and the ground depth's in `view`'s projection,
    /// made on `gpu` in `context` the first time; `None`, logged once, where
    /// the backend has either not.
    pub(crate) fn shaders(&mut self, gpu: &mut dyn Gpu, context: &mut Context, view: &View) -> Option<[ShaderKey; 2]> {
        let variant = variant(view);
        if let Some((made_for, shaders)) = &self.shaders
            && *made_for == variant
        {
            return Some(shaders.clone());
        }
        if self.failed {
            return None;
        }
        let shaders = [&programs::TERRAIN, &programs::TERRAIN_DEPTH]
            .map(|program| context.shader_key(program, Uniforms::default(), variant));
        if let Err(error) = shaders.iter().try_for_each(|shader| context.shader(gpu, shader).map(|_| ())) {
            self.failed = true;
            log::warn!(target: log_target::RENDER, "Terrain draws nothing on this backend: {error}");
            return None;
        }
        Some(self.shaders.insert((variant, shaders)).1.clone())
    }

    /// Uploads the GPU objects of the frame's draws.
    pub(crate) fn upload(&mut self, gpu: &mut dyn Gpu, context: &mut Context) {
        for StackDraws { tiles, frame, .. } in self.stacks.iter_mut().chain(&mut self.depth).chain(&mut self.ground) {
            frame.upload(gpu, tiles, tile_objects, context);
        }
    }

    /// Draws the frame's draws of `stack` into the open pass of `gpu`, the
    /// context's.
    pub(crate) fn render(&mut self, stack: usize, gpu: &mut dyn Gpu, parameters: &mut PaintParameters<'_>) {
        if let Some(StackDraws { tiles, frame, .. }) = self.stacks.get_mut(stack) {
            frame.render(tiles, tile_objects, gpu, parameters);
        }
    }

    /// Whether the frame's draws draw the ground's depth.
    pub(crate) fn draws_ground(&self) -> bool {
        self.ground.is_some()
    }

    /// Draws the frame's draws of the surface's depth, if it made them, into
    /// the open pass of `gpu`, the context's: the frame's pass, before its
    /// layers.
    pub(crate) fn render_depth(&mut self, gpu: &mut dyn Gpu, parameters: &mut PaintParameters<'_>) {
        if let Some(StackDraws { tiles, frame, .. }) = &mut self.depth {
            frame.render(tiles, tile_objects, gpu, parameters);
        }
    }

    /// Draws the frame's draws of the ground's depth, if it made them, into
    /// the open pass of `gpu`, the context's: the ground's pass.
    pub(crate) fn render_ground(&mut self, gpu: &mut dyn Gpu, parameters: &mut PaintParameters<'_>) {
        if let Some(StackDraws { tiles, frame, .. }) = &mut self.ground {
            frame.render(tiles, tile_objects, gpu, parameters);
        }
    }
}

/// A set of the terrain's draws.
#[derive(Clone, Copy)]
enum Set {
    /// A stack of draped layers' surface.
    Stack(usize),
    /// The surface's depth, in the frame's pass.
    Depth,
    /// The ground's depth, in its own pass.
    Ground,
}

impl Set {
    /// The stack, for a stack's set.
    fn stack(self) -> Option<usize> {
        match self {
            Set::Stack(stack) => Some(stack),
            Set::Depth | Set::Ground => None,
        }
    }
}

/// The sets of draws: each of `stacks`, then `depth` and `ground` where the
/// frame has them.
fn sets<'a>(
    stacks: &'a mut [StackDraws],
    depth: &'a mut Option<StackDraws>,
    ground: &'a mut Option<StackDraws>,
) -> impl Iterator<Item = (Set, &'a mut StackDraws)> {
    let stacks = stacks.iter_mut().enumerate().map(|(stack, draws)| (Set::Stack(stack), draws));
    stacks
        .chain(depth.iter_mut().map(|draws| (Set::Depth, draws)))
        .chain(ground.iter_mut().map(|draws| (Set::Ground, draws)))
}

/// The variant of the surface's shader in `view`: the globe's while it
/// draws the globe, through its handover to Mercator too.
fn variant(view: &View) -> ProjectionVariant {
    if view.is_globe() { ProjectionVariant::Globe } else { ProjectionVariant::Mercator }
}

/// Whether the grid of `tile` in `view` closes the north and the south
/// pole: on the globe, for a tile of the first or the last row.
fn poles(view: &View, tile: CanonicalTileID) -> [bool; 2] {
    let last = (1u32 << tile.z) - 1;
    [view.is_globe() && tile.y == 0, view.is_globe() && tile.y == last]
}

/// The index in `meshes` of the grid with `skirts` and the caps of `poles`,
/// made in the stores of `context` the first time.
fn mesh(meshes: &mut Vec<SharedMesh>, context: &mut Context, skirts: bool, poles: [bool; 2]) -> usize {
    if let Some(index) = meshes.iter().position(|mesh| mesh.skirts == skirts && mesh.poles == poles) {
        return index;
    }
    let TerrainMesh { vertices, indices } = TerrainMesh::new(skirts, poles);
    meshes.push(SharedMesh {
        skirts,
        poles,
        vertex_count: vertices.len(),
        vertices: context.add_vertices(vertices),
        index_count: indices.len(),
        indexes: context.add_indexes(Indexes::new(indices)),
    });
    meshes.len() - 1
}

/// The GPU objects of a tile's draw of `mesh` with `shader`, in
/// `color_mode`, culling as `cull_face_mode` says.
fn mesh_objects(
    shader: &ShaderKey,
    color_mode: ColorMode,
    mesh: &SharedMesh,
    cull_face_mode: CullFaceMode,
) -> DrawObjects {
    let mut vertices = VertexAttributeArray::new();
    vertices.set_shared(
        "pos",
        SharedAttribute::new(mesh.vertices.clone(), AttributeDataType::Short4, 0, size_of::<TerrainVertex>() as u32),
    );
    let geometry = DrawGeometry {
        vertices,
        vertex_count: mesh.vertex_count,
        mode: DrawMode::Triangles(),
        indexes: mesh.indexes.clone().into(),
        segments: vec![SegmentBase::with_lengths(0, 0, mesh.vertex_count, mesh.index_count)],
        instances: None,
    };
    DrawObjects::new(shader.clone(), color_mode, cull_face_mode, geometry, Textures::default())
}