X-Git-Url: https://git.sesse.net/?p=movit;a=blobdiff_plain;f=blur_effect.cpp;h=adffe089a6d452d5f06a2c9907710aa152ee1f6f;hp=027d25b1113ef83e37f472e5dc561b95251a5484;hb=34776d3ed2565ee834405e575bf3bfc7f7933e36;hpb=8b58b0d92cbf62e173bd8a903774ba0d2c91c81f diff --git a/blur_effect.cpp b/blur_effect.cpp index 027d25b..adffe08 100644 --- a/blur_effect.cpp +++ b/blur_effect.cpp @@ -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,11 +185,21 @@ 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) { @@ -188,24 +207,16 @@ void SingleBlurPassEffect::set_gl_state(GLuint glsl_program_num, const string &p float w1 = weight[base_pos]; float w2 = weight[base_pos + 1]; - float offset, total_weight; - combine_two_samples(w1, w2, &offset, &total_weight, NULL); - - if (direction == HORIZONTAL) { - samples[2 * i + 0] = (base_pos + offset) / (float)width; - } else if (direction == VERTICAL) { - samples[2 * i + 0] = (base_pos + offset) / (float)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, num_subtexels, inv_num_subtexels, &pos, &total_weight, NULL); - 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()