summaryrefslogtreecommitdiff
path: root/src/modules/evas/engines/gl_common/shader/fragment.glsl
blob: b715c8de9924a65659ff8e33b666bac77c39efa5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* General-purpose fragment shader for all operations in Evas.
 * This file can either be used directly by evas at runtime to
 * generate its shaders with the appropriate #defines, or passed
 * through cpp first (in which case the precision must be manually added).
 */

FRAGMENT_SHADER

#define M_PI   3.141592653589793238462643383279502884
#define M_PI_2 1.570796326794896619231321691639751442

#ifndef SHD_NOMUL
varying vec4 col;
#endif

#ifdef SHD_EXTERNAL
uniform SAMPLER_EXTERNAL_OES tex;
varying vec2 tex_c;
#elif defined(SHD_TEX)
uniform sampler2D tex;
varying vec2 tex_c;
#endif

#if defined(SHD_NV12) || defined(SHD_YUY2)
uniform sampler2D texuv;
varying vec2 tex_c2;
#endif

#if defined(SHD_YUV)
uniform sampler2D texu;
uniform sampler2D texv;
varying vec2 tex_c2;
varying vec2 tex_c3;
#endif

#ifdef SHD_TEXA
uniform sampler2D texa;
varying vec2 tex_a;
#endif

#if defined(SHD_SAM12) || defined(SHD_SAM21) || defined(SHD_SAM22)
varying vec4 div_s;
# if defined(SHD_SAM12) || defined(SHD_SAM21)
varying vec2 tex_s[2];
# else
varying vec2 tex_s[4];
# endif
#endif

#ifdef SHD_MASK
uniform sampler2D texm;
varying vec2 tex_m;
# if defined(SHD_MASKSAM12) || defined(SHD_MASKSAM21)
varying float maskdiv_s;
varying vec2 masktex_s[2];
# elif defined(SHD_MASKSAM22)
varying float maskdiv_s;
varying vec2 masktex_s[4];
# endif
#endif

#ifdef SHD_ALPHA
# define SWZ aaaa
#else
# ifndef SHD_BGRA
#  if defined(SHD_IMG) && defined(SHD_BIGENDIAN)
#   define SWZ gbar
#  else
#   define SWZ bgra
#endif
# else
#  if defined(SHD_IMG) && defined(SHD_BIGENDIAN)
#   define SWZ grab
#  else
#   define SWZ rgba
#  endif
# endif
#endif

#ifdef SHD_FILTER_DISPLACE
uniform sampler2D tex_filter;
varying vec2 displace_vector;
varying vec2 displace_min;
varying vec2 displace_max;
#endif

#ifdef SHD_FILTER_CURVE
uniform sampler2D tex_filter;
#endif

#ifdef SHD_FILTER_BLUR
uniform sampler2D tex_filter;
uniform int blur_count;
uniform float blur_texlen;
uniform float blur_div;
#endif

// ----------------------------------------------------------------------------

#ifdef SHD_DITHER
const mat4 dm = mat4(vec4( 0,  8,  2, 10),
                     vec4(12,  4, 14,  6),
                     vec4( 3, 11,  1,  9),
                     vec4(15,  7, 13,  5));

float dither_closest(vec2 pos, float val)
{
   float limit = dm[int(pos.x)][int(pos.y)] / 16.0;
   if (val <= limit) return 0.0;
   return 1.0;
}

float dither_8bit(vec2 modpos, float val)
{
   float val_quant = float(floor(val * 255.0)) / 255.0;
   float val_delta = (val - val_quant) / (1.0 / 256.0);
   float val_roundup = dither_closest(modpos, val_delta);
   return val_quant + (val_roundup * (1.0 / 256.0));
}

vec4 dither(vec4 col, vec2 pos)
{
   vec2 modpos = vec2(mod(float(pos.x), 4.0), mod(float(pos.y), 4.0));
   return vec4(dither_8bit(modpos, col.r),
               dither_8bit(modpos, col.g),
               dither_8bit(modpos, col.b),
               dither_8bit(modpos, col.a));
}
#endif

#ifndef SHD_FILTER_BLUR
void main()
{
#if defined(SHD_EXTERNAL) || defined(SHD_TEX)
   vec2 coord = tex_c;
#endif

#else // SHD_FILTER_BLUR

vec4 fetch_pixel(float ox, float oy)
{
   vec2 coord = tex_c + vec2(ox, oy);

#endif // SHD_FILTER_BLUR

   vec4 c;

#ifdef SHD_FILTER_DISPLACE
   vec2 dxy = (texture2D(tex_filter, tex_c).rg - vec2(0.5, 0.5)) * displace_vector;
   float fa = texture2D(tex_filter, tex_c).a;
   coord = clamp(tex_c + dxy, displace_min, displace_max);
#endif

#if defined(SHD_YUV) || defined(SHD_NV12) || defined(SHD_YUY2)
   float r, g, b, y, u, v, vmu;
# if defined(SHD_YUV)
   y = texture2D(tex, tex_c).r;
   u = texture2D(texu, tex_c2).r;
   v = texture2D(texv, tex_c3).r;
# elif defined(SHD_NV12) || defined(SHD_YUY2)
   y = texture2D(tex, tex_c).g;
   u = texture2D(texuv, tex_c2).g;
   v = texture2D(texuv, tex_c2).a;
# endif
// center u and v around 0 for uv and y (with 128/255 for u + v, 16/255 for y)
   u = u - 0.5;
   v = v - 0.5;

# if defined (SHD_YUV_709)
// 709 yuv colorspace for hd content
   y = (y - 0.062) * 1.164;
   vmu = (v * 0.534) + (u * 0.213);
   v = v * 1.793;
   u = u * 2.115;
# else
// 601 colorspace constants (older yuv content)
   y = (y - 0.062) * 1.164;
   vmu = (v * 0.813) + (u * 0.391);
   v = v * 1.596;
   u = u * 2.018;
# endif
// common yuv
   r = y + v;
   g = y - vmu;
   b = y + u;
   c = vec4(r, g, b, 1.0);

#elif defined(SHD_SAM12) || defined(SHD_SAM21)
   vec4 col00 = texture2D(tex, coord + tex_s[0]).SWZ;
   vec4 col01 = texture2D(tex, coord + tex_s[1]).SWZ;
   c = (col00 + col01) / div_s;

#elif defined(SHD_SAM22)
   vec4 col00 = texture2D(tex, coord + tex_s[0]).SWZ;
   vec4 col01 = texture2D(tex, coord + tex_s[1]).SWZ;
   vec4 col10 = texture2D(tex, coord + tex_s[2]).SWZ;
   vec4 col11 = texture2D(tex, coord + tex_s[3]).SWZ;
   c = (col00 + col01 + col10 + col11) / div_s;

#elif defined(SHD_TEX) || defined(SHD_EXTERNAL)
   c = texture2D(tex, coord).SWZ;

#else
   c = vec4(1, 1, 1, 1);
#endif

#ifdef SHD_MASK
# ifndef SHD_MASK_COLOR
   // Classic mask: alpha only
   float ma;
#  if defined(SHD_MASKSAM12) || defined(SHD_MASKSAM21)
   float ma00 = texture2D(texm, tex_m + masktex_s[0]).a;
   float ma01 = texture2D(texm, tex_m + masktex_s[1]).a;
   ma = (ma00 + ma01) / maskdiv_s;
#  elif defined(SHD_MASKSAM22)
   float ma00 = texture2D(texm, tex_m + masktex_s[0]).a;
   float ma01 = texture2D(texm, tex_m + masktex_s[1]).a;
   float ma10 = texture2D(texm, tex_m + masktex_s[2]).a;
   float ma11 = texture2D(texm, tex_m + masktex_s[3]).a;
   ma = (ma00 + ma01 + ma10 + ma11) / maskdiv_s;
#  else
   ma = texture2D(texm, tex_m).a;
#  endif
# else
   // Full color mask
   vec4 ma;
#  if defined(SHD_MASKSAM12) || defined(SHD_MASKSAM21)
   vec4 ma00 = texture2D(texm, tex_m + masktex_s[0]);
   vec4 ma01 = texture2D(texm, tex_m + masktex_s[1]);
   ma = (ma00 + ma01) / maskdiv_s;
#  elif defined(SHD_MASKSAM22)
   vec4 ma00 = texture2D(texm, tex_m + masktex_s[0]);
   vec4 ma01 = texture2D(texm, tex_m + masktex_s[1]);
   vec4 ma10 = texture2D(texm, tex_m + masktex_s[2]);
   vec4 ma11 = texture2D(texm, tex_m + masktex_s[3]);
   ma = (ma00 + ma01 + ma10 + ma11) / maskdiv_s;
#  else
   ma = texture2D(texm, tex_m);
#  endif
# endif
#endif

#ifdef SHD_AFILL
   c.a = 1.0;
#endif

#ifdef SHD_TEXA
   c *= texture2D(texa, tex_a).r;
#endif

#if defined(SHD_FILTER_CURVE)
   float old_alpha = max(c.a, 0.00001);
   float new_alpha = texture2D(tex_filter, vec2(old_alpha, 0.0)).a;
   c = vec4(texture2D(tex_filter, vec2(c.r / old_alpha, 0.0)).r * new_alpha,
            texture2D(tex_filter, vec2(c.g / old_alpha, 0.0)).g * new_alpha,
            texture2D(tex_filter, vec2(c.b / old_alpha, 0.0)).b * new_alpha,
            new_alpha);
#endif

#ifdef SHD_ALPHA_ONLY
   c = vec4(c.a, c.a, c.a, c.a);
#endif

#ifdef SHD_FILTER_GRAYSCALE
   c.r = 0.3 * c.r + 0.59 * c.g + 0.11 * c.b;
   c.g = c.r;
   c.b = c.r;
#endif

#ifdef SHD_FILTER_INVERSE_COLOR
   c.rgb = c.a - c.rgba;
#endif

#ifndef SHD_FILTER_BLUR

   gl_FragColor =
#ifdef SHD_DITHER
   dither(
#endif
       c
#ifndef SHD_NOMUL
     * col
#endif
#ifdef SHD_MASK
     * ma
#endif
#ifdef SHD_FILTER_DISPLACE
     * fa
#endif
#ifdef SHD_DITHER
   , gl_FragCoord.xy)
#endif
   ;
}

#else // SHD_FILTER_BLUR

   return c;
}

#ifndef SHD_FILTER_DIR_Y
# define FETCH_PIXEL(x) fetch_pixel((x), 0.0)
#else
# define FETCH_PIXEL(x) fetch_pixel(0.0, (x))
#endif

float weight_get(float u, float count, float index)
{
   vec4 val = texture2D(tex_filter, vec2(u / count, index)).bgra;
   return val.a * 255.0 + val.r + val.g / 256.0 + val.b / 65536.0;
}

float offset_get(float u, float count, float index)
{
   // val.a is always 0 here ~ discard
   vec4 val = texture2D(tex_filter, vec2(u / count, index)).bgra;
   return val.r + val.g / 256.0 + val.b / 65536.0;
}

void main()
{
   float weight, offset, count;
   vec4 acc, px;
   int k;

   count = float(blur_count);


   // Center pixel, offset is 0.0
   weight = weight_get(0.0, count, 0.0);
   px = FETCH_PIXEL(0.0);
   acc = px * weight;

   // Left & right pixels
   for (k = 1; k <= blur_count; k++)
   {
      float u = float(k);

      weight = weight_get(u, count, 0.0);
      offset = offset_get(u, count, 1.0);

      // Left
      vec4 px1 = FETCH_PIXEL(-((offset + (2.0 * u) - 1.0)) / blur_texlen);

      // Right
      vec4 px2 = FETCH_PIXEL((offset + (2.0 * u) - 1.0) / blur_texlen);

      acc += (px1 + px2) * weight;
   }

#ifndef SHD_NOMUL
   gl_FragColor =
#ifdef SHD_DITHER
     dither(
#endif
            (acc / blur_div) * col, gl_FragCoord.xy
#ifdef SHD_DITHER
           )
#endif
   ;
#else
   gl_FragColor =
#ifdef SHD_DITHER
     dither(
#endif
            (acc / blur_div), gl_FragCoord.xy
#ifdef SHD_DITHER
           )
#endif
   ;
#endif
}

#endif // SHD_FILTER_BLUR