Wrapped ShaderHints enum inside a struct
[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 UtcDaliShaderAssignmentOperator(void)
77 {
78   TestApplication application;
79
80   Shader shader1 = Shader::New(VertexSource, FragmentSource);
81
82   Shader shader2;
83
84   DALI_TEST_CHECK(!(shader1 == shader2));
85
86   shader2 = shader1;
87
88   DALI_TEST_CHECK(shader1 == shader2);
89
90   shader2 = Shader::New(VertexSource, FragmentSource);;
91
92   DALI_TEST_CHECK(!(shader1 == shader2));
93
94   END_TEST;
95 }
96
97 int UtcDaliShaderDownCast01(void)
98 {
99   TestApplication application;
100
101   Shader shader = Shader::New(VertexSource, FragmentSource);
102
103   BaseHandle handle(shader);
104   Shader shader2 = Shader::DownCast(handle);
105   DALI_TEST_EQUALS( (bool)shader2, true, TEST_LOCATION );
106   END_TEST;
107 }
108
109 int UtcDaliShaderDownCast02(void)
110 {
111   TestApplication application;
112
113   Handle handle = Handle::New(); // Create a custom object
114   Shader shader = Shader::DownCast(handle);
115   DALI_TEST_EQUALS( (bool)shader, false, TEST_LOCATION );
116   END_TEST;
117 }
118
119 int UtcDaliShaderConstraint01(void)
120 {
121   TestApplication application;
122
123   tet_infoline("Test that a non-uniform shader property can be constrained");
124
125   Shader shader = Shader::New(VertexSource, FragmentSource);
126   Geometry geometry = CreateQuadGeometry();
127   Renderer renderer = Renderer::New( geometry, shader );
128
129   Actor actor = Actor::New();
130   actor.AddRenderer(renderer);
131   actor.SetSize(400, 400);
132   Stage::GetCurrent().Add(actor);
133
134   Vector4 initialColor = Color::WHITE;
135   Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
136
137   application.SendNotification();
138   application.Render(0);
139   DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
140
141   // Apply constraint
142   Constraint constraint = Constraint::New<Vector4>( shader, colorIndex, TestConstraintNoBlue );
143   constraint.Apply();
144   application.SendNotification();
145   application.Render(0);
146
147   // Expect no blue component in either buffer - yellow
148   DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
149   application.Render(0);
150   DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
151
152   shader.RemoveConstraints();
153   shader.SetProperty(colorIndex, Color::WHITE );
154   application.SendNotification();
155   application.Render(0);
156   DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::WHITE, TEST_LOCATION );
157
158   END_TEST;
159 }
160
161 int UtcDaliShaderConstraint02(void)
162 {
163   TestApplication application;
164
165   tet_infoline("Test that a uniform map shader property can be constrained");
166
167   Shader shader = Shader::New(VertexSource, FragmentSource);
168   Geometry geometry = CreateQuadGeometry();
169   Renderer renderer = Renderer::New( geometry, shader );
170
171   Actor actor = Actor::New();
172   actor.AddRenderer(renderer);
173   actor.SetSize(400, 400);
174   Stage::GetCurrent().Add(actor);
175   application.SendNotification();
176   application.Render(0);
177
178   Vector4 initialColor = Color::WHITE;
179   Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
180
181   TestGlAbstraction& gl = application.GetGlAbstraction();
182
183   application.SendNotification();
184   application.Render(0);
185
186   Vector4 actualValue(Vector4::ZERO);
187   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
188   DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
189
190   // Apply constraint
191   Constraint constraint = Constraint::New<Vector4>( shader, colorIndex, TestConstraintNoBlue );
192   constraint.Apply();
193   application.SendNotification();
194   application.Render(0);
195
196    // Expect no blue component in either buffer - yellow
197   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
198   DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
199
200   application.Render(0);
201   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
202   DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
203
204   shader.RemoveConstraints();
205   shader.SetProperty(colorIndex, Color::WHITE );
206   application.SendNotification();
207   application.Render(0);
208
209   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
210   DALI_TEST_EQUALS( actualValue, Color::WHITE, TEST_LOCATION );
211
212   END_TEST;
213 }
214
215 int UtcDaliShaderAnimatedProperty01(void)
216 {
217   TestApplication application;
218
219   tet_infoline("Test that a non-uniform shader property can be animated");
220
221   Shader shader = Shader::New(VertexSource, FragmentSource);
222   Geometry geometry = CreateQuadGeometry();
223   Renderer renderer = Renderer::New( geometry, shader );
224
225   Actor actor = Actor::New();
226   actor.AddRenderer(renderer);
227   actor.SetSize(400, 400);
228   Stage::GetCurrent().Add(actor);
229
230   Vector4 initialColor = Color::WHITE;
231   Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
232
233   application.SendNotification();
234   application.Render(0);
235   DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
236
237   Animation  animation = Animation::New(1.0f);
238   KeyFrames keyFrames = KeyFrames::New();
239   keyFrames.Add(0.0f, initialColor);
240   keyFrames.Add(1.0f, Color::TRANSPARENT);
241   animation.AnimateBetween( Property( shader, colorIndex ), keyFrames );
242   animation.Play();
243
244   application.SendNotification();
245   application.Render(500);
246
247   DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::WHITE * 0.5f, TEST_LOCATION );
248
249   application.Render(500);
250
251   DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::TRANSPARENT, TEST_LOCATION );
252
253   END_TEST;
254 }
255
256 int UtcDaliShaderAnimatedProperty02(void)
257 {
258   TestApplication application;
259
260   tet_infoline("Test that a uniform map shader property can be animated");
261
262   Shader shader = Shader::New(VertexSource, FragmentSource);
263   Geometry geometry = CreateQuadGeometry();
264   Renderer renderer = Renderer::New( geometry, shader );
265
266   Actor actor = Actor::New();
267   actor.AddRenderer(renderer);
268   actor.SetSize(400, 400);
269   Stage::GetCurrent().Add(actor);
270   application.SendNotification();
271   application.Render(0);
272
273   Vector4 initialColor = Color::WHITE;
274   Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
275
276   TestGlAbstraction& gl = application.GetGlAbstraction();
277
278   application.SendNotification();
279   application.Render(0);
280
281   Vector4 actualValue(Vector4::ZERO);
282   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
283   DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
284
285   Animation  animation = Animation::New(1.0f);
286   KeyFrames keyFrames = KeyFrames::New();
287   keyFrames.Add(0.0f, initialColor);
288   keyFrames.Add(1.0f, Color::TRANSPARENT);
289   animation.AnimateBetween( Property( shader, colorIndex ), keyFrames );
290   animation.Play();
291
292   application.SendNotification();
293   application.Render(500);
294
295   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
296   DALI_TEST_EQUALS( actualValue, Color::WHITE * 0.5f, TEST_LOCATION );
297
298   application.Render(500);
299   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
300   DALI_TEST_EQUALS( actualValue, Color::TRANSPARENT, TEST_LOCATION );
301
302   END_TEST;
303 }
304
305 int UtcDaliShaderProgramProperty(void)
306 {
307   TestApplication application;
308
309   tet_infoline("Test get/set progam property");
310
311   Shader shader = Shader::New("", "");
312   std::string hintSet = "MODIFIES_GEOMETRY";
313
314   Property::Map map;
315   map["vertex"] = VertexSource;
316   map["fragment"] = FragmentSource;
317   map["hints"] = hintSet;
318
319   shader.SetProperty( Shader::Property::PROGRAM, Property::Value(map) );
320
321   Property::Value value = shader.GetProperty(Shader::Property::PROGRAM);
322   DALI_TEST_CHECK( value.GetType() == Property::MAP);
323   const Property::Map* outMap = value.GetMap();
324
325   std::string v = (*outMap)["vertex"].Get<std::string>();
326   std::string f = (*outMap)["fragment"].Get<std::string>();
327   std::string h = (*outMap)["hints"].Get<std::string>();
328
329   DALI_TEST_CHECK( v == VertexSource );
330   DALI_TEST_CHECK( f == FragmentSource );
331   DALI_TEST_CHECK( h == hintSet );
332
333   std::string hintGot;
334
335   hintSet = "OUTPUT_IS_TRANSPARENT,MODIFIES_GEOMETRY";
336   map["hints"] = hintSet;
337   shader.SetProperty( Shader::Property::PROGRAM, Property::Value(map) );
338   value = shader.GetProperty(Shader::Property::PROGRAM);
339   hintGot = (*value.GetMap())["hints"].Get<std::string>();
340   DALI_TEST_CHECK( hintGot == hintSet );
341
342   hintSet = "OUTPUT_IS_TRANSPARENT";
343   map["hints"] = hintSet;
344   shader.SetProperty( Shader::Property::PROGRAM, Property::Value(map) );
345   value = shader.GetProperty(Shader::Property::PROGRAM);
346   hintGot = (*value.GetMap())["hints"].Get<std::string>();
347   DALI_TEST_CHECK( hintGot == hintSet );
348
349   hintSet = "NONE";
350   map["hints"] = hintSet;
351   shader.SetProperty( Shader::Property::PROGRAM, Property::Value(map) );
352   value = shader.GetProperty(Shader::Property::PROGRAM);
353   hintGot = (*value.GetMap())["hints"].Get<std::string>();
354   DALI_TEST_CHECK( hintGot == hintSet );
355
356   hintSet = "";
357   map["hints"] = hintSet;
358   shader.SetProperty( Shader::Property::PROGRAM, Property::Value(map) );
359   value = shader.GetProperty(Shader::Property::PROGRAM);
360   hintGot = (*value.GetMap())["hints"].Get<std::string>();
361   DALI_TEST_CHECK( hintGot == "NONE" );
362
363   END_TEST;
364 }