]> git.sesse.net Git - movit/blob - blur_effect.cpp
Comment all of *_effect.h.
[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(std::vector<Effect *> *chain) {
22         hpass->add_self_to_effect_chain(chain);
23         vpass->add_self_to_effect_chain(chain);
24 }
25
26 bool BlurEffect::set_float(const std::string &key, float value) {
27         if (!hpass->set_float(key, value)) {
28                 return false;
29         }
30         return vpass->set_float(key, value);
31 }
32
33 SingleBlurPassEffect::SingleBlurPassEffect()
34         : radius(3.0f),
35           direction(HORIZONTAL)
36 {
37         register_float("radius", (float *)&radius);
38         register_int("direction", (int *)&direction);
39 }
40
41 std::string SingleBlurPassEffect::output_fragment_shader()
42 {
43         return read_file("blur_effect.frag");
44 }
45
46 void SingleBlurPassEffect::set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
47 {
48         Effect::set_uniforms(glsl_program_num, prefix, sampler_num);
49
50         int base_texture_size, texture_size;
51         if (direction == HORIZONTAL) {
52                 base_texture_size = texture_size = 1280;  // FIXME
53         } else if (direction == VERTICAL) {
54                 base_texture_size = texture_size = 720;  // FIXME
55         } else {
56                 assert(false);
57         }
58
59         // We only have 16 taps to work with on each side, and we want that to
60         // reach out to about 2.5*sigma.  Bump up the mipmap levels (giving us
61         // box blurs) until we have what we need.
62         //
63         // FIXME: we really need to pick the same mipmap level for both horizontal and vertical!
64         unsigned base_mipmap_level = 0;
65         float adjusted_radius = radius;
66         while (texture_size > 1 && adjusted_radius * 2.5f > NUM_TAPS / 2) {
67                 ++base_mipmap_level;
68                 texture_size /= 2;  // Rounding down.
69                 adjusted_radius = radius * float(texture_size) / float(base_texture_size);
70         }
71
72         // In the second pass, we do the same, but don't sample from a mipmap;
73         // that would re-blur the other direction in an ugly fashion, and we already
74         // have the vertical box blur we need from that pass.
75         //
76         // TODO: We really need to present horizontal+vertical as a unit;
77         // currently, there's really no guarantee vertical blur is the second pass.
78         if (direction == VERTICAL) {
79                 base_mipmap_level = 0;
80         }
81
82         glActiveTexture(GL_TEXTURE0);
83         check_error();
84         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, base_mipmap_level);
85         check_error();
86         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, base_mipmap_level);
87         check_error();
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 / adjusted_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 #if 0
120         // NOTE: This is currently broken.
121
122         // Since the GPU gives us bilinear sampling for free, we can get two
123         // samples for the price of one (for every but the center sample,
124         // in which case this trick doesn't buy us anything). Simply sample
125         // between the two pixel centers, and we can do with fewer weights.
126         // (This is right even in the vertical pass where we don't actually
127         // sample between the pixels, because we have linear interpolation
128         // there too.)
129         //
130         // We pack the parameters into a float4: The relative sample coordinates
131         // in (x,y), and the weight in z. w is unused.
132         float samples[4 * (NUM_TAPS / 2 + 1)];
133
134         // Center sample.
135         samples[4 * 0 + 0] = 0.0f;
136         samples[4 * 0 + 1] = 0.0f;
137         samples[4 * 0 + 2] = weight[0];
138         samples[4 * 0 + 3] = 0.0f;
139
140         // All other samples.
141         for (unsigned i = 1; i < NUM_TAPS / 2 + 1; ++i) {
142                 unsigned base_pos = i * 2 - 1;
143                 float w1 = weight[base_pos];
144                 float w2 = weight[base_pos + 1];
145
146                 float offset, total_weight;
147                 if (w1 + w2 < 1e-6) {
148                         offset = 0.5f;
149                         total_weight = 0.0f;
150                 } else {
151                         offset = w2 / (w1 + w2);
152                         total_weight = w1 + w2;
153                 }
154 #if 0
155                 // hack for easier visualization
156                 offset = 0.5f;
157                 total_weight = 8.0f;
158 #endif
159                 float x = 0.0f, y = 0.0f;
160
161                 if (direction == HORIZONTAL) {
162                         x = (base_pos + offset) / (float)texture_size;
163                 } else if (direction == VERTICAL) {
164                         y = (base_pos + offset) / (float)texture_size;
165                 } else {
166                         assert(false);
167                 }
168
169                 samples[4 * i + 0] = x;
170                 samples[4 * i + 1] = y;
171                 samples[4 * i + 2] = total_weight;
172                 samples[4 * i + 3] = 0.0f;
173         }
174
175         set_uniform_vec4_array(glsl_program_num, prefix, "samples", samples, NUM_TAPS / 2 + 1);
176 #else
177         // Boring, at-whole-pixels sampling.
178         float samples[4 * NUM_TAPS];
179
180         // All other samples.
181         for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
182                 float x = 0.0f, y = 0.0f;
183
184                 if (direction == HORIZONTAL) {
185                         x = i / (float)texture_size;
186                 } else if (direction == VERTICAL) {
187                         y = i / (float)texture_size;
188                 } else {
189                         assert(false);
190                 }
191
192                 samples[4 * i + 0] = x;
193                 samples[4 * i + 1] = y;
194                 samples[4 * i + 2] = weight[i];
195                 samples[4 * i + 3] = 0.0f;
196         }
197
198         set_uniform_vec4_array(glsl_program_num, prefix, "samples", samples, NUM_TAPS + 1);
199 #endif
200 }