4 #include "blur_effect.h"
8 // Must match blur_effect.frag.
11 BlurEffect::BlurEffect()
14 hpass = new SingleBlurPassEffect();
15 hpass->set_int("direction", SingleBlurPassEffect::HORIZONTAL);
16 vpass = new SingleBlurPassEffect();
17 vpass->set_int("direction", SingleBlurPassEffect::VERTICAL);
22 void BlurEffect::add_self_to_effect_chain(EffectChain *chain, const std::vector<Effect *> &inputs)
24 assert(inputs.size() == 1);
25 hpass->add_self_to_effect_chain(chain, inputs);
27 std::vector<Effect *> vpass_inputs;
28 vpass_inputs.push_back(hpass);
29 vpass->add_self_to_effect_chain(chain, vpass_inputs);
32 void BlurEffect::update_radius()
34 // We only have 16 taps to work with on each side, and we want that to
35 // reach out to about 2.5*sigma. Bump up the mipmap levels (giving us
36 // box blurs) until we have what we need.
38 // TODO: Consider the actual width and height (they influence mipmap
40 unsigned base_mipmap_level = 0;
41 float adjusted_radius = radius;
42 while (adjusted_radius * 1.5f > NUM_TAPS / 2) {
44 adjusted_radius /= 2.0f;
47 bool ok = hpass->set_float("radius", adjusted_radius);
48 ok |= hpass->set_int("width", 1280 / (1 << base_mipmap_level)); // FIXME
49 ok |= hpass->set_int("height", 720 / (1 << base_mipmap_level)); // FIXME
51 ok |= vpass->set_float("radius", adjusted_radius);
52 ok |= vpass->set_int("width", 1280 / (1 << base_mipmap_level)); // FIXME
53 ok |= vpass->set_int("height", 720 / (1 << base_mipmap_level)); // FIXME
58 bool BlurEffect::set_float(const std::string &key, float value) {
59 if (key == "radius") {
67 SingleBlurPassEffect::SingleBlurPassEffect()
69 direction(HORIZONTAL),
73 register_float("radius", &radius);
74 register_int("direction", (int *)&direction);
75 register_int("width", &width);
76 register_int("height", &height);
79 std::string SingleBlurPassEffect::output_fragment_shader()
81 return read_file("blur_effect.frag");
84 void SingleBlurPassEffect::set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
86 Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
88 // Compute the weights; they will be symmetrical, so we only compute
90 float weight[NUM_TAPS + 1];
93 for (unsigned i = 1; i < NUM_TAPS + 1; ++i) {
98 for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
101 // Gaussian blur is a common, but maybe not the prettiest choice;
102 // it can feel a bit too blurry in the fine detail and too little
103 // long-tail. This is a simple logistic distribution, which has
104 // a narrower peak but longer tails.
105 weight[i] = 1.0f / (cosh(z) * cosh(z));
110 sum += 2.0f * weight[i];
113 for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
118 // Since the GPU gives us bilinear sampling for free, we can get two
119 // samples for the price of one (for every but the center sample,
120 // in which case this trick doesn't buy us anything). Simply sample
121 // between the two pixel centers, and we can do with fewer weights.
122 // (This is right even in the vertical pass where we don't actually
123 // sample between the pixels, because we have linear interpolation
126 // We pack the parameters into a float4: The relative sample coordinates
127 // in (x,y), and the weight in z. w is unused.
128 float samples[4 * (NUM_TAPS / 2 + 1)];
131 samples[4 * 0 + 0] = 0.0f;
132 samples[4 * 0 + 1] = 0.0f;
133 samples[4 * 0 + 2] = weight[0];
134 samples[4 * 0 + 3] = 0.0f;
136 // All other samples.
137 for (unsigned i = 1; i < NUM_TAPS / 2 + 1; ++i) {
138 unsigned base_pos = i * 2 - 1;
139 float w1 = weight[base_pos];
140 float w2 = weight[base_pos + 1];
142 float offset, total_weight;
143 if (w1 + w2 < 1e-6) {
147 offset = w2 / (w1 + w2);
148 total_weight = w1 + w2;
150 float x = 0.0f, y = 0.0f;
152 if (direction == HORIZONTAL) {
153 x = (base_pos + offset) / (float)width;
154 } else if (direction == VERTICAL) {
155 y = (base_pos + offset) / (float)height;
160 samples[4 * i + 0] = x;
161 samples[4 * i + 1] = y;
162 samples[4 * i + 2] = total_weight;
163 samples[4 * i + 3] = 0.0f;
166 set_uniform_vec4_array(glsl_program_num, prefix, "samples", samples, NUM_TAPS / 2 + 1);
169 void SingleBlurPassEffect::clear_gl_state()