4 #include "blur_effect.h"
5 #include "effect_chain.h"
9 // Must match blur_effect.frag.
12 BlurEffect::BlurEffect()
17 // The first blur pass will forward resolution information to us.
18 hpass = new SingleBlurPassEffect(this);
19 hpass->set_int("direction", SingleBlurPassEffect::HORIZONTAL);
20 vpass = new SingleBlurPassEffect(NULL);
21 vpass->set_int("direction", SingleBlurPassEffect::VERTICAL);
26 void BlurEffect::rewrite_graph(EffectChain *graph, Node *self)
28 Node *hpass_node = graph->add_node(hpass);
29 Node *vpass_node = graph->add_node(vpass);
30 graph->connect_nodes(hpass_node, vpass_node);
31 graph->replace_receiver(self, hpass_node);
32 graph->replace_sender(self, vpass_node);
33 self->disabled = true;
36 // We get this information forwarded from the first blur pass,
37 // since we are not part of the chain ourselves.
38 void BlurEffect::inform_input_size(unsigned input_num, unsigned width, unsigned height)
40 assert(input_num == 0);
44 input_height = height;
48 void BlurEffect::update_radius()
50 // We only have 16 taps to work with on each side, and we want that to
51 // reach out to about 2.5*sigma. Bump up the mipmap levels (giving us
52 // box blurs) until we have what we need.
53 unsigned mipmap_width = input_width, mipmap_height = input_height;
54 float adjusted_radius = radius;
55 while ((mipmap_width > 1 || mipmap_height > 1) && adjusted_radius * 1.5f > NUM_TAPS / 2) {
56 // Find the next mipmap size (round down, minimum 1 pixel).
57 mipmap_width = std::max(mipmap_width / 2, 1u);
58 mipmap_height = std::max(mipmap_height / 2, 1u);
60 // Approximate when mipmap sizes are odd, but good enough.
61 adjusted_radius = radius * float(mipmap_width) / float(input_width);
64 bool ok = hpass->set_float("radius", adjusted_radius);
65 ok |= hpass->set_int("width", mipmap_width);
66 ok |= hpass->set_int("height", mipmap_height);
68 ok |= vpass->set_float("radius", adjusted_radius);
69 ok |= vpass->set_int("width", mipmap_width);
70 ok |= vpass->set_int("height", mipmap_height);
75 bool BlurEffect::set_float(const std::string &key, float value) {
76 if (key == "radius") {
84 SingleBlurPassEffect::SingleBlurPassEffect(BlurEffect *parent)
87 direction(HORIZONTAL),
91 register_float("radius", &radius);
92 register_int("direction", (int *)&direction);
93 register_int("width", &width);
94 register_int("height", &height);
97 std::string SingleBlurPassEffect::output_fragment_shader()
99 return read_file("blur_effect.frag");
102 void SingleBlurPassEffect::set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
104 Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
106 // Compute the weights; they will be symmetrical, so we only compute
108 float weight[NUM_TAPS + 1];
111 for (unsigned i = 1; i < NUM_TAPS + 1; ++i) {
116 for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
117 // Gaussian blur is a common, but maybe not the prettiest choice;
118 // it can feel a bit too blurry in the fine detail and too little
119 // long-tail. This is a simple logistic distribution, which has
120 // a narrower peak but longer tails.
122 // We interpret the radius as sigma, similar to Gaussian blur.
123 // Wikipedia says that sigma² = pi² s² / 3, which yields:
124 const float s = (sqrt(3.0) / M_PI) * radius;
125 float z = i / (2.0 * s);
127 weight[i] = 1.0f / (cosh(z) * cosh(z));
132 sum += 2.0f * weight[i];
135 for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
140 // Since the GPU gives us bilinear sampling for free, we can get two
141 // samples for the price of one (for every but the center sample,
142 // in which case this trick doesn't buy us anything). Simply sample
143 // between the two pixel centers, and we can do with fewer weights.
144 // (This is right even in the vertical pass where we don't actually
145 // sample between the pixels, because we have linear interpolation
148 // We pack the parameters into a float4: The relative sample coordinates
149 // in (x,y), and the weight in z. w is unused.
150 float samples[4 * (NUM_TAPS / 2 + 1)];
153 samples[4 * 0 + 0] = 0.0f;
154 samples[4 * 0 + 1] = 0.0f;
155 samples[4 * 0 + 2] = weight[0];
156 samples[4 * 0 + 3] = 0.0f;
158 // All other samples.
159 for (unsigned i = 1; i < NUM_TAPS / 2 + 1; ++i) {
160 unsigned base_pos = i * 2 - 1;
161 float w1 = weight[base_pos];
162 float w2 = weight[base_pos + 1];
164 float offset, total_weight;
165 if (w1 + w2 < 1e-6) {
169 offset = w2 / (w1 + w2);
170 total_weight = w1 + w2;
172 float x = 0.0f, y = 0.0f;
174 if (direction == HORIZONTAL) {
175 x = (base_pos + offset) / (float)width;
176 } else if (direction == VERTICAL) {
177 y = (base_pos + offset) / (float)height;
182 samples[4 * i + 0] = x;
183 samples[4 * i + 1] = y;
184 samples[4 * i + 2] = total_weight;
185 samples[4 * i + 3] = 0.0f;
188 set_uniform_vec4_array(glsl_program_num, prefix, "samples", samples, NUM_TAPS / 2 + 1);
191 void SingleBlurPassEffect::clear_gl_state()