]> git.sesse.net Git - movit/commitdiff
Some microoptimizations in combine_two_samples(). Saves about 4% in ResampleEffect...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 2 Jul 2017 08:06:54 +0000 (10:06 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 2 Jul 2017 08:06:54 +0000 (10:06 +0200)
blur_effect.cpp
resample_effect.cpp
util.cpp
util.h

index adffe089a6d452d5f06a2c9907710aa152ee1f6f..6c1fd3af6ddcab1495e8d0ae53f2635630f07b50 100644 (file)
@@ -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;
index ba8a71c0241443c0730a5ce99d68a2fa7c014ad3..30c40c4145fdd73903a7ce485d8ca75f7b28c783 100644 (file)
@@ -109,7 +109,7 @@ unsigned gcd(unsigned a, unsigned b)
 }
 
 template<class DestFloat>
-unsigned combine_samples(const Tap<float> *src, Tap<DestFloat> *dst, float num_subtexels, float inv_num_subtexels, unsigned num_src_samples, unsigned max_samples_saved)
+unsigned combine_samples(const Tap<float> *src, Tap<DestFloat> *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<float> *src, Tap<DestFloat> *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<float> *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<DestFloat>(weights + y * src_samples, NULL, num_subtexels, inv_num_subtexels, src_samples, max_samples_saved);
+               unsigned num_samples_saved = combine_samples<DestFloat>(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<float> *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);
        }
index 3bbc86963f33b35d624daf833e675799ff040dbf..54c815cc2be0f7e45967bcfddabea04cd4060639 100644 (file)
--- a/util.cpp
+++ b/util.cpp
@@ -220,7 +220,7 @@ string output_glsl_vec3(const string &name, float x, float y, float z)
 }
 
 template<class DestFloat>
-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<DestFloat>(pos1 + z * (pos2 - pos1));
-       z = (to_fp32(*offset) - pos1) / (pos2 - pos1);
+       *offset = from_fp32<DestFloat>(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>(float w1, float w2, float pos1, float pos2, float num_subtexels, float inv_num_subtexels,
+void combine_two_samples<float>(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<fp16_int_t>(float w1, float w2, float pos1, float pos2, float num_subtexels, float inv_num_subtexels,
+void combine_two_samples<fp16_int_t>(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 45fe6ba922b9728c5ac827ddb4b4d63a13a8ad11..577140b50f3ba7a05796a61f27a6683606c6b227 100644 (file)
--- 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.
-// <inv_num_subtexels> is simply its inverse (1/x).
+// <inv_num_subtexels> is simply its inverse (1/x). <pos1_pos2_diff> is
+// (pos2-pos1) and <inv_pos1_pos2_diff> 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<class DestFloat>
-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.