MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
line_gradient.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"(
12 // the attribute conveying progress along a line is scaled to [0, 2^15)
13 #define MAX_LINE_DISTANCE 32767.0
14 
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
19 // this case).
20 // #define scale 63.0
21 #define scale 0.015873016
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_units_to_pixels;
30 
31 out vec2 v_normal;
32 out vec2 v_width2;
33 out float v_gamma_scale;
34 out highp float v_lineprogress;
35 
36 #ifndef HAS_UNIFORM_u_blur
37 uniform lowp float u_blur_t;
38 layout (location = 2) in lowp vec2 a_blur;
39 out lowp float blur;
40 #else
41 uniform lowp float u_blur;
42 #endif
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;
47 #else
48 uniform lowp float u_opacity;
49 #endif
50 #ifndef HAS_UNIFORM_u_gapwidth
51 uniform lowp float u_gapwidth_t;
52 layout (location = 4) in mediump vec2 a_gapwidth;
53 #else
54 uniform mediump float u_gapwidth;
55 #endif
56 #ifndef HAS_UNIFORM_u_offset
57 uniform lowp float u_offset_t;
58 layout (location = 5) in lowp vec2 a_offset;
59 #else
60 uniform lowp float u_offset;
61 #endif
62 #ifndef HAS_UNIFORM_u_width
63 uniform lowp float u_width_t;
64 layout (location = 6) in mediump vec2 a_width;
65 #else
66 uniform mediump float u_width;
67 #endif
68 
69 void main() {
70  #ifndef HAS_UNIFORM_u_blur
71 blur = unpack_mix_vec2(a_blur, u_blur_t);
72 #else
73 lowp float blur = u_blur;
74 #endif
75  #ifndef HAS_UNIFORM_u_opacity
76 opacity = unpack_mix_vec2(a_opacity, u_opacity_t);
77 #else
78 lowp float opacity = u_opacity;
79 #endif
80  #ifndef HAS_UNIFORM_u_gapwidth
81 mediump float gapwidth = unpack_mix_vec2(a_gapwidth, u_gapwidth_t);
82 #else
83 mediump float gapwidth = u_gapwidth;
84 #endif
85  #ifndef HAS_UNIFORM_u_offset
86 lowp float offset = unpack_mix_vec2(a_offset, u_offset_t);
87 #else
88 lowp float offset = u_offset;
89 #endif
90  #ifndef HAS_UNIFORM_u_width
91 mediump float width = unpack_mix_vec2(a_width, u_width_t);
92 #else
93 mediump float width = u_width;
94 #endif
95 
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;
99 
100  vec2 a_extrude = a_data.xy - 128.0;
101  float a_direction = mod(a_data.z, 4.0) - 1.0;
102 
103  v_lineprogress = (floor(a_data.z / 4.0) + a_data.w * 64.0) * 2.0 / MAX_LINE_DISTANCE;
104 
105  vec2 pos = floor(a_pos_normal * 0.5);
106 
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;
112  v_normal = normal;
113 
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;
119 
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);
122 
123  // Scale the extrusion vector down to a normal and then up by the line width
124  // of this vertex.
125  mediump vec2 dist = outset * a_extrude * scale;
126 
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);
134 
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;
137 
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;
142 
143  v_width2 = vec2(outset, inset);
144 }
145 )";
146  static constexpr const char* fragment = R"(uniform lowp float u_device_pixel_ratio;
147 uniform sampler2D u_image;
148 
149 in vec2 v_width2;
150 in vec2 v_normal;
151 in float v_gamma_scale;
152 in highp float v_lineprogress;
153 
154 #ifndef HAS_UNIFORM_u_blur
155 in lowp float blur;
156 #else
157 uniform lowp float u_blur;
158 #endif
159 #ifndef HAS_UNIFORM_u_opacity
160 in lowp float opacity;
161 #else
162 uniform lowp float u_opacity;
163 #endif
164 
165 void main() {
166  #ifdef HAS_UNIFORM_u_blur
167 lowp float blur = u_blur;
168 #endif
169  #ifdef HAS_UNIFORM_u_opacity
170 lowp float opacity = u_opacity;
171 #endif
172 
173  // Calculate the distance of the pixel from the line in pixels.
174  float dist = length(v_normal) * v_width2.s;
175 
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
178  // (v_width2.s)
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);
181 
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));
185 
186  fragColor = color * (alpha * opacity);
187 
188 #ifdef OVERDRAW_INSPECTOR
189  fragColor = vec4(1.0);
190 #endif
191 }
192 )";
193 };
194 
195 } // namespace shaders
196 } // 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.