Added test cases and fixed bugs
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Shader.cpp
1 /*
2  * Copyright (c) 2015 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 #include <iostream>
19
20 #include <stdlib.h>
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23 #include <mesh-builder.h>
24
25 using namespace Dali;
26
27 void utc_dali_shader_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void utc_dali_shader_cleanup(void)
33 {
34   test_return_value = TET_PASS;
35 }
36
37 namespace
38 {
39
40 static const char* VertexSource =
41 "This is a custom vertex shader\n"
42 "made on purpose to look nothing like a normal vertex shader inside dali\n";
43
44 static const char* FragmentSource =
45 "This is a custom fragment shader\n"
46 "made on purpose to look nothing like a normal fragment shader inside dali\n";
47
48
49 void TestConstraintNoBlue( Vector4& current, const PropertyInputContainer& inputs )
50 {
51   current.b = 0.0f;
52 }
53
54
55 } // anon namespace
56
57
58 int UtcDaliShaderMethodNew01(void)
59 {
60   TestApplication application;
61
62   Shader shader = Shader::New( VertexSource, FragmentSource );
63   DALI_TEST_EQUALS((bool)shader, true, TEST_LOCATION);
64   END_TEST;
65 }
66
67 int UtcDaliShaderMethodNew02(void)
68 {
69   TestApplication application;
70
71   Shader shader;
72   DALI_TEST_EQUALS((bool)shader, false, TEST_LOCATION);
73   END_TEST;
74 }
75
76 int UtcDaliShaderDownCast01(void)
77 {
78   TestApplication application;
79
80   Shader shader = Shader::New(VertexSource, FragmentSource);
81
82   BaseHandle handle(shader);
83   Shader shader2 = Shader::DownCast(handle);
84   DALI_TEST_EQUALS( (bool)shader2, true, TEST_LOCATION );
85   END_TEST;
86 }
87
88 int UtcDaliShaderDownCast02(void)
89 {
90   TestApplication application;
91
92   Handle handle = Handle::New(); // Create a custom object
93   Shader shader = Shader::DownCast(handle);
94   DALI_TEST_EQUALS( (bool)shader, false, TEST_LOCATION );
95   END_TEST;
96 }
97
98 int UtcDaliShaderConstraint01(void)
99 {
100   TestApplication application;
101
102   tet_infoline("Test that a non-uniform shader property can be constrained");
103
104   Shader shader = Shader::New(VertexSource, FragmentSource);
105   Material material = Material::New( shader );
106   material.SetProperty(Material::Property::COLOR, Color::WHITE);
107
108   Geometry geometry = CreateQuadGeometry();
109   Renderer renderer = Renderer::New( geometry, material );
110
111   Actor actor = Actor::New();
112   actor.AddRenderer(renderer);
113   actor.SetSize(400, 400);
114   Stage::GetCurrent().Add(actor);
115
116   Vector4 initialColor = Color::WHITE;
117   Property::Index colorIndex = shader.RegisterProperty( "fade-color", initialColor );
118
119   application.SendNotification();
120   application.Render(0);
121   DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
122
123   // Apply constraint
124   Constraint constraint = Constraint::New<Vector4>( shader, colorIndex, TestConstraintNoBlue );
125   constraint.Apply();
126   application.SendNotification();
127   application.Render(0);
128
129   // Expect no blue component in either buffer - yellow
130   DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
131   application.Render(0);
132   DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
133
134   shader.RemoveConstraints();
135   shader.SetProperty(colorIndex, Color::WHITE );
136   application.SendNotification();
137   application.Render(0);
138   DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::WHITE, TEST_LOCATION );
139
140   END_TEST;
141 }
142
143 int UtcDaliShaderConstraint02(void)
144 {
145   TestApplication application;
146
147   tet_infoline("Test that a uniform map shader property can be constrained");
148
149   Shader shader = Shader::New(VertexSource, FragmentSource);
150   Material material = Material::New( shader );
151   material.SetProperty(Material::Property::COLOR, Color::WHITE);
152
153   Geometry geometry = CreateQuadGeometry();
154   Renderer renderer = Renderer::New( geometry, material );
155
156   Actor actor = Actor::New();
157   actor.AddRenderer(renderer);
158   actor.SetSize(400, 400);
159   Stage::GetCurrent().Add(actor);
160   application.SendNotification();
161   application.Render(0);
162
163   Vector4 initialColor = Color::WHITE;
164   Property::Index colorIndex = shader.RegisterProperty( "fade-color", initialColor );
165   shader.AddUniformMapping( colorIndex, std::string("uFadeColor") );
166
167   TestGlAbstraction& gl = application.GetGlAbstraction();
168
169   application.SendNotification();
170   application.Render(0);
171
172   Vector4 actualValue(Vector4::ZERO);
173   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
174   DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
175
176   // Apply constraint
177   Constraint constraint = Constraint::New<Vector4>( shader, colorIndex, TestConstraintNoBlue );
178   constraint.Apply();
179   application.SendNotification();
180   application.Render(0);
181
182    // Expect no blue component in either buffer - yellow
183   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
184   DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
185
186   application.Render(0);
187   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
188   DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
189
190   shader.RemoveConstraints();
191   shader.SetProperty(colorIndex, Color::WHITE );
192   application.SendNotification();
193   application.Render(0);
194
195   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
196   DALI_TEST_EQUALS( actualValue, Color::WHITE, TEST_LOCATION );
197
198   END_TEST;
199 }
200
201
202
203 int UtcDaliShaderAnimatedProperty01(void)
204 {
205   TestApplication application;
206
207   tet_infoline("Test that a non-uniform shader property can be animated");
208
209   Shader shader = Shader::New(VertexSource, FragmentSource);
210   Material material = Material::New( shader );
211   material.SetProperty(Material::Property::COLOR, Color::WHITE);
212
213   Geometry geometry = CreateQuadGeometry();
214   Renderer renderer = Renderer::New( geometry, material );
215
216   Actor actor = Actor::New();
217   actor.AddRenderer(renderer);
218   actor.SetSize(400, 400);
219   Stage::GetCurrent().Add(actor);
220
221   Vector4 initialColor = Color::WHITE;
222   Property::Index colorIndex = shader.RegisterProperty( "fade-color", initialColor );
223
224   application.SendNotification();
225   application.Render(0);
226   DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
227
228   Animation  animation = Animation::New(1.0f);
229   KeyFrames keyFrames = KeyFrames::New();
230   keyFrames.Add(0.0f, initialColor);
231   keyFrames.Add(1.0f, Color::TRANSPARENT);
232   animation.AnimateBetween( Property( shader, colorIndex ), keyFrames );
233   animation.Play();
234
235   application.SendNotification();
236   application.Render(500);
237
238   DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::WHITE * 0.5f, TEST_LOCATION );
239
240   application.Render(500);
241
242   DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::TRANSPARENT, TEST_LOCATION );
243
244   END_TEST;
245 }
246
247 int UtcDaliShaderAnimatedProperty02(void)
248 {
249   TestApplication application;
250
251   tet_infoline("Test that a uniform map shader property can be animated");
252
253   Shader shader = Shader::New(VertexSource, FragmentSource);
254   Material material = Material::New( shader );
255   material.SetProperty(Material::Property::COLOR, Color::WHITE);
256
257   Geometry geometry = CreateQuadGeometry();
258   Renderer renderer = Renderer::New( geometry, material );
259
260   Actor actor = Actor::New();
261   actor.AddRenderer(renderer);
262   actor.SetSize(400, 400);
263   Stage::GetCurrent().Add(actor);
264   application.SendNotification();
265   application.Render(0);
266
267   Vector4 initialColor = Color::WHITE;
268   Property::Index colorIndex = shader.RegisterProperty( "fade-color", initialColor );
269   shader.AddUniformMapping( colorIndex, std::string("uFadeColor") );
270
271   TestGlAbstraction& gl = application.GetGlAbstraction();
272
273   application.SendNotification();
274   application.Render(0);
275
276   Vector4 actualValue(Vector4::ZERO);
277   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
278   DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
279
280   Animation  animation = Animation::New(1.0f);
281   KeyFrames keyFrames = KeyFrames::New();
282   keyFrames.Add(0.0f, initialColor);
283   keyFrames.Add(1.0f, Color::TRANSPARENT);
284   animation.AnimateBetween( Property( shader, colorIndex ), keyFrames );
285   animation.Play();
286
287   application.SendNotification();
288   application.Render(500);
289
290   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
291   DALI_TEST_EQUALS( actualValue, Color::WHITE * 0.5f, TEST_LOCATION );
292
293   application.Render(500);
294   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
295   DALI_TEST_EQUALS( actualValue, Color::TRANSPARENT, TEST_LOCATION );
296
297   END_TEST;
298 }