1use core::ffi::c_void;
4
5#[derive(Clone, Copy, Debug, Default, PartialEq)]
7pub struct Color {
8 pub r: f32,
9 pub g: f32,
10 pub b: f32,
11 pub a: f32,
12}
13
14impl Color {
15 pub const BLACK: Self = Self::rgba(0.0, 0.0, 0.0, 1.0);
16 pub const WHITE: Self = Self::rgba(1.0, 1.0, 1.0, 1.0);
17 pub const TRANSPARENT: Self = Self::rgba(0.0, 0.0, 0.0, 0.0);
18
19 #[must_use]
20 pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
21 Self { r, g, b, a }
22 }
23
24 #[must_use]
27 pub fn to_premultiplied(self) -> [f32; 4] {
28 let r = self.r.clamp(0.0, 1.0);
29 let g = self.g.clamp(0.0, 1.0);
30 let b = self.b.clamp(0.0, 1.0);
31 [r * self.a, g * self.a, b * self.a, self.a]
32 }
33}
34
35#[derive(Clone, Copy, Debug, Default, PartialEq)]
37pub struct Vec2 {
38 pub x: f32,
39 pub y: f32,
40}
41
42impl Vec2 {
43 #[must_use]
44 pub const fn new(x: f32, y: f32) -> Self {
45 Self { x, y }
46 }
47}
48
49impl From<[f32; 2]> for Vec2 {
50 fn from([x, y]: [f32; 2]) -> Self {
51 Self { x, y }
52 }
53}
54
55impl From<Vec2> for [f32; 2] {
56 fn from(v: Vec2) -> Self {
57 [v.x, v.y]
58 }
59}
60
61#[derive(Clone, Copy, Debug, Default, PartialEq)]
63pub struct LatLng {
64 pub latitude: f64,
65 pub longitude: f64,
66}
67
68impl LatLng {
69 #[must_use]
70 pub const fn new(latitude: f64, longitude: f64) -> Self {
71 Self { latitude, longitude }
72 }
73
74 #[must_use]
76 pub fn wrapped(self) -> Self {
77 let v = self.longitude;
78 let lon = if (-180.0..180.0).contains(&v) {
79 v
80 } else if v == 180.0 {
81 -180.0
82 } else {
83 let w = -180.0 + (v + 180.0) % 360.0;
84 if v < -180.0 { w + 360.0 } else { w }
85 };
86 Self { latitude: self.latitude, longitude: lon }
87 }
88}
89
90#[derive(Clone, Copy, Debug, PartialEq)]
92pub struct LatLngBounds {
93 pub southwest: LatLng,
94 pub northeast: LatLng,
95}
96
97#[repr(C)]
99#[derive(Clone, Copy, Debug, Default, PartialEq)]
100pub struct ScreenPoint {
101 pub x: f64,
102 pub y: f64,
103}
104
105#[repr(C)]
107#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
108pub struct PointI16 {
109 pub x: i16,
110 pub y: i16,
111}
112
113impl PointI16 {
114 #[must_use]
115 pub const fn new(x: i16, y: i16) -> Self {
116 Self { x, y }
117 }
118}
119
120#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
122pub struct CanonicalTileId {
123 pub z: u32,
124 pub x: u32,
125 pub y: u32,
126}
127
128impl CanonicalTileId {
129 #[must_use]
130 pub const fn new(z: u32, x: u32, y: u32) -> Self {
131 Self { z, x, y }
132 }
133}
134
135#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
137pub struct TileId {
138 pub overscaled_z: u32,
139 pub wrap: i32,
140 pub canonical_z: u32,
141 pub canonical_x: u32,
142 pub canonical_y: u32,
143}
144
145impl TileId {
146 #[must_use]
147 pub const fn canonical(self) -> CanonicalTileId {
148 CanonicalTileId { z: self.canonical_z, x: self.canonical_x, y: self.canonical_y }
149 }
150
151 #[must_use]
152 pub const fn from_canonical(z: u32, x: u32, y: u32) -> Self {
153 Self { overscaled_z: z, wrap: 0, canonical_z: z, canonical_x: x, canonical_y: y }
154 }
155
156 #[must_use]
157 pub const fn with_wrap(wrap: i32, canonical: CanonicalTileId) -> Self {
158 Self {
159 overscaled_z: canonical.z,
160 wrap,
161 canonical_z: canonical.z,
162 canonical_x: canonical.x,
163 canonical_y: canonical.y,
164 }
165 }
166
167 #[must_use]
168 pub const fn unwrapped(wrap: i32, z: u32, x: u32, y: u32) -> Self {
169 Self { overscaled_z: z, wrap, canonical_z: z, canonical_x: x, canonical_y: y }
170 }
171
172 #[must_use]
174 pub fn pixels_to_tile_units(self, pixels: f32, zoom: f32) -> f32 {
175 pixels * (8192.0 / (512.0 * 2.0f32.powf(zoom - self.canonical_z as f32)))
176 }
177}
178
179#[repr(C)]
181#[derive(Clone, Copy, Debug, PartialEq, Eq)]
182pub struct DrawRange {
183 pub first_index: u32,
184 pub index_count: u32,
185 pub base_vertex: i32,
186 pub first_instance: u32,
187 pub instance_count: u32,
188}
189
190impl DrawRange {
191 #[must_use]
192 pub const fn indexes(index_count: u32) -> Self {
193 Self { first_index: 0, index_count, base_vertex: 0, first_instance: 0, instance_count: 1 }
194 }
195}
196
197#[repr(C)]
199#[derive(Clone, Copy, Debug, Default, PartialEq)]
200pub struct ProjectionUbo {
201 pub matrix: [f32; 16],
202 pub fallback_matrix: [f32; 16],
203 pub tile_mercator_coords: [f32; 4],
204 pub clipping_plane: [f32; 4],
205 pub projection_transition: f32,
206 pub depth_offset: f32,
207 pub translate: [f32; 2],
208 pub anchor: [f32; 4],
209 pub anchor_angles: [f32; 4],
210}
211
212#[derive(Clone, Copy, Debug, PartialEq, Eq)]
214pub enum LayerInput {
215 Frame,
217 ViewportTiles,
219 GeometryTiles,
221 RasterTiles,
223 RasterDemTiles,
225 SourceTiles,
228}
229
230#[derive(Clone, Copy, Debug, Default, PartialEq)]
233pub struct CollisionShape {
234 pub kind: CollisionShapeKind,
235 pub placed: bool,
238}
239
240#[derive(Clone, Copy, Debug, PartialEq)]
243pub enum CollisionShapeKind {
244 Box { min: Vec2, max: Vec2 },
246 Circle { center: Vec2, radius: f32 },
248}
249
250impl Default for CollisionShapeKind {
251 fn default() -> Self {
252 Self::Box { min: Vec2::default(), max: Vec2::default() }
253 }
254}
255
256#[derive(Clone, Copy, Debug, PartialEq, Eq)]
258pub enum RenderBackend {
259 Metal,
260 Vulkan,
261 OpenGl,
262 WebGpu,
263 D3d12,
264}
265
266#[derive(Clone, Copy, Debug, PartialEq, Eq)]
268pub enum LendAt {
269 BeforePasses,
271 Pass,
273}
274
275#[derive(Clone, Copy, Debug, PartialEq, Eq)]
277pub enum GpuEnd {
278 Completed,
280 ContextLost,
282}
283
284#[derive(Clone, Copy, Debug, PartialEq, Eq)]
286pub enum MapMode {
287 Continuous,
288 Static,
289 Tile,
290}
291
292#[derive(Clone, Copy, Debug, PartialEq, Eq)]
294pub enum ProjectionVariant {
295 Mercator,
296 Globe,
297 MercatorStanding,
298 GlobeStanding,
299}
300
301#[derive(Clone, Copy, Debug, PartialEq, Eq)]
303pub enum Alignment {
304 Map,
305 Viewport,
306}
307
308#[derive(Clone, Copy, Debug, PartialEq, Eq)]
310pub enum FeatureType {
311 Unknown,
312 Point,
313 LineString,
314 Polygon,
315}
316
317#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
319pub struct RenderPasses(pub u32);
320
321impl RenderPasses {
322 pub const NONE: Self = Self(0);
323 pub const OPAQUE: Self = Self(1);
324 pub const TRANSLUCENT: Self = Self(2);
325
326 #[must_use]
327 pub const fn from_bits(bits: u32) -> Self {
328 Self(bits)
329 }
330
331 #[must_use]
332 pub const fn bits(self) -> u32 {
333 self.0
334 }
335
336 #[must_use]
337 pub const fn union(self, other: Self) -> Self {
338 Self(self.0 | other.0)
339 }
340
341 #[must_use]
342 pub const fn contains(self, other: Self) -> bool {
343 (self.0 & other.0) == other.0
344 }
345}
346
347#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
349pub struct TileChanges(pub u32);
350
351impl TileChanges {
352 pub const PAINT: u32 = 1;
353 pub const OPACITY: u32 = 2;
354 pub const DYNAMIC: u32 = 4;
355 pub const SORT: u32 = 8;
356
357 #[must_use]
358 pub const fn from_bits(bits: u32) -> Self {
359 Self(bits)
360 }
361
362 #[must_use]
363 pub const fn bits(self) -> u32 {
364 self.0
365 }
366
367 #[must_use]
368 pub const fn paint(self) -> bool {
369 (self.0 & Self::PAINT) != 0
370 }
371
372 #[must_use]
373 pub const fn opacity(self) -> bool {
374 (self.0 & Self::OPACITY) != 0
375 }
376
377 #[must_use]
378 pub const fn dynamic(self) -> bool {
379 (self.0 & Self::DYNAMIC) != 0
380 }
381
382 #[must_use]
383 pub const fn sort(self) -> bool {
384 (self.0 & Self::SORT) != 0
385 }
386
387 #[must_use]
388 pub const fn any_paint(self) -> bool {
389 self.paint() || self.dynamic()
390 }
391}
392
393#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
395pub enum TileOrder {
396 #[default]
397 ById,
398 ByY,
399}
400
401#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
403pub enum TerrainMode {
404 #[default]
405 Flat,
406 Stands,
407 StandsHidingBehindGround,
408 Drapes,
409}
410
411#[derive(Clone, Copy, Debug)]
413pub struct TileTransform {
414 pub translate: [f32; 2],
415 pub translate_anchor: Alignment,
416 pub near_clipped: bool,
417 pub pixel_aligned: bool,
418}
419
420impl Default for TileTransform {
421 fn default() -> Self {
422 Self { translate: [0.0, 0.0], translate_anchor: Alignment::Map, near_clipped: false, pixel_aligned: false }
423 }
424}
425
426#[derive(Clone, Copy, Debug)]
428pub struct FrameInfo {
429 pub width: u32,
430 pub height: u32,
431 pub pixel_ratio: f32,
432 pub pixel_scale: f64,
433 pub pixels_to_gl_units: [f32; 2],
434 pub camera_moving: bool,
435 pub terrain: bool,
436 pub composites_with_frame_depth: bool,
437 pub drape_lit: bool,
438 pub has_ground: bool,
439 pub time_point_ns: Option<i64>,
440}
441
442#[derive(Clone, Copy, Debug)]
444pub struct ViewInfo {
445 pub width: u32,
446 pub height: u32,
447 pub zoom: f64,
448 pub integer_zoom: u8,
449 pub angle_rad: f64,
450 pub pitch_rad: f64,
451 pub camera_to_center_distance: f64,
452 pub is_globe: bool,
453 pub world_wraps: bool,
454}
455
456#[derive(Clone, Copy, Debug)]
458pub struct ProjectedPoint {
459 pub clip_x: f64,
460 pub clip_y: f64,
461 pub signed_distance_from_camera: f64,
462 pub occluded: bool,
463}
464
465#[derive(Clone, Copy, Debug, PartialEq)]
467pub enum ColorMode {
468 AlphaBlended,
469 Unblended,
470 Additive,
471 Disabled,
472}
473
474#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
476pub struct CullFaceMode {
477 pub enabled: bool,
478 pub side: CullFaceSide,
479 pub winding: CullFaceWinding,
480}
481
482#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
483pub enum CullFaceSide {
484 #[default]
485 Back,
486 Front,
487 FrontAndBack,
488}
489
490#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
491pub enum CullFaceWinding {
492 #[default]
493 Ccw,
494 Cw,
495}
496
497impl CullFaceMode {
498 pub const DISABLED: Self = Self { enabled: false, side: CullFaceSide::Back, winding: CullFaceWinding::Ccw };
499}
500
501#[derive(Clone, Copy, Debug, PartialEq)]
503pub enum DrawMode {
504 Triangles,
505 TriangleStrip,
506 TriangleFan,
507 Lines(f32),
508 LineLoop(f32),
509 LineStrip(f32),
510 Points(f32),
511}
512
513#[derive(Clone, Copy, Debug, PartialEq, Eq)]
515pub enum DepthMode {
516 Layer { sub_layer: u8, write: bool },
517 Off,
518 Volume { write: bool },
519}
520
521impl Default for DepthMode {
522 fn default() -> Self {
523 Self::Layer { sub_layer: 0, write: false }
524 }
525}
526
527#[derive(Clone, Copy, Debug, PartialEq, Eq)]
529pub struct DrawOptions {
530 pub order: i64,
531 pub opaque: bool,
532 pub depth: DepthMode,
533 pub clip: bool,
534}
535
536impl Default for DrawOptions {
537 fn default() -> Self {
538 Self { order: 0, opaque: false, depth: DepthMode::Layer { sub_layer: 0, write: false }, clip: false }
539 }
540}
541
542#[derive(Clone, Copy, Debug, PartialEq, Eq)]
544pub enum TextureFormat {
545 Rgba8Unorm,
546 R8Unorm,
547 A8Unorm,
548 R16Float,
549 R32Float,
550 Rgba16Float,
551 Rgba32Float,
552}
553
554#[derive(Clone, Copy, Debug, PartialEq, Eq)]
556pub enum TextureChannelType {
557 UnsignedByte,
558 HalfFloat,
559 Float,
560}
561
562#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
564pub enum TextureFilter {
565 Nearest,
566 #[default]
567 Linear,
568}
569
570#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
572pub enum TextureWrap {
573 #[default]
574 Clamp,
575 Repeat,
576}
577
578#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
580pub struct SamplerState {
581 pub filter: TextureFilter,
582 pub wrap_u: TextureWrap,
583 pub wrap_v: TextureWrap,
584 pub max_anisotropy: u8,
585 pub mipmapped: bool,
586}
587
588#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
590pub enum TileScheme {
591 #[default]
592 Xyz,
593 Tms,
594}
595
596#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
598pub struct TileTimes {
599 pub modified_unix_ms: Option<i64>,
600 pub expires_unix_ms: Option<i64>,
601}
602
603#[derive(Clone, Copy, Debug, PartialEq, Eq)]
605pub enum TileNecessity {
606 Required,
607 Optional,
608}
609
610#[derive(Clone, Copy, Debug, PartialEq, Eq)]
612pub enum RasterEncoding {
613 Mapbox,
614 Terrarium,
615}
616
617#[derive(Clone, Copy, Debug, PartialEq)]
623pub struct LendFrame {
624 pub center: LatLng,
625 pub zoom: f64,
626 pub bearing_deg: f64,
627 pub pitch_deg: f64,
628 pub projection_matrix: [[f64; 4]; 4],
629 pub clip_depth_zero_to_one: bool,
630 pub near_distance: f64,
631 pub far_distance: f64,
632 pub far_depth: f64,
633 pub globe: bool,
634 pub width: u32,
635 pub height: u32,
636}
637
638#[derive(Clone, Copy, Debug)]
640pub struct VulkanContextDescriptor {
641 pub instance: *mut c_void,
642 pub physical_device: *mut c_void,
643 pub device: *mut c_void,
644 pub graphics_queue: *mut c_void,
645 pub graphics_queue_family_index: u32,
646 pub get_instance_proc_addr: *mut c_void,
647 pub get_device_proc_addr: *mut c_void,
648}
649
650#[derive(Clone, Copy, Debug)]
652pub struct MetalEncoder {
653 pub command_buffer: *mut c_void,
654}
655
656#[derive(Clone, Copy, Debug)]
658pub struct VulkanEncoder {
659 pub context: VulkanContextDescriptor,
660 pub command_buffer: *mut c_void,
661}
662
663#[derive(Clone, Copy, Debug)]
665pub struct OpenGlEncoder {
666 pub web_context: u32,
668}
669
670#[derive(Clone, Copy, Debug)]
672pub struct WebGpuEncoder {
673 pub device: u32,
674 pub encoder: u32,
675}
676
677#[derive(Clone, Copy, Debug)]
679pub struct D3d12Encoder {
680 pub device: *mut c_void,
681 pub command_list: *mut c_void,
682}
683
684#[derive(Clone, Copy, Debug)]
686pub struct MetalPass {
687 pub encoder: *mut c_void,
688 pub command_buffer: *mut c_void,
689 pub color_format: u64,
690 pub depth_format: u64,
691 pub stencil_format: u64,
692 pub sample_count: u32,
693}
694
695#[derive(Clone, Copy, Debug)]
697pub struct VulkanPass {
698 pub context: VulkanContextDescriptor,
699 pub command_buffer: *mut c_void,
700 pub render_pass: u64,
701 pub subpass: u32,
702 pub sample_count: u32,
703 pub color_format: u32,
704 pub depth_stencil_format: u32,
705}
706
707#[derive(Clone, Copy, Debug)]
709pub struct OpenGlPass {
710 pub framebuffer: u32,
712 pub web_context: u32,
714 pub web_framebuffer: u32,
716}
717
718#[derive(Clone, Copy, Debug)]
720pub struct D3d12Pass {
721 pub device: *mut c_void,
722 pub command_list: *mut c_void,
723 pub render_target_view: u64,
724 pub color_format: u32,
725 pub depth_stencil_view: u64,
726 pub depth_stencil_format: u32,
727}
728
729#[derive(Clone, Copy, Debug)]
731pub enum LendEncoder {
732 Metal(MetalEncoder),
733 Vulkan(VulkanEncoder),
734 OpenGl(OpenGlEncoder),
735 WebGpu(WebGpuEncoder),
736 D3d12(D3d12Encoder),
737}
738
739#[derive(Clone, Copy, Debug)]
741pub enum LendPass<'a> {
742 Metal(MetalPass),
743 Vulkan(VulkanPass),
744 OpenGl(OpenGlPass),
745 WebGpu { device: u32, encoder: u32, color_format: &'a str, depth_stencil_format: &'a str, sample_count: u32 },
746 D3d12(D3d12Pass),
747}
748
749#[derive(Clone, Copy, Debug)]
751pub enum LendStage<'a> {
752 BeforePasses { backend: RenderBackend, encoder: LendEncoder },
753 Pass { backend: RenderBackend, pass: LendPass<'a>, layer_depth: f64 },
754}
755
756#[cfg(test)]
757mod tests {
758 use super::*;
759
760 #[test]
761 fn lat_lng_wrapped_normalizes_longitudes() {
762 let cases = [
763 (0.0, 0.0),
764 (-180.0, -180.0),
765 (180.0, -180.0),
766 (180.0f64.next_down(), 180.0f64.next_down()),
767 (-540.0, 180.0),
768 (540.0, -180.0),
769 (190.0, -170.0),
770 (-190.0, 170.0),
771 ];
772 for (input, expected) in cases {
773 assert_eq!(LatLng::new(10.0, input).wrapped().longitude, expected, "input={input}");
774 }
775 }
776
777 #[test]
778 fn color_to_premultiplied_clamps_rgb_and_scales_by_alpha() {
779 assert_eq!(Color::rgba(0.5, 1.5, -0.2, 0.5).to_premultiplied(), [0.25, 0.5, 0.0, 0.5]);
780 assert_eq!(Color::TRANSPARENT.to_premultiplied(), [0.0, 0.0, 0.0, 0.0]);
781 }
782}