]> git.sesse.net Git - movit/blob - resample_effect.frag
Loosen up the 0.499 vs. 0.501 subpixel resample test.
[movit] / resample_effect.frag
1 // DIRECTION_VERTICAL will be #defined to 1 if we are scaling vertically,
2 // and 0 otherwise.
3
4 uniform sampler2D PREFIX(sample_tex);
5 uniform int PREFIX(num_samples);
6 uniform float PREFIX(num_loops);
7 uniform float PREFIX(sample_x_scale);
8 uniform float PREFIX(sample_x_offset);
9 uniform float PREFIX(slice_height);
10
11 // We put the fractional part of the offset (-0.5 to 0.5 pixels) in the weights
12 // because we have to (otherwise they'd do nothing). However, the support texture
13 // has limited numerical precision; we'd need as much of it as we can for
14 // getting the subpixel sampling right, and adding a large constant to each value
15 // will reduce the precision further. Thus, the non-fractional part of the offset
16 // is sent in through a uniform that we simply add in. (It should be said that
17 // for high values of (dst_size/num_loop), we're pretty much hosed anyway wrt.
18 // this accuracy.)
19 //
20 // Unfortunately, we cannot just do it at the beginning of the shader,
21 // since the texcoord value is used to index into the support texture,
22 // and if zoom != 1, the support texture will not wrap properly, causing
23 // us to read the wrong texels. (Also remember that whole_pixel_offset is
24 // measured in _input_ pixels and tc is in _output_ pixels, although we could
25 // compensate for that.) However, the shader should be mostly bandwidth bound
26 // and not ALU bound, so an extra add per sample shouldn't be too hopeless.
27 uniform float PREFIX(whole_pixel_offset);
28
29 // Sample a single weight. First fetch information about where to sample
30 // and the weight from sample_tex, and then read the pixel itself.
31 vec4 PREFIX(do_sample)(vec2 tc, int i)
32 {
33         vec2 sample_tc;
34         sample_tc.x = float(i) * PREFIX(sample_x_scale) + PREFIX(sample_x_offset);
35 #if DIRECTION_VERTICAL
36         sample_tc.y = tc.y * PREFIX(num_loops);
37 #else
38         sample_tc.y = tc.x * PREFIX(num_loops);
39 #endif
40         vec2 sample = tex2D(PREFIX(sample_tex), sample_tc).rg;
41
42 #if DIRECTION_VERTICAL
43         tc.y = sample.g + floor(sample_tc.y) * PREFIX(slice_height) + PREFIX(whole_pixel_offset);
44 #else
45         tc.x = sample.g + floor(sample_tc.y) * PREFIX(slice_height) + PREFIX(whole_pixel_offset);
46 #endif
47         return vec4(sample.r) * INPUT(tc);
48 }
49
50 vec4 FUNCNAME(vec2 tc) {
51         vec4 sum = PREFIX(do_sample)(tc, 0);
52         for (int i = 1; i < PREFIX(num_samples); ++i) {
53                 sum += PREFIX(do_sample)(tc, i);
54         }
55         return sum;
56 }
57
58 #undef DIRECTION_VERTICAL