]> git.sesse.net Git - movit/blob - resample_effect.cpp
Fix a bug where combined fp16 weights would be horribly wrong.
[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, float num_subtexels, float inv_num_subtexels, 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                 DestFloat pos, total_weight;
111                 float sum_sq_error;
112                 combine_two_samples(w1, w2, pos1, pos2, num_subtexels, inv_num_subtexels, &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                 double inv_sum = 1.0 / sum;
146                 for (unsigned i = 0; i < num; ++i) {
147                         vals[i].weight = from_fp64<T>(to_fp64(vals[i].weight) * inv_sum);
148                 }
149         }
150 }
151
152 // Make use of the bilinear filtering in the GPU to reduce the number of samples
153 // we need to make. This is a bit more complex than BlurEffect since we cannot combine
154 // two neighboring samples if their weights have differing signs, so we first need to
155 // figure out the maximum number of samples. Then, we downconvert all the weights to
156 // that number -- we could have gone for a variable-length system, but this is simpler,
157 // and the gains would probably be offset by the extra cost of checking when to stop.
158 //
159 // The greedy strategy for combining samples is optimal.
160 template<class DestFloat>
161 unsigned combine_many_samples(const Tap<float> *weights, unsigned src_size, unsigned src_samples, unsigned dst_samples, Tap<DestFloat> **bilinear_weights)
162 {
163         float num_subtexels = src_size / movit_texel_subpixel_precision;
164         float inv_num_subtexels = movit_texel_subpixel_precision / src_size;
165
166         unsigned max_samples_saved = UINT_MAX;
167         for (unsigned y = 0; y < dst_samples && max_samples_saved > 0; ++y) {
168                 unsigned num_samples_saved = combine_samples<DestFloat>(weights + y * src_samples, NULL, num_subtexels, inv_num_subtexels, src_samples, max_samples_saved);
169                 max_samples_saved = min(max_samples_saved, num_samples_saved);
170         }
171
172         // Now that we know the right width, actually combine the samples.
173         unsigned src_bilinear_samples = src_samples - max_samples_saved;
174         *bilinear_weights = new Tap<DestFloat>[dst_samples * src_bilinear_samples];
175         for (unsigned y = 0; y < dst_samples; ++y) {
176                 Tap<DestFloat> *bilinear_weights_ptr = *bilinear_weights + y * src_bilinear_samples;
177                 unsigned num_samples_saved = combine_samples(
178                         weights + y * src_samples,
179                         bilinear_weights_ptr,
180                         num_subtexels,
181                         inv_num_subtexels,
182                         src_samples,
183                         max_samples_saved);
184                 assert(num_samples_saved == max_samples_saved);
185                 normalize_sum(bilinear_weights_ptr, src_bilinear_samples);
186         }
187         return src_bilinear_samples;
188 }
189
190 // Compute the sum of squared errors between the ideal weights (which are
191 // assumed to fall exactly on pixel centers) and the weights that result
192 // from sampling at <bilinear_weights>. The primary reason for the difference
193 // is inaccuracy in the sampling positions, both due to limited precision
194 // in storing them (already inherent in sending them in as fp16_int_t)
195 // and in subtexel sampling precision (which we calculate in this function).
196 template<class T>
197 double compute_sum_sq_error(const Tap<float>* weights, unsigned num_weights,
198                             const Tap<T>* bilinear_weights, unsigned num_bilinear_weights,
199                             unsigned size)
200 {
201         // Find the effective range of the bilinear-optimized kernel.
202         // Due to rounding of the positions, this is not necessarily the same
203         // as the intended range (ie., the range of the original weights).
204         int lower_pos = int(floor(to_fp64(bilinear_weights[0].pos) * size - 0.5));
205         int upper_pos = int(ceil(to_fp64(bilinear_weights[num_bilinear_weights - 1].pos) * size - 0.5)) + 2;
206         lower_pos = min<int>(lower_pos, lrintf(weights[0].pos * size - 0.5));
207         upper_pos = max<int>(upper_pos, lrintf(weights[num_weights - 1].pos * size - 0.5) + 1);
208
209         float* effective_weights = new float[upper_pos - lower_pos];
210         for (int i = 0; i < upper_pos - lower_pos; ++i) {
211                 effective_weights[i] = 0.0f;
212         }
213
214         // Now find the effective weights that result from this sampling.
215         for (unsigned i = 0; i < num_bilinear_weights; ++i) {
216                 const float pixel_pos = to_fp64(bilinear_weights[i].pos) * size - 0.5f;
217                 const int x0 = int(floor(pixel_pos)) - lower_pos;
218                 const int x1 = x0 + 1;
219                 const float f = lrintf((pixel_pos - (x0 + lower_pos)) / movit_texel_subpixel_precision) * movit_texel_subpixel_precision;
220
221                 assert(x0 >= 0);
222                 assert(x1 >= 0);
223                 assert(x0 < upper_pos - lower_pos);
224                 assert(x1 < upper_pos - lower_pos);
225
226                 effective_weights[x0] += to_fp64(bilinear_weights[i].weight) * (1.0 - f);
227                 effective_weights[x1] += to_fp64(bilinear_weights[i].weight) * f;
228         }
229
230         // Subtract the desired weights to get the error.
231         for (unsigned i = 0; i < num_weights; ++i) {
232                 const int x = lrintf(weights[i].pos * size - 0.5f) - lower_pos;
233                 assert(x >= 0);
234                 assert(x < upper_pos - lower_pos);
235
236                 effective_weights[x] -= weights[i].weight;
237         }
238
239         double sum_sq_error = 0.0;
240         for (unsigned i = 0; i < num_weights; ++i) {
241                 sum_sq_error += effective_weights[i] * effective_weights[i];
242         }
243
244         delete[] effective_weights;
245         return sum_sq_error;
246 }
247
248 }  // namespace
249
250 ResampleEffect::ResampleEffect()
251         : input_width(1280),
252           input_height(720),
253           offset_x(0.0f), offset_y(0.0f),
254           zoom_x(1.0f), zoom_y(1.0f),
255           zoom_center_x(0.5f), zoom_center_y(0.5f)
256 {
257         register_int("width", &output_width);
258         register_int("height", &output_height);
259
260         // The first blur pass will forward resolution information to us.
261         hpass = new SingleResamplePassEffect(this);
262         CHECK(hpass->set_int("direction", SingleResamplePassEffect::HORIZONTAL));
263         vpass = new SingleResamplePassEffect(NULL);
264         CHECK(vpass->set_int("direction", SingleResamplePassEffect::VERTICAL));
265
266         update_size();
267 }
268
269 void ResampleEffect::rewrite_graph(EffectChain *graph, Node *self)
270 {
271         Node *hpass_node = graph->add_node(hpass);
272         Node *vpass_node = graph->add_node(vpass);
273         graph->connect_nodes(hpass_node, vpass_node);
274         graph->replace_receiver(self, hpass_node);
275         graph->replace_sender(self, vpass_node);
276         self->disabled = true;
277
278
279 // We get this information forwarded from the first blur pass,
280 // since we are not part of the chain ourselves.
281 void ResampleEffect::inform_input_size(unsigned input_num, unsigned width, unsigned height)
282 {
283         assert(input_num == 0);
284         assert(width != 0);
285         assert(height != 0);
286         input_width = width;
287         input_height = height;
288         update_size();
289 }
290
291 void ResampleEffect::update_size()
292 {
293         bool ok = true;
294         ok |= hpass->set_int("input_width", input_width);
295         ok |= hpass->set_int("input_height", input_height);
296         ok |= hpass->set_int("output_width", output_width);
297         ok |= hpass->set_int("output_height", input_height);
298
299         ok |= vpass->set_int("input_width", output_width);
300         ok |= vpass->set_int("input_height", input_height);
301         ok |= vpass->set_int("output_width", output_width);
302         ok |= vpass->set_int("output_height", output_height);
303
304         assert(ok);
305
306         // The offset added due to zoom may have changed with the size.
307         update_offset_and_zoom();
308 }
309
310 void ResampleEffect::update_offset_and_zoom()
311 {
312         bool ok = true;
313
314         // Zoom from the right origin. (zoom_center is given in normalized coordinates,
315         // i.e. 0..1.)
316         float extra_offset_x = zoom_center_x * (1.0f - 1.0f / zoom_x) * input_width;
317         float extra_offset_y = (1.0f - zoom_center_y) * (1.0f - 1.0f / zoom_y) * input_height;
318
319         ok |= hpass->set_float("offset", extra_offset_x + offset_x);
320         ok |= vpass->set_float("offset", extra_offset_y - offset_y);  // Compensate for the bottom-left origin.
321         ok |= hpass->set_float("zoom", zoom_x);
322         ok |= vpass->set_float("zoom", zoom_y);
323
324         assert(ok);
325 }
326
327 bool ResampleEffect::set_float(const string &key, float value) {
328         if (key == "width") {
329                 output_width = value;
330                 update_size();
331                 return true;
332         }
333         if (key == "height") {
334                 output_height = value;
335                 update_size();
336                 return true;
337         }
338         if (key == "top") {
339                 offset_y = value;
340                 update_offset_and_zoom();
341                 return true;
342         }
343         if (key == "left") {
344                 offset_x = value;
345                 update_offset_and_zoom();
346                 return true;
347         }
348         if (key == "zoom_x") {
349                 if (value <= 0.0f) {
350                         return false;
351                 }
352                 zoom_x = value;
353                 update_offset_and_zoom();
354                 return true;
355         }
356         if (key == "zoom_y") {
357                 if (value <= 0.0f) {
358                         return false;
359                 }
360                 zoom_y = value;
361                 update_offset_and_zoom();
362                 return true;
363         }
364         if (key == "zoom_center_x") {
365                 zoom_center_x = value;
366                 update_offset_and_zoom();
367                 return true;
368         }
369         if (key == "zoom_center_y") {
370                 zoom_center_y = value;
371                 update_offset_and_zoom();
372                 return true;
373         }
374         return false;
375 }
376
377 SingleResamplePassEffect::SingleResamplePassEffect(ResampleEffect *parent)
378         : parent(parent),
379           direction(HORIZONTAL),
380           input_width(1280),
381           input_height(720),
382           offset(0.0),
383           zoom(1.0),
384           last_input_width(-1),
385           last_input_height(-1),
386           last_output_width(-1),
387           last_output_height(-1),
388           last_offset(0.0 / 0.0),  // NaN.
389           last_zoom(0.0 / 0.0),  // NaN.
390           last_texture_width(-1), last_texture_height(-1)
391 {
392         register_int("direction", (int *)&direction);
393         register_int("input_width", &input_width);
394         register_int("input_height", &input_height);
395         register_int("output_width", &output_width);
396         register_int("output_height", &output_height);
397         register_float("offset", &offset);
398         register_float("zoom", &zoom);
399         register_uniform_sampler2d("sample_tex", &uniform_sample_tex);
400         register_uniform_int("num_samples", &uniform_num_samples);  // FIXME: What about GLSL pre-1.30?
401         register_uniform_float("num_loops", &uniform_num_loops);
402         register_uniform_float("slice_height", &uniform_slice_height);
403         register_uniform_float("sample_x_scale", &uniform_sample_x_scale);
404         register_uniform_float("sample_x_offset", &uniform_sample_x_offset);
405         register_uniform_float("whole_pixel_offset", &uniform_whole_pixel_offset);
406
407         glGenTextures(1, &texnum);
408 }
409
410 SingleResamplePassEffect::~SingleResamplePassEffect()
411 {
412         glDeleteTextures(1, &texnum);
413 }
414
415 string SingleResamplePassEffect::output_fragment_shader()
416 {
417         char buf[256];
418         sprintf(buf, "#define DIRECTION_VERTICAL %d\n", (direction == VERTICAL));
419         return buf + read_file("resample_effect.frag");
420 }
421
422 // Using vertical scaling as an example:
423 //
424 // Generally out[y] = w0 * in[yi] + w1 * in[yi + 1] + w2 * in[yi + 2] + ...
425 //
426 // Obviously, yi will depend on y (in a not-quite-linear way), but so will
427 // the weights w0, w1, w2, etc.. The easiest way of doing this is to encode,
428 // for each sample, the weight and the yi value, e.g. <yi, w0>, <yi + 1, w1>,
429 // and so on. For each y, we encode these along the x-axis (since that is spare),
430 // so out[0] will read from parameters <x,y> = <0,0>, <1,0>, <2,0> and so on.
431 //
432 // For horizontal scaling, we fill in the exact same texture;
433 // the shader just interprets it differently.
434 void SingleResamplePassEffect::update_texture(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
435 {
436         unsigned src_size, dst_size;
437         if (direction == SingleResamplePassEffect::HORIZONTAL) {
438                 assert(input_height == output_height);
439                 src_size = input_width;
440                 dst_size = output_width;
441         } else if (direction == SingleResamplePassEffect::VERTICAL) {
442                 assert(input_width == output_width);
443                 src_size = input_height;
444                 dst_size = output_height;
445         } else {
446                 assert(false);
447         }
448
449         // For many resamplings (e.g. 640 -> 1280), we will end up with the same
450         // set of samples over and over again in a loop. Thus, we can compute only
451         // the first such loop, and then ask the card to repeat the texture for us.
452         // This is both easier on the texture cache and lowers our CPU cost for
453         // generating the kernel somewhat.
454         float scaling_factor;
455         if (fabs(zoom - 1.0f) < 1e-6) {
456                 num_loops = gcd(src_size, dst_size);
457                 scaling_factor = float(dst_size) / float(src_size);
458         } else {
459                 // If zooming is enabled (ie., zoom != 1), we turn off the looping.
460                 // We _could_ perhaps do it for rational zoom levels (especially
461                 // things like 2:1), but it doesn't seem to be worth it, given that
462                 // the most common use case would seem to be varying the zoom
463                 // from frame to frame.
464                 num_loops = 1;
465                 scaling_factor = zoom * float(dst_size) / float(src_size);
466         }
467         slice_height = 1.0f / num_loops;
468         unsigned dst_samples = dst_size / num_loops;
469
470         // Sample the kernel in the right place. A diagram with a triangular kernel
471         // (corresponding to linear filtering, and obviously with radius 1)
472         // for easier ASCII art drawing:
473         //
474         //                *
475         //               / \                      |
476         //              /   \                     |
477         //             /     \                    |
478         //    x---x---x   x   x---x---x---x
479         //
480         // Scaling up (in this case, 2x) means sampling more densely:
481         //
482         //                *
483         //               / \                      |
484         //              /   \                     |
485         //             /     \                    |
486         //   x-x-x-x-x-x x x x-x-x-x-x-x-x-x
487         //
488         // When scaling up, any destination pixel will only be influenced by a few
489         // (in this case, two) neighboring pixels, and more importantly, the number
490         // will not be influenced by the scaling factor. (Note, however, that the
491         // pixel centers have moved, due to OpenGL's center-pixel convention.)
492         // The only thing that changes is the weights themselves, as the sampling
493         // points are at different distances from the original pixels.
494         //
495         // Scaling down is a different story:
496         //
497         //                *
498         //               / \                      |
499         //              /   \                     |
500         //             /     \                    |
501         //    --x------ x     --x-------x--
502         //
503         // Again, the pixel centers have moved in a maybe unintuitive fashion,
504         // although when you consider that there are multiple source pixels around,
505         // it's not so bad as at first look:
506         //
507         //            *   *   *   *
508         //           / \ / \ / \ / \              |
509         //          /   X   X   X   \             |
510         //         /   / \ / \ / \   \            |
511         //    --x-------x-------x-------x--
512         //
513         // As you can see, the new pixels become averages of the two neighboring old
514         // ones (the situation for Lanczos is of course more complex).
515         //
516         // Anyhow, in this case we clearly need to look at more source pixels
517         // to compute the destination pixel, and how many depend on the scaling factor.
518         // Thus, the kernel width will vary with how much we scale.
519         float radius_scaling_factor = min(scaling_factor, 1.0f);
520         int int_radius = lrintf(LANCZOS_RADIUS / radius_scaling_factor);
521         int src_samples = int_radius * 2 + 1;
522         Tap<float> *weights = new Tap<float>[dst_samples * src_samples];
523         float subpixel_offset = offset - lrintf(offset);  // The part not covered by whole_pixel_offset.
524         assert(subpixel_offset >= -0.5f && subpixel_offset <= 0.5f);
525         for (unsigned y = 0; y < dst_samples; ++y) {
526                 // Find the point around which we want to sample the source image,
527                 // compensating for differing pixel centers as the scale changes.
528                 float center_src_y = (y + 0.5f) / scaling_factor - 0.5f;
529                 int base_src_y = lrintf(center_src_y);
530
531                 // Now sample <int_radius> pixels on each side around that point.
532                 for (int i = 0; i < src_samples; ++i) {
533                         int src_y = base_src_y + i - int_radius;
534                         float weight = lanczos_weight(radius_scaling_factor * (src_y - center_src_y - subpixel_offset), LANCZOS_RADIUS);
535                         weights[y * src_samples + i].weight = weight * radius_scaling_factor;
536                         weights[y * src_samples + i].pos = (src_y + 0.5) / float(src_size);
537                 }
538         }
539
540         // Now make use of the bilinear filtering in the GPU to reduce the number of samples
541         // we need to make. Try fp16 first; if it's not accurate enough, we go to fp32.
542         // Our tolerance level for total error is a bit higher than the one for invididual
543         // samples, since one would assume overall errors in the shape don't matter as much.
544         const float max_error = 2.0f / (255.0f * 255.0f);
545         Tap<fp16_int_t> *bilinear_weights_fp16;
546         src_bilinear_samples = combine_many_samples(weights, src_size, src_samples, dst_samples, &bilinear_weights_fp16);
547         Tap<float> *bilinear_weights_fp32 = NULL;
548         bool fallback_to_fp32 = false;
549         double max_sum_sq_error_fp16 = 0.0;
550         for (unsigned y = 0; y < dst_samples; ++y) {
551                 double sum_sq_error_fp16 = compute_sum_sq_error(
552                         weights + y * src_samples, src_samples,
553                         bilinear_weights_fp16 + y * src_bilinear_samples, src_bilinear_samples,
554                         src_size);
555                 max_sum_sq_error_fp16 = std::max(max_sum_sq_error_fp16, sum_sq_error_fp16);
556                 if (max_sum_sq_error_fp16 > max_error) {
557                         break;
558                 }
559         }
560
561         if (max_sum_sq_error_fp16 > max_error) {
562                 fallback_to_fp32 = true;
563                 src_bilinear_samples = combine_many_samples(weights, src_size, src_samples, dst_samples, &bilinear_weights_fp32);
564         }
565
566         // Encode as a two-component texture. Note the GL_REPEAT.
567         glActiveTexture(GL_TEXTURE0 + *sampler_num);
568         check_error();
569         glBindTexture(GL_TEXTURE_2D, texnum);
570         check_error();
571         if (last_texture_width == -1) {
572                 // Need to set this state the first time.
573                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
574                 check_error();
575                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
576                 check_error();
577                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
578                 check_error();
579         }
580
581         GLenum type, internal_format;
582         void *pixels;
583         if (fallback_to_fp32) {
584                 type = GL_FLOAT;
585                 internal_format = GL_RG32F;
586                 pixels = bilinear_weights_fp32;
587         } else {
588                 type = GL_HALF_FLOAT;
589                 internal_format = GL_RG16F;
590                 pixels = bilinear_weights_fp16;
591         }
592
593         if (int(src_bilinear_samples) == last_texture_width &&
594             int(dst_samples) == last_texture_height &&
595             internal_format == last_texture_internal_format) {
596                 // Texture dimensions and type are unchanged; it is more efficient
597                 // to just update it rather than making an entirely new texture.
598                 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, src_bilinear_samples, dst_samples, GL_RG, type, pixels);
599         } else {
600                 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, src_bilinear_samples, dst_samples, 0, GL_RG, type, pixels);
601                 last_texture_width = src_bilinear_samples;
602                 last_texture_height = dst_samples;
603                 last_texture_internal_format = internal_format;
604         }
605         check_error();
606
607         delete[] weights;
608         delete[] bilinear_weights_fp16;
609         delete[] bilinear_weights_fp32;
610 }
611
612 void SingleResamplePassEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
613 {
614         Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
615
616         assert(input_width > 0);
617         assert(input_height > 0);
618         assert(output_width > 0);
619         assert(output_height > 0);
620
621         if (input_width != last_input_width ||
622             input_height != last_input_height ||
623             output_width != last_output_width ||
624             output_height != last_output_height ||
625             offset != last_offset ||
626             zoom != last_zoom) {
627                 update_texture(glsl_program_num, prefix, sampler_num);
628                 last_input_width = input_width;
629                 last_input_height = input_height;
630                 last_output_width = output_width;
631                 last_output_height = output_height;
632                 last_offset = offset;
633                 last_zoom = zoom;
634         }
635
636         glActiveTexture(GL_TEXTURE0 + *sampler_num);
637         check_error();
638         glBindTexture(GL_TEXTURE_2D, texnum);
639         check_error();
640
641         uniform_sample_tex = *sampler_num;
642         ++*sampler_num;
643         uniform_num_samples = src_bilinear_samples;
644         uniform_num_loops = num_loops;
645         uniform_slice_height = slice_height;
646
647         // Instructions for how to convert integer sample numbers to positions in the weight texture.
648         uniform_sample_x_scale = 1.0f / src_bilinear_samples;
649         uniform_sample_x_offset = 0.5f / src_bilinear_samples;
650
651         if (direction == SingleResamplePassEffect::VERTICAL) {
652                 uniform_whole_pixel_offset = lrintf(offset) / float(input_height);
653         } else {
654                 uniform_whole_pixel_offset = lrintf(offset) / float(input_width);
655         }
656
657         // We specifically do not want mipmaps on the input texture;
658         // they break minification.
659         Node *self = chain->find_node_for_effect(this);
660         glActiveTexture(chain->get_input_sampler(self, 0));
661         check_error();
662         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
663         check_error();
664 }
665
666 }  // namespace movit