Debian Codegraph @

lvgl/src/drivers/opengles/assets/lv_opengles_shader.c

raw symbols SCIP
1 #include "lv_opengles_shader.h"
2
3 #if LV_USE_OPENGLES
4
5 #include "../opengl_shader/lv_opengl_shader_internal.h"
6 #include "../../../misc/lv_types.h"
7
8 static const lv_opengl_shader_t src_includes_v100[] = {{
9 "hsv_adjust.glsl", R"(
10
11 uniform float u_Hue;
12 uniform float u_Saturation;
13 uniform float u_Value;
14
15 // Convert RGB to HSV
16 vec3 rgb2hsv(vec3 c) {
17 vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
18 vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
19 vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
20 float d = q.x - min(q.w, q.y);
21 float e = 1.0e-10;
22 return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
23 }
24
25 // Convert HSV to RGB
26 vec3 hsv2rgb(vec3 c) {
27 vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
28 vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
29 return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
30 }
31
32 vec3 adjustHSV(vec3 color){
33 vec3 hsv = rgb2hsv(color);
34 hsv.x = fract(hsv.x + u_Hue);
35 hsv.y = clamp(hsv.y * u_Saturation, 0.0, 1.0);
36 hsv.z = clamp(hsv.z * u_Value, 0.0, 1.0);
37 return hsv2rgb(hsv);
38 }
39 )"
40 }, {
41 "brightness_adjust.glsl", R"(
42 uniform float u_Brightness; // add/subtract in [ -1.0 .. +1.0 ], 0.0 = no change
43
44 vec3 adjustBrightness(vec3 color){
45 return clamp(color + vec3(u_Brightness), 0.0, 1.0);
46 }
47
48 )"
49 }, {
50 "contrast_adjust.glsl", R"(
51 uniform float u_Contrast; // 0.0 = mid-gray, 1.0 = no change, >1.0 increases contrast
52
53 vec3 adjustContrast(vec3 color){
54 // shift to [-0.5..0.5], scale, shift back
55 return clamp(((color - 0.5) * u_Contrast) + 0.5, 0.0, 1.0);
56 }
57 )"
58 },
59 };
60
61 static const char * src_vertex_shader_v100 = R"(
62 precision mediump float;
63
64 attribute vec4 position;
65 attribute vec2 texCoord;
66
67 varying vec2 v_TexCoord;
68
69 uniform mat3 u_VertexTransform;
70
71 void main()
72 {
73 gl_Position = vec4((u_VertexTransform * vec3(position.xy, 1.0)).xy, position.zw);
74 v_TexCoord = texCoord;
75 }
76 )";
77
78 static const char *src_fragment_shader_v100 = R"(
79 precision lowp float;
80
81 varying vec2 v_TexCoord;
82
83 uniform sampler2D u_Texture;
84 uniform float u_ColorDepth;
85 uniform float u_Opa;
86 uniform bool u_IsFill;
87 uniform vec3 u_FillColor;
88 uniform bool u_SwapRB;
89
90 #ifdef HSV_ADJUST
91 #include <hsv_adjust.glsl>
92 #endif
93
94 void main()
95 {
96 vec4 texColor;
97 if (u_IsFill) {
98 texColor = vec4(u_FillColor, 1.0);
99 } else {
100 texColor = texture2D(u_Texture, v_TexCoord);
101 }
102 if (abs(u_ColorDepth - 8.0) < 0.1) {
103 float gray = texColor.r;
104 gl_FragColor = vec4(vec3(gray * u_Opa), u_Opa);
105 } else {
106 float combinedAlpha = texColor.a * u_Opa;
107 gl_FragColor = vec4(texColor.rgb * combinedAlpha, combinedAlpha);
108 }
109 if (u_SwapRB) {
110 gl_FragColor.bgr = gl_FragColor.rgb;
111 }
112 #ifdef HSV_ADJUST
113 gl_FragColor.rgb = adjustHSV(gl_FragColor.rgb);
114 #endif
115 }
116 )";
117
118 static const lv_opengl_shader_t src_includes_v300es[] = {{
119 "hsv_adjust.glsl", R"(
120 uniform float u_Hue;
121 uniform float u_Saturation;
122 uniform float u_Value;
123
124 // Convert RGB to HSV
125 vec3 rgb2hsv(vec3 c) {
126 vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
127 vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
128 vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
129 float d = q.x - min(q.w, q.y);
130 float e = 1.0e-10;
131 return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
132 }
133
134 // Convert HSV to RGB
135 vec3 hsv2rgb(vec3 c) {
136 vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
137 vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
138 return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
139 }
140
141 vec3 adjustHSV(vec3 color){
142 vec3 hsv = rgb2hsv(color);
143 hsv.x = fract(hsv.x + u_Hue);
144 hsv.y = clamp(hsv.y * u_Saturation, 0.0, 1.0);
145 hsv.z = clamp(hsv.z * u_Value, 0.0, 1.0);
146 return hsv2rgb(hsv);
147 }
148 )"
149 }, {
150 "brightness_adjust.glsl", R"(
151 uniform float u_Brightness; // add/subtract in [ -1.0 .. +1.0 ], 0.0 = no change
152
153 vec3 adjustBrightness(vec3 color){
154 return clamp(color + vec3(u_Brightness), 0.0, 1.0);
155 }
156
157 )"
158 }, {
159 "contrast_adjust.glsl", R"(
160 uniform float u_Contrast; // 0.0 = mid-gray, 1.0 = no change, >1.0 increases contrast
161
162 vec3 adjustContrast(vec3 color){
163 // shift to [-0.5..0.5], scale, shift back
164 return clamp(((color - 0.5) * u_Contrast) + 0.5, 0.0, 1.0);
165 }
166 )"
167 },
168 };
169
170 static const char * src_vertex_shader_v300es = R"(
171 precision mediump float;
172
173 in vec4 position;
174 in vec2 texCoord;
175
176 out vec2 v_TexCoord;
177 flat out lowp vec4 fill_color_alpha;
178 flat out lowp int is_gray;
179
180 uniform lowp float u_Opa;
181 uniform bool u_IsFill;
182 uniform vec3 u_FillColor;
183 uniform mat3 u_VertexTransform;
184 uniform float u_ColorDepth;
185
186 void main()
187 {
188 gl_Position = vec4((u_VertexTransform * vec3(position.xy, 1)).xy, position.zw);
189 v_TexCoord = texCoord;
190 is_gray = (abs(u_ColorDepth - 8.0) < 0.1) ? 1 : 0;
191
192 if (u_IsFill) {
193 if (is_gray == 1) {
194 fill_color_alpha = vec4(u_FillColor.rrr, 1.0) * u_Opa;
195 } else {
196 fill_color_alpha = vec4((u_FillColor.rgb * u_Opa), u_Opa);
197 }
198 } else {
199 fill_color_alpha = vec4(0.0, 0.0, 0.0, -1.0);
200 }
201 }
202 )";
203
204 static const char *src_fragment_shader_v300es = R"(
205 precision lowp float;
206
207 out vec4 color;
208
209 in vec2 v_TexCoord;
210 flat in lowp vec4 fill_color_alpha;
211 flat in lowp int is_gray;
212
213 uniform sampler2D u_Texture;
214 uniform lowp float u_Opa;
215 uniform bool u_SwapRB;
216
217 #ifdef HSV_ADJUST
218 #include <hsv_adjust.glsl>
219 #endif
220
221 void main()
222 {
223 if (fill_color_alpha.a != -1.0) {
224 color = fill_color_alpha;
225 } else {
226 color = texture(u_Texture, v_TexCoord);
227 /* If the vertices have been transformed, and mipmaps have not been generated,
228 * some rotation angles (notably 90 and 270) require using textureLod() to mitigate
229 * derivative calculation errors from interpolator increments flipping direction.
230 * color = textureLod(u_Texture, v_TexCoord, u_LodLevel);
231 */
232 if (is_gray != 0) {
233 color.r *= u_Opa;
234 color.gba = vec3(color.rr, u_Opa);
235 } else {
236 color.a *= u_Opa;
237 color.rgb *= color.a;
238 }
239 }
240 if (u_SwapRB) {
241 color.bgr = color.rgb;
242 }
243 #ifdef HSV_ADJUST
244 color.rgb = adjustHSV(color.rgb);
245 #endif
246 }
247 )";
248
249 static const size_t src_includes_v100_count = sizeof src_includes_v100 / sizeof src_includes_v100[0];
250 static const size_t src_includes_v300es_count = sizeof src_includes_v300es / sizeof src_includes_v300es[0];
251
252 /**********************
253 * GLOBAL FUNCTIONS
254 **********************/
255
256 char * lv_opengles_shader_get_vertex(lv_opengl_glsl_version_t version) {
257 switch (version){
258 case LV_OPENGL_GLSL_VERSION_330:
259 case LV_OPENGL_GLSL_VERSION_300ES:
260 return lv_opengl_shader_manager_process_includes(src_vertex_shader_v300es, src_includes_v300es, src_includes_v300es_count);
261 case LV_OPENGL_GLSL_VERSION_100:
262 return lv_opengl_shader_manager_process_includes(src_vertex_shader_v100, src_includes_v100, src_includes_v100_count);
263 case LV_OPENGL_GLSL_VERSION_LAST:
264 LV_LOG_ERROR("Invalid glsl version %d", version);
265 return NULL;
266 }
267 LV_UNREACHABLE();
268 }
269
270 char * lv_opengles_shader_get_fragment(lv_opengl_glsl_version_t version) {
271 switch (version){
272 case LV_OPENGL_GLSL_VERSION_330:
273 case LV_OPENGL_GLSL_VERSION_300ES:
274 return lv_opengl_shader_manager_process_includes(src_fragment_shader_v300es, src_includes_v300es, src_includes_v300es_count);
275 case LV_OPENGL_GLSL_VERSION_100:
276 return lv_opengl_shader_manager_process_includes(src_fragment_shader_v100, src_includes_v100, src_includes_v100_count);
277 case LV_OPENGL_GLSL_VERSION_LAST:
278 LV_LOG_ERROR("Invalid glsl version %d", version);
279 return NULL;
280 }
281 LV_UNREACHABLE();
282 }
283
284 void lv_opengles_shader_get_source(lv_opengl_shader_portions_t *portions, lv_opengl_glsl_version_t version)
285 {
286 switch (version){
287 case LV_OPENGL_GLSL_VERSION_330:
288 case LV_OPENGL_GLSL_VERSION_300ES:
289 portions->all = src_includes_v300es;
290 portions->count = src_includes_v300es_count;
291 return;
292 case LV_OPENGL_GLSL_VERSION_100:
293 portions->all = src_includes_v100;
294 portions->count = src_includes_v100_count;
295 return;
296 case LV_OPENGL_GLSL_VERSION_LAST:
297 LV_LOG_ERROR("Invalid glsl version %d", version);
298 portions->count = 0;
299 return;
300 }
301
302 LV_UNREACHABLE();
303 }
304
305 #endif /*LV_USE_OPENGLES*/