-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathMaskBlur.frag
72 lines (66 loc) · 1.73 KB
/
MaskBlur.frag
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
#extension GL_ARB_texture_rectangle : enable
uniform sampler2DRect tex, mask;
uniform vec2 direction;
uniform int k;
//#define LOW_RES
//#define USE_HARDWARE_INTERPOLATION
#define STANDARD
void main() {
vec2 pos = gl_TexCoord[0].st;
vec4 sum = texture2DRect(tex, pos);
int i;
// 350 fps
#ifdef LOW_RES
for(i = 2; i < k; i += 4) {
vec2 offset = float(i) * direction;
vec4 leftMask = texture2DRect(mask, pos - offset);
vec4 rightMask = texture2DRect(mask, pos + offset);
bool valid = leftMask.r == 1. && rightMask.r == 1.; // ignore black pixels
if(valid) {
sum +=
texture2DRect(tex, pos + offset) +
texture2DRect(tex, pos - offset);
} else {
break;
}
}
int samples = 1 + (i - 1) * 2 / 4;
#endif
// 240 fps
#ifdef USE_HARDWARE_INTERPOLATION
for(i = 2; i < k; i += 2) {
vec2 maskOffset = float(i) * direction;
vec4 leftMask = texture2DRect(mask, pos - maskOffset);
vec4 rightMask = texture2DRect(mask, pos + maskOffset);
bool valid = leftMask.r == 1. && rightMask.r == 1.; // ignore black pixels
if(valid) {
vec2 sampleOffset = (float(i) - .5) * direction;
sum +=
texture2DRect(tex, pos + sampleOffset) +
texture2DRect(tex, pos - sampleOffset);
} else {
break;
}
}
int samples = 1 + (i - 2);
#endif
// 140 fps
#ifdef STANDARD
int samples = 1;
for(i = 1; i < k; i++) {
vec2 curOffset = float(i) * direction;
vec4 leftMask = texture2DRect(mask, pos - curOffset);
vec4 rightMask = texture2DRect(mask, pos + curOffset);
bool valid = leftMask.r == 1. && rightMask.r == 1.; // ignore black pixels
if(valid) {
sum +=
texture2DRect(tex, pos + curOffset) +
texture2DRect(tex, pos - curOffset);
samples += 2;
} else {
break;
}
}
#endif
gl_FragColor = sum / float(samples);
}