Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / effects / renderer / FUiEffects_RendererEffectShader.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file    FUiEffects_RendererEffectShader.cpp
20  * @brief               This is the file with effects library shaders sources
21  *
22  */
23
24 #include <FUiEffects_RendererEffectShader.h>
25
26 #include <FBaseSysLog.h>
27
28 using std::string;
29
30 namespace Tizen { namespace Ui { namespace Effects { namespace _Renderer
31 {
32
33 const char* const EFFECT_VERTEX_SHADER_STR =
34                 "#define SHADER_NAME EffectVertexShader    \n"
35                 "attribute vec3 "SHADER_IN_POSITION_STR";\n"
36                 "attribute vec3 "SHADER_IN_NORMAL_STR";\n"
37                 "attribute vec2 "SHADER_IN_TEXCOORDS_STR";\n"
38                 "\n"
39                 "uniform highp mat4 WorldViewProjection;\n"
40                 "uniform highp mat4 ModelMtr; \n"
41                 "uniform mediump mat4 NormalMtr; \n"
42                 "\n"
43                 "varying highp vec3 Position; \n"
44                 "varying mediump vec3 Normal; \n"
45                 "varying mediump vec2 TexCoords;\n"
46                 "\n"
47                 "void main()\n"
48                 "{\n"
49                 "       highp vec4 WorldPos = ModelMtr * vec4(in_Position, 1.0); \n"
50                 "   gl_Position = WorldViewProjection * WorldPos;\n"
51                 "       Position =  WorldPos.xyz; \n"
52                 "       Normal = (NormalMtr * vec4(in_Normal, 0.0)).xyz; \n"
53                 "       TexCoords = in_TexCoords;\n"
54                 "}\n";
55
56 const char* const EFFECT_FRAGMENT_SHADER_STR =
57                 "#define SHADER_NAME EffectFragmentShader    \n"
58                 "//precision highp float;\n"
59                 "//precision highp vec3;\n"
60                 "//precision highp vec4;\n"
61                 "varying highp vec3 Position; \n"
62                 "varying mediump vec3 Normal;\n"
63                 "varying mediump vec2 TexCoords;\n"
64                 "\n"
65                 "uniform mediump sampler2D  "MAIN_TEX_STR";\n"
66                 "\n"
67                 "struct LightingParameters \n"
68                 "{ \n"
69                 "       mediump vec3 ambientColorI; \n"
70                 "       mediump float attenuationR2; \n"
71                 "}; \n"
72                 "\n"
73                 "struct PointLight \n"
74                 "{ \n"
75                 "       highp vec3 position; \n"
76                 "       mediump vec3 colorI; \n"
77                 "       mediump float Rk; \n"
78                 "}; \n"
79                 "\n"
80                 "struct SpotLight \n"
81                 "{ \n"
82                 "       highp vec3 position; \n"
83                 "       mediump vec3 direction; \n"
84                 "       mediump float k1; \n"
85                 "       mediump float k2; \n"
86                 "       mediump vec3 colorI; \n"
87                 "       mediump float Rk; \n"
88                 "}; \n"
89                 "\n"
90                 "struct DirectionalLight \n"
91                 "{ \n"
92                 "       mediump vec3 direction; \n"
93                 "       mediump vec3 colorI; \n"
94                 "}; \n"
95                 "\n"
96                 "uniform LightingParameters lightingParameters; \n"
97                 POINTLIGHT_DEFPOS_STR
98                 SPOTLIGHT_DEFPOS_STR
99                 DIRECTIOALLIGHT_DEFPOS_STR
100                 "uniform lowp float opacity; \n"
101                 "\n"
102                 "void main ()\n"
103                 "{   \n"
104                 "       mediump vec3 normal = normalize(Normal); \n"
105                 "       mediump vec3 color = lightingParameters.ambientColorI; \n"
106                 "       highp vec3 lightdir; \n"
107                 "       mediump float att; \n"
108                 "        \n"
109                 POINTLIGHT_SRCPOS_STR
110                 "\n"
111                 SPOTLIGHT_SRCPOS_STR
112                 "\n"
113                 DIRECTIOALLIGHT_SRCPOS_STR
114                 " \n"
115                 "       gl_FragColor.rgb = texture2D(TEX_MAIN, TexCoords).bgr * color; \n"
116                 "       gl_FragColor.a = opacity; \n"
117                 "}";
118
119 const char* const  EFFECT_DUMMY_FRAGMENT_SHADER_STR = "void main() { gl_FragColor = vec4(0.5, 0.9, 0.2, 1.0); }";
120
121 const char* const BLUR_VS =
122                 "#define SHADER_NAME BlurVS    \n"
123                 "attribute vec3 "SHADER_IN_POSITION_STR";\n"
124                 "attribute vec2 "SHADER_IN_TEXCOORDS_STR";\n"
125                 "\n"
126                 "varying mediump vec2 TexCoords;\n"
127                 "\n"
128                 "void main()\n"
129                 "{\n"
130                 "   gl_Position = vec4(in_Position, 1.0);\n"
131                 "       TexCoords = in_TexCoords;\n"
132                 "}\n";
133
134 const char* const BLUR_HORIZ_FS =
135                 "#define SHADER_NAME BlurHorizFS_2    \n"
136                 "varying mediump vec2 TexCoords;\n"
137                 "\n"
138                 "uniform mediump sampler2D  "MAIN_TEX_STR";\n"
139                 "uniform mediump float weights[48]; \n"
140                 "uniform mediump float stepx;  \n"
141                 "uniform mediump float stepy;  \n"
142                 "uniform mediump float size;            \n"
143                 "//mediump float weights2[5]; \n"
144                 "\n"
145                 "void main (void)\n"
146                 "{\n"
147                 "       /////if(int(size) == 4) gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); else gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);                  \n"
148                 "       // weights2[0] = 0.3990503; weights2[1] = 0.24203622; weights2[2] = 0.054005586; weights2[3] = 0.0044330484;            \n"
149                 "       mediump vec4 result = vec4(0.0);        \n"
150                 "       mediump vec2 coords1 = TexCoords;       \n"
151                 "       mediump vec2 coords2 = TexCoords;       \n"
152                 "       mediump int sizei = int(size);          \n"
153                 "       mediump vec4 ppp;                                       \n"
154                 "       \n"
155                 "       for(mediump int i = 1;  i < 201; i++){ if(i >= 2) break;  ppp = vec4(vec3(weights[i]), 1.0); result = ppp; }    \n"
156                 "\n"
157                 "       gl_FragColor =  result;         \n"
158                 "}  \n";
159
160 const char* const BLUR_VERT_FS =
161                 "#define SHADER_NAME BlurVertFS_TEST    \n"
162                 "varying lowp vec2 TexCoords;\n"
163                 "void main() { mediump vec4 p = vec4(0.0); gl_FragColor = p; }";
164
165
166 void replace(std::string &str, const std::string what, const std::string with)
167 {
168     if (what.empty())
169     {
170         return;
171     }
172
173     string::size_type start_pos = str.find(what);
174
175     while (start_pos != string::npos)
176     {
177         str.replace(start_pos, what.length(), with);
178         start_pos += with.length();
179         start_pos = str.find(what, start_pos);
180     }
181
182     return;
183 }
184
185 void printstring(const std::string name, const std::string &str)
186 {
187         std::string delim = "\n";
188
189         string::size_type pos = 0;
190         string::size_type pos2, dlen = delim.length(), slen = str.length();
191         int i = 1;
192         char buffer [30];
193
194         while (pos < slen && i < 10000)
195         {
196                 sprintf(buffer, "%d", i);
197
198                 pos2 = str.find(delim, pos);
199
200                 string sss = name + buffer + ". " + string(str, pos, pos2);
201
202                 SysLog(0, "%s", sss.length() == 0? "<empty>" : sss.c_str());
203
204                 if (pos2 == string::npos)
205                 {
206                         break;
207                 }
208
209                 pos = pos2 + dlen;
210                 i++;
211         }
212
213         return;
214 }
215
216 }}}} //Tizen::Ui::Effects::_Renderer