X-Git-Url: https://git.sesse.net/?p=movit;a=blobdiff_plain;f=fft_pass_effect.cpp;h=9c8aeb058e71fa33435901d4f5a2588a38ccb4f3;hp=a3de3792b380060f1c6effe112bfd5a1d0270344;hb=0597b51d28b5f095931c54a5e5f90ed9d5e18790;hpb=c4f0d4e876a8177db5738596f22349e030e0a1dc diff --git a/fft_pass_effect.cpp b/fft_pass_effect.cpp index a3de379..9c8aeb0 100644 --- a/fft_pass_effect.cpp +++ b/fft_pass_effect.cpp @@ -1,9 +1,16 @@ #include +#include -#include "fft_pass_effect.h" +#include "effect_chain.h" #include "effect_util.h" +#include "fp16.h" +#include "fft_pass_effect.h" #include "util.h" +using namespace std; + +namespace movit { + FFTPassEffect::FFTPassEffect() : input_width(1280), input_height(720), @@ -21,28 +28,26 @@ FFTPassEffect::~FFTPassEffect() glDeleteTextures(1, &tex); } -std::string FFTPassEffect::output_fragment_shader() +string FFTPassEffect::output_fragment_shader() { char buf[256]; sprintf(buf, "#define DIRECTION_VERTICAL %d\n", (direction == VERTICAL)); return buf + read_file("fft_pass_effect.frag"); } -void FFTPassEffect::set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num) +void FFTPassEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num) { Effect::set_gl_state(glsl_program_num, prefix, sampler_num); 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); + // 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(); @@ -74,7 +79,7 @@ void FFTPassEffect::set_gl_state(GLuint glsl_program_num, const std::string &pre // 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) { @@ -119,30 +124,33 @@ void FFTPassEffect::set_gl_state(GLuint glsl_program_num, const std::string &pre } else { support_texture_index = i; } - 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((base - support_texture_index) / double(input_size)); + tmp[support_texture_index * 4 + 1] = fp64_to_fp16((base + stride - support_texture_index) / 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); check_error(); - glBindTexture(GL_TEXTURE_1D, tex); + glBindTexture(GL_TEXTURE_2D, tex); check_error(); - glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); check_error(); - glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); check_error(); - glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT); + 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), 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). - glTexImage1D(GL_TEXTURE_1D, 0, (subfft_size <= 4) ? GL_RGBA16F : GL_RGBA32F, fft_size, 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, fft_size, 1, 0, GL_RGBA, GL_HALF_FLOAT, tmp); check_error(); delete[] tmp; @@ -153,3 +161,5 @@ void FFTPassEffect::set_gl_state(GLuint glsl_program_num, const std::string &pre assert(input_size % fft_size == 0); set_uniform_float(glsl_program_num, prefix, "num_repeats", input_size / fft_size); } + +} // namespace movit