# globals.rs - source
Source of the Rust file `maplibre/src/render/resource/globals.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

    //! A bind group which binds a buffer with global data like the current camera transformations.

    use crate::platform::MIN_BUFFER_SIZE;
    use crate::render::shaders::ShaderGlobals;
    use std::cmp;
    use std::mem::size_of;

    pub struct Globals {
        pub uniform_buffer: wgpu::Buffer,
        pub bind_group: wgpu::BindGroup,
    }

    impl Globals {
        pub fn from_device(device: &wgpu::Device, group: &wgpu::BindGroupLayout) -> Self {
            let globals_buffer_byte_size = cmp::max(MIN_BUFFER_SIZE, size_of::<ShaderGlobals>() as u64);

            let uniform_buffer = device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("Globals ubo"),
                size: globals_buffer_byte_size,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });

            let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("Bind group"),
                layout: group,
                entries: &[wgpu::BindGroupEntry {
                    binding: 0,
                    resource: wgpu::BindingResource::Buffer(uniform_buffer.as_entire_buffer_binding()),
                }],
            });
            Self {
                uniform_buffer,
                bind_group,
            }
        }
    }