-
Notifications
You must be signed in to change notification settings - Fork 3
/
fft.glsl
66 lines (53 loc) · 1.45 KB
/
fft.glsl
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
const float TWOPI = 6.283185307179586;
const float PI2 = 1.5707963267948966;
vec4 fft (
sampler2D src,
vec2 resolution,
float subtransformSize,
bool horizontal,
bool forward
) {
vec2 twiddle;
vec4 rePos, imPos, evenVal, oddRe, oddIm;;
float index, evenIndex, twiddleArgument;
vec2 pos = gl_FragCoord.xy * resolution;
bool real = (horizontal ? pos.y : pos.x) < 0.5;
index = (horizontal ? gl_FragCoord.x : gl_FragCoord.y) - 0.5;
evenIndex = floor(index / subtransformSize) *
(subtransformSize * 0.5) +
mod(index, subtransformSize * 0.5) +
0.5;
if (horizontal) {
rePos = vec4(evenIndex, gl_FragCoord.y, evenIndex, gl_FragCoord.y);
} else {
rePos = vec4(gl_FragCoord.x, evenIndex, gl_FragCoord.x, evenIndex);
}
rePos *= resolution.xyxy;
if (horizontal) {
rePos.z += 0.5;
} else {
rePos.w += 0.5;
}
imPos = rePos;
if (horizontal) {
if (real) {
imPos.yw += 0.5;
} else {
rePos.yw -= 0.5;
}
} else {
if (real) {
imPos.xz += 0.5;
} else {
rePos.xz -= 0.5;
}
}
evenVal = texture2D(src, real ? rePos.xy : imPos.xy);
oddRe = texture2D(src, rePos.zw);
oddIm = texture2D(src, imPos.zw);
twiddleArgument = (forward ? TWOPI : -TWOPI) * (index / subtransformSize);
if (!real) twiddleArgument -= PI2;
twiddle = vec2(cos(twiddleArgument), sin(twiddleArgument));
return evenVal + twiddle.x * oddRe + -twiddle.y * oddIm;
}
#pragma glslify: export(fft)