]> git.sesse.net Git - movit/blob - blur_effect.h
Make shader generation more deterministic by removing a sort of pointers.
[movit] / blur_effect.h
1 #ifndef _MOVIT_BLUR_EFFECT_H
2 #define _MOVIT_BLUR_EFFECT_H 1
3
4 // A separable 2D blur implemented by a combination of mipmap filtering
5 // and convolution (essentially giving a convolution with a piecewise linear
6 // approximation to the true impulse response).
7 //
8 // Works in two passes; first horizontal, then vertical (BlurEffect,
9 // which is what the user is intended to use, instantiates two copies of
10 // SingleBlurPassEffect behind the scenes).
11 //
12 // The recommended number of taps is the default (16). Fewer will be faster
13 // but uglier; a tradeoff that might be worth it as part of more complicated
14 // effects. This can be set only before finalization, and must be an
15 // even number.
16
17 #include <epoxy/gl.h>
18 #include <assert.h>
19 #include <stddef.h>
20 #include <string>
21
22 #include "effect.h"
23
24 namespace movit {
25
26 class EffectChain;
27 class Node;
28 class SingleBlurPassEffect;
29
30 class BlurEffect : public Effect {
31 public:
32         BlurEffect();
33
34         virtual std::string effect_type_id() const { return "BlurEffect"; }
35
36         // We want this for the same reason as ResizeEffect; we could end up scaling
37         // down quite a lot.
38         virtual bool needs_texture_bounce() const { return true; }
39         virtual bool needs_mipmaps() const { return true; }
40         virtual bool needs_srgb_primaries() const { return false; }
41
42         virtual void inform_input_size(unsigned input_num, unsigned width, unsigned height);
43
44         virtual std::string output_fragment_shader() {
45                 assert(false);
46         }
47         virtual void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num) {
48                 assert(false);
49         }
50
51         virtual void rewrite_graph(EffectChain *graph, Node *self);
52         virtual bool set_float(const std::string &key, float value);
53         virtual bool set_int(const std::string &key, int value);
54         
55 private:
56         void update_radius();
57
58         int num_taps;
59         float radius;
60         SingleBlurPassEffect *hpass, *vpass;
61         unsigned input_width, input_height;
62 };
63
64 class SingleBlurPassEffect : public Effect {
65 public:
66         // If parent is non-NULL, calls to inform_input_size will be forwarded
67         // so that it can make reasonable decisions for both blur passes.
68         SingleBlurPassEffect(BlurEffect *parent);
69         virtual ~SingleBlurPassEffect();
70         virtual std::string effect_type_id() const { return "SingleBlurPassEffect"; }
71
72         std::string output_fragment_shader();
73
74         virtual bool needs_texture_bounce() const { return true; }
75         virtual bool needs_mipmaps() const { return true; }
76         virtual bool needs_srgb_primaries() const { return false; }
77         virtual AlphaHandling alpha_handling() const { return INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK; }
78
79         virtual void inform_input_size(unsigned input_num, unsigned width, unsigned height) {
80                 if (parent != NULL) {
81                         parent->inform_input_size(input_num, width, height);
82                 }
83         }
84         virtual bool changes_output_size() const { return true; }
85         virtual bool sets_virtual_output_size() const { return true; }
86         virtual bool one_to_one_sampling() const { return false; }  // Can sample outside the border.
87
88         virtual void get_output_size(unsigned *width, unsigned *height, unsigned *virtual_width, unsigned *virtual_height) const {
89                 *width = this->width;
90                 *height = this->height;
91                 *virtual_width = this->virtual_width;
92                 *virtual_height = this->virtual_height;
93         }
94
95         void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
96         void clear_gl_state();
97         
98         enum Direction { HORIZONTAL = 0, VERTICAL = 1 };
99
100 private:
101         BlurEffect *parent;
102         int num_taps;
103         float radius;
104         Direction direction;
105         int width, height, virtual_width, virtual_height;
106         float *uniform_samples;
107 };
108
109 }  // namespace movit
110
111 #endif // !defined(_MOVIT_BLUR_EFFECT_H)