[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / graphics / shaders / gradient-visual-shader.vert
1 INPUT mediump vec2 aPosition;
2 OUTPUT mediump vec2 vTexCoord;
3 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
4 OUTPUT highp vec2 vPosition;
5 OUTPUT highp vec2 vRectSize;
6 OUTPUT highp vec2 vOptRectSize;
7 OUTPUT highp float vAliasMargin;
8 #ifdef IS_REQUIRED_ROUNDED_CORNER
9 OUTPUT highp vec4 vCornerRadius;
10 #endif
11 #endif
12
13 uniform highp mat4 uMvpMatrix;
14 uniform highp vec3 uSize;
15 uniform mediump mat3 uAlignmentMatrix;
16
17 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
18 // Be used when we calculate anti-alias range near 1 pixel.
19 uniform highp vec3 uScale;
20 #endif
21
22 //Visual size and offset
23 uniform highp vec2 offset;
24 uniform highp vec2 size;
25 uniform mediump vec4 offsetSizeMode;
26 uniform mediump vec2 origin;
27 uniform mediump vec2 anchorPoint;
28 #ifdef IS_REQUIRED_BORDERLINE
29 uniform highp float borderlineWidth;
30 uniform highp float borderlineOffset;
31 #endif
32 #ifdef IS_REQUIRED_ROUNDED_CORNER
33 uniform highp vec4 cornerRadius;
34 uniform mediump float cornerRadiusPolicy;
35 #endif
36
37 vec4 ComputeVertexPosition()
38 {
39   highp vec2 visualSize = mix(size * uSize.xy, size, offsetSizeMode.zw );
40   highp vec2 visualOffset = mix(offset * uSize.xy, offset, offsetSizeMode.xy);
41
42 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
43   vRectSize = visualSize * 0.5;
44   vOptRectSize = vRectSize;
45
46   // Set soft anti-alias range at most 10% of visual size.
47   // The range should be inverse proportion with scale of view.
48   // To avoid divid-by-zero, let we allow minimum scale value is 0.001 (0.1%)
49   vAliasMargin = min(1.0, max(visualSize.x, visualSize.y) * 0.1) / max(0.001, max(uScale.x, uScale.y));
50
51   highp float vertexMargin = 0.0;
52 #endif
53
54 #ifdef IS_REQUIRED_ROUNDED_CORNER
55 #ifdef IS_REQUIRED_BORDERLINE
56   highp float maxSize = max(visualSize.x, visualSize.y) + (1.0 + clamp(borderlineOffset, -1.0, 1.0)) * borderlineWidth;
57   highp float minSize = min(visualSize.x, visualSize.y) + (1.0 + clamp(borderlineOffset, -1.0, 1.0)) * borderlineWidth;
58 #else
59   highp float maxSize = max(visualSize.x, visualSize.y);
60   highp float minSize = min(visualSize.x, visualSize.y);
61 #endif
62   vCornerRadius = mix(cornerRadius * minSize, cornerRadius, cornerRadiusPolicy);
63   vCornerRadius = min(vCornerRadius, minSize * 0.5);
64   // Optimize fragment shader. 0.2929 ~= 1.0 - sqrt(0.5)
65   highp float maxRadius = max(max(vCornerRadius.x, vCornerRadius.y), max(vCornerRadius.z, vCornerRadius.w));
66   highp float minRadius = min(min(vCornerRadius.x, vCornerRadius.y), min(vCornerRadius.z, vCornerRadius.w));
67   vOptRectSize -= 0.2929 * maxRadius + 1.0;
68
69   // Set vertex margin as vAliasMargin if we need to make some more fragments for alias.
70   // Do not increase margin if the minRadius is small enough rather than maxSize.
71   // TODO : We should change the magic parameter, 0.49
72   vertexMargin = 2.0 * vAliasMargin * smoothstep(maxSize * 0.49, maxSize * 0.5, minRadius);
73 #endif
74
75   highp vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);
76 #ifdef IS_REQUIRED_BORDERLINE
77   vPosition = aPosition * (visualSize + (1.0 + clamp(borderlineOffset, -1.0, 1.0)) * borderlineWidth + vertexMargin);
78   vertexPosition.xy *= (1.0 + ((1.0 + clamp(borderlineOffset, -1.0, 1.0)) * borderlineWidth + vertexMargin) / visualSize);
79   vOptRectSize -= (1.0 - clamp(borderlineOffset, -1.0, 1.0)) * 0.5 * borderlineWidth + 1.0;
80 #elif defined(IS_REQUIRED_ROUNDED_CORNER)
81   vPosition = aPosition * (visualSize + vertexMargin);
82 #else
83   highp vec2 vPosition = aPosition * visualSize;
84 #endif
85
86 #ifdef USER_SPACE
87   vertexPosition.xyz *= uSize;
88 #endif
89
90   vTexCoord = (uAlignmentMatrix*vertexPosition.xyw).xy;
91
92   return vec4(vPosition + anchorPoint * visualSize + visualOffset + origin * uSize.xy, 0.0, 1.0);
93 }
94
95 void main()
96 {
97   gl_Position = uMvpMatrix * ComputeVertexPosition();
98 }