]> git.sesse.net Git - movit/blobdiff - blur_effect.cpp
Hard-assert on something that has bitten me too many times now.
[movit] / blur_effect.cpp
index 29e46b0583890f1e1f2c4b65ec130ec086f38bb9..adffe089a6d452d5f06a2c9907710aa152ee1f6f 100644 (file)
@@ -6,6 +6,7 @@
 #include "blur_effect.h"
 #include "effect_chain.h"
 #include "effect_util.h"
+#include "init.h"
 #include "util.h"
 
 using namespace std;
@@ -108,8 +109,9 @@ SingleBlurPassEffect::SingleBlurPassEffect(BlurEffect *parent)
          num_taps(16),
          radius(3.0f),
          direction(HORIZONTAL),
-         width(1280),
-         height(720)
+         width(1280),
+         height(720),
+         uniform_samples(NULL)
 {
        register_float("radius", &radius);
        register_int("direction", (int *)&direction);
@@ -120,11 +122,18 @@ SingleBlurPassEffect::SingleBlurPassEffect(BlurEffect *parent)
        register_int("num_taps", &num_taps);
 }
 
+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");
 }
 
@@ -176,39 +185,38 @@ void SingleBlurPassEffect::set_gl_state(GLuint glsl_program_num, const string &p
        //
        // We pack the parameters into a float4: The relative sample coordinates
        // in (x,y), and the weight in z. w is unused.
-       float* samples = new float[2 * (num_taps / 2 + 1)];
 
        // Center sample.
-       samples[2 * 0 + 0] = 0.0f;
-       samples[2 * 0 + 1] = weight[0];
+       uniform_samples[2 * 0 + 0] = 0.0f;
+       uniform_samples[2 * 0 + 1] = weight[0];
+
+       int size;
+       if (direction == HORIZONTAL) {
+               size = width;
+       } else if (direction == VERTICAL) {
+               size = height;
+       } else {
+               assert(false);
+       }
+       float num_subtexels = size / movit_texel_subpixel_precision;
+       float inv_num_subtexels = movit_texel_subpixel_precision / size;
 
        // All other samples.
        for (int i = 1; i < num_taps / 2 + 1; ++i) {
                unsigned base_pos = i * 2 - 1;
                float w1 = weight[base_pos];
                float w2 = weight[base_pos + 1];
-               int size;
-               if (direction == HORIZONTAL) {
-                       size = width;
-               } else if (direction == VERTICAL) {
-                       size = height;
-               } else {
-                       assert(false);
-               }
 
                float pos1 = base_pos / (float)size;
                float pos2 = (base_pos + 1) / (float)size;
                float pos, total_weight;
-               combine_two_samples(w1, w2, pos1, pos2, size, &pos, &total_weight, NULL);
+               combine_two_samples(w1, w2, pos1, pos2, num_subtexels, inv_num_subtexels, &pos, &total_weight, NULL);
 
-               samples[2 * i + 0] = pos;
-               samples[2 * i + 1] = total_weight;
+               uniform_samples[2 * i + 0] = pos;
+               uniform_samples[2 * i + 1] = total_weight;
        }
 
-       set_uniform_vec2_array(glsl_program_num, prefix, "samples", samples, num_taps / 2 + 1);
-
        delete[] weight;
-       delete[] samples;
 }
 
 void SingleBlurPassEffect::clear_gl_state()