mln_engine/draw/
atlas_binding.rs1use mln_gfx::gpu::ShaderKey;
2use mln_gfx::texture2d::SamplerState;
3use mln_gfx::types::{TextureFilterType, TextureWrapType};
4use mln_style::types::AlignmentType;
5
6use crate::draw::context::Context;
7use crate::draw::objects::{DrawObjects, Textures};
8use crate::draw::store::SharedTexture;
9use crate::draw::tile_render_data::TileAtlasTextures;
10use crate::render::paint_parameters::PaintParameters;
11
12#[derive(Clone)]
15pub struct AtlasBinding {
16 atlases: Option<(Option<SharedTexture>, Option<SharedTexture>)>,
19 icon_slot: Option<u8>,
20 glyph_slot: Option<u8>,
21 is_text: bool,
22 sdf_icons: bool,
23 rotation_alignment: AlignmentType,
24 icon_scaled: bool,
25 text_size_is_zoom_constant: bool,
26}
27
28impl AtlasBinding {
29 #[expect(clippy::too_many_arguments, reason = "each argument is a field of the binding")]
33 #[must_use]
34 pub fn new(
35 atlases: Option<TileAtlasTextures>,
36 icon_slot: Option<usize>,
37 glyph_slot: Option<usize>,
38 is_text: bool,
39 sdf_icons: bool,
40 rotation_alignment: AlignmentType,
41 icon_scaled: bool,
42 text_size_is_zoom_constant: bool,
43 ) -> Self {
44 debug_assert_ne!(icon_slot, glyph_slot);
45 let slot = |slot: Option<usize>| slot.and_then(|slot| u8::try_from(slot).ok());
46 AtlasBinding {
47 atlases: atlases.map(|atlases| (atlases.glyph().cloned(), atlases.icon().cloned())),
48 icon_slot: slot(icon_slot),
49 glyph_slot: slot(glyph_slot),
50 is_text,
51 sdf_icons,
52 rotation_alignment,
53 icon_scaled,
54 text_size_is_zoom_constant,
55 }
56 }
57
58 #[must_use]
61 pub fn textures(&self, shader: &ShaderKey) -> Textures {
62 let mut textures = Textures::default();
63 self.set_textures(self.samples_icon(shader), |slot, texture| textures[slot] = texture.cloned());
64 textures
65 }
66
67 pub(crate) fn bind(&self, mesh: &mut DrawObjects, parameters: &mut PaintParameters<'_>) {
71 let state = parameters.view;
72 let transformed = self.rotation_alignment == AlignmentType::Map || state.pitch() != 0.0;
73 let linear_filter_for_icons = if self.is_text {
74 parameters.camera_moving || transformed || !self.text_size_is_zoom_constant
75 } else {
76 self.sdf_icons || parameters.camera_moving || self.icon_scaled || transformed
77 };
78 if self.glyph_slot.is_some()
79 && let Some((glyph, icon)) = &self.atlases
80 {
81 set_samplers(parameters.context, glyph.as_ref(), icon.as_ref(), linear_filter_for_icons);
82 }
83 let samples_icon = self.samples_icon(mesh.shader());
84 self.set_textures(samples_icon, |slot, texture| mesh.set_texture(slot, texture));
85 }
86
87 fn set_textures(&self, samples_icon: bool, mut set: impl FnMut(usize, Option<&SharedTexture>)) {
90 let (Some(glyph_slot), Some((glyph, icon))) = (self.glyph_slot, &self.atlases) else {
91 return;
92 };
93 let (glyph, icon) = (glyph.as_ref(), icon.as_ref());
94 let glyph_slot = usize::from(glyph_slot);
95 if let Some(icon_slot) = self.icon_slot.map(usize::from).filter(|_| samples_icon) {
96 debug_assert_ne!(glyph_slot, icon_slot);
97 set(glyph_slot, glyph);
98 set(icon_slot, icon);
99 } else {
100 set(glyph_slot, if self.is_text { glyph } else { icon });
101 }
102 }
103
104 fn samples_icon(&self, shader: &ShaderKey) -> bool {
106 let textures = shader.program().textures;
107 self.icon_slot.is_some_and(|slot| textures.get(usize::from(slot)).is_some_and(|texture| !texture.is_unused()))
108 }
109}
110
111fn set_samplers(
114 context: &mut Context,
115 glyph: Option<&SharedTexture>,
116 icon: Option<&SharedTexture>,
117 linear_filter_for_icons: bool,
118) {
119 if let Some(mut glyph) = glyph.and_then(|glyph| context.texture_mut(glyph)) {
120 glyph.set_sampler(SamplerState {
121 filter: TextureFilterType::Linear,
122 wrap_u: TextureWrapType::Clamp,
123 wrap_v: TextureWrapType::Clamp,
124 ..Default::default()
125 });
126 }
127 if let Some(mut icon) = icon.and_then(|icon| context.texture_mut(icon)) {
128 icon.set_sampler(SamplerState {
129 filter: if linear_filter_for_icons { TextureFilterType::Linear } else { TextureFilterType::Nearest },
130 wrap_u: TextureWrapType::Clamp,
131 wrap_v: TextureWrapType::Clamp,
132 ..Default::default()
133 });
134 }
135}