aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Eric Karl <ericrk@chromium.org>2017-08-07 15:55:13 -0700
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2017-08-09 17:43:25 +0000
commite1fc9ac0453470cae2b76f3d22443b9a13ef0811 (patch)
treea5981b3fb172c56a540b8343d9ffb051ebf1a38b /src
parent7e59e391efc82585cbacbd8223a4aaf5ae632b42 (diff)
Improve float-based dither logic
This code simulates the integer-based ordered-dither using step/mod with only floating point values. Produces similar results. R=bsalomon@google.com Bug: skia:4430 Change-Id: I1406f751f0ddd6bfd14e532dfb4efc0bb5784992 Reviewed-on: https://skia-review.googlesource.com/28942 Commit-Queue: Eric Karl <ericrk@chromium.org> Reviewed-by: Brian Salomon <bsalomon@google.com>
Diffstat (limited to 'src')
-rw-r--r--src/gpu/effects/GrDitherEffect.cpp11
-rw-r--r--src/gpu/effects/GrDitherEffect.fp14
2 files changed, 12 insertions, 13 deletions
diff --git a/src/gpu/effects/GrDitherEffect.cpp b/src/gpu/effects/GrDitherEffect.cpp
index 17b94ff087..f2fc6784a2 100644
--- a/src/gpu/effects/GrDitherEffect.cpp
+++ b/src/gpu/effects/GrDitherEffect.cpp
@@ -27,13 +27,14 @@ public:
"float value;\nfloat range;\n@switch (%d) {\n case 0:\n range = "
"0.0039215686274509803;\n break;\n case 1:\n range = "
"0.015873015873015872;\n break;\n default:\n range = "
- "0.0083333333333333332;\n break;\n}\n@if (sk_Caps.integerSupport) {\n "
+ "0.066666666666666666;\n break;\n}\n@if (sk_Caps.integerSupport) {\n "
"uint x = uint(sk_FragCoord.x);\n uint y = uint(sk_FragCoord.y);\n uint m = "
"(((((y & 1) << 5 | (x & 1) << 4) | (y & 2) << 2) | (x & 2) << 1) | (y & 4) >> 1) "
- "| (x & 4) >> 2;\n value = float(m) / 64.0 - 0.4921875;\n} else {\n value = "
- "fract(sin(dot(sk_FragCoord.xy, float2(12.989800000000001, 78.233000000000004))) * "
- "43758.545299999998) - 0.5;\n}\n%s = float4(clamp(%s.xyz + value * range, 0.0, "
- "%s.w), %s.w);\n",
+ "| (x & 4) >> 2;\n value = float(m) / 64.0 - 0.4921875;\n} else {\n float4 "
+ "modValues = mod(sk_FragCoord.xyxy, float4(2.0, 2.0, 4.0, 4.0));\n float4 "
+ "stepValues = step(modValues, float4(1.0, 1.0, 2.0, 2.0));\n value = "
+ "dot(stepValues, float4(0.5, 0.25, 0.125, 0.0625)) - 0.46875;\n}\n%s = "
+ "float4(clamp(%s.xyz + value * range, 0.0, %s.w), %s.w);\n",
_outer.rangeType(), args.fOutputColor,
args.fInputColor ? args.fInputColor : "float4(1)",
args.fInputColor ? args.fInputColor : "float4(1)",
diff --git a/src/gpu/effects/GrDitherEffect.fp b/src/gpu/effects/GrDitherEffect.fp
index 308e02ed43..7ec852e6b0 100644
--- a/src/gpu/effects/GrDitherEffect.fp
+++ b/src/gpu/effects/GrDitherEffect.fp
@@ -43,7 +43,7 @@ void main() {
break;
default:
// Experimentally this looks better than the expected value of 1/15.
- range = 0.125 / 15.0;
+ range = 1.0 / 15.0;
break;
}
@if (sk_Caps.integerSupport) {
@@ -55,13 +55,11 @@ void main() {
(y & 4) >> 1 | (x & 4) >> 2;
value = float(m) * 1.0 / 64.0 - 63.0 / 128.0;
} else {
- // Generate a random number based on the fragment position. For this
- // random number generator, we use the "GLSL rand" function
- // that seems to be floating around on the internet. It works under
- // the assumption that sin(<big number>) oscillates with high frequency
- // and sampling it will generate "randomness". Since we're using this
- // for rendering and not cryptography it should be OK.
- value = fract(sin(dot(sk_FragCoord.xy, float2(12.9898, 78.233))) * 43758.5453) - .5;
+ // Simulate the integer effect used above using step/mod. For speed, simulates a 4x4
+ // dither pattern rather than an 8x8 one.
+ float4 modValues = mod(sk_FragCoord.xyxy, float4(2.0, 2.0, 4.0, 4.0));
+ float4 stepValues = step(modValues, float4(1.0, 1.0, 2.0, 2.0));
+ value = dot(stepValues, float4(8.0 / 16.0, 4.0 / 16.0, 2.0 / 16.0, 1.0 / 16.0)) - 15.0 / 32.0;
}
// For each color channel, add the random offset to the channel value and then clamp
// between 0 and alpha to keep the color premultiplied.