4 #include "blur_effect.h"
5 #include "effect_chain.h"
9 // Must match blur_effect.frag.
12 BlurEffect::BlurEffect()
15 hpass = new SingleBlurPassEffect();
16 hpass->set_int("direction", SingleBlurPassEffect::HORIZONTAL);
17 vpass = new SingleBlurPassEffect();
18 vpass->set_int("direction", SingleBlurPassEffect::VERTICAL);
23 void BlurEffect::rewrite_graph(EffectChain *graph, Node *self)
25 Node *hpass_node = graph->add_node(hpass);
26 Node *vpass_node = graph->add_node(vpass);
27 graph->connect_nodes(hpass_node, vpass_node);
28 graph->replace_receiver(self, hpass_node);
29 graph->replace_sender(self, vpass_node);
30 self->disabled = true;
33 void BlurEffect::update_radius()
35 // We only have 16 taps to work with on each side, and we want that to
36 // reach out to about 2.5*sigma. Bump up the mipmap levels (giving us
37 // box blurs) until we have what we need.
39 // TODO: Consider the actual width and height (they influence mipmap
41 unsigned base_mipmap_level = 0;
42 float adjusted_radius = radius;
43 while (adjusted_radius * 1.5f > NUM_TAPS / 2) {
45 adjusted_radius /= 2.0f;
48 bool ok = hpass->set_float("radius", adjusted_radius);
49 ok |= hpass->set_int("width", 1280 / (1 << base_mipmap_level)); // FIXME
50 ok |= hpass->set_int("height", 720 / (1 << base_mipmap_level)); // FIXME
52 ok |= vpass->set_float("radius", adjusted_radius);
53 ok |= vpass->set_int("width", 1280 / (1 << base_mipmap_level)); // FIXME
54 ok |= vpass->set_int("height", 720 / (1 << base_mipmap_level)); // FIXME
59 bool BlurEffect::set_float(const std::string &key, float value) {
60 if (key == "radius") {
68 SingleBlurPassEffect::SingleBlurPassEffect()
70 direction(HORIZONTAL),
74 register_float("radius", &radius);
75 register_int("direction", (int *)&direction);
76 register_int("width", &width);
77 register_int("height", &height);
80 std::string SingleBlurPassEffect::output_fragment_shader()
82 return read_file("blur_effect.frag");
85 void SingleBlurPassEffect::set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
87 Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
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 / 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) {
119 // Since the GPU gives us bilinear sampling for free, we can get two
120 // samples for the price of one (for every but the center sample,
121 // in which case this trick doesn't buy us anything). Simply sample
122 // between the two pixel centers, and we can do with fewer weights.
123 // (This is right even in the vertical pass where we don't actually
124 // sample between the pixels, because we have linear interpolation
127 // We pack the parameters into a float4: The relative sample coordinates
128 // in (x,y), and the weight in z. w is unused.
129 float samples[4 * (NUM_TAPS / 2 + 1)];
132 samples[4 * 0 + 0] = 0.0f;
133 samples[4 * 0 + 1] = 0.0f;
134 samples[4 * 0 + 2] = weight[0];
135 samples[4 * 0 + 3] = 0.0f;
137 // All other samples.
138 for (unsigned i = 1; i < NUM_TAPS / 2 + 1; ++i) {
139 unsigned base_pos = i * 2 - 1;
140 float w1 = weight[base_pos];
141 float w2 = weight[base_pos + 1];
143 float offset, total_weight;
144 if (w1 + w2 < 1e-6) {
148 offset = w2 / (w1 + w2);
149 total_weight = w1 + w2;
151 float x = 0.0f, y = 0.0f;
153 if (direction == HORIZONTAL) {
154 x = (base_pos + offset) / (float)width;
155 } else if (direction == VERTICAL) {
156 y = (base_pos + offset) / (float)height;
161 samples[4 * i + 0] = x;
162 samples[4 * i + 1] = y;
163 samples[4 * i + 2] = total_weight;
164 samples[4 * i + 3] = 0.0f;
167 set_uniform_vec4_array(glsl_program_num, prefix, "samples", samples, NUM_TAPS / 2 + 1);
170 void SingleBlurPassEffect::clear_gl_state()