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