use mln_gfx::gfx_types::AttributeDataType;
use mln_style::collections::VecMap;
use mln_style::color::Color;
use mln_style::expression::{Dependency, EvaluationContext, Image, Value, ValueConverter};
use mln_style::feature::GeometryTileFeature;
use mln_style::property::evaluated::{Evaluated, Faded};
use mln_style::property::expression::PropertyExpression;
use mln_style::range::Range;
use mln_style::tile_id::CanonicalTileID;
use crate::draw::vertex_attribute::{SharedAttribute, Uniforms, VertexAttributeArray};
use crate::draw::vertices::SharedVertices;
use crate::layer::bucket_geometry::GeometryVec;
use crate::layer::paint_property_statistics::{PaintPropertyStatistics, StatisticValue};
use crate::text::atlas::ImagePositions;
use crate::tile::TileLayer;
use crate::tile::feature::feature_id_to_string;
#[derive(Clone, Copy, Debug, PartialEq, Eq, mln_wire::Wire)]
pub(crate) struct FeatureVertexRange {
pub(crate) feature_index: usize,
pub(crate) start: usize,
pub(crate) end: usize,
}
pub(crate) type FeatureVertexRangeMap = VecMap<String, Vec<FeatureVertexRange>>;
#[derive(Clone, Debug, Default, PartialEq, mln_wire::Wire)]
pub struct InterleavedVertexBuffer {
pub stride: usize,
pub vertex_count: usize,
pub shared_vertex_vector: GeometryVec<u8>,
}
impl InterleavedVertexBuffer {
pub fn fill(&mut self, indexes: std::ops::Range<usize>, offset: usize, value: &[u8]) {
if indexes.is_empty() {
return;
}
let stride = self.stride;
let data = self.shared_vertex_vector.get_mut();
for index in indexes {
let start = stride * index + offset;
debug_assert!(start + value.len() <= data.len());
data[start..start + value.len()].copy_from_slice(value);
}
}
pub fn extend_vertex_format(&mut self, size: usize, alignment: usize) -> usize {
let mut offset = self.stride & (alignment - 1);
if offset != 0 {
offset = alignment - offset;
}
let vertex_offset = self.stride + offset;
self.stride += size + offset;
vertex_offset
}
}
pub trait BinderValue: Clone + Default + ValueConverter + StatisticValue + std::fmt::Debug {
const DIMENSIONS: usize;
const BASE_DATA_TYPE: AttributeDataType;
const INTERPOLATED_DATA_TYPE: AttributeDataType;
fn attribute_value(&self) -> [f32; 2];
}
impl BinderValue for f32 {
const DIMENSIONS: usize = 1;
const BASE_DATA_TYPE: AttributeDataType = AttributeDataType::Float;
const INTERPOLATED_DATA_TYPE: AttributeDataType = AttributeDataType::Float2;
fn attribute_value(&self) -> [f32; 2] {
[*self, 0.0]
}
}
fn pack_uint8_pair(a: f32, b: f32) -> u16 {
(i32::from(a as u16) * 256 + i32::from(b as u16)) as u16
}
impl BinderValue for Color {
const DIMENSIONS: usize = 2;
const BASE_DATA_TYPE: AttributeDataType = AttributeDataType::Float2;
const INTERPOLATED_DATA_TYPE: AttributeDataType = AttributeDataType::Float4;
fn attribute_value(&self) -> [f32; 2] {
[
f32::from(pack_uint8_pair(255.0 * self.r, 255.0 * self.g)),
f32::from(pack_uint8_pair(255.0 * self.b, 255.0 * self.a)),
]
}
}
fn zoom_stop_values<T: BinderValue>(min: &T, max: &T) -> [f32; 4] {
let mut values = [0.0; 4];
values[..T::DIMENSIONS].copy_from_slice(&min.attribute_value()[..T::DIMENSIONS]);
values[T::DIMENSIONS..2 * T::DIMENSIONS].copy_from_slice(&max.attribute_value()[..T::DIMENSIONS]);
values
}
#[derive(Clone, Debug, mln_wire::Wire)]
pub(crate) enum PaintPropertyBinderKind<T> {
Constant {
constant: T,
},
SourceFunction {
expression: PropertyExpression<T>,
default_value: T,
feature_map: FeatureVertexRangeMap,
vertex_data: InterleavedVertexData,
},
CompositeFunction {
expression: PropertyExpression<T>,
default_value: T,
zoom_range: Range<f32>,
feature_map: FeatureVertexRangeMap,
vertex_data: InterleavedVertexData,
},
}
#[derive(Clone, Debug, mln_wire::Wire, PartialEq, Eq)]
pub enum InterleavedVertexData {
Uninitialized,
Interleaved { offset: usize },
}
impl InterleavedVertexData {
#[must_use]
pub fn offset(&self) -> Option<usize> {
match self {
Self::Uninitialized => None,
Self::Interleaved { offset } => Some(*offset),
}
}
}
impl<T: BinderValue> PaintPropertyBinderKind<T> {
fn vertex_offset(&self) -> Option<usize> {
match self {
Self::Constant { .. } => None,
Self::SourceFunction { vertex_data, .. } | Self::CompositeFunction { vertex_data, .. } => {
vertex_data.offset()
}
}
}
fn update_vertex_vector(
&self,
statistics: &mut PaintPropertyStatistics<T>,
buffer: &mut InterleavedVertexBuffer,
vertex_offset: usize,
range: &FeatureVertexRange,
feature: &dyn GeometryTileFeature,
state: &mln_style::feature::FeatureState,
) {
match self {
Self::Constant { .. } => {}
Self::SourceFunction { expression, default_value, .. } => {
let evaluated = expression.evaluate(
&EvaluationContext::from_feature(Some(feature)).with_feature_state(Some(state)),
default_value.clone(),
);
statistics.add(&evaluated);
let value = evaluated.attribute_value();
buffer.fill(range.start..range.end, vertex_offset, bytemuck::cast_slice(&value[..T::DIMENSIONS]));
}
Self::CompositeFunction { expression, default_value, zoom_range, .. } => {
let min = expression.evaluate(
&EvaluationContext::with_zoom_feature_state(zoom_range.min, Some(feature), Some(state)),
default_value.clone(),
);
let max = expression.evaluate(
&EvaluationContext::with_zoom_feature_state(zoom_range.max, Some(feature), Some(state)),
default_value.clone(),
);
statistics.add(&min);
statistics.add(&max);
let value = zoom_stop_values(&min, &max);
buffer.fill(range.start..range.end, vertex_offset, bytemuck::cast_slice(&value[..2 * T::DIMENSIONS]));
}
}
}
}
#[derive(Clone, Debug, mln_wire::Wire)]
pub struct PaintPropertyBinder<T> {
pub(crate) kind: PaintPropertyBinderKind<T>,
pub statistics: PaintPropertyStatistics<T>,
}
impl<T: BinderValue> PaintPropertyBinder<T> {
pub fn create(value: &Evaluated<T>, zoom: f32) -> Self {
let kind = match value {
Evaluated::Constant(constant) | Evaluated::CrossFaded(Faded { to: constant, .. }) => {
PaintPropertyBinderKind::Constant { constant: constant.clone() }
}
Evaluated::PerFeature { expression, default } => {
let default_value = default.clone();
if expression.is_zoom_constant() {
PaintPropertyBinderKind::SourceFunction {
expression: expression.clone(),
default_value,
feature_map: FeatureVertexRangeMap::new(),
vertex_data: InterleavedVertexData::Uninitialized,
}
} else {
PaintPropertyBinderKind::CompositeFunction {
expression: expression.clone(),
default_value,
zoom_range: Range::new(zoom, zoom + 1.0),
feature_map: FeatureVertexRangeMap::new(),
vertex_data: InterleavedVertexData::Uninitialized,
}
}
}
};
PaintPropertyBinder { kind, statistics: PaintPropertyStatistics::default() }
}
fn zoom_interpolated_size() -> usize {
2 * T::DIMENSIONS * 4
}
pub(crate) fn set_interleaved_buffer(&mut self, buffer: &mut InterleavedVertexBuffer) {
let vertex_data = match &mut self.kind {
PaintPropertyBinderKind::Constant { .. } => return,
PaintPropertyBinderKind::SourceFunction { vertex_data, .. }
| PaintPropertyBinderKind::CompositeFunction { vertex_data, .. } => vertex_data,
};
let new_vertex_offset = buffer.extend_vertex_format(Self::zoom_interpolated_size(), align_of::<f32>());
if matches!(vertex_data, InterleavedVertexData::Uninitialized) {
*vertex_data = InterleavedVertexData::Interleaved { offset: new_vertex_offset };
}
}
pub(crate) fn vertex_count(&self, buffer: &InterleavedVertexBuffer) -> usize {
if self.kind.vertex_offset().is_some() { buffer.vertex_count } else { 0 }
}
pub(crate) fn is_interpolated(&self) -> bool {
matches!(self.kind, PaintPropertyBinderKind::CompositeFunction { .. })
}
pub(crate) fn populate_vertex_vector(
&mut self,
buffer: &mut InterleavedVertexBuffer,
feature: &dyn GeometryTileFeature,
length: usize,
index: usize,
canonical: &CanonicalTileID,
formatted_section: &Value,
) {
let Some(vertex_offset) = self.kind.vertex_offset() else { return };
let elements = self.vertex_count(buffer);
match &mut self.kind {
PaintPropertyBinderKind::Constant { .. } => {}
PaintPropertyBinderKind::SourceFunction { expression, default_value, feature_map, .. } => {
let evaluated = expression.evaluate(
&EvaluationContext::from_feature(Some(feature))
.with_formatted_section(Some(formatted_section))
.with_canonical_tile_id(Some(canonical)),
default_value.clone(),
);
self.statistics.add(&evaluated);
let value = evaluated.attribute_value();
buffer.fill(elements..length, vertex_offset, bytemuck::cast_slice(&value[..T::DIMENSIONS]));
if let Some(id_str) = feature_id_to_string(&feature.get_id()) {
feature_map.entry(id_str).or_default().push(FeatureVertexRange {
feature_index: index,
start: elements,
end: length,
});
}
}
PaintPropertyBinderKind::CompositeFunction {
expression, default_value, zoom_range, feature_map, ..
} => {
let min = expression.evaluate(
&EvaluationContext::with_zoom_feature(zoom_range.min, Some(feature))
.with_formatted_section(Some(formatted_section))
.with_canonical_tile_id(Some(canonical)),
default_value.clone(),
);
let max = expression.evaluate(
&EvaluationContext::with_zoom_feature(zoom_range.max, Some(feature))
.with_formatted_section(Some(formatted_section))
.with_canonical_tile_id(Some(canonical)),
default_value.clone(),
);
self.statistics.add(&min);
self.statistics.add(&max);
let value = zoom_stop_values(&min, &max);
buffer.fill(elements..length, vertex_offset, bytemuck::cast_slice(&value[..2 * T::DIMENSIONS]));
if let Some(id_str) = feature_id_to_string(&feature.get_id()) {
feature_map.entry(id_str).or_default().push(FeatureVertexRange {
feature_index: index,
start: elements,
end: length,
});
}
}
}
}
pub(crate) fn update_vertex_vectors(
&mut self,
buffer: &mut InterleavedVertexBuffer,
states: &crate::layer::bucket::FeatureStates,
layer: &TileLayer<'_>,
) {
let Self { kind, statistics } = self;
let (feature_map, vertex_data) = match &*kind {
PaintPropertyBinderKind::Constant { .. } => return,
PaintPropertyBinderKind::SourceFunction { feature_map, vertex_data, .. }
| PaintPropertyBinderKind::CompositeFunction { feature_map, vertex_data, .. } => (feature_map, vertex_data),
};
let Some(vertex_offset) = vertex_data.offset() else {
return;
};
for (id, state) in states {
let Some(positions) = feature_map.get(id) else {
continue;
};
for pos in positions {
let Ok(feature) = layer.feature(pos.feature_index) else {
continue;
};
kind.update_vertex_vector(statistics, buffer, vertex_offset, pos, &feature, state);
}
}
}
pub fn interpolation_factor(&self, current_zoom: f32) -> f32 {
match &self.kind {
PaintPropertyBinderKind::Constant { .. } | PaintPropertyBinderKind::SourceFunction { .. } => 0.0,
PaintPropertyBinderKind::CompositeFunction { expression, zoom_range, .. } => {
let possibly_rounded_zoom =
if expression.get_use_integer_zoom() { current_zoom.floor() } else { current_zoom };
mln_style::math::fmaxf(
0.0,
mln_style::math::fminf(1.0, expression.interpolation_factor(*zoom_range, possibly_rounded_zoom)),
)
}
}
}
pub(crate) fn apply_paint_property(
&self,
buffer: &InterleavedVertexBuffer,
shared: &SharedVertices,
) -> Option<SharedAttribute> {
if let Some(vertex_offset) = self.kind.vertex_offset() {
let raw_size = buffer.stride as u32;
let data_type = if self.is_interpolated() { T::INTERPOLATED_DATA_TYPE } else { T::BASE_DATA_TYPE };
debug_assert_eq!(shared.count() / buffer.stride.max(1), self.vertex_count(buffer));
Some(shared_paint(shared, vertex_offset, raw_size, data_type))
} else {
debug_assert_eq!(self.vertex_count(buffer), 0);
None
}
}
}
#[derive(Clone, Debug, mln_wire::Wire)]
pub(crate) enum CrossFadedPaintPropertyBinderKind<T> {
Constant {
constant: Faded<T>,
},
Composite {
expression: PropertyExpression<T>,
default_value: T,
zoom_range: Range<f32>,
vertex_data: InterleavedVertexData,
#[wire(skip)]
populated_count: usize,
},
}
impl<T> CrossFadedPaintPropertyBinderKind<T> {
fn vertex_offset(&self) -> Option<usize> {
match self {
Self::Constant { .. } => None,
Self::Composite { vertex_data, .. } => vertex_data.offset(),
}
}
}
#[derive(Clone, Debug, mln_wire::Wire)]
pub struct CrossFadedPaintPropertyBinder<T> {
pub(crate) kind: CrossFadedPaintPropertyBinderKind<T>,
}
const PATTERN_ZOOM_INTERPOLATED_SIZE: usize = 16;
impl CrossFadedPaintPropertyBinder<Image> {
#[must_use]
pub fn create(value: &Evaluated<Image>, zoom: f32) -> Self {
let kind = match value {
Evaluated::Constant(constant) => CrossFadedPaintPropertyBinderKind::Constant {
constant: Faded { from: constant.clone(), to: constant.clone() },
},
Evaluated::CrossFaded(constant) => {
CrossFadedPaintPropertyBinderKind::Constant { constant: constant.clone() }
}
Evaluated::PerFeature { expression, default } => CrossFadedPaintPropertyBinderKind::Composite {
expression: expression.clone(),
default_value: default.clone(),
zoom_range: Range::new(zoom, zoom + 1.0),
vertex_data: InterleavedVertexData::Uninitialized,
populated_count: 0,
},
};
CrossFadedPaintPropertyBinder { kind }
}
pub(crate) fn set_interleaved_buffer(&mut self, buffer: &mut InterleavedVertexBuffer) {
let CrossFadedPaintPropertyBinderKind::Composite { vertex_data, .. } = &mut self.kind else {
return;
};
let new_vertex_offset = buffer.extend_vertex_format(PATTERN_ZOOM_INTERPOLATED_SIZE, align_of::<u16>());
if matches!(vertex_data, InterleavedVertexData::Uninitialized) {
*vertex_data = InterleavedVertexData::Interleaved { offset: new_vertex_offset };
}
}
pub(crate) fn vertex_count(&self, buffer: &InterleavedVertexBuffer) -> usize {
if self.kind.vertex_offset().is_some() { buffer.vertex_count } else { 0 }
}
pub(crate) fn populate_vertex_vector(
&mut self,
buffer: &mut InterleavedVertexBuffer,
length: usize,
pattern_positions: &ImagePositions,
pattern_dependencies: Option<&PatternDependency>,
) {
let CrossFadedPaintPropertyBinderKind::Composite { populated_count, vertex_data, .. } = &mut self.kind else {
return;
};
let InterleavedVertexData::Interleaved { offset: vertex_offset } = vertex_data else {
return;
};
match pattern_dependencies {
Some(dependencies) if !dependencies.mid.is_empty() => {
if !pattern_positions.is_empty() {
let (Some(_), Some(image_mid), Some(_)) = (
pattern_positions.get(&dependencies.min),
pattern_positions.get(&dependencies.mid),
pattern_positions.get(&dependencies.max),
) else {
return;
};
buffer.fill(*populated_count..length, *vertex_offset, bytemuck::cast_slice(&image_mid.tlbr()));
*populated_count = (*populated_count).max(length);
}
}
_ => {
buffer.fill(*populated_count..length, *vertex_offset, bytemuck::cast_slice(&[0u16; 4]));
*populated_count = (*populated_count).max(length);
}
}
}
#[must_use]
pub fn interpolation_factor(&self, _current_zoom: f32) -> (f32, f32) {
(0.0, 0.0)
}
pub(crate) fn apply_paint_property(
&self,
buffer: &InterleavedVertexBuffer,
shared: &SharedVertices,
) -> Option<SharedAttribute> {
if let Some(vertex_offset) = self.kind.vertex_offset() {
let raw_size = buffer.stride as u32;
Some(shared_paint(shared, vertex_offset, raw_size, AttributeDataType::UShort4))
} else {
None
}
}
}
pub trait PaintPropertyBinderBase {
fn set_interleaved(&mut self, buffer: &mut InterleavedVertexBuffer);
#[expect(
clippy::too_many_arguments,
reason = "the vertex buffer, the feature and its index range, its patterns, the tile and the formatted section that evaluate a property"
)]
fn populate(
&mut self,
buffer: &mut InterleavedVertexBuffer,
feature: &dyn GeometryTileFeature,
length: usize,
index: usize,
pattern_positions: &ImagePositions,
pattern_dependencies: Option<&PatternDependency>,
canonical: &CanonicalTileID,
formatted_section: &Value,
);
fn update_vectors(
&mut self,
_buffer: &mut InterleavedVertexBuffer,
_states: &crate::layer::bucket::FeatureStates,
_layer: &TileLayer<'_>,
) {
}
fn depends_on_feature_state(&self) -> bool {
false
}
fn binder_vertex_count(&self, buffer: &InterleavedVertexBuffer) -> usize;
fn apply(&self, buffer: &InterleavedVertexBuffer, shared: &SharedVertices) -> Option<SharedAttribute>;
}
impl<T: BinderValue> PaintPropertyBinderBase for PaintPropertyBinder<T> {
fn set_interleaved(&mut self, buffer: &mut InterleavedVertexBuffer) {
self.set_interleaved_buffer(buffer);
}
fn populate(
&mut self,
buffer: &mut InterleavedVertexBuffer,
feature: &dyn GeometryTileFeature,
length: usize,
index: usize,
_pattern_positions: &ImagePositions,
_pattern_dependencies: Option<&PatternDependency>,
canonical: &CanonicalTileID,
formatted_section: &Value,
) {
self.populate_vertex_vector(buffer, feature, length, index, canonical, formatted_section);
}
fn update_vectors(
&mut self,
buffer: &mut InterleavedVertexBuffer,
states: &crate::layer::bucket::FeatureStates,
layer: &TileLayer<'_>,
) {
self.update_vertex_vectors(buffer, states, layer);
}
fn depends_on_feature_state(&self) -> bool {
match &self.kind {
PaintPropertyBinderKind::Constant { .. } => false,
PaintPropertyBinderKind::SourceFunction { expression, .. }
| PaintPropertyBinderKind::CompositeFunction { expression, .. } => {
expression.get_expression().has(Dependency::FEATURE_STATE)
}
}
}
fn binder_vertex_count(&self, buffer: &InterleavedVertexBuffer) -> usize {
self.vertex_count(buffer)
}
fn apply(&self, buffer: &InterleavedVertexBuffer, shared: &SharedVertices) -> Option<SharedAttribute> {
self.apply_paint_property(buffer, shared)
}
}
impl PaintPropertyBinderBase for CrossFadedPaintPropertyBinder<Image> {
fn set_interleaved(&mut self, buffer: &mut InterleavedVertexBuffer) {
self.set_interleaved_buffer(buffer);
}
fn populate(
&mut self,
buffer: &mut InterleavedVertexBuffer,
_feature: &dyn GeometryTileFeature,
length: usize,
_index: usize,
pattern_positions: &ImagePositions,
pattern_dependencies: Option<&PatternDependency>,
_canonical: &CanonicalTileID,
_formatted_section: &Value,
) {
self.populate_vertex_vector(buffer, length, pattern_positions, pattern_dependencies);
}
fn binder_vertex_count(&self, buffer: &InterleavedVertexBuffer) -> usize {
self.vertex_count(buffer)
}
fn apply(&self, buffer: &InterleavedVertexBuffer, shared: &SharedVertices) -> Option<SharedAttribute> {
self.apply_paint_property(buffer, shared)
}
}
#[must_use]
pub fn shared_paint(
shared: &SharedVertices,
offset: usize,
stride: u32,
data_type: AttributeDataType,
) -> SharedAttribute {
SharedAttribute::new(shared.clone(), data_type, offset as u32, stride)
}
pub fn bind_paint_property(
binder: &dyn PaintPropertyBinderBase,
buffer: &InterleavedVertexBuffer,
shared: Option<&SharedVertices>,
attributes: &[&'static str],
is_constant: bool,
attrs: &mut VertexAttributeArray,
uniforms: &mut Uniforms,
) {
if is_constant || binder.binder_vertex_count(buffer) == 0 {
for &attribute in attributes {
uniforms.insert(attribute);
}
} else if let Some(values) = shared.and_then(|shared| binder.apply(buffer, shared)) {
for &attribute in attributes {
attrs.set_shared(attribute, values.clone());
}
}
}
pub trait PaintBinders: Clone + Send + mln_wire::Wire + 'static {
type Paint: 'static;
fn new(paint: &Self::Paint, zoom: f32) -> Self;
fn vertices_mut(&mut self) -> &mut InterleavedVertexBuffer;
#[expect(clippy::too_many_arguments, reason = "the feature, its vertices, its tile, its section and its patterns")]
fn populate(
&mut self,
feature: &dyn GeometryTileFeature,
length: usize,
index: usize,
positions: &ImagePositions,
pattern: Option<&PatternDependency>,
canonical: &CanonicalTileID,
section: &Value,
);
fn feature_state(&mut self, states: &crate::layer::bucket::FeatureStates, layer: &TileLayer<'_>);
fn depends_on_feature_state(&self) -> bool;
}
#[derive(Clone, Debug, mln_wire::Wire)]
#[wire(bound = "P: mln_wire::Wire")]
pub struct LayerBinders<P> {
layers: Vec<(String, P)>,
}
impl<P> Default for LayerBinders<P> {
fn default() -> Self {
LayerBinders { layers: Vec::new() }
}
}
impl<P> LayerBinders<P> {
#[must_use]
pub fn get(&self, id: &str) -> Option<&P> {
self.layers.iter().find(|(layer, _)| layer == id).map(|(_, binders)| binders)
}
pub fn values(&self) -> impl Iterator<Item = &P> {
self.layers.iter().map(|(_, binders)| binders)
}
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut P> {
self.layers.iter_mut().map(|(_, binders)| binders)
}
pub(crate) fn iter_mut(&mut self) -> impl Iterator<Item = (&str, &mut P)> {
self.layers.iter_mut().map(|(id, binders)| (id.as_str(), binders))
}
}
impl<P: PaintBinders> LayerBinders<P> {
pub fn new<T>(layers: &[std::sync::Arc<crate::layer::layer_type::EvaluatedLayer<T>>], zoom: f32) -> Self
where
T: mln_style::layer::StyleLayerType<PaintProperties: mln_style::layer::PaintSet<Evaluated = P::Paint>>,
{
let mut binders: Vec<(String, P)> = Vec::with_capacity(layers.len());
for layer in layers {
if !binders.iter().any(|(id, _)| *id == layer.style.id) {
binders.push((layer.style.id.clone(), P::new(&layer.paint, zoom)));
}
}
LayerBinders { layers: binders }
}
#[expect(clippy::too_many_arguments, reason = "the feature, its vertices, its tile, its section and its patterns")]
pub fn populate(
&mut self,
feature: &dyn GeometryTileFeature,
length: usize,
index: usize,
positions: &ImagePositions,
patterns: Option<&PatternLayerMap>,
canonical: &CanonicalTileID,
section: &Value,
) {
for (id, binders) in &mut self.layers {
let pattern = patterns.and_then(|patterns| patterns.get(id));
binders.populate(feature, length, index, positions, pattern, canonical, section);
}
}
}
#[macro_export]
macro_rules! paint_property_binders {
(
$(#[$meta:meta])*
pub struct $name:ident for $props:path {
$($field:ident: $binder:ty => [$($attribute:expr),+]),+ $(,)?
}
) => {
$(#[$meta])*
#[derive(Clone, Debug, $crate::__private::mln_wire::Wire)]
#[wire(crate = $crate::__private::mln_wire)]
pub(crate) struct $name {
pub interleaved_vertex_buffer: $crate::layer::binders::InterleavedVertexBuffer,
$(pub $field: $binder,)+
}
impl $crate::layer::binders::PaintBinders for $name {
type Paint = $props;
fn new(properties: &$props, z: f32) -> Self {
let mut result = $name {
interleaved_vertex_buffer: Default::default(),
$($field: <$binder>::create(&properties.$field, z),)+
};
$($crate::layer::binders::PaintPropertyBinderBase::set_interleaved(
&mut result.$field, &mut result.interleaved_vertex_buffer);)+
result
}
fn vertices_mut(&mut self) -> &mut $crate::layer::binders::InterleavedVertexBuffer {
&mut self.interleaved_vertex_buffer
}
fn populate(
&mut self,
feature: &dyn $crate::__private::mln_style::feature::GeometryTileFeature,
length: usize,
index: usize,
pattern_positions: &$crate::draw::ImagePositions,
pattern_dependencies: Option<&$crate::layer::binders::PatternDependency>,
canonical: &$crate::__private::mln_style::tile_id::CanonicalTileID,
formatted_section: &$crate::__private::mln_style::expression::Value,
) {
let buffer = &mut self.interleaved_vertex_buffer;
let added = (length - buffer.vertex_count) * buffer.stride;
let data = buffer.shared_vertex_vector.get_mut();
data.resize(data.len() + added, 0);
$($crate::layer::binders::PaintPropertyBinderBase::populate(
&mut self.$field,
&mut self.interleaved_vertex_buffer,
feature,
length,
index,
pattern_positions,
pattern_dependencies,
canonical,
formatted_section,
);)+
let buffer = &mut self.interleaved_vertex_buffer;
buffer.vertex_count += length - buffer.vertex_count;
}
fn feature_state(
&mut self,
states: &$crate::layer::bucket::FeatureStates,
layer: &$crate::tile::TileLayer<'_>,
) {
if self.interleaved_vertex_buffer.shared_vertex_vector.is_moved() {
return;
}
$($crate::layer::binders::PaintPropertyBinderBase::update_vectors(
&mut self.$field, &mut self.interleaved_vertex_buffer, states, layer);)+
}
fn depends_on_feature_state(&self) -> bool {
false $(|| $crate::layer::binders::PaintPropertyBinderBase::depends_on_feature_state(
&self.$field))+
}
}
impl $name {
pub(crate) fn bind(
&self,
evaluated: &$props,
shared: Option<&$crate::draw::SharedVertices>,
attrs: &mut $crate::draw::VertexAttributeArray,
uniforms: &mut $crate::draw::Uniforms,
) {
$($crate::layer::binders::bind_paint_property(
&self.$field,
&self.interleaved_vertex_buffer,
shared,
&[$($attribute),+],
evaluated.$field.is_constant(),
attrs,
uniforms,
);)+
}
}
};
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct PatternDependency {
pub min: String,
pub mid: String,
pub max: String,
}
pub type PatternLayerMap = mln_style::hash::HashMap<String, PatternDependency>;