1 #ifndef IS_REQUIRED_ROUNDED_CORNER
2 #define IS_REQUIRED_ROUNDED_CORNER 0
4 #ifndef IS_REQUIRED_BORDERLINE
5 #define IS_REQUIRED_BORDERLINE 0
7 #ifndef IS_REQUIRED_BLUR
8 #define IS_REQUIRED_BLUR 0
11 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE || IS_REQUIRED_BLUR
12 INPUT mediump vec2 vPosition;
13 INPUT mediump vec2 vRectSize;
14 INPUT mediump vec2 vOptRectSize;
15 #if IS_REQUIRED_ROUNDED_CORNER
16 INPUT mediump vec4 vCornerRadius;
20 uniform lowp vec4 uColor;
21 uniform lowp vec3 mixColor;
22 #if !IS_REQUIRED_BLUR && IS_REQUIRED_BORDERLINE
23 uniform mediump float borderlineWidth;
24 uniform mediump float borderlineOffset;
25 uniform lowp vec4 borderlineColor;
26 uniform lowp vec4 uActorColor;
29 uniform mediump float blurRadius;
33 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE || IS_REQUIRED_BLUR
34 // Global values both rounded corner and borderline use
36 // radius of rounded corner on this quadrant
37 mediump float gRadius = 0.0;
39 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
40 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
41 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
42 mediump float gCenterPosition = 0.0;
43 // relative coordinate of gFragmentPosition from gCenterPosition.
44 mediump vec2 gDiff = vec2(0.0, 0.0);
45 // potential value what our algorithm use.
46 mediump float gPotential = 0.0;
48 // threshold of potential
49 mediump float gPotentialRange = 0.0;
50 mediump float gMaxOutlinePotential = 0.0;
51 mediump float gMinOutlinePotential = 0.0;
52 mediump float gMaxInlinePotential = 0.0;
53 mediump float gMinInlinePotential = 0.0;
55 void calculateCornerRadius()
57 #if IS_REQUIRED_ROUNDED_CORNER
60 mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
61 mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
62 sign(vPosition.y) * 0.5 + 0.5
67 void calculatePosition()
69 gFragmentPosition = abs(vPosition) - vRectSize;
70 gCenterPosition = -gRadius;
71 #if !IS_REQUIRED_BLUR && IS_REQUIRED_BORDERLINE
72 gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
74 gDiff = gFragmentPosition - gCenterPosition;
77 void calculatePotential()
79 gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
82 void setupMinMaxPotential()
84 gPotentialRange = 1.0;
86 gMaxOutlinePotential = gRadius + gPotentialRange;
87 gMinOutlinePotential = gRadius - gPotentialRange;
89 #if !IS_REQUIRED_BLUR && IS_REQUIRED_BORDERLINE
90 gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
91 gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
93 gMaxInlinePotential = gMaxOutlinePotential;
94 gMinInlinePotential = gMinOutlinePotential;
97 // reduce defect near edge of rounded corner.
98 gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
99 gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
102 void PreprocessPotential()
104 calculateCornerRadius();
106 calculatePotential();
108 setupMinMaxPotential();
112 #if !IS_REQUIRED_BLUR && IS_REQUIRED_BORDERLINE
113 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
115 mediump float potential = gPotential;
117 // default opacity of borderline is 0.0
118 mediump float borderlineOpacity = 0.0;
120 // calculate borderline opacity by potential
121 if(potential > gMinInlinePotential)
123 // potential is inside borderline range.
124 borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
126 // Muliply borderlineWidth to resolve very thin borderline
127 borderlineOpacity *= min(1.0, borderlineWidth);
130 lowp vec3 borderlineColorRGB = borderlineColor.rgb * uActorColor.rgb;
131 lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
132 // NOTE : color-visual is always not preMultiplied.
134 // Calculate inside of borderline when alpha is between (0.0 1.0). So we need to apply texture color.
135 // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code.
136 // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
137 if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
139 mediump float tCornerRadius = -gCenterPosition;
140 mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
141 mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
142 if(potential > MaxTexturelinePotential)
144 // potential is out of texture range.
145 textureColor = vec4(0.0);
149 // potential is in texture range.
150 lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
151 textureColor.a *= textureAlphaScale;
152 textureColor.rgb *= textureColor.a;
155 // NOTE : color-visual is always not preMultiplied.
156 borderlineColorAlpha *= borderlineOpacity;
157 borderlineColorRGB *= borderlineColorAlpha;
158 // We use pre-multiplied color to reduce operations.
159 // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
161 // Manual blend operation with premultiplied colors.
162 // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a.
163 // (Final rgb * alpha) = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb
164 // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha)
165 // Else, return vec4((rgb*alpha) / alpha, alpha)
167 lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
168 lowp vec3 finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
169 // TODO : Need to find some way without division
170 return vec4(finalMultipliedRGB / finalAlpha, finalAlpha);
172 return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
176 #if !IS_REQUIRED_BLUR && IS_REQUIRED_ROUNDED_CORNER
177 mediump float calculateCornerOpacity()
179 mediump float potential = gPotential;
181 // default opacity is 1.0
182 mediump float opacity = 1.0;
184 // calculate borderline opacity by potential
185 if(potential > gMaxOutlinePotential)
187 // potential is out of borderline range. just discard here
190 else if(potential > gMinOutlinePotential)
192 opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
199 mediump float calculateBlurOpacity()
201 // Don't use borderline!
202 mediump vec2 v = gDiff;
203 mediump float cy = gRadius + blurRadius;
204 mediump float cr = gRadius + blurRadius;
206 #if IS_REQUIRED_ROUNDED_CORNER
207 // This routine make perfect circle. If corner radius is not exist, we don't consider prefect circle.
208 cy = min(cy, min(vRectSize.x, vRectSize.y) - gRadius);
210 v = vec2(min(v.x, v.y), max(v.x, v.y));
213 mediump float potential = 0.0;
214 mediump float alias = min(gRadius, 1.0);
215 mediump float potentialMin = cy + gRadius - blurRadius - alias;
216 mediump float potentialMax = cy + gRadius + blurRadius + alias;
218 // move center of circles for reduce defact
219 mediump float cyDiff = min(cy, 0.2 * blurRadius);
223 mediump float diffFromBaseline = cy * v.y - (cy + cr) * v.x;
225 if(diffFromBaseline > 0.0)
227 // out of calculation bound.
230 // for anti-alias when blurRaidus = 0.0
231 mediump float heuristicBaselineScale = max(1.0 , cr * (cr + cy));
232 mediump float potentialDiff = min(alias, diffFromBaseline / heuristicBaselineScale);
233 potentialMin += potentialDiff;
234 potentialMax -= potentialDiff;
238 // get some circle centered (x, x) and radius (r = cr / cy * x)
239 // s.t. point v is on that circle
240 // highest point of that circle is (x, x + r) and potential is x + r
242 // solve (v.x - x)^2 + (v.y - x)^2 = (cr / cy * x)^2
243 #if IS_REQUIRED_ROUNDED_CORNER
244 // Note : lowspec HW cannot calculate here. need to reduce numeric error
245 highp float A = (cr * cr - 2.0 * cy * cy);
246 highp float B = cy * (v.x + v.y);
247 highp float V = dot(v,v);
248 highp float D = B * B + A * V;
249 potential = V * (cr + cy) / (sqrt(D) + B);
251 // We can simplify this value cause cy = 0.8 * blurRadius, cr = 1.2 * blurRadius
252 // potential = 5.0*(sqrt(4.0*(v.x+v.y)^2 + dot(v,v)) - 2.0*(v.x+v.y));
253 // = 10.0*(v.x+v.y) * (sqrt(1.0 + (length(v) / (2.0*(v.x+v.y)))^2) - 1.0);
254 // = 10.0*(v.x+v.y) * (sqrt(1.25 - x + x^2) - 1.0);
255 // ~= 10.0*(v.x+v.y) * (0.11803399 - 0.44721360x + 0.35777088x^2 - 0.14310x^3 + O(x^5)) (Taylor series)
256 // ~= -1.0557281 * (v.x + v.y) + 2.236068 * length(v) - ~~~ (here, x <= 0.5 * (1.0 - sqrt(0.5)) < 0.1464467)
257 // Note : This simplify need cause we should use it on lowspec HW.
258 mediump float x = 0.5 * (1.0 - length(v) / (v.x + v.y));
259 potential = -1.0557281 * (v.x + v.y) + 2.236068 * length(v) + 10.0 * (v.x + v.y) * (0.35777088 - 0.14310 * x) * x * x;
263 return 1.0 - smoothstep(potentialMin, potentialMax, potential);
269 lowp vec4 targetColor = vec4(mixColor, 1.0) * uColor;
271 #if IS_REQUIRED_BLUR || IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
272 // skip most potential calculate for performance
273 if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
275 OUT_COLOR = targetColor;
279 PreprocessPotential();
282 #if !IS_REQUIRED_BLUR && IS_REQUIRED_BORDERLINE
283 targetColor = convertBorderlineColor(targetColor);
285 OUT_COLOR = targetColor;
288 mediump float opacity = calculateBlurOpacity();
289 OUT_COLOR.a *= opacity;
290 #elif IS_REQUIRED_ROUNDED_CORNER
291 mediump float opacity = calculateCornerOpacity();
292 OUT_COLOR.a *= opacity;
295 #if IS_REQUIRED_BLUR || IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE