1 #define GL_GLEXT_PROTOTYPES 1
8 #include "blur_effect.h"
11 // Must match blur_effect.frag.
14 BlurEffect::BlurEffect()
18 register_float("radius", (float *)&radius);
19 register_int("direction", (int *)&direction);
22 std::string BlurEffect::output_fragment_shader()
24 return read_file("blur_effect.frag");
27 void BlurEffect::set_uniforms(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
29 Effect::set_uniforms(glsl_program_num, prefix, sampler_num);
31 int base_texture_size, texture_size;
32 if (direction == HORIZONTAL) {
33 base_texture_size = texture_size = 1280; // FIXME
34 } else if (direction == VERTICAL) {
35 base_texture_size = texture_size = 720; // FIXME
40 // We only have 16 taps to work with on each side, and we want that to
41 // reach out to about 2.5*sigma. Bump up the mipmap levels (giving us
42 // box blurs) until we have what we need.
44 // FIXME: we really need to pick the same mipmap level for both horizontal and vertical!
45 unsigned base_mipmap_level = 0;
46 float adjusted_radius = radius;
47 while (texture_size > 1 && adjusted_radius * 2.5f > NUM_TAPS / 2) {
49 texture_size /= 2; // Rounding down.
50 adjusted_radius = radius * float(texture_size) / float(base_texture_size);
53 // In the second pass, we do the same, but don't sample from a mipmap;
54 // that would re-blur the other direction in an ugly fashion, and we already
55 // have the vertical box blur we need from that pass.
57 // TODO: We really need to present horizontal+vertical as a unit;
58 // currently, there's really no guarantee vertical blur is the second pass.
59 if (direction == VERTICAL) {
60 base_mipmap_level = 0;
63 glActiveTexture(GL_TEXTURE0);
65 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, base_mipmap_level);
67 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, base_mipmap_level);
70 // Compute the weights; they will be symmetrical, so we only compute
72 float weight[NUM_TAPS + 1];
75 for (unsigned i = 1; i < NUM_TAPS + 1; ++i) {
80 for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
81 float z = i / adjusted_radius;
83 // Gaussian blur is a common, but maybe not the prettiest choice;
84 // it can feel a bit too blurry in the fine detail and too little
85 // long-tail. This is a simple logistic distribution, which has
86 // a narrower peak but longer tails.
87 weight[i] = 1.0f / (cosh(z) * cosh(z));
92 sum += 2.0f * weight[i];
95 for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
101 // NOTE: This is currently broken.
103 // Since the GPU gives us bilinear sampling for free, we can get two
104 // samples for the price of one (for every but the center sample,
105 // in which case this trick doesn't buy us anything). Simply sample
106 // between the two pixel centers, and we can do with fewer weights.
107 // (This is right even in the vertical pass where we don't actually
108 // sample between the pixels, because we have linear interpolation
111 // We pack the parameters into a float4: The relative sample coordinates
112 // in (x,y), and the weight in z. w is unused.
113 float samples[4 * (NUM_TAPS / 2 + 1)];
116 samples[4 * 0 + 0] = 0.0f;
117 samples[4 * 0 + 1] = 0.0f;
118 samples[4 * 0 + 2] = weight[0];
119 samples[4 * 0 + 3] = 0.0f;
121 // All other samples.
122 for (unsigned i = 1; i < NUM_TAPS / 2 + 1; ++i) {
123 unsigned base_pos = i * 2 - 1;
124 float w1 = weight[base_pos];
125 float w2 = weight[base_pos + 1];
127 float offset, total_weight;
128 if (w1 + w2 < 1e-6) {
132 offset = w2 / (w1 + w2);
133 total_weight = w1 + w2;
136 // hack for easier visualization
140 float x = 0.0f, y = 0.0f;
142 if (direction == HORIZONTAL) {
143 x = (base_pos + offset) / (float)texture_size;
144 } else if (direction == VERTICAL) {
145 y = (base_pos + offset) / (float)texture_size;
150 samples[4 * i + 0] = x;
151 samples[4 * i + 1] = y;
152 samples[4 * i + 2] = total_weight;
153 samples[4 * i + 3] = 0.0f;
156 set_uniform_vec4_array(glsl_program_num, prefix, "samples", samples, NUM_TAPS / 2 + 1);
158 // Boring, at-whole-pixels sampling.
159 float samples[4 * NUM_TAPS];
161 // All other samples.
162 for (unsigned i = 0; i < NUM_TAPS + 1; ++i) {
163 float x = 0.0f, y = 0.0f;
165 if (direction == HORIZONTAL) {
166 x = i / (float)texture_size;
167 } else if (direction == VERTICAL) {
168 y = i / (float)texture_size;
173 samples[4 * i + 0] = x;
174 samples[4 * i + 1] = y;
175 samples[4 * i + 2] = weight[i];
176 samples[4 * i + 3] = 0.0f;
179 set_uniform_vec4_array(glsl_program_num, prefix, "samples", samples, NUM_TAPS + 1);