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