]> git.sesse.net Git - movit/commitdiff
Cache the FFT support texture.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 22 Mar 2014 16:18:12 +0000 (17:18 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 22 Mar 2014 16:18:32 +0000 (17:18 +0100)
Regenerating it every time is a waste of CPU, and also of GL
state changes.

fft_pass_effect.cpp
fft_pass_effect.h

index deb0a1cd94db7373cddbe40abe326ed891fc29f3..e3999b30682a1514b922ef64b825fe9096ab6065 100644 (file)
@@ -14,7 +14,12 @@ namespace movit {
 FFTPassEffect::FFTPassEffect()
        : input_width(1280),
          input_height(720),
-         direction(HORIZONTAL)
+         direction(HORIZONTAL),
+         last_fft_size(-1),
+         last_direction(INVALID),
+         last_pass_number(-1),
+         last_inverse(-1),
+         last_input_size(-1)
 {
        register_int("fft_size", &fft_size);
        register_int("direction", (int *)&direction);
@@ -39,7 +44,18 @@ void FFTPassEffect::set_gl_state(GLuint glsl_program_num, const string &prefix,
 {
        Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
 
-       int input_size = (direction == VERTICAL) ? input_height : input_width;
+       // This is needed because it counteracts the precision issues we get
+       // because we sample the input texture with normalized coordinates
+       // (especially when the repeat count along the axis is not a power of
+       // two); we very rapidly end up in narrowly missing a texel center,
+       // which causes precision loss to propagate throughout the FFT.
+       Node *self = chain->find_node_for_effect(this);
+       glActiveTexture(chain->get_input_sampler(self, 0));
+       check_error();
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+       check_error();
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+       check_error();
 
        // Because of the memory layout (see below) and because we use offsets,
        // the support texture values for many consecutive values will be
@@ -47,13 +63,36 @@ void FFTPassEffect::set_gl_state(GLuint glsl_program_num, const string &prefix,
        // performance boost) and just sample it with NEAREST. Also, this
        // counteracts any precision issues we might get from linear
        // interpolation.
-       Node *self = chain->find_node_for_effect(this);
-       glActiveTexture(chain->get_input_sampler(self, 0));
+       glActiveTexture(GL_TEXTURE0 + *sampler_num);
+       check_error();
+       glBindTexture(GL_TEXTURE_2D, tex);
        check_error();
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        check_error();
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        check_error();
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+       check_error();
+
+       int input_size = (direction == VERTICAL) ? input_height : input_width;
+       if (last_fft_size != fft_size ||
+           last_direction != direction ||
+           last_pass_number != pass_number ||
+           last_inverse != inverse ||
+           last_input_size != input_size) {
+               generate_support_texture();
+       }
+
+       set_uniform_int(glsl_program_num, prefix, "support_tex", *sampler_num);
+       ++*sampler_num;
+
+       assert(input_size % fft_size == 0);
+       set_uniform_float(glsl_program_num, prefix, "num_repeats", input_size / fft_size);
+}
+
+void FFTPassEffect::generate_support_texture()
+{
+       int input_size = (direction == VERTICAL) ? input_height : input_width;
 
        // The memory layout follows figure 5.2 on page 25 of
        // http://gpuwave.sesse.net/gpuwave.pdf -- it can be a bit confusing
@@ -132,17 +171,6 @@ void FFTPassEffect::set_gl_state(GLuint glsl_program_num, const string &prefix,
                tmp[support_texture_index * 4 + 3] = fp64_to_fp16(twiddle_imag);
        }
 
-       glActiveTexture(GL_TEXTURE0 + *sampler_num);
-       check_error();
-       glBindTexture(GL_TEXTURE_2D, tex);
-       check_error();
-       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-       check_error();
-       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-       check_error();
-       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
-       check_error();
-
        // Supposedly FFTs are very sensitive to inaccuracies in the twiddle factors,
        // at least according to a paper by Schatzman (see gpuwave.pdf reference [30]
        // for the full reference); however, practical testing indicates that it's
@@ -157,11 +185,11 @@ void FFTPassEffect::set_gl_state(GLuint glsl_program_num, const string &prefix,
 
        delete[] tmp;
 
-       set_uniform_int(glsl_program_num, prefix, "support_tex", *sampler_num);
-       ++*sampler_num;
-
-       assert(input_size % fft_size == 0);
-       set_uniform_float(glsl_program_num, prefix, "num_repeats", input_size / fft_size);
+       last_fft_size = fft_size;
+       last_direction = direction;
+       last_pass_number = pass_number;
+       last_inverse = inverse;
+       last_input_size = input_size;
 }
 
 }  // namespace movit
index 460a946be4dcbdb24f8bddfcc011cb3cdb6af6a0..7689cf1e60688fb60d1c620baffb6ba1fac1dab9 100644 (file)
@@ -101,16 +101,25 @@ public:
 
        virtual void inform_added(EffectChain *chain) { this->chain = chain; }
        
-       enum Direction { HORIZONTAL = 0, VERTICAL = 1 };
+       enum Direction { INVALID = -1, HORIZONTAL = 0, VERTICAL = 1 };
 
 private:
+       void generate_support_texture();
+
        EffectChain *chain;
        int input_width, input_height;
        GLuint tex;
+
        int fft_size;
        Direction direction;
        int pass_number;  // From 1..n.
        int inverse;  // 0 = forward (FFT), 1 = reverse (IFFT).
+
+       int last_fft_size;
+       Direction last_direction;
+       int last_pass_number;
+       int last_inverse;
+       int last_input_size;
 };
 
 }  // namespace movit