]> git.sesse.net Git - movit/blob - dither_effect.frag
Update .gitignore.
[movit] / dither_effect.frag
1 // Implicit uniforms:
2 // uniform sampler2D PREFIX(dither_tex);
3 // uniform vec2 PREFIX(tc_scale);
4 // uniform float PREFIX(round_fac), PREFIX(inv_round_fac);
5
6 // See footer.frag for details about this if statement.
7 #ifndef YCBCR_ALSO_OUTPUT_RGBA
8 #define YCBCR_ALSO_OUTPUT_RGBA 0
9 #endif
10
11 #if YCBCR_ALSO_OUTPUT_RGBA
12
13 // There are two values to dither; otherwise, exactly the same as the algorithm below
14 // (so comments are not duplicated).
15
16 vec4[2] FUNCNAME(vec2 tc) {
17         vec4[2] result = INPUT(tc);
18         float d = tex2D(PREFIX(dither_tex), tc * PREFIX(tc_scale)).x;
19         result[0].rgb += vec3(d);
20         result[1].rgb += vec3(d);
21
22 #if NEED_EXPLICIT_ROUND
23         result[0] = round(result[0] * vec4(PREFIX(round_fac))) * vec4(PREFIX(inv_round_fac));
24         result[1] = round(result[1] * vec4(PREFIX(round_fac))) * vec4(PREFIX(inv_round_fac));
25 #endif
26
27         return result;
28 }
29
30 #else
31
32 vec4 FUNCNAME(vec2 tc) {
33         vec4 result = INPUT(tc);
34         float d = tex2D(PREFIX(dither_tex), tc * PREFIX(tc_scale)).x;
35
36         // Don't dither alpha; the case of alpha=255 (1.0) is very important to us,
37         // and if there's any inaccuracy earlier in the chain so that it becomes e.g.
38         // 254.8, it's better to just get it rounded off than to dither and have it
39         // possibly get down to 254. This is not the case for the color components.
40         result.rgb += vec3(d);
41
42         // NEED_EXPLICIT_ROUND will be #defined to 1 if the GPU has inaccurate
43         // fp32 -> int8 framebuffer rounding, and 0 otherwise.
44 #if NEED_EXPLICIT_ROUND
45         result = round(result * vec4(PREFIX(round_fac))) * vec4(PREFIX(inv_round_fac));
46 #endif
47
48         return result;
49 }
50
51 #endif