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
17 #define scale 0.015873016
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
23 layout (location = 0) in vec2 a_pos_normal;
24 layout (location = 1) in vec4 a_data;
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;
39 out float v_gamma_scale;
41 #ifndef HAS_UNIFORM_u_color
42 uniform lowp float u_color_t;
43 layout (location = 2) in highp vec4 a_color;
46 uniform highp vec4 u_color;
48 #ifndef HAS_UNIFORM_u_blur
49 uniform lowp float u_blur_t;
50 layout (location = 3) in lowp vec2 a_blur;
53 uniform lowp float u_blur;
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;
60 uniform lowp float u_opacity;
62 #ifndef HAS_UNIFORM_u_gapwidth
63 uniform lowp float u_gapwidth_t;
64 layout (location = 5) in mediump vec2 a_gapwidth;
66 uniform mediump float u_gapwidth;
68 #ifndef HAS_UNIFORM_u_offset
69 uniform lowp float u_offset_t;
70 layout (location = 6) in lowp vec2 a_offset;
72 uniform lowp float u_offset;
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;
79 uniform mediump float u_width;
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;
86 uniform lowp float u_floorwidth;
90 #ifndef HAS_UNIFORM_u_color
91 color = unpack_mix_color(a_color, u_color_t);
93 highp vec4 color = u_color;
95 #ifndef HAS_UNIFORM_u_blur
96 blur = unpack_mix_vec2(a_blur, u_blur_t);
98 lowp float blur = u_blur;
100 #ifndef HAS_UNIFORM_u_opacity
101 opacity = unpack_mix_vec2(a_opacity, u_opacity_t);
103 lowp float opacity = u_opacity;
105 #ifndef HAS_UNIFORM_u_gapwidth
106 mediump float gapwidth = unpack_mix_vec2(a_gapwidth, u_gapwidth_t);
108 mediump float gapwidth = u_gapwidth;
110 #ifndef HAS_UNIFORM_u_offset
111 lowp float offset = unpack_mix_vec2(a_offset, u_offset_t);
113 lowp float offset = u_offset;
115 #ifndef HAS_UNIFORM_u_width
116 width = unpack_mix_vec2(a_width, u_width_t);
118 mediump float width = u_width;
120 #ifndef HAS_UNIFORM_u_floorwidth
121 floorwidth = unpack_mix_vec2(a_floorwidth, u_floorwidth_t);
123 lowp float floorwidth = u_floorwidth;
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;
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;
134 vec2 pos = floor(a_pos_normal * 0.5);
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;
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;
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);
152 // Scale the extrusion vector down to a normal and then up by the line width
154 mediump vec2 dist =outset * a_extrude * scale;
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);
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;
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;
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);
175 v_width2 = vec2(outset, inset);
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;
188 in float v_gamma_scale;
190 #ifndef HAS_UNIFORM_u_color
193 uniform highp vec4 u_color;
195 #ifndef HAS_UNIFORM_u_blur
198 uniform lowp float u_blur;
200 #ifndef HAS_UNIFORM_u_opacity
201 in lowp float opacity;
203 uniform lowp float u_opacity;
205 #ifndef HAS_UNIFORM_u_width
206 in mediump float width;
208 uniform mediump float u_width;
210 #ifndef HAS_UNIFORM_u_floorwidth
211 in lowp float floorwidth;
213 uniform lowp float u_floorwidth;
217 #ifdef HAS_UNIFORM_u_color
218 highp vec4 color = u_color;
220 #ifdef HAS_UNIFORM_u_blur
221 lowp float blur = u_blur;
223 #ifdef HAS_UNIFORM_u_opacity
224 lowp float opacity = u_opacity;
226 #ifdef HAS_UNIFORM_u_width
227 mediump float width = u_width;
229 #ifdef HAS_UNIFORM_u_floorwidth
230 lowp float floorwidth = u_floorwidth;
233 // Calculate the distance of the pixel from the line in pixels.
234 float dist = length(v_normal) * v_width2.s;
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
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);
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);
247 fragColor = color * (alpha * opacity);
249 #ifdef OVERDRAW_INSPECTOR
250 fragColor = vec4(1.0);
@ OpenGL
The OpenGL API backend.
BuiltIn
This enum is used with the ShaderSource template to select source code for the desired program and gr...
Select shader source based on a program type and a desired graphics API.