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