]> git.sesse.net Git - movit/blob - complex_modulate_effect.h
Convert another glReadPixels() to RGBA.
[movit] / complex_modulate_effect.h
1 #ifndef _MOVIT_COMPLEX_MODULATE_EFFECT_H
2 #define _MOVIT_COMPLEX_MODULATE_EFFECT_H 1
3
4 // An effect that treats each pixel as two complex numbers (xy and zw),
5 // and multiplies it with some other complex number (xy and xy, so the
6 // same in both cases). The latter can be repeated both horizontally and
7 // vertically if desired.
8 //
9 // The typical use is to implement convolution by way of FFT; since
10 // FFT(A ⊙ B) = FFT(A) * FFT(B), you can FFT both inputs (where B
11 // would often even be a constant, so you'd only need to do FFT once),
12 // multiply them together and then IFFT the result to get a convolution.
13 //
14 // It is in a sense “wrong” to do this directly on pixels, since the color
15 // channels are independent and real-valued (ie., not complex numbers), but
16 // since convolution is a linear operation, it's unproblematic to treat R + Gi
17 // as a single complex number and B + Ai and another one; barring numerical
18 // errors, there should be no leakage between the channels as long as you're
19 // convolving with a real quantity. (There are more sophisticated ways of doing
20 // two real FFTs with a single complex one, but we won't need them, as we
21 // don't care about the actual FFT result, just that the convolution property
22 // holds.)
23
24 #include <epoxy/gl.h>
25 #include <string>
26
27 #include "effect.h"
28
29 namespace movit {
30
31 class EffectChain;
32
33 class ComplexModulateEffect : public Effect {
34 public:
35         ComplexModulateEffect();
36         virtual std::string effect_type_id() const { return "ComplexModulateEffect"; }
37         std::string output_fragment_shader();
38
39         // Technically we only need texture bounce for the second input
40         // (to be allowed to mess with its sampler state), but there's
41         // no way of expressing that currently.
42         virtual bool needs_texture_bounce() const { return true; }
43         virtual bool changes_output_size() const { return true; }
44
45         virtual void inform_input_size(unsigned input_num, unsigned width, unsigned height);
46         virtual void get_output_size(unsigned *width, unsigned *height,
47                                      unsigned *virtual_width, unsigned *virtual_height) const;
48         virtual unsigned num_inputs() const { return 2; }
49         virtual void inform_added(EffectChain *chain) { this->chain = chain; }
50
51         void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
52
53 private:
54         EffectChain *chain;
55         int primary_input_width, primary_input_height;
56         int num_repeats_x, num_repeats_y;
57 };
58
59 }  // namespace movit
60
61 #endif // !defined(_MOVIT_COMPLEX_MODULATE_EFFECT_H)