-
-
Notifications
You must be signed in to change notification settings - Fork 561
Expand file tree
/
Copy pathsymbol.hpp
More file actions
895 lines (768 loc) · 34.1 KB
/
Copy pathsymbol.hpp
File metadata and controls
895 lines (768 loc) · 34.1 KB
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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
#pragma once
#include <mbgl/shaders/symbol_layer_ubo.hpp>
#include <mbgl/shaders/shader_source.hpp>
#include <mbgl/shaders/mtl/shader_program.hpp>
namespace mbgl {
namespace shaders {
constexpr auto symbolShaderPrelude = R"(
enum {
idSymbolDrawableUBO = idDrawableReservedVertexOnlyUBO,
idSymbolTilePropsUBO = idDrawableReservedFragmentOnlyUBO,
idSymbolEvaluatedPropsUBO = drawableReservedUBOCount,
symbolUBOCount
};
struct alignas(16) SymbolDrawableUBO {
/* 0 */ float4x4 matrix;
/* 64 */ float4x4 label_plane_matrix;
/* 128 */ float4x4 coord_matrix;
/* 192 */ float2 texsize;
/* 200 */ float2 texsize_icon;
/* 208 */ /*bool*/ int is_text_prop;
/* 212 */ /*bool*/ int rotate_symbol;
/* 216 */ /*bool*/ int pitch_with_map;
/* 220 */ /*bool*/ int is_size_zoom_constant;
/* 224 */ /*bool*/ int is_size_feature_constant;
/* 228 */ /*bool*/ int is_offset;
/* 232 */ float size_t;
/* 236 */ float size;
// Interpolations
/* 240 */ float fill_color_t;
/* 244 */ float halo_color_t;
/* 248 */ float opacity_t;
/* 252 */ float halo_width_t;
/* 256 */ float halo_blur_t;
/* 260 */
};
static_assert(sizeof(SymbolDrawableUBO) == 17 * 16, "wrong size");
struct alignas(16) SymbolTilePropsUBO {
/* 0 */ /*bool*/ int is_text;
/* 4 */ /*bool*/ int is_halo;
/* 8 */ float gamma_scale;
/* 12 */ float pad1;
/* 16 */
};
static_assert(sizeof(SymbolTilePropsUBO) == 16, "wrong size");
/// Evaluated properties that do not depend on the tile
struct alignas(16) SymbolEvaluatedPropsUBO {
/* 0 */ float4 text_fill_color;
/* 16 */ float4 text_halo_color;
/* 32 */ float text_opacity;
/* 36 */ float text_halo_width;
/* 40 */ float text_halo_blur;
/* 44 */ float pad1;
/* 48 */ float4 icon_fill_color;
/* 64 */ float4 icon_halo_color;
/* 80 */ float icon_opacity;
/* 84 */ float icon_halo_width;
/* 88 */ float icon_halo_blur;
/* 92 */ float pad2;
/* 96 */
};
static_assert(sizeof(SymbolEvaluatedPropsUBO) == 6 * 16, "wrong size");
#define c_offscreen_degenerate_triangle_location -2.0
)";
template <>
struct ShaderSource<BuiltIn::SymbolIconShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "SymbolIconShader";
static constexpr auto vertexMainFunction = "vertexMain";
static constexpr auto fragmentMainFunction = "fragmentMain";
static const std::array<AttributeInfo, 1> attributes;
static const std::array<AttributeInfo, 10> instanceAttributes;
static const std::array<TextureInfo, 1> textures;
static constexpr auto prelude = symbolShaderPrelude;
static constexpr auto source = R"(
struct VertexStage {
float2 pos [[attribute(0)]];
ushort instance [[attribute(1)]];
//float4 pos_scale [[attribute(1)]];
//float4 offset_tltr [[attribute(2)]];
//float4 offset_blbr [[attribute(3)]];
//float4 texture_rect [[attribute(4)]];
//float4 pixeloffset [[attribute(5)]];
//float2 size_sdf [[attribute(6)]];
//float3 projected_pos [[attribute(7)]];
//float fade_opacity [[attribute(8)]];
#if !defined(HAS_UNIFORM_u_opacity)
//float2 opacity [[attribute(9)]];
#endif
};
struct SymbolInstance {
short2 pos_scale[2];
short2 offset_tltr[2];
short2 offset_blbr[2];
ushort2 texture_rect[2];
short2 pixeloffset[2];
ushort2 size_sdf;
};
struct DynamicInstance {
float projected_pos[3];
};
struct OpacityInstance {
float fade_opacity;
};
struct DataInstance {
#if !defined(HAS_UNIFORM_u_opacity)
float opacity[2];
#endif
};
struct FragmentStage {
float4 position [[position, invariant]];
half2 tex;
#if defined(HAS_UNIFORM_u_opacity)
// We only need to pass `fade_opacity` separately if opacity is a
// uniform, otherwise it's multiplied into fragment opacity, below.
half fade_opacity;
#else
half opacity;
#endif
};
FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]],
device const GlobalPaintParamsUBO& paintParams [[buffer(idGlobalPaintParamsUBO)]],
device const uint32_t& uboIndex [[buffer(idGlobalUBOIndex)]],
device const SymbolDrawableUBO* drawableVector [[buffer(idSymbolDrawableUBO)]],
uint instanceID [[ instance_id ]],
device const SymbolInstance* symbolInstances [[buffer(symbolUBOCount + 2)]],
device const DynamicInstance* dynamicInstances [[buffer(symbolUBOCount + 3)]],
device const OpacityInstance* opacityInstances [[buffer(symbolUBOCount + 4)]],
device const DataInstance* dataInstances [[buffer(symbolUBOCount + 5)]]) {
device const SymbolDrawableUBO& drawable = drawableVector[uboIndex];
device const SymbolInstance& symbol = symbolInstances[vertx.instance];
device const DynamicInstance& dynamic = dynamicInstances[vertx.instance];
device const OpacityInstance& opacity = opacityInstances[vertx.instance];
device const DataInstance& data = dataInstances[vertx.instance];
const float2 raw_fade_opacity = unpack_opacity(opacity.fade_opacity);
const float fade_change = raw_fade_opacity[1] > 0.5 ? paintParams.symbol_fade_change : -paintParams.symbol_fade_change;
const float fade_opacity = max(0.0, min(1.0, raw_fade_opacity[0] + fade_change));
#if defined(HAS_UNIFORM_u_opacity)
const half fo = half(fade_opacity);
#else
const half fo = half(unpack_mix_float(data.opacity, drawable.opacity_t) * fade_opacity);
#endif
// This will check to see if the opacity is zero and push the triangle offscreen if it is
// so the GPU will cull the vertex and never send it to the fragment shader
if (fo == 0.0) {
return {
.position = float4(c_offscreen_degenerate_triangle_location,
c_offscreen_degenerate_triangle_location,
c_offscreen_degenerate_triangle_location, 1.0),
};
}
const float2 a_pos = float2(symbol.pos_scale[0].xy);
float2 a_offset;
if (vertx.pos.x == 0) {
if (vertx.pos.y == 0) {
a_offset = float2(symbol.offset_tltr[0].xy); //tl
} else {
a_offset = float2(symbol.offset_blbr[0].xy); //bl
}
} else {
if (vertx.pos.y == 0) {
a_offset = float2(symbol.offset_tltr[1].xy); //tr
} else {
a_offset = float2(symbol.offset_blbr[1].xy); //br
}
}
const float2 a_tex = float2(symbol.texture_rect[0].xy) + vertx.pos * float2(symbol.texture_rect[1].xy);
const float2 a_size = float2(symbol.size_sdf);
const float a_size_min = floor(a_size[0] * 0.5);
const float2 a_pxoffset = float2(symbol.pixeloffset[0].xy) + vertx.pos * float2(symbol.pixeloffset[1].xy - symbol.pixeloffset[0].xy);
const float2 a_minFontScale = float2(symbol.pos_scale[1].xy) / 256.0;
const float segment_angle = -dynamic.projected_pos[2];
float size;
if (!drawable.is_size_zoom_constant && !drawable.is_size_feature_constant) {
size = mix(a_size_min, a_size[1], drawable.size_t) / 128.0;
} else if (drawable.is_size_zoom_constant && !drawable.is_size_feature_constant) {
size = a_size_min / 128.0;
} else {
size = drawable.size;
}
const float4 projectedPoint = drawable.matrix * float4(a_pos, 0, 1);
const float camera_to_anchor_distance = projectedPoint.w;
// See comments in symbol_sdf.vertex
const float distance_ratio = drawable.pitch_with_map ?
camera_to_anchor_distance / paintParams.camera_to_center_distance :
paintParams.camera_to_center_distance / camera_to_anchor_distance;
const float perspective_ratio = clamp(
0.5 + 0.5 * distance_ratio,
0.0, // Prevents oversized near-field symbols in pitched/overzoomed tiles
4.0);
if (!drawable.is_offset) {
size *= perspective_ratio;
}
const float fontScale = drawable.is_text_prop ? size / 24.0 : size;
float symbol_rotation = 0.0;
if (drawable.rotate_symbol) {
// See comments in symbol_sdf.vertex
const float4 offsetProjectedPoint = drawable.matrix * float4(a_pos + float2(1, 0), 0, 1);
const float2 a = projectedPoint.xy / projectedPoint.w;
const float2 b = offsetProjectedPoint.xy / offsetProjectedPoint.w;
symbol_rotation = atan2((b.y - a.y) / paintParams.aspect_ratio, b.x - a.x);
}
const float angle_sin = sin(segment_angle + symbol_rotation);
const float angle_cos = cos(segment_angle + symbol_rotation);
const float2x2 rotation_matrix = float2x2(angle_cos, -1.0 * angle_sin, angle_sin, angle_cos);
const float4 projected_pos = drawable.label_plane_matrix * float4(dynamic.projected_pos[0], dynamic.projected_pos[1], 0.0, 1.0);
const float2 pos0 = projected_pos.xy / projected_pos.w;
const float2 posOffset = a_offset * max(a_minFontScale, fontScale) / 32.0 + a_pxoffset / 16.0;
const float4 position = drawable.coord_matrix * float4(pos0 + rotation_matrix * posOffset, 0.0, 1.0);
return {
.position = position,
.tex = half2(a_tex / drawable.texsize),
#if defined(HAS_UNIFORM_u_opacity)
.fade_opacity = fo,
#else
.opacity = fo,
#endif
};
}
half4 fragment fragmentMain(FragmentStage in [[stage_in]],
device const uint32_t& uboIndex [[buffer(idGlobalUBOIndex)]],
device const SymbolTilePropsUBO* tilePropsVector [[buffer(idSymbolTilePropsUBO)]],
device const SymbolEvaluatedPropsUBO& props [[buffer(idSymbolEvaluatedPropsUBO)]],
texture2d<float, access::sample> image [[texture(0)]],
sampler image_sampler [[sampler(0)]]) {
#if defined(OVERDRAW_INSPECTOR)
return half4(1.0);
#endif
device const SymbolTilePropsUBO& tileProps = tilePropsVector[uboIndex];
#if defined(HAS_UNIFORM_u_opacity)
const float opacity = (tileProps.is_text ? props.text_opacity : props.icon_opacity) * in.fade_opacity;
#else
const float opacity = in.opacity; // fade_opacity is baked in for this case
#endif
return half4(image.sample(image_sampler, float2(in.tex)) * opacity);
}
)";
};
template <>
struct ShaderSource<BuiltIn::SymbolSDFShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "SymbolSDFShader";
static constexpr auto vertexMainFunction = "vertexMain";
static constexpr auto fragmentMainFunction = "fragmentMain";
static const std::array<AttributeInfo, 1> attributes;
static const std::array<AttributeInfo, 14> instanceAttributes;
static const std::array<TextureInfo, 1> textures;
static constexpr auto prelude = symbolShaderPrelude;
static constexpr auto source = R"(
struct VertexStage {
float2 pos [[attribute(0)]];
ushort instance [[attribute(1)]];
//float4 pos_scale [[attribute(1)]];
//float4 offset_tltr [[attribute(2)]];
//float4 offset_blbr [[attribute(3)]];
//float4 texture_rect [[attribute(4)]];
//float4 pixeloffset [[attribute(5)]];
//float2 size_sdf [[attribute(6)]];
//float3 projected_pos [[attribute(7)]];
//float fade_opacity [[attribute(8)]];
#if !defined(HAS_UNIFORM_u_opacity)
//float2 opacity [[attribute(9)]];
#endif
#if !defined(HAS_UNIFORM_u_fill_color)
//float4 fill_color [[attribute(10)]];
#endif
#if !defined(HAS_UNIFORM_u_halo_color)
//float4 halo_color [[attribute(11)]];
#endif
#if !defined(HAS_UNIFORM_u_halo_width)
//float2 halo_width [[attribute(12)]];
#endif
#if !defined(HAS_UNIFORM_u_halo_blur)
//float2 halo_blur [[attribute(13)]];
#endif
};
struct SymbolInstance {
short2 pos_scale[2];
short2 offset_tltr[2];
short2 offset_blbr[2];
ushort2 texture_rect[2];
short2 pixeloffset[2];
ushort2 size_sdf;
};
struct DynamicInstance {
float projected_pos[3];
};
struct OpacityInstance {
float fade_opacity;
};
struct DataInstance {
#if !defined(HAS_UNIFORM_u_opacity)
float opacity[2];
#endif
#if !defined(HAS_UNIFORM_u_fill_color)
float fill_color[4];
#endif
#if !defined(HAS_UNIFORM_u_halo_color)
float halo_color[4];
#endif
#if !defined(HAS_UNIFORM_u_halo_width)
float halo_width[2];
#endif
#if !defined(HAS_UNIFORM_u_halo_blur)
float halo_blur[2];
#endif
};
struct FragmentStage {
float4 position [[position, invariant]];
#if !defined(HAS_UNIFORM_u_fill_color)
half4 fill_color;
#endif
#if !defined(HAS_UNIFORM_u_halo_color)
half4 halo_color;
#endif
half2 tex;
half gamma_scale;
half fontScale;
half fade_opacity;
#if !defined(HAS_UNIFORM_u_opacity)
half opacity;
#endif
#if !defined(HAS_UNIFORM_u_halo_width)
half halo_width;
#endif
#if !defined(HAS_UNIFORM_u_halo_blur)
half halo_blur;
#endif
};
FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]],
device const GlobalPaintParamsUBO& paintParams [[buffer(idGlobalPaintParamsUBO)]],
device const uint32_t& uboIndex [[buffer(idGlobalUBOIndex)]],
device const SymbolDrawableUBO* drawableVector [[buffer(idSymbolDrawableUBO)]],
uint instanceID [[ instance_id ]],
device const SymbolInstance* symbolInstances [[buffer(symbolUBOCount + 2)]],
device const DynamicInstance* dynamicInstances [[buffer(symbolUBOCount + 3)]],
device const OpacityInstance* opacityInstances [[buffer(symbolUBOCount + 4)]],
device const DataInstance* dataInstances [[buffer(symbolUBOCount + 5)]]) {
device const SymbolDrawableUBO& drawable = drawableVector[uboIndex];
device const SymbolInstance& symbol = symbolInstances[vertx.instance];
device const DynamicInstance& dynamic = dynamicInstances[vertx.instance];
device const OpacityInstance& opacity = opacityInstances[vertx.instance];
device const DataInstance& data = dataInstances[vertx.instance];
const float2 fade_opacity = unpack_opacity(opacity.fade_opacity);
const float fade_change = (fade_opacity[1] > 0.5) ? paintParams.symbol_fade_change : -paintParams.symbol_fade_change;
const half fo = half(max(0.0, min(1.0, fade_opacity[0] + fade_change)));
// This will check to see if the opacity is zero and push the triangle offscreen if it is
// so the GPU will cull the vertex and never send it to the fragment shader
if (fo == 0.0) {
return {
.position = float4(c_offscreen_degenerate_triangle_location,
c_offscreen_degenerate_triangle_location,
c_offscreen_degenerate_triangle_location, 1.0),
};
}
const float2 a_pos = float2(symbol.pos_scale[0].xy);
float2 a_offset;
if (vertx.pos.x == 0) {
if (vertx.pos.y == 0) {
a_offset = float2(symbol.offset_tltr[0].xy); //tl
} else {
a_offset = float2(symbol.offset_blbr[0].xy); //bl
}
} else {
if (vertx.pos.y == 0) {
a_offset = float2(symbol.offset_tltr[1].xy); //tr
} else {
a_offset = float2(symbol.offset_blbr[1].xy); //br
}
}
const float2 a_tex = float2(symbol.texture_rect[0].xy) + vertx.pos * float2(symbol.texture_rect[1].xy);
const float2 a_size = float2(symbol.size_sdf);
const float a_size_min = floor(a_size[0] * 0.5);
const float2 a_pxoffset = float2(symbol.pixeloffset[0].xy) + vertx.pos * float2(symbol.pixeloffset[1].xy - symbol.pixeloffset[0].xy);
const float segment_angle = -dynamic.projected_pos[2];
float size;
if (!drawable.is_size_zoom_constant && !drawable.is_size_feature_constant) {
size = mix(a_size_min, a_size[1], drawable.size_t) / 128.0;
} else if (drawable.is_size_zoom_constant && !drawable.is_size_feature_constant) {
size = a_size_min / 128.0;
} else {
size = drawable.size;
}
const float4 projectedPoint = drawable.matrix * float4(a_pos, 0, 1);
const 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.
const float distance_ratio = drawable.pitch_with_map ?
camera_to_anchor_distance / paintParams.camera_to_center_distance :
paintParams.camera_to_center_distance / camera_to_anchor_distance;
const float perspective_ratio = clamp(
0.5 + 0.5 * distance_ratio,
0.0, // Prevents oversized near-field symbols in pitched/overzoomed tiles
4.0);
if (!drawable.is_offset) {
size *= perspective_ratio;
}
const float fontScale = drawable.is_text_prop ? size / 24.0 : size;
float symbol_rotation = 0.0;
if (drawable.rotate_symbol) {
// 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.
const float4 offsetProjectedPoint = drawable.matrix * float4(a_pos + float2(1, 0), 0, 1);
const float2 a = projectedPoint.xy / projectedPoint.w;
const float2 b = offsetProjectedPoint.xy / offsetProjectedPoint.w;
symbol_rotation = atan2((b.y - a.y) / paintParams.aspect_ratio, b.x - a.x);
}
const float angle_sin = sin(segment_angle + symbol_rotation);
const float angle_cos = cos(segment_angle + symbol_rotation);
const auto rotation_matrix = float2x2(angle_cos, -1.0 * angle_sin, angle_sin, angle_cos);
const float4 projected_pos = drawable.label_plane_matrix * float4(dynamic.projected_pos[0], dynamic.projected_pos[1], 0.0, 1.0);
const float2 pos_rot = a_offset / 32.0 * fontScale + a_pxoffset;
const float2 pos0 = projected_pos.xy / projected_pos.w + rotation_matrix * pos_rot;
const float4 position = drawable.coord_matrix * float4(pos0, 0.0, 1.0);
return {
.position = position,
#if !defined(HAS_UNIFORM_u_fill_color)
.fill_color = half4(unpack_mix_color(data.fill_color, drawable.fill_color_t)),
#endif
#if !defined(HAS_UNIFORM_u_halo_color)
.halo_color = half4(unpack_mix_color(data.halo_color, drawable.halo_color_t)),
#endif
#if !defined(HAS_UNIFORM_u_halo_width)
.halo_width = half(unpack_mix_float(data.halo_width, drawable.halo_width_t)),
#endif
#if !defined(HAS_UNIFORM_u_halo_blur)
.halo_blur = half(unpack_mix_float(data.halo_blur, drawable.halo_blur_t)),
#endif
#if !defined(HAS_UNIFORM_u_opacity)
.opacity = half(unpack_mix_float(data.opacity, drawable.opacity_t)),
#endif
.tex = half2(a_tex / drawable.texsize),
.gamma_scale = half(position.w),
.fontScale = half(fontScale),
.fade_opacity = fo,
};
}
half4 fragment fragmentMain(FragmentStage in [[stage_in]],
device const uint32_t& uboIndex [[buffer(idGlobalUBOIndex)]],
device const SymbolTilePropsUBO* tilePropsVector [[buffer(idSymbolTilePropsUBO)]],
device const SymbolEvaluatedPropsUBO& props [[buffer(idSymbolEvaluatedPropsUBO)]],
texture2d<float, access::sample> image [[texture(0)]],
sampler image_sampler [[sampler(0)]]) {
#if defined(OVERDRAW_INSPECTOR)
return half4(1.0);
#endif
device const SymbolTilePropsUBO& tileProps = tilePropsVector[uboIndex];
#if defined(HAS_UNIFORM_u_fill_color)
const half4 fill_color = half4(tileProps.is_text ? props.text_fill_color : props.icon_fill_color);
#else
const half4 fill_color = in.fill_color;
#endif
#if defined(HAS_UNIFORM_u_halo_color)
const half4 halo_color = half4(tileProps.is_text ? props.text_halo_color : props.icon_halo_color);
#else
const half4 halo_color = in.halo_color;
#endif
#if defined(HAS_UNIFORM_u_opacity)
const float opacity = tileProps.is_text ? props.text_opacity : props.icon_opacity;
#else
const float opacity = in.opacity;
#endif
#if defined(HAS_UNIFORM_u_halo_width)
const float halo_width = tileProps.is_text ? props.text_halo_width : props.icon_halo_width;
#else
const float halo_width = in.halo_width;
#endif
#if defined(HAS_UNIFORM_u_halo_blur)
const float halo_blur = tileProps.is_text ? props.text_halo_blur : props.icon_halo_blur;
#else
const float halo_blur = in.halo_blur;
#endif
const float EDGE_GAMMA = 0.105 / DEVICE_PIXEL_RATIO;
const float fontGamma = in.fontScale * tileProps.gamma_scale;
const float fillGamma = EDGE_GAMMA / fontGamma;
const float haloGamma = (halo_blur * 1.19 / SDF_PX + EDGE_GAMMA) / fontGamma;
const float gamma = tileProps.is_halo ? haloGamma : fillGamma;
const float gammaScaled = gamma * in.gamma_scale;
const half4 color = tileProps.is_halo ? halo_color : fill_color;
const float fillInnerEdge = (256.0 - 64.0) / 256.0;
const float haloInnerEdge = fillInnerEdge + haloGamma * tileProps.gamma_scale;
const float innerEdge = tileProps.is_halo ? haloInnerEdge : fillInnerEdge;
const float dist = image.sample(image_sampler, float2(in.tex)).a;
float alpha = smoothstep(innerEdge - gammaScaled, innerEdge + gammaScaled, dist);
if (tileProps.is_halo) {
// When drawing halos, if the fill is translucent we want the inside of the halo to be translucent as well
const float haloEdge = (6.0 - halo_width / in.fontScale) / SDF_PX;
alpha = min(smoothstep(haloEdge - gammaScaled, haloEdge + gammaScaled, dist), 1.0 - alpha);
}
return half4(color * (alpha * opacity * in.fade_opacity));
}
)";
};
template <>
struct ShaderSource<BuiltIn::SymbolTextAndIconShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "SymbolTextAndIconShader";
static constexpr auto vertexMainFunction = "vertexMain";
static constexpr auto fragmentMainFunction = "fragmentMain";
static const std::array<AttributeInfo, 1> attributes;
static const std::array<AttributeInfo, 14> instanceAttributes;
static const std::array<TextureInfo, 2> textures;
static constexpr auto prelude = symbolShaderPrelude;
static constexpr auto source = R"(
#define SDF 1.0
#define ICON 0.0
struct VertexStage {
float2 pos [[attribute(0)]];
ushort instance [[attribute(1)]];
//float4 pos_scale [[attribute(1)]];
//float4 offset_tltr [[attribute(2)]];
//float4 offset_blbr [[attribute(3)]];
//float4 texture_rect [[attribute(4)]];
//float4 pixeloffset [[attribute(5)]];
//float2 size_sdf [[attribute(6)]];
//float3 projected_pos [[attribute(7)]];
//float fade_opacity [[attribute(8)]];
#if !defined(HAS_UNIFORM_u_opacity)
//float2 opacity [[attribute(9)]];
#endif
#if !defined(HAS_UNIFORM_u_fill_color)
//float4 fill_color [[attribute(10)]];
#endif
#if !defined(HAS_UNIFORM_u_halo_color)
//float4 halo_color [[attribute(11)]];
#endif
#if !defined(HAS_UNIFORM_u_halo_width)
//float2 halo_width [[attribute(12)]];
#endif
#if !defined(HAS_UNIFORM_u_halo_blur)
//float2 halo_blur [[attribute(13)]];
#endif
};
struct SymbolInstance {
short2 pos_scale[2];
short2 offset_tltr[2];
short2 offset_blbr[2];
ushort2 texture_rect[2];
short2 pixeloffset[2];
ushort2 size_sdf;
};
struct DynamicInstance {
float projected_pos[3];
};
struct OpacityInstance {
float fade_opacity;
};
struct DataInstance {
#if !defined(HAS_UNIFORM_u_opacity)
float opacity[2];
#endif
#if !defined(HAS_UNIFORM_u_fill_color)
float fill_color[4];
#endif
#if !defined(HAS_UNIFORM_u_halo_color)
float halo_color[4];
#endif
#if !defined(HAS_UNIFORM_u_halo_width)
float halo_width[2];
#endif
#if !defined(HAS_UNIFORM_u_halo_blur)
float halo_blur[2];
#endif
};
struct FragmentStage {
float4 position [[position, invariant]];
#if !defined(HAS_UNIFORM_u_fill_color)
half4 fill_color;
#endif
#if !defined(HAS_UNIFORM_u_halo_color)
half4 halo_color;
#endif
half2 tex;
#if !defined(HAS_UNIFORM_u_opacity)
half opacity;
#endif
#if !defined(HAS_UNIFORM_u_halo_width)
half halo_width;
#endif
#if !defined(HAS_UNIFORM_u_halo_blur)
half halo_blur;
#endif
half gamma_scale;
half fontScale;
half fade_opacity;
bool is_icon;
};
FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]],
device const GlobalPaintParamsUBO& paintParams [[buffer(idGlobalPaintParamsUBO)]],
device const uint32_t& uboIndex [[buffer(idGlobalUBOIndex)]],
device const SymbolDrawableUBO* drawableVector [[buffer(idSymbolDrawableUBO)]],
uint instanceID [[ instance_id ]],
device const SymbolInstance* symbolInstances [[buffer(symbolUBOCount + 2)]],
device const DynamicInstance* dynamicInstances [[buffer(symbolUBOCount + 3)]],
device const OpacityInstance* opacityInstances [[buffer(symbolUBOCount + 4)]],
device const DataInstance* dataInstances [[buffer(symbolUBOCount + 5)]]) {
device const SymbolDrawableUBO& drawable = drawableVector[uboIndex];
device const SymbolInstance& symbol = symbolInstances[vertx.instance];
device const DynamicInstance& dynamic = dynamicInstances[vertx.instance];
device const OpacityInstance& opacity = opacityInstances[vertx.instance];
device const DataInstance& data = dataInstances[vertx.instance];
const float2 fade_opacity = unpack_opacity(opacity.fade_opacity);
const float fade_change = (fade_opacity[1] > 0.5) ? paintParams.symbol_fade_change : -paintParams.symbol_fade_change;
const half fo = half(max(0.0, min(1.0, fade_opacity[0] + fade_change)));
// This will check to see if the opacity is zero and push the triangle offscreen if it is
// so the GPU will cull the vertex and never send it to the fragment shader
if (fo == 0.0) {
return {
.position = float4(c_offscreen_degenerate_triangle_location,
c_offscreen_degenerate_triangle_location,
c_offscreen_degenerate_triangle_location, 1.0),
};
}
const float2 a_pos = float2(symbol.pos_scale[0].xy);
float2 a_offset;
if (vertx.pos.x == 0) {
if (vertx.pos.y == 0) {
a_offset = float2(symbol.offset_tltr[0].xy); //tl
} else {
a_offset = float2(symbol.offset_blbr[0].xy); //bl
}
} else {
if (vertx.pos.y == 0) {
a_offset = float2(symbol.offset_tltr[1].xy); //tr
} else {
a_offset = float2(symbol.offset_blbr[1].xy); //br
}
}
const float2 a_tex = float2(symbol.texture_rect[0].xy) + vertx.pos * float2(symbol.texture_rect[1].xy);
const float2 a_size = float2(symbol.size_sdf);
const float a_size_min = floor(a_size[0] * 0.5);
const float is_sdf = a_size[0] - 2.0 * a_size_min;
const float segment_angle = -dynamic.projected_pos[2];
float size;
if (!drawable.is_size_zoom_constant && !drawable.is_size_feature_constant) {
size = mix(a_size_min, a_size[1], drawable.size_t) / 128.0;
} else if (drawable.is_size_zoom_constant && !drawable.is_size_feature_constant) {
size = a_size_min / 128.0;
} else {
size = drawable.size;
}
const float4 projectedPoint = drawable.matrix * float4(a_pos, 0, 1);
const 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.
const float distance_ratio = drawable.pitch_with_map ?
camera_to_anchor_distance / paintParams.camera_to_center_distance :
paintParams.camera_to_center_distance / camera_to_anchor_distance;
const float perspective_ratio = clamp(
0.5 + 0.5 * distance_ratio,
0.0, // Prevents oversized near-field symbols in pitched/overzoomed tiles
4.0);
if (!drawable.is_offset) {
size *= perspective_ratio;
}
const float fontScale = size / 24.0;
float symbol_rotation = 0.0;
if (drawable.rotate_symbol) {
// 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.
const float4 offsetProjectedPoint = drawable.matrix * float4(a_pos + float2(1, 0), 0, 1);
const float2 a = projectedPoint.xy / projectedPoint.w;
const float2 b = offsetProjectedPoint.xy / offsetProjectedPoint.w;
symbol_rotation = atan2((b.y - a.y) / paintParams.aspect_ratio, b.x - a.x);
}
const float angle_sin = sin(segment_angle + symbol_rotation);
const float angle_cos = cos(segment_angle + symbol_rotation);
const float2x2 rotation_matrix = float2x2(angle_cos, -1.0 * angle_sin, angle_sin, angle_cos);
const float4 projected_pos = drawable.label_plane_matrix * float4(dynamic.projected_pos[0], dynamic.projected_pos[1], 0.0, 1.0);
const float2 pos_rot = a_offset / 32.0 * fontScale;
const float2 pos0 = projected_pos.xy / projected_pos.w + rotation_matrix * pos_rot;
const float4 position = drawable.coord_matrix * float4(pos0, 0.0, 1.0);
const float gamma_scale = position.w;
const bool is_icon = (is_sdf == ICON);
return {
.position = position,
.tex = half2(a_tex / (is_icon ? drawable.texsize_icon : drawable.texsize)),
.gamma_scale = half(gamma_scale),
.fontScale = half(fontScale),
.fade_opacity = fo,
.is_icon = is_icon,
#if !defined(HAS_UNIFORM_u_fill_color)
.fill_color = half(unpack_mix_color(data.fill_color, drawable.fill_color_t));
#endif
#if !defined(HAS_UNIFORM_u_halo_color)
.halo_color = half(unpack_mix_color(data.halo_color, drawable.halo_color_t));
#endif
#if !defined(HAS_UNIFORM_u_opacity)
.opacity = half(unpack_mix_float(data.opacity, drawable.opacity_t));
#endif
#if !defined(HAS_UNIFORM_u_halo_width)
.halo_width = half(unpack_mix_float(data.halo_width, drawable.halo_width_t));
#endif
#if !defined(HAS_UNIFORM_u_halo_blur)
.halo_blur = half(unpack_mix_float(data.halo_blur, drawable.halo_blur_t));
#endif
};
}
half4 fragment fragmentMain(FragmentStage in [[stage_in]],
device const uint32_t& uboIndex [[buffer(idGlobalUBOIndex)]],
device const SymbolTilePropsUBO* tilePropsVector [[buffer(idSymbolTilePropsUBO)]],
device const SymbolEvaluatedPropsUBO& props [[buffer(idSymbolEvaluatedPropsUBO)]],
texture2d<float, access::sample> glyph_image [[texture(0)]],
texture2d<float, access::sample> icon_image [[texture(1)]],
sampler glyph_sampler [[sampler(0)]],
sampler icon_sampler [[sampler(1)]]) {
#if defined(OVERDRAW_INSPECTOR)
return half4(1.0);
#endif
device const SymbolTilePropsUBO& tileProps = tilePropsVector[uboIndex];
#if defined(HAS_UNIFORM_u_fill_color)
const half4 fill_color = half4(tileProps.is_text ? props.text_fill_color : props.icon_fill_color);
#else
const half4 fill_color = in.fill_color;
#endif
#if defined(HAS_UNIFORM_u_halo_color)
const half4 halo_color = half4(tileProps.is_text ? props.text_halo_color : props.icon_halo_color);
#else
const half4 halo_color = in.halo_color;
#endif
#if defined(HAS_UNIFORM_u_opacity)
const half opacity = half(tileProps.is_text ? props.text_opacity : props.icon_opacity);
#else
const half opacity = in.opacity;
#endif
#if defined(HAS_UNIFORM_u_halo_width)
const half halo_width = half(tileProps.is_text ? props.text_halo_width : props.icon_halo_width);
#else
const half halo_width = in.halo_width;
#endif
#if defined(HAS_UNIFORM_u_halo_blur)
const half halo_blur = half(tileProps.is_text ? props.text_halo_blur : props.icon_halo_blur);
#else
const half halo_blur = in.halo_blur;
#endif
if (in.is_icon) {
const float alpha = opacity * in.fade_opacity;
return half4(icon_image.sample(icon_sampler, float2(in.tex)) * alpha);
}
const float EDGE_GAMMA = 0.105 / DEVICE_PIXEL_RATIO;
const float fontGamma = in.fontScale * tileProps.gamma_scale;
const float fillGamma = EDGE_GAMMA / fontGamma;
const float haloGamma = (halo_blur * 1.19 / SDF_PX + EDGE_GAMMA) / fontGamma;
const float gamma = tileProps.is_halo ? haloGamma : fillGamma;
const float gammaScaled = gamma * in.gamma_scale;
const half4 color = tileProps.is_halo ? halo_color : fill_color;
const float fillInnerEdge = (256.0 - 64.0) / 256.0;
const float haloInnerEdge = fillInnerEdge + haloGamma * tileProps.gamma_scale;
const float innerEdge = tileProps.is_halo ? haloInnerEdge : fillInnerEdge;
const float dist = glyph_image.sample(glyph_sampler, float2(in.tex)).a;
float alpha = smoothstep(innerEdge - gammaScaled, innerEdge + gammaScaled, dist);
if (tileProps.is_halo) {
// When drawing halos, if the fill is translucent we want the inside of the halo to be translucent as well
const float haloEdge = (6.0 - halo_width / in.fontScale) / SDF_PX;
alpha = min(smoothstep(haloEdge - gammaScaled, haloEdge + gammaScaled, dist), 1.0 - alpha);
}
return half4(color * (alpha * opacity * in.fade_opacity));
}
)";
};
} // namespace shaders
} // namespace mbgl