Fix opacity issue when we use corner radius 93/251293/9
authorHeeyong Song <heeyong.song@samsung.com>
Tue, 12 Jan 2021 08:03:59 +0000 (17:03 +0900)
committerAdeel Kazmi <adeel.kazmi@samsung.com>
Thu, 21 Jan 2021 16:18:31 +0000 (16:18 +0000)
NOTE : As you know, GPU doesn't like if-else statement.
But if we can skip operations for most adjacent pixels,
if-else statemnet is somtimes more efficient.

Change-Id: Ibb09a84e383e4769c3e7ead39f48073455d6e650
Signed-off-by: Eunki, Hong <eunkiki.hong@samsung.com>
dali-toolkit/internal/graphics/shaders/color-visual-rounded-corner-shader.frag
dali-toolkit/internal/graphics/shaders/color-visual-rounded-corner-shader.vert
dali-toolkit/internal/graphics/shaders/gradient-visual-bounding-box-rounded-corner-shader.vert
dali-toolkit/internal/graphics/shaders/gradient-visual-linear-rounded-corner-shader.frag
dali-toolkit/internal/graphics/shaders/gradient-visual-radial-rounded-corner-shader.frag
dali-toolkit/internal/graphics/shaders/gradient-visual-user-space-rounded-corner-shader.vert
dali-toolkit/internal/graphics/shaders/image-visual-rounded-corner-shader.frag
dali-toolkit/internal/graphics/shaders/image-visual-rounded-corner-shader.vert

index d68e66f..4654ab4 100644 (file)
@@ -7,7 +7,19 @@ uniform lowp vec3 mixColor;
 
 void main()
 {
-  mediump float dist = length( max( abs( vPosition ), vRectSize ) - vRectSize ) - vCornerRadius;
   OUT_COLOR = vec4(mixColor, 1.0) * uColor;
-  OUT_COLOR.a *= 1.0 - smoothstep( -1.0, 1.0, dist );
-}
\ No newline at end of file
+  mediump vec2 diff = abs( vPosition ) - vRectSize;
+  mediump float dist = length( max( diff, vec2( 0.0 ) ) ) - vCornerRadius;
+  if( dist > 1.0 )
+  {
+    OUT_COLOR.a = 0.0;
+  }
+  else if( dist > -1.0 )
+  {
+    if( min( diff.x, diff.y ) < 0.0)
+    {
+      dist += min( diff.x, diff.y ) / vCornerRadius;
+    }
+    OUT_COLOR.a *= 1.0 - smoothstep( -1.0, 1.0, dist );
+  }
+}
index f4bc1ad..33216e5 100644 (file)
@@ -24,6 +24,7 @@ vec4 ComputeVertexPosition()
   vCornerRadius = mix( cornerRadius * minSize, cornerRadius, cornerRadiusPolicy);
   vCornerRadius = min( vCornerRadius, minSize * 0.5 );
   vRectSize = visualSize / 2.0 - vCornerRadius;
+  vCornerRadius = max( vCornerRadius, 1.0 );
   vPosition = aPosition* visualSize;
   return vec4( vPosition + anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );
 }
@@ -31,4 +32,4 @@ vec4 ComputeVertexPosition()
 void main()
 {
   gl_Position = uMvpMatrix * ComputeVertexPosition();
-}
\ No newline at end of file
+}
index 79724e7..144fa81 100644 (file)
@@ -24,6 +24,7 @@ vec4 ComputeVertexPosition()
   vCornerRadius = mix( cornerRadius * minSize, cornerRadius, cornerRadiusPolicy);
   vCornerRadius = min( vCornerRadius, minSize * 0.5 );
   vRectSize = visualSize * 0.5 - vCornerRadius;
+  vCornerRadius = max( vCornerRadius, 1.0 );
   vPosition = aPosition * visualSize;
   return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );
 }
index 61984eb..548367c 100644 (file)
@@ -8,7 +8,19 @@ varying mediump float vCornerRadius;
 
 void main()
 {
-  mediump float dist = length( max( abs( vPosition ), vRectSize ) - vRectSize ) - vCornerRadius;
   gl_FragColor = texture2D( sTexture, vec2( vTexCoord.y, 0.5 ) ) * vec4(mixColor, 1.0) * uColor;
-  gl_FragColor *= 1.0 - smoothstep( -1.0, 1.0, dist );
+  mediump vec2 diff = abs( vPosition ) - vRectSize;
+  mediump float dist = length( max( diff, vec2( 0.0 ) ) ) - vCornerRadius;
+  if( dist > 1.0 )
+  {
+    gl_FragColor = vec4( 0.0 );
+  }
+  else if( dist > -1.0 )
+  {
+    if( min( diff.x, diff.y ) < 0.0 )
+    {
+      dist += min( diff.x, diff.y ) / vCornerRadius;
+    }
+    gl_FragColor *= 1.0 - smoothstep( -1.0, 1.0, dist );
+  }
 }
index 0ae8163..10bf0fe 100644 (file)
@@ -8,7 +8,19 @@ varying mediump float vCornerRadius;
 
 void main()
 {
-  mediump float dist = length( max( abs( vPosition ), vRectSize ) - vRectSize ) - vCornerRadius;
   gl_FragColor = texture2D( sTexture, vec2( length(vTexCoord), 0.5 ) ) * vec4(mixColor, 1.0) * uColor;
-  gl_FragColor *= 1.0 - smoothstep( -1.0, 1.0, dist );
+  mediump vec2 diff = abs( vPosition ) - vRectSize;
+  mediump float dist = length( max( diff, vec2( 0.0 ) ) ) - vCornerRadius;
+  if( dist > 1.0 )
+  {
+    gl_FragColor = vec4( 0.0 );
+  }
+  else if( dist > -1.0 )
+  {
+    if( min( diff.x, diff.y ) < 0.0)
+    {
+      dist += min( diff.x, diff.y ) / vCornerRadius;
+    }
+    gl_FragColor *= 1.0 - smoothstep( -1.0, 1.0, dist );
+  }
 }
index 38f05fb..f383185 100644 (file)
@@ -24,6 +24,7 @@ vec4 ComputeVertexPosition()
   vCornerRadius = mix( cornerRadius * minSize, cornerRadius, cornerRadiusPolicy);
   vCornerRadius = min( vCornerRadius, minSize * 0.5 );
   vRectSize = visualSize * 0.5 - vCornerRadius;
+  vCornerRadius = max( vCornerRadius, 1.0 );
   vPosition = aPosition * visualSize;
   return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );
 }
index 1e3a1a7..0e2c1d8 100644 (file)
@@ -10,10 +10,23 @@ uniform lowp float preMultipliedAlpha;
 
 void main()
 {
-  mediump float dist = length( max( abs( vPosition ), vRectSize ) - vRectSize ) - vCornerRadius;
-  mediump float opacity = 1.0 - smoothstep( -1.0, 1.0, dist );
+  mediump vec2 diff = abs( vPosition ) - vRectSize;
+  mediump float dist = length( max( diff, vec2( 0.0 ) ) ) - vCornerRadius;
+  mediump float opacity = 1.0;
+  if( dist > 1.0 )
+  {
+    opacity = 0.0;
+  }
+  else if( dist > -1.0 )
+  {
+    if( min( diff.x, diff.y ) < 0.0 )
+    {
+      dist += min( diff.x, diff.y ) / vCornerRadius;
+    }
+    opacity = 1.0 - smoothstep( -1.0, 1.0, dist );
+  }
 
   OUT_COLOR = TEXTURE( sTexture, vTexCoord ) * uColor * vec4( mixColor, 1.0 );
   OUT_COLOR.a *= opacity;
   OUT_COLOR.rgb *= mix( 1.0, opacity, preMultipliedAlpha );
-}
\ No newline at end of file
+}
index d4b129e..0bd3530 100644 (file)
@@ -26,6 +26,7 @@ vec4 ComputeVertexPosition()
   vCornerRadius = mix( cornerRadius * minSize, cornerRadius, cornerRadiusPolicy);
   vCornerRadius = min( vCornerRadius, minSize * 0.5 );
   vRectSize = visualSize * 0.5 - vCornerRadius;
+  vCornerRadius = max( vCornerRadius, 1.0 );
   vPosition = aPosition* visualSize;
   return vec4( vPosition + anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );
 }
@@ -34,4 +35,4 @@ void main()
 {
   gl_Position = uMvpMatrix * ComputeVertexPosition();
   vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );
-}
\ No newline at end of file
+}