[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / graphics / shaders / image-visual-shader.vert
1 INPUT mediump vec2 aPosition;
2 OUTPUT mediump vec2 vTexCoord;
3 #if defined(IS_REQUIRED_DEBUG_VISUAL_SHADER) || 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 #ifdef IS_REQUIRED_DEBUG_VISUAL_SHADER
14 #define DEBUG_EXTRA_ATTRIBUTES
15 #define DEBUG_EXTRA_VARYINGS
16
17 DEBUG_EXTRA_ATTRIBUTES
18 DEBUG_EXTRA_VARYINGS
19 #endif
20
21 uniform highp mat4 uMvpMatrix;
22 uniform highp vec3 uSize;
23 uniform highp vec4 pixelArea;
24
25 #if defined(IS_REQUIRED_DEBUG_VISUAL_SHADER) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
26 // Be used when we calculate anti-alias range near 1 pixel.
27 uniform highp vec3 uScale;
28 #endif
29
30 //Visual size and offset
31 uniform highp vec2 offset;
32 uniform highp vec2 size;
33 uniform mediump vec4 offsetSizeMode;
34 uniform mediump vec2 origin;
35 uniform mediump vec2 anchorPoint;
36 #ifdef IS_REQUIRED_BORDERLINE
37 uniform highp float borderlineWidth;
38 uniform highp float borderlineOffset;
39 #endif
40 #ifdef IS_REQUIRED_ROUNDED_CORNER
41 uniform highp vec4 cornerRadius;
42 uniform mediump float cornerRadiusPolicy;
43 #endif
44 #ifdef IS_REQUIRED_ALPHA_MASKING
45 OUTPUT  mediump vec2  vMaskTexCoord;
46 uniform lowp    float cropToMask;
47 uniform mediump vec2  maskTextureRatio;
48 #endif
49 uniform highp vec2 extraSize;
50
51 vec4 ComputeVertexPosition()
52 {
53   highp vec2 visualSize = mix(size * uSize.xy, size, offsetSizeMode.zw) + extraSize;
54   highp vec2 visualOffset = mix(offset * uSize.xy, offset, offsetSizeMode.xy);
55
56 #if defined(IS_REQUIRED_DEBUG_VISUAL_SHADER) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
57   vRectSize = visualSize * 0.5;
58   vOptRectSize = vRectSize;
59
60   // Set soft anti-alias range at most 10% of visual size.
61   // The range should be inverse proportion with scale of view.
62   // To avoid divid-by-zero, let we allow minimum scale value is 0.001 (0.1%)
63   vAliasMargin = min(1.0, max(visualSize.x, visualSize.y) * 0.1) / max(0.001, max(uScale.x, uScale.y));
64
65   highp float vertexMargin = 0.0;
66 #endif
67
68 #ifdef IS_REQUIRED_BORDERLINE
69   // Extend size of visual by borderline.
70   highp float outerBorderlineSize = (1.0 + clamp(borderlineOffset, -1.0, 1.0)) * borderlineWidth;
71 #endif
72
73 #ifdef IS_REQUIRED_ROUNDED_CORNER
74 #ifdef IS_REQUIRED_BORDERLINE
75   highp float maxSize = max(visualSize.x, visualSize.y) + outerBorderlineSize;
76   highp float minSize = min(visualSize.x, visualSize.y) + outerBorderlineSize;
77 #else
78   highp float maxSize = max(visualSize.x, visualSize.y);
79   highp float minSize = min(visualSize.x, visualSize.y);
80 #endif
81   vCornerRadius = mix(cornerRadius * minSize, cornerRadius, cornerRadiusPolicy);
82   vCornerRadius = min(vCornerRadius, minSize * 0.5);
83   // Optimize fragment shader. 0.2929 ~= 1.0 - sqrt(0.5)
84   highp float maxRadius = max(max(vCornerRadius.x, vCornerRadius.y), max(vCornerRadius.z, vCornerRadius.w));
85   highp float minRadius = min(min(vCornerRadius.x, vCornerRadius.y), min(vCornerRadius.z, vCornerRadius.w));
86   vOptRectSize -= 0.2929 * maxRadius + 1.0;
87
88   // Set vertex margin as vAliasMargin if we need to make some more fragments for alias.
89   // Do not increase margin if the minRadius is small enough rather than maxSize.
90   // TODO : We should change the magic parameter, 0.49
91   vertexMargin = 2.0 * vAliasMargin * smoothstep(maxSize * 0.49, maxSize * 0.5, minRadius);
92 #endif
93
94 #ifdef IS_REQUIRED_BORDERLINE
95   vPosition = aPosition * (visualSize + outerBorderlineSize + vertexMargin);
96   vOptRectSize -= (borderlineWidth - outerBorderlineSize * 0.5) + 1.0;
97 #elif defined(IS_REQUIRED_DEBUG_VISUAL_SHADER) || defined(IS_REQUIRED_ROUNDED_CORNER)
98   vPosition = aPosition * (visualSize + vertexMargin);
99 #else
100   highp vec2 vPosition = aPosition * visualSize;
101 #endif
102
103   highp vec4 finalPixelArea = pixelArea;
104 #ifdef IS_REQUIRED_ALPHA_MASKING
105   finalPixelArea = mix(pixelArea,
106                        vec4(
107                             vec2(0.5) + (pixelArea.xy - vec2(0.5)) * maskTextureRatio,
108                             pixelArea.zw * maskTextureRatio
109                        ),
110                        cropToMask);
111   vMaskTexCoord = pixelArea.xy + pixelArea.zw * (vec2(0.5) + aPosition.xy
112 #ifdef IS_REQUIRED_BORDERLINE
113                                                   * (1.0 + (outerBorderlineSize + vertexMargin) / visualSize)
114 #elif defined(IS_REQUIRED_ROUNDED_CORNER)
115                                                   * (1.0 + vertexMargin / visualSize)
116 #endif
117                                                 );
118 #endif
119   vTexCoord = finalPixelArea.xy + finalPixelArea.zw * (vec2(0.5) + aPosition.xy
120 #ifdef IS_REQUIRED_BORDERLINE
121                                                         * (1.0 + (outerBorderlineSize + vertexMargin) / visualSize)
122 #elif defined(IS_REQUIRED_ROUNDED_CORNER)
123                                                         * (1.0 + vertexMargin / visualSize)
124 #endif
125                                                       );
126
127   return vec4(vPosition + anchorPoint * visualSize + visualOffset + origin * uSize.xy, 0.0, 1.0);
128 }
129
130 #ifdef IS_REQUIRED_DEBUG_VISUAL_SHADER
131 // These lines in the shader may be replaced with actual definitions by the debug-image-visual-shader-script.json.
132 #define DEBUG_APPLY_VARYING_CODE
133 #define DEBUG_EXTRA_UNIFORMS
134
135 DEBUG_EXTRA_UNIFORMS
136
137 void ApplyDebugVarying()
138 {
139   DEBUG_APPLY_VARYING_CODE
140 }
141 #endif
142 void main()
143 {
144   gl_Position = uMvpMatrix * ComputeVertexPosition();
145
146 #ifdef IS_REQUIRED_DEBUG_VISUAL_SHADER
147   ApplyDebugVarying();
148 #endif
149 }