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