MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
line_sdf.hpp
Go to the documentation of this file.
1 // Generated code, do not modify this file!
2 // Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
3 
4 #pragma once
6 
7 namespace mbgl {
8 namespace shaders {
9 
11  static constexpr const char* vertex = R"(// floor(127 / 2) == 63.0
12 // the maximum allowed miter limit is 2.0 at the moment. the extrude normal is
13 // stored in a byte (-128..127). we scale regular normals up to length 63, but
14 // there are also "special" normals that have a bigger length (of up to 126 in
15 // this case).
16 // #define scale 63.0
17 #define scale 0.015873016
18 
19 // We scale the distance before adding it to the buffers so that we can store
20 // long distances for long segments. Use this value to unscale the distance.
21 #define LINE_DISTANCE_SCALE 2.0
22 
23 layout (location = 0) in vec2 a_pos_normal;
24 layout (location = 1) in vec4 a_data;
25 
26 uniform mat4 u_matrix;
27 uniform mediump float u_ratio;
28 uniform lowp float u_device_pixel_ratio;
29 uniform vec2 u_patternscale_a;
30 uniform float u_tex_y_a;
31 uniform vec2 u_patternscale_b;
32 uniform float u_tex_y_b;
33 uniform vec2 u_units_to_pixels;
34 
35 out vec2 v_normal;
36 out vec2 v_width2;
37 out vec2 v_tex_a;
38 out vec2 v_tex_b;
39 out float v_gamma_scale;
40 
41 #ifndef HAS_UNIFORM_u_color
42 uniform lowp float u_color_t;
43 layout (location = 2) in highp vec4 a_color;
44 out highp vec4 color;
45 #else
46 uniform highp vec4 u_color;
47 #endif
48 #ifndef HAS_UNIFORM_u_blur
49 uniform lowp float u_blur_t;
50 layout (location = 3) in lowp vec2 a_blur;
51 out lowp float blur;
52 #else
53 uniform lowp float u_blur;
54 #endif
55 #ifndef HAS_UNIFORM_u_opacity
56 uniform lowp float u_opacity_t;
57 layout (location = 4) in lowp vec2 a_opacity;
58 out lowp float opacity;
59 #else
60 uniform lowp float u_opacity;
61 #endif
62 #ifndef HAS_UNIFORM_u_gapwidth
63 uniform lowp float u_gapwidth_t;
64 layout (location = 5) in mediump vec2 a_gapwidth;
65 #else
66 uniform mediump float u_gapwidth;
67 #endif
68 #ifndef HAS_UNIFORM_u_offset
69 uniform lowp float u_offset_t;
70 layout (location = 6) in lowp vec2 a_offset;
71 #else
72 uniform lowp float u_offset;
73 #endif
74 #ifndef HAS_UNIFORM_u_width
75 uniform lowp float u_width_t;
76 layout (location = 7) in mediump vec2 a_width;
77 out mediump float width;
78 #else
79 uniform mediump float u_width;
80 #endif
81 #ifndef HAS_UNIFORM_u_floorwidth
82 uniform lowp float u_floorwidth_t;
83 layout (location = 8) in lowp vec2 a_floorwidth;
84 out lowp float floorwidth;
85 #else
86 uniform lowp float u_floorwidth;
87 #endif
88 
89 void main() {
90  #ifndef HAS_UNIFORM_u_color
91 color = unpack_mix_color(a_color, u_color_t);
92 #else
93 highp vec4 color = u_color;
94 #endif
95  #ifndef HAS_UNIFORM_u_blur
96 blur = unpack_mix_vec2(a_blur, u_blur_t);
97 #else
98 lowp float blur = u_blur;
99 #endif
100  #ifndef HAS_UNIFORM_u_opacity
101 opacity = unpack_mix_vec2(a_opacity, u_opacity_t);
102 #else
103 lowp float opacity = u_opacity;
104 #endif
105  #ifndef HAS_UNIFORM_u_gapwidth
106 mediump float gapwidth = unpack_mix_vec2(a_gapwidth, u_gapwidth_t);
107 #else
108 mediump float gapwidth = u_gapwidth;
109 #endif
110  #ifndef HAS_UNIFORM_u_offset
111 lowp float offset = unpack_mix_vec2(a_offset, u_offset_t);
112 #else
113 lowp float offset = u_offset;
114 #endif
115  #ifndef HAS_UNIFORM_u_width
116 width = unpack_mix_vec2(a_width, u_width_t);
117 #else
118 mediump float width = u_width;
119 #endif
120  #ifndef HAS_UNIFORM_u_floorwidth
121 floorwidth = unpack_mix_vec2(a_floorwidth, u_floorwidth_t);
122 #else
123 lowp float floorwidth = u_floorwidth;
124 #endif
125 
126  // the distance over which the line edge fades out.
127  // Retina devices need a smaller distance to avoid aliasing.
128  float ANTIALIASING = 1.0 / u_device_pixel_ratio / 2.0;
129 
130  vec2 a_extrude = a_data.xy - 128.0;
131  float a_direction = mod(a_data.z, 4.0) - 1.0;
132  float a_linesofar = (floor(a_data.z / 4.0) + a_data.w * 64.0) * LINE_DISTANCE_SCALE;
133 
134  vec2 pos = floor(a_pos_normal * 0.5);
135 
136  // x is 1 if it's a round cap, 0 otherwise
137  // y is 1 if the normal points up, and -1 if it points down
138  // We store these in the least significant bit of a_pos_normal
139  mediump vec2 normal = a_pos_normal - 2.0 * pos;
140  normal.y = normal.y * 2.0 - 1.0;
141  v_normal = normal;
142 
143  // these transformations used to be applied in the JS and native code bases.
144  // moved them into the shader for clarity and simplicity.
145  gapwidth = gapwidth / 2.0;
146  float halfwidth = width / 2.0;
147  offset = -1.0 * offset;
148 
149  float inset = gapwidth + (gapwidth > 0.0 ? ANTIALIASING : 0.0);
150  float outset = gapwidth + halfwidth * (gapwidth > 0.0 ? 2.0 : 1.0) + (halfwidth == 0.0 ? 0.0 : ANTIALIASING);
151 
152  // Scale the extrusion vector down to a normal and then up by the line width
153  // of this vertex.
154  mediump vec2 dist =outset * a_extrude * scale;
155 
156  // Calculate the offset when drawing a line that is to the side of the actual line.
157  // We do this by creating a vector that points towards the extrude, but rotate
158  // it when we're drawing round end points (a_direction = -1 or 1) since their
159  // extrude vector points in another direction.
160  mediump float u = 0.5 * a_direction;
161  mediump float t = 1.0 - abs(u);
162  mediump vec2 offset2 = offset * a_extrude * scale * normal.y * mat2(t, -u, u, t);
163 
164  vec4 projected_extrude = u_matrix * vec4(dist / u_ratio, 0.0, 0.0);
165  gl_Position = u_matrix * vec4(pos + offset2 / u_ratio, 0.0, 1.0) + projected_extrude;
166 
167  // calculate how much the perspective view squishes or stretches the extrude
168  float extrude_length_without_perspective = length(dist);
169  float extrude_length_with_perspective = length(projected_extrude.xy / gl_Position.w * u_units_to_pixels);
170  v_gamma_scale = extrude_length_without_perspective / extrude_length_with_perspective;
171 
172  v_tex_a = vec2(a_linesofar * u_patternscale_a.x / floorwidth, normal.y * u_patternscale_a.y + u_tex_y_a);
173  v_tex_b = vec2(a_linesofar * u_patternscale_b.x / floorwidth, normal.y * u_patternscale_b.y + u_tex_y_b);
174 
175  v_width2 = vec2(outset, inset);
176 }
177 )";
178  static constexpr const char* fragment = R"(
179 uniform lowp float u_device_pixel_ratio;
180 uniform sampler2D u_image;
181 uniform float u_sdfgamma;
182 uniform float u_mix;
183 
184 in vec2 v_normal;
185 in vec2 v_width2;
186 in vec2 v_tex_a;
187 in vec2 v_tex_b;
188 in float v_gamma_scale;
189 
190 #ifndef HAS_UNIFORM_u_color
191 in highp vec4 color;
192 #else
193 uniform highp vec4 u_color;
194 #endif
195 #ifndef HAS_UNIFORM_u_blur
196 in lowp float blur;
197 #else
198 uniform lowp float u_blur;
199 #endif
200 #ifndef HAS_UNIFORM_u_opacity
201 in lowp float opacity;
202 #else
203 uniform lowp float u_opacity;
204 #endif
205 #ifndef HAS_UNIFORM_u_width
206 in mediump float width;
207 #else
208 uniform mediump float u_width;
209 #endif
210 #ifndef HAS_UNIFORM_u_floorwidth
211 in lowp float floorwidth;
212 #else
213 uniform lowp float u_floorwidth;
214 #endif
215 
216 void main() {
217  #ifdef HAS_UNIFORM_u_color
218 highp vec4 color = u_color;
219 #endif
220  #ifdef HAS_UNIFORM_u_blur
221 lowp float blur = u_blur;
222 #endif
223  #ifdef HAS_UNIFORM_u_opacity
224 lowp float opacity = u_opacity;
225 #endif
226  #ifdef HAS_UNIFORM_u_width
227 mediump float width = u_width;
228 #endif
229  #ifdef HAS_UNIFORM_u_floorwidth
230 lowp float floorwidth = u_floorwidth;
231 #endif
232 
233  // Calculate the distance of the pixel from the line in pixels.
234  float dist = length(v_normal) * v_width2.s;
235 
236  // Calculate the antialiasing fade factor. This is either when fading in
237  // the line in case of an offset line (v_width2.t) or when fading out
238  // (v_width2.s)
239  float blur2 = (blur + 1.0 / u_device_pixel_ratio) * v_gamma_scale;
240  float alpha = clamp(min(dist - (v_width2.t - blur2), v_width2.s - dist) / blur2, 0.0, 1.0);
241 
242  float sdfdist_a = texture(u_image, v_tex_a).a;
243  float sdfdist_b = texture(u_image, v_tex_b).a;
244  float sdfdist = mix(sdfdist_a, sdfdist_b, u_mix);
245  alpha *= smoothstep(0.5 - u_sdfgamma / floorwidth, 0.5 + u_sdfgamma / floorwidth, sdfdist);
246 
247  fragColor = color * (alpha * opacity);
248 
249 #ifdef OVERDRAW_INSPECTOR
250  fragColor = vec4(1.0);
251 #endif
252 }
253 )";
254 };
255 
256 } // namespace shaders
257 } // namespace mbgl
@ OpenGL
The OpenGL API backend.
BuiltIn
This enum is used with the ShaderSource template to select source code for the desired program and gr...
Definition: actor.hpp:15
Select shader source based on a program type and a desired graphics API.