]> git.sesse.net Git - movit/blob - fft_pass_effect.cpp
9e1b7b54af52bfcc087fff958a1f31e5cf7128f0
[movit] / fft_pass_effect.cpp
1 #include <GL/glew.h>
2 #include <math.h>
3
4 #include "effect_util.h"
5 #include "fft_pass_effect.h"
6 #include "util.h"
7
8 using namespace std;
9
10 FFTPassEffect::FFTPassEffect()
11         : input_width(1280),
12           input_height(720),
13           direction(HORIZONTAL)
14 {
15         register_int("fft_size", &fft_size);
16         register_int("direction", (int *)&direction);
17         register_int("pass_number", &pass_number);
18         register_int("inverse", &inverse);
19         glGenTextures(1, &tex);
20 }
21
22 FFTPassEffect::~FFTPassEffect()
23 {
24         glDeleteTextures(1, &tex);
25 }
26
27 string FFTPassEffect::output_fragment_shader()
28 {
29         char buf[256];
30         sprintf(buf, "#define DIRECTION_VERTICAL %d\n", (direction == VERTICAL));
31         return buf + read_file("fft_pass_effect.frag");
32 }
33
34 void FFTPassEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
35 {
36         Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
37
38         int input_size = (direction == VERTICAL) ? input_height : input_width;
39
40         // See the comments on changes_output_size() in the .h file to see
41         // why this is legal. It is _needed_ because it counteracts the
42         // precision issues we get because we sample the input texture with
43         // normalized coordinates (especially when the repeat count along
44         // the axis is not a power of two); we very rapidly end up in narrowly
45         // missing a texel center, which causes precision loss to propagate
46         // throughout the FFT.
47         assert(*sampler_num == 1);
48         glActiveTexture(GL_TEXTURE0);
49         check_error();
50         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
51         check_error();
52         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
53         check_error();
54
55         // The memory layout follows figure 5.2 on page 25 of
56         // http://gpuwave.sesse.net/gpuwave.pdf -- it can be a bit confusing
57         // at first, but is classically explained more or less as follows:
58         //
59         // The classic Cooley-Tukey decimation-in-time FFT algorithm works
60         // by first splitting input data into odd and even elements
61         // (e.g. bit-wise xxxxx0 and xxxxx1 for a size-32 FFT), then FFTing
62         // them separately and combining them using twiddle factors.
63         // So the outer pass (done _last_) looks only at the last bit,
64         // and does one such merge pass of sub-size N/2 (FFT size N).
65         //
66         // FFT of the first part must then necessarily be split into xxxx00 and
67         // xxxx10, and similarly xxxx01 and xxxx11 for the other part. Since
68         // these two FFTs are handled identically, it means we split into xxxx0x
69         // and xxxx1x, so that the second-outer pass (done second-to-last)
70         // looks only at the second last bit, and so on. We do two such merge
71         // passes of sub-size N/4 (sub-FFT size N/2).
72         //
73         // Thus, the inner, Nth pass (done first) splits at the first bit,
74         // so 0 is paired with 16, 1 with 17 and so on, doing N/2 such merge
75         // passes of sub-size 1 (sub-FFT size 2). We say that the stride is 16.
76         // The second-inner, (N-1)th pass (done second) splits at the second
77         // bit, so the stride is 8, and so on.
78
79         assert((fft_size & (fft_size - 1)) == 0);  // Must be power of two.
80         float *tmp = new float[fft_size * 4];
81         int subfft_size = 1 << pass_number;
82         double mulfac;
83         if (inverse) {
84                 mulfac = 2.0 * M_PI;
85         } else {
86                 mulfac = -2.0 * M_PI;
87         }
88
89         assert((fft_size & (fft_size - 1)) == 0);  // Must be power of two.
90         assert(fft_size % subfft_size == 0);
91         int stride = fft_size / subfft_size;
92         for (int i = 0; i < fft_size; ++i) {
93                 int k = i / stride;         // Element number within this sub-FFT.
94                 int offset = i % stride;    // Sub-FFT number.
95                 double twiddle_real, twiddle_imag;
96
97                 if (k < subfft_size / 2) {
98                         twiddle_real = cos(mulfac * (k / double(subfft_size)));
99                         twiddle_imag = sin(mulfac * (k / double(subfft_size)));
100                 } else {
101                         // This is mathematically equivalent to the twiddle factor calculations
102                         // in the other branch of the if, but not numerically; the range
103                         // reductions on x87 are not all that precise, and this keeps us within
104                         // [0,pi>.
105                         k -= subfft_size / 2;
106                         twiddle_real = -cos(mulfac * (k / double(subfft_size)));
107                         twiddle_imag = -sin(mulfac * (k / double(subfft_size)));
108                 }
109
110                 // The support texture contains everything we need for the FFT:
111                 // Obviously, the twiddle factor (in the Z and W components), but also
112                 // which two samples to fetch. These are stored as normalized
113                 // X coordinate offsets (Y coordinate for a vertical FFT); the reason
114                 // for using offsets and not direct coordinates as in GPUwave
115                 // is that we can have multiple FFTs along the same line,
116                 // and want to reuse the support texture by repeating it.
117                 int base = k * stride * 2 + offset;
118                 int support_texture_index;
119                 if (direction == FFTPassEffect::VERTICAL) {
120                         // Compensate for OpenGL's bottom-left convention.
121                         support_texture_index = fft_size - i - 1;
122                 } else {
123                         support_texture_index = i;
124                 }
125                 tmp[support_texture_index * 4 + 0] = (base - support_texture_index) / double(input_size);
126                 tmp[support_texture_index * 4 + 1] = (base + stride - support_texture_index) / double(input_size);
127                 tmp[support_texture_index * 4 + 2] = twiddle_real;
128                 tmp[support_texture_index * 4 + 3] = twiddle_imag;
129         }
130
131         glActiveTexture(GL_TEXTURE0 + *sampler_num);
132         check_error();
133         glBindTexture(GL_TEXTURE_1D, tex);
134         check_error();
135         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
136         check_error();
137         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
138         check_error();
139         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
140         check_error();
141
142         // Supposedly FFTs are very sensitive to inaccuracies in the twiddle factors,
143         // at least according to a paper by Schatzman (see gpuwave.pdf reference [30]
144         // for the full reference), so we keep them at 32-bit. However, for
145         // small sizes, all components are exact anyway, so we can cheat there
146         // (although noting that the source coordinates become somewhat less
147         // accurate then, too).
148         glTexImage1D(GL_TEXTURE_1D, 0, (subfft_size <= 4) ? GL_RGBA16F : GL_RGBA32F, fft_size, 0, GL_RGBA, GL_FLOAT, tmp);
149         check_error();
150
151         delete[] tmp;
152
153         set_uniform_int(glsl_program_num, prefix, "support_tex", *sampler_num);
154         ++*sampler_num;
155
156         assert(input_size % fft_size == 0);
157         set_uniform_float(glsl_program_num, prefix, "num_repeats", input_size / fft_size);
158 }