Skip to content

Commit 573836a

Browse files
aleksigrongithub-actions[bot]
authored andcommitted
move certain shader features from implicit to explicit includes
GitOrigin-RevId: 3492529beda62fb98d8c290f1a3bf9629cb33bfb
1 parent ea750d4 commit 573836a

22 files changed

Lines changed: 144 additions & 90 deletions

3d-style/shaders/_prelude_shadow.fragment.glsl

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,29 @@ uniform vec2 u_fade_range;
1212
uniform mediump vec3 u_shadow_direction;
1313
uniform highp vec3 u_shadow_bias;
1414

15+
// Metal/SPIRV-Cross depth-compare sampling needs Y negated when mapping light-view NDC to
16+
// shadow-map UV. Vulkan and GL do not define SHADOW_MAP_FLIP_Y.
17+
highp vec2 shadow_map_ndc_to_uv(highp vec2 ndc_xy) {
18+
#ifdef SHADOW_MAP_FLIP_Y
19+
return ndc_xy * vec2(0.5, -0.5) + 0.5;
20+
#else
21+
return ndc_xy * 0.5 + 0.5;
22+
#endif
23+
}
24+
25+
highp vec3 shadow_map_ndc_to_sample_space(highp vec3 ndc) {
26+
#ifdef CLIP_ZERO_TO_ONE
27+
return vec3(shadow_map_ndc_to_uv(ndc.xy), ndc.z);
28+
#else
29+
return vec3(shadow_map_ndc_to_uv(ndc.xy), ndc.z * 0.5 + 0.5);
30+
#endif
31+
}
32+
1533
float shadow_sample(sampler2DShadow shadowmap, highp vec3 pos, highp float bias) {
1634
#ifdef CLIP_ZERO_TO_ONE
17-
highp vec3 coord = vec3(pos.xy * 0.5 + 0.5, pos.z - bias);
35+
highp vec3 coord = vec3(shadow_map_ndc_to_uv(pos.xy), pos.z - bias);
1836
#else
19-
highp vec3 coord = vec3(pos.xy * 0.5 + 0.5, pos.z * 0.5 + 0.5 - bias);
37+
highp vec3 coord = vec3(shadow_map_ndc_to_uv(pos.xy), pos.z * 0.5 + 0.5 - bias);
2038
#endif
2139
return texture(shadowmap, coord);
2240
}
@@ -95,7 +113,7 @@ highp vec2 compute_receiver_plane_depth_bias(highp vec3 pos_dx, highp vec3 pos_d
95113
return biasUV;
96114
}
97115
float shadowed_light_factor_plane_bias(highp vec4 light_view_pos0, highp vec4 light_view_pos1, float view_depth) {
98-
highp vec3 light_view_pos0_xyz = light_view_pos0.xyz / light_view_pos0.w * 0.5 + 0.5;
116+
highp vec3 light_view_pos0_xyz = shadow_map_ndc_to_sample_space(light_view_pos0.xyz / light_view_pos0.w);
99117
highp vec3 light_view_pos0_ddx = dFdx(light_view_pos0_xyz);
100118
highp vec3 light_view_pos0_ddy = dFdy(light_view_pos0_xyz);
101119
highp vec2 plane_depth_bias = compute_receiver_plane_depth_bias(light_view_pos0_ddx, light_view_pos0_ddy);

3d-style/shaders/building.fragment.glsl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "_prelude_fog.fragment.glsl"
22
#include "_prelude_shadow.fragment.glsl"
33
#include "_prelude_lighting.glsl"
4+
#include "_prelude_indicator_cutout.fragment.glsl"
5+
#include "_prelude_feature_cutout.fragment.glsl"
46

57
const float window_depth = 0.5; // meters
68
const float ao_radius = 0.2; // meters
@@ -240,7 +242,7 @@ void main() {
240242
{
241243
float ditherOpacity = cutoutGroundRoofOpacity(v_ground_roof);
242244
if (ditherOpacity < 1.0) {
243-
int index = (int(gl_FragCoord.x) % 4) * 4 + (int(gl_FragCoord.y) % 4);
245+
int index = viewport_dither_index(gl_FragCoord.xy);
244246
if (ditherOpacity < DITHER_THRESHOLDS[index]) {
245247
discard;
246248
}
@@ -253,7 +255,7 @@ void main() {
253255

254256
#ifdef RENDER_FRONT_CUTOFF
255257
if (v_front_cutoff_opacity < 1.0) {
256-
int index = (int(gl_FragCoord.x) % 4) * 4 + (int(gl_FragCoord.y) % 4);
258+
int index = viewport_dither_index(gl_FragCoord.xy);
257259
if (v_front_cutoff_opacity < DITHER_THRESHOLDS[index]) {
258260
discard;
259261
}

3d-style/shaders/elevated_structures_depth_reconstruct.fragment.glsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include "_prelude_feature_cutout.fragment.glsl"
2+
13
#ifdef DEPTH_RECONSTRUCTION
24
in float v_height;
35
#endif

3d-style/shaders/elevated_structures_model.fragment.glsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "_prelude_fog.fragment.glsl"
22
#include "_prelude_lighting.glsl"
33
#include "_prelude_shadow.fragment.glsl"
4+
#include "_prelude_indicator_cutout.fragment.glsl"
5+
#include "_prelude_feature_cutout.fragment.glsl"
46

57
in vec3 v_normal;
68
in float v_height;

3d-style/shaders/ground_shadow.fragment.glsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "_prelude_shadow.fragment.glsl"
2+
#include "_prelude_indicator_cutout.fragment.glsl"
3+
#include "_prelude_feature_cutout.fragment.glsl"
24

35
precision highp float;
46

3d-style/shaders/model.fragment.glsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "_prelude_fog.fragment.glsl"
22
#include "_prelude_shadow.fragment.glsl"
33
#include "_prelude_lighting.glsl"
4+
#include "_prelude_indicator_cutout.fragment.glsl"
5+
#include "_prelude_feature_cutout.fragment.glsl"
46

57
uniform float u_opacity;
68

3d-style/shaders/model.vertex.glsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "_prelude_fog.vertex.glsl"
22
#include "_prelude_shadow.vertex.glsl"
3+
#include "_prelude_feature_cutout.vertex.glsl"
34

45
in vec3 a_pos_3f;
56

src/shaders/_prelude.fragment.glsl

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,57 +32,16 @@ highp vec4 pack_depth(highp float ndc_z) {
3232
return res;
3333
}
3434

35-
#ifdef INDICATOR_CUTOUT
36-
uniform vec3 u_indicator_cutout_centers;
37-
uniform vec4 u_indicator_cutout_params;
38-
#endif
39-
4035
const float DITHER_THRESHOLDS[16] = float[16](
4136
1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0,
4237
13.0 / 17.0, 5.0 / 17.0, 15.0 / 17.0, 7.0 / 17.0,
4338
4.0 / 17.0, 12.0 / 17.0, 2.0 / 17.0, 10.0 / 17.0,
4439
16.0 / 17.0, 8.0 / 17.0, 14.0 / 17.0, 6.0 / 17.0
4540
);
4641

47-
vec4 applyCutout(vec4 color, float height) {
48-
#ifdef INDICATOR_CUTOUT
49-
float verticalFadeRange = u_indicator_cutout_centers.z * 0.25; // Fade relative to the height of the indicator
50-
float holeMinOpacity = mix(1.0, u_indicator_cutout_params.x, smoothstep(u_indicator_cutout_centers.z, u_indicator_cutout_centers.z + verticalFadeRange, height));
51-
float holeRadius = max(u_indicator_cutout_params.y, 0.0);
52-
float holeAspectRatio = u_indicator_cutout_params.z;
53-
float fadeStart = u_indicator_cutout_params.w;
54-
float distA = distance(vec2(gl_FragCoord.x, gl_FragCoord.y * holeAspectRatio), vec2(u_indicator_cutout_centers[0], u_indicator_cutout_centers[1] * holeAspectRatio));
55-
return color * min(smoothstep(fadeStart, holeRadius, distA) + holeMinOpacity, 1.0);
56-
#else
57-
return color;
58-
#endif
59-
}
60-
61-
// Cutout with uniform vertical transparency across the building face.
62-
// All coordinates are in NDC [-1,1]. Centers, radius, fadeStart pre-converted to NDC on CPU.
63-
// groundRoof = (groundNdcX, groundNdcY, roofNdcX, roofNdcY).
64-
// Returns indicator cutout opacity for dithering. Color is not modified.
65-
float cutoutGroundRoofOpacity(vec4 groundRoof) {
66-
#ifdef INDICATOR_CUTOUT
67-
float fadeStartX = u_indicator_cutout_params.w;
68-
float holeRadius = u_indicator_cutout_params.y;
69-
70-
float holeMinOpacity = mix(u_indicator_cutout_params.x, 1.0,
71-
smoothstep(u_indicator_cutout_params.z, u_indicator_cutout_centers.z, groundRoof.y));
72-
73-
float distX = abs(u_indicator_cutout_centers.x - groundRoof.x);
74-
75-
float roofOpacity = mix(holeMinOpacity, 1.0,
76-
smoothstep(fadeStartX, holeRadius,
77-
u_indicator_cutout_centers.y - groundRoof.w));
78-
79-
float groundOpacity = min(smoothstep(fadeStartX, holeRadius, distX)
80-
+ holeMinOpacity, 1.0);
81-
82-
return max(roofOpacity, groundOpacity);
83-
#else
84-
return 1.0;
85-
#endif
42+
// Bayer dither index in gl_FragCoord framebuffer space (front-cutoff, indicator cutout).
43+
int viewport_dither_index(vec2 fragCoordXY) {
44+
return (int(fragCoordXY.x) % 4) * 4 + (int(fragCoordXY.y) % 4);
8645
}
8746

8847
#ifdef DEBUG_WIREFRAME
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// NOTE: Include explicitly in fragment shaders that use indicator cutout
2+
// (applyCutout, cutoutGroundRoofOpacity).
3+
4+
#ifdef INDICATOR_CUTOUT
5+
uniform vec3 u_indicator_cutout_centers;
6+
uniform vec4 u_indicator_cutout_params;
7+
#endif
8+
9+
vec4 applyCutout(vec4 color, float height) {
10+
#ifdef INDICATOR_CUTOUT
11+
float verticalFadeRange = u_indicator_cutout_centers.z * 0.25; // Fade relative to the height of the indicator
12+
float holeMinOpacity = mix(1.0, u_indicator_cutout_params.x, smoothstep(u_indicator_cutout_centers.z, u_indicator_cutout_centers.z + verticalFadeRange, height));
13+
float holeRadius = max(u_indicator_cutout_params.y, 0.0);
14+
float holeAspectRatio = u_indicator_cutout_params.z;
15+
float fadeStart = u_indicator_cutout_params.w;
16+
float distA = distance(vec2(gl_FragCoord.x, gl_FragCoord.y * holeAspectRatio), vec2(u_indicator_cutout_centers[0], u_indicator_cutout_centers[1] * holeAspectRatio));
17+
return color * min(smoothstep(fadeStart, holeRadius, distA) + holeMinOpacity, 1.0);
18+
#else
19+
return color;
20+
#endif
21+
}
22+
23+
// Cutout with uniform vertical transparency across the building face.
24+
// All coordinates are in NDC [-1,1]. Centers, radius, fadeStart pre-converted to NDC on CPU.
25+
// groundRoof = (groundNdcX, groundNdcY, roofNdcX, roofNdcY).
26+
// Returns indicator cutout opacity for dithering. Color is not modified.
27+
float cutoutGroundRoofOpacity(vec4 groundRoof) {
28+
#ifdef INDICATOR_CUTOUT
29+
float fadeStartX = u_indicator_cutout_params.w;
30+
float holeRadius = u_indicator_cutout_params.y;
31+
32+
float holeMinOpacity = mix(u_indicator_cutout_params.x, 1.0,
33+
smoothstep(u_indicator_cutout_params.z, u_indicator_cutout_centers.z, groundRoof.y));
34+
35+
float distX = abs(u_indicator_cutout_centers.x - groundRoof.x);
36+
37+
float roofOpacity = mix(holeMinOpacity, 1.0,
38+
smoothstep(fadeStartX, holeRadius,
39+
u_indicator_cutout_centers.y - groundRoof.w));
40+
41+
float groundOpacity = min(smoothstep(fadeStartX, holeRadius, distX)
42+
+ holeMinOpacity, 1.0);
43+
44+
return max(roofOpacity, groundOpacity);
45+
#else
46+
return 1.0;
47+
#endif
48+
}

src/shaders/_prelude_terrain.vertex.glsl

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@
88
uniform vec3 u_tile_br_up;
99
uniform vec3 u_tile_bl_up;
1010
uniform float u_tile_up_scale;
11-
vec3 elevationVector(vec2 pos) {
12-
vec2 uv = pos / EXTENT;
13-
vec3 up = normalize(mix(
14-
mix(u_tile_tl_up, u_tile_tr_up, uv.xxx),
15-
mix(u_tile_bl_up, u_tile_br_up, uv.xxx),
16-
uv.yyy));
17-
return up * u_tile_up_scale;
18-
}
11+
#endif // PROJECTION_GLOBE_VIEW
12+
13+
vec3 elevationVector(vec2 pos) {
14+
#ifdef PROJECTION_GLOBE_VIEW
15+
vec2 uv = pos / EXTENT;
16+
vec3 up = normalize(mix(
17+
mix(u_tile_tl_up, u_tile_tr_up, uv.xxx),
18+
mix(u_tile_bl_up, u_tile_br_up, uv.xxx),
19+
uv.yyy));
20+
return up * u_tile_up_scale;
1921
#else // PROJECTION_GLOBE_VIEW
20-
vec3 elevationVector(vec2 pos) { return vec3(0, 0, 1); }
22+
return vec3(0, 0, 1);
2123
#endif // PROJECTION_GLOBE_VIEW
24+
}
2225

2326
#ifdef TERRAIN
2427

@@ -79,24 +82,6 @@
7982
#endif // TERRAIN_DEM_FLOAT_FORMAT
8083
}
8184

82-
#ifdef TERRAIN_VERTEX_MORPHING
83-
float elevation(vec2 apos) {
84-
#ifdef ZERO_EXAGGERATION
85-
return 0.0;
86-
#endif // ZERO_EXAGGERATION
87-
float nextElevation = currentElevation(apos);
88-
float prevElevation = prevElevation(apos);
89-
return mix(prevElevation, nextElevation, u_dem_lerp);
90-
}
91-
#else // TERRAIN_VERTEX_MORPHING
92-
float elevation(vec2 apos) {
93-
#ifdef ZERO_EXAGGERATION
94-
return 0.0;
95-
#endif // ZERO_EXAGGERATION
96-
return currentElevation(apos);
97-
}
98-
#endif // TERRAIN_VERTEX_MORPHING
99-
10085
// BEGIN: code for fill-extrusion height offseting
10186
// When making changes here please also update associated JS ports in src/style/style_layer/fill-extrusion-style-layer.js
10287
// This is so that rendering changes are reflected on CPU side for feature querying.
@@ -144,11 +129,24 @@
144129

145130
// END: code for fill-extrusion height offseting
146131

147-
#else // TERRAIN
148-
149-
float elevation(vec2 pos) { return 0.0; }
132+
#endif // TERRAIN
150133

151-
#endif
134+
float elevation(vec2 apos) {
135+
#ifdef TERRAIN
136+
#ifdef ZERO_EXAGGERATION
137+
return 0.0;
138+
#endif // ZERO_EXAGGERATION
139+
#ifdef TERRAIN_VERTEX_MORPHING
140+
float nextElevation = currentElevation(apos);
141+
float prevElevation = prevElevation(apos);
142+
return mix(prevElevation, nextElevation, u_dem_lerp);
143+
#else // TERRAIN_VERTEX_MORPHING
144+
return currentElevation(apos);
145+
#endif // TERRAIN_VERTEX_MORPHING
146+
#else // TERRAIN
147+
return 0.0;
148+
#endif // TERRAIN
149+
}
152150

153151
#ifdef DEPTH_OCCLUSION
154152

0 commit comments

Comments
 (0)