Move to 'using namespace std;' in all .cpp files.
[movit] / blur_effect.cpp
1 #include <GL/glew.h>
2 #include <assert.h>
3 #include <math.h>
4 #include <algorithm>
5
6 #include "blur_effect.h"
7 #include "effect_chain.h"
8 #include "effect_util.h"
9 #include "util.h"
10
11 // Must match blur_effect.frag.
12 #define NUM_TAPS 16
13
14 using namespace std;
15         
16 BlurEffect::BlurEffect()
17         : radius(3.0f),
18           input_width(1280),
19           input_height(720)
20 {
21         // The first blur pass will forward resolution information to us.
22         hpass = new SingleBlurPassEffect(this);
23         CHECK(hpass->set_int("direction", SingleBlurPassEffect::HORIZONTAL));
24         vpass = new SingleBlurPassEffect(NULL);
25         CHECK(vpass->set_int("direction", SingleBlurPassEffect::VERTICAL));
26
27         update_radius();
28 }
29
30 void BlurEffect::rewrite_graph(EffectChain *graph, Node *self)
31 {
32         Node *hpass_node = graph->add_node(hpass);
33         Node *vpass_node = graph->add_node(vpass);
34         graph->connect_nodes(hpass_node, vpass_node);
35         graph->replace_receiver(self, hpass_node);
36         graph->replace_sender(self, vpass_node);
37         self->disabled = true;
38
39
40 // We get this information forwarded from the first blur pass,
41 // since we are not part of the chain ourselves.
42 void BlurEffect::inform_input_size(unsigned input_num, unsigned width, unsigned height)
43 {
44         assert(input_num == 0);
45         assert(width != 0);
46         assert(height != 0);
47         input_width = width;
48         input_height = height;
49         update_radius();
50 }
51                 
52 void BlurEffect::update_radius()
53 {
54         // We only have 16 taps to work with on each side, and we want that to
55         // reach out to about 2.5*sigma. Bump up the mipmap levels (giving us
56         // box blurs) until we have what we need.
57         unsigned mipmap_width = input_width, mipmap_height = input_height;
58         float adjusted_radius = radius;
59         while ((mipmap_width > 1 || mipmap_height > 1) && adjusted_radius * 1.5f > NUM_TAPS / 2) {
60                 // Find the next mipmap size (round down, minimum 1 pixel).
61                 mipmap_width = max(mipmap_width / 2, 1u);
62                 mipmap_height = max(mipmap_height / 2, 1u);
63
64                 // Approximate when mipmap sizes are odd, but good enough.
65                 adjusted_radius = radius * float(mipmap_width) / float(input_width);
66         }
67         
68         bool ok = hpass->set_float("radius", adjusted_radius);
69         ok |= hpass->set_int("width", mipmap_width);
70         ok |= hpass->set_int("height", mipmap_height);
71         ok |= hpass->set_int("virtual_width", mipmap_width);
72         ok |= hpass->set_int("virtual_height", mipmap_height);
73
74         ok |= vpass->set_float("radius", adjusted_radius);
75         ok |= vpass->set_int("width", mipmap_width);
76         ok |= vpass->set_int("height", mipmap_height);
77         ok |= vpass->set_int("virtual_width", input_width);
78         ok |= vpass->set_int("virtual_height", input_height);
79
80         assert(ok);
81 }
82
83 bool BlurEffect::set_float(const string &key, float value) {
84         if (key == "radius") {
85                 radius = value;
86                 update_radius();
87                 return true;
88         }
89         return false;
90 }
91
92 SingleBlurPassEffect::SingleBlurPassEffect(BlurEffect *parent)
93         : parent(parent),
94           radius(3.0f),
95           direction(HORIZONTAL),
96           width(1280),
97           height(720)
98 {
99         register_float("radius", &radius);
100         register_int("direction", (int *)&direction);
101         register_int("width", &width);
102         register_int("height", &height);
103         register_int("virtual_width", &virtual_width);
104         register_int("virtual_height", &virtual_height);
105 }
106
107 string SingleBlurPassEffect::output_fragment_shader()
108 {
109         return read_file("blur_effect.frag");
110 }
111
112 void SingleBlurPassEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
113 {
114         Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
115
116         // Compute the weights; they will be symmetrical, so we only compute
117         // the right side.
118         float weight[NUM_TAPS + 1];
119         if (radius < 1e-3) {
120                 weight[0] = 1.0f;
121                 for (unsigned i = 1; i < NUM_TAPS + 1; ++i) {
122                         weight[i] = 0.0f;
123                 }
124         } else {
125                 float sum = 0.0f;
126                 for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
127                         // Gaussian blur is a common, but maybe not the prettiest choice;
128                         // it can feel a bit too blurry in the fine detail and too little
129                         // long-tail. This is a simple logistic distribution, which has
130                         // a narrower peak but longer tails.
131                         //
132                         // We interpret the radius as sigma, similar to Gaussian blur.
133                         // Wikipedia says that sigma² = pi² s² / 3, which yields:
134                         const float s = (sqrt(3.0) / M_PI) * radius;
135                         float z = i / (2.0 * s);
136
137                         weight[i] = 1.0f / (cosh(z) * cosh(z));
138
139                         if (i == 0) {
140                                 sum += weight[i];
141                         } else {
142                                 sum += 2.0f * weight[i];
143                         }
144                 }
145                 for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
146                         weight[i] /= sum;
147                 }
148         }
149
150         // Since the GPU gives us bilinear sampling for free, we can get two
151         // samples for the price of one (for every but the center sample,
152         // in which case this trick doesn't buy us anything). Simply sample
153         // between the two pixel centers, and we can do with fewer weights.
154         // (This is right even in the vertical pass where we don't actually
155         // sample between the pixels, because we have linear interpolation
156         // there too.)
157         //
158         // We pack the parameters into a float4: The relative sample coordinates
159         // in (x,y), and the weight in z. w is unused.
160         float samples[4 * (NUM_TAPS / 2 + 1)];
161
162         // Center sample.
163         samples[4 * 0 + 0] = 0.0f;
164         samples[4 * 0 + 1] = 0.0f;
165         samples[4 * 0 + 2] = weight[0];
166         samples[4 * 0 + 3] = 0.0f;
167
168         // All other samples.
169         for (unsigned i = 1; i < NUM_TAPS / 2 + 1; ++i) {
170                 unsigned base_pos = i * 2 - 1;
171                 float w1 = weight[base_pos];
172                 float w2 = weight[base_pos + 1];
173
174                 float offset, total_weight;
175                 combine_two_samples(w1, w2, &offset, &total_weight, NULL);
176
177                 float x = 0.0f, y = 0.0f;
178
179                 if (direction == HORIZONTAL) {
180                         x = (base_pos + offset) / (float)width;
181                 } else if (direction == VERTICAL) {
182                         y = (base_pos + offset) / (float)height;
183                 } else {
184                         assert(false);
185                 }
186
187                 samples[4 * i + 0] = x;
188                 samples[4 * i + 1] = y;
189                 samples[4 * i + 2] = total_weight;
190                 samples[4 * i + 3] = 0.0f;
191         }
192
193         set_uniform_vec4_array(glsl_program_num, prefix, "samples", samples, NUM_TAPS / 2 + 1);
194 }
195
196 void SingleBlurPassEffect::clear_gl_state()
197 {
198 }