use core::any::Any;
use core::marker::PhantomData;
use std::borrow::Cow;
use std::sync::Arc;
use crate::backend::{
AfterGpuHandle, AfterGpuRefBackend, DrawGroupBackend, DrawsBackend, EvaluateCxBackend, EvaluatedLayerBackend,
FrameBackend, FrameQueryCxMutBackend, FrameQueryCxReadBackend, GroundHeightsHandle, HitCxBackend, HostBackend,
IndexBufferHandle, MeshHandle, PaintAttributesHandle, PrepareCxMutBackend, PrepareCxReadBackend, ResourcesBackend,
TargetHandle, TextureHandle, TileBackend, TileBuildCxBackend, TileGeometryBackend, TileGeometryBuilderBackend,
UpdateCxMutBackend, UpdateCxReadBackend, VertexBufferHandle, ViewBackend,
};
use crate::error::Result;
use crate::program::{AttributeFormat, Program, ProgramDesc};
use crate::types::{
Alignment, CollisionShape, Color, ColorMode, CullFaceMode, DrawMode, DrawOptions, DrawRange, FeatureType,
FrameInfo, GpuEnd, LatLng, LayerInput, LendAt, LendFrame, LendStage, MapMode, PointI16, ProjectedPoint,
ProjectionUbo, ProjectionVariant, RenderPasses, SamplerState, ScreenPoint, TerrainMode, TextureChannelType,
TextureFormat, TileChanges, TileId, TileOrder, TileTransform, Vec2, ViewInfo,
};
pub struct Host<'a> {
backend: &'a dyn HostBackend,
}
impl<'a> Host<'a> {
#[doc(hidden)]
#[must_use]
pub fn from_backend(backend: &'a dyn HostBackend) -> Self {
Self { backend }
}
pub fn create_program(&self, desc: &ProgramDesc<'_>) -> Result<Program> {
self.backend.create_program(desc)
}
pub fn create_ground_heights(&self) -> Result<GroundHeights> {
self.backend.create_ground_heights()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PropertyScope {
Paint,
Layout,
}
#[derive(Clone, Copy, Debug)]
pub enum PropertyDefault<'a> {
Float(f32),
Bool(bool),
Color(Color),
Vec2(Vec2),
LatLng(LatLng),
RotationDegrees(f32),
String(&'a str),
Enum { variants: &'a [&'a str], default_index: u32 },
}
#[derive(Clone, Copy, Debug)]
pub struct PropertyDesc<'a> {
pub name: &'a str,
pub scope: PropertyScope,
pub data_driven: bool,
pub paint_lays_out: bool,
pub attribute_name: &'a str,
pub default: PropertyDefault<'a>,
}
impl<'a> PropertyDesc<'a> {
#[must_use]
pub const fn paint(name: &'a str, default: PropertyDefault<'a>) -> Self {
Self {
name,
scope: PropertyScope::Paint,
data_driven: false,
paint_lays_out: false,
attribute_name: "",
default,
}
}
#[must_use]
pub const fn paint_data_driven(name: &'a str, attribute_name: &'a str, default: PropertyDefault<'a>) -> Self {
Self { name, scope: PropertyScope::Paint, data_driven: true, paint_lays_out: false, attribute_name, default }
}
#[must_use]
pub const fn layout(name: &'a str, default: PropertyDefault<'a>) -> Self {
Self {
name,
scope: PropertyScope::Layout,
data_driven: false,
paint_lays_out: false,
attribute_name: "",
default,
}
}
}
#[derive(Clone, Debug)]
pub struct LayerDesc<'a> {
pub name: &'a str,
pub input: LayerInput,
pub tile_order: TileOrder,
pub terrain_mode: TerrainMode,
pub is_volume: bool,
pub properties: &'a [PropertyDesc<'a>],
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Evaluation {
pub passes: RenderPasses,
pub solid_color: Option<Color>,
pub drapes_by_bearing: bool,
pub drapes_by_zoom: bool,
pub needs_repaint: bool,
pub repaint_after_ns: Option<u64>,
pub reads_collision_shapes: bool,
}
impl Evaluation {
#[must_use]
pub const fn passes(passes: RenderPasses) -> Self {
Self {
passes,
solid_color: None,
drapes_by_bearing: false,
drapes_by_zoom: false,
needs_repaint: false,
repaint_after_ns: None,
reads_collision_shapes: false,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum Hit {
#[default]
Miss,
InPlace,
AtDistance(f64),
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum LayerBlend {
#[default]
Normal,
Multiply,
Screen,
Overlay,
Darken,
Lighten,
ColorDodge,
ColorBurn,
HardLight,
SoftLight,
Difference,
Exclusion,
Hue,
Saturation,
Color,
Luminosity,
PlusLighter,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Composite {
pub opacity: f32,
pub blend: LayerBlend,
pub frame_depth: bool,
}
#[derive(Debug)]
pub struct VertexBuffer {
inner: Box<dyn VertexBufferHandle>,
}
impl VertexBuffer {
#[doc(hidden)]
#[must_use]
pub fn from_handle(inner: Box<dyn VertexBufferHandle>) -> Self {
Self { inner }
}
#[doc(hidden)]
#[must_use]
pub fn handle(&self) -> &dyn VertexBufferHandle {
&*self.inner
}
}
#[derive(Debug)]
pub struct IndexBuffer {
inner: Box<dyn IndexBufferHandle>,
}
impl IndexBuffer {
#[doc(hidden)]
#[must_use]
pub fn from_handle(inner: Box<dyn IndexBufferHandle>) -> Self {
Self { inner }
}
#[doc(hidden)]
#[must_use]
pub fn handle(&self) -> &dyn IndexBufferHandle {
&*self.inner
}
}
#[derive(Debug)]
pub struct Texture {
inner: Box<dyn TextureHandle>,
}
impl Texture {
#[doc(hidden)]
#[must_use]
pub fn from_handle(inner: Box<dyn TextureHandle>) -> Self {
Self { inner }
}
#[doc(hidden)]
#[must_use]
pub fn handle(&self) -> &dyn TextureHandle {
&*self.inner
}
}
#[derive(Debug)]
pub struct Target {
inner: Box<dyn TargetHandle>,
}
impl Target {
#[doc(hidden)]
#[must_use]
pub fn from_handle(inner: Box<dyn TargetHandle>) -> Self {
Self { inner }
}
#[doc(hidden)]
#[must_use]
pub fn handle(&self) -> &dyn TargetHandle {
&*self.inner
}
#[must_use]
pub fn texture(&self) -> Option<Texture> {
self.inner.texture()
}
}
#[derive(Debug)]
pub struct PaintAttributes {
inner: Box<dyn PaintAttributesHandle>,
}
impl PaintAttributes {
#[doc(hidden)]
#[must_use]
pub fn from_handle(inner: Box<dyn PaintAttributesHandle>) -> Self {
Self { inner }
}
#[doc(hidden)]
#[must_use]
pub fn handle(&self) -> &dyn PaintAttributesHandle {
&*self.inner
}
}
#[derive(Debug)]
pub struct Mesh {
inner: Box<dyn MeshHandle>,
}
impl Mesh {
#[doc(hidden)]
#[must_use]
pub fn from_handle(inner: Box<dyn MeshHandle>) -> Self {
Self { inner }
}
#[doc(hidden)]
#[must_use]
pub fn handle(&self) -> &dyn MeshHandle {
&*self.inner
}
#[must_use]
pub fn seq(&self) -> i64 {
self.inner.seq()
}
pub fn set_texture(&self, res: &mut Resources<'_>, slot: u32, texture: Option<&Texture>) -> Result<()> {
res.backend.mesh_set_texture(self, slot, texture)
}
pub fn set_sampler(&self, res: &mut Resources<'_>, slot: u32, sampler: SamplerState) -> Result<()> {
res.backend.mesh_set_sampler(self, slot, sampler)
}
pub fn set_paint(&self, res: &mut Resources<'_>, paint: &PaintAttributes) -> Result<()> {
res.backend.mesh_set_paint(self, paint)
}
pub fn bind_vertices(&self, res: &mut Resources<'_>, binding: &VertexBinding<'_>) -> Result<()> {
res.backend.mesh_bind_vertices(self, binding)
}
}
#[derive(Debug)]
pub struct GroundHeights {
inner: Box<dyn GroundHeightsHandle>,
}
impl GroundHeights {
#[doc(hidden)]
#[must_use]
pub fn from_handle(inner: Box<dyn GroundHeightsHandle>) -> Self {
Self { inner }
}
#[doc(hidden)]
#[must_use]
pub fn handle_mut(&mut self) -> &mut dyn GroundHeightsHandle {
&mut *self.inner
}
pub fn apply(
&mut self,
mesh: &Mesh,
res: &mut Resources<'_>,
frame: Frame<'_>,
tile_id: TileId,
attribute_name: &str,
centers: &[PointI16],
) -> Result<()> {
res.backend.apply_ground_heights(self, mesh, frame, tile_id, attribute_name, centers)
}
}
#[derive(Clone, Copy, Debug)]
pub struct VertexAttribute<'a> {
pub name: &'a str,
pub format: AttributeFormat,
pub offset: u32,
}
impl<'a> VertexAttribute<'a> {
#[must_use]
pub const fn new(name: &'a str, format: AttributeFormat, offset: u32) -> Self {
Self { name, format, offset }
}
}
#[derive(Clone, Copy, Debug)]
pub struct VertexBinding<'a> {
pub buffer: &'a VertexBuffer,
pub stride: u32,
pub first_vertex: u32,
pub attributes: &'a [VertexAttribute<'a>],
}
pub struct MeshDesc<'a> {
pub program: &'a Program,
pub projection_variant: ProjectionVariant,
pub extra_defines: &'a [&'a str],
pub paint_attributes: Option<&'a PaintAttributes>,
pub color_mode: ColorMode,
pub cull_face_mode: CullFaceMode,
pub draw_mode: DrawMode,
pub vertex_bindings: &'a [VertexBinding<'a>],
pub vertex_count: usize,
pub instance_bindings: &'a [VertexBinding<'a>],
pub indexes: &'a IndexBuffer,
pub ranges: &'a [DrawRange],
pub textures: &'a [Option<&'a Texture>],
}
#[derive(Clone, Copy)]
pub struct EvaluatedLayer<'a> {
backend: &'a dyn EvaluatedLayerBackend,
}
impl<'a> EvaluatedLayer<'a> {
#[doc(hidden)]
#[must_use]
pub fn from_backend(backend: &'a dyn EvaluatedLayerBackend) -> Self {
Self { backend }
}
#[doc(hidden)]
#[must_use]
pub fn backend(&self) -> &'a dyn EvaluatedLayerBackend {
self.backend
}
#[must_use]
pub fn id(&self) -> &'a str {
self.backend.id()
}
#[must_use]
pub fn paint_is_constant(&self, slot: u32) -> bool {
self.backend.paint_is_constant(slot)
}
pub fn paint_f32(&self, slot: u32) -> Result<f32> {
self.backend.paint_f32(slot)
}
pub fn paint_f32_constant_or(&self, slot: u32, fallback: f32) -> Result<f32> {
self.backend.paint_f32_constant_or(slot, fallback)
}
pub fn paint_bool(&self, slot: u32) -> Result<bool> {
self.backend.paint_bool(slot)
}
pub fn paint_color(&self, slot: u32) -> Result<Color> {
self.backend.paint_color(slot)
}
pub fn paint_color_constant_or(&self, slot: u32, fallback: Color) -> Result<Color> {
self.backend.paint_color_constant_or(slot, fallback)
}
pub fn paint_vec2(&self, slot: u32) -> Result<Vec2> {
self.backend.paint_vec2(slot)
}
pub fn paint_lat_lng(&self, slot: u32) -> Result<LatLng> {
self.backend.paint_lat_lng(slot)
}
pub fn paint_rotation_degrees(&self, slot: u32) -> Result<f32> {
self.backend.paint_rotation_degrees(slot)
}
pub fn paint_str(&self, slot: u32) -> Result<&'a str> {
self.backend.paint_str(slot)
}
pub fn paint_enum(&self, slot: u32) -> Result<u32> {
self.backend.paint_enum(slot)
}
}
#[derive(Clone, Copy)]
pub struct EvaluateCx<'a> {
backend: &'a dyn EvaluateCxBackend,
}
impl<'a> EvaluateCx<'a> {
#[doc(hidden)]
#[must_use]
pub fn from_backend(backend: &'a dyn EvaluateCxBackend) -> Self {
Self { backend }
}
#[must_use]
pub fn evaluated(&self) -> EvaluatedLayer<'a> {
self.backend.evaluated()
}
#[must_use]
pub fn time_point_ns(&self) -> Option<i64> {
self.backend.time_point_ns()
}
}
pub struct UpdateCx<'a> {
ro: &'a dyn UpdateCxReadBackend,
rw: &'a mut dyn UpdateCxMutBackend,
}
impl<'a> UpdateCx<'a> {
#[doc(hidden)]
#[must_use]
pub fn from_backends(ro: &'a dyn UpdateCxReadBackend, rw: &'a mut dyn UpdateCxMutBackend) -> Self {
Self { ro, rw }
}
#[must_use]
pub fn evaluated(&self) -> EvaluatedLayer<'a> {
self.ro.evaluated()
}
#[must_use]
pub fn source_max_zoom(&self) -> Option<u8> {
self.ro.source_max_zoom()
}
#[must_use]
pub fn tile_count(&self) -> usize {
self.ro.tile_count()
}
#[must_use]
pub fn tile(&self, index: usize) -> Option<Tile<'a>> {
self.ro.tile(index)
}
pub fn pick_tiles(&mut self, tiles: &[TileId]) -> Result<()> {
self.rw.pick_tiles(tiles)
}
}
pub struct TileCx<'a> {
evaluated: EvaluatedLayer<'a>,
projection_variant: ProjectionVariant,
frame: Frame<'a>,
tile: Tile<'a>,
resources: &'a mut dyn ResourcesBackend,
}
impl<'a> TileCx<'a> {
#[doc(hidden)]
#[must_use]
pub fn new(
evaluated: EvaluatedLayer<'a>,
projection_variant: ProjectionVariant,
frame: Frame<'a>,
tile: Tile<'a>,
resources: &'a mut dyn ResourcesBackend,
) -> Self {
Self { evaluated, projection_variant, frame, tile, resources }
}
#[must_use]
pub fn evaluated(&self) -> EvaluatedLayer<'a> {
self.evaluated
}
#[must_use]
pub fn projection_variant(&self) -> ProjectionVariant {
self.projection_variant
}
#[must_use]
pub fn frame(&self) -> Frame<'a> {
self.frame
}
#[must_use]
pub fn tile(&self) -> Tile<'a> {
self.tile
}
#[must_use]
pub fn resources(&mut self) -> Resources<'_> {
Resources::from_backend(self.resources)
}
}
pub struct PrepareCx<'a, T: 'static> {
evaluated: EvaluatedLayer<'a>,
frame: Frame<'a>,
projection_variant: ProjectionVariant,
ro: &'a dyn PrepareCxReadBackend,
rw: &'a mut dyn PrepareCxMutBackend,
_marker: PhantomData<*mut T>,
}
impl<'a, T: 'static> PrepareCx<'a, T> {
#[doc(hidden)]
#[must_use]
pub fn new(
evaluated: EvaluatedLayer<'a>,
frame: Frame<'a>,
projection_variant: ProjectionVariant,
ro: &'a dyn PrepareCxReadBackend,
rw: &'a mut dyn PrepareCxMutBackend,
) -> Self {
Self { evaluated, frame, projection_variant, ro, rw, _marker: PhantomData }
}
#[must_use]
pub fn evaluated(&self) -> EvaluatedLayer<'a> {
self.evaluated
}
#[must_use]
pub fn frame(&self) -> Frame<'a> {
self.frame
}
#[must_use]
pub fn projection_variant(&self) -> ProjectionVariant {
self.projection_variant
}
#[must_use]
pub fn tile_count(&self) -> usize {
self.ro.tile_count()
}
#[must_use]
pub fn tile(&self, index: usize) -> Option<Tile<'a>> {
self.ro.tile(index)
}
pub fn tiles(&self) -> impl Iterator<Item = Tile<'a>> + '_ {
(0..self.tile_count()).filter_map(|i| self.tile(i))
}
#[must_use]
pub fn resources(&mut self) -> Resources<'_> {
self.rw.resources()
}
#[must_use]
pub fn tile_resources_and_allocator(&mut self, tile_id: TileId) -> (Option<&mut T>, Resources<'_>) {
let (slot, res) = self.rw.tile_resources_and_allocator(tile_id);
(slot.and_then(<dyn Any>::downcast_mut::<T>), res)
}
#[must_use]
pub fn tile_resources_mut(&mut self, tile_id: TileId) -> Option<&mut T> {
self.rw.tile_resources_mut(tile_id).and_then(<dyn Any>::downcast_mut::<T>)
}
#[must_use]
pub fn take_tile_changes(&mut self, tile_id: TileId) -> TileChanges {
self.rw.take_tile_changes(tile_id)
}
#[must_use]
pub fn picked_tiles(&self) -> Vec<TileId> {
self.ro.picked_tiles()
}
#[must_use]
pub fn read_shared_target(&mut self, name: &str) -> Option<Texture> {
self.rw.read_shared_target(name)
}
}
#[derive(Clone, Copy, Debug)]
pub struct RasterTileInfo<'a> {
pub image_id: u64,
pub width: u32,
pub height: u32,
pub pixels: &'a [u8],
}
#[derive(Clone, Copy)]
pub struct Tile<'a> {
backend: &'a dyn TileBackend,
}
impl<'a> Tile<'a> {
#[doc(hidden)]
#[must_use]
pub fn from_backend(backend: &'a dyn TileBackend) -> Self {
Self { backend }
}
#[must_use]
pub fn id(&self) -> TileId {
self.backend.id()
}
#[must_use]
pub fn draw_id(&self) -> TileId {
self.backend.draw_id()
}
#[must_use]
pub fn render_passes(&self) -> RenderPasses {
self.backend.render_passes()
}
#[must_use]
pub fn has_data_for_pass(&self, pass: RenderPasses) -> bool {
self.backend.has_data_for_pass(pass)
}
#[must_use]
pub fn geometry(&self) -> Option<TileGeometry<'a>> {
self.backend.geometry()
}
#[must_use]
pub fn geometry_vertices(&self, stream_index: usize) -> Option<VertexBuffer> {
self.backend.geometry_vertices(stream_index)
}
#[must_use]
pub fn geometry_indexes(&self, stream_index: usize) -> Option<IndexBuffer> {
self.backend.geometry_indexes(stream_index)
}
#[must_use]
pub fn paint_attributes(&self, res: &mut Resources<'_>, evaluated: EvaluatedLayer<'_>) -> Option<PaintAttributes> {
self.backend.paint_attributes(res, evaluated)
}
#[must_use]
pub fn raster_info(&self) -> Option<RasterTileInfo<'a>> {
self.backend.raster_info()
}
}
#[derive(Clone, Copy)]
pub struct TileGeometry<'a> {
backend: &'a dyn TileGeometryBackend,
}
impl<'a> TileGeometry<'a> {
#[doc(hidden)]
#[must_use]
pub fn from_backend(backend: &'a dyn TileGeometryBackend) -> Self {
Self { backend }
}
#[must_use]
pub fn vertex_stream_count(&self) -> usize {
self.backend.vertex_stream_count()
}
#[must_use]
pub fn vertex_count(&self, stream_index: usize) -> usize {
self.backend.vertex_count(stream_index)
}
#[must_use]
pub fn vertex_stride(&self, stream_index: usize) -> u32 {
self.backend.vertex_stride(stream_index)
}
#[must_use]
pub fn index_stream_count(&self) -> usize {
self.backend.index_stream_count()
}
#[must_use]
pub fn index_count(&self, stream_index: usize) -> usize {
self.backend.index_count(stream_index)
}
#[must_use]
pub fn draw_ranges(&self) -> Cow<'a, [DrawRange]> {
self.backend.draw_ranges()
}
#[must_use]
pub fn metadata(&self) -> &'a [u8] {
self.backend.metadata()
}
#[must_use]
pub fn max_elevation(&self) -> f32 {
self.backend.max_elevation()
}
#[must_use]
pub fn binder_interpolation_factor(&self, layer_id: &str, paint_slot: u32, zoom: f32) -> f32 {
self.backend.binder_interpolation_factor(layer_id, paint_slot, zoom)
}
#[must_use]
pub fn binder_statistics_max_f32(&self, layer_id: &str, paint_slot: u32) -> Option<f32> {
self.backend.binder_statistics_max_f32(layer_id, paint_slot)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LoadedFeatureInfo {
pub feature_index: usize,
pub feature_type: FeatureType,
pub ring_count: usize,
}
pub struct TileBuildCx<'a> {
backend: &'a mut dyn TileBuildCxBackend,
}
impl<'a> TileBuildCx<'a> {
#[doc(hidden)]
#[must_use]
pub fn from_backend(backend: &'a mut dyn TileBuildCxBackend) -> Self {
Self { backend }
}
#[must_use]
pub fn tile_id(&self) -> TileId {
self.backend.tile_id()
}
#[must_use]
pub fn zoom(&self) -> f64 {
self.backend.zoom()
}
#[must_use]
pub fn mode(&self) -> MapMode {
self.backend.mode()
}
#[must_use]
pub fn pixel_ratio(&self) -> f32 {
self.backend.pixel_ratio()
}
#[must_use]
pub fn first_load(&self) -> bool {
self.backend.first_load()
}
#[must_use]
pub fn layer_count(&self) -> usize {
self.backend.layer_count()
}
#[must_use]
pub fn layer(&self, index: usize) -> Option<EvaluatedLayer<'_>> {
self.backend.layer(index)
}
pub fn eval_layout_f32(&self, layer_index: usize, layout_slot: u32, for_current_feature: bool) -> Result<f32> {
self.backend.eval_layout_f32(layer_index, layout_slot, for_current_feature)
}
pub fn eval_layout_bool(&self, layer_index: usize, layout_slot: u32, for_current_feature: bool) -> Result<bool> {
self.backend.eval_layout_bool(layer_index, layout_slot, for_current_feature)
}
pub fn eval_layout_color(&self, layer_index: usize, layout_slot: u32, for_current_feature: bool) -> Result<Color> {
self.backend.eval_layout_color(layer_index, layout_slot, for_current_feature)
}
pub fn eval_layout_vec2(&self, layer_index: usize, layout_slot: u32, for_current_feature: bool) -> Result<Vec2> {
self.backend.eval_layout_vec2(layer_index, layout_slot, for_current_feature)
}
pub fn eval_layout_lat_lng(
&self,
layer_index: usize,
layout_slot: u32,
for_current_feature: bool,
) -> Result<LatLng> {
self.backend.eval_layout_lat_lng(layer_index, layout_slot, for_current_feature)
}
pub fn eval_layout_rotation_degrees(
&self,
layer_index: usize,
layout_slot: u32,
for_current_feature: bool,
) -> Result<f32> {
self.backend.eval_layout_rotation_degrees(layer_index, layout_slot, for_current_feature)
}
pub fn eval_layout_str(&mut self, layer_index: usize, layout_slot: u32, for_current_feature: bool) -> Result<&str> {
self.backend.eval_layout_str(layer_index, layout_slot, for_current_feature)
}
pub fn eval_layout_enum(&self, layer_index: usize, layout_slot: u32, for_current_feature: bool) -> Result<u32> {
self.backend.eval_layout_enum(layer_index, layout_slot, for_current_feature)
}
#[must_use]
pub fn feature_count(&self) -> usize {
self.backend.feature_count()
}
pub fn load_feature(&mut self, selected_index: usize) -> Result<LoadedFeatureInfo> {
self.backend.load_feature(selected_index)
}
#[must_use]
pub fn feature_ring(&self, ring_index: usize) -> Option<Cow<'_, [PointI16]>> {
self.backend.feature_ring(ring_index)
}
pub fn populate_binders(&mut self, vertex_count: usize) -> Result<()> {
self.backend.populate_binders(vertex_count)
}
pub fn index_current_feature(&mut self) -> Result<()> {
self.backend.index_current_feature()
}
}
pub struct TileGeometryBuilder<'a> {
backend: &'a mut dyn TileGeometryBuilderBackend,
}
impl<'a> TileGeometryBuilder<'a> {
#[doc(hidden)]
#[must_use]
pub fn from_backend(backend: &'a mut dyn TileGeometryBuilderBackend) -> Self {
Self { backend }
}
pub fn add_vertices(&mut self, bytes: &[u8], vertex_count: usize, stride: u32) -> Result<usize> {
self.backend.add_vertices(bytes, vertex_count, stride)
}
pub fn add_indexes(&mut self, indexes: &[u16]) -> Result<usize> {
self.backend.add_indexes(indexes)
}
pub fn add_draw_range(&mut self, range: DrawRange) -> Result<()> {
self.backend.add_draw_range(range)
}
pub fn set_metadata(&mut self, bytes: &[u8]) -> Result<()> {
self.backend.set_metadata(bytes)
}
pub fn set_max_elevation(&mut self, max_elevation: f32) -> Result<()> {
self.backend.set_max_elevation(max_elevation)
}
}
#[derive(Clone, Copy)]
pub struct Frame<'a> {
backend: &'a dyn FrameBackend,
}
impl<'a> Frame<'a> {
#[doc(hidden)]
#[must_use]
pub fn from_backend(backend: &'a dyn FrameBackend) -> Self {
Self { backend }
}
#[doc(hidden)]
#[must_use]
pub fn backend(&self) -> &'a dyn FrameBackend {
self.backend
}
#[must_use]
pub fn info(&self) -> FrameInfo {
self.backend.info()
}
#[must_use]
pub fn view(&self) -> View<'a> {
self.backend.view()
}
pub fn tile_projection(&self, tile_id: TileId, transform: TileTransform) -> Result<ProjectionUbo> {
self.backend.tile_projection(tile_id, transform)
}
#[must_use]
pub fn ground_height(&self, tile_id: TileId, x: f32, y: f32) -> f32 {
self.backend.ground_height(tile_id, x, y)
}
#[must_use]
pub fn ground_depth_texture(&self) -> Option<Texture> {
self.backend.ground_depth_texture()
}
pub fn collision_shapes(&self) -> impl ExactSizeIterator<Item = CollisionShape> + 'a {
let backend = self.backend;
(0..backend.collision_shape_count()).map(move |index| backend.collision_shape(index).unwrap_or_default())
}
}
#[derive(Clone, Copy)]
pub struct View<'a> {
backend: &'a dyn ViewBackend,
}
impl<'a> View<'a> {
#[doc(hidden)]
#[must_use]
pub fn from_backend(backend: &'a dyn ViewBackend) -> Self {
Self { backend }
}
#[must_use]
pub fn info(&self) -> ViewInfo {
self.backend.info()
}
pub fn world_from_lat_lng(&self, lat_lng: LatLng) -> Result<(f64, f64)> {
self.backend.world_from_lat_lng(lat_lng)
}
pub fn lat_lng_near_center(&self, lat_lng: LatLng) -> Result<LatLng> {
self.backend.lat_lng_near_center(lat_lng)
}
#[must_use]
pub fn ground_meters_per_world_unit_at(&self, lat_lng: LatLng) -> f64 {
self.backend.ground_meters_per_world_unit_at(lat_lng)
}
pub fn world_copies(&self) -> Result<Vec<i16>> {
self.backend.world_copies()
}
pub fn project_tile_point(&self, tile_id: TileId, x: f64, y: f64, elevation_m: f64) -> Result<ProjectedPoint> {
self.backend.project_tile_point(tile_id, x, y, elevation_m)
}
#[must_use]
pub fn map_aligned_scale_at(&self, tile_id: TileId, x: f64, y: f64) -> f64 {
self.backend.map_aligned_scale_at(tile_id, x, y)
}
#[must_use]
pub fn globe_extrude_scale(&self, tile_id: TileId) -> f32 {
self.backend.globe_extrude_scale(tile_id)
}
pub fn tile_unit_translation(&self, tile_id: TileId, translate: Vec2, anchor: Alignment) -> Result<Vec2> {
self.backend.tile_unit_translation(tile_id, translate, anchor)
}
}
pub struct Resources<'a> {
backend: &'a mut dyn ResourcesBackend,
}
impl<'a> Resources<'a> {
#[doc(hidden)]
#[must_use]
pub fn from_backend(backend: &'a mut dyn ResourcesBackend) -> Self {
Self { backend }
}
#[doc(hidden)]
#[must_use]
pub fn backend_mut(&mut self) -> &mut dyn ResourcesBackend {
self.backend
}
pub fn create_program(&mut self, desc: &ProgramDesc<'_>) -> Result<Program> {
self.backend.create_program(desc)
}
pub fn create_ground_heights(&mut self) -> Result<GroundHeights> {
self.backend.create_ground_heights()
}
pub fn create_vertices(&mut self, bytes: &[u8], vertex_count: usize, stride: u32) -> Result<VertexBuffer> {
self.backend.create_vertices(bytes, vertex_count, stride)
}
pub fn create_dynamic_vertices(&mut self, bytes: &[u8], vertex_count: usize, stride: u32) -> Result<VertexBuffer> {
self.backend.create_dynamic_vertices(bytes, vertex_count, stride)
}
pub fn update_vertices(&mut self, buffer: &VertexBuffer, bytes: &[u8]) -> bool {
self.backend.update_vertices(buffer, bytes)
}
pub fn create_indexes(&mut self, indexes: &[u16]) -> Result<IndexBuffer> {
self.backend.create_indexes(indexes)
}
pub fn quad_indexes(&mut self) -> Result<IndexBuffer> {
self.backend.quad_indexes()
}
pub fn create_texture(&mut self, format: TextureFormat, sampler: SamplerState) -> Result<Texture> {
self.backend.create_texture(format, sampler)
}
pub fn write_texture(&mut self, texture: &Texture, data: &[u8], width: u32, height: u32) -> Result<()> {
self.backend.write_texture(texture, data, width, height)
}
pub fn create_target(
&mut self,
width: u32,
height: u32,
channel_type: TextureChannelType,
clear_color: Color,
) -> Result<Target> {
self.backend.create_target(width, height, channel_type, clear_color)
}
pub fn create_mesh(&mut self, desc: &MeshDesc<'_>) -> Result<Mesh> {
self.backend.create_mesh(desc)
}
}
pub struct Draws<'a> {
backend: &'a mut dyn DrawsBackend,
}
impl<'a> Draws<'a> {
#[doc(hidden)]
#[must_use]
pub fn from_backend(backend: &'a mut dyn DrawsBackend) -> Self {
Self { backend }
}
#[must_use]
pub fn main(&mut self) -> DrawGroup<'_> {
self.backend.main()
}
#[must_use]
pub fn target(&mut self, target: &Target) -> DrawGroup<'_> {
self.backend.target(target)
}
pub fn lend(&mut self, at: LendAt) -> Result<()> {
self.backend.lend(at)
}
}
pub struct DrawGroup<'a> {
backend: &'a mut dyn DrawGroupBackend,
}
impl<'a> DrawGroup<'a> {
#[doc(hidden)]
#[must_use]
pub fn from_backend(backend: &'a mut dyn DrawGroupBackend) -> Self {
Self { backend }
}
pub fn push(&mut self, mesh: &Mesh, options: DrawOptions, blocks: &[&[u8]]) -> Result<()> {
self.backend.push(mesh, options, blocks)
}
pub fn push_at(
&mut self,
mesh: &Mesh,
tile_id: TileId,
projection: &ProjectionUbo,
options: DrawOptions,
blocks: &[&[u8]],
) -> Result<()> {
self.backend.push_at(mesh, tile_id, projection, options, blocks)
}
pub fn set_block(&mut self, program: &Program, buffer_name: &str, bytes: &[u8]) -> Result<()> {
self.backend.set_block(program, buffer_name, bytes)
}
pub fn set_composite(&mut self, composite: Composite) -> Result<()> {
self.backend.set_composite(composite)
}
}
#[derive(Debug)]
pub struct AfterGpu {
inner: Arc<dyn AfterGpuHandle>,
}
impl AfterGpu {
#[doc(hidden)]
#[must_use]
pub fn from_handle(inner: Arc<dyn AfterGpuHandle>) -> Self {
Self { inner }
}
pub fn push<F>(&self, callback: F) -> Result<()>
where
F: FnOnce(GpuEnd) + Send + 'static,
{
self.inner.push(Box::new(callback))
}
}
impl Clone for AfterGpu {
fn clone(&self) -> Self {
Self { inner: self.inner.clone_handle().unwrap_or_else(|_| Arc::clone(&self.inner)) }
}
}
#[derive(Clone, Copy)]
pub struct AfterGpuRef<'a> {
backend: &'a dyn AfterGpuRefBackend,
}
impl<'a> AfterGpuRef<'a> {
#[doc(hidden)]
#[must_use]
pub fn from_backend(backend: &'a dyn AfterGpuRefBackend) -> Self {
Self { backend }
}
pub fn clone_handle(self) -> Result<AfterGpu> {
self.backend.clone_handle()
}
pub fn push<F>(self, callback: F) -> Result<()>
where
F: FnOnce(GpuEnd) + Send + 'static,
{
self.backend.push(Box::new(callback))
}
}
#[derive(Clone, Copy)]
pub struct LendParameters<'a> {
at: LendAt,
frame: LendFrame,
stage: LendStage<'a>,
after_gpu: Option<AfterGpuRef<'a>>,
}
impl<'a> LendParameters<'a> {
#[doc(hidden)]
#[must_use]
pub const fn new(at: LendAt, frame: LendFrame, stage: LendStage<'a>, after_gpu: Option<AfterGpuRef<'a>>) -> Self {
Self { at, frame, stage, after_gpu }
}
#[must_use]
pub const fn at(&self) -> LendAt {
self.at
}
#[must_use]
pub const fn frame(&self) -> &LendFrame {
&self.frame
}
#[must_use]
pub const fn stage(&self) -> LendStage<'a> {
self.stage
}
#[must_use]
pub const fn after_gpu(&self) -> Option<AfterGpuRef<'a>> {
self.after_gpu
}
}
#[derive(Clone, Debug)]
pub struct HitInfo<'a> {
pub tile_id: TileId,
pub pixels_to_tile_units: f32,
pub has_ground: bool,
pub query_geometry: Cow<'a, [PointI16]>,
pub screen_geometry: Cow<'a, [ScreenPoint]>,
pub feature_ring_count: usize,
}
#[derive(Clone, Copy)]
pub struct HitCx<'a> {
backend: &'a dyn HitCxBackend,
}
impl<'a> HitCx<'a> {
#[doc(hidden)]
#[must_use]
pub fn from_backend(backend: &'a dyn HitCxBackend) -> Self {
Self { backend }
}
#[must_use]
pub fn info(&self) -> HitInfo<'a> {
self.backend.info()
}
#[must_use]
pub fn evaluated(&self) -> EvaluatedLayer<'a> {
self.backend.evaluated()
}
#[must_use]
pub fn view(&self) -> View<'a> {
self.backend.view()
}
#[must_use]
pub fn feature_ring(&self, index: usize) -> Option<Cow<'a, [PointI16]>> {
self.backend.feature_ring(index)
}
pub fn eval_paint_f32(&self, slot: u32) -> Result<f32> {
self.backend.eval_paint_f32(slot)
}
pub fn eval_paint_bool(&self, slot: u32) -> Result<bool> {
self.backend.eval_paint_bool(slot)
}
pub fn eval_paint_color(&self, slot: u32) -> Result<Color> {
self.backend.eval_paint_color(slot)
}
pub fn eval_paint_vec2(&self, slot: u32) -> Result<Vec2> {
self.backend.eval_paint_vec2(slot)
}
pub fn eval_paint_enum(&self, slot: u32) -> Result<u32> {
self.backend.eval_paint_enum(slot)
}
pub fn eval_paint_string(&self, slot: u32) -> Result<String> {
self.backend.eval_paint_string(slot)
}
#[must_use]
pub fn ground_height(&self, point: PointI16) -> f64 {
self.backend.ground_height(point)
}
pub fn translated_query_geometry(&self, translate: Vec2, anchor: Alignment) -> Result<Vec<PointI16>> {
self.backend.translated_query_geometry(translate, anchor)
}
pub fn plane_geometry(&self, elevation_m: f64, translate: Vec2, anchor: Alignment) -> Result<Vec<PointI16>> {
self.backend.plane_geometry(elevation_m, translate, anchor)
}
#[must_use]
pub fn viewport_point(&self, x: f64, y: f64, elevation_m: f64) -> Option<Vec2> {
self.backend.viewport_point(x, y, elevation_m)
}
#[must_use]
pub fn map_aligned_scale_at(&self, x: f64, y: f64) -> f64 {
self.backend.map_aligned_scale_at(x, y)
}
#[must_use]
pub fn pitch_scaled_size(
&self,
size: f32,
point: PointI16,
elevation_m: f64,
pitch_scale: Alignment,
pitch_alignment: Alignment,
) -> f32 {
self.backend.pitch_scaled_size(size, point, elevation_m, pitch_scale, pitch_alignment)
}
#[must_use]
pub fn shows(&self, x: f64, y: f64, elevation_m: f64) -> bool {
self.backend.shows(x, y, elevation_m)
}
}
pub struct FrameQueryCx<'a> {
ro: &'a dyn FrameQueryCxReadBackend,
rw: &'a mut dyn FrameQueryCxMutBackend,
}
impl<'a> FrameQueryCx<'a> {
#[doc(hidden)]
#[must_use]
pub fn from_backends(ro: &'a dyn FrameQueryCxReadBackend, rw: &'a mut dyn FrameQueryCxMutBackend) -> Self {
Self { ro, rw }
}
#[must_use]
pub fn evaluated(&self) -> EvaluatedLayer<'a> {
self.ro.evaluated()
}
#[must_use]
pub fn view(&self) -> View<'a> {
self.ro.view()
}
#[must_use]
pub fn geometry(&self) -> Cow<'a, [ScreenPoint]> {
self.ro.geometry()
}
#[must_use]
pub fn has_ground(&self) -> bool {
self.ro.has_ground()
}
#[must_use]
pub fn ground_height(&self, tile_id: TileId, x: f32, y: f32) -> f32 {
self.ro.ground_height(tile_id, x, y)
}
#[must_use]
pub fn ground_visibility(&self, tile_id: TileId, x: f64, y: f64, elevation_m: f64) -> f64 {
self.ro.ground_visibility(tile_id, x, y, elevation_m)
}
pub fn push_feature_json(&mut self, geojson_feature: &str) -> Result<()> {
self.rw.push_feature_json(geojson_feature)
}
pub fn push_point_feature(&mut self, position: LatLng) -> Result<()> {
self.rw.push_point_feature(position)
}
}
pub trait LayerPlugin: Send + Sync + 'static {
type Instance: LayerInstance;
fn desc(&self) -> LayerDesc<'_>;
fn init(&mut self, _host: &Host<'_>) -> Result<()> {
Ok(())
}
fn create_instance(&self, evaluated: EvaluatedLayer<'_>) -> Result<Self::Instance>;
fn tile_fade_ns(&self, _evaluated: EvaluatedLayer<'_>) -> u64 {
0
}
fn build_tile_geometry(&self, _cx: &mut TileBuildCx<'_>, _out: &mut TileGeometryBuilder<'_>) -> Result<()> {
Ok(())
}
fn query_radius(&self, _geometry: TileGeometry<'_>, _evaluated: EvaluatedLayer<'_>) -> f32 {
0.0
}
}
pub trait LayerInstance: 'static {
type TileResources: 'static;
fn evaluate(&mut self, cx: EvaluateCx<'_>) -> Evaluation;
fn update(&mut self, _cx: &mut UpdateCx<'_>) {}
fn create_tile_resources(
&mut self,
_cx: &mut TileCx<'_>,
_previous: Option<&mut Self::TileResources>,
) -> Result<Option<Self::TileResources>> {
Ok(None)
}
fn prepare(&mut self, _cx: &mut PrepareCx<'_, Self::TileResources>, _draws: &mut Draws<'_>) {}
fn hit(&self, _cx: HitCx<'_>) -> Hit {
Hit::Miss
}
fn hit_frame(&self, _cx: &mut FrameQueryCx<'_>) {}
fn lend(&mut self, _params: &LendParameters<'_>) {}
}