X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=fft_pass_effect.cpp;h=deb0a1cd94db7373cddbe40abe326ed891fc29f3;hb=c2e19c0fb1952bb7f91ac9ce3b4d25f06b6d2ad6;hp=51bde35aec5d31ea313694b892668ab586b38026;hpb=5dfd8dfbd444210f9c9e3a0baf5c82b49e76e7ff;p=movit diff --git a/fft_pass_effect.cpp b/fft_pass_effect.cpp index 51bde35..deb0a1c 100644 --- a/fft_pass_effect.cpp +++ b/fft_pass_effect.cpp @@ -1,7 +1,9 @@ #include #include +#include "effect_chain.h" #include "effect_util.h" +#include "fp16.h" #include "fft_pass_effect.h" #include "util.h" @@ -39,15 +41,14 @@ void FFTPassEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, int input_size = (direction == VERTICAL) ? input_height : input_width; - // See the comments on changes_output_size() in the .h file to see - // why this is legal. It 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. - assert(*sampler_num == 1); - glActiveTexture(GL_TEXTURE0); + // Because of the memory layout (see below) and because we use offsets, + // the support texture values for many consecutive values will be + // the same. Thus, we can store a smaller texture (giving a small + // 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)); check_error(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); check_error(); @@ -79,7 +80,7 @@ void FFTPassEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, // bit, so the stride is 8, and so on. assert((fft_size & (fft_size - 1)) == 0); // Must be power of two. - float *tmp = new float[fft_size * 4]; + fp16_int_t *tmp = new fp16_int_t[fft_size * 4]; int subfft_size = 1 << pass_number; double mulfac; if (inverse) { @@ -91,9 +92,8 @@ void FFTPassEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, assert((fft_size & (fft_size - 1)) == 0); // Must be power of two. assert(fft_size % subfft_size == 0); int stride = fft_size / subfft_size; - for (int i = 0; i < fft_size; ++i) { - int k = i / stride; // Element number within this sub-FFT. - int offset = i % stride; // Sub-FFT number. + for (int i = 0; i < subfft_size; i++) { + int k = i; double twiddle_real, twiddle_imag; if (k < subfft_size / 2) { @@ -116,18 +116,20 @@ void FFTPassEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, // for using offsets and not direct coordinates as in GPUwave // is that we can have multiple FFTs along the same line, // and want to reuse the support texture by repeating it. - int base = k * stride * 2 + offset; - int support_texture_index; + int base = k * stride * 2; + int support_texture_index = i; + int src1 = base; + int src2 = base + stride; + double sign = 1.0; if (direction == FFTPassEffect::VERTICAL) { // Compensate for OpenGL's bottom-left convention. - support_texture_index = fft_size - i - 1; - } else { - support_texture_index = i; + support_texture_index = subfft_size - support_texture_index - 1; + sign = -1.0; } - tmp[support_texture_index * 4 + 0] = (base - support_texture_index) / double(input_size); - tmp[support_texture_index * 4 + 1] = (base + stride - support_texture_index) / double(input_size); - tmp[support_texture_index * 4 + 2] = twiddle_real; - tmp[support_texture_index * 4 + 3] = twiddle_imag; + tmp[support_texture_index * 4 + 0] = fp64_to_fp16(sign * (src1 - i * stride) / double(input_size)); + tmp[support_texture_index * 4 + 1] = fp64_to_fp16(sign * (src2 - i * stride) / double(input_size)); + tmp[support_texture_index * 4 + 2] = fp64_to_fp16(twiddle_real); + tmp[support_texture_index * 4 + 3] = fp64_to_fp16(twiddle_imag); } glActiveTexture(GL_TEXTURE0 + *sampler_num); @@ -143,11 +145,14 @@ void FFTPassEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, // 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), so we keep them at 32-bit. However, for - // small sizes, all components are exact anyway, so we can cheat there - // (although noting that the source coordinates become somewhat less - // accurate then, too). - glTexImage2D(GL_TEXTURE_2D, 0, (subfft_size <= 4) ? GL_RGBA16F : GL_RGBA32F, fft_size, 1, 0, GL_RGBA, GL_FLOAT, tmp); + // for the full reference); however, practical testing indicates that it's + // not a problem to keep the twiddle factors at 16-bit, at least as long as + // we round them properly--it would seem that Schatzman were mainly talking + // about poor sin()/cos() approximations. Thus, we store them in 16-bit, + // which gives a nice speed boost. + // + // Note that the source coordinates become somewhat less accurate too, though. + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, subfft_size, 1, 0, GL_RGBA, GL_HALF_FLOAT, tmp); check_error(); delete[] tmp;