From: Steinar H. Gunderson Date: Sun, 2 Jul 2017 08:06:54 +0000 (+0200) Subject: Some microoptimizations in combine_two_samples(). Saves about 4% in ResampleEffect... X-Git-Tag: 1.5.2~6 X-Git-Url: https://git.sesse.net/?p=movit;a=commitdiff_plain;h=532dc15e43ee8c26de0e9f13bc56d92b4a6b1379 Some microoptimizations in combine_two_samples(). Saves about 4% in ResampleEffect::calculate_weights. --- diff --git a/blur_effect.cpp b/blur_effect.cpp index adffe08..6c1fd3a 100644 --- a/blur_effect.cpp +++ b/blur_effect.cpp @@ -208,9 +208,8 @@ void SingleBlurPassEffect::set_gl_state(GLuint glsl_program_num, const string &p float w2 = weight[base_pos + 1]; 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); + combine_two_samples(w1, w2, pos1, 1.0 / (float)size, size, num_subtexels, inv_num_subtexels, &pos, &total_weight, NULL); uniform_samples[2 * i + 0] = pos; uniform_samples[2 * i + 1] = total_weight; diff --git a/resample_effect.cpp b/resample_effect.cpp index ba8a71c..30c40c4 100644 --- a/resample_effect.cpp +++ b/resample_effect.cpp @@ -109,7 +109,7 @@ unsigned gcd(unsigned a, unsigned b) } template -unsigned combine_samples(const Tap *src, Tap *dst, float num_subtexels, float inv_num_subtexels, unsigned num_src_samples, unsigned max_samples_saved) +unsigned combine_samples(const Tap *src, Tap *dst, float num_subtexels, float inv_num_subtexels, unsigned num_src_samples, unsigned max_samples_saved, float pos1_pos2_diff, float inv_pos1_pos2_diff) { // Cut off near-zero values at both sides. unsigned num_samples_saved = 0; @@ -157,7 +157,7 @@ unsigned combine_samples(const Tap *src, Tap *dst, float num_s DestFloat pos, total_weight; float sum_sq_error; - combine_two_samples(w1, w2, pos1, pos2, num_subtexels, inv_num_subtexels, &pos, &total_weight, &sum_sq_error); + combine_two_samples(w1, w2, pos1, pos1_pos2_diff, inv_pos1_pos2_diff, num_subtexels, inv_num_subtexels, &pos, &total_weight, &sum_sq_error); // If the interpolation error is larger than that of about sqrt(2) of // a level at 8-bit precision, don't combine. (You'd think 1.0 was enough, @@ -210,10 +210,12 @@ unsigned combine_many_samples(const Tap *weights, unsigned src_size, unsi { float num_subtexels = src_size / movit_texel_subpixel_precision; float inv_num_subtexels = movit_texel_subpixel_precision / src_size; + float pos1_pos2_diff = 1.0f / src_size; + float inv_pos1_pos2_diff = src_size; unsigned max_samples_saved = UINT_MAX; for (unsigned y = 0; y < dst_samples && max_samples_saved > 0; ++y) { - unsigned num_samples_saved = combine_samples(weights + y * src_samples, NULL, num_subtexels, inv_num_subtexels, src_samples, max_samples_saved); + unsigned num_samples_saved = combine_samples(weights + y * src_samples, NULL, num_subtexels, inv_num_subtexels, src_samples, max_samples_saved, pos1_pos2_diff, inv_pos1_pos2_diff); max_samples_saved = min(max_samples_saved, num_samples_saved); } @@ -228,7 +230,9 @@ unsigned combine_many_samples(const Tap *weights, unsigned src_size, unsi num_subtexels, inv_num_subtexels, src_samples, - max_samples_saved); + max_samples_saved, + pos1_pos2_diff, + inv_pos1_pos2_diff); assert(num_samples_saved == max_samples_saved); normalize_sum(bilinear_weights_ptr, src_bilinear_samples); } diff --git a/util.cpp b/util.cpp index 3bbc869..54c815c 100644 --- a/util.cpp +++ b/util.cpp @@ -220,7 +220,7 @@ string output_glsl_vec3(const string &name, float x, float y, float z) } template -void combine_two_samples(float w1, float w2, float pos1, float pos2, float num_subtexels, float inv_num_subtexels, +void combine_two_samples(float w1, float w2, float pos1, float pos1_pos2_diff, float inv_pos1_pos2_diff, float num_subtexels, float inv_num_subtexels, DestFloat *offset, DestFloat *total_weight, float *sum_sq_error) { assert(movit_initialized); @@ -233,8 +233,8 @@ void combine_two_samples(float w1, float w2, float pos1, float pos2, float num_s } // Round to the desired precision. Note that this might take z outside the 0..1 range. - *offset = from_fp32(pos1 + z * (pos2 - pos1)); - z = (to_fp32(*offset) - pos1) / (pos2 - pos1); + *offset = from_fp32(pos1 + z * pos1_pos2_diff); + z = (to_fp32(*offset) - pos1) * inv_pos1_pos2_diff; // Round to the minimum number of bits we have measured earlier. // The card will do this for us anyway, but if we know what the real z @@ -265,11 +265,11 @@ void combine_two_samples(float w1, float w2, float pos1, float pos2, float num_s // Explicit instantiations. template -void combine_two_samples(float w1, float w2, float pos1, float pos2, float num_subtexels, float inv_num_subtexels, +void combine_two_samples(float w1, float w2, float pos1, float pos1_pos2_diff, float inv_pos1_pos2_diff, float num_subtexels, float inv_num_subtexels, float *offset, float *total_weight, float *sum_sq_error); template -void combine_two_samples(float w1, float w2, float pos1, float pos2, float num_subtexels, float inv_num_subtexels, +void combine_two_samples(float w1, float w2, float pos1, float pos1_pos2_diff, float inv_pos1_pos2_diff, float num_subtexels, float inv_num_subtexels, fp16_int_t *offset, fp16_int_t *total_weight, float *sum_sq_error); GLuint generate_vbo(GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data) diff --git a/util.h b/util.h index 45fe6ba..577140b 100644 --- a/util.h +++ b/util.h @@ -59,7 +59,8 @@ enum CombineRoundingBehavior { // number of distinct accessible subtexels in the given mipmap level, // calculated by num_texels / movit_texel_subpixel_precision. It is a float // for performance reasons, even though it is expected to be a whole number. -// is simply its inverse (1/x). +// is simply its inverse (1/x). is +// (pos2-pos1) and is 1/(pos2-pos1). // // Note that since the GPU might have limited precision in its linear // interpolation, the effective weights might be different from the ones you @@ -72,7 +73,7 @@ enum CombineRoundingBehavior { // rounded fp16 value. This enables more precise calculation of total_weight // and sum_sq_error. template -void combine_two_samples(float w1, float w2, float pos1, float pos2, float num_subtexels, float inv_num_subtexels, +void combine_two_samples(float w1, float w2, float pos1, float pos1_pos2_diff, float inv_pos1_pos2_diff, float num_subtexels, float inv_num_subtexels, DestFloat *offset, DestFloat *total_weight, float *sum_sq_error); // Create a VBO with the given data. Returns the VBO number.