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