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