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