1 #define GL_GLEXT_PROTOTYPES 1
8 #include "blur_effect.h"
11 // Must match blur_effect.frag.
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);
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);
26 bool BlurEffect::set_float(const std::string &key, float value) {
27 if (!hpass->set_float(key, value)) {
30 return vpass->set_float(key, value);
33 SingleBlurPassEffect::SingleBlurPassEffect()
37 register_float("radius", (float *)&radius);
38 register_int("direction", (int *)&direction);
41 std::string SingleBlurPassEffect::output_fragment_shader()
43 return read_file("blur_effect.frag");
46 void SingleBlurPassEffect::set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
48 Effect::set_uniforms(glsl_program_num, prefix, sampler_num);
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
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.
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) {
68 texture_size /= 2; // Rounding down.
69 adjusted_radius = radius * float(texture_size) / float(base_texture_size);
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.
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;
82 glActiveTexture(GL_TEXTURE0);
84 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, base_mipmap_level);
86 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, base_mipmap_level);
89 // Compute the weights; they will be symmetrical, so we only compute
91 float weight[NUM_TAPS + 1];
94 for (unsigned i = 1; i < NUM_TAPS + 1; ++i) {
99 for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
100 float z = i / adjusted_radius;
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));
111 sum += 2.0f * weight[i];
114 for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
120 // NOTE: This is currently broken.
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
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)];
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;
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];
146 float offset, total_weight;
147 if (w1 + w2 < 1e-6) {
151 offset = w2 / (w1 + w2);
152 total_weight = w1 + w2;
155 // hack for easier visualization
159 float x = 0.0f, y = 0.0f;
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;
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;
175 set_uniform_vec4_array(glsl_program_num, prefix, "samples", samples, NUM_TAPS / 2 + 1);
177 // Boring, at-whole-pixels sampling.
178 float samples[4 * NUM_TAPS];
180 // All other samples.
181 for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
182 float x = 0.0f, y = 0.0f;
184 if (direction == HORIZONTAL) {
185 x = i / (float)texture_size;
186 } else if (direction == VERTICAL) {
187 y = i / (float)texture_size;
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;
198 set_uniform_vec4_array(glsl_program_num, prefix, "samples", samples, NUM_TAPS + 1);