[dali_2.1.23] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / graphics / shaders / image-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 ATLAS_DEFAULT_WARP
8 #define ATLAS_DEFAULT_WARP 0
9 #endif
10 #ifndef ATLAS_CUSTOM_WARP
11 #define ATLAS_CUSTOM_WARP 0
12 #endif
13
14 INPUT mediump vec2 vTexCoord;
15 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
16 INPUT mediump vec2 vPosition;
17 INPUT mediump vec2 vRectSize;
18 INPUT mediump vec2 vOptRectSize;
19 #if IS_REQUIRED_ROUNDED_CORNER
20 INPUT mediump vec4 vCornerRadius;
21 #endif
22 #endif
23
24 uniform sampler2D sTexture;
25 #if ATLAS_DEFAULT_WARP
26 uniform mediump vec4 uAtlasRect;
27 #elif ATLAS_CUSTOM_WARP
28 // WrapMode -- 0: CLAMP; 1: REPEAT; 2: REFLECT;
29 uniform lowp vec2 wrapMode;
30 #endif
31
32 uniform lowp vec4 uColor;
33 uniform lowp vec3 mixColor;
34 uniform lowp float preMultipliedAlpha;
35 #if IS_REQUIRED_BORDERLINE
36 uniform mediump float borderlineWidth;
37 uniform mediump float borderlineOffset;
38 uniform lowp vec4 borderlineColor;
39 uniform lowp vec4 uActorColor;
40 #endif
41
42 #if ATLAS_CUSTOM_WARP
43 mediump float wrapCoordinate( mediump vec2 range, mediump float coordinate, lowp float wrap )
44 {
45   mediump float coord;
46   if( wrap > 1.5 ) /* REFLECT */
47     coord = 1.0 - abs(fract(coordinate*0.5)*2.0 - 1.0);
48   else /* warp is 0 or 1 */
49     coord = mix(coordinate, fract(coordinate), wrap);
50   return clamp(mix(range.x, range.y, coord), range.x, range.y);
51 }
52 #endif
53
54 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
55 // Global values both rounded corner and borderline use
56
57 // radius of rounded corner on this quadrant
58 mediump float gRadius = 0.0;
59
60 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
61 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
62 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
63 mediump float gCenterPosition = 0.0;
64 // relative coordinate of gFragmentPosition from gCenterPosition.
65 mediump vec2 gDiff = vec2(0.0, 0.0);
66 // potential value what our algorithm use.
67 mediump float gPotential = 0.0;
68
69 // threshold of potential
70 mediump float gPotentialRange = 0.0;
71 mediump float gMaxOutlinePotential = 0.0;
72 mediump float gMinOutlinePotential = 0.0;
73 mediump float gMaxInlinePotential = 0.0;
74 mediump float gMinInlinePotential = 0.0;
75
76 void calculateCornerRadius()
77 {
78 #if IS_REQUIRED_ROUNDED_CORNER
79   gRadius =
80   mix(
81     mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
82     mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
83     sign(vPosition.y) * 0.5 + 0.5
84   );
85 #endif
86 }
87
88 void calculatePosition()
89 {
90   gFragmentPosition = abs(vPosition) - vRectSize;
91   gCenterPosition = -gRadius;
92 #if IS_REQUIRED_BORDERLINE
93   gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
94 #endif
95   gDiff = gFragmentPosition - gCenterPosition;
96 }
97
98 void calculatePotential()
99 {
100   gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
101 }
102
103 void setupMinMaxPotential()
104 {
105   gPotentialRange = 1.0;
106
107   gMaxOutlinePotential = gRadius + gPotentialRange;
108   gMinOutlinePotential = gRadius - gPotentialRange;
109
110 #if IS_REQUIRED_BORDERLINE
111   gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
112   gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
113 #else
114   gMaxInlinePotential = gMaxOutlinePotential;
115   gMinInlinePotential = gMinOutlinePotential;
116 #endif
117
118   // reduce defect near edge of rounded corner.
119   gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
120   gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
121 }
122
123 void PreprocessPotential()
124 {
125   calculateCornerRadius();
126   calculatePosition();
127   calculatePotential();
128
129   setupMinMaxPotential();
130 }
131 #endif
132
133 #if IS_REQUIRED_BORDERLINE
134 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
135 {
136   mediump float potential = gPotential;
137
138   // default opacity of borderline is 0.0
139   mediump float borderlineOpacity = 0.0;
140
141   // calculate borderline opacity by potential
142   if(potential > gMinInlinePotential)
143   {
144     // potential is inside borderline range.
145     borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
146
147     // Muliply borderlineWidth to resolve very thin borderline
148     borderlineOpacity *= min(1.0, borderlineWidth);
149   }
150
151   lowp vec3  borderlineColorRGB   = borderlineColor.rgb * uActorColor.rgb;
152   lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
153   borderlineColorRGB *= mix(1.0, borderlineColorAlpha, preMultipliedAlpha);
154
155   // Calculate inside of borderline when alpha is between (0.0  1.0). So we need to apply texture color.
156   // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code.
157   // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
158   if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
159   {
160     mediump float tCornerRadius = -gCenterPosition;
161     mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
162     mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
163     if(potential > MaxTexturelinePotential)
164     {
165       // potential is out of texture range.
166       textureColor = vec4(0.0);
167     }
168     else
169     {
170       // potential is in texture range.
171       lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
172       textureColor.a *= textureAlphaScale;
173       textureColor.rgb *= mix(textureColor.a, textureAlphaScale, preMultipliedAlpha);
174     }
175
176     borderlineColorAlpha *= borderlineOpacity;
177     borderlineColorRGB *= mix(borderlineColorAlpha, borderlineOpacity, preMultipliedAlpha);
178     // We use pre-multiplied color to reduce operations.
179     // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
180
181     // Manual blend operation with premultiplied colors.
182     // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a.
183     // (Final rgb * alpha) =  borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb
184     // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha)
185     // Else, return vec4((rgb*alpha) / alpha, alpha)
186
187     lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
188     lowp vec3  finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
189     // TODO : Need to find some way without division
190     return vec4(finalMultipliedRGB * mix(1.0 / finalAlpha, 1.0, preMultipliedAlpha), finalAlpha);
191   }
192   return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
193 }
194 #endif
195
196 #if IS_REQUIRED_ROUNDED_CORNER
197 mediump float calculateCornerOpacity()
198 {
199   mediump float potential = gPotential;
200
201   // default opacity is 1.0
202   mediump float opacity = 1.0;
203
204   // calculate borderline opacity by potential
205   if(potential > gMaxOutlinePotential)
206   {
207     // potential is out of borderline range. just discard here
208     discard;
209   }
210   else if(potential > gMinOutlinePotential)
211   {
212     opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
213   }
214   return opacity;
215 }
216 #endif
217
218 void main()
219 {
220 #if ATLAS_DEFAULT_WARP
221   mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );
222 #elif ATLAS_CUSTOM_WARP
223   mediump vec2 texCoord = vec2( wrapCoordinate( uAtlasRect.xz, vTexCoord.x, wrapMode.x ),
224                                 wrapCoordinate( uAtlasRect.yw, vTexCoord.y, wrapMode.y ) );
225 #else
226   mediump vec2 texCoord = vTexCoord;
227 #endif
228
229   lowp vec4 textureColor = TEXTURE( sTexture, texCoord ) * vec4( mixColor, 1.0 ) * uColor;
230
231 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
232   // skip most potential calculate for performance
233   if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
234   {
235     OUT_COLOR = textureColor;
236   }
237   else
238   {
239     PreprocessPotential();
240 #endif
241
242 #if IS_REQUIRED_BORDERLINE
243     textureColor = convertBorderlineColor(textureColor);
244 #endif
245     OUT_COLOR = textureColor;
246
247 #if IS_REQUIRED_ROUNDED_CORNER
248     mediump float opacity = calculateCornerOpacity();
249     OUT_COLOR.a *= opacity;
250     OUT_COLOR.rgb *= mix(1.0, opacity, preMultipliedAlpha);
251 #endif
252
253 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
254   }
255 #endif
256 }