Skip to content

Commit c755e41

Browse files
Andrcraft9github-actions[bot]
authored andcommitted
Update symbol shaders: replace RENDER_SDF define with uniform
GitOrigin-RevId: 323b3ee23ee2c3e7c3250e15a672fa692a42bb58
1 parent 7a58b86 commit c755e41

4 files changed

Lines changed: 43 additions & 98 deletions

File tree

src/render/draw_symbol.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -466,10 +466,6 @@ function drawLayerSymbols(
466466
baseDefines.push('COLOR_ADJUSTMENT');
467467
}
468468

469-
if (bucket.sdfIcons) {
470-
baseDefines.push('RENDER_SDF');
471-
}
472-
473469
if (renderWithShadows) {
474470
baseDefines.push('RENDER_SHADOWS', 'NORMAL_OFFSET');
475471
}
@@ -515,7 +511,7 @@ function drawLayerSymbols(
515511

516512
const colorAdjustmentMatrix = layer.getColorAdjustmentMatrix(iconSaturation, iconContrast, iconBrightnessMin, iconBrightnessMax);
517513
const uniformValues = symbolUniformValues(sizeData.kind, size, rotateInShader, iconPitchWithMap, painter,
518-
matrix, uLabelPlaneMatrix, uglCoordMatrix, elevationFromSea, false, texSize, [0, 0], true, coord, globeToMercator, mercatorCenter, invMatrix,
514+
matrix, uLabelPlaneMatrix, uglCoordMatrix, elevationFromSea, false, bucket.sdfIcons, texSize, [0, 0], true, coord, globeToMercator, mercatorCenter, invMatrix,
519515
cameraUpVector, bucket.getProjection(), groundShadowFactor, orientationNormalScale, colorAdjustmentMatrix, transitionProgress, null);
520516

521517
const atlasTexture = tile.imageAtlasTexture ? tile.imageAtlasTexture : null;
@@ -586,8 +582,6 @@ function drawLayerSymbols(
586582
baseDefines.push('RENDER_TEXT_AND_SYMBOL');
587583
}
588584

589-
baseDefines.push('RENDER_SDF');
590-
591585
if (renderWithShadows) {
592586
baseDefines.push('RENDER_SHADOWS', 'NORMAL_OFFSET');
593587
}
@@ -651,7 +645,7 @@ function drawLayerSymbols(
651645
const cameraUpVector = bucketIsGlobeProjection ? globeCameraUp : mercatorCameraUp;
652646

653647
const uniformValues = symbolUniformValues(sizeData.kind, size, rotateInShader, textPitchWithMap, painter,
654-
matrix, uLabelPlaneMatrix, uglCoordMatrix, elevationFromSea, true, texSize, texSizeIcon, true, coord, globeToMercator, mercatorCenter, invMatrix,
648+
matrix, uLabelPlaneMatrix, uglCoordMatrix, elevationFromSea, true, true, texSize, texSizeIcon, true, coord, globeToMercator, mercatorCenter, invMatrix,
655649
cameraUpVector, bucket.getProjection(), groundShadowFactor, orientationNormalScale, null, null, textScaleFactor);
656650

657651
const atlasTexture = tile.glyphAtlasTexture ? tile.glyphAtlasTexture : null;

src/render/program/symbol_program.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type SymbolUniformsType = {
2424
['u_label_plane_matrix']: UniformMatrix4f;
2525
['u_coord_matrix']: UniformMatrix4f;
2626
['u_is_text']: Uniform1i;
27+
['u_is_sdf']: Uniform1i;
2728
['u_elevation_from_sea']: Uniform1i;
2829
['u_pitch_with_map']: Uniform1i;
2930
['u_texsize']: Uniform2f;
@@ -81,7 +82,6 @@ export type SymbolDefinesType =
8182
| 'ICON_TRANSITION'
8283
| 'PITCH_WITH_MAP_TERRAIN'
8384
| 'PROJECTED_POS_ON_VIEWPORT'
84-
| 'RENDER_SDF'
8585
| 'RENDER_TEXT_AND_SYMBOL'
8686
| 'Z_OFFSET'
8787
| 'APPLY_LUT_ON_GPU';
@@ -99,6 +99,7 @@ const symbolUniforms = (context: Context): SymbolUniformsType => ({
9999
'u_label_plane_matrix': new UniformMatrix4f(context),
100100
'u_coord_matrix': new UniformMatrix4f(context),
101101
'u_is_text': new Uniform1i(context),
102+
'u_is_sdf': new Uniform1i(context),
102103
'u_elevation_from_sea': new Uniform1i(context),
103104
'u_pitch_with_map': new Uniform1i(context),
104105
'u_texsize': new Uniform2f(context),
@@ -158,6 +159,7 @@ const symbolUniformValues = (
158159
glCoordMatrix: mat4,
159160
elevationFromSea: boolean,
160161
isText: boolean,
162+
isSDF: boolean,
161163
texSize: [number, number],
162164
texSizeIcon: [number, number],
163165
isHalo: boolean,
@@ -188,6 +190,7 @@ const symbolUniformValues = (
188190
'u_label_plane_matrix': labelPlaneMatrix,
189191
'u_coord_matrix': glCoordMatrix,
190192
'u_is_text': +isText,
193+
'u_is_sdf': +isSDF,
191194
'u_elevation_from_sea': elevationFromSea ? 1.0 : 0.0,
192195
'u_pitch_with_map': +pitchWithMap,
193196
'u_texsize': texSize,
@@ -204,7 +207,7 @@ const symbolUniformValues = (
204207
'u_up_vector': [0, -1, 0] as [number, number, number],
205208
'u_color_adj_mat': colorAdjustmentMatrix,
206209
'u_icon_transition': transition ? transition : 0.0,
207-
'u_gamma_scale': pitchWithMap ? painter.transform.getCameraToCenterDistance(projection) * Math.cos(painter.terrain ? 0 : painter.transform._pitch) : 1,
210+
'u_gamma_scale': isSDF ? (pitchWithMap ? painter.transform.getCameraToCenterDistance(projection) * Math.cos(painter.terrain ? 0 : painter.transform._pitch) : 1) : 0,
208211
'u_device_pixel_ratio': browser.devicePixelRatio,
209212
'u_is_halo': +isHalo,
210213
'u_scale_factor': scaleFactor ? scaleFactor : 1.0,

src/shaders/symbol.fragment.glsl

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ uniform sampler2D u_texture;
99
#ifdef RENDER_TEXT_AND_SYMBOL
1010
uniform sampler2D u_texture_icon;
1111
#endif
12-
#ifdef RENDER_SDF
12+
1313
uniform highp float u_gamma_scale;
1414
uniform lowp float u_device_pixel_ratio;
1515
uniform bool u_is_text;
16+
uniform bool u_is_sdf;
1617
uniform lowp float u_scale_factor;
17-
#endif
18+
1819
// Boolean-config transition fragment alpha multiplier (dual-pass: originals + deltas).
1920
uniform lowp float u_opacity_multiplier;
2021
#ifdef ICON_TRANSITION
@@ -38,9 +39,7 @@ in vec2 v_tex_a;
3839
in vec2 v_tex_b;
3940
#endif
4041

41-
#ifdef RENDER_SDF
4242
in float v_draw_halo;
43-
#endif
4443
in vec3 v_gamma_scale_size_fade_opacity;
4544
#ifdef RENDER_TEXT_AND_SYMBOL
4645
in float is_sdf;
@@ -61,13 +60,10 @@ uniform highp sampler3D u_lutTexture;
6160

6261
/// Symbol paint properties.
6362
in lowp float v_opacity;
64-
#ifdef RENDER_SDF
6563
in lowp vec4 v_fill_np_color;
6664
in lowp vec4 v_halo_np_color;
6765
in lowp float v_halo_width;
6866
in lowp float v_halo_blur;
69-
#endif
70-
7167
#ifdef LIGHTING_3D_MODE
7268
in lowp float v_emissive_strength;
7369
#endif
@@ -78,14 +74,13 @@ void main() {
7874
lowp vec4 halo_color = vec4(0.0);
7975
lowp float halo_width = 0.0;
8076
lowp float halo_blur = 0.0;
81-
#ifdef RENDER_SDF
82-
// Pre-multiply colors by alpha.
83-
fill_color = vec4(v_fill_np_color.rgb * v_fill_np_color.a, v_fill_np_color.a);
84-
halo_color = vec4(v_halo_np_color.rgb * v_halo_np_color.a, v_halo_np_color.a);
85-
halo_width = v_halo_width;
86-
halo_blur = v_halo_blur;
87-
#endif
88-
77+
if (u_is_sdf) {
78+
// Pre-multiply colors by alpha.
79+
fill_color = vec4(v_fill_np_color.rgb * v_fill_np_color.a, v_fill_np_color.a);
80+
halo_color = vec4(v_halo_np_color.rgb * v_halo_np_color.a, v_halo_np_color.a);
81+
halo_width = v_halo_width;
82+
halo_blur = v_halo_blur;
83+
}
8984
lowp float emissive_strength = 0.0;
9085
#ifdef LIGHTING_3D_MODE
9186
emissive_strength = v_emissive_strength;
@@ -112,47 +107,47 @@ void main() {
112107
cutout_factors = get_cutout_factors(gl_FragCoord);
113108
#endif
114109

115-
#ifdef RENDER_SDF
116-
float EDGE_GAMMA = 0.105 / u_device_pixel_ratio;
110+
if (u_is_sdf) {
111+
float EDGE_GAMMA = 0.105 / u_device_pixel_ratio;
117112

118-
float gamma_scale = v_gamma_scale_size_fade_opacity.x;
119-
float size = v_gamma_scale_size_fade_opacity.y;
113+
float gamma_scale = v_gamma_scale_size_fade_opacity.x;
114+
float size = v_gamma_scale_size_fade_opacity.y;
120115

121-
float fontScale = u_is_text ? size / 24.0 : size;
116+
float fontScale = u_is_text ? size / 24.0 : size;
122117

123-
out_color = fill_color;
124-
highp float gamma = EDGE_GAMMA / (fontScale * u_gamma_scale);
125-
lowp float buff = (256.0 - 64.0) / 256.0;
118+
out_color = fill_color;
119+
highp float gamma = EDGE_GAMMA / (fontScale * u_gamma_scale);
120+
lowp float buff = (256.0 - 64.0) / 256.0;
126121

127-
bool draw_halo = v_draw_halo > 0.0;
128-
if (draw_halo) {
129-
out_color = halo_color;
130-
gamma = (halo_blur * u_scale_factor * 1.19 / SDF_PX + EDGE_GAMMA) / (fontScale * u_gamma_scale);
131-
buff = (6.0 - halo_width * u_scale_factor / fontScale) / SDF_PX;
132-
}
122+
bool draw_halo = v_draw_halo > 0.0;
123+
if (draw_halo) {
124+
out_color = halo_color;
125+
gamma = (halo_blur * u_scale_factor * 1.19 / SDF_PX + EDGE_GAMMA) / (fontScale * u_gamma_scale);
126+
buff = (6.0 - halo_width * u_scale_factor / fontScale) / SDF_PX;
127+
}
133128

134-
lowp float dist = texture(u_texture, v_tex_a).r;
135-
highp float gamma_scaled = gamma * gamma_scale;
136-
highp float alpha = smoothstep(buff - gamma_scaled, buff + gamma_scaled, dist);
129+
lowp float dist = texture(u_texture, v_tex_a).r;
130+
highp float gamma_scaled = gamma * gamma_scale;
131+
highp float alpha = smoothstep(buff - gamma_scaled, buff + gamma_scaled, dist);
137132

138-
out_color *= alpha;
139-
#else // RENDER_SDF
140-
#ifdef ICON_TRANSITION
133+
out_color *= alpha;
134+
} else {
135+
#ifdef ICON_TRANSITION
141136
vec4 a = texture(u_texture, v_tex_a) * (1.0 - u_icon_transition);
142137
vec4 b = texture(u_texture, v_tex_b) * u_icon_transition;
143138
out_color = (a + b);
144-
#else
139+
#else
145140
out_color = texture(u_texture, v_tex_a);
146-
#endif
141+
#endif
147142

148-
#ifdef APPLY_LUT_ON_GPU
143+
#ifdef APPLY_LUT_ON_GPU
149144
out_color = applyLUT(u_lutTexture, out_color);
150-
#endif
145+
#endif
151146

152-
#ifdef COLOR_ADJUSTMENT
147+
#ifdef COLOR_ADJUSTMENT
153148
out_color = u_color_adj_mat * out_color;
154-
#endif
155149
#endif
150+
}
156151

157152
out_color *= opacity * fade_opacity * u_opacity_multiplier;
158153

0 commit comments

Comments
 (0)