]> git.sesse.net Git - movit/blob - blur_effect.cpp
Reuse the VAO across all phases.
[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 using namespace std;
12
13 namespace movit {
14         
15 BlurEffect::BlurEffect()
16         : num_taps(16),
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         ok |= hpass->set_int("num_taps", num_taps);
74
75         ok |= vpass->set_float("radius", adjusted_radius);
76         ok |= vpass->set_int("width", mipmap_width);
77         ok |= vpass->set_int("height", mipmap_height);
78         ok |= vpass->set_int("virtual_width", input_width);
79         ok |= vpass->set_int("virtual_height", input_height);
80         ok |= vpass->set_int("num_taps", num_taps);
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 bool BlurEffect::set_int(const string &key, int value) {
95         if (key == "num_taps") {
96                 if (value < 2 || value % 2 != 0) {
97                         return false;
98                 }
99                 num_taps = value;
100                 update_radius();
101                 return true;
102         }
103         return false;
104 }
105
106 SingleBlurPassEffect::SingleBlurPassEffect(BlurEffect *parent)
107         : parent(parent),
108           num_taps(16),
109           radius(3.0f),
110           direction(HORIZONTAL),
111           width(1280),
112           height(720),
113           uniform_samples(NULL)
114 {
115         register_float("radius", &radius);
116         register_int("direction", (int *)&direction);
117         register_int("width", &width);
118         register_int("height", &height);
119         register_int("virtual_width", &virtual_width);
120         register_int("virtual_height", &virtual_height);
121         register_int("num_taps", &num_taps);
122 }
123
124 SingleBlurPassEffect::~SingleBlurPassEffect()
125 {
126         delete[] uniform_samples;
127 }
128
129 string SingleBlurPassEffect::output_fragment_shader()
130 {
131         char buf[256];
132         sprintf(buf, "#define DIRECTION_VERTICAL %d\n#define NUM_TAPS %d\n",
133                 (direction == VERTICAL), num_taps);
134         uniform_samples = new float[2 * (num_taps / 2 + 1)];
135         register_uniform_vec2_array("samples", uniform_samples, num_taps / 2 + 1);
136         return buf + read_file("blur_effect.frag");
137 }
138
139 void SingleBlurPassEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
140 {
141         Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
142
143         // Compute the weights; they will be symmetrical, so we only compute
144         // the right side.
145         float* weight = new float[num_taps + 1];
146         if (radius < 1e-3) {
147                 weight[0] = 1.0f;
148                 for (int i = 1; i < num_taps + 1; ++i) {
149                         weight[i] = 0.0f;
150                 }
151         } else {
152                 float sum = 0.0f;
153                 for (int i = 0; i < num_taps + 1; ++i) {
154                         // Gaussian blur is a common, but maybe not the prettiest choice;
155                         // it can feel a bit too blurry in the fine detail and too little
156                         // long-tail. This is a simple logistic distribution, which has
157                         // a narrower peak but longer tails.
158                         //
159                         // We interpret the radius as sigma, similar to Gaussian blur.
160                         // Wikipedia says that sigma² = pi² s² / 3, which yields:
161                         const float s = (sqrt(3.0) / M_PI) * radius;
162                         float z = i / (2.0 * s);
163
164                         weight[i] = 1.0f / (cosh(z) * cosh(z));
165
166                         if (i == 0) {
167                                 sum += weight[i];
168                         } else {
169                                 sum += 2.0f * weight[i];
170                         }
171                 }
172                 for (int i = 0; i < num_taps + 1; ++i) {
173                         weight[i] /= sum;
174                 }
175         }
176
177         // Since the GPU gives us bilinear sampling for free, we can get two
178         // samples for the price of one (for every but the center sample,
179         // in which case this trick doesn't buy us anything). Simply sample
180         // between the two pixel centers, and we can do with fewer weights.
181         // (This is right even in the vertical pass where we don't actually
182         // sample between the pixels, because we have linear interpolation
183         // there too.)
184         //
185         // We pack the parameters into a float4: The relative sample coordinates
186         // in (x,y), and the weight in z. w is unused.
187
188         // Center sample.
189         uniform_samples[2 * 0 + 0] = 0.0f;
190         uniform_samples[2 * 0 + 1] = weight[0];
191
192         // All other samples.
193         for (int i = 1; i < num_taps / 2 + 1; ++i) {
194                 unsigned base_pos = i * 2 - 1;
195                 float w1 = weight[base_pos];
196                 float w2 = weight[base_pos + 1];
197                 int size;
198                 if (direction == HORIZONTAL) {
199                         size = width;
200                 } else if (direction == VERTICAL) {
201                         size = height;
202                 } else {
203                         assert(false);
204                 }
205
206                 float pos1 = base_pos / (float)size;
207                 float pos2 = (base_pos + 1) / (float)size;
208                 float pos, total_weight;
209                 combine_two_samples(w1, w2, pos1, pos2, size, &pos, &total_weight, NULL);
210
211                 uniform_samples[2 * i + 0] = pos;
212                 uniform_samples[2 * i + 1] = total_weight;
213         }
214
215         delete[] weight;
216 }
217
218 void SingleBlurPassEffect::clear_gl_state()
219 {
220 }
221
222 }  // namespace movit