Merge "Fix visual defect during text scroll" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / graphics / shaders / gradient-visual-shader.frag
1 #ifndef IS_REQUIRED_ROUNDED_CORNER
2 #define IS_REQUIRED_ROUNDED_CORNER 0
3 #endif
4 #ifndef IS_REQUIRED_BORDERLINE
5 #define IS_REQUIRED_BORDERLINE 0
6 #endif
7 #ifndef RADIAL
8 #define RADIAL 0
9 #endif
10
11 INPUT mediump vec2 vTexCoord;
12 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
13 INPUT mediump vec2 vPosition;
14 INPUT mediump vec2 vRectSize;
15 INPUT mediump vec2 vOptRectSize;
16 #if IS_REQUIRED_ROUNDED_CORNER
17 INPUT mediump vec4 vCornerRadius;
18 #endif
19 #endif
20
21 uniform sampler2D sTexture; // sampler1D?
22 uniform lowp vec4 uColor;
23 uniform lowp vec3 mixColor;
24 #if IS_REQUIRED_BORDERLINE
25 uniform mediump float borderlineWidth;
26 uniform mediump float borderlineOffset;
27 uniform lowp vec4 borderlineColor;
28 uniform lowp vec4 uActorColor;
29 #endif
30
31 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
32 // Global values both rounded corner and borderline use
33
34 // radius of rounded corner on this quadrant
35 mediump float gRadius = 0.0;
36
37 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
38 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
39 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
40 mediump float gCenterPosition = 0.0;
41 // relative coordinate of gFragmentPosition from gCenterPosition.
42 mediump vec2 gDiff = vec2(0.0, 0.0);
43 // potential value what our algorithm use.
44 mediump float gPotential = 0.0;
45
46 // threshold of potential
47 mediump float gPotentialRange = 0.0;
48 mediump float gMaxOutlinePotential = 0.0;
49 mediump float gMinOutlinePotential = 0.0;
50 mediump float gMaxInlinePotential = 0.0;
51 mediump float gMinInlinePotential = 0.0;
52
53 void calculateCornerRadius()
54 {
55 #if IS_REQUIRED_ROUNDED_CORNER
56   gRadius =
57   mix(
58     mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
59     mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
60     sign(vPosition.y) * 0.5 + 0.5
61   );
62 #endif
63 }
64
65 void calculatePosition()
66 {
67   gFragmentPosition = abs(vPosition) - vRectSize;
68   gCenterPosition = -gRadius;
69 #if IS_REQUIRED_BORDERLINE
70   gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
71 #endif
72   gDiff = gFragmentPosition - gCenterPosition;
73 }
74
75 void calculatePotential()
76 {
77   gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
78 }
79
80 void setupMinMaxPotential()
81 {
82   gPotentialRange = 1.0;
83
84   gMaxOutlinePotential = gRadius + gPotentialRange;
85   gMinOutlinePotential = gRadius - gPotentialRange;
86
87 #if IS_REQUIRED_BORDERLINE
88   gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
89   gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
90 #else
91   gMaxInlinePotential = gMaxOutlinePotential;
92   gMinInlinePotential = gMinOutlinePotential;
93 #endif
94
95   // reduce defect near edge of rounded corner.
96   gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
97   gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
98 }
99
100 void PreprocessPotential()
101 {
102   calculateCornerRadius();
103   calculatePosition();
104   calculatePotential();
105
106   setupMinMaxPotential();
107 }
108 #endif
109
110
111 #if IS_REQUIRED_BORDERLINE
112 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
113 {
114   mediump float potential = gPotential;
115
116   // default opacity of borderline is 0.0
117   mediump float borderlineOpacity = 0.0;
118
119   // calculate borderline opacity by potential
120   if(potential > gMinInlinePotential)
121   {
122     // potential is inside borderline range.
123     borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
124
125     // Muliply borderlineWidth to resolve very thin borderline
126     borderlineOpacity *= min(1.0, borderlineWidth);
127   }
128
129   lowp vec3  borderlineColorRGB   = borderlineColor.rgb * uActorColor.rgb;
130   lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
131   // NOTE : gradient-visual is always preMultiplied.
132   borderlineColorRGB *= borderlineColorAlpha;
133
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)
138   {
139     mediump float tCornerRadius = -gCenterPosition;
140     mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
141     mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
142     if(potential > MaxTexturelinePotential)
143     {
144       // potential is out of texture range.
145       textureColor = vec4(0.0);
146     }
147     else
148     {
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 *= textureAlphaScale;
153     }
154
155     // NOTE : gradient-visual is always preMultiplied.
156     borderlineColorAlpha *= borderlineOpacity;
157     borderlineColorRGB *= borderlineOpacity;
158     // We use pre-multiplied color to reduce operations.
159     // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
160
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)
166
167     lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
168     lowp vec3  finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
169     return vec4(finalMultipliedRGB, finalAlpha);
170   }
171   return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
172 }
173 #endif
174
175 #if IS_REQUIRED_ROUNDED_CORNER
176 mediump float calculateCornerOpacity()
177 {
178   mediump float potential = gPotential;
179
180   // default opacity is 1.0
181   mediump float opacity = 1.0;
182
183   // calculate borderline opacity by potential
184   if(potential > gMaxOutlinePotential)
185   {
186     // potential is out of borderline range. just discard here
187     discard;
188   }
189   else if(potential > gMinOutlinePotential)
190   {
191     opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
192   }
193   return opacity;
194 }
195 #endif
196
197 void main()
198 {
199 #if RADIAL
200   lowp vec4 textureColor = TEXTURE(sTexture, vec2(length(vTexCoord), 0.5)) * vec4(mixColor, 1.0) * uColor;
201 #else
202   lowp vec4 textureColor = TEXTURE(sTexture, vec2(vTexCoord.y, 0.5)) * vec4(mixColor, 1.0) * uColor;
203 #endif
204
205 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
206   // skip most potential calculate for performance
207   if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
208   {
209     OUT_COLOR = textureColor;
210   }
211   else
212   {
213     PreprocessPotential();
214 #endif
215
216 #if IS_REQUIRED_BORDERLINE
217     textureColor = convertBorderlineColor(textureColor);
218 #endif
219     OUT_COLOR = textureColor;
220
221 #if IS_REQUIRED_ROUNDED_CORNER
222     mediump float opacity = calculateCornerOpacity();
223     OUT_COLOR *= opacity;
224 #endif
225
226 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
227   }
228 #endif
229 }