Let Anti-Alias consider scale factor
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / graphics / shaders / gradient-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 // scale factor to fit start and end position of gradient.
12 uniform mediump float uTextureCoordinateScaleFactor;
13
14 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
15 // Be used when we calculate anti-alias range near 1 pixel.
16 uniform highp vec3 uScale;
17 #endif
18
19 uniform sampler2D sTexture; // sampler1D?
20 uniform lowp vec4 uColor;
21 uniform lowp vec3 mixColor;
22 #ifdef IS_REQUIRED_BORDERLINE
23 uniform mediump float borderlineWidth;
24 uniform mediump float borderlineOffset;
25 uniform lowp vec4 borderlineColor;
26 uniform lowp vec4 uActorColor;
27 #endif
28
29 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
30 // Global values both rounded corner and borderline use
31
32 // radius of rounded corner on this quadrant
33 mediump float gRadius = 0.0;
34
35 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
36 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
37 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
38 mediump float gCenterPosition = 0.0;
39 // relative coordinate of gFragmentPosition from gCenterPosition.
40 mediump vec2 gDiff = vec2(0.0, 0.0);
41 // potential value what our algorithm use.
42 mediump float gPotential = 0.0;
43
44 // threshold of potential
45 mediump float gPotentialRange = 0.0;
46 mediump float gMaxOutlinePotential = 0.0;
47 mediump float gMinOutlinePotential = 0.0;
48 mediump float gMaxInlinePotential = 0.0;
49 mediump float gMinInlinePotential = 0.0;
50
51 void calculateCornerRadius()
52 {
53 #ifdef IS_REQUIRED_ROUNDED_CORNER
54   gRadius =
55   mix(
56     mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
57     mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
58     sign(vPosition.y) * 0.5 + 0.5
59   );
60 #endif
61 }
62
63 void calculatePosition()
64 {
65   gFragmentPosition = abs(vPosition) - vRectSize;
66   gCenterPosition = -gRadius;
67 #ifdef IS_REQUIRED_BORDERLINE
68   gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
69 #endif
70   gDiff = gFragmentPosition - gCenterPosition;
71 }
72
73 void calculatePotential()
74 {
75   gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
76 }
77
78 void setupMinMaxPotential()
79 {
80   // Set soft anti-alias range at most 10% of visual size.
81   // The range should be inverse proportion with scale of view.
82   // To avoid divid-by-zero, let we allow minimum scale value is 0.001 (0.1%)
83   gPotentialRange = min(1.0, max(vRectSize.x, vRectSize.y) * 0.2) / max(0.001, max(uScale.x, uScale.y));
84
85   gMaxOutlinePotential = gRadius + gPotentialRange;
86   gMinOutlinePotential = gRadius - gPotentialRange;
87
88 #ifdef IS_REQUIRED_BORDERLINE
89   gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
90   gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
91 #else
92   gMaxInlinePotential = gMaxOutlinePotential;
93   gMinInlinePotential = gMinOutlinePotential;
94 #endif
95
96   // reduce defect near edge of rounded corner.
97   gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y) / max(1.0, gRadius), 0.0, 1.0);
98   gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y) / max(1.0, gRadius), 0.0, 1.0);
99 }
100
101 void PreprocessPotential()
102 {
103   calculateCornerRadius();
104   calculatePosition();
105   calculatePotential();
106
107   setupMinMaxPotential();
108 }
109 #endif
110
111
112 #ifdef IS_REQUIRED_BORDERLINE
113 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
114 {
115   mediump float potential = gPotential;
116
117   // default opacity of borderline is 0.0
118   mediump float borderlineOpacity = 0.0;
119
120   // calculate borderline opacity by potential
121   if(potential > gMinInlinePotential)
122   {
123     // potential is inside borderline range.
124     borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
125
126     // Muliply borderlineWidth to resolve very thin borderline
127     borderlineOpacity *= min(1.0, borderlineWidth / gPotentialRange);
128   }
129
130   lowp vec3  borderlineColorRGB   = borderlineColor.rgb * uActorColor.rgb;
131   lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
132   // NOTE : gradient-visual is always preMultiplied.
133   borderlineColorRGB *= borderlineColorAlpha;
134
135   // Calculate inside of borderline when alpha is between (0.0  1.0). So we need to apply texture color.
136   // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code.
137   // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
138   if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
139   {
140     mediump float tCornerRadius = -gCenterPosition + gPotentialRange;
141     mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
142     mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
143     if(potential > MaxTexturelinePotential)
144     {
145       // potential is out of texture range.
146       textureColor = vec4(0.0);
147     }
148     else
149     {
150       // potential is in texture range.
151       lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
152       textureColor.a *= textureAlphaScale;
153       textureColor.rgb *= textureAlphaScale;
154     }
155
156     // NOTE : gradient-visual is always preMultiplied.
157     borderlineColorAlpha *= borderlineOpacity;
158     borderlineColorRGB *= borderlineOpacity;
159     // We use pre-multiplied color to reduce operations.
160     // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
161
162     // Manual blend operation with premultiplied colors.
163     // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a.
164     // (Final rgb * alpha) =  borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb
165     // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha)
166     // Else, return vec4((rgb*alpha) / alpha, alpha)
167
168     lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
169     lowp vec3  finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
170     return vec4(finalMultipliedRGB, finalAlpha);
171   }
172   return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
173 }
174 #endif
175
176 #ifdef IS_REQUIRED_ROUNDED_CORNER
177 mediump float calculateCornerOpacity()
178 {
179   mediump float potential = gPotential;
180
181   // default opacity is 1.0
182   mediump float opacity = 1.0;
183
184   // calculate borderline opacity by potential
185   if(potential > gMaxOutlinePotential)
186   {
187     // potential is out of borderline range. just discard here
188     discard;
189   }
190   else if(potential > gMinOutlinePotential)
191   {
192     opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
193   }
194   return opacity;
195 }
196 #endif
197
198 void main()
199 {
200 #ifdef RADIAL
201   mediump float radialTexCoord = ((length(vTexCoord) - 0.5) * uTextureCoordinateScaleFactor) + 0.5;
202   lowp vec4 textureColor = TEXTURE(sTexture, vec2(radialTexCoord, 0.5)) * vec4(mixColor, 1.0) * uColor;
203 #else
204   lowp vec4 textureColor = TEXTURE(sTexture, vec2(vTexCoord.y, 0.5)) * vec4(mixColor, 1.0) * uColor;
205 #endif
206
207 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
208   // skip most potential calculate for performance
209   if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
210   {
211     OUT_COLOR = textureColor;
212   }
213   else
214   {
215     PreprocessPotential();
216 #endif
217
218 #ifdef IS_REQUIRED_BORDERLINE
219     textureColor = convertBorderlineColor(textureColor);
220 #endif
221     OUT_COLOR = textureColor;
222
223 #ifdef IS_REQUIRED_ROUNDED_CORNER
224     mediump float opacity = calculateCornerOpacity();
225     OUT_COLOR *= opacity;
226 #endif
227
228 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
229   }
230 #endif
231 }