6 #include "blur_effect.h"
7 #include "effect_chain.h"
8 #include "effect_util.h"
15 BlurEffect::BlurEffect()
21 // The first blur pass will forward resolution information to us.
22 hpass = new SingleBlurPassEffect(this);
23 CHECK(hpass->set_int("direction", SingleBlurPassEffect::HORIZONTAL));
24 vpass = new SingleBlurPassEffect(NULL);
25 CHECK(vpass->set_int("direction", SingleBlurPassEffect::VERTICAL));
30 void BlurEffect::rewrite_graph(EffectChain *graph, Node *self)
32 Node *hpass_node = graph->add_node(hpass);
33 Node *vpass_node = graph->add_node(vpass);
34 graph->connect_nodes(hpass_node, vpass_node);
35 graph->replace_receiver(self, hpass_node);
36 graph->replace_sender(self, vpass_node);
37 self->disabled = true;
40 // We get this information forwarded from the first blur pass,
41 // since we are not part of the chain ourselves.
42 void BlurEffect::inform_input_size(unsigned input_num, unsigned width, unsigned height)
44 assert(input_num == 0);
48 input_height = height;
52 void BlurEffect::update_radius()
54 // We only have 16 taps to work with on each side, and we want that to
55 // reach out to about 2.5*sigma. Bump up the mipmap levels (giving us
56 // box blurs) until we have what we need.
57 unsigned mipmap_width = input_width, mipmap_height = input_height;
58 float adjusted_radius = radius;
59 while ((mipmap_width > 1 || mipmap_height > 1) && adjusted_radius * 1.5f > num_taps / 2) {
60 // Find the next mipmap size (round down, minimum 1 pixel).
61 mipmap_width = max(mipmap_width / 2, 1u);
62 mipmap_height = max(mipmap_height / 2, 1u);
64 // Approximate when mipmap sizes are odd, but good enough.
65 adjusted_radius = radius * float(mipmap_width) / float(input_width);
68 bool ok = hpass->set_float("radius", adjusted_radius);
69 ok |= hpass->set_int("width", mipmap_width);
70 ok |= hpass->set_int("height", mipmap_height);
71 ok |= hpass->set_int("virtual_width", mipmap_width);
72 ok |= hpass->set_int("virtual_height", mipmap_height);
73 ok |= hpass->set_int("num_taps", num_taps);
75 ok |= vpass->set_float("radius", adjusted_radius);
76 ok |= vpass->set_int("width", mipmap_width);
77 ok |= vpass->set_int("height", mipmap_height);
78 ok |= vpass->set_int("virtual_width", input_width);
79 ok |= vpass->set_int("virtual_height", input_height);
80 ok |= vpass->set_int("num_taps", num_taps);
85 bool BlurEffect::set_float(const string &key, float value) {
86 if (key == "radius") {
94 bool BlurEffect::set_int(const string &key, int value) {
95 if (key == "num_taps") {
96 if (value < 2 || value % 2 != 0) {
106 SingleBlurPassEffect::SingleBlurPassEffect(BlurEffect *parent)
110 direction(HORIZONTAL),
114 register_float("radius", &radius);
115 register_int("direction", (int *)&direction);
116 register_int("width", &width);
117 register_int("height", &height);
118 register_int("virtual_width", &virtual_width);
119 register_int("virtual_height", &virtual_height);
120 register_int("num_taps", &num_taps);
123 string SingleBlurPassEffect::output_fragment_shader()
126 sprintf(buf, "#define DIRECTION_VERTICAL %d\n#define NUM_TAPS %d\n",
127 (direction == VERTICAL), num_taps);
128 return buf + read_file("blur_effect.frag");
131 void SingleBlurPassEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
133 Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
135 // Compute the weights; they will be symmetrical, so we only compute
137 float* weight = new float[num_taps + 1];
140 for (int i = 1; i < num_taps + 1; ++i) {
145 for (int i = 0; i < num_taps + 1; ++i) {
146 // Gaussian blur is a common, but maybe not the prettiest choice;
147 // it can feel a bit too blurry in the fine detail and too little
148 // long-tail. This is a simple logistic distribution, which has
149 // a narrower peak but longer tails.
151 // We interpret the radius as sigma, similar to Gaussian blur.
152 // Wikipedia says that sigma² = pi² s² / 3, which yields:
153 const float s = (sqrt(3.0) / M_PI) * radius;
154 float z = i / (2.0 * s);
156 weight[i] = 1.0f / (cosh(z) * cosh(z));
161 sum += 2.0f * weight[i];
164 for (int i = 0; i < num_taps + 1; ++i) {
169 // Since the GPU gives us bilinear sampling for free, we can get two
170 // samples for the price of one (for every but the center sample,
171 // in which case this trick doesn't buy us anything). Simply sample
172 // between the two pixel centers, and we can do with fewer weights.
173 // (This is right even in the vertical pass where we don't actually
174 // sample between the pixels, because we have linear interpolation
177 // We pack the parameters into a float4: The relative sample coordinates
178 // in (x,y), and the weight in z. w is unused.
179 float* samples = new float[2 * (num_taps / 2 + 1)];
182 samples[2 * 0 + 0] = 0.0f;
183 samples[2 * 0 + 1] = weight[0];
185 // All other samples.
186 for (int i = 1; i < num_taps / 2 + 1; ++i) {
187 unsigned base_pos = i * 2 - 1;
188 float w1 = weight[base_pos];
189 float w2 = weight[base_pos + 1];
191 if (direction == HORIZONTAL) {
193 } else if (direction == VERTICAL) {
199 float pos1 = base_pos / (float)size;
200 float pos2 = (base_pos + 1) / (float)size;
201 float pos, total_weight;
202 combine_two_samples(w1, w2, pos1, pos2, size, COMBINE_DO_NOT_ROUND, &pos, &total_weight, NULL);
204 samples[2 * i + 0] = pos;
205 samples[2 * i + 1] = total_weight;
208 set_uniform_vec2_array(glsl_program_num, prefix, "samples", samples, num_taps / 2 + 1);
214 void SingleBlurPassEffect::clear_gl_state()