# upload_stage.rs - source
Source of the Rust file `maplibre/src/render/stages/upload_stage.rs`.
Keywords: rust, rustlang, rust-lang

      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

    //! Uploads data to the GPU which is needed for rendering.

    use crate::context::MapContext;
    use crate::coords::{ViewRegion, Zoom};
    use crate::io::tile_repository::{StoredLayer, TileRepository};
    use crate::render::camera::ViewProjection;
    use crate::render::resource::IndexEntry;
    use crate::render::shaders::{
        ShaderCamera, ShaderFeatureStyle, ShaderGlobals, ShaderLayerMetadata, Vec4f32,
    };
    use crate::render::tile_view_pattern::TileInView;
    use crate::render::util::Eventually::Initialized;
    use crate::schedule::Stage;
    use crate::{RenderState, Renderer, Style};

    use std::iter;

    #[derive(Default)]
    pub struct UploadStage;

    impl Stage for UploadStage {
        #[tracing::instrument(name = "UploadStage", skip_all)]
        fn run(
            &mut self,
            MapContext {
                view_state,
                style,
                tile_repository,
                renderer: Renderer { queue, state, .. },
                ..
            }: &mut MapContext,
        ) {
            let visible_level = view_state.visible_level();

            let view_proj = view_state.view_projection();

            if let Initialized(globals_bind_group) = &state.globals_bind_group {
                // Update globals
                queue.write_buffer(
                    &globals_bind_group.uniform_buffer,
                    0,
                    bytemuck::cast_slice(&[ShaderGlobals::new(ShaderCamera::new(
                        view_proj.downcast().into(),
                        view_state
                            .camera
                            .position
                            .to_homogeneous()
                            .cast::<f32>()
                            .unwrap()
                            .into(),
                    ))]),
                );
            }

            let view_region = view_state
                .camera
                .view_region_bounding_box(&view_proj.invert())
                .map(|bounding_box| ViewRegion::new(bounding_box, 0, *view_state.zoom, visible_level));

            if let Some(view_region) = &view_region {
                self.upload_tile_geometry(state, queue, tile_repository, style, view_region);
                self.upload_tile_view_pattern(state, queue, &view_proj);
                self.update_metadata();
            }
        }
    }

    impl UploadStage {
        #[tracing::instrument(skip_all)]
        pub(crate) fn update_metadata(&self) {
            /*let animated_one = 0.5
            * (1.0
                + ((SystemTime::now()
                    .duration_since(SystemTime::UNIX_EPOCH)
                    .unwrap()
                    .as_secs_f64()
                    * 10.0)
                    .sin()));*/

            // Factor which determines how much we need to adjust the width of lines for example.
            // If zoom == z -> zoom_factor == 1

            /*  for entries in self.buffer_pool.index().iter() {
            for entry in entries {
                let world_coords = entry.coords;*/

            // TODO: Update features
            /*let source_layer = entry.style_layer.source_layer.as_ref().unwrap();

            if let Some(result) = scheduler
                .get_tile_repository()
                .iter_tessellated_layers_at(&world_coords)
                .unwrap()
                .find(|layer| source_layer.as_str() == layer.layer_name())
            {
                let color: Option<Vec4f32> = entry
                    .style_layer
                    .paint
                    .as_ref()
                    .and_then(|paint| paint.get_color())
                    .map(|mut color| {
                        color.color.b = animated_one as f32;
                        color.into()
                    });

                match result {
                    LayerTessellateResult::UnavailableLayer { .. } => {}
                    LayerTessellateResult::TessellatedLayer {
                        layer_data,
                        feature_indices,
                        ..
                    } => {

                        let feature_metadata = layer_data
                            .features()
                            .iter()
                            .enumerate()
                            .flat_map(|(i, _feature)| {
                                iter::repeat(ShaderFeatureStyle {
                                    color: color.unwrap(),
                                })
                                .take(feature_indices[i] as usize)
                            })
                            .collect::<Vec<_>>();

                        self.buffer_pool.update_feature_metadata(
                            &self.queue,
                            entry,
                            &feature_metadata,
                        );
                    }
                }
            }*/
            /*            }
            }*/
        }

        #[tracing::instrument(skip_all)]
        pub fn upload_tile_view_pattern(
            &self,
            RenderState {
                tile_view_pattern, ..
            }: &mut RenderState,
            queue: &wgpu::Queue,
            view_proj: &ViewProjection,
        ) {
            if let Initialized(tile_view_pattern) = tile_view_pattern {
                tile_view_pattern.upload_pattern(queue, view_proj);
            }
        }

        #[tracing::instrument(skip_all)]
        pub fn upload_tile_geometry(
            &self,
            RenderState { buffer_pool, .. }: &mut RenderState,
            queue: &wgpu::Queue,
            tile_repository: &TileRepository,
            style: &Style,
            view_region: &ViewRegion,
        ) {
            if let Initialized(buffer_pool) = buffer_pool {
                // Upload all tessellated layers which are in view
                for world_coords in view_region.iter() {
                    let loaded_layers = buffer_pool
                        .get_loaded_layers_at(&world_coords)
                        .unwrap_or_default();
                    if let Some(available_layers) = tile_repository
                        .iter_tessellated_layers_at(&world_coords)
                        .map(|layers| {
                            layers
                                .filter(|result| !loaded_layers.contains(&result.layer_name()))
                                .collect::<Vec<_>>()
                        })
                    {
                        for style_layer in &style.layers {
                            let source_layer = style_layer.source_layer.as_ref().unwrap();

                            if let Some(message) = available_layers
                                .iter()
                                .find(|layer| source_layer.as_str() == layer.layer_name())
                            {
                                let color: Option<Vec4f32> = style_layer
                                    .paint
                                    .as_ref()
                                    .and_then(|paint| paint.get_color())
                                    .map(|color| color.into());

                                match message {
                                    StoredLayer::UnavailableLayer { coords: _, .. } => {
                                        /*self.buffer_pool.mark_layer_unavailable(*coords);*/
                                    }
                                    StoredLayer::TessellatedLayer {
                                        coords,
                                        feature_indices,
                                        layer_data,
                                        buffer,
                                        ..
                                    } => {
                                        let allocate_feature_metadata = tracing::span!(
                                            tracing::Level::TRACE,
                                            "allocate_feature_metadata"
                                        );

                                        let guard = allocate_feature_metadata.enter();
                                        let feature_metadata = layer_data
                                            .features
                                            .iter()
                                            .enumerate()
                                            .flat_map(|(i, _feature)| {
                                                iter::repeat(ShaderFeatureStyle {
                                                    color: color.unwrap(),
                                                })
                                                .take(feature_indices[i] as usize)
                                            })
                                            .collect::<Vec<_>>();
                                        drop(guard);

                                        tracing::trace!("Allocating geometry at {}", &coords);
                                        buffer_pool.allocate_layer_geometry(
                                            queue,
                                            *coords,
                                            style_layer.clone(),
                                            buffer,
                                            ShaderLayerMetadata::new(style_layer.index as f32),
                                            &feature_metadata,
                                        );
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }