]> git.sesse.net Git - movit/blob - blur_effect.cpp
Move Node and Phase out from being inner classes, given that we soon want to expose...
[movit] / blur_effect.cpp
1 #include <math.h>
2 #include <assert.h>
3
4 #include "blur_effect.h"
5 #include "util.h"
6 #include "opengl.h"
7
8 // Must match blur_effect.frag.
9 #define NUM_TAPS 16
10         
11 BlurEffect::BlurEffect()
12         : radius(3.0f)
13 {
14         hpass = new SingleBlurPassEffect();
15         hpass->set_int("direction", SingleBlurPassEffect::HORIZONTAL);
16         vpass = new SingleBlurPassEffect();
17         vpass->set_int("direction", SingleBlurPassEffect::VERTICAL);
18
19         update_radius();
20 }
21
22 void BlurEffect::add_self_to_effect_chain(EffectChain *chain, const std::vector<Effect *> &inputs)
23 {
24         assert(inputs.size() == 1);
25         hpass->add_self_to_effect_chain(chain, inputs);
26
27         std::vector<Effect *> vpass_inputs;
28         vpass_inputs.push_back(hpass);
29         vpass->add_self_to_effect_chain(chain, vpass_inputs); 
30 }
31                 
32 void BlurEffect::update_radius()
33 {
34         // We only have 16 taps to work with on each side, and we want that to
35         // reach out to about 2.5*sigma. Bump up the mipmap levels (giving us
36         // box blurs) until we have what we need.
37         //
38         // TODO: Consider the actual width and height (they influence mipmap
39         // sizes subtly).
40         unsigned base_mipmap_level = 0;
41         float adjusted_radius = radius;
42         while (adjusted_radius * 1.5f > NUM_TAPS / 2) {
43                 ++base_mipmap_level;
44                 adjusted_radius /= 2.0f;
45         }
46         
47         bool ok = hpass->set_float("radius", adjusted_radius);
48         ok |= hpass->set_int("width", 1280 / (1 << base_mipmap_level));  // FIXME
49         ok |= hpass->set_int("height", 720 / (1 << base_mipmap_level));  // FIXME
50
51         ok |= vpass->set_float("radius", adjusted_radius);
52         ok |= vpass->set_int("width", 1280 / (1 << base_mipmap_level));  // FIXME
53         ok |= vpass->set_int("height", 720 / (1 << base_mipmap_level));  // FIXME
54
55         assert(ok);
56 }
57
58 bool BlurEffect::set_float(const std::string &key, float value) {
59         if (key == "radius") {
60                 radius = value;
61                 update_radius();
62                 return true;
63         }
64         return false;
65 }
66
67 SingleBlurPassEffect::SingleBlurPassEffect()
68         : radius(3.0f),
69           direction(HORIZONTAL),
70           width(1280),
71           height(720)
72 {
73         register_float("radius", &radius);
74         register_int("direction", (int *)&direction);
75         register_int("width", &width);
76         register_int("height", &height);
77 }
78
79 std::string SingleBlurPassEffect::output_fragment_shader()
80 {
81         return read_file("blur_effect.frag");
82 }
83
84 void SingleBlurPassEffect::set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
85 {
86         Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
87
88         // Compute the weights; they will be symmetrical, so we only compute
89         // the right side.
90         float weight[NUM_TAPS + 1];
91         if (radius < 1e-3) {
92                 weight[0] = 1.0f;
93                 for (unsigned i = 1; i < NUM_TAPS + 1; ++i) {
94                         weight[i] = 0.0f;
95                 }
96         } else {
97                 float sum = 0.0f;
98                 for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
99                         float z = i / radius;
100
101                         // Gaussian blur is a common, but maybe not the prettiest choice;
102                         // it can feel a bit too blurry in the fine detail and too little
103                         // long-tail. This is a simple logistic distribution, which has
104                         // a narrower peak but longer tails.
105                         weight[i] = 1.0f / (cosh(z) * cosh(z));
106
107                         if (i == 0) {
108                                 sum += weight[i];
109                         } else {
110                                 sum += 2.0f * weight[i];
111                         }
112                 }
113                 for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
114                         weight[i] /= sum;
115                 }
116         }
117
118         // Since the GPU gives us bilinear sampling for free, we can get two
119         // samples for the price of one (for every but the center sample,
120         // in which case this trick doesn't buy us anything). Simply sample
121         // between the two pixel centers, and we can do with fewer weights.
122         // (This is right even in the vertical pass where we don't actually
123         // sample between the pixels, because we have linear interpolation
124         // there too.)
125         //
126         // We pack the parameters into a float4: The relative sample coordinates
127         // in (x,y), and the weight in z. w is unused.
128         float samples[4 * (NUM_TAPS / 2 + 1)];
129
130         // Center sample.
131         samples[4 * 0 + 0] = 0.0f;
132         samples[4 * 0 + 1] = 0.0f;
133         samples[4 * 0 + 2] = weight[0];
134         samples[4 * 0 + 3] = 0.0f;
135
136         // All other samples.
137         for (unsigned i = 1; i < NUM_TAPS / 2 + 1; ++i) {
138                 unsigned base_pos = i * 2 - 1;
139                 float w1 = weight[base_pos];
140                 float w2 = weight[base_pos + 1];
141
142                 float offset, total_weight;
143                 if (w1 + w2 < 1e-6) {
144                         offset = 0.5f;
145                         total_weight = 0.0f;
146                 } else {
147                         offset = w2 / (w1 + w2);
148                         total_weight = w1 + w2;
149                 }
150                 float x = 0.0f, y = 0.0f;
151
152                 if (direction == HORIZONTAL) {
153                         x = (base_pos + offset) / (float)width;
154                 } else if (direction == VERTICAL) {
155                         y = (base_pos + offset) / (float)height;
156                 } else {
157                         assert(false);
158                 }
159
160                 samples[4 * i + 0] = x;
161                 samples[4 * i + 1] = y;
162                 samples[4 * i + 2] = total_weight;
163                 samples[4 * i + 3] = 0.0f;
164         }
165
166         set_uniform_vec4_array(glsl_program_num, prefix, "samples", samples, NUM_TAPS / 2 + 1);
167 }
168
169 void SingleBlurPassEffect::clear_gl_state()
170 {
171 }