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