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