aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean-Philippe Andre <jp.andre@samsung.com>2016-03-17 16:30:24 +0900
committerJean-Philippe Andre <jp.andre@samsung.com>2016-03-17 16:30:24 +0900
commitaa9a0b2bedb7b29fb0df5283d4bdef68006ee3ab (patch)
tree2b9c43194cd598529d06e603bbd07008b2558d9f
parentEfl.Canvas.Image: Add forgotten files! (diff)
downloadefl-devs/jpeg/dithering.tar.gz
Evas GL shaders: Add random noise in the fragment shaderdevs/jpeg/dithering
This small noise, added to the fragment shader's output color, makes smooth up-scaling of gradient images look very good. Gradient images that are zoomed in a lot show some banding or blocks, that are the dithering pixels (good for the original size, terrible when upscaled). The simplest solution is to add noise, not even using a proper dithering pattern. This patch adds noise everytime there is a texture, without any smart logic. Still looks beautiful.
-rw-r--r--src/modules/evas/engines/gl_common/shader/evas_gl_shaders.x18
-rw-r--r--src/modules/evas/engines/gl_common/shader/fragment.glsl21
2 files changed, 38 insertions, 1 deletions
diff --git a/src/modules/evas/engines/gl_common/shader/evas_gl_shaders.x b/src/modules/evas/engines/gl_common/shader/evas_gl_shaders.x
index 558531a50b..dec8aa93bc 100644
--- a/src/modules/evas/engines/gl_common/shader/evas_gl_shaders.x
+++ b/src/modules/evas/engines/gl_common/shader/evas_gl_shaders.x
@@ -26,9 +26,11 @@ static const char fragment_glsl[] =
"#ifdef SHD_EXTERNAL\n"
"uniform SAMPLER_EXTERNAL_OES tex;\n"
"varying vec2 tex_c;\n"
+ "#define ADD_NOISE 1\n"
"#elif defined(SHD_TEX)\n"
"uniform sampler2D tex;\n"
"varying vec2 tex_c;\n"
+ "#define ADD_NOISE 1\n"
"#endif\n"
"#if defined(SHD_NV12) || defined(SHD_YUY2)\n"
"uniform sampler2D texuv;\n"
@@ -80,6 +82,14 @@ static const char fragment_glsl[] =
"# endif\n"
"# endif\n"
"#endif\n"
+ "#ifdef ADD_NOISE\n"
+ "float rand(vec2 co)\n"
+ "{\n"
+ " /* Magic 'random' number in range [0..1]. 'co' should be random coordinate.\n"
+ " * Distribution is not great. Falls apart with lowp float. */\n"
+ " return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453) - 0.5;\n"
+ "}\n"
+ "#endif\n"
"void main()\n"
"{\n"
" vec4 c;\n"
@@ -130,6 +140,11 @@ static const char fragment_glsl[] =
"#else\n"
" c = vec4(1, 1, 1, 1);\n"
"#endif\n"
+ "#ifdef ADD_NOISE\n"
+ " vec4 noise = vec4(rand(tex_c) / 256.0, rand(tex_c + vec2(1, 1)) / 256.0, rand(tex_c + vec2(2, 2)) / 256.0, 0);\n"
+ " noise.a = max(noise.r, noise.g);\n"
+ " noise.a = max(noise.a, noise.b);\n"
+ "#endif\n"
"#ifdef SHD_MASK\n"
" float ma;\n"
"# if defined(SHD_MASKSAM12) || defined(SHD_MASKSAM21)\n"
@@ -160,6 +175,9 @@ static const char fragment_glsl[] =
"#ifdef SHD_TEXA\n"
" * texture2D(texa, tex_a).r\n"
"#endif\n"
+ "#ifdef ADD_NOISE\n"
+ " + noise\n"
+ "#endif\n"
" ;\n"
"}\n";
diff --git a/src/modules/evas/engines/gl_common/shader/fragment.glsl b/src/modules/evas/engines/gl_common/shader/fragment.glsl
index b534961300..8436298bfe 100644
--- a/src/modules/evas/engines/gl_common/shader/fragment.glsl
+++ b/src/modules/evas/engines/gl_common/shader/fragment.glsl
@@ -13,9 +13,11 @@ varying vec4 col;
#ifdef SHD_EXTERNAL
uniform SAMPLER_EXTERNAL_OES tex;
varying vec2 tex_c;
+#define ADD_NOISE 1
#elif defined(SHD_TEX)
uniform sampler2D tex;
varying vec2 tex_c;
+#define ADD_NOISE 1
#endif
#if defined(SHD_NV12) || defined(SHD_YUY2)
@@ -74,6 +76,15 @@ varying vec2 masktex_s[4];
# endif
#endif
+#ifdef ADD_NOISE
+float rand(vec2 co)
+{
+ /* Magic 'random' number in range [0..1]. 'co' should be random coordinate.
+ * Distribution is not great. Falls apart with lowp float. */
+ return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453) - 0.5;
+}
+#endif
+
void main()
{
vec4 c;
@@ -126,11 +137,16 @@ void main()
#elif defined(SHD_TEX) || defined(SHD_EXTERNAL)
c = texture2D(tex, tex_c).SWZ;
-
#else
c = vec4(1, 1, 1, 1);
#endif
+#ifdef ADD_NOISE
+ vec4 noise = vec4(rand(tex_c) / 256.0, rand(tex_c + vec2(1, 1)) / 256.0, rand(tex_c + vec2(2, 2)) / 256.0, 0);
+ noise.a = max(noise.r, noise.g);
+ noise.a = max(noise.a, noise.b);
+#endif
+
#ifdef SHD_MASK
float ma;
# if defined(SHD_MASKSAM12) || defined(SHD_MASKSAM21)
@@ -163,6 +179,9 @@ void main()
#ifdef SHD_TEXA
* texture2D(texa, tex_a).r
#endif
+#ifdef ADD_NOISE
+ + noise
+#endif
;
}