[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / graphics / shaders / color-visual-shader.frag
1 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE) || defined(IS_REQUIRED_BLUR)
2 INPUT highp vec2 vPosition;
3 INPUT highp vec2 vRectSize;
4 INPUT highp vec2 vOptRectSize;
5 INPUT highp float vAliasMargin;
6 #ifdef IS_REQUIRED_ROUNDED_CORNER
7 INPUT highp vec4 vCornerRadius;
8 #endif
9 #endif
10
11 uniform lowp vec4 uColor;
12 uniform lowp vec3 mixColor;
13 #ifdef IS_REQUIRED_BLUR
14 uniform highp float blurRadius;
15 #elif defined(IS_REQUIRED_BORDERLINE)
16 uniform highp float borderlineWidth;
17 uniform highp float borderlineOffset;
18 uniform lowp vec4 borderlineColor;
19 uniform lowp vec4 uActorColor;
20 #endif
21
22 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE) || defined(IS_REQUIRED_BLUR)
23 // Global values both rounded corner and borderline use
24
25 // radius of rounded corner on this quadrant
26 highp float gRadius = 0.0;
27
28 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
29 highp vec2 gFragmentPosition = vec2(0.0, 0.0);
30 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
31 highp float gCenterPosition = 0.0;
32 // relative coordinate of gFragmentPosition from gCenterPosition.
33 highp vec2 gDiff = vec2(0.0, 0.0);
34 // potential value what our algorithm use.
35 highp float gPotential = 0.0;
36
37 // threshold of potential
38 highp float gPotentialRange = 0.0;
39 highp float gMaxOutlinePotential = 0.0;
40 highp float gMinOutlinePotential = 0.0;
41 highp float gMaxInlinePotential = 0.0;
42 highp float gMinInlinePotential = 0.0;
43
44 void calculateCornerRadius()
45 {
46 #ifdef IS_REQUIRED_ROUNDED_CORNER
47   gRadius =
48   mix(
49     mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
50     mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
51     sign(vPosition.y) * 0.5 + 0.5
52   );
53 #endif
54 }
55
56 void calculatePosition()
57 {
58   gFragmentPosition = abs(vPosition) - vRectSize;
59   gCenterPosition = -gRadius;
60 #ifdef IS_REQUIRED_BLUR
61 #elif defined(IS_REQUIRED_BORDERLINE)
62   gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
63 #endif
64   gDiff = gFragmentPosition - gCenterPosition;
65 }
66
67 void calculatePotential()
68 {
69   gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
70 }
71
72 void setupMinMaxPotential()
73 {
74   gPotentialRange = vAliasMargin;
75
76   gMaxOutlinePotential = gRadius + gPotentialRange;
77   gMinOutlinePotential = gRadius - gPotentialRange;
78
79 #ifdef IS_REQUIRED_BLUR
80   gMaxInlinePotential = gMaxOutlinePotential;
81   gMinInlinePotential = gMinOutlinePotential;
82 #elif defined(IS_REQUIRED_BORDERLINE)
83   gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
84   gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
85 #else
86   gMaxInlinePotential = gMaxOutlinePotential;
87   gMinInlinePotential = gMinOutlinePotential;
88 #endif
89
90   // reduce defect near edge of rounded corner.
91   gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y) / max(1.0, gRadius), 0.0, 1.0);
92   gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y) / max(1.0, gRadius), 0.0, 1.0);
93 }
94
95 void PreprocessPotential()
96 {
97   calculateCornerRadius();
98   calculatePosition();
99   calculatePotential();
100
101   setupMinMaxPotential();
102 }
103 #endif
104
105 #ifdef IS_REQUIRED_BLUR
106 #elif defined(IS_REQUIRED_BORDERLINE)
107 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
108 {
109   highp float potential = gPotential;
110
111   // default opacity of borderline is 0.0
112   mediump float borderlineOpacity = 0.0;
113
114   // calculate borderline opacity by potential
115   if(potential > gMinInlinePotential)
116   {
117     // potential is inside borderline range.
118     borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
119
120     // Muliply borderlineWidth to resolve very thin borderline
121     borderlineOpacity *= min(1.0, borderlineWidth / gPotentialRange);
122   }
123
124   lowp vec3  borderlineColorRGB   = borderlineColor.rgb * uActorColor.rgb;
125   lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
126   // NOTE : color-visual is always not preMultiplied.
127
128   // Calculate inside of borderline when alpha is between (0.0  1.0). So we need to apply texture color.
129   // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code.
130   // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
131   if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
132   {
133     highp float tCornerRadius = -gCenterPosition + gPotentialRange;
134     highp float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
135     highp float MinTexturelinePotential = tCornerRadius - gPotentialRange;
136     if(potential > MaxTexturelinePotential)
137     {
138       // potential is out of texture range.
139       textureColor = vec4(0.0);
140     }
141     else
142     {
143       // potential is in texture range.
144       lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
145       textureColor.a *= textureAlphaScale;
146       textureColor.rgb *= textureColor.a;
147     }
148
149     // NOTE : color-visual is always not preMultiplied.
150     borderlineColorAlpha *= borderlineOpacity;
151     borderlineColorRGB *= borderlineColorAlpha;
152     // We use pre-multiplied color to reduce operations.
153     // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
154
155     // Manual blend operation with premultiplied colors.
156     // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a.
157     // (Final rgb * alpha) =  borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb
158     // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha)
159     // Else, return vec4((rgb*alpha) / alpha, alpha)
160
161     lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
162     lowp vec3  finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
163     // TODO : Need to find some way without division
164     return vec4(finalMultipliedRGB / finalAlpha, finalAlpha);
165   }
166   return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
167 }
168 #endif
169
170 #ifdef IS_REQUIRED_BLUR
171 #elif defined(IS_REQUIRED_ROUNDED_CORNER)
172 mediump float calculateCornerOpacity()
173 {
174   highp float potential = gPotential;
175
176   // default opacity is 1.0
177   mediump float opacity = 1.0;
178
179   // calculate borderline opacity by potential
180   if(potential > gMaxOutlinePotential)
181   {
182     // potential is out of borderline range. just discard here
183     discard;
184   }
185   else if(potential > gMinOutlinePotential)
186   {
187     opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
188   }
189   return opacity;
190 }
191 #endif
192
193 #ifdef IS_REQUIRED_BLUR
194 #ifdef SL_VERSION_LOW
195 // Legacy code for low version glsl
196 mediump float calculateBlurOpacity()
197 {
198   highp float potential = gPotential;
199
200   highp float alias = min(gRadius, vAliasMargin);
201   highp float potentialMin = gMinOutlinePotential - blurRadius - alias;
202   highp float potentialMax = gMaxOutlinePotential + blurRadius + alias;
203
204   return 1.0 - smoothstep(potentialMin, potentialMax, potential);
205 }
206 #else
207 mediump float calculateBlurOpacity()
208 {
209 // Don't use borderline!
210   highp vec2 v = gDiff;
211   highp float cy = gRadius + blurRadius;
212   highp float cr = gRadius + blurRadius;
213
214 #ifdef IS_REQUIRED_ROUNDED_CORNER
215   // This routine make perfect circle. If corner radius is not exist, we don't consider prefect circle.
216   cy = min(cy, min(vRectSize.x, vRectSize.y) - gRadius);
217 #endif
218   v = vec2(min(v.x, v.y), max(v.x, v.y));
219   v = v + cy;
220
221   highp float potential = 0.0;
222   highp float alias = min(gRadius, vAliasMargin);
223   highp float potentialMin = cy + gRadius - blurRadius - alias;
224   highp float potentialMax = cy + gRadius + blurRadius + alias;
225
226   // move center of circles for reduce defact
227   highp float cyDiff = min(cy, 0.2 * blurRadius);
228   cy -= cyDiff;
229   cr += cyDiff;
230
231   highp float diffFromBaseline = cy * v.y - (cy + cr) * v.x;
232
233   if(diffFromBaseline > 0.0)
234   {
235     // out of calculation bound.
236     potential = v.y;
237
238     // for anti-alias when blurRaidus = 0.0
239     highp float heuristicBaselineScale = max(vAliasMargin , cr * (cr + cy));
240     highp float potentialDiff = min(alias, diffFromBaseline / heuristicBaselineScale);
241     potentialMin += potentialDiff;
242     potentialMax -= potentialDiff;
243   }
244   else
245   {
246     // get some circle centered (x, x) and radius (r = cr / cy * x)
247     // s.t. point v is on that circle
248     // highest point of that circle is (x, x + r) and potential is x + r
249
250     // solve (v.x - x)^2 + (v.y - x)^2 = (cr / cy * x)^2
251 #ifdef IS_REQUIRED_ROUNDED_CORNER
252     // Note : lowspec HW cannot calculate here. need to reduce numeric error
253     highp float A = (cr * cr - 2.0 * cy * cy);
254     highp float B = cy * (v.x + v.y);
255     highp float V = dot(v,v);
256     highp float D = B * B + A * V;
257     potential = V * (cr + cy) / (sqrt(D) + B);
258 #else
259     // We can simplify this value cause cy = 0.8 * blurRadius, cr = 1.2 * blurRadius
260     // potential = 5.0*(sqrt(4.0*(v.x+v.y)^2 + dot(v,v)) - 2.0*(v.x+v.y));
261     //           = 10.0*(v.x+v.y) * (sqrt(1.0 + (length(v) / (2.0*(v.x+v.y)))^2) - 1.0);
262     //           = 10.0*(v.x+v.y) * (sqrt(1.25 - x + x^2) - 1.0);
263     //          ~= 10.0*(v.x+v.y) * (0.11803399 - 0.44721360x + 0.35777088x^2 - 0.14310x^3 + O(x^5)) (Taylor series)
264     //          ~= -1.0557281 * (v.x + v.y) + 2.236068 * length(v) - ~~~ (here, x <= 0.5 * (1.0 - sqrt(0.5)) < 0.1464467)
265     // Note : This simplify need cause we should use it on lowspec HW.
266     highp float x = 0.5 * (1.0 - length(v) / (v.x + v.y));
267     potential = -1.0557281 * (v.x + v.y) + 2.236068 * length(v) + 10.0 * (v.x + v.y) * (0.35777088 - 0.14310 * x) * x * x;
268 #endif
269   }
270
271   return 1.0 - smoothstep(potentialMin, potentialMax, potential);
272 }
273 #endif
274 #endif
275
276 void main()
277 {
278   lowp vec4 targetColor = vec4(mixColor, 1.0) * uColor;
279
280 #if defined(IS_REQUIRED_BLUR) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
281   // skip most potential calculate for performance
282   if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
283   {
284     OUT_COLOR = targetColor;
285   }
286   else
287   {
288     PreprocessPotential();
289 #endif
290
291 #ifdef IS_REQUIRED_BLUR
292 #elif defined(IS_REQUIRED_BORDERLINE)
293     targetColor = convertBorderlineColor(targetColor);
294 #endif
295     OUT_COLOR = targetColor;
296
297 #ifdef IS_REQUIRED_BLUR
298     mediump float opacity = calculateBlurOpacity();
299     OUT_COLOR.a *= opacity;
300 #elif defined(IS_REQUIRED_ROUNDED_CORNER)
301     mediump float opacity = calculateCornerOpacity();
302     OUT_COLOR.a *= opacity;
303 #endif
304
305 #if defined(IS_REQUIRED_BLUR) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
306   }
307 #endif
308 }