]> git.sesse.net Git - movit/blob - resample_effect.cpp
Small refactoring in YCbCrInput.
[movit] / resample_effect.cpp
1 // Three-lobed Lanczos, the most common choice.
2 #define LANCZOS_RADIUS 3.0
3
4 #include <epoxy/gl.h>
5 #include <assert.h>
6 #include <limits.h>
7 #include <math.h>
8 #include <stdio.h>
9 #include <algorithm>
10 #include <Eigen/Sparse>
11 #include <Eigen/SparseQR>
12 #include <Eigen/OrderingMethods>
13
14 #include "effect_chain.h"
15 #include "effect_util.h"
16 #include "fp16.h"
17 #include "init.h"
18 #include "resample_effect.h"
19 #include "util.h"
20
21 using namespace Eigen;
22 using namespace std;
23
24 namespace movit {
25
26 namespace {
27
28 template<class T>
29 struct Tap {
30         T weight;
31         T pos;
32 };
33
34 float sinc(float x)
35 {
36         if (fabs(x) < 1e-6) {
37                 return 1.0f - fabs(x);
38         } else {
39                 return sin(x) / x;
40         }
41 }
42
43 float lanczos_weight(float x, float a)
44 {
45         if (fabs(x) > a) {
46                 return 0.0f;
47         } else {
48                 return sinc(M_PI * x) * sinc(M_PI * x / a);
49         }
50 }
51
52 // Euclid's algorithm, from Wikipedia.
53 unsigned gcd(unsigned a, unsigned b)
54 {
55         while (b != 0) {
56                 unsigned t = b;
57                 b = a % b;
58                 a = t;
59         }
60         return a;
61 }
62
63 template<class DestFloat>
64 unsigned combine_samples(const Tap<float> *src, Tap<DestFloat> *dst, unsigned src_size, unsigned num_src_samples, unsigned max_samples_saved)
65 {
66         // Cut off near-zero values at both sides.
67         unsigned num_samples_saved = 0;
68         while (num_samples_saved < max_samples_saved &&
69                num_src_samples > 0 &&
70                fabs(src[0].weight) < 1e-6) {
71                 ++src;
72                 --num_src_samples;
73                 ++num_samples_saved;
74         }
75         while (num_samples_saved < max_samples_saved &&
76                num_src_samples > 0 &&
77                fabs(src[num_src_samples - 1].weight) < 1e-6) {
78                 --num_src_samples;
79                 ++num_samples_saved;
80         }
81
82         for (unsigned i = 0, j = 0; i < num_src_samples; ++i, ++j) {
83                 // Copy the sample directly; it will be overwritten later if we can combine.
84                 if (dst != NULL) {
85                         dst[j].weight = convert_float<float, DestFloat>(src[i].weight);
86                         dst[j].pos = convert_float<float, DestFloat>(src[i].pos);
87                 }
88
89                 if (i == num_src_samples - 1) {
90                         // Last sample; cannot combine.
91                         continue;
92                 }
93                 assert(num_samples_saved <= max_samples_saved);
94                 if (num_samples_saved == max_samples_saved) {
95                         // We could maybe save more here, but other rows can't, so don't bother.
96                         continue;
97                 }
98
99                 float w1 = src[i].weight;
100                 float w2 = src[i + 1].weight;
101                 if (w1 * w2 < 0.0f) {
102                         // Differing signs; cannot combine.
103                         continue;
104                 }
105
106                 float pos1 = src[i].pos;
107                 float pos2 = src[i + 1].pos;
108                 assert(pos2 > pos1);
109
110                 fp16_int_t pos, total_weight;
111                 float sum_sq_error;
112                 combine_two_samples(w1, w2, pos1, pos2, src_size, &pos, &total_weight, &sum_sq_error);
113
114                 // If the interpolation error is larger than that of about sqrt(2) of
115                 // a level at 8-bit precision, don't combine. (You'd think 1.0 was enough,
116                 // but since the artifacts are not really random, they can get quite
117                 // visible. On the other hand, going to 0.25f, I can see no change at
118                 // all with 8-bit output, so it would not seem to be worth it.)
119                 if (sum_sq_error > 0.5f / (255.0f * 255.0f)) {
120                         continue;
121                 }
122
123                 // OK, we can combine this and the next sample.
124                 if (dst != NULL) {
125                         dst[j].weight = total_weight;
126                         dst[j].pos = pos;
127                 }
128
129                 ++i;  // Skip the next sample.
130                 ++num_samples_saved;
131         }
132         return num_samples_saved;
133 }
134
135 // Normalize so that the sum becomes one. Note that we do it twice;
136 // this sometimes helps a tiny little bit when we have many samples.
137 template<class T>
138 void normalize_sum(Tap<T>* vals, unsigned num)
139 {
140         for (int normalize_pass = 0; normalize_pass < 2; ++normalize_pass) {
141                 double sum = 0.0;
142                 for (unsigned i = 0; i < num; ++i) {
143                         sum += to_fp64(vals[i].weight);
144                 }
145                 for (unsigned i = 0; i < num; ++i) {
146                         vals[i].weight = from_fp64<T>(to_fp64(vals[i].weight) / sum);
147                 }
148         }
149 }
150
151 // Make use of the bilinear filtering in the GPU to reduce the number of samples
152 // we need to make. This is a bit more complex than BlurEffect since we cannot combine
153 // two neighboring samples if their weights have differing signs, so we first need to
154 // figure out the maximum number of samples. Then, we downconvert all the weights to
155 // that number -- we could have gone for a variable-length system, but this is simpler,
156 // and the gains would probably be offset by the extra cost of checking when to stop.
157 //
158 // The greedy strategy for combining samples is optimal.
159 template<class DestFloat>
160 unsigned combine_many_samples(const Tap<float> *weights, unsigned src_size, unsigned src_samples, unsigned dst_samples, Tap<DestFloat> **bilinear_weights)
161 {
162         int src_bilinear_samples = 0;
163         for (unsigned y = 0; y < dst_samples; ++y) {
164                 unsigned num_samples_saved = combine_samples<DestFloat>(weights + y * src_samples, NULL, src_size, src_samples, UINT_MAX);
165                 src_bilinear_samples = max<int>(src_bilinear_samples, src_samples - num_samples_saved);
166         }
167
168         // Now that we know the right width, actually combine the samples.
169         *bilinear_weights = new Tap<DestFloat>[dst_samples * src_bilinear_samples];
170         for (unsigned y = 0; y < dst_samples; ++y) {
171                 Tap<DestFloat> *bilinear_weights_ptr = *bilinear_weights + y * src_bilinear_samples;
172                 unsigned num_samples_saved = combine_samples(
173                         weights + y * src_samples,
174                         bilinear_weights_ptr,
175                         src_size,
176                         src_samples,
177                         src_samples - src_bilinear_samples);
178                 assert(int(src_samples) - int(num_samples_saved) == src_bilinear_samples);
179                 normalize_sum(bilinear_weights_ptr, src_bilinear_samples);
180         }
181         return src_bilinear_samples;
182 }
183
184 // Compute the sum of squared errors between the ideal weights (which are
185 // assumed to fall exactly on pixel centers) and the weights that result
186 // from sampling at <bilinear_weights>. The primary reason for the difference
187 // is inaccuracy in the sampling positions, both due to limited precision
188 // in storing them (already inherent in sending them in as fp16_int_t)
189 // and in subtexel sampling precision (which we calculate in this function).
190 template<class T>
191 double compute_sum_sq_error(const Tap<float>* weights, unsigned num_weights,
192                             const Tap<T>* bilinear_weights, unsigned num_bilinear_weights,
193                             unsigned size)
194 {
195         // Find the effective range of the bilinear-optimized kernel.
196         // Due to rounding of the positions, this is not necessarily the same
197         // as the intended range (ie., the range of the original weights).
198         int lower_pos = int(floor(to_fp64(bilinear_weights[0].pos) * size - 0.5));
199         int upper_pos = int(ceil(to_fp64(bilinear_weights[num_bilinear_weights - 1].pos) * size - 0.5)) + 2;
200         lower_pos = min<int>(lower_pos, lrintf(weights[0].pos * size - 0.5));
201         upper_pos = max<int>(upper_pos, lrintf(weights[num_weights - 1].pos * size - 0.5) + 1);
202
203         float* effective_weights = new float[upper_pos - lower_pos];
204         for (int i = 0; i < upper_pos - lower_pos; ++i) {
205                 effective_weights[i] = 0.0f;
206         }
207
208         // Now find the effective weights that result from this sampling.
209         for (unsigned i = 0; i < num_bilinear_weights; ++i) {
210                 const float pixel_pos = to_fp64(bilinear_weights[i].pos) * size - 0.5f;
211                 const int x0 = int(floor(pixel_pos)) - lower_pos;
212                 const int x1 = x0 + 1;
213                 const float f = lrintf((pixel_pos - (x0 + lower_pos)) / movit_texel_subpixel_precision) * movit_texel_subpixel_precision;
214
215                 assert(x0 >= 0);
216                 assert(x1 >= 0);
217                 assert(x0 < upper_pos - lower_pos);
218                 assert(x1 < upper_pos - lower_pos);
219
220                 effective_weights[x0] += to_fp64(bilinear_weights[i].weight) * (1.0 - f);
221                 effective_weights[x1] += to_fp64(bilinear_weights[i].weight) * f;
222         }
223
224         // Subtract the desired weights to get the error.
225         for (unsigned i = 0; i < num_weights; ++i) {
226                 const int x = lrintf(weights[i].pos * size - 0.5f) - lower_pos;
227                 assert(x >= 0);
228                 assert(x < upper_pos - lower_pos);
229
230                 effective_weights[x] -= weights[i].weight;
231         }
232
233         double sum_sq_error = 0.0;
234         for (unsigned i = 0; i < num_weights; ++i) {
235                 sum_sq_error += effective_weights[i] * effective_weights[i];
236         }
237
238         delete[] effective_weights;
239         return sum_sq_error;
240 }
241
242 }  // namespace
243
244 ResampleEffect::ResampleEffect()
245         : input_width(1280),
246           input_height(720),
247           offset_x(0.0f), offset_y(0.0f),
248           zoom_x(1.0f), zoom_y(1.0f),
249           zoom_center_x(0.5f), zoom_center_y(0.5f)
250 {
251         register_int("width", &output_width);
252         register_int("height", &output_height);
253
254         // The first blur pass will forward resolution information to us.
255         hpass = new SingleResamplePassEffect(this);
256         CHECK(hpass->set_int("direction", SingleResamplePassEffect::HORIZONTAL));
257         vpass = new SingleResamplePassEffect(NULL);
258         CHECK(vpass->set_int("direction", SingleResamplePassEffect::VERTICAL));
259
260         update_size();
261 }
262
263 void ResampleEffect::rewrite_graph(EffectChain *graph, Node *self)
264 {
265         Node *hpass_node = graph->add_node(hpass);
266         Node *vpass_node = graph->add_node(vpass);
267         graph->connect_nodes(hpass_node, vpass_node);
268         graph->replace_receiver(self, hpass_node);
269         graph->replace_sender(self, vpass_node);
270         self->disabled = true;
271
272
273 // We get this information forwarded from the first blur pass,
274 // since we are not part of the chain ourselves.
275 void ResampleEffect::inform_input_size(unsigned input_num, unsigned width, unsigned height)
276 {
277         assert(input_num == 0);
278         assert(width != 0);
279         assert(height != 0);
280         input_width = width;
281         input_height = height;
282         update_size();
283 }
284
285 void ResampleEffect::update_size()
286 {
287         bool ok = true;
288         ok |= hpass->set_int("input_width", input_width);
289         ok |= hpass->set_int("input_height", input_height);
290         ok |= hpass->set_int("output_width", output_width);
291         ok |= hpass->set_int("output_height", input_height);
292
293         ok |= vpass->set_int("input_width", output_width);
294         ok |= vpass->set_int("input_height", input_height);
295         ok |= vpass->set_int("output_width", output_width);
296         ok |= vpass->set_int("output_height", output_height);
297
298         assert(ok);
299
300         // The offset added due to zoom may have changed with the size.
301         update_offset_and_zoom();
302 }
303
304 void ResampleEffect::update_offset_and_zoom()
305 {
306         bool ok = true;
307
308         // Zoom from the right origin. (zoom_center is given in normalized coordinates,
309         // i.e. 0..1.)
310         float extra_offset_x = zoom_center_x * (1.0f - 1.0f / zoom_x) * input_width;
311         float extra_offset_y = (1.0f - zoom_center_y) * (1.0f - 1.0f / zoom_y) * input_height;
312
313         ok |= hpass->set_float("offset", extra_offset_x + offset_x);
314         ok |= vpass->set_float("offset", extra_offset_y - offset_y);  // Compensate for the bottom-left origin.
315         ok |= hpass->set_float("zoom", zoom_x);
316         ok |= vpass->set_float("zoom", zoom_y);
317
318         assert(ok);
319 }
320
321 bool ResampleEffect::set_float(const string &key, float value) {
322         if (key == "width") {
323                 output_width = value;
324                 update_size();
325                 return true;
326         }
327         if (key == "height") {
328                 output_height = value;
329                 update_size();
330                 return true;
331         }
332         if (key == "top") {
333                 offset_y = value;
334                 update_offset_and_zoom();
335                 return true;
336         }
337         if (key == "left") {
338                 offset_x = value;
339                 update_offset_and_zoom();
340                 return true;
341         }
342         if (key == "zoom_x") {
343                 if (value <= 0.0f) {
344                         return false;
345                 }
346                 zoom_x = value;
347                 update_offset_and_zoom();
348                 return true;
349         }
350         if (key == "zoom_y") {
351                 if (value <= 0.0f) {
352                         return false;
353                 }
354                 zoom_y = value;
355                 update_offset_and_zoom();
356                 return true;
357         }
358         if (key == "zoom_center_x") {
359                 zoom_center_x = value;
360                 update_offset_and_zoom();
361                 return true;
362         }
363         if (key == "zoom_center_y") {
364                 zoom_center_y = value;
365                 update_offset_and_zoom();
366                 return true;
367         }
368         return false;
369 }
370
371 SingleResamplePassEffect::SingleResamplePassEffect(ResampleEffect *parent)
372         : parent(parent),
373           direction(HORIZONTAL),
374           input_width(1280),
375           input_height(720),
376           offset(0.0),
377           zoom(1.0),
378           last_input_width(-1),
379           last_input_height(-1),
380           last_output_width(-1),
381           last_output_height(-1),
382           last_offset(0.0 / 0.0),  // NaN.
383           last_zoom(0.0 / 0.0)  // NaN.
384 {
385         register_int("direction", (int *)&direction);
386         register_int("input_width", &input_width);
387         register_int("input_height", &input_height);
388         register_int("output_width", &output_width);
389         register_int("output_height", &output_height);
390         register_float("offset", &offset);
391         register_float("zoom", &zoom);
392
393         glGenTextures(1, &texnum);
394 }
395
396 SingleResamplePassEffect::~SingleResamplePassEffect()
397 {
398         glDeleteTextures(1, &texnum);
399 }
400
401 string SingleResamplePassEffect::output_fragment_shader()
402 {
403         char buf[256];
404         sprintf(buf, "#define DIRECTION_VERTICAL %d\n", (direction == VERTICAL));
405         return buf + read_file("resample_effect.frag");
406 }
407
408 // Using vertical scaling as an example:
409 //
410 // Generally out[y] = w0 * in[yi] + w1 * in[yi + 1] + w2 * in[yi + 2] + ...
411 //
412 // Obviously, yi will depend on y (in a not-quite-linear way), but so will
413 // the weights w0, w1, w2, etc.. The easiest way of doing this is to encode,
414 // for each sample, the weight and the yi value, e.g. <yi, w0>, <yi + 1, w1>,
415 // and so on. For each y, we encode these along the x-axis (since that is spare),
416 // so out[0] will read from parameters <x,y> = <0,0>, <1,0>, <2,0> and so on.
417 //
418 // For horizontal scaling, we fill in the exact same texture;
419 // the shader just interprets it differently.
420 void SingleResamplePassEffect::update_texture(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
421 {
422         unsigned src_size, dst_size;
423         if (direction == SingleResamplePassEffect::HORIZONTAL) {
424                 assert(input_height == output_height);
425                 src_size = input_width;
426                 dst_size = output_width;
427         } else if (direction == SingleResamplePassEffect::VERTICAL) {
428                 assert(input_width == output_width);
429                 src_size = input_height;
430                 dst_size = output_height;
431         } else {
432                 assert(false);
433         }
434
435         // For many resamplings (e.g. 640 -> 1280), we will end up with the same
436         // set of samples over and over again in a loop. Thus, we can compute only
437         // the first such loop, and then ask the card to repeat the texture for us.
438         // This is both easier on the texture cache and lowers our CPU cost for
439         // generating the kernel somewhat.
440         float scaling_factor;
441         if (fabs(zoom - 1.0f) < 1e-6) {
442                 num_loops = gcd(src_size, dst_size);
443                 scaling_factor = float(dst_size) / float(src_size);
444         } else {
445                 // If zooming is enabled (ie., zoom != 1), we turn off the looping.
446                 // We _could_ perhaps do it for rational zoom levels (especially
447                 // things like 2:1), but it doesn't seem to be worth it, given that
448                 // the most common use case would seem to be varying the zoom
449                 // from frame to frame.
450                 num_loops = 1;
451                 scaling_factor = zoom * float(dst_size) / float(src_size);
452         }
453         slice_height = 1.0f / num_loops;
454         unsigned dst_samples = dst_size / num_loops;
455
456         // Sample the kernel in the right place. A diagram with a triangular kernel
457         // (corresponding to linear filtering, and obviously with radius 1)
458         // for easier ASCII art drawing:
459         //
460         //                *
461         //               / \                      |
462         //              /   \                     |
463         //             /     \                    |
464         //    x---x---x   x   x---x---x---x
465         //
466         // Scaling up (in this case, 2x) means sampling more densely:
467         //
468         //                *
469         //               / \                      |
470         //              /   \                     |
471         //             /     \                    |
472         //   x-x-x-x-x-x x x x-x-x-x-x-x-x-x
473         //
474         // When scaling up, any destination pixel will only be influenced by a few
475         // (in this case, two) neighboring pixels, and more importantly, the number
476         // will not be influenced by the scaling factor. (Note, however, that the
477         // pixel centers have moved, due to OpenGL's center-pixel convention.)
478         // The only thing that changes is the weights themselves, as the sampling
479         // points are at different distances from the original pixels.
480         //
481         // Scaling down is a different story:
482         //
483         //                *
484         //               / \                      |
485         //              /   \                     |
486         //             /     \                    |
487         //    --x------ x     --x-------x--
488         //
489         // Again, the pixel centers have moved in a maybe unintuitive fashion,
490         // although when you consider that there are multiple source pixels around,
491         // it's not so bad as at first look:
492         //
493         //            *   *   *   *
494         //           / \ / \ / \ / \              |
495         //          /   X   X   X   \             |
496         //         /   / \ / \ / \   \            |
497         //    --x-------x-------x-------x--
498         //
499         // As you can see, the new pixels become averages of the two neighboring old
500         // ones (the situation for Lanczos is of course more complex).
501         //
502         // Anyhow, in this case we clearly need to look at more source pixels
503         // to compute the destination pixel, and how many depend on the scaling factor.
504         // Thus, the kernel width will vary with how much we scale.
505         float radius_scaling_factor = min(scaling_factor, 1.0f);
506         int int_radius = lrintf(LANCZOS_RADIUS / radius_scaling_factor);
507         int src_samples = int_radius * 2 + 1;
508         Tap<float> *weights = new Tap<float>[dst_samples * src_samples];
509         float subpixel_offset = offset - lrintf(offset);  // The part not covered by whole_pixel_offset.
510         assert(subpixel_offset >= -0.5f && subpixel_offset <= 0.5f);
511         for (unsigned y = 0; y < dst_samples; ++y) {
512                 // Find the point around which we want to sample the source image,
513                 // compensating for differing pixel centers as the scale changes.
514                 float center_src_y = (y + 0.5f) / scaling_factor - 0.5f;
515                 int base_src_y = lrintf(center_src_y);
516
517                 // Now sample <int_radius> pixels on each side around that point.
518                 for (int i = 0; i < src_samples; ++i) {
519                         int src_y = base_src_y + i - int_radius;
520                         float weight = lanczos_weight(radius_scaling_factor * (src_y - center_src_y - subpixel_offset), LANCZOS_RADIUS);
521                         weights[y * src_samples + i].weight = weight * radius_scaling_factor;
522                         weights[y * src_samples + i].pos = (src_y + 0.5) / float(src_size);
523                 }
524         }
525
526         // Now make use of the bilinear filtering in the GPU to reduce the number of samples
527         // we need to make. Try fp16 first; if it's not accurate enough, we go to fp32.
528         // Our tolerance level for total error is a bit higher than the one for invididual
529         // samples, since one would assume overall errors in the shape don't matter as much.
530         const float max_error = 2.0f / (255.0f * 255.0f);
531         Tap<fp16_int_t> *bilinear_weights_fp16;
532         src_bilinear_samples = combine_many_samples(weights, src_size, src_samples, dst_samples, &bilinear_weights_fp16);
533         Tap<float> *bilinear_weights_fp32 = NULL;
534         bool fallback_to_fp32 = false;
535         double max_sum_sq_error_fp16 = 0.0;
536         for (unsigned y = 0; y < dst_samples; ++y) {
537                 double sum_sq_error_fp16 = compute_sum_sq_error(
538                         weights + y * src_samples, src_samples,
539                         bilinear_weights_fp16 + y * src_bilinear_samples, src_bilinear_samples,
540                         src_size);
541                 max_sum_sq_error_fp16 = std::max(max_sum_sq_error_fp16, sum_sq_error_fp16);
542                 if (max_sum_sq_error_fp16 > max_error) {
543                         break;
544                 }
545         }
546
547         if (max_sum_sq_error_fp16 > max_error) {
548                 fallback_to_fp32 = true;
549                 src_bilinear_samples = combine_many_samples(weights, src_size, src_samples, dst_samples, &bilinear_weights_fp32);
550         }
551
552         // Encode as a two-component texture. Note the GL_REPEAT.
553         glActiveTexture(GL_TEXTURE0 + *sampler_num);
554         check_error();
555         glBindTexture(GL_TEXTURE_2D, texnum);
556         check_error();
557         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
558         check_error();
559         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
560         check_error();
561         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
562         check_error();
563         if (fallback_to_fp32) {
564                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG32F, src_bilinear_samples, dst_samples, 0, GL_RG, GL_FLOAT, bilinear_weights_fp32);
565         } else {
566                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG16F, src_bilinear_samples, dst_samples, 0, GL_RG, GL_HALF_FLOAT, bilinear_weights_fp16);
567         }
568         check_error();
569
570         delete[] weights;
571         delete[] bilinear_weights_fp16;
572         delete[] bilinear_weights_fp32;
573 }
574
575 void SingleResamplePassEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
576 {
577         Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
578
579         assert(input_width > 0);
580         assert(input_height > 0);
581         assert(output_width > 0);
582         assert(output_height > 0);
583
584         if (input_width != last_input_width ||
585             input_height != last_input_height ||
586             output_width != last_output_width ||
587             output_height != last_output_height ||
588             offset != last_offset ||
589             zoom != last_zoom) {
590                 update_texture(glsl_program_num, prefix, sampler_num);
591                 last_input_width = input_width;
592                 last_input_height = input_height;
593                 last_output_width = output_width;
594                 last_output_height = output_height;
595                 last_offset = offset;
596                 last_zoom = zoom;
597         }
598
599         glActiveTexture(GL_TEXTURE0 + *sampler_num);
600         check_error();
601         glBindTexture(GL_TEXTURE_2D, texnum);
602         check_error();
603
604         set_uniform_int(glsl_program_num, prefix, "sample_tex", *sampler_num);
605         ++*sampler_num;
606         set_uniform_int(glsl_program_num, prefix, "num_samples", src_bilinear_samples);
607         set_uniform_float(glsl_program_num, prefix, "num_loops", num_loops);
608         set_uniform_float(glsl_program_num, prefix, "slice_height", slice_height);
609
610         // Instructions for how to convert integer sample numbers to positions in the weight texture.
611         set_uniform_float(glsl_program_num, prefix, "sample_x_scale", 1.0f / src_bilinear_samples);
612         set_uniform_float(glsl_program_num, prefix, "sample_x_offset", 0.5f / src_bilinear_samples);
613
614         float whole_pixel_offset;
615         if (direction == SingleResamplePassEffect::VERTICAL) {
616                 whole_pixel_offset = lrintf(offset) / float(input_height);
617         } else {
618                 whole_pixel_offset = lrintf(offset) / float(input_width);
619         }
620         set_uniform_float(glsl_program_num, prefix, "whole_pixel_offset", whole_pixel_offset);
621
622         // We specifically do not want mipmaps on the input texture;
623         // they break minification.
624         Node *self = chain->find_node_for_effect(this);
625         glActiveTexture(chain->get_input_sampler(self, 0));
626         check_error();
627         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
628         check_error();
629 }
630
631 }  // namespace movit