11 static constexpr
const char* vertex = R
"(
12 // the attribute conveying progress along a line is scaled to [0, 2^15)
13 #define MAX_LINE_DISTANCE 32767.0
15 // floor(127 / 2) == 63.0
16 // the maximum allowed miter limit is 2.0 at the moment. the extrude normal is
17 // stored in a byte (-128..127). we scale regular normals up to length 63, but
18 // there are also "special" normals that have a bigger length (of up to 126 in
21 #define scale 0.015873016
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_units_to_pixels;
33 out float v_gamma_scale;
34 out highp float v_lineprogress;
36 #ifndef HAS_UNIFORM_u_blur
37 uniform lowp float u_blur_t;
38 layout (location = 2) in lowp vec2 a_blur;
41 uniform lowp float u_blur;
43 #ifndef HAS_UNIFORM_u_opacity
44 uniform lowp float u_opacity_t;
45 layout (location = 3) in lowp vec2 a_opacity;
46 out lowp float opacity;
48 uniform lowp float u_opacity;
50 #ifndef HAS_UNIFORM_u_gapwidth
51 uniform lowp float u_gapwidth_t;
52 layout (location = 4) in mediump vec2 a_gapwidth;
54 uniform mediump float u_gapwidth;
56 #ifndef HAS_UNIFORM_u_offset
57 uniform lowp float u_offset_t;
58 layout (location = 5) in lowp vec2 a_offset;
60 uniform lowp float u_offset;
62 #ifndef HAS_UNIFORM_u_width
63 uniform lowp float u_width_t;
64 layout (location = 6) in mediump vec2 a_width;
66 uniform mediump float u_width;
70 #ifndef HAS_UNIFORM_u_blur
71 blur = unpack_mix_vec2(a_blur, u_blur_t);
73 lowp float blur = u_blur;
75 #ifndef HAS_UNIFORM_u_opacity
76 opacity = unpack_mix_vec2(a_opacity, u_opacity_t);
78 lowp float opacity = u_opacity;
80 #ifndef HAS_UNIFORM_u_gapwidth
81 mediump float gapwidth = unpack_mix_vec2(a_gapwidth, u_gapwidth_t);
83 mediump float gapwidth = u_gapwidth;
85 #ifndef HAS_UNIFORM_u_offset
86 lowp float offset = unpack_mix_vec2(a_offset, u_offset_t);
88 lowp float offset = u_offset;
90 #ifndef HAS_UNIFORM_u_width
91 mediump float width = unpack_mix_vec2(a_width, u_width_t);
93 mediump float width = u_width;
96 // the distance over which the line edge fades out.
97 // Retina devices need a smaller distance to avoid aliasing.
98 float ANTIALIASING = 1.0 / u_device_pixel_ratio / 2.0;
100 vec2 a_extrude = a_data.xy - 128.0;
101 float a_direction = mod(a_data.z, 4.0) - 1.0;
103 v_lineprogress = (floor(a_data.z / 4.0) + a_data.w * 64.0) * 2.0 / MAX_LINE_DISTANCE;
105 vec2 pos = floor(a_pos_normal * 0.5);
107 // x is 1 if it's a round cap, 0 otherwise
108 // y is 1 if the normal points up, and -1 if it points down
109 // We store these in the least significant bit of a_pos_normal
110 mediump vec2 normal = a_pos_normal - 2.0 * pos;
111 normal.y = normal.y * 2.0 - 1.0;
114 // these transformations used to be applied in the JS and native code bases.
115 // moved them into the shader for clarity and simplicity.
116 gapwidth = gapwidth / 2.0;
117 float halfwidth = width / 2.0;
118 offset = -1.0 * offset;
120 float inset = gapwidth + (gapwidth > 0.0 ? ANTIALIASING : 0.0);
121 float outset = gapwidth + halfwidth * (gapwidth > 0.0 ? 2.0 : 1.0) + (halfwidth == 0.0 ? 0.0 : ANTIALIASING);
123 // Scale the extrusion vector down to a normal and then up by the line width
125 mediump vec2 dist = outset * a_extrude * scale;
127 // Calculate the offset when drawing a line that is to the side of the actual line.
128 // We do this by creating a vector that points towards the extrude, but rotate
129 // it when we're drawing round end points (a_direction = -1 or 1) since their
130 // extrude vector points in another direction.
131 mediump float u = 0.5 * a_direction;
132 mediump float t = 1.0 - abs(u);
133 mediump vec2 offset2 = offset * a_extrude * scale * normal.y * mat2(t, -u, u, t);
135 vec4 projected_extrude = u_matrix * vec4(dist / u_ratio, 0.0, 0.0);
136 gl_Position = u_matrix * vec4(pos + offset2 / u_ratio, 0.0, 1.0) + projected_extrude;
138 // calculate how much the perspective view squishes or stretches the extrude
139 float extrude_length_without_perspective = length(dist);
140 float extrude_length_with_perspective = length(projected_extrude.xy / gl_Position.w * u_units_to_pixels);
141 v_gamma_scale = extrude_length_without_perspective / extrude_length_with_perspective;
143 v_width2 = vec2(outset, inset);
146 static constexpr
const char* fragment = R
"(uniform lowp float u_device_pixel_ratio;
147 uniform sampler2D u_image;
151 in float v_gamma_scale;
152 in highp float v_lineprogress;
154 #ifndef HAS_UNIFORM_u_blur
157 uniform lowp float u_blur;
159 #ifndef HAS_UNIFORM_u_opacity
160 in lowp float opacity;
162 uniform lowp float u_opacity;
166 #ifdef HAS_UNIFORM_u_blur
167 lowp float blur = u_blur;
169 #ifdef HAS_UNIFORM_u_opacity
170 lowp float opacity = u_opacity;
173 // Calculate the distance of the pixel from the line in pixels.
174 float dist = length(v_normal) * v_width2.s;
176 // Calculate the antialiasing fade factor. This is either when fading in
177 // the line in case of an offset line (v_width2.t) or when fading out
179 float blur2 = (blur + 1.0 / u_device_pixel_ratio) * v_gamma_scale;
180 float alpha = clamp(min(dist - (v_width2.t - blur2), v_width2.s - dist) / blur2, 0.0, 1.0);
182 // For gradient lines, v_lineprogress is the ratio along the entire line,
183 // scaled to [0, 2^15), and the gradient ramp is stored in a texture.
184 vec4 color = texture(u_image, vec2(v_lineprogress, 0.5));
186 fragColor = color * (alpha * opacity);
188 #ifdef OVERDRAW_INSPECTOR
189 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.