[dali_2.3.22] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / graphics / shaders / image-visual-shader.frag
index 703c649..bf9f787 100644 (file)
@@ -1,45 +1,53 @@
-#ifndef IS_REQUIRED_ROUNDED_CORNER
-#define IS_REQUIRED_ROUNDED_CORNER 0
-#endif
-#ifndef IS_REQUIRED_BORDERLINE
-#define IS_REQUIRED_BORDERLINE 0
+INPUT mediump vec2 vTexCoord;
+#if defined(IS_REQUIRED_DEBUG_VISUAL_SHADER) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
+INPUT highp vec2 vPosition;
+INPUT highp vec2 vRectSize;
+INPUT highp vec2 vOptRectSize;
+INPUT highp float vAliasMargin;
+#ifdef IS_REQUIRED_ROUNDED_CORNER
+INPUT highp vec4 vCornerRadius;
 #endif
-#ifndef ATLAS_DEFAULT_WARP
-#define ATLAS_DEFAULT_WARP 0
 #endif
-#ifndef ATLAS_CUSTOM_WARP
-#define ATLAS_CUSTOM_WARP 0
+#ifdef IS_REQUIRED_DEBUG_VISUAL_SHADER
+#define DEBUG_EXTRA_VARYINGS
+DEBUG_EXTRA_VARYINGS
 #endif
 
-INPUT mediump vec2 vTexCoord;
-#if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
-INPUT mediump vec2 vPosition;
-INPUT mediump vec2 vRectSize;
-INPUT mediump vec2 vOptRectSize;
-#if IS_REQUIRED_ROUNDED_CORNER
-INPUT mediump vec4 vCornerRadius;
+uniform sampler2D sTexture;
+#if defined(IS_REQUIRED_YUV_TO_RGB) || defined(IS_REQUIRED_UNIFIED_YUV_AND_RGB)
+uniform sampler2D sTextureU;
+uniform sampler2D sTextureV;
 #endif
+
+#ifdef IS_REQUIRED_ALPHA_MASKING
+uniform sampler2D sMaskTexture;
+uniform lowp float uYFlipMaskTexture;
+INPUT mediump vec2 vMaskTexCoord;
 #endif
 
-uniform sampler2D sTexture;
-#if ATLAS_DEFAULT_WARP
+#ifdef ATLAS_DEFAULT_WARP
 uniform mediump vec4 uAtlasRect;
-#elif ATLAS_CUSTOM_WARP
+#elif defined(ATLAS_CUSTOM_WARP)
 // WrapMode -- 0: CLAMP; 1: REPEAT; 2: REFLECT;
 uniform lowp vec2 wrapMode;
 #endif
 
+
+#if defined(IS_REQUIRED_DEBUG_VISUAL_SHADER)
+uniform highp vec3 uScale;
+#endif
+
 uniform lowp vec4 uColor;
 uniform lowp vec3 mixColor;
 uniform lowp float preMultipliedAlpha;
-#if IS_REQUIRED_BORDERLINE
-uniform mediump float borderlineWidth;
-uniform mediump float borderlineOffset;
+#ifdef IS_REQUIRED_BORDERLINE
+uniform highp float borderlineWidth;
+uniform highp float borderlineOffset;
 uniform lowp vec4 borderlineColor;
 uniform lowp vec4 uActorColor;
 #endif
 
-#if ATLAS_CUSTOM_WARP
+#ifdef ATLAS_CUSTOM_WARP
 mediump float wrapCoordinate( mediump vec2 range, mediump float coordinate, lowp float wrap )
 {
   mediump float coord;
@@ -51,31 +59,31 @@ mediump float wrapCoordinate( mediump vec2 range, mediump float coordinate, lowp
 }
 #endif
 
-#if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
+#if defined(IS_REQUIRED_DEBUG_VISUAL_SHADER) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
 // Global values both rounded corner and borderline use
 
 // radius of rounded corner on this quadrant
-mediump float gRadius = 0.0;
+highp float gRadius = 0.0;
 
 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
-mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
+highp vec2 gFragmentPosition = vec2(0.0, 0.0);
 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
-mediump float gCenterPosition = 0.0;
+highp float gCenterPosition = 0.0;
 // relative coordinate of gFragmentPosition from gCenterPosition.
-mediump vec2 gDiff = vec2(0.0, 0.0);
+highp vec2 gDiff = vec2(0.0, 0.0);
 // potential value what our algorithm use.
-mediump float gPotential = 0.0;
+highp float gPotential = 0.0;
 
 // threshold of potential
-mediump float gPotentialRange = 0.0;
-mediump float gMaxOutlinePotential = 0.0;
-mediump float gMinOutlinePotential = 0.0;
-mediump float gMaxInlinePotential = 0.0;
-mediump float gMinInlinePotential = 0.0;
+highp float gPotentialRange = 0.0;
+highp float gMaxOutlinePotential = 0.0;
+highp float gMinOutlinePotential = 0.0;
+highp float gMaxInlinePotential = 0.0;
+highp float gMinInlinePotential = 0.0;
 
 void calculateCornerRadius()
 {
-#if IS_REQUIRED_ROUNDED_CORNER
+#ifdef IS_REQUIRED_ROUNDED_CORNER
   gRadius =
   mix(
     mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
@@ -89,7 +97,7 @@ void calculatePosition()
 {
   gFragmentPosition = abs(vPosition) - vRectSize;
   gCenterPosition = -gRadius;
-#if IS_REQUIRED_BORDERLINE
+#ifdef IS_REQUIRED_BORDERLINE
   gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
 #endif
   gDiff = gFragmentPosition - gCenterPosition;
@@ -102,12 +110,12 @@ void calculatePotential()
 
 void setupMinMaxPotential()
 {
-  gPotentialRange = 1.0;
+  gPotentialRange = vAliasMargin;
 
   gMaxOutlinePotential = gRadius + gPotentialRange;
   gMinOutlinePotential = gRadius - gPotentialRange;
 
-#if IS_REQUIRED_BORDERLINE
+#ifdef IS_REQUIRED_BORDERLINE
   gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
   gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
 #else
@@ -116,8 +124,8 @@ void setupMinMaxPotential()
 #endif
 
   // reduce defect near edge of rounded corner.
-  gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
-  gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
+  gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y) / max(1.0, gRadius), 0.0, 1.0);
+  gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y) / max(1.0, gRadius), 0.0, 1.0);
 }
 
 void PreprocessPotential()
@@ -130,10 +138,10 @@ void PreprocessPotential()
 }
 #endif
 
-#if IS_REQUIRED_BORDERLINE
+#ifdef IS_REQUIRED_BORDERLINE
 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
 {
-  mediump float potential = gPotential;
+  highp float potential = gPotential;
 
   // default opacity of borderline is 0.0
   mediump float borderlineOpacity = 0.0;
@@ -145,7 +153,7 @@ lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
     borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
 
     // Muliply borderlineWidth to resolve very thin borderline
-    borderlineOpacity *= min(1.0, borderlineWidth);
+    borderlineOpacity *= min(1.0, borderlineWidth / gPotentialRange);
   }
 
   lowp vec3  borderlineColorRGB   = borderlineColor.rgb * uActorColor.rgb;
@@ -157,9 +165,9 @@ lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
   // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
   if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
   {
-    mediump float tCornerRadius = -gCenterPosition;
-    mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
-    mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
+    highp float tCornerRadius = -gCenterPosition + gPotentialRange;
+    highp float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
+    highp float MinTexturelinePotential = tCornerRadius - gPotentialRange;
     if(potential > MaxTexturelinePotential)
     {
       // potential is out of texture range.
@@ -193,10 +201,10 @@ lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
 }
 #endif
 
-#if IS_REQUIRED_ROUNDED_CORNER
+#ifdef IS_REQUIRED_ROUNDED_CORNER
 mediump float calculateCornerOpacity()
 {
-  mediump float potential = gPotential;
+  highp float potential = gPotential;
 
   // default opacity is 1.0
   mediump float opacity = 1.0;
@@ -215,42 +223,203 @@ mediump float calculateCornerOpacity()
 }
 #endif
 
+#if defined(IS_REQUIRED_YUV_TO_RGB) || defined(IS_REQUIRED_UNIFIED_YUV_AND_RGB)
+lowp vec4 ConvertYuvToRgba(mediump vec2 texCoord)
+{
+#ifdef IS_REQUIRED_UNIFIED_YUV_AND_RGB
+  // Special case when shader use YUV but actual textures are not YUV format.
+  // In this case, just resturn sTexture.
+  if(textureSize(sTextureU, 0) != textureSize(sTextureV, 0))
+  {
+    return texture(sTexture, texCoord);
+  }
+#endif
+
+  lowp float y = texture(sTexture, texCoord).r;
+  lowp float u = texture(sTextureU, texCoord).r - 0.5;
+  lowp float v = texture(sTextureV, texCoord).r - 0.5;
+  lowp vec4 rgba;
+  rgba.r = y + (1.403 * v);
+  rgba.g = y - (0.344 * u) - (0.714 * v);
+  rgba.b = y + (1.770 * u);
+  rgba.a = 1.0;
+  return rgba;
+}
+#endif
+
+#ifdef IS_REQUIRED_DEBUG_VISUAL_SHADER
+
+// Predefined values whether some macro defined or not.
+// Since we make debug codes replace by macro,
+// sharp if keyword cannot be used.
+// Instead, let we use bool values so we can use define checked in script
+#ifdef IS_REQUIRED_ROUNDED_CORNER
+const bool IS_REQUIRED_ROUNDED_CORNER_BOOL = true;
+#else
+const bool IS_REQUIRED_ROUNDED_CORNER_BOOL = false;
+#endif
+#ifdef IS_REQUIRED_BORDERLINE
+const bool IS_REQUIRED_BORDERLINE_BOOL = true;
+#else
+const bool IS_REQUIRED_BORDERLINE_BOOL = false;
+#endif
+#ifdef IS_REQUIRED_YUV_TO_RGB
+const bool IS_REQUIRED_YUV_TO_RGB_BOOL = true;
+#else
+const bool IS_REQUIRED_YUV_TO_RGB_BOOL = false;
+#endif
+#ifdef IS_REQUIRED_UNIFIED_YUV_AND_RGB
+const bool IS_REQUIRED_UNIFIED_YUV_AND_RGB_BOOL = true;
+#else
+const bool IS_REQUIRED_UNIFIED_YUV_AND_RGB_BOOL = false;
+#endif
+#ifdef IS_REQUIRED_ALPHA_MASKING
+const bool IS_REQUIRED_ALPHA_MASKING_BOOL = true;
+#else
+const bool IS_REQUIRED_ALPHA_MASKING_BOOL = false;
+#endif
+#ifdef ATLAS_DEFAULT_WARP
+const bool ATLAS_DEFAULT_WARP_BOOL = true;
+#else
+const bool ATLAS_DEFAULT_WARP_BOOL = false;
+#endif
+#ifdef ATLAS_CUSTOM_WARP
+const bool ATLAS_CUSTOM_WARP_BOOL = true;
+#else
+const bool ATLAS_CUSTOM_WARP_BOOL = false;
+#endif
+
+// These lines in the shader may be replaced with actual definitions by the debug-image-visual-shader-script.json.
+// DEBUG_TRIGGER_CODE return bool type, and DEBUG_RATIO_CODE return float value which will be clamped between 0.0 and 1.0
+// If DEBUG_TRIGGER_CODE return true, it mean we will change final color's channel value.
+// If ratio is 0.0, debug color rate become MINIMUM_DEBUG_COLOR_RATE, and 1.0 than MAXIMUM_DEBUG_COLOR_RATE.
+#define MINIMUM_DEBUG_COLOR_RATE
+#define MAXIMUM_DEBUG_COLOR_RATE
+#define DEBUG_TRIGGER_RED_CODE
+#define DEBUG_TRIGGER_GREEN_CODE
+#define DEBUG_TRIGGER_BLUE_CODE
+#define DEBUG_RATIO_RED_CODE
+#define DEBUG_RATIO_GREEN_CODE
+#define DEBUG_RATIO_BLUE_CODE
+#define DEBUG_EXTRA_UNIFORMS
+
+DEBUG_EXTRA_UNIFORMS
+
+const mediump float gMinDebugColorRate = MINIMUM_DEBUG_COLOR_RATE;
+const mediump float gMaxDebugColorRate = MAXIMUM_DEBUG_COLOR_RATE;
+
+bool DebugTriggerRed(mediump vec4 originColor)
+{
+  DEBUG_TRIGGER_RED_CODE
+}
+
+bool DebugTriggerGreen(mediump vec4 originColor)
+{
+  DEBUG_TRIGGER_GREEN_CODE
+}
+
+bool DebugTriggerBlue(mediump vec4 originColor)
+{
+  DEBUG_TRIGGER_BLUE_CODE
+}
+
+mediump float DebugRatioRed(mediump vec4 originColor)
+{
+  DEBUG_RATIO_RED_CODE
+}
+
+mediump float DebugRatioGreen(mediump vec4 originColor)
+{
+  DEBUG_RATIO_GREEN_CODE
+}
+
+mediump float DebugRatioBlue(mediump vec4 originColor)
+{
+  DEBUG_RATIO_BLUE_CODE
+}
+
+mediump vec3 ApplyDebugMixColor(mediump vec4 originColor)
+{
+  mediump float debugColorRateRed = 0.0;
+  mediump float debugColorRateGreen = 0.0;
+  mediump float debugColorRateBlue = 0.0;
+
+  if(DebugTriggerRed(originColor))
+  {
+    debugColorRateRed = mix(gMinDebugColorRate, gMaxDebugColorRate, smoothstep(0.0, 1.0, DebugRatioRed(originColor)));
+  }
+  if(DebugTriggerGreen(originColor))
+  {
+    debugColorRateGreen = mix(gMinDebugColorRate, gMaxDebugColorRate, smoothstep(0.0, 1.0, DebugRatioGreen(originColor)));
+  }
+  if(DebugTriggerBlue(originColor))
+  {
+    debugColorRateBlue = mix(gMinDebugColorRate, gMaxDebugColorRate, smoothstep(0.0, 1.0, DebugRatioBlue(originColor)));
+  }
+
+  mediump float colorRate = max(debugColorRateRed, max(debugColorRateGreen, debugColorRateBlue));
+  mediump vec3 debugColor = vec3(debugColorRateRed, debugColorRateGreen, debugColorRateBlue);
+
+  debugColor *= mix(1.0, originColor.a, preMultipliedAlpha);
+
+  return originColor.rgb * (1.0 - colorRate) + debugColor;
+}
+#endif
+
 void main()
 {
-#if ATLAS_DEFAULT_WARP
+#ifdef ATLAS_DEFAULT_WARP
   mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );
-#elif ATLAS_CUSTOM_WARP
+#elif defined(ATLAS_CUSTOM_WARP)
   mediump vec2 texCoord = vec2( wrapCoordinate( uAtlasRect.xz, vTexCoord.x, wrapMode.x ),
                                 wrapCoordinate( uAtlasRect.yw, vTexCoord.y, wrapMode.y ) );
 #else
   mediump vec2 texCoord = vTexCoord;
 #endif
 
+#if defined(IS_REQUIRED_YUV_TO_RGB) || defined(IS_REQUIRED_UNIFIED_YUV_AND_RGB)
+  lowp vec4 textureColor = ConvertYuvToRgba(texCoord) * vec4( mixColor, 1.0 ) * uColor;
+#else
   lowp vec4 textureColor = TEXTURE( sTexture, texCoord ) * vec4( mixColor, 1.0 ) * uColor;
+#endif
 
-#if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
+#ifdef IS_REQUIRED_ALPHA_MASKING
+  mediump vec2 maskTexCoord = vMaskTexCoord;
+  maskTexCoord.y = mix(maskTexCoord.y, 1.0-maskTexCoord.y, uYFlipMaskTexture);
+  mediump float maskAlpha = TEXTURE(sMaskTexture, maskTexCoord).a;
+  textureColor.a *= maskAlpha;
+  textureColor.rgb *= mix(1.0, maskAlpha, preMultipliedAlpha);
+#endif
+
+#if defined(IS_REQUIRED_DEBUG_VISUAL_SHADER) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
+#ifndef IS_REQUIRED_DEBUG_VISUAL_SHADER
   // skip most potential calculate for performance
   if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
   {
     OUT_COLOR = textureColor;
   }
   else
+#endif
   {
     PreprocessPotential();
 #endif
 
-#if IS_REQUIRED_BORDERLINE
+#ifdef IS_REQUIRED_BORDERLINE
     textureColor = convertBorderlineColor(textureColor);
 #endif
     OUT_COLOR = textureColor;
 
-#if IS_REQUIRED_ROUNDED_CORNER
+#ifdef IS_REQUIRED_ROUNDED_CORNER
     mediump float opacity = calculateCornerOpacity();
     OUT_COLOR.a *= opacity;
     OUT_COLOR.rgb *= mix(1.0, opacity, preMultipliedAlpha);
 #endif
 
-#if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
+#if defined(IS_REQUIRED_DEBUG_VISUAL_SHADER) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
   }
 #endif
+
+#ifdef IS_REQUIRED_DEBUG_VISUAL_SHADER
+  OUT_COLOR.rgb = ApplyDebugMixColor(OUT_COLOR);
+#endif
 }