| 1 |
//------------------------------------------------------------------------------ |
| 2 |
// Dithering configuration |
| 3 |
//------------------------------------------------------------------------------ |
| 4 |
|
| 5 |
// Dithering operators |
| 6 |
#define DITHERING_NONE 0 |
| 7 |
#define DITHERING_INTERLEAVED_NOISE 1 |
| 8 |
#define DITHERING_VLACHOS 2 |
| 9 |
#define DITHERING_TRIANGLE_NOISE 3 |
| 10 |
#define DITHERING_TRIANGLE_NOISE_RGB 4 |
| 11 |
|
| 12 |
#define DITHERING_OPERATOR DITHERING_TRIANGLE_NOISE |
| 13 |
|
| 14 |
//------------------------------------------------------------------------------ |
| 15 |
// Noise |
| 16 |
//------------------------------------------------------------------------------ |
| 17 |
|
| 18 |
// n must be normalized in [0..1] (e.g. texture coordinates) |
| 19 |
float triangleNoise(highp vec2 n) { |
| 20 |
// triangle noise, in [-1.0..1.0[ range |
| 21 |
n = fract(n * vec2(5.3987, 5.4421)); |
| 22 |
n += dot(n.yx, n.xy + vec2(21.5351, 14.3137)); |
| 23 |
|
| 24 |
highp float xy = n.x * n.y; |
| 25 |
// compute in [0..2[ and remap to [-1.0..1.0[ |
| 26 |
return fract(xy * 95.4307) + fract(xy * 75.04961) - 1.0; |
| 27 |
} |
| 28 |
|
| 29 |
// n must not be normalize (e.g. window coordinates) |
| 30 |
float interleavedGradientNoise(highp vec2 n) { |
| 31 |
return fract(52.982919 * fract(dot(vec2(0.06711, 0.00584), n))); |
| 32 |
} |
| 33 |
|
| 34 |
//------------------------------------------------------------------------------ |
| 35 |
// Dithering |
| 36 |
//------------------------------------------------------------------------------ |
| 37 |
|
| 38 |
vec4 Dither_InterleavedGradientNoise(vec4 rgba, const highp float temporalNoise01) { |
| 39 |
// Jimenez 2014, "Next Generation Post-Processing in Call of Duty" |
| 40 |
highp vec2 uv = gl_FragCoord.xy + temporalNoise01; |
| 41 |
|
| 42 |
// The noise variable must be highp to workaround Adreno bug #1096. |
| 43 |
highp float noise = interleavedGradientNoise(uv); |
| 44 |
|
| 45 |
// remap from [0..1[ to [-0.5..0.5[ |
| 46 |
noise -= 0.5; |
| 47 |
|
| 48 |
return rgba + vec4(noise / 255.0); |
| 49 |
} |
| 50 |
|
| 51 |
vec4 Dither_TriangleNoise(vec4 rgba, const highp float temporalNoise01) { |
| 52 |
// Gjøl 2016, "Banding in Games: A Noisy Rant" |
| 53 |
highp vec2 uv = gl_FragCoord.xy * frameUniforms.resolution.zw; |
| 54 |
uv += vec2(0.07 * temporalNoise01); |
| 55 |
|
| 56 |
// The noise variable must be highp to workaround Adreno bug #1096. |
| 57 |
highp float noise = triangleNoise(uv); |
| 58 |
|
| 59 |
// noise is in [-1..1[ |
| 60 |
|
| 61 |
return rgba + vec4(noise / 255.0); |
| 62 |
} |
| 63 |
|
| 64 |
vec4 Dither_Vlachos(vec4 rgba, const highp float temporalNoise01) { |
| 65 |
// Vlachos 2016, "Advanced VR Rendering" |
| 66 |
float noise = dot(vec2(171.0, 231.0), gl_FragCoord.xy + temporalNoise01); |
| 67 |
vec3 noiseRGB = fract(vec3(noise) / vec3(103.0, 71.0, 97.0)); |
| 68 |
|
| 69 |
// remap from [0..1[ to [-0.5..0.5[ |
| 70 |
noiseRGB -= 0.5; |
| 71 |
|
| 72 |
return vec4(rgba.rgb + (noiseRGB / 255.0), rgba.a); |
| 73 |
} |
| 74 |
|
| 75 |
vec4 Dither_TriangleNoiseRGB(vec4 rgba, const highp float temporalNoise01) { |
| 76 |
// Gjøl 2016, "Banding in Games: A Noisy Rant" |
| 77 |
highp vec2 uv = gl_FragCoord.xy * frameUniforms.resolution.zw; |
| 78 |
uv += vec2(0.07 * temporalNoise01); |
| 79 |
|
| 80 |
vec3 noiseRGB = vec3( |
| 81 |
triangleNoise(uv), |
| 82 |
triangleNoise(uv + 0.1337), |
| 83 |
triangleNoise(uv + 0.3141)); |
| 84 |
|
| 85 |
// noise is in [-1..1[ |
| 86 |
|
| 87 |
return rgba + noiseRGB.xyzx / 255.0; |
| 88 |
} |
| 89 |
|
| 90 |
//------------------------------------------------------------------------------ |
| 91 |
// Dithering dispatch |
| 92 |
//------------------------------------------------------------------------------ |
| 93 |
|
| 94 |
/** |
| 95 |
* Dithers the specified RGBA color based on the current time and fragment |
| 96 |
* coordinates the input must be in the final color space (including OECF). |
| 97 |
* This dithering function assumes we are dithering to an 8-bit target. |
| 98 |
* This function dithers the alpha channel assuming premultiplied output |
| 99 |
*/ |
| 100 |
vec4 dither(vec4 rgba, const highp float temporalNoise01) { |
| 101 |
#if DITHERING_OPERATOR == DITHERING_NONE |
| 102 |
return rgba; |
| 103 |
#elif DITHERING_OPERATOR == DITHERING_INTERLEAVED_NOISE |
| 104 |
return Dither_InterleavedGradientNoise(rgba, temporalNoise01); |
| 105 |
#elif DITHERING_OPERATOR == DITHERING_VLACHOS |
| 106 |
return Dither_Vlachos(rgba, temporalNoise01); |
| 107 |
#elif DITHERING_OPERATOR == DITHERING_TRIANGLE_NOISE |
| 108 |
return Dither_TriangleNoise(rgba, temporalNoise01); |
| 109 |
#elif DITHERING_OPERATOR == DITHERING_TRIANGLE_NOISE_RGB |
| 110 |
return Dither_TriangleNoiseRGB(rgba, temporalNoise01); |
| 111 |
#endif |
| 112 |
} |