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