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