- // We only have 15 taps to work with, and we want that to reach out to about 2.5*sigma.
- // Bump up the mipmap levels (giving us box blurs) until we have what we need.
- unsigned base_mipmap_level = 0;
- float adjusted_radius = radius;
- float pixel_size = 1.0f;
- while (adjusted_radius * 2.5f > 7.0f) {
- ++base_mipmap_level;
- adjusted_radius *= 0.5f;
- pixel_size *= 2.0f;
- }
-
- glActiveTexture(GL_TEXTURE0);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, base_mipmap_level);
- check_error();
-
- set_uniform_float(glsl_program_num, prefix, "pixel_offset", pixel_size / 1280.0f); // FIXME
-
- // Simple Gaussian weights for now.
- float weight[15], total = 0.0f;
- for (unsigned i = 0; i < 15; ++i) {
- float z = (i - 7.0f) / adjusted_radius;
- weight[i] = exp(-(z*z));
- total += weight[i];
+SingleBlurPassEffect::~SingleBlurPassEffect()
+{
+ delete[] uniform_samples;
+}
+
+string SingleBlurPassEffect::output_fragment_shader()
+{
+ char buf[256];
+ sprintf(buf, "#define DIRECTION_VERTICAL %d\n#define NUM_TAPS %d\n",
+ (direction == VERTICAL), num_taps);
+ uniform_samples = new float[2 * (num_taps / 2 + 1)];
+ register_uniform_vec2_array("samples", uniform_samples, num_taps / 2 + 1);
+ return buf + read_file("blur_effect.frag");
+}
+
+void SingleBlurPassEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
+{
+ Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
+
+ // Compute the weights; they will be symmetrical, so we only compute
+ // the right side.
+ float* weight = new float[num_taps + 1];
+ if (radius < 1e-3) {
+ weight[0] = 1.0f;
+ for (int i = 1; i < num_taps + 1; ++i) {
+ weight[i] = 0.0f;
+ }
+ } else {
+ float sum = 0.0f;
+ for (int i = 0; i < num_taps + 1; ++i) {
+ // Gaussian blur is a common, but maybe not the prettiest choice;
+ // it can feel a bit too blurry in the fine detail and too little
+ // long-tail. This is a simple logistic distribution, which has
+ // a narrower peak but longer tails.
+ //
+ // We interpret the radius as sigma, similar to Gaussian blur.
+ // Wikipedia says that sigma² = pi² s² / 3, which yields:
+ const float s = (sqrt(3.0) / M_PI) * radius;
+ float z = i / (2.0 * s);
+
+ weight[i] = 1.0f / (cosh(z) * cosh(z));
+
+ if (i == 0) {
+ sum += weight[i];
+ } else {
+ sum += 2.0f * weight[i];
+ }
+ }
+ for (int i = 0; i < num_taps + 1; ++i) {
+ weight[i] /= sum;
+ }