0bba311d8012508804f457bc1a441a74a4e03310
[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 #endif
40
41 #if 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 IS_REQUIRED_ROUNDED_CORNER || 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 #if 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 #if 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 = 1.0;
105
106   gMaxOutlinePotential = gRadius + gPotentialRange;
107   gMinOutlinePotential = gRadius - gPotentialRange;
108
109 #if 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 #if 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
147   //calculate inside of borderline when outilneColor.a < 1.0
148   if(borderlineColor.a < 1.0)
149   {
150     mediump float tCornerRadius = -gCenterPosition;
151     mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
152     mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
153     lowp vec3 BorderlineColorRGB = borderlineColor.xyz;
154     BorderlineColorRGB *= mix(1.0, borderlineColor.a, preMultipliedAlpha);
155     if(potential > MaxTexturelinePotential)
156     {
157       // potential is out of texture range. use borderline color instead of texture
158       textureColor = vec4(BorderlineColorRGB, 0.0);
159     }
160     else if(potential > MinTexturelinePotential)
161     {
162       // potential is in texture range
163       textureColor = mix(textureColor, vec4(BorderlineColorRGB, 0.0), smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
164     }
165     borderlineOpacity *= borderlineColor.a;
166     return mix(textureColor, vec4(BorderlineColorRGB, 1.0), borderlineOpacity);
167   }
168   return mix(textureColor, borderlineColor, borderlineOpacity);
169 }
170 #endif
171
172 #if IS_REQUIRED_ROUNDED_CORNER
173 mediump float calculateCornerOpacity()
174 {
175   mediump float potential = gPotential;
176
177   // default opacity is 1.0
178   mediump float opacity = 1.0;
179
180   // calculate borderline opacity by potential
181   if(potential > gMaxOutlinePotential)
182   {
183     // potential is out of borderline range. just discard here
184     discard;
185   }
186   else if(potential > gMinOutlinePotential)
187   {
188     opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
189   }
190   return opacity;
191 }
192 #endif
193
194 void main()
195 {
196 #if ATLAS_DEFAULT_WARP
197   mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );
198 #elif ATLAS_CUSTOM_WARP
199   mediump vec2 texCoord = vec2( wrapCoordinate( uAtlasRect.xz, vTexCoord.x, wrapMode.x ),
200                                 wrapCoordinate( uAtlasRect.yw, vTexCoord.y, wrapMode.y ) );
201 #else
202   mediump vec2 texCoord = vTexCoord;
203 #endif
204
205   lowp vec4 textureColor = TEXTURE( sTexture, texCoord ) * vec4( mixColor, 1.0 );
206
207 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
208   // skip most potential calculate for performance
209   if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
210   {
211     OUT_COLOR = textureColor * uColor;
212     return;
213   }
214   PreprocessPotential();
215 #endif
216
217 #if IS_REQUIRED_BORDERLINE
218   textureColor = convertBorderlineColor(textureColor);
219 #endif
220   OUT_COLOR = textureColor * uColor;
221
222 #if IS_REQUIRED_ROUNDED_CORNER
223   mediump float opacity = calculateCornerOpacity();
224   OUT_COLOR.a *= opacity;
225   OUT_COLOR.rgb *= mix(1.0, opacity, preMultipliedAlpha);
226 #endif
227 }