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