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