Let CornerRadius / Borderline works on very small visual
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / graphics / shaders / image-visual-shader.frag
1 INPUT mediump vec2 vTexCoord;
2 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
3 INPUT mediump vec2 vPosition;
4 INPUT mediump vec2 vRectSize;
5 INPUT mediump vec2 vOptRectSize;
6 #ifdef IS_REQUIRED_ROUNDED_CORNER
7 INPUT mediump vec4 vCornerRadius;
8 #endif
9 #endif
10
11 uniform sampler2D sTexture;
12 #ifdef IS_REQUIRED_YUV_TO_RGB
13 uniform sampler2D sTextureU;
14 uniform sampler2D sTextureV;
15 #endif
16
17 #ifdef IS_REQUIRED_ALPHA_MASKING
18 uniform sampler2D sMaskTexture;
19 INPUT mediump vec2 vMaskTexCoord;
20 #endif
21
22 #ifdef ATLAS_DEFAULT_WARP
23 uniform mediump vec4 uAtlasRect;
24 #elif defined(ATLAS_CUSTOM_WARP)
25 // WrapMode -- 0: CLAMP; 1: REPEAT; 2: REFLECT;
26 uniform lowp vec2 wrapMode;
27 #endif
28
29 uniform lowp vec4 uColor;
30 uniform lowp vec3 mixColor;
31 uniform lowp float preMultipliedAlpha;
32 #ifdef IS_REQUIRED_BORDERLINE
33 uniform mediump float borderlineWidth;
34 uniform mediump float borderlineOffset;
35 uniform lowp vec4 borderlineColor;
36 uniform lowp vec4 uActorColor;
37 #endif
38
39 #ifdef ATLAS_CUSTOM_WARP
40 mediump float wrapCoordinate( mediump vec2 range, mediump float coordinate, lowp float wrap )
41 {
42   mediump float coord;
43   if( wrap > 1.5 ) /* REFLECT */
44     coord = 1.0 - abs(fract(coordinate*0.5)*2.0 - 1.0);
45   else /* warp is 0 or 1 */
46     coord = mix(coordinate, fract(coordinate), wrap);
47   return clamp(mix(range.x, range.y, coord), range.x, range.y);
48 }
49 #endif
50
51 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
52 // Global values both rounded corner and borderline use
53
54 // radius of rounded corner on this quadrant
55 mediump float gRadius = 0.0;
56
57 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
58 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
59 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
60 mediump float gCenterPosition = 0.0;
61 // relative coordinate of gFragmentPosition from gCenterPosition.
62 mediump vec2 gDiff = vec2(0.0, 0.0);
63 // potential value what our algorithm use.
64 mediump float gPotential = 0.0;
65
66 // threshold of potential
67 mediump float gPotentialRange = 0.0;
68 mediump float gMaxOutlinePotential = 0.0;
69 mediump float gMinOutlinePotential = 0.0;
70 mediump float gMaxInlinePotential = 0.0;
71 mediump float gMinInlinePotential = 0.0;
72
73 void calculateCornerRadius()
74 {
75 #ifdef IS_REQUIRED_ROUNDED_CORNER
76   gRadius =
77   mix(
78     mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
79     mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
80     sign(vPosition.y) * 0.5 + 0.5
81   );
82 #endif
83 }
84
85 void calculatePosition()
86 {
87   gFragmentPosition = abs(vPosition) - vRectSize;
88   gCenterPosition = -gRadius;
89 #ifdef IS_REQUIRED_BORDERLINE
90   gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
91 #endif
92   gDiff = gFragmentPosition - gCenterPosition;
93 }
94
95 void calculatePotential()
96 {
97   gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
98 }
99
100 void setupMinMaxPotential()
101 {
102   // Set soft anti-alias range at most 2% of visual size
103   gPotentialRange = min(1.0, max(vRectSize.x, vRectSize.y) * 0.02);
104
105   gMaxOutlinePotential = gRadius + gPotentialRange;
106   gMinOutlinePotential = gRadius - gPotentialRange;
107
108 #ifdef IS_REQUIRED_BORDERLINE
109   gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
110   gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
111 #else
112   gMaxInlinePotential = gMaxOutlinePotential;
113   gMinInlinePotential = gMinOutlinePotential;
114 #endif
115
116   // reduce defect near edge of rounded corner.
117   gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
118   gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
119 }
120
121 void PreprocessPotential()
122 {
123   calculateCornerRadius();
124   calculatePosition();
125   calculatePotential();
126
127   setupMinMaxPotential();
128 }
129 #endif
130
131 #ifdef IS_REQUIRED_BORDERLINE
132 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
133 {
134   mediump float potential = gPotential;
135
136   // default opacity of borderline is 0.0
137   mediump float borderlineOpacity = 0.0;
138
139   // calculate borderline opacity by potential
140   if(potential > gMinInlinePotential)
141   {
142     // potential is inside borderline range.
143     borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
144
145     // Muliply borderlineWidth to resolve very thin borderline
146     borderlineOpacity *= min(1.0, borderlineWidth / gPotentialRange);
147   }
148
149   lowp vec3  borderlineColorRGB   = borderlineColor.rgb * uActorColor.rgb;
150   lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
151   borderlineColorRGB *= mix(1.0, borderlineColorAlpha, preMultipliedAlpha);
152
153   // Calculate inside of borderline when alpha is between (0.0  1.0). So we need to apply texture color.
154   // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code.
155   // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
156   if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
157   {
158     mediump float tCornerRadius = -gCenterPosition + gPotentialRange;
159     mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
160     mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
161     if(potential > MaxTexturelinePotential)
162     {
163       // potential is out of texture range.
164       textureColor = vec4(0.0);
165     }
166     else
167     {
168       // potential is in texture range.
169       lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
170       textureColor.a *= textureAlphaScale;
171       textureColor.rgb *= mix(textureColor.a, textureAlphaScale, preMultipliedAlpha);
172     }
173
174     borderlineColorAlpha *= borderlineOpacity;
175     borderlineColorRGB *= mix(borderlineColorAlpha, borderlineOpacity, preMultipliedAlpha);
176     // We use pre-multiplied color to reduce operations.
177     // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
178
179     // Manual blend operation with premultiplied colors.
180     // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a.
181     // (Final rgb * alpha) =  borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb
182     // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha)
183     // Else, return vec4((rgb*alpha) / alpha, alpha)
184
185     lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
186     lowp vec3  finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
187     // TODO : Need to find some way without division
188     return vec4(finalMultipliedRGB * mix(1.0 / finalAlpha, 1.0, preMultipliedAlpha), finalAlpha);
189   }
190   return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
191 }
192 #endif
193
194 #ifdef IS_REQUIRED_ROUNDED_CORNER
195 mediump float calculateCornerOpacity()
196 {
197   mediump float potential = gPotential;
198
199   // default opacity is 1.0
200   mediump float opacity = 1.0;
201
202   // calculate borderline opacity by potential
203   if(potential > gMaxOutlinePotential)
204   {
205     // potential is out of borderline range. just discard here
206     discard;
207   }
208   else if(potential > gMinOutlinePotential)
209   {
210     opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
211   }
212   return opacity;
213 }
214 #endif
215
216 #ifdef IS_REQUIRED_YUV_TO_RGB
217 lowp vec3 ConvertYuvToRgb(mediump vec2 texCoord)
218 {
219   lowp float y = texture(sTexture, texCoord).r;
220   lowp float u = texture(sTextureU, texCoord).r - 0.5;
221   lowp float v = texture(sTextureV, texCoord).r - 0.5;
222   lowp vec3 rgb;
223   rgb.r = y + (1.403 * v);
224   rgb.g = y - (0.344 * u) - (0.714 * v);
225   rgb.b = y + (1.770 * u);
226   return rgb;
227 }
228 #endif
229
230 void main()
231 {
232 #ifdef ATLAS_DEFAULT_WARP
233   mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );
234 #elif defined(ATLAS_CUSTOM_WARP)
235   mediump vec2 texCoord = vec2( wrapCoordinate( uAtlasRect.xz, vTexCoord.x, wrapMode.x ),
236                                 wrapCoordinate( uAtlasRect.yw, vTexCoord.y, wrapMode.y ) );
237 #else
238   mediump vec2 texCoord = vTexCoord;
239 #endif
240
241 #ifdef IS_REQUIRED_YUV_TO_RGB
242   lowp vec4 textureColor = vec4(ConvertYuvToRgb(texCoord), 1.0) * vec4( mixColor, 1.0 ) * uColor;
243 #else
244   lowp vec4 textureColor = TEXTURE( sTexture, texCoord ) * vec4( mixColor, 1.0 ) * uColor;
245 #endif
246
247 #ifdef IS_REQUIRED_ALPHA_MASKING
248   mediump float maskAlpha = TEXTURE(sMaskTexture, vMaskTexCoord).a;
249   textureColor.a *= maskAlpha;
250   textureColor.rgb *= mix(1.0, maskAlpha, preMultipliedAlpha);
251 #endif
252
253 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
254   // skip most potential calculate for performance
255   if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
256   {
257     OUT_COLOR = textureColor;
258   }
259   else
260   {
261     PreprocessPotential();
262 #endif
263
264 #ifdef IS_REQUIRED_BORDERLINE
265     textureColor = convertBorderlineColor(textureColor);
266 #endif
267     OUT_COLOR = textureColor;
268
269 #ifdef IS_REQUIRED_ROUNDED_CORNER
270     mediump float opacity = calculateCornerOpacity();
271     OUT_COLOR.a *= opacity;
272     OUT_COLOR.rgb *= mix(1.0, opacity, preMultipliedAlpha);
273 #endif
274
275 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
276   }
277 #endif
278 }