[dali_2.1.30] Merge branch 'devel/master'
[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   gPotentialRange = 1.0;
73
74   gMaxOutlinePotential = gRadius + gPotentialRange;
75   gMinOutlinePotential = gRadius - gPotentialRange;
76
77 #ifdef IS_REQUIRED_BORDERLINE
78   gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
79   gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
80 #else
81   gMaxInlinePotential = gMaxOutlinePotential;
82   gMinInlinePotential = gMinOutlinePotential;
83 #endif
84
85   // reduce defect near edge of rounded corner.
86   gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
87   gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
88 }
89
90 void PreprocessPotential()
91 {
92   calculateCornerRadius();
93   calculatePosition();
94   calculatePotential();
95
96   setupMinMaxPotential();
97 }
98 #endif
99
100
101 #ifdef IS_REQUIRED_BORDERLINE
102 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
103 {
104   mediump float potential = gPotential;
105
106   // default opacity of borderline is 0.0
107   mediump float borderlineOpacity = 0.0;
108
109   // calculate borderline opacity by potential
110   if(potential > gMinInlinePotential)
111   {
112     // potential is inside borderline range.
113     borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
114
115     // Muliply borderlineWidth to resolve very thin borderline
116     borderlineOpacity *= min(1.0, borderlineWidth);
117   }
118
119   lowp vec3  borderlineColorRGB   = borderlineColor.rgb * uActorColor.rgb;
120   lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
121   // NOTE : gradient-visual is always preMultiplied.
122   borderlineColorRGB *= borderlineColorAlpha;
123
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)
128   {
129     mediump float tCornerRadius = -gCenterPosition + gPotentialRange;
130     mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
131     mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
132     if(potential > MaxTexturelinePotential)
133     {
134       // potential is out of texture range.
135       textureColor = vec4(0.0);
136     }
137     else
138     {
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 *= textureAlphaScale;
143     }
144
145     // NOTE : gradient-visual is always preMultiplied.
146     borderlineColorAlpha *= borderlineOpacity;
147     borderlineColorRGB *= borderlineOpacity;
148     // We use pre-multiplied color to reduce operations.
149     // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
150
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)
156
157     lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
158     lowp vec3  finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
159     return vec4(finalMultipliedRGB, finalAlpha);
160   }
161   return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
162 }
163 #endif
164
165 #ifdef IS_REQUIRED_ROUNDED_CORNER
166 mediump float calculateCornerOpacity()
167 {
168   mediump float potential = gPotential;
169
170   // default opacity is 1.0
171   mediump float opacity = 1.0;
172
173   // calculate borderline opacity by potential
174   if(potential > gMaxOutlinePotential)
175   {
176     // potential is out of borderline range. just discard here
177     discard;
178   }
179   else if(potential > gMinOutlinePotential)
180   {
181     opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
182   }
183   return opacity;
184 }
185 #endif
186
187 void main()
188 {
189 #ifdef RADIAL
190   lowp vec4 textureColor = TEXTURE(sTexture, vec2(length(vTexCoord), 0.5)) * vec4(mixColor, 1.0) * uColor;
191 #else
192   lowp vec4 textureColor = TEXTURE(sTexture, vec2(vTexCoord.y, 0.5)) * vec4(mixColor, 1.0) * uColor;
193 #endif
194
195 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
196   // skip most potential calculate for performance
197   if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
198   {
199     OUT_COLOR = textureColor;
200   }
201   else
202   {
203     PreprocessPotential();
204 #endif
205
206 #ifdef IS_REQUIRED_BORDERLINE
207     textureColor = convertBorderlineColor(textureColor);
208 #endif
209     OUT_COLOR = textureColor;
210
211 #ifdef IS_REQUIRED_ROUNDED_CORNER
212     mediump float opacity = calculateCornerOpacity();
213     OUT_COLOR *= opacity;
214 #endif
215
216 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
217   }
218 #endif
219 }