Revert "[Tizen] Move DevelHandle::GetCurrentProperty to public"
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Shader.cpp
1 /*
2  * Copyright (c) 2017 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/devel-api/object/handle-devel.h>
23 #include <dali-test-suite-utils.h>
24 #include <mesh-builder.h>
25
26 using namespace Dali;
27
28 void utc_dali_shader_startup(void)
29 {
30   test_return_value = TET_UNDEF;
31 }
32
33 void utc_dali_shader_cleanup(void)
34 {
35   test_return_value = TET_PASS;
36 }
37
38 namespace
39 {
40
41 static const char* VertexSource =
42 "This is a custom vertex shader\n"
43 "made on purpose to look nothing like a normal vertex shader inside dali\n";
44
45 static const char* FragmentSource =
46 "This is a custom fragment shader\n"
47 "made on purpose to look nothing like a normal fragment shader inside dali\n";
48
49
50 void TestConstraintNoBlue( Vector4& current, const PropertyInputContainer& inputs )
51 {
52   current.b = 0.0f;
53 }
54
55
56 } // anon namespace
57
58
59 int UtcDaliShaderMethodNew01(void)
60 {
61   TestApplication application;
62
63   Shader shader = Shader::New( VertexSource, FragmentSource );
64   DALI_TEST_EQUALS((bool)shader, true, TEST_LOCATION);
65   END_TEST;
66 }
67
68 int UtcDaliShaderMethodNew02(void)
69 {
70   TestApplication application;
71
72   Shader shader;
73   DALI_TEST_EQUALS((bool)shader, false, TEST_LOCATION);
74   END_TEST;
75 }
76
77 int UtcDaliShaderAssignmentOperator(void)
78 {
79   TestApplication application;
80
81   Shader shader1 = Shader::New(VertexSource, FragmentSource);
82
83   Shader shader2;
84
85   DALI_TEST_CHECK(!(shader1 == shader2));
86
87   shader2 = shader1;
88
89   DALI_TEST_CHECK(shader1 == shader2);
90
91   shader2 = Shader::New(VertexSource, FragmentSource);;
92
93   DALI_TEST_CHECK(!(shader1 == shader2));
94
95   END_TEST;
96 }
97
98 int UtcDaliShaderDownCast01(void)
99 {
100   TestApplication application;
101
102   Shader shader = Shader::New(VertexSource, FragmentSource);
103
104   BaseHandle handle(shader);
105   Shader shader2 = Shader::DownCast(handle);
106   DALI_TEST_EQUALS( (bool)shader2, true, TEST_LOCATION );
107   END_TEST;
108 }
109
110 int UtcDaliShaderDownCast02(void)
111 {
112   TestApplication application;
113
114   Handle handle = Handle::New(); // Create a custom object
115   Shader shader = Shader::DownCast(handle);
116   DALI_TEST_EQUALS( (bool)shader, false, TEST_LOCATION );
117   END_TEST;
118 }
119
120 int UtcDaliShaderConstraint01(void)
121 {
122   TestApplication application;
123
124   tet_infoline("Test that a non-uniform shader property can be constrained");
125
126   Shader shader = Shader::New(VertexSource, FragmentSource);
127   Geometry geometry = CreateQuadGeometry();
128   Renderer renderer = Renderer::New( geometry, shader );
129
130   Actor actor = Actor::New();
131   actor.AddRenderer(renderer);
132   actor.SetSize(400, 400);
133   Stage::GetCurrent().Add(actor);
134
135   Vector4 initialColor = Color::WHITE;
136   Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
137
138   application.SendNotification();
139   application.Render(0);
140   DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
141
142   // Apply constraint
143   Constraint constraint = Constraint::New<Vector4>( shader, colorIndex, TestConstraintNoBlue );
144   constraint.Apply();
145   application.SendNotification();
146   application.Render(0);
147
148   // Expect no blue component in either buffer - yellow
149   DALI_TEST_EQUALS( DevelHandle::GetCurrentProperty< Vector4 >( shader, colorIndex ), Color::YELLOW, TEST_LOCATION );
150   application.Render(0);
151   DALI_TEST_EQUALS( DevelHandle::GetCurrentProperty< Vector4 >( shader, colorIndex ), Color::YELLOW, TEST_LOCATION );
152
153   shader.RemoveConstraints();
154   shader.SetProperty(colorIndex, Color::WHITE );
155   application.SendNotification();
156   application.Render(0);
157   DALI_TEST_EQUALS( DevelHandle::GetCurrentProperty< Vector4 >( shader, colorIndex ), Color::WHITE, TEST_LOCATION );
158
159   END_TEST;
160 }
161
162 int UtcDaliShaderConstraint02(void)
163 {
164   TestApplication application;
165
166   tet_infoline("Test that a uniform map shader property can be constrained");
167
168   Shader shader = Shader::New(VertexSource, FragmentSource);
169   Geometry geometry = CreateQuadGeometry();
170   Renderer renderer = Renderer::New( geometry, shader );
171
172   Actor actor = Actor::New();
173   actor.AddRenderer(renderer);
174   actor.SetSize(400, 400);
175   Stage::GetCurrent().Add(actor);
176   application.SendNotification();
177   application.Render(0);
178
179   Vector4 initialColor = Color::WHITE;
180   Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
181
182   TestGlAbstraction& gl = application.GetGlAbstraction();
183
184   application.SendNotification();
185   application.Render(0);
186
187   Vector4 actualValue(Vector4::ZERO);
188   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
189   DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
190
191   // Apply constraint
192   Constraint constraint = Constraint::New<Vector4>( shader, colorIndex, TestConstraintNoBlue );
193   constraint.Apply();
194   application.SendNotification();
195   application.Render(0);
196
197    // Expect no blue component in either buffer - yellow
198   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
199   DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
200
201   application.Render(0);
202   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
203   DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
204
205   shader.RemoveConstraints();
206   shader.SetProperty(colorIndex, Color::WHITE );
207   application.SendNotification();
208   application.Render(0);
209
210   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
211   DALI_TEST_EQUALS( actualValue, Color::WHITE, TEST_LOCATION );
212
213   END_TEST;
214 }
215
216 int UtcDaliShaderAnimatedProperty01(void)
217 {
218   TestApplication application;
219
220   tet_infoline("Test that a non-uniform shader property can be animated");
221
222   Shader shader = Shader::New(VertexSource, FragmentSource);
223   Geometry geometry = CreateQuadGeometry();
224   Renderer renderer = Renderer::New( geometry, shader );
225
226   Actor actor = Actor::New();
227   actor.AddRenderer(renderer);
228   actor.SetSize(400, 400);
229   Stage::GetCurrent().Add(actor);
230
231   Vector4 initialColor = Color::WHITE;
232   Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
233
234   application.SendNotification();
235   application.Render(0);
236   DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
237
238   Animation  animation = Animation::New(1.0f);
239   KeyFrames keyFrames = KeyFrames::New();
240   keyFrames.Add(0.0f, initialColor);
241   keyFrames.Add(1.0f, Color::TRANSPARENT);
242   animation.AnimateBetween( Property( shader, colorIndex ), keyFrames );
243   animation.Play();
244
245   application.SendNotification();
246   application.Render(500);
247
248   DALI_TEST_EQUALS( DevelHandle::GetCurrentProperty< Vector4 >( shader, colorIndex ), Color::WHITE * 0.5f, TEST_LOCATION );
249
250   application.Render(500);
251
252   DALI_TEST_EQUALS( DevelHandle::GetCurrentProperty< Vector4 >( shader, colorIndex ), Color::TRANSPARENT, TEST_LOCATION );
253
254   END_TEST;
255 }
256
257 int UtcDaliShaderAnimatedProperty02(void)
258 {
259   TestApplication application;
260
261   tet_infoline("Test that a uniform map shader property can be animated");
262
263   Shader shader = Shader::New(VertexSource, FragmentSource);
264   Geometry geometry = CreateQuadGeometry();
265   Renderer renderer = Renderer::New( geometry, shader );
266
267   Actor actor = Actor::New();
268   actor.AddRenderer(renderer);
269   actor.SetSize(400, 400);
270   Stage::GetCurrent().Add(actor);
271   application.SendNotification();
272   application.Render(0);
273
274   Vector4 initialColor = Color::WHITE;
275   Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
276
277   TestGlAbstraction& gl = application.GetGlAbstraction();
278
279   application.SendNotification();
280   application.Render(0);
281
282   Vector4 actualValue(Vector4::ZERO);
283   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
284   DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
285
286   Animation  animation = Animation::New(1.0f);
287   KeyFrames keyFrames = KeyFrames::New();
288   keyFrames.Add(0.0f, initialColor);
289   keyFrames.Add(1.0f, Color::TRANSPARENT);
290   animation.AnimateBetween( Property( shader, colorIndex ), keyFrames );
291   animation.Play();
292
293   application.SendNotification();
294   application.Render(500);
295
296   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
297   DALI_TEST_EQUALS( actualValue, Color::WHITE * 0.5f, TEST_LOCATION );
298
299   application.Render(500);
300   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
301   DALI_TEST_EQUALS( actualValue, Color::TRANSPARENT, TEST_LOCATION );
302
303   END_TEST;
304 }
305
306 int UtcDaliShaderProgramProperty(void)
307 {
308   TestApplication application;
309
310   tet_infoline("Test get/set progam property");
311
312   Shader shader = Shader::New("", "");
313   std::string hintSet = "MODIFIES_GEOMETRY";
314
315   Property::Map map;
316   map["vertex"] = VertexSource;
317   map["fragment"] = FragmentSource;
318   map["hints"] = hintSet;
319
320   shader.SetProperty( Shader::Property::PROGRAM, Property::Value(map) );
321
322   Property::Value value = shader.GetProperty(Shader::Property::PROGRAM);
323   DALI_TEST_CHECK( value.GetType() == Property::MAP);
324   const Property::Map* outMap = value.GetMap();
325
326   std::string v = (*outMap)["vertex"].Get<std::string>();
327   std::string f = (*outMap)["fragment"].Get<std::string>();
328   std::string h = (*outMap)["hints"].Get<std::string>();
329
330   DALI_TEST_CHECK( v == VertexSource );
331   DALI_TEST_CHECK( f == FragmentSource );
332   DALI_TEST_CHECK( h == hintSet );
333
334   value = DevelHandle::GetCurrentProperty( shader, Shader::Property::PROGRAM );
335   DALI_TEST_CHECK( value.GetType() == Property::MAP);
336   outMap = value.GetMap();
337
338   v = (*outMap)["vertex"].Get<std::string>();
339   f = (*outMap)["fragment"].Get<std::string>();
340   h = (*outMap)["hints"].Get<std::string>();
341
342   DALI_TEST_CHECK( v == VertexSource );
343   DALI_TEST_CHECK( f == FragmentSource );
344   DALI_TEST_CHECK( h == hintSet );
345
346   std::string hintGot;
347
348   hintSet = "OUTPUT_IS_TRANSPARENT,MODIFIES_GEOMETRY";
349   map["hints"] = hintSet;
350   shader.SetProperty( Shader::Property::PROGRAM, Property::Value(map) );
351   value = shader.GetProperty(Shader::Property::PROGRAM);
352   hintGot = (*value.GetMap())["hints"].Get<std::string>();
353   DALI_TEST_CHECK( hintGot == hintSet );
354
355   hintSet = "OUTPUT_IS_TRANSPARENT";
356   map["hints"] = hintSet;
357   shader.SetProperty( Shader::Property::PROGRAM, Property::Value(map) );
358   value = shader.GetProperty(Shader::Property::PROGRAM);
359   hintGot = (*value.GetMap())["hints"].Get<std::string>();
360   DALI_TEST_CHECK( hintGot == hintSet );
361
362   hintSet = "NONE";
363   map["hints"] = hintSet;
364   shader.SetProperty( Shader::Property::PROGRAM, Property::Value(map) );
365   value = shader.GetProperty(Shader::Property::PROGRAM);
366   hintGot = (*value.GetMap())["hints"].Get<std::string>();
367   DALI_TEST_CHECK( hintGot == hintSet );
368
369   hintSet = "";
370   map["hints"] = hintSet;
371   shader.SetProperty( Shader::Property::PROGRAM, Property::Value(map) );
372   value = shader.GetProperty(Shader::Property::PROGRAM);
373   hintGot = (*value.GetMap())["hints"].Get<std::string>();
374   DALI_TEST_CHECK( hintGot == "NONE" );
375
376   END_TEST;
377 }