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