4be25246a06e4e76b2ba37091f220eec7cd6ee37
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / rendering / vector-based / glyphy-shader / glyphy-shader.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/text/rendering/vector-based/glyphy-shader/glyphy-shader.h>
20
21 // EXTERNAL HEADERS
22 #include <sstream>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/internal/text/rendering/vector-based/glyphy-shader/glyphy-common-glsl.h>
26 #include <dali-toolkit/internal/text/rendering/vector-based/glyphy-shader/glyphy-sdf-glsl.h>
27
28 using namespace Dali;
29
30 namespace
31 {
32 const char* const ENABLE_EXTENSION_PREFIX =
33   "#extension GL_OES_standard_derivatives : enable\n"
34   "precision highp float;\n"
35   "precision highp int;\n";
36
37 const char* const VERTEX_SHADER_MAIN =
38   "uniform   mediump mat4    uProjection;\n"
39   "uniform   mediump mat4    uModelView;\n"
40   "uniform   mediump mat4    uMvpMatrix;\n"
41   "uniform           bool    uTextureMapped;\n"
42   "uniform   mediump vec4    uCustomTextureCoords;\n"
43   "attribute highp   vec2    aTexCoord;\n"
44   "varying   mediump vec2    vTexCoord;\n"
45   "uniform   mat3            uModelViewIT;\n"
46   "attribute mediump vec3    aNormal;\n"
47   "varying   mediump vec3    vNormal;\n"
48   "attribute mediump vec2    aPosition;\n"
49   "varying   mediump vec4    vVertex;\n"
50   "attribute mediump vec4    aColor;\n"
51   "varying   mediump vec4    vColor;\n"
52   "varying vec4 v_glyph;\n"
53   "\n"
54   "vec4\n"
55   "glyph_vertex_transcode (vec2 v)\n"
56   "{\n"
57   "  ivec2 g = ivec2 (v);\n"
58   "  ivec2 corner = ivec2 (mod (v, 2.));\n"
59   "  g /= 2;\n"
60   "  ivec2 nominal_size = ivec2 (mod (vec2(g), 64.));\n"
61   "  return vec4 (corner * nominal_size, g * 4);\n"
62   "}\n"
63   "\n"
64   "void\n"
65   "main()\n"
66   "{\n"
67   "  gl_Position = uMvpMatrix * vec4 (aPosition, 0.0, 1.0);\n"
68   "  v_glyph = glyph_vertex_transcode (aTexCoord);\n"
69   "  vColor = aColor;\n"
70   "}\n";
71
72 const char* const FRAGMENT_SHADER_PREFIX =
73   "struct Material\n"
74   "{\n"
75   "  mediump float mOpacity;\n"
76   "  mediump float mShininess;\n"
77   "  lowp    vec4  mAmbient;\n"
78   "  lowp    vec4  mDiffuse;\n"
79   "  lowp    vec4  mSpecular;\n"
80   "  lowp    vec4  mEmissive;\n"
81   "};\n"
82   "uniform sampler2D     sTexture;\n"
83   "uniform sampler2D     sOpacityTexture;\n"
84   "uniform sampler2D     sNormalMapTexture;\n"
85   "uniform sampler2D     sEffect;\n"
86   "varying mediump vec2 vTexCoord;\n"
87   "uniform Material      uMaterial;\n"
88   "uniform lowp  vec4    uColor;\n"
89   "varying highp vec4    vVertex;\n"
90   "varying highp vec3    vNormal;\n"
91   "varying mediump vec4  vColor;\n"
92   "uniform vec4 u_atlas_info;\n"
93   "\n"
94   "#define GLYPHY_TEXTURE1D_EXTRA_DECLS , sampler2D _tex, ivec4 _atlas_info, ivec2 _atlas_pos\n"
95   "#define GLYPHY_TEXTURE1D_EXTRA_ARGS , _tex, _atlas_info, _atlas_pos\n"
96   "#define GLYPHY_DEMO_EXTRA_ARGS , sTexture, uu_atlas_info, gi.atlas_pos\n"
97   "\n"
98   "vec4\n"
99   "glyphy_texture1D_func (int offset GLYPHY_TEXTURE1D_EXTRA_DECLS)\n"
100   "{\n"
101   "  ivec2 item_geom = _atlas_info.zw;\n"
102   "  vec2 pos = (vec2 (_atlas_pos.xy * item_geom +\n"
103   "                    ivec2 (mod (float (offset), float (item_geom.x)), offset / item_geom.x)) +\n"
104   "             + vec2 (.5, .5)) / vec2(_atlas_info.xy);\n"
105   "  return texture2D (_tex, pos);\n"
106   "}\n";
107
108 static const char* FRAGMENT_SHADER_MAIN =
109   "uniform float u_contrast;\n"
110   "uniform float u_gamma_adjust;\n"
111   "uniform float u_outline_thickness;\n"
112   "uniform float u_outline;\n"
113   "uniform float u_boldness;\n"
114   "\n"
115   "varying vec4 v_glyph;\n"
116   "\n"
117   "\n"
118   "#define SQRT2_2 0.70711 /* 1 / sqrt(2.) */\n"
119   "#define SQRT2   1.41421\n"
120   "\n"
121   "struct glyph_info_t {\n"
122   "  ivec2 nominal_size;\n"
123   "  ivec2 atlas_pos;\n"
124   "};\n"
125   "\n"
126   "glyph_info_t\n"
127   "glyph_info_decode (vec4 v)\n"
128   "{\n"
129   "  glyph_info_t gi;\n"
130   "  gi.nominal_size = (ivec2 (mod (v.zw, 256.)) + 2) / 4;\n"
131   "  gi.atlas_pos = ivec2 (v_glyph.zw) / 256;\n"
132   "  return gi;\n"
133   "}\n"
134   "\n"
135   "\n"
136   "float\n"
137   "antialias (float d)\n"
138   "{\n"
139   "  return smoothstep (-.75, +.75, d);\n"
140   "}\n"
141   "\n"
142   "vec4\n"
143   "source_over (const vec4 src, const vec4 dst)\n"
144   "{\n"
145   "  // http://dev.w3.org/fxtf/compositing-1/#porterduffcompositingoperators_srcover\n"
146   "  float alpha = src.a + (dst.a * (1. - src.a));\n"
147   "  return vec4 (((src.rgb * src.a) + (dst.rgb * dst.a * (1. - src.a))) / alpha, alpha);\n"
148   "}\n"
149   "\n"
150   "void\n"
151   "main()\n"
152   "{\n"
153   "  vec2 p = v_glyph.xy;\n"
154   "  glyph_info_t gi = glyph_info_decode (v_glyph);\n"
155   "\n"
156   "  /* isotropic antialiasing */\n"
157   "  vec2 dpdx = dFdx (p);\n"
158   "  vec2 dpdy = dFdy (p);\n"
159   "  float m = length (vec2 (length (dpdx), length (dpdy))) * SQRT2_2;\n"
160   "\n"
161   "  vec4 color = vec4( vColor.rgb * uColor.rgb, vColor.a * uColor.a );\n"
162   "\n"
163   "  ivec4 uu_atlas_info = ivec4( u_atlas_info );"
164   "  float gsdist = glyphy_sdf (p, gi.nominal_size GLYPHY_DEMO_EXTRA_ARGS);\n"
165   "  float sdist = gsdist / m * u_contrast;\n"
166   "\n"
167   "    sdist -= u_boldness * 10.;\n"
168   "    if ( glyphy_iszero( u_outline ) )\n"
169   "      sdist = abs (sdist) - u_outline_thickness * .5;\n"
170   "    if (sdist > 1.)\n"
171   "      discard;\n"
172   "    float alpha = antialias (-sdist);\n"
173   "    if (u_gamma_adjust != 1.)\n"
174   "      alpha = pow (alpha, 1./u_gamma_adjust);\n"
175   "    color = vec4 (color.rgb,color.a * alpha);\n"
176   "\n"
177   "  gl_FragColor = color;\n"
178   "}\n";
179
180 } // namespace
181
182 namespace Dali
183 {
184 namespace Toolkit
185 {
186 namespace Text
187 {
188 GlyphyShader::GlyphyShader()
189 {
190 }
191
192 GlyphyShader::GlyphyShader(Shader handle)
193 : Shader(handle)
194 {
195 }
196
197 GlyphyShader::~GlyphyShader()
198 {
199 }
200
201 GlyphyShader GlyphyShader::New(const Dali::Vector4& atlasInfo)
202 {
203   std::ostringstream vertexShaderStringStream;
204   std::ostringstream fragmentShaderStringStream;
205
206   vertexShaderStringStream << ENABLE_EXTENSION_PREFIX << VERTEX_SHADER_MAIN;
207
208   fragmentShaderStringStream << ENABLE_EXTENSION_PREFIX
209                              << FRAGMENT_SHADER_PREFIX
210                              << glyphy_common_glsl
211                              << "#define GLYPHY_SDF_PSEUDO_DISTANCE 1\n"
212                              << glyphy_sdf_glsl
213                              << FRAGMENT_SHADER_MAIN;
214
215   Shader shaderEffectCustom = Shader::New(vertexShaderStringStream.str(),
216                                           fragmentShaderStringStream.str(),
217                                           Shader::Hint::OUTPUT_IS_TRANSPARENT);
218
219   GlyphyShader handle(shaderEffectCustom);
220
221   handle.RegisterProperty("u_atlas_info", atlasInfo);
222   handle.RegisterProperty("u_contrast", 1.f);
223   handle.RegisterProperty("u_gamma_adjust", 1.f);
224   handle.RegisterProperty("u_outline_thickness", 1.f);
225   handle.RegisterProperty("u_outline", 1.f);
226   handle.RegisterProperty("u_boldness", 0.f);
227
228   return handle;
229 }
230
231 } // namespace Text
232
233 } // namespace Toolkit
234
235 } // namespace Dali