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