MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
line.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 layout (location = 0) in vec2 a_pos_normal;
20 layout (location = 1) in vec4 a_data;
21 
22 uniform mat4 u_matrix;
23 uniform mediump float u_ratio;
24 uniform vec2 u_units_to_pixels;
25 uniform lowp float u_device_pixel_ratio;
26 
27 out vec2 v_normal;
28 out vec2 v_width2;
29 out float v_gamma_scale;
30 out highp float v_linesofar;
31 
32 #ifndef HAS_UNIFORM_u_color
33 uniform lowp float u_color_t;
34 layout (location = 2) in highp vec4 a_color;
35 out highp vec4 color;
36 #else
37 uniform highp vec4 u_color;
38 #endif
39 #ifndef HAS_UNIFORM_u_blur
40 uniform lowp float u_blur_t;
41 layout (location = 3) in lowp vec2 a_blur;
42 out lowp float blur;
43 #else
44 uniform lowp float u_blur;
45 #endif
46 #ifndef HAS_UNIFORM_u_opacity
47 uniform lowp float u_opacity_t;
48 layout (location = 4) in lowp vec2 a_opacity;
49 out lowp float opacity;
50 #else
51 uniform lowp float u_opacity;
52 #endif
53 #ifndef HAS_UNIFORM_u_gapwidth
54 uniform lowp float u_gapwidth_t;
55 layout (location = 5) in mediump vec2 a_gapwidth;
56 #else
57 uniform mediump float u_gapwidth;
58 #endif
59 #ifndef HAS_UNIFORM_u_offset
60 uniform lowp float u_offset_t;
61 layout (location = 6) in lowp vec2 a_offset;
62 #else
63 uniform lowp float u_offset;
64 #endif
65 #ifndef HAS_UNIFORM_u_width
66 uniform lowp float u_width_t;
67 layout (location = 7) in mediump vec2 a_width;
68 #else
69 uniform mediump float u_width;
70 #endif
71 
72 void main() {
73  #ifndef HAS_UNIFORM_u_color
74 color = unpack_mix_color(a_color, u_color_t);
75 #else
76 highp vec4 color = u_color;
77 #endif
78  #ifndef HAS_UNIFORM_u_blur
79 blur = unpack_mix_vec2(a_blur, u_blur_t);
80 #else
81 lowp float blur = u_blur;
82 #endif
83  #ifndef HAS_UNIFORM_u_opacity
84 opacity = unpack_mix_vec2(a_opacity, u_opacity_t);
85 #else
86 lowp float opacity = u_opacity;
87 #endif
88  #ifndef HAS_UNIFORM_u_gapwidth
89 mediump float gapwidth = unpack_mix_vec2(a_gapwidth, u_gapwidth_t);
90 #else
91 mediump float gapwidth = u_gapwidth;
92 #endif
93  #ifndef HAS_UNIFORM_u_offset
94 lowp float offset = unpack_mix_vec2(a_offset, u_offset_t);
95 #else
96 lowp float offset = u_offset;
97 #endif
98  #ifndef HAS_UNIFORM_u_width
99 mediump float width = unpack_mix_vec2(a_width, u_width_t);
100 #else
101 mediump float width = u_width;
102 #endif
103 
104  // the distance over which the line edge fades out.
105  // Retina devices need a smaller distance to avoid aliasing.
106  float ANTIALIASING = 1.0 / u_device_pixel_ratio / 2.0;
107 
108  vec2 a_extrude = a_data.xy - 128.0;
109  float a_direction = mod(a_data.z, 4.0) - 1.0;
110 
111  v_linesofar = (floor(a_data.z / 4.0) + a_data.w * 64.0) * 2.0;
112 
113  vec2 pos = floor(a_pos_normal * 0.5);
114 
115  // x is 1 if it's a round cap, 0 otherwise
116  // y is 1 if the normal points up, and -1 if it points down
117  // We store these in the least significant bit of a_pos_normal
118  mediump vec2 normal = a_pos_normal - 2.0 * pos;
119  normal.y = normal.y * 2.0 - 1.0;
120  v_normal = normal;
121 
122  // these transformations used to be applied in the JS and native code bases.
123  // moved them into the shader for clarity and simplicity.
124  gapwidth = gapwidth / 2.0;
125  float halfwidth = width / 2.0;
126  offset = -1.0 * offset;
127 
128  float inset = gapwidth + (gapwidth > 0.0 ? ANTIALIASING : 0.0);
129  float outset = gapwidth + halfwidth * (gapwidth > 0.0 ? 2.0 : 1.0) + (halfwidth == 0.0 ? 0.0 : ANTIALIASING);
130 
131  // Scale the extrusion vector down to a normal and then up by the line width
132  // of this vertex.
133  mediump vec2 dist = outset * a_extrude * scale;
134 
135  // Calculate the offset when drawing a line that is to the side of the actual line.
136  // We do this by creating a vector that points towards the extrude, but rotate
137  // it when we're drawing round end points (a_direction = -1 or 1) since their
138  // extrude vector points in another direction.
139  mediump float u = 0.5 * a_direction;
140  mediump float t = 1.0 - abs(u);
141  mediump vec2 offset2 = offset * a_extrude * scale * normal.y * mat2(t, -u, u, t);
142 
143  vec4 projected_extrude = u_matrix * vec4(dist / u_ratio, 0.0, 0.0);
144  gl_Position = u_matrix * vec4(pos + offset2 / u_ratio, 0.0, 1.0) + projected_extrude;
145 
146  // calculate how much the perspective view squishes or stretches the extrude
147  float extrude_length_without_perspective = length(dist);
148  float extrude_length_with_perspective = length(projected_extrude.xy / gl_Position.w * u_units_to_pixels);
149  v_gamma_scale = extrude_length_without_perspective / extrude_length_with_perspective;
150 
151  v_width2 = vec2(outset, inset);
152 }
153 )";
154  static constexpr const char* fragment = R"(uniform lowp float u_device_pixel_ratio;
155 
156 in vec2 v_width2;
157 in vec2 v_normal;
158 in float v_gamma_scale;
159 
160 #ifndef HAS_UNIFORM_u_color
161 in highp vec4 color;
162 #else
163 uniform highp vec4 u_color;
164 #endif
165 #ifndef HAS_UNIFORM_u_blur
166 in lowp float blur;
167 #else
168 uniform lowp float u_blur;
169 #endif
170 #ifndef HAS_UNIFORM_u_opacity
171 in lowp float opacity;
172 #else
173 uniform lowp float u_opacity;
174 #endif
175 
176 void main() {
177  #ifdef HAS_UNIFORM_u_color
178 highp vec4 color = u_color;
179 #endif
180  #ifdef HAS_UNIFORM_u_blur
181 lowp float blur = u_blur;
182 #endif
183  #ifdef HAS_UNIFORM_u_opacity
184 lowp float opacity = u_opacity;
185 #endif
186 
187  // Calculate the distance of the pixel from the line in pixels.
188  float dist = length(v_normal) * v_width2.s;
189 
190  // Calculate the antialiasing fade factor. This is either when fading in
191  // the line in case of an offset line (v_width2.t) or when fading out
192  // (v_width2.s)
193  float blur2 = (blur + 1.0 / u_device_pixel_ratio) * v_gamma_scale;
194  float alpha = clamp(min(dist - (v_width2.t - blur2), v_width2.s - dist) / blur2, 0.0, 1.0);
195 
196  fragColor = color * (alpha * opacity);
197 
198 #ifdef OVERDRAW_INSPECTOR
199  fragColor = vec4(1.0);
200 #endif
201 }
202 )";
203 };
204 
205 } // namespace shaders
206 } // 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.