use core::ffi::c_void;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Color {
pub r: f32,
pub g: f32,
pub b: f32,
pub a: f32,
}
impl Color {
pub const BLACK: Self = Self::rgba(0.0, 0.0, 0.0, 1.0);
pub const WHITE: Self = Self::rgba(1.0, 1.0, 1.0, 1.0);
pub const TRANSPARENT: Self = Self::rgba(0.0, 0.0, 0.0, 0.0);
#[must_use]
pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
Self { r, g, b, a }
}
#[must_use]
pub fn to_premultiplied(self) -> [f32; 4] {
let r = self.r.clamp(0.0, 1.0);
let g = self.g.clamp(0.0, 1.0);
let b = self.b.clamp(0.0, 1.0);
[r * self.a, g * self.a, b * self.a, self.a]
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Vec2 {
pub x: f32,
pub y: f32,
}
impl Vec2 {
#[must_use]
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
}
impl From<[f32; 2]> for Vec2 {
fn from([x, y]: [f32; 2]) -> Self {
Self { x, y }
}
}
impl From<Vec2> for [f32; 2] {
fn from(v: Vec2) -> Self {
[v.x, v.y]
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct LatLng {
pub latitude: f64,
pub longitude: f64,
}
impl LatLng {
#[must_use]
pub const fn new(latitude: f64, longitude: f64) -> Self {
Self { latitude, longitude }
}
#[must_use]
pub fn wrapped(self) -> Self {
let v = self.longitude;
let lon = if (-180.0..180.0).contains(&v) {
v
} else if v == 180.0 {
-180.0
} else {
let w = -180.0 + (v + 180.0) % 360.0;
if v < -180.0 { w + 360.0 } else { w }
};
Self { latitude: self.latitude, longitude: lon }
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LatLngBounds {
pub southwest: LatLng,
pub northeast: LatLng,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ScreenPoint {
pub x: f64,
pub y: f64,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct PointI16 {
pub x: i16,
pub y: i16,
}
impl PointI16 {
#[must_use]
pub const fn new(x: i16, y: i16) -> Self {
Self { x, y }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct CanonicalTileId {
pub z: u32,
pub x: u32,
pub y: u32,
}
impl CanonicalTileId {
#[must_use]
pub const fn new(z: u32, x: u32, y: u32) -> Self {
Self { z, x, y }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct TileId {
pub overscaled_z: u32,
pub wrap: i32,
pub canonical_z: u32,
pub canonical_x: u32,
pub canonical_y: u32,
}
impl TileId {
#[must_use]
pub const fn canonical(self) -> CanonicalTileId {
CanonicalTileId { z: self.canonical_z, x: self.canonical_x, y: self.canonical_y }
}
#[must_use]
pub const fn from_canonical(z: u32, x: u32, y: u32) -> Self {
Self { overscaled_z: z, wrap: 0, canonical_z: z, canonical_x: x, canonical_y: y }
}
#[must_use]
pub const fn with_wrap(wrap: i32, canonical: CanonicalTileId) -> Self {
Self {
overscaled_z: canonical.z,
wrap,
canonical_z: canonical.z,
canonical_x: canonical.x,
canonical_y: canonical.y,
}
}
#[must_use]
pub const fn unwrapped(wrap: i32, z: u32, x: u32, y: u32) -> Self {
Self { overscaled_z: z, wrap, canonical_z: z, canonical_x: x, canonical_y: y }
}
#[must_use]
pub fn pixels_to_tile_units(self, pixels: f32, zoom: f32) -> f32 {
pixels * (8192.0 / (512.0 * 2.0f32.powf(zoom - self.canonical_z as f32)))
}
}
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DrawRange {
pub first_index: u32,
pub index_count: u32,
pub base_vertex: i32,
pub first_instance: u32,
pub instance_count: u32,
}
impl DrawRange {
#[must_use]
pub const fn indexes(index_count: u32) -> Self {
Self { first_index: 0, index_count, base_vertex: 0, first_instance: 0, instance_count: 1 }
}
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ProjectionUbo {
pub matrix: [f32; 16],
pub fallback_matrix: [f32; 16],
pub tile_mercator_coords: [f32; 4],
pub clipping_plane: [f32; 4],
pub projection_transition: f32,
pub depth_offset: f32,
pub translate: [f32; 2],
pub anchor: [f32; 4],
pub anchor_angles: [f32; 4],
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LayerInput {
Frame,
ViewportTiles,
GeometryTiles,
RasterTiles,
RasterDemTiles,
SourceTiles,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct CollisionShape {
pub kind: CollisionShapeKind,
pub placed: bool,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum CollisionShapeKind {
Box { min: Vec2, max: Vec2 },
Circle { center: Vec2, radius: f32 },
}
impl Default for CollisionShapeKind {
fn default() -> Self {
Self::Box { min: Vec2::default(), max: Vec2::default() }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RenderBackend {
Metal,
Vulkan,
OpenGl,
WebGpu,
D3d12,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LendAt {
BeforePasses,
Pass,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GpuEnd {
Completed,
ContextLost,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MapMode {
Continuous,
Static,
Tile,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ProjectionVariant {
Mercator,
Globe,
MercatorStanding,
GlobeStanding,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Alignment {
Map,
Viewport,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FeatureType {
Unknown,
Point,
LineString,
Polygon,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct RenderPasses(pub u32);
impl RenderPasses {
pub const NONE: Self = Self(0);
pub const OPAQUE: Self = Self(1);
pub const TRANSLUCENT: Self = Self(2);
#[must_use]
pub const fn from_bits(bits: u32) -> Self {
Self(bits)
}
#[must_use]
pub const fn bits(self) -> u32 {
self.0
}
#[must_use]
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
#[must_use]
pub const fn contains(self, other: Self) -> bool {
(self.0 & other.0) == other.0
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct TileChanges(pub u32);
impl TileChanges {
pub const PAINT: u32 = 1;
pub const OPACITY: u32 = 2;
pub const DYNAMIC: u32 = 4;
pub const SORT: u32 = 8;
#[must_use]
pub const fn from_bits(bits: u32) -> Self {
Self(bits)
}
#[must_use]
pub const fn bits(self) -> u32 {
self.0
}
#[must_use]
pub const fn paint(self) -> bool {
(self.0 & Self::PAINT) != 0
}
#[must_use]
pub const fn opacity(self) -> bool {
(self.0 & Self::OPACITY) != 0
}
#[must_use]
pub const fn dynamic(self) -> bool {
(self.0 & Self::DYNAMIC) != 0
}
#[must_use]
pub const fn sort(self) -> bool {
(self.0 & Self::SORT) != 0
}
#[must_use]
pub const fn any_paint(self) -> bool {
self.paint() || self.dynamic()
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TileOrder {
#[default]
ById,
ByY,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TerrainMode {
#[default]
Flat,
Stands,
StandsHidingBehindGround,
Drapes,
}
#[derive(Clone, Copy, Debug)]
pub struct TileTransform {
pub translate: [f32; 2],
pub translate_anchor: Alignment,
pub near_clipped: bool,
pub pixel_aligned: bool,
}
impl Default for TileTransform {
fn default() -> Self {
Self { translate: [0.0, 0.0], translate_anchor: Alignment::Map, near_clipped: false, pixel_aligned: false }
}
}
#[derive(Clone, Copy, Debug)]
pub struct FrameInfo {
pub width: u32,
pub height: u32,
pub pixel_ratio: f32,
pub pixel_scale: f64,
pub pixels_to_gl_units: [f32; 2],
pub camera_moving: bool,
pub terrain: bool,
pub composites_with_frame_depth: bool,
pub drape_lit: bool,
pub has_ground: bool,
pub time_point_ns: Option<i64>,
}
#[derive(Clone, Copy, Debug)]
pub struct ViewInfo {
pub width: u32,
pub height: u32,
pub zoom: f64,
pub integer_zoom: u8,
pub angle_rad: f64,
pub pitch_rad: f64,
pub camera_to_center_distance: f64,
pub is_globe: bool,
pub world_wraps: bool,
}
#[derive(Clone, Copy, Debug)]
pub struct ProjectedPoint {
pub clip_x: f64,
pub clip_y: f64,
pub signed_distance_from_camera: f64,
pub occluded: bool,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ColorMode {
AlphaBlended,
Unblended,
Additive,
Disabled,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct CullFaceMode {
pub enabled: bool,
pub side: CullFaceSide,
pub winding: CullFaceWinding,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum CullFaceSide {
#[default]
Back,
Front,
FrontAndBack,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum CullFaceWinding {
#[default]
Ccw,
Cw,
}
impl CullFaceMode {
pub const DISABLED: Self = Self { enabled: false, side: CullFaceSide::Back, winding: CullFaceWinding::Ccw };
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum DrawMode {
Triangles,
TriangleStrip,
TriangleFan,
Lines(f32),
LineLoop(f32),
LineStrip(f32),
Points(f32),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DepthMode {
Layer { sub_layer: u8, write: bool },
Off,
Volume { write: bool },
}
impl Default for DepthMode {
fn default() -> Self {
Self::Layer { sub_layer: 0, write: false }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DrawOptions {
pub order: i64,
pub opaque: bool,
pub depth: DepthMode,
pub clip: bool,
}
impl Default for DrawOptions {
fn default() -> Self {
Self { order: 0, opaque: false, depth: DepthMode::Layer { sub_layer: 0, write: false }, clip: false }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TextureFormat {
Rgba8Unorm,
R8Unorm,
A8Unorm,
R16Float,
R32Float,
Rgba16Float,
Rgba32Float,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TextureChannelType {
UnsignedByte,
HalfFloat,
Float,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TextureFilter {
Nearest,
#[default]
Linear,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TextureWrap {
#[default]
Clamp,
Repeat,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct SamplerState {
pub filter: TextureFilter,
pub wrap_u: TextureWrap,
pub wrap_v: TextureWrap,
pub max_anisotropy: u8,
pub mipmapped: bool,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TileScheme {
#[default]
Xyz,
Tms,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct TileTimes {
pub modified_unix_ms: Option<i64>,
pub expires_unix_ms: Option<i64>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TileNecessity {
Required,
Optional,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RasterEncoding {
Mapbox,
Terrarium,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LendFrame {
pub center: LatLng,
pub zoom: f64,
pub bearing_deg: f64,
pub pitch_deg: f64,
pub projection_matrix: [[f64; 4]; 4],
pub clip_depth_zero_to_one: bool,
pub near_distance: f64,
pub far_distance: f64,
pub far_depth: f64,
pub globe: bool,
pub width: u32,
pub height: u32,
}
#[derive(Clone, Copy, Debug)]
pub struct VulkanContextDescriptor {
pub instance: *mut c_void,
pub physical_device: *mut c_void,
pub device: *mut c_void,
pub graphics_queue: *mut c_void,
pub graphics_queue_family_index: u32,
pub get_instance_proc_addr: *mut c_void,
pub get_device_proc_addr: *mut c_void,
}
#[derive(Clone, Copy, Debug)]
pub struct MetalEncoder {
pub command_buffer: *mut c_void,
}
#[derive(Clone, Copy, Debug)]
pub struct VulkanEncoder {
pub context: VulkanContextDescriptor,
pub command_buffer: *mut c_void,
}
#[derive(Clone, Copy, Debug)]
pub struct OpenGlEncoder {
pub web_context: u32,
}
#[derive(Clone, Copy, Debug)]
pub struct WebGpuEncoder {
pub device: u32,
pub encoder: u32,
}
#[derive(Clone, Copy, Debug)]
pub struct D3d12Encoder {
pub device: *mut c_void,
pub command_list: *mut c_void,
}
#[derive(Clone, Copy, Debug)]
pub struct MetalPass {
pub encoder: *mut c_void,
pub command_buffer: *mut c_void,
pub color_format: u64,
pub depth_format: u64,
pub stencil_format: u64,
pub sample_count: u32,
}
#[derive(Clone, Copy, Debug)]
pub struct VulkanPass {
pub context: VulkanContextDescriptor,
pub command_buffer: *mut c_void,
pub render_pass: u64,
pub subpass: u32,
pub sample_count: u32,
pub color_format: u32,
pub depth_stencil_format: u32,
}
#[derive(Clone, Copy, Debug)]
pub struct OpenGlPass {
pub framebuffer: u32,
pub web_context: u32,
pub web_framebuffer: u32,
}
#[derive(Clone, Copy, Debug)]
pub struct D3d12Pass {
pub device: *mut c_void,
pub command_list: *mut c_void,
pub render_target_view: u64,
pub color_format: u32,
pub depth_stencil_view: u64,
pub depth_stencil_format: u32,
}
#[derive(Clone, Copy, Debug)]
pub enum LendEncoder {
Metal(MetalEncoder),
Vulkan(VulkanEncoder),
OpenGl(OpenGlEncoder),
WebGpu(WebGpuEncoder),
D3d12(D3d12Encoder),
}
#[derive(Clone, Copy, Debug)]
pub enum LendPass<'a> {
Metal(MetalPass),
Vulkan(VulkanPass),
OpenGl(OpenGlPass),
WebGpu { device: u32, encoder: u32, color_format: &'a str, depth_stencil_format: &'a str, sample_count: u32 },
D3d12(D3d12Pass),
}
#[derive(Clone, Copy, Debug)]
pub enum LendStage<'a> {
BeforePasses { backend: RenderBackend, encoder: LendEncoder },
Pass { backend: RenderBackend, pass: LendPass<'a>, layer_depth: f64 },
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lat_lng_wrapped_normalizes_longitudes() {
let cases = [
(0.0, 0.0),
(-180.0, -180.0),
(180.0, -180.0),
(180.0f64.next_down(), 180.0f64.next_down()),
(-540.0, 180.0),
(540.0, -180.0),
(190.0, -170.0),
(-190.0, 170.0),
];
for (input, expected) in cases {
assert_eq!(LatLng::new(10.0, input).wrapped().longitude, expected, "input={input}");
}
}
#[test]
fn color_to_premultiplied_clamps_rgb_and_scales_by_alpha() {
assert_eq!(Color::rgba(0.5, 1.5, -0.2, 0.5).to_premultiplied(), [0.25, 0.5, 0.0, 0.5]);
assert_eq!(Color::TRANSPARENT.to_premultiplied(), [0.0, 0.0, 0.0, 0.0]);
}
}