]> git.sesse.net Git - movit/blob - fft_pass_effect.h
Retire the GL_GENERATE_MIPMAP hack for FlatInput.
[movit] / fft_pass_effect.h
1 #ifndef _MOVIT_FFT_PASS_EFFECT_H
2 #define _MOVIT_FFT_PASS_EFFECT_H 1
3
4 // One pass of a radix-2, in-order, decimation-in-time 1D FFT/IFFT. If you
5 // connect multiple ones of these together, you will eventually have a complete
6 // FFT or IFFT. The FFTed data is not so useful for video effects in itself,
7 // but enables faster convolutions (especially non-separable 2D convolutions)
8 // than can be done directly, by doing FFT -> multiply -> IFFT. The utilities
9 // for doing this efficiently will probably be added to Movit at a later date;
10 // for now, this effect isn't the most useful.
11 //
12 // An introduction to FFTs is outside the scope of a file-level comment; see
13 // http://en.wikipedia.org/wiki/Cooley%E2%80%93Tukey_FFT_algorithm#The_radix-2_DIT_case .
14 //
15 // The pixels are not really interpreted as pixels, but are interpreted as two
16 // complex numbers with (real,imaginary) parts stored in (R,G) and (B,A).
17 // On top of this two-way parallelism, many FFTs are done in parallel (see below).
18 //
19 // Implementing a high-performance FFT on the GPU is not easy, especially
20 // within the demands of Movit filters. (This is one of the places where
21 // using CUDA or D3D would be easier, as both ship with pre-made and highly
22 // tuned FFTs.) We don't go to great lengths to get an optimal implementation,
23 // but rather stay with someting simple. I'll conveniently enough refer to
24 // my own report on this topic from 2007, namely
25 //
26 //    Steinar H. Gunderson: “GPUwave: An implementation of the split-step
27 //    Fourier method for the GPU”, http://gpuwave.sesse.net/gpuwave.pdf
28 //
29 // Chapter 5 contains the details of the FFT. We follow this rather closely,
30 // with the exception that in Movit, we only ever draw a single quad,
31 // so the strategy used in GPUwave with drawing multiple quads with constant
32 // twiddle factors on them will not be in use here. (It requires some
33 // benchmarking to find the optimal crossover point anyway.)
34 //
35 // Also, we support doing many FFTs along the same axis, so e.g. if you
36 // have a 128x128 image and ask for a horizontal FFT of size 64, you will
37 // actually get 256 of them (two wide, 128 high). This is in contrast with
38 // GPUwave, which only supports them one wide; in a picture setting,
39 // moving blocks around to create only one block wide FFTs would rapidly
40 // lead to way too slender textures to be practical (e.g., 1280x720
41 // with an FFT of size 64 would be 64x14400 rearranged, and many GPUs
42 // have limits of 8192 pixels or even 2048 along one dimension).
43 //
44 // Note that this effect produces an _unnormalized_ FFT, which means that a
45 // FFT -> IFFT chain will end up not returning the original data (even modulo
46 // precision errors) but rather the original data with each element multiplied
47 // by N, the FFT size. As the FFT and IFFT contribute equally to this energy
48 // gain, it is recommended that you do the division by N after the FFT but
49 // before the IFFT. This way, you use the least range possible (for one
50 // scaling), and as fp16 has quite limited range at times, this can be relevant
51 // on some GPUs for larger sizes.
52
53 #include <stdio.h>
54 #include <GL/glew.h>
55 #include <string>
56
57 #include "effect.h"
58
59 class FFTPassEffect : public Effect {
60 public:
61         FFTPassEffect();
62         ~FFTPassEffect();
63         virtual std::string effect_type_id() const {
64                 char buf[256];
65                 if (inverse) {
66                         snprintf(buf, sizeof(buf), "IFFTPassEffect[%d]", (1 << pass_number));
67                 } else {
68                         snprintf(buf, sizeof(buf), "FFTPassEffect[%d]", (1 << pass_number));
69                 }
70                 return buf;
71         }
72         std::string output_fragment_shader();
73
74         void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
75
76         // We don't actually change the output size, but this flag makes sure
77         // that no other effect is chained after us. This is important since
78         // we cannot deliver filtered results; any attempt at sampling in-between
79         // pixels would necessarily give garbage. In addition, we set our sampling
80         // mode to GL_NEAREST, which other effects are not ready for; so, the
81         // combination of these two flags guarantee that we're run entirely alone
82         // in our own phase, which is exactly what we want.
83         virtual bool needs_texture_bounce() const { return true; }
84         virtual bool changes_output_size() const { return true; }
85
86         virtual void inform_input_size(unsigned input_num, unsigned width, unsigned height)
87         {
88                 assert(input_num == 0);
89                 input_width = width;
90                 input_height = height;
91         }
92         
93         virtual void get_output_size(unsigned *width, unsigned *height,
94                                      unsigned *virtual_width, unsigned *virtual_height) const {
95                 *width = *virtual_width = input_width;
96                 *height = *virtual_height = input_height;
97         }
98         
99         enum Direction { HORIZONTAL = 0, VERTICAL = 1 };
100
101 private:
102         int input_width, input_height;
103         GLuint tex;
104         int fft_size;
105         Direction direction;
106         int pass_number;  // From 1..n.
107         int inverse;  // 0 = forward (FFT), 1 = reverse (IFFT).
108 };
109
110 #endif // !defined(_MOVIT_FFT_PASS_EFFECT_H)