Add visualization option for check texture and visual size
[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_DEBUG_VISUAL_SHADER) || 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
32 #if defined(IS_REQUIRED_DEBUG_VISUAL_SHADER)
33 uniform highp vec3 uScale;
34 #endif
35
36 uniform lowp vec4 uColor;
37 uniform lowp vec3 mixColor;
38 uniform lowp float preMultipliedAlpha;
39 #ifdef IS_REQUIRED_BORDERLINE
40 uniform mediump float borderlineWidth;
41 uniform mediump float borderlineOffset;
42 uniform lowp vec4 borderlineColor;
43 uniform lowp vec4 uActorColor;
44 #endif
45
46 #ifdef ATLAS_CUSTOM_WARP
47 mediump float wrapCoordinate( mediump vec2 range, mediump float coordinate, lowp float wrap )
48 {
49   mediump float coord;
50   if( wrap > 1.5 ) /* REFLECT */
51     coord = 1.0 - abs(fract(coordinate*0.5)*2.0 - 1.0);
52   else /* warp is 0 or 1 */
53     coord = mix(coordinate, fract(coordinate), wrap);
54   return clamp(mix(range.x, range.y, coord), range.x, range.y);
55 }
56 #endif
57
58 #if defined(IS_REQUIRED_DEBUG_VISUAL_SHADER) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
59 // Global values both rounded corner and borderline use
60
61 // radius of rounded corner on this quadrant
62 mediump float gRadius = 0.0;
63
64 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
65 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
66 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
67 mediump float gCenterPosition = 0.0;
68 // relative coordinate of gFragmentPosition from gCenterPosition.
69 mediump vec2 gDiff = vec2(0.0, 0.0);
70 // potential value what our algorithm use.
71 mediump float gPotential = 0.0;
72
73 // threshold of potential
74 mediump float gPotentialRange = 0.0;
75 mediump float gMaxOutlinePotential = 0.0;
76 mediump float gMinOutlinePotential = 0.0;
77 mediump float gMaxInlinePotential = 0.0;
78 mediump float gMinInlinePotential = 0.0;
79
80 void calculateCornerRadius()
81 {
82 #ifdef IS_REQUIRED_ROUNDED_CORNER
83   gRadius =
84   mix(
85     mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
86     mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
87     sign(vPosition.y) * 0.5 + 0.5
88   );
89 #endif
90 }
91
92 void calculatePosition()
93 {
94   gFragmentPosition = abs(vPosition) - vRectSize;
95   gCenterPosition = -gRadius;
96 #ifdef IS_REQUIRED_BORDERLINE
97   gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
98 #endif
99   gDiff = gFragmentPosition - gCenterPosition;
100 }
101
102 void calculatePotential()
103 {
104   gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
105 }
106
107 void setupMinMaxPotential()
108 {
109   gPotentialRange = vAliasMargin;
110
111   gMaxOutlinePotential = gRadius + gPotentialRange;
112   gMinOutlinePotential = gRadius - gPotentialRange;
113
114 #ifdef IS_REQUIRED_BORDERLINE
115   gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
116   gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
117 #else
118   gMaxInlinePotential = gMaxOutlinePotential;
119   gMinInlinePotential = gMinOutlinePotential;
120 #endif
121
122   // reduce defect near edge of rounded corner.
123   gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y) / max(1.0, gRadius), 0.0, 1.0);
124   gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y) / max(1.0, gRadius), 0.0, 1.0);
125 }
126
127 void PreprocessPotential()
128 {
129   calculateCornerRadius();
130   calculatePosition();
131   calculatePotential();
132
133   setupMinMaxPotential();
134 }
135 #endif
136
137 #ifdef IS_REQUIRED_BORDERLINE
138 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
139 {
140   mediump float potential = gPotential;
141
142   // default opacity of borderline is 0.0
143   mediump float borderlineOpacity = 0.0;
144
145   // calculate borderline opacity by potential
146   if(potential > gMinInlinePotential)
147   {
148     // potential is inside borderline range.
149     borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
150
151     // Muliply borderlineWidth to resolve very thin borderline
152     borderlineOpacity *= min(1.0, borderlineWidth / gPotentialRange);
153   }
154
155   lowp vec3  borderlineColorRGB   = borderlineColor.rgb * uActorColor.rgb;
156   lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
157   borderlineColorRGB *= mix(1.0, borderlineColorAlpha, preMultipliedAlpha);
158
159   // Calculate inside of borderline when alpha is between (0.0  1.0). So we need to apply texture color.
160   // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code.
161   // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
162   if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
163   {
164     mediump float tCornerRadius = -gCenterPosition + gPotentialRange;
165     mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
166     mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
167     if(potential > MaxTexturelinePotential)
168     {
169       // potential is out of texture range.
170       textureColor = vec4(0.0);
171     }
172     else
173     {
174       // potential is in texture range.
175       lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
176       textureColor.a *= textureAlphaScale;
177       textureColor.rgb *= mix(textureColor.a, textureAlphaScale, preMultipliedAlpha);
178     }
179
180     borderlineColorAlpha *= borderlineOpacity;
181     borderlineColorRGB *= mix(borderlineColorAlpha, borderlineOpacity, preMultipliedAlpha);
182     // We use pre-multiplied color to reduce operations.
183     // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
184
185     // Manual blend operation with premultiplied colors.
186     // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a.
187     // (Final rgb * alpha) =  borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb
188     // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha)
189     // Else, return vec4((rgb*alpha) / alpha, alpha)
190
191     lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
192     lowp vec3  finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
193     // TODO : Need to find some way without division
194     return vec4(finalMultipliedRGB * mix(1.0 / finalAlpha, 1.0, preMultipliedAlpha), finalAlpha);
195   }
196   return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
197 }
198 #endif
199
200 #ifdef IS_REQUIRED_ROUNDED_CORNER
201 mediump float calculateCornerOpacity()
202 {
203   mediump float potential = gPotential;
204
205   // default opacity is 1.0
206   mediump float opacity = 1.0;
207
208   // calculate borderline opacity by potential
209   if(potential > gMaxOutlinePotential)
210   {
211     // potential is out of borderline range. just discard here
212     discard;
213   }
214   else if(potential > gMinOutlinePotential)
215   {
216     opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
217   }
218   return opacity;
219 }
220 #endif
221
222 #if defined(IS_REQUIRED_YUV_TO_RGB) || defined(IS_REQUIRED_UNIFIED_YUV_AND_RGB)
223 lowp vec4 ConvertYuvToRgba(mediump vec2 texCoord)
224 {
225 #ifdef IS_REQUIRED_UNIFIED_YUV_AND_RGB
226   // Special case when shader use YUV but actual textures are not YUV format.
227   // In this case, just resturn sTexture.
228   if(textureSize(sTextureU, 0) != textureSize(sTextureV, 0))
229   {
230     return texture(sTexture, texCoord);
231   }
232 #endif
233
234   lowp float y = texture(sTexture, texCoord).r;
235   lowp float u = texture(sTextureU, texCoord).r - 0.5;
236   lowp float v = texture(sTextureV, texCoord).r - 0.5;
237   lowp vec4 rgba;
238   rgba.r = y + (1.403 * v);
239   rgba.g = y - (0.344 * u) - (0.714 * v);
240   rgba.b = y + (1.770 * u);
241   rgba.a = 1.0;
242   return rgba;
243 }
244 #endif
245
246 #ifdef IS_REQUIRED_DEBUG_VISUAL_SHADER
247
248 // Predefined values whether some macro defined or not.
249 // Since we make debug codes replace by macro,
250 // sharp if keyword cannot be used.
251 // Instead, let we use bool values so we can use define checked in script
252 #ifdef IS_REQUIRED_ROUNDED_CORNER
253 const bool IS_REQUIRED_ROUNDED_CORNER_BOOL = true;
254 #else
255 const bool IS_REQUIRED_ROUNDED_CORNER_BOOL = false;
256 #endif
257 #ifdef IS_REQUIRED_BORDERLINE
258 const bool IS_REQUIRED_BORDERLINE_BOOL = true;
259 #else
260 const bool IS_REQUIRED_BORDERLINE_BOOL = false;
261 #endif
262 #ifdef IS_REQUIRED_YUV_TO_RGB
263 const bool IS_REQUIRED_YUV_TO_RGB_BOOL = true;
264 #else
265 const bool IS_REQUIRED_YUV_TO_RGB_BOOL = false;
266 #endif
267 #ifdef IS_REQUIRED_UNIFIED_YUV_AND_RGB
268 const bool IS_REQUIRED_UNIFIED_YUV_AND_RGB_BOOL = true;
269 #else
270 const bool IS_REQUIRED_UNIFIED_YUV_AND_RGB_BOOL = false;
271 #endif
272 #ifdef IS_REQUIRED_ALPHA_MASKING
273 const bool IS_REQUIRED_ALPHA_MASKING_BOOL = true;
274 #else
275 const bool IS_REQUIRED_ALPHA_MASKING_BOOL = false;
276 #endif
277 #ifdef ATLAS_DEFAULT_WARP
278 const bool ATLAS_DEFAULT_WARP_BOOL = true;
279 #else
280 const bool ATLAS_DEFAULT_WARP_BOOL = false;
281 #endif
282 #ifdef ATLAS_CUSTOM_WARP
283 const bool ATLAS_CUSTOM_WARP_BOOL = true;
284 #else
285 const bool ATLAS_CUSTOM_WARP_BOOL = false;
286 #endif
287
288 // These lines in the shader may be replaced with actual definitions by the debug-image-visual-shader-script.json.
289 // DEBUG_TRIGGER_CODE return bool type, and DEBUG_RATIO_CODE return float value which will be clamped between 0.0 and 1.0
290 // If DEBUG_TRIGGER_CODE return true, it mean we will change final color's channel value.
291 // If ratio is 0.0, debug color rate become MINIMUM_DEBUG_COLOR_RATE, and 1.0 than MAXIMUM_DEBUG_COLOR_RATE.
292 #define MINIMUM_DEBUG_COLOR_RATE
293 #define MAXIMUM_DEBUG_COLOR_RATE
294 #define DEBUG_TRIGGER_RED_CODE
295 #define DEBUG_TRIGGER_GREEN_CODE
296 #define DEBUG_TRIGGER_BLUE_CODE
297 #define DEBUG_RATIO_RED_CODE
298 #define DEBUG_RATIO_GREEN_CODE
299 #define DEBUG_RATIO_BLUE_CODE
300
301 const mediump float gMinDebugColorRate = MINIMUM_DEBUG_COLOR_RATE;
302 const mediump float gMaxDebugColorRate = MAXIMUM_DEBUG_COLOR_RATE;
303
304 bool DebugTriggerRed(mediump vec4 originColor)
305 {
306   DEBUG_TRIGGER_RED_CODE
307 }
308
309 bool DebugTriggerGreen(mediump vec4 originColor)
310 {
311   DEBUG_TRIGGER_GREEN_CODE
312 }
313
314 bool DebugTriggerBlue(mediump vec4 originColor)
315 {
316   DEBUG_TRIGGER_BLUE_CODE
317 }
318
319 mediump float DebugRatioRed(mediump vec4 originColor)
320 {
321   DEBUG_RATIO_RED_CODE
322 }
323
324 mediump float DebugRatioGreen(mediump vec4 originColor)
325 {
326   DEBUG_RATIO_GREEN_CODE
327 }
328
329 mediump float DebugRatioBlue(mediump vec4 originColor)
330 {
331   DEBUG_RATIO_BLUE_CODE
332 }
333
334 mediump vec3 ApplyDebugMixColor(mediump vec4 originColor)
335 {
336   mediump float debugColorRateRed = 0.0;
337   mediump float debugColorRateGreen = 0.0;
338   mediump float debugColorRateBlue = 0.0;
339
340   if(DebugTriggerRed(originColor))
341   {
342     debugColorRateRed = mix(gMinDebugColorRate, gMaxDebugColorRate, smoothstep(0.0, 1.0, DebugRatioRed(originColor)));
343   }
344   if(DebugTriggerGreen(originColor))
345   {
346     debugColorRateGreen = mix(gMinDebugColorRate, gMaxDebugColorRate, smoothstep(0.0, 1.0, DebugRatioGreen(originColor)));
347   }
348   if(DebugTriggerBlue(originColor))
349   {
350     debugColorRateBlue = mix(gMinDebugColorRate, gMaxDebugColorRate, smoothstep(0.0, 1.0, DebugRatioBlue(originColor)));
351   }
352
353   mediump float colorRate = max(debugColorRateRed, max(debugColorRateGreen, debugColorRateBlue));
354   mediump vec3 debugColor = vec3(debugColorRateRed, debugColorRateGreen, debugColorRateBlue);
355
356   debugColor *= mix(1.0, originColor.a, preMultipliedAlpha);
357
358   return originColor.rgb * (1.0 - colorRate) + debugColor;
359 }
360 #endif
361
362 void main()
363 {
364 #ifdef ATLAS_DEFAULT_WARP
365   mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );
366 #elif defined(ATLAS_CUSTOM_WARP)
367   mediump vec2 texCoord = vec2( wrapCoordinate( uAtlasRect.xz, vTexCoord.x, wrapMode.x ),
368                                 wrapCoordinate( uAtlasRect.yw, vTexCoord.y, wrapMode.y ) );
369 #else
370   mediump vec2 texCoord = vTexCoord;
371 #endif
372
373 #if defined(IS_REQUIRED_YUV_TO_RGB) || defined(IS_REQUIRED_UNIFIED_YUV_AND_RGB)
374   lowp vec4 textureColor = ConvertYuvToRgba(texCoord) * vec4( mixColor, 1.0 ) * uColor;
375 #else
376   lowp vec4 textureColor = TEXTURE( sTexture, texCoord ) * vec4( mixColor, 1.0 ) * uColor;
377 #endif
378
379 #ifdef IS_REQUIRED_ALPHA_MASKING
380   mediump vec2 maskTexCoord = vMaskTexCoord;
381   maskTexCoord.y = mix(maskTexCoord.y, 1.0-maskTexCoord.y, uYFlipMaskTexture);
382   mediump float maskAlpha = TEXTURE(sMaskTexture, maskTexCoord).a;
383   textureColor.a *= maskAlpha;
384   textureColor.rgb *= mix(1.0, maskAlpha, preMultipliedAlpha);
385 #endif
386
387 #if defined(IS_REQUIRED_DEBUG_VISUAL_SHADER) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
388 #ifndef IS_REQUIRED_DEBUG_VISUAL_SHADER
389   // skip most potential calculate for performance
390   if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
391   {
392     OUT_COLOR = textureColor;
393   }
394   else
395 #endif
396   {
397     PreprocessPotential();
398 #endif
399
400 #ifdef IS_REQUIRED_BORDERLINE
401     textureColor = convertBorderlineColor(textureColor);
402 #endif
403     OUT_COLOR = textureColor;
404
405 #ifdef IS_REQUIRED_ROUNDED_CORNER
406     mediump float opacity = calculateCornerOpacity();
407     OUT_COLOR.a *= opacity;
408     OUT_COLOR.rgb *= mix(1.0, opacity, preMultipliedAlpha);
409 #endif
410
411 #if defined(IS_REQUIRED_DEBUG_VISUAL_SHADER) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
412   }
413 #endif
414
415 #ifdef IS_REQUIRED_DEBUG_VISUAL_SHADER
416   OUT_COLOR.rgb = ApplyDebugMixColor(OUT_COLOR);
417 #endif
418 }