evas: apply dithering and softeness algorithm for rounded corner. 64/317064/1
authorHosang Kim <hosang12.kim@samsung.com>
Tue, 3 Sep 2024 10:11:08 +0000 (19:11 +0900)
committerHosang Kim <hosang12.kim@samsung.com>
Tue, 3 Sep 2024 10:11:08 +0000 (19:11 +0900)
Change-Id: I961cac3a68afeffdb484ffdb70faafe04562b1e0

src/modules/evas/engines/gl_common/evas_gl_context.c
src/modules/evas/engines/gl_common/shader/evas_gl_shaders.x
src/modules/evas/engines/gl_common/shader/fragment.glsl

index 774b266..be9136a 100644 (file)
@@ -4317,7 +4317,7 @@ evas_gl_common_filter_rounded_corner_push(Evas_Engine_GL_Context *gc,
         prog->filter->uniform.resolution_value[0] = w;
         prog->filter->uniform.resolution_value[1] = h;
         glUseProgram(prog->prog);
-        glUniform2iv(prog->filter->uniform.corner_radius_loc, 1, prog->filter->uniform.corner_radius_value);
+        glUniform2fv(prog->filter->uniform.corner_radius_loc, 1, prog->filter->uniform.corner_radius_value);
         glUniform2fv(prog->filter->uniform.resolution_loc, 1, prog->filter->uniform.resolution_value);
      }
 
index 6fb399b..90b58f1 100755 (executable)
@@ -69,21 +69,23 @@ static const char fragment_glsl[] =
    "uniform float xDerivative;\n"
    "uniform float yDerivative;\n"
    "uniform sampler2D paletteSampler;\n"
-   "vec4 texture2DPaletteResize(sampler2D sampler, highp vec2 texC) {\n"
-   "float a = texture2D(sampler, texC).a;\n"
-   "float a1 = texture2D(sampler, texC + vec2(xDerivative, 0.)).a;\n"
-   "float a2 = texture2D(sampler, texC + vec2(0., yDerivative)).a;\n"
-   "float a3 = texture2D(sampler, texC + vec2(xDerivative, yDerivative)).a;\n"
-   "vec4 color = texture2D(paletteSampler, vec2(a, .5));\n"
-   "vec4 color1 = texture2D(paletteSampler, vec2(a1, .5));\n"
-   "vec4 color2 = texture2D(paletteSampler, vec2(a2, .5));\n"
-   "vec4 color3 = texture2D(paletteSampler, vec2(a3, .5));\n"
-   "vec2 f = fract(texC.xy / vec2(xDerivative, yDerivative));\n"
-   "return mix(mix(color, color1, f.x), mix(color2, color3, f.x), f.y);\n"
+   "vec4 texture2DPaletteResize(sampler2D sampler, highp vec2 texC)\n"
+   "{\n"
+   "   float a = texture2D(sampler, texC).a;\n"
+   "   float a1 = texture2D(sampler, texC + vec2(xDerivative, 0.)).a;\n"
+   "   float a2 = texture2D(sampler, texC + vec2(0., yDerivative)).a;\n"
+   "   float a3 = texture2D(sampler, texC + vec2(xDerivative, yDerivative)).a;\n"
+   "   vec4 color = texture2D(paletteSampler, vec2(a, .5));\n"
+   "   vec4 color1 = texture2D(paletteSampler, vec2(a1, .5));\n"
+   "   vec4 color2 = texture2D(paletteSampler, vec2(a2, .5));\n"
+   "   vec4 color3 = texture2D(paletteSampler, vec2(a3, .5));\n"
+   "   vec2 f = fract(texC.xy / vec2(xDerivative, yDerivative));\n"
+   "   return mix(mix(color, color1, f.x), mix(color2, color3, f.x), f.y);\n"
    "}\n"
-   "vec4 texture2DPalette(sampler2D sampler, highp vec2 texC) {\n"
-   "float a = texture2D(sampler, texC).a;\n"
-   "return texture2D(paletteSampler, vec2(a, .5));\n"
+   "vec4 texture2DPalette(sampler2D sampler, highp vec2 texC)\n"
+   "{\n"
+   "   float a = texture2D(sampler, texC).a;\n"
+   "   return texture2D(paletteSampler, vec2(a, .5));\n"
    "}\n"
    "#endif\n"
    "#ifdef SHD_ALPHA\n"
@@ -121,6 +123,18 @@ static const char fragment_glsl[] =
    "#ifdef SHD_FILTER_ROUNDED_CORNER\n"
    "uniform vec2 corner_radius;\n"
    "uniform vec2 resolution;\n"
+   "highp float nrand(const in vec2 uv)\n"
+   "{\n"
+   "  const highp float a = 12.9898, b = 78.233, c = 43758.5453, pi = 3.141592653589793;\n"
+   "  highp float dt = dot(uv, vec2(a, b)), sn = mod(dt, pi);\n"
+   "  return fract(sin(sn) * c);\n"
+   "}\n"
+   "vec3 applyDithering( vec3 inColor )\n"
+   "{\n"
+   "  float rnd = nrand(tex_c) - 0.5;\n"
+   "  inColor.rgb += rnd / 255.0;\n"
+   "  return inColor;\n"
+   "}\n"
    "float roundedBoxSDF(vec2 p, vec2 size, float r) {\n"
    "    return length(max(abs(p)-size+r,0.0)) - r;\n"
    "}\n"
@@ -259,12 +273,18 @@ static const char fragment_glsl[] =
    "   c.rgb = c.a - c.rgba;\n"
    "#endif\n"
    "#ifdef SHD_FILTER_ROUNDED_CORNER\n"
+   "   float radius =\n"
+   "    mix(\n"
+   "      mix(corner_radius.x, corner_radius.y, sign(coord.x)*0.5 + 0.5),\n"
+   "      mix(corner_radius.x, corner_radius.y, sign(coord.x)*0.5 + 0.5),\n"
+   "      sign(coord.y) * 0.5 + 0.5\n"
+   "     );\n"
    "   vec2 location = resolution / 2.0;\n"
-   "   float edgeSoftness  = 1.0;\n"
-   "   float radius = 30.0;\n"
+   "   float edgeSoftness  = min(1.0, radius);\n"
    "   float distance = roundedBoxSDF(gl_FragCoord.xy - location, location, radius);\n"
-   "   float smoothedAlpha = smoothstep(0.0, edgeSoftness, distance);\n"
+   "   float smoothedAlpha = smoothstep(-edgeSoftness, edgeSoftness, distance);\n"
    "   c = mix(c, vec4(0.0), smoothedAlpha);\n"
+   "   c.rgb = applyDithering(c.rgb);\n"
    "#endif\n"
    "#ifndef SHD_FILTER_BLUR\n"
    "   gl_FragColor =\n"
index 1c9f1e9..25d2281 100755 (executable)
@@ -122,6 +122,22 @@ uniform float blur_div;
 #ifdef SHD_FILTER_ROUNDED_CORNER
 uniform vec2 corner_radius;
 uniform vec2 resolution;
+
+highp float nrand(const in vec2 uv)
+{
+  const highp float a = 12.9898, b = 78.233, c = 43758.5453, pi = 3.141592653589793;
+  highp float dt = dot(uv, vec2(a, b)), sn = mod(dt, pi);
+  return fract(sin(sn) * c);
+}
+
+vec3 applyDithering( vec3 inColor )
+{
+  float rnd = nrand(tex_c) - 0.5;
+  inColor.rgb += rnd / 255.0;
+  return inColor;
+}
+
+
 float roundedBoxSDF(vec2 p, vec2 size, float r) {
     return length(max(abs(p)-size+r,0.0)) - r;
 }
@@ -282,12 +298,19 @@ vec4 fetch_pixel(float ox, float oy)
 #endif
 
 #ifdef SHD_FILTER_ROUNDED_CORNER
+   float radius =
+    mix(
+      mix(corner_radius.x, corner_radius.y, sign(coord.x)*0.5 + 0.5),
+      mix(corner_radius.x, corner_radius.y, sign(coord.x)*0.5 + 0.5),
+      sign(coord.y) * 0.5 + 0.5
+     );
+
    vec2 location = resolution / 2.0;
-   float edgeSoftness  = 1.0;
-   float radius = 30.0;
+   float edgeSoftness  = min(1.0, radius);
    float distance = roundedBoxSDF(gl_FragCoord.xy - location, location, radius);
-   float smoothedAlpha = smoothstep(0.0, edgeSoftness, distance);
+   float smoothedAlpha = smoothstep(-edgeSoftness, edgeSoftness, distance);
    c = mix(c, vec4(0.0), smoothedAlpha);
+   c.rgb = applyDithering(c.rgb);
 #endif
 
 #ifndef SHD_FILTER_BLUR