]> git.sesse.net Git - movit/blob - resample_effect.frag
Fix a bug when scaling and doing offset at the same time. (At least one more remains.)
[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 and 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 the beginning of the shader.
17 uniform float PREFIX(whole_pixel_offset);
18
19 // Sample a single weight. First fetch information about where to sample
20 // and the weight from sample_tex, and then read the pixel itself.
21 vec4 PREFIX(do_sample)(vec2 tc, int i)
22 {
23         vec2 sample_tc;
24         sample_tc.x = float(i) * PREFIX(sample_x_scale) + PREFIX(sample_x_offset);
25 #if DIRECTION_VERTICAL
26         sample_tc.y = tc.y * PREFIX(num_loops);
27 #else
28         sample_tc.y = tc.x * PREFIX(num_loops);
29 #endif
30         vec2 sample = tex2D(PREFIX(sample_tex), sample_tc).rg;
31
32 #if DIRECTION_VERTICAL
33         tc.y = sample.g + floor(sample_tc.y) * PREFIX(slice_height);
34 #else
35         tc.x = sample.g + floor(sample_tc.y) * PREFIX(slice_height);
36 #endif
37         return vec4(sample.r) * INPUT(tc);
38 }
39
40 vec4 FUNCNAME(vec2 tc) {
41 #if DIRECTION_VERTICAL
42         tc.y += PREFIX(whole_pixel_offset);
43 #else
44         tc.x += PREFIX(whole_pixel_offset);
45 #endif
46         vec4 sum = PREFIX(do_sample)(tc, 0);
47         for (int i = 1; i < PREFIX(num_samples); ++i) {
48                 sum += PREFIX(do_sample)(tc, i);
49         }
50         return sum;
51 }
52
53 #undef DIRECTION_VERTICAL