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