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(EffectChain *chain, const std::vector<Effect *> &inputs) {
22 assert(inputs.size() == 1);
23 hpass->add_self_to_effect_chain(chain, inputs);
25 std::vector<Effect *> vpass_inputs;
26 vpass_inputs.push_back(hpass);
27 vpass->add_self_to_effect_chain(chain, vpass_inputs);
30 bool BlurEffect::set_float(const std::string &key, float value) {
31 if (!hpass->set_float(key, value)) {
34 return vpass->set_float(key, value);
37 SingleBlurPassEffect::SingleBlurPassEffect()
41 register_float("radius", (float *)&radius);
42 register_int("direction", (int *)&direction);
45 std::string SingleBlurPassEffect::output_fragment_shader()
47 return read_file("blur_effect.frag");
50 void SingleBlurPassEffect::set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
52 Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
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
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.
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) {
72 texture_size /= 2; // Rounding down.
73 adjusted_radius = radius * float(texture_size) / float(base_texture_size);
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.
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;
86 glActiveTexture(GL_TEXTURE0);
88 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, base_mipmap_level);
90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, base_mipmap_level);
93 // Compute the weights; they will be symmetrical, so we only compute
95 float weight[NUM_TAPS + 1];
98 for (unsigned i = 1; i < NUM_TAPS + 1; ++i) {
103 for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
104 float z = i / adjusted_radius;
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));
115 sum += 2.0f * weight[i];
118 for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
124 // NOTE: This is currently broken.
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
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)];
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;
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];
150 float offset, total_weight;
151 if (w1 + w2 < 1e-6) {
155 offset = w2 / (w1 + w2);
156 total_weight = w1 + w2;
159 // hack for easier visualization
163 float x = 0.0f, y = 0.0f;
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;
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;
179 set_uniform_vec4_array(glsl_program_num, prefix, "samples", samples, NUM_TAPS / 2 + 1);
181 // Boring, at-whole-pixels sampling.
182 float samples[4 * NUM_TAPS];
184 // All other samples.
185 for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
186 float x = 0.0f, y = 0.0f;
188 if (direction == HORIZONTAL) {
189 x = i / (float)texture_size;
190 } else if (direction == VERTICAL) {
191 y = i / (float)texture_size;
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;
202 set_uniform_vec4_array(glsl_program_num, prefix, "samples", samples, NUM_TAPS + 1);
206 void SingleBlurPassEffect::clear_gl_state()
208 glActiveTexture(GL_TEXTURE0);
210 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
212 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1000);