]> git.sesse.net Git - movit/blob - blur_effect.cpp
Typo fix.
[movit] / blur_effect.cpp
1 #define GL_GLEXT_PROTOTYPES 1
2
3 #include <math.h>
4 #include <GL/gl.h>
5 #include <GL/glext.h>
6 #include <assert.h>
7
8 #include "blur_effect.h"
9 #include "util.h"
10
11 // Must match blur_effect.frag.
12 #define NUM_TAPS 16
13         
14 BlurEffect::BlurEffect() {
15         hpass = new SingleBlurPassEffect();
16         hpass->set_int("direction", SingleBlurPassEffect::HORIZONTAL);
17         vpass = new SingleBlurPassEffect();
18         vpass->set_int("direction", SingleBlurPassEffect::VERTICAL);
19 }
20
21 void BlurEffect::add_self_to_effect_chain(EffectChain *chain, const std::vector<Effect *> &inputs) {
22         assert(inputs.size() == 1);
23         hpass->add_self_to_effect_chain(chain, inputs);
24
25         std::vector<Effect *> vpass_inputs;
26         vpass_inputs.push_back(hpass);
27         vpass->add_self_to_effect_chain(chain, vpass_inputs);
28 }
29
30 bool BlurEffect::set_float(const std::string &key, float value) {
31         if (!hpass->set_float(key, value)) {
32                 return false;
33         }
34         return vpass->set_float(key, value);
35 }
36
37 SingleBlurPassEffect::SingleBlurPassEffect()
38         : radius(3.0f),
39           direction(HORIZONTAL)
40 {
41         register_float("radius", (float *)&radius);
42         register_int("direction", (int *)&direction);
43 }
44
45 std::string SingleBlurPassEffect::output_fragment_shader()
46 {
47         return read_file("blur_effect.frag");
48 }
49
50 void SingleBlurPassEffect::set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
51 {
52         Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
53
54         int base_texture_size, texture_size;
55         if (direction == HORIZONTAL) {
56                 base_texture_size = texture_size = 1280;  // FIXME
57         } else if (direction == VERTICAL) {
58                 base_texture_size = texture_size = 720;  // FIXME
59         } else {
60                 assert(false);
61         }
62
63         // We only have 16 taps to work with on each side, and we want that to
64         // reach out to about 2.5*sigma.  Bump up the mipmap levels (giving us
65         // box blurs) until we have what we need.
66         //
67         // FIXME: we really need to pick the same mipmap level for both horizontal and vertical!
68         unsigned base_mipmap_level = 0;
69         float adjusted_radius = radius;
70         while (texture_size > 1 && adjusted_radius * 2.5f > NUM_TAPS / 2) {
71                 ++base_mipmap_level;
72                 texture_size /= 2;  // Rounding down.
73                 adjusted_radius = radius * float(texture_size) / float(base_texture_size);
74         }
75
76         // In the second pass, we do the same, but don't sample from a mipmap;
77         // that would re-blur the other direction in an ugly fashion, and we already
78         // have the vertical box blur we need from that pass.
79         //
80         // TODO: We really need to present horizontal+vertical as a unit;
81         // currently, there's really no guarantee vertical blur is the second pass.
82         if (direction == VERTICAL) {
83                 base_mipmap_level = 0;
84         }
85
86         glActiveTexture(GL_TEXTURE0);
87         check_error();
88         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, base_mipmap_level);
89         check_error();
90         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, base_mipmap_level);
91         check_error();
92
93         // Compute the weights; they will be symmetrical, so we only compute
94         // the right side.
95         float weight[NUM_TAPS + 1];
96         if (radius < 1e-3) {
97                 weight[0] = 1.0f;
98                 for (unsigned i = 1; i < NUM_TAPS + 1; ++i) {
99                         weight[i] = 0.0f;
100                 }
101         } else {
102                 float sum = 0.0f;
103                 for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
104                         float z = i / adjusted_radius;
105
106                         // Gaussian blur is a common, but maybe not the prettiest choice;
107                         // it can feel a bit too blurry in the fine detail and too little
108                         // long-tail. This is a simple logistic distribution, which has
109                         // a narrower peak but longer tails.
110                         weight[i] = 1.0f / (cosh(z) * cosh(z));
111
112                         if (i == 0) {
113                                 sum += weight[i];
114                         } else {
115                                 sum += 2.0f * weight[i];
116                         }
117                 }
118                 for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
119                         weight[i] /= sum;
120                 }
121         }
122
123 #if 0
124         // NOTE: This is currently broken.
125
126         // Since the GPU gives us bilinear sampling for free, we can get two
127         // samples for the price of one (for every but the center sample,
128         // in which case this trick doesn't buy us anything). Simply sample
129         // between the two pixel centers, and we can do with fewer weights.
130         // (This is right even in the vertical pass where we don't actually
131         // sample between the pixels, because we have linear interpolation
132         // there too.)
133         //
134         // We pack the parameters into a float4: The relative sample coordinates
135         // in (x,y), and the weight in z. w is unused.
136         float samples[4 * (NUM_TAPS / 2 + 1)];
137
138         // Center sample.
139         samples[4 * 0 + 0] = 0.0f;
140         samples[4 * 0 + 1] = 0.0f;
141         samples[4 * 0 + 2] = weight[0];
142         samples[4 * 0 + 3] = 0.0f;
143
144         // All other samples.
145         for (unsigned i = 1; i < NUM_TAPS / 2 + 1; ++i) {
146                 unsigned base_pos = i * 2 - 1;
147                 float w1 = weight[base_pos];
148                 float w2 = weight[base_pos + 1];
149
150                 float offset, total_weight;
151                 if (w1 + w2 < 1e-6) {
152                         offset = 0.5f;
153                         total_weight = 0.0f;
154                 } else {
155                         offset = w2 / (w1 + w2);
156                         total_weight = w1 + w2;
157                 }
158 #if 0
159                 // hack for easier visualization
160                 offset = 0.5f;
161                 total_weight = 8.0f;
162 #endif
163                 float x = 0.0f, y = 0.0f;
164
165                 if (direction == HORIZONTAL) {
166                         x = (base_pos + offset) / (float)texture_size;
167                 } else if (direction == VERTICAL) {
168                         y = (base_pos + offset) / (float)texture_size;
169                 } else {
170                         assert(false);
171                 }
172
173                 samples[4 * i + 0] = x;
174                 samples[4 * i + 1] = y;
175                 samples[4 * i + 2] = total_weight;
176                 samples[4 * i + 3] = 0.0f;
177         }
178
179         set_uniform_vec4_array(glsl_program_num, prefix, "samples", samples, NUM_TAPS / 2 + 1);
180 #else
181         // Boring, at-whole-pixels sampling.
182         float samples[4 * NUM_TAPS];
183
184         // All other samples.
185         for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
186                 float x = 0.0f, y = 0.0f;
187
188                 if (direction == HORIZONTAL) {
189                         x = i / (float)texture_size;
190                 } else if (direction == VERTICAL) {
191                         y = i / (float)texture_size;
192                 } else {
193                         assert(false);
194                 }
195
196                 samples[4 * i + 0] = x;
197                 samples[4 * i + 1] = y;
198                 samples[4 * i + 2] = weight[i];
199                 samples[4 * i + 3] = 0.0f;
200         }
201
202         set_uniform_vec4_array(glsl_program_num, prefix, "samples", samples, NUM_TAPS + 1);
203 #endif
204 }
205
206 void SingleBlurPassEffect::clear_gl_state()
207 {
208         glActiveTexture(GL_TEXTURE0);
209         check_error();
210         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
211         check_error();
212         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1000);
213         check_error();
214 }