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