mln-rs-engine 0.0.18

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
//! The GLSL ES, for OpenGL, of the symbols' programs.

use mln_gfx::shaders::block::{Block, UniformBlock};
use mln_gfx::shaders::source::Precision::Low;
use mln_gfx::shaders::source::{GlslEsDeclarations, GlslEsSource};

use super::ubo::SymbolDrawableUBO;
use crate::render::terrain::glsl_es::ground_visibility;

/// The symbols' tile properties have `is_halo` too.
const SYMBOL_NAMES: &[(&Block, &str, &str)] = &[(SymbolDrawableUBO::BLOCK, "is_halo", "u_is_halo_pass")];

/// The precisions of the icons' values, and their names.
const SYMBOL_ICON_DECLARATIONS: GlslEsDeclarations = GlslEsDeclarations {
    members: &[],
    attributes: &[(Low, &["opacity"])],
    highp_samplers: &["ground_depth"],
    names: SYMBOL_NAMES,
};

/// The precisions of the text's values, and their names.
const SYMBOL_SDF_DECLARATIONS: GlslEsDeclarations = GlslEsDeclarations {
    members: &[],
    attributes: &[(Low, &["halo_blur", "halo_width", "opacity"])],
    highp_samplers: &["ground_depth"],
    names: SYMBOL_NAMES,
};

/// `unpack_opacity`, a symbol's fade opacity from its packed attribute.
macro_rules! unpack_opacity {
    () => {
        r"
vec2 unpack_opacity(const float packedOpacity) {
    int intOpacity = int(packedOpacity) / 2;
    return vec2(float(intOpacity) / 127.0, mod(packedOpacity, 2.0));
}
"
    };
}

pub(crate) const SYMBOL_ICON: GlslEsSource = GlslEsSource {
    vertex: concat!(
        unpack_opacity!(),
        ground_visibility!(),
        r"out highp vec2 v_tex;
flat out highp vec2 v_tex_origin;
// The fade opacity, times the opacity when it's per vertex.
out float v_fade_opacity;

void main() {
    // How much of the symbol the ground in front of its anchor leaves.
#ifdef TERRAIN
    highp float ground_visible = ground_visibility(
        projectTileWithElevation(a_pos_offset.xy + u_translation, a_height_offset), u_world_size.y / u_pixel_ratio);
#else
    highp float ground_visible = 1.0;
#endif
    vec2 fade_opacity = unpack_opacity(a_fade_opacity);
    float fade_change = fade_opacity[1] > 0.5 ? u_symbol_fade_change : -u_symbol_fade_change;
    v_fade_opacity = max(0.0, min(ground_visible, fade_opacity[0] + fade_change));
#ifndef HAS_UNIFORM_u_opacity
    lowp float opacity = unpack_mix_vec2(a_opacity, u_opacity_t);
    v_fade_opacity *= opacity;
#endif

    // A quad that draws nothing goes to one point off screen, so it's never
    // rasterized. All four vertices of a quad share its fade and opacity.
    if (v_fade_opacity == 0.0) {
        gl_Position = vec4(-2.0, -2.0, -2.0, 1.0);
        return;
    }

    vec2 a_pos = a_pos_offset.xy;
    vec2 a_offset = a_pos_offset.zw;

    vec2 a_tex = a_data.xy;
    vec2 a_size = a_data.zw;

    float a_size_min = floor(a_size[0] * 0.5);
    vec2 a_pxoffset = a_pixeloffset.xy;
    vec2 a_minFontScale = a_pixeloffset.zw / 256.0;

    highp float segment_angle = -a_projected_pos[2];
    float size;

    if (u_is_size_zoom_constant == 0 && u_is_size_feature_constant == 0) {
        size = mix(a_size_min, a_size[1], u_size_t) / 128.0;
    } else if (u_is_size_zoom_constant != 0 && u_is_size_feature_constant == 0) {
        size = a_size_min / 128.0;
    } else {
        size = u_size;
    }

    vec2 translated_a_pos = a_pos + u_translation;
    vec4 projectedPoint = projectTileWithElevation(translated_a_pos, a_height_offset);
#ifdef PROJECTION_GLOBE
    // A screen-aligned label whose anchor is behind the horizon goes at
    // once, rather than fading out once placement next finds it occluded:
    // its clip Z is 0, so the horizon never clips it. A pitched label's
    // vertices go through the globe projection, which clips them.
    // A raised label is left to placement, which hides it once the planet
    // blocks the line of sight to it.
    if (u_pitch_with_map == 0 && a_height_offset <= 0.0 && projectedPoint.z > projectedPoint.w) {
        gl_Position = vec4(-2.0, -2.0, -2.0, 1.0);
        return;
    }
#endif
    highp float camera_to_anchor_distance = projectedPoint.w;
    // See comments in symbol_sdf.vertex
    highp float distance_ratio = u_pitch_with_map != 0 ?
        camera_to_anchor_distance / u_camera_to_center_distance :
        u_camera_to_center_distance / camera_to_anchor_distance;
    highp float perspective_ratio = clamp(
            0.5 + 0.5 * distance_ratio,
            0.0, // Prevents oversized near-field symbols in pitched/overzoomed tiles
            4.0);

    if (u_is_offset == 0) {
        size *= perspective_ratio;
    }

    float fontScale = u_is_text_prop != 0 ? size / 24.0 : size;

    highp float symbol_rotation = 0.0;
    if (u_rotate_symbol != 0) {
        // See comments in symbol_sdf.vertex
        vec4 offsetProjectedPoint = projectTileWithElevation(translated_a_pos + vec2(1, 0), a_height_offset);

        vec2 a = projectedPoint.xy / projectedPoint.w;
        vec2 b = offsetProjectedPoint.xy / offsetProjectedPoint.w;

        symbol_rotation = atan((b.y - a.y) / u_aspect_ratio, b.x - a.x);
    }

    highp float angle_sin = sin(segment_angle + symbol_rotation);
    highp float angle_cos = cos(segment_angle + symbol_rotation);
    mat2 rotation_matrix = mat2(angle_cos, -1.0 * angle_sin, angle_sin, angle_cos);

    vec4 projected_pos;
    if (u_is_along_line != 0 || u_is_variable_anchor != 0) {
        // A glyph along a line stands at its own height, which the label's
        // layout gives it; a label at a variable anchor at its anchor's.
        projected_pos = vec4(a_projected_pos.xy, u_is_along_line != 0 ? a_projected_pos.w : a_height_offset, 1.0);
    } else if (u_pitch_with_map != 0) {
        projected_pos = u_label_plane_matrix * vec4(a_projected_pos.xy + u_translation, a_height_offset, 1.0);
    } else {
        projected_pos = u_label_plane_matrix * projectTileWithElevation(a_projected_pos.xy + u_translation, a_height_offset);
    }
    float z = float(u_pitch_with_map) * projected_pos.z / projected_pos.w;

    float projectionScaling = 1.0;
#ifdef PROJECTION_GLOBE
    if (u_pitch_with_map != 0) {
        float anchor_pos_tile_y = (u_coord_matrix * vec4(projected_pos.xy / projected_pos.w, z, 1.0)).y;
        projectionScaling = mix(projectionScaling, 1.0 / circumferenceRatioAtTileY(anchor_pos_tile_y) * u_pitched_scale, projection.projection_transition);
    }
#endif

    gl_Position = u_coord_matrix * vec4(projected_pos.xy / projected_pos.w + rotation_matrix * (a_offset / 32.0 * max(a_minFontScale, fontScale) + a_pxoffset / 16.0) * projectionScaling, z, 1.0);
    if (u_pitch_with_map != 0) {
        gl_Position = projectTileWithElevation(gl_Position.xy, gl_Position.z);
    }

    v_tex = (a_tex - a_tex_origin) / u_texsize;
    v_tex_origin = a_tex_origin / u_texsize;
}
"
    ),
    fragment: r"in highp vec2 v_tex;
flat in highp vec2 v_tex_origin;
in float v_fade_opacity;

void main() {
#ifdef HAS_UNIFORM_u_opacity
    lowp float alpha = (u_is_text != 0 ? u_text_opacity : u_icon_opacity) * v_fade_opacity;
#else
    lowp float alpha = v_fade_opacity;
#endif
    fragColor = texture(u_texture, atlas_pos(v_tex_origin, v_tex)) * alpha;
}
",
    declarations: &SYMBOL_ICON_DECLARATIONS,
};

pub(crate) const SYMBOL_SDF: GlslEsSource = GlslEsSource {
    vertex: concat!(
        unpack_opacity!(),
        ground_visibility!(),
        r"#define SDF_PX 8.0

// contents of a_size vary based on the type of property value
// used for {text,icon}-size.
// For constants, a_size is disabled.
// For source functions, we bind only one value per vertex: the value of {text,icon}-size evaluated for the current feature.
// For composite functions:
// [ text-size(lowerZoomStop, feature),
//   text-size(upperZoomStop, feature) ]

out highp vec2 v_data0;
flat out highp vec2 v_tex_origin;
out vec3 v_data1;

#ifndef HAS_UNIFORM_u_fill_color
out highp vec4 fill_color;
#endif
#ifndef HAS_UNIFORM_u_halo_color
out highp vec4 halo_color;
#endif

void main() {
    highp vec4 u_fill_color = u_is_text_prop != 0 ? u_text_fill_color : u_icon_fill_color;
    highp vec4 u_halo_color = u_is_text_prop != 0 ? u_text_halo_color : u_icon_halo_color;
    highp float u_opacity = u_is_text_prop != 0 ? u_text_opacity : u_icon_opacity;
    highp float u_halo_width = u_is_text_prop != 0 ? u_text_halo_width : u_icon_halo_width;
    highp float u_halo_blur = u_is_text_prop != 0 ? u_text_halo_blur : u_icon_halo_blur;

    #ifndef HAS_UNIFORM_u_fill_color
fill_color = unpack_mix_color(a_fill_color, u_fill_color_t);
#else
highp vec4 fill_color = u_fill_color;
#endif
    #ifndef HAS_UNIFORM_u_halo_color
halo_color = unpack_mix_color(a_halo_color, u_halo_color_t);
#else
highp vec4 halo_color = u_halo_color;
#endif
    #ifndef HAS_UNIFORM_u_opacity
lowp float opacity = unpack_mix_vec2(a_opacity, u_opacity_t);
#else
lowp float opacity = u_opacity;
#endif
    #ifndef HAS_UNIFORM_u_halo_width
lowp float halo_width = unpack_mix_vec2(a_halo_width, u_halo_width_t);
#else
lowp float halo_width = u_halo_width;
#endif
    #ifndef HAS_UNIFORM_u_halo_blur
lowp float halo_blur = unpack_mix_vec2(a_halo_blur, u_halo_blur_t);
#else
lowp float halo_blur = u_halo_blur;
#endif

    // How much of the symbol the ground in front of its anchor leaves.
#ifdef TERRAIN
    highp float ground_visible = ground_visibility(
        projectTileWithElevation(a_pos_offset.xy + u_translation, a_height_offset), u_world_size.y / u_pixel_ratio);
#else
    highp float ground_visible = 1.0;
#endif
    vec2 fade_opacity = unpack_opacity(a_fade_opacity);
    float fade_change = fade_opacity[1] > 0.5 ? u_symbol_fade_change : -u_symbol_fade_change;
    float interpolated_fade_opacity = max(0.0, min(ground_visible, fade_opacity[0] + fade_change));

    // A quad that draws nothing goes to one point off screen, so it's never
    // rasterized. All four vertices of a quad share its fade and opacity.
    if (interpolated_fade_opacity == 0.0 || opacity == 0.0) {
        gl_Position = vec4(-2.0, -2.0, -2.0, 1.0);
        return;
    }

    vec2 a_pos = a_pos_offset.xy;
    vec2 a_offset = a_pos_offset.zw;

    vec2 a_tex = a_data.xy;
    vec2 a_size = a_data.zw;

    float a_size_min = floor(a_size[0] * 0.5);
    vec2 a_pxoffset = a_pixeloffset.xy;

    highp float segment_angle = -a_projected_pos[2];
    float size;

    if (u_is_size_zoom_constant == 0 && u_is_size_feature_constant == 0) {
        size = mix(a_size_min, a_size[1], u_size_t) / 128.0;
    } else if (u_is_size_zoom_constant != 0 && u_is_size_feature_constant == 0) {
        size = a_size_min / 128.0;
    } else {
        size = u_size;
    }

    vec2 translated_a_pos = a_pos + u_translation;
    vec4 projectedPoint = projectTileWithElevation(translated_a_pos, a_height_offset);
#ifdef PROJECTION_GLOBE
    // A screen-aligned label whose anchor is behind the horizon goes at
    // once, rather than fading out once placement next finds it occluded:
    // its clip Z is 0, so the horizon never clips it. A pitched label's
    // vertices go through the globe projection, which clips them.
    // A raised label is left to placement, which hides it once the planet
    // blocks the line of sight to it.
    if (u_pitch_with_map == 0 && a_height_offset <= 0.0 && projectedPoint.z > projectedPoint.w) {
        gl_Position = vec4(-2.0, -2.0, -2.0, 1.0);
        return;
    }
#endif
    highp float camera_to_anchor_distance = projectedPoint.w;
    // If the label is pitched with the map, layout is done in pitched space,
    // which makes labels in the distance smaller relative to viewport space.
    // We counteract part of that effect by multiplying by the perspective ratio.
    // If the label isn't pitched with the map, we do layout in viewport space,
    // which makes labels in the distance larger relative to the features around
    // them. We counteract part of that effect by dividing by the perspective ratio.
    highp float distance_ratio = u_pitch_with_map != 0 ?
        camera_to_anchor_distance / u_camera_to_center_distance :
        u_camera_to_center_distance / camera_to_anchor_distance;
    highp float perspective_ratio = clamp(
        0.5 + 0.5 * distance_ratio,
        0.0, // Prevents oversized near-field symbols in pitched/overzoomed tiles
        4.0);

    if (u_is_offset == 0) {
        size *= perspective_ratio;
    }

    float fontScale = u_is_text_prop != 0 ? size / 24.0 : size;

    highp float symbol_rotation = 0.0;
    if (u_rotate_symbol != 0) {
        // Point labels with 'rotation-alignment: map' are horizontal with respect to tile units
        // To figure out that angle in projected space, we draw a short horizontal line in tile
        // space, project it, and measure its angle in projected space.
        vec4 offsetProjectedPoint = projectTileWithElevation(translated_a_pos + vec2(1, 0), a_height_offset);

        vec2 a = projectedPoint.xy / projectedPoint.w;
        vec2 b = offsetProjectedPoint.xy / offsetProjectedPoint.w;

        symbol_rotation = atan((b.y - a.y) / u_aspect_ratio, b.x - a.x);
    }

    highp float angle_sin = sin(segment_angle + symbol_rotation);
    highp float angle_cos = cos(segment_angle + symbol_rotation);
    mat2 rotation_matrix = mat2(angle_cos, -1.0 * angle_sin, angle_sin, angle_cos);

    vec4 projected_pos;
    if (u_is_along_line != 0 || u_is_variable_anchor != 0) {
        // A glyph along a line stands at its own height, which the label's
        // layout gives it; a label at a variable anchor at its anchor's.
        projected_pos = vec4(a_projected_pos.xy, u_is_along_line != 0 ? a_projected_pos.w : a_height_offset, 1.0);
    } else if (u_pitch_with_map != 0) {
        projected_pos = u_label_plane_matrix * vec4(a_projected_pos.xy + u_translation, a_height_offset, 1.0);
    } else {
        projected_pos = u_label_plane_matrix * projectTileWithElevation(a_projected_pos.xy + u_translation, a_height_offset);
    }
    float z = float(u_pitch_with_map) * projected_pos.z / projected_pos.w;

    float projectionScaling = 1.0;
#ifdef PROJECTION_GLOBE
    if (u_pitch_with_map != 0) {
        float anchor_pos_tile_y = (u_coord_matrix * vec4(projected_pos.xy / projected_pos.w, z, 1.0)).y;
        projectionScaling = mix(projectionScaling, 1.0 / circumferenceRatioAtTileY(anchor_pos_tile_y) * u_pitched_scale, projection.projection_transition);
    }
#endif

    gl_Position = u_coord_matrix * vec4(projected_pos.xy / projected_pos.w + rotation_matrix * (a_offset / 32.0 * fontScale + a_pxoffset) * projectionScaling, z, 1.0);
    if (u_pitch_with_map != 0) {
        gl_Position = projectTileWithElevation(gl_Position.xy, gl_Position.z);
    }
    // A glyph's edge's gamma, scaled by clip w, and the inner edge of its
    // halo: the same over a quad but for the clip w, which interpolates
    // linearly.
    float EDGE_GAMMA = 0.105 / DEVICE_PIXEL_RATIO;
    float halo_blur_gamma = u_is_halo_pass != 0 ? halo_blur * 1.19 / SDF_PX : 0.0;
    float gamma_scaled = (halo_blur_gamma + EDGE_GAMMA) / (fontScale * u_gamma_scale) * gl_Position.w;
    float halo_edge = (6.0 - halo_width / fontScale) / SDF_PX;
#ifndef HAS_UNIFORM_u_opacity
    interpolated_fade_opacity *= opacity;
#endif

    v_data0 = (a_tex - a_tex_origin) / u_texsize;
    v_tex_origin = a_tex_origin / u_texsize;
    v_data1 = vec3(gamma_scaled, halo_edge, interpolated_fade_opacity);
}
"
    ),
    fragment: r"#define SDF_PX 8.0

in highp vec2 v_data0;
flat in highp vec2 v_tex_origin;
in vec3 v_data1;

#ifndef HAS_UNIFORM_u_fill_color
in highp vec4 fill_color;
#endif
#ifndef HAS_UNIFORM_u_halo_color
in highp vec4 halo_color;
#endif

void main() {
    highp vec4 u_fill_color = u_is_text != 0 ? u_text_fill_color : u_icon_fill_color;
    highp vec4 u_halo_color = u_is_text != 0 ? u_text_halo_color : u_icon_halo_color;
    highp float u_opacity = u_is_text != 0 ? u_text_opacity : u_icon_opacity;

    #ifdef HAS_UNIFORM_u_fill_color
highp vec4 fill_color = u_fill_color;
#endif
    #ifdef HAS_UNIFORM_u_halo_color
highp vec4 halo_color = u_halo_color;
#endif
#ifdef HAS_UNIFORM_u_opacity
    lowp float opacity = u_opacity;
#else
    lowp float opacity = 1.0;
#endif

    highp vec2 tex = atlas_pos(v_tex_origin, v_data0.xy);
    float gamma_scaled = v_data1.x;
    float halo_edge = v_data1.y;
    float fade_opacity = v_data1.z;

    lowp vec4 color = fill_color;
    lowp float inner_edge = (256.0 - 64.0) / 256.0;
    if (u_is_halo != 0) {
        color = halo_color;
        inner_edge = inner_edge + gamma_scaled;
    }

    lowp float dist = texture(u_texture, tex).a;
    highp float alpha = smoothstep(inner_edge - gamma_scaled, inner_edge + gamma_scaled, dist);
    if (u_is_halo != 0) {
        // When drawing halos, we want the inside of the halo to be transparent as well
        // in case the text fill is transparent.
        alpha = min(smoothstep(halo_edge - gamma_scaled, halo_edge + gamma_scaled, dist), 1.0 - alpha);
    }

    fragColor = color * (alpha * opacity * fade_opacity);
}
",
    declarations: &SYMBOL_SDF_DECLARATIONS,
};

pub(crate) const SYMBOL_TEXT_AND_ICON: GlslEsSource = GlslEsSource {
    vertex: concat!(
        unpack_opacity!(),
        ground_visibility!(),
        r"#define SDF_PX 8.0
#define ICON 0.0

// contents of a_size vary based on the type of property value
// used for {text,icon}-size.
// For constants, a_size is disabled.
// For source functions, we bind only one value per vertex: the value of {text,icon}-size evaluated for the current feature.
// For composite functions:
// [ text-size(lowerZoomStop, feature),
//   text-size(upperZoomStop, feature) ]

out highp vec2 v_data0;
flat out highp vec2 v_tex_origin;
out vec3 v_data1;
flat out lowp int v_is_icon;

#ifndef HAS_UNIFORM_u_fill_color
out highp vec4 fill_color;
#endif
#ifndef HAS_UNIFORM_u_halo_color
out highp vec4 halo_color;
#endif

void main() {
    highp vec4 u_fill_color = u_is_text_prop != 0 ? u_text_fill_color : u_icon_fill_color;
    highp vec4 u_halo_color = u_is_text_prop != 0 ? u_text_halo_color : u_icon_halo_color;
    highp float u_opacity = u_is_text_prop != 0 ? u_text_opacity : u_icon_opacity;
    highp float u_halo_width = u_is_text_prop != 0 ? u_text_halo_width : u_icon_halo_width;
    highp float u_halo_blur = u_is_text_prop != 0 ? u_text_halo_blur : u_icon_halo_blur;

    #ifndef HAS_UNIFORM_u_fill_color
fill_color = unpack_mix_color(a_fill_color, u_fill_color_t);
#else
highp vec4 fill_color = u_fill_color;
#endif
    #ifndef HAS_UNIFORM_u_halo_color
halo_color = unpack_mix_color(a_halo_color, u_halo_color_t);
#else
highp vec4 halo_color = u_halo_color;
#endif
    #ifndef HAS_UNIFORM_u_opacity
lowp float opacity = unpack_mix_vec2(a_opacity, u_opacity_t);
#else
lowp float opacity = u_opacity;
#endif
    #ifndef HAS_UNIFORM_u_halo_width
lowp float halo_width = unpack_mix_vec2(a_halo_width, u_halo_width_t);
#else
lowp float halo_width = u_halo_width;
#endif
    #ifndef HAS_UNIFORM_u_halo_blur
lowp float halo_blur = unpack_mix_vec2(a_halo_blur, u_halo_blur_t);
#else
lowp float halo_blur = u_halo_blur;
#endif

    // How much of the symbol the ground in front of its anchor leaves.
#ifdef TERRAIN
    highp float ground_visible = ground_visibility(
        projectTileWithElevation(a_pos_offset.xy + u_translation, a_height_offset), u_world_size.y / u_pixel_ratio);
#else
    highp float ground_visible = 1.0;
#endif
    vec2 fade_opacity = unpack_opacity(a_fade_opacity);
    float fade_change = fade_opacity[1] > 0.5 ? u_symbol_fade_change : -u_symbol_fade_change;
    float interpolated_fade_opacity = max(0.0, min(ground_visible, fade_opacity[0] + fade_change));

    // A quad that draws nothing goes to one point off screen, so it's never
    // rasterized. All four vertices of a quad share its fade and opacity.
    if (interpolated_fade_opacity == 0.0 || opacity == 0.0) {
        gl_Position = vec4(-2.0, -2.0, -2.0, 1.0);
        return;
    }

    vec2 a_pos = a_pos_offset.xy;
    vec2 a_offset = a_pos_offset.zw;

    vec2 a_tex = a_data.xy;
    vec2 a_size = a_data.zw;

    float a_size_min = floor(a_size[0] * 0.5);
    float is_sdf = a_size[0] - 2.0 * a_size_min;

    highp float segment_angle = -a_projected_pos[2];
    float size;

    if (u_is_size_zoom_constant == 0 && u_is_size_feature_constant == 0) {
        size = mix(a_size_min, a_size[1], u_size_t) / 128.0;
    } else if (u_is_size_zoom_constant != 0 && u_is_size_feature_constant == 0) {
        size = a_size_min / 128.0;
    } else {
        size = u_size;
    }

    vec2 translated_a_pos = a_pos + u_translation;
    vec4 projectedPoint = projectTileWithElevation(translated_a_pos, a_height_offset);
#ifdef PROJECTION_GLOBE
    // A screen-aligned label whose anchor is behind the horizon goes at
    // once, rather than fading out once placement next finds it occluded:
    // its clip Z is 0, so the horizon never clips it. A pitched label's
    // vertices go through the globe projection, which clips them.
    // A raised label is left to placement, which hides it once the planet
    // blocks the line of sight to it.
    if (u_pitch_with_map == 0 && a_height_offset <= 0.0 && projectedPoint.z > projectedPoint.w) {
        gl_Position = vec4(-2.0, -2.0, -2.0, 1.0);
        return;
    }
#endif
    highp float camera_to_anchor_distance = projectedPoint.w;
    // If the label is pitched with the map, layout is done in pitched space,
    // which makes labels in the distance smaller relative to viewport space.
    // We counteract part of that effect by multiplying by the perspective ratio.
    // If the label isn't pitched with the map, we do layout in viewport space,
    // which makes labels in the distance larger relative to the features around
    // them. We counteract part of that effect by dividing by the perspective ratio.
    highp float distance_ratio = u_pitch_with_map != 0 ?
        camera_to_anchor_distance / u_camera_to_center_distance :
        u_camera_to_center_distance / camera_to_anchor_distance;
    highp float perspective_ratio = clamp(
        0.5 + 0.5 * distance_ratio,
        0.0, // Prevents oversized near-field symbols in pitched/overzoomed tiles
        4.0);

    if (u_is_offset == 0) {
        size *= perspective_ratio;
    }

    float fontScale = size / 24.0;

    highp float symbol_rotation = 0.0;
    if (u_rotate_symbol != 0) {
        // Point labels with 'rotation-alignment: map' are horizontal with respect to tile units
        // To figure out that angle in projected space, we draw a short horizontal line in tile
        // space, project it, and measure its angle in projected space.
        vec4 offsetProjectedPoint = projectTileWithElevation(translated_a_pos + vec2(1, 0), a_height_offset);

        vec2 a = projectedPoint.xy / projectedPoint.w;
        vec2 b = offsetProjectedPoint.xy / offsetProjectedPoint.w;

        symbol_rotation = atan((b.y - a.y) / u_aspect_ratio, b.x - a.x);
    }

    highp float angle_sin = sin(segment_angle + symbol_rotation);
    highp float angle_cos = cos(segment_angle + symbol_rotation);
    mat2 rotation_matrix = mat2(angle_cos, -1.0 * angle_sin, angle_sin, angle_cos);

    vec4 projected_pos;
    if (u_is_along_line != 0 || u_is_variable_anchor != 0) {
        // A glyph along a line stands at its own height, which the label's
        // layout gives it; a label at a variable anchor at its anchor's.
        projected_pos = vec4(a_projected_pos.xy, u_is_along_line != 0 ? a_projected_pos.w : a_height_offset, 1.0);
    } else if (u_pitch_with_map != 0) {
        projected_pos = u_label_plane_matrix * vec4(a_projected_pos.xy + u_translation, a_height_offset, 1.0);
    } else {
        projected_pos = u_label_plane_matrix * projectTileWithElevation(a_projected_pos.xy + u_translation, a_height_offset);
    }
    float z = float(u_pitch_with_map) * projected_pos.z / projected_pos.w;

    float projectionScaling = 1.0;
#ifdef PROJECTION_GLOBE
    if (u_pitch_with_map != 0 && u_is_along_line == 0) {
        float anchor_pos_tile_y = (u_coord_matrix * vec4(projected_pos.xy / projected_pos.w, z, 1.0)).y;
        projectionScaling = mix(projectionScaling, 1.0 / circumferenceRatioAtTileY(anchor_pos_tile_y) * u_pitched_scale, projection.projection_transition);
    }
#endif

    gl_Position = u_coord_matrix * vec4(projected_pos.xy / projected_pos.w + rotation_matrix * (a_offset / 32.0 * fontScale) * projectionScaling, z, 1.0);
    if (u_pitch_with_map != 0) {
        gl_Position = projectTileWithElevation(gl_Position.xy, gl_Position.z);
    }
    // A glyph's edge's gamma, scaled by clip w, and the inner edge of its
    // halo: the same over a quad but for the clip w, which interpolates
    // linearly.
    float EDGE_GAMMA = 0.105 / DEVICE_PIXEL_RATIO;
    float halo_blur_gamma = u_is_halo_pass != 0 ? halo_blur * 1.19 / SDF_PX : 0.0;
    float gamma_scaled = (halo_blur_gamma + EDGE_GAMMA) / (fontScale * u_gamma_scale) * gl_Position.w;
    float halo_edge = (6.0 - halo_width / fontScale) / SDF_PX;
#ifndef HAS_UNIFORM_u_opacity
    interpolated_fade_opacity *= opacity;
#endif

    bool is_icon = is_sdf == ICON;
    vec2 texsize = is_icon ? u_texsize_icon : u_texsize;
    v_data0 = (a_tex - a_tex_origin) / texsize;
    v_tex_origin = a_tex_origin / texsize;
    v_data1 = vec3(gamma_scaled, halo_edge, interpolated_fade_opacity);
    v_is_icon = int(is_icon);
}
"
    ),
    fragment: r"#define SDF_PX 8.0

in highp vec2 v_data0;
flat in highp vec2 v_tex_origin;
in vec3 v_data1;
flat in lowp int v_is_icon;

#ifndef HAS_UNIFORM_u_fill_color
in highp vec4 fill_color;
#endif
#ifndef HAS_UNIFORM_u_halo_color
in highp vec4 halo_color;
#endif

void main() {
    highp vec4 u_fill_color = u_is_text != 0 ? u_text_fill_color : u_icon_fill_color;
    highp vec4 u_halo_color = u_is_text != 0 ? u_text_halo_color : u_icon_halo_color;
    highp float u_opacity = u_is_text != 0 ? u_text_opacity : u_icon_opacity;

    #ifdef HAS_UNIFORM_u_fill_color
highp vec4 fill_color = u_fill_color;
#endif
    #ifdef HAS_UNIFORM_u_halo_color
highp vec4 halo_color = u_halo_color;
#endif
#ifdef HAS_UNIFORM_u_opacity
    lowp float opacity = u_opacity;
#else
    lowp float opacity = 1.0;
#endif

    float gamma_scaled = v_data1.x;
    float halo_edge = v_data1.y;
    float fade_opacity = v_data1.z;

    highp vec2 tex = atlas_pos(v_tex_origin, v_data0);

    if (v_is_icon != 0) {
        lowp float alpha = opacity * fade_opacity;
        fragColor = texture(u_texture_icon, tex) * alpha;

        return;
    }

    lowp vec4 color = fill_color;
    lowp float inner_edge = (256.0 - 64.0) / 256.0;
    if (u_is_halo != 0) {
        color = halo_color;
        inner_edge = inner_edge + gamma_scaled;
    }

    lowp float dist = texture(u_texture, tex).a;
    highp float alpha = smoothstep(inner_edge - gamma_scaled, inner_edge + gamma_scaled, dist);
    if (u_is_halo != 0) {
        // When drawing halos, we want the inside of the halo to be transparent as well
        // in case the text fill is transparent.
        alpha = min(smoothstep(halo_edge - gamma_scaled, halo_edge + gamma_scaled, dist), 1.0 - alpha);
    }

    fragColor = color * (alpha * opacity * fade_opacity);
}
",
    declarations: &SYMBOL_SDF_DECLARATIONS,
};