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 d68e66f8c5c6f7b030c5f55d45a98f006add93ff..4654ab4620541da70f4a5f755dca57b954b817bf 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 f4bc1ad1ad9e0a2f27d78be434e1929ef1468c47..33216e54ebcc687855eecd3bc3ac2aceb8019b93 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 79724e7b1c92e577946842b163cceea677e6df7f..144fa819c899c54c3165d7eae28a9bb933a20b7e 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 61984ebb0312f3d2d0c662381c9777d02bb6301c..548367ce72e388a8ffdb72a381b69ac8fbee2422 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 0ae8163ae0ae7f3a9f8ace3640b0f99544a9b0dd..10bf0fe544e26b78dd479d4deee71b961a834a87 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 38f05fb4e2b5bf31254ded6dbed12da394159431..f383185a163786daff9619fd7c59611f9267711a 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 1e3a1a743035cac3f000fcdc257f50bda26de9cd..0e2c1d85902537eef801ec3eb8f2ff659be3008a 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 d4b129ec56371c3026480c94caa95962070556c2..0bd35308dc4bd8218e7dc8819e7f1ff143478672 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
+}