1 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE) || defined(IS_REQUIRED_BLUR)
2 INPUT mediump vec2 vPosition;
3 INPUT mediump vec2 vRectSize;
4 INPUT mediump vec2 vOptRectSize;
5 #ifdef IS_REQUIRED_ROUNDED_CORNER
6 INPUT mediump vec4 vCornerRadius;
10 uniform lowp vec4 uColor;
11 uniform lowp vec3 mixColor;
12 #if !defined(IS_REQUIRED_BLUR) && defined(IS_REQUIRED_BORDERLINE)
13 uniform mediump float borderlineWidth;
14 uniform mediump float borderlineOffset;
15 uniform lowp vec4 borderlineColor;
16 uniform lowp vec4 uActorColor;
18 #ifdef IS_REQUIRED_BLUR
19 uniform mediump float blurRadius;
23 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE) || defined(IS_REQUIRED_BLUR)
24 // Global values both rounded corner and borderline use
26 // radius of rounded corner on this quadrant
27 mediump float gRadius = 0.0;
29 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
30 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
31 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
32 mediump float gCenterPosition = 0.0;
33 // relative coordinate of gFragmentPosition from gCenterPosition.
34 mediump vec2 gDiff = vec2(0.0, 0.0);
35 // potential value what our algorithm use.
36 mediump float gPotential = 0.0;
38 // threshold of potential
39 mediump float gPotentialRange = 0.0;
40 mediump float gMaxOutlinePotential = 0.0;
41 mediump float gMinOutlinePotential = 0.0;
42 mediump float gMaxInlinePotential = 0.0;
43 mediump float gMinInlinePotential = 0.0;
45 void calculateCornerRadius()
47 #ifdef IS_REQUIRED_ROUNDED_CORNER
50 mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
51 mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
52 sign(vPosition.y) * 0.5 + 0.5
57 void calculatePosition()
59 gFragmentPosition = abs(vPosition) - vRectSize;
60 gCenterPosition = -gRadius;
61 #if !defined(IS_REQUIRED_BLUR) && defined(IS_REQUIRED_BORDERLINE)
62 gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
64 gDiff = gFragmentPosition - gCenterPosition;
67 void calculatePotential()
69 gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
72 void setupMinMaxPotential()
74 gPotentialRange = 1.0;
76 gMaxOutlinePotential = gRadius + gPotentialRange;
77 gMinOutlinePotential = gRadius - gPotentialRange;
79 #if !defined(IS_REQUIRED_BLUR) && defined(IS_REQUIRED_BORDERLINE)
80 gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
81 gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
83 gMaxInlinePotential = gMaxOutlinePotential;
84 gMinInlinePotential = gMinOutlinePotential;
87 // reduce defect near edge of rounded corner.
88 gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
89 gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
92 void PreprocessPotential()
94 calculateCornerRadius();
98 setupMinMaxPotential();
102 #if !defined(IS_REQUIRED_BLUR) && defined(IS_REQUIRED_BORDERLINE)
103 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
105 mediump float potential = gPotential;
107 // default opacity of borderline is 0.0
108 mediump float borderlineOpacity = 0.0;
110 // calculate borderline opacity by potential
111 if(potential > gMinInlinePotential)
113 // potential is inside borderline range.
114 borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
116 // Muliply borderlineWidth to resolve very thin borderline
117 borderlineOpacity *= min(1.0, borderlineWidth);
120 lowp vec3 borderlineColorRGB = borderlineColor.rgb * uActorColor.rgb;
121 lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
122 // NOTE : color-visual is always not preMultiplied.
124 // Calculate inside of borderline when alpha is between (0.0 1.0). So we need to apply texture color.
125 // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code.
126 // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
127 if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
129 mediump float tCornerRadius = -gCenterPosition;
130 mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
131 mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
132 if(potential > MaxTexturelinePotential)
134 // potential is out of texture range.
135 textureColor = vec4(0.0);
139 // potential is in texture range.
140 lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
141 textureColor.a *= textureAlphaScale;
142 textureColor.rgb *= textureColor.a;
145 // NOTE : color-visual is always not preMultiplied.
146 borderlineColorAlpha *= borderlineOpacity;
147 borderlineColorRGB *= borderlineColorAlpha;
148 // We use pre-multiplied color to reduce operations.
149 // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
151 // Manual blend operation with premultiplied colors.
152 // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a.
153 // (Final rgb * alpha) = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb
154 // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha)
155 // Else, return vec4((rgb*alpha) / alpha, alpha)
157 lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
158 lowp vec3 finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
159 // TODO : Need to find some way without division
160 return vec4(finalMultipliedRGB / finalAlpha, finalAlpha);
162 return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
166 #if !defined(IS_REQUIRED_BLUR) && defined(IS_REQUIRED_ROUNDED_CORNER)
167 mediump float calculateCornerOpacity()
169 mediump float potential = gPotential;
171 // default opacity is 1.0
172 mediump float opacity = 1.0;
174 // calculate borderline opacity by potential
175 if(potential > gMaxOutlinePotential)
177 // potential is out of borderline range. just discard here
180 else if(potential > gMinOutlinePotential)
182 opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
188 #ifdef IS_REQUIRED_BLUR
189 mediump float calculateBlurOpacity()
191 // Don't use borderline!
192 mediump vec2 v = gDiff;
193 mediump float cy = gRadius + blurRadius;
194 mediump float cr = gRadius + blurRadius;
196 #ifdef IS_REQUIRED_ROUNDED_CORNER
197 // This routine make perfect circle. If corner radius is not exist, we don't consider prefect circle.
198 cy = min(cy, min(vRectSize.x, vRectSize.y) - gRadius);
200 v = vec2(min(v.x, v.y), max(v.x, v.y));
203 mediump float potential = 0.0;
204 mediump float alias = min(gRadius, 1.0);
205 mediump float potentialMin = cy + gRadius - blurRadius - alias;
206 mediump float potentialMax = cy + gRadius + blurRadius + alias;
208 // move center of circles for reduce defact
209 mediump float cyDiff = min(cy, 0.2 * blurRadius);
213 mediump float diffFromBaseline = cy * v.y - (cy + cr) * v.x;
215 if(diffFromBaseline > 0.0)
217 // out of calculation bound.
220 // for anti-alias when blurRaidus = 0.0
221 mediump float heuristicBaselineScale = max(1.0 , cr * (cr + cy));
222 mediump float potentialDiff = min(alias, diffFromBaseline / heuristicBaselineScale);
223 potentialMin += potentialDiff;
224 potentialMax -= potentialDiff;
228 // get some circle centered (x, x) and radius (r = cr / cy * x)
229 // s.t. point v is on that circle
230 // highest point of that circle is (x, x + r) and potential is x + r
232 // solve (v.x - x)^2 + (v.y - x)^2 = (cr / cy * x)^2
233 #ifdef IS_REQUIRED_ROUNDED_CORNER
234 // Note : lowspec HW cannot calculate here. need to reduce numeric error
235 highp float A = (cr * cr - 2.0 * cy * cy);
236 highp float B = cy * (v.x + v.y);
237 highp float V = dot(v,v);
238 highp float D = B * B + A * V;
239 potential = V * (cr + cy) / (sqrt(D) + B);
241 // We can simplify this value cause cy = 0.8 * blurRadius, cr = 1.2 * blurRadius
242 // potential = 5.0*(sqrt(4.0*(v.x+v.y)^2 + dot(v,v)) - 2.0*(v.x+v.y));
243 // = 10.0*(v.x+v.y) * (sqrt(1.0 + (length(v) / (2.0*(v.x+v.y)))^2) - 1.0);
244 // = 10.0*(v.x+v.y) * (sqrt(1.25 - x + x^2) - 1.0);
245 // ~= 10.0*(v.x+v.y) * (0.11803399 - 0.44721360x + 0.35777088x^2 - 0.14310x^3 + O(x^5)) (Taylor series)
246 // ~= -1.0557281 * (v.x + v.y) + 2.236068 * length(v) - ~~~ (here, x <= 0.5 * (1.0 - sqrt(0.5)) < 0.1464467)
247 // Note : This simplify need cause we should use it on lowspec HW.
248 mediump float x = 0.5 * (1.0 - length(v) / (v.x + v.y));
249 potential = -1.0557281 * (v.x + v.y) + 2.236068 * length(v) + 10.0 * (v.x + v.y) * (0.35777088 - 0.14310 * x) * x * x;
253 return 1.0 - smoothstep(potentialMin, potentialMax, potential);
259 lowp vec4 targetColor = vec4(mixColor, 1.0) * uColor;
261 #if defined(IS_REQUIRED_BLUR) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
262 // skip most potential calculate for performance
263 if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
265 OUT_COLOR = targetColor;
269 PreprocessPotential();
272 #if !defined(IS_REQUIRED_BLUR) && defined(IS_REQUIRED_BORDERLINE)
273 targetColor = convertBorderlineColor(targetColor);
275 OUT_COLOR = targetColor;
277 #ifdef IS_REQUIRED_BLUR
278 mediump float opacity = calculateBlurOpacity();
279 OUT_COLOR.a *= opacity;
280 #elif defined(IS_REQUIRED_ROUNDED_CORNER)
281 mediump float opacity = calculateCornerOpacity();
282 OUT_COLOR.a *= opacity;
285 #if defined(IS_REQUIRED_BLUR) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)