2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23 #include <mesh-builder.h>
27 void utc_dali_shader_startup(void)
29 test_return_value = TET_UNDEF;
32 void utc_dali_shader_cleanup(void)
34 test_return_value = TET_PASS;
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";
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";
49 void TestConstraintNoBlue( Vector4& current, const PropertyInputContainer& inputs )
58 int UtcDaliShaderMethodNew01(void)
60 TestApplication application;
62 Shader shader = Shader::New( VertexSource, FragmentSource );
63 DALI_TEST_EQUALS((bool)shader, true, TEST_LOCATION);
67 int UtcDaliShaderMethodNew02(void)
69 TestApplication application;
72 DALI_TEST_EQUALS((bool)shader, false, TEST_LOCATION);
76 int UtcDaliShaderAssignmentOperator(void)
78 TestApplication application;
80 Shader shader1 = Shader::New(VertexSource, FragmentSource);
84 DALI_TEST_CHECK(!(shader1 == shader2));
88 DALI_TEST_CHECK(shader1 == shader2);
90 shader2 = Shader::New(VertexSource, FragmentSource);;
92 DALI_TEST_CHECK(!(shader1 == shader2));
97 int UtcDaliShaderDownCast01(void)
99 TestApplication application;
101 Shader shader = Shader::New(VertexSource, FragmentSource);
103 BaseHandle handle(shader);
104 Shader shader2 = Shader::DownCast(handle);
105 DALI_TEST_EQUALS( (bool)shader2, true, TEST_LOCATION );
109 int UtcDaliShaderDownCast02(void)
111 TestApplication application;
113 Handle handle = Handle::New(); // Create a custom object
114 Shader shader = Shader::DownCast(handle);
115 DALI_TEST_EQUALS( (bool)shader, false, TEST_LOCATION );
119 int UtcDaliShaderDefaultProperties(void)
121 TestApplication application;
122 // from shader-impl.cpp
123 // DALI_PROPERTY( "program", MAP, true, false, false, Dali::Shader::Property::PROGRAM )
125 Shader shader = Shader::New(VertexSource, FragmentSource);
126 DALI_TEST_EQUALS( shader.GetPropertyCount(), 1, TEST_LOCATION );
128 DALI_TEST_EQUALS( shader.GetPropertyName( Shader::Property::PROGRAM ), "program", TEST_LOCATION );
129 DALI_TEST_EQUALS( shader.GetPropertyIndex( "program" ), (Property::Index)Shader::Property::PROGRAM, TEST_LOCATION );
130 DALI_TEST_EQUALS( shader.GetPropertyType( Shader::Property::PROGRAM ), Property::MAP, TEST_LOCATION );
131 DALI_TEST_EQUALS( shader.IsPropertyWritable( Shader::Property::PROGRAM ), true, TEST_LOCATION );
132 DALI_TEST_EQUALS( shader.IsPropertyAnimatable( Shader::Property::PROGRAM ), false, TEST_LOCATION );
133 DALI_TEST_EQUALS( shader.IsPropertyAConstraintInput( Shader::Property::PROGRAM ), false, TEST_LOCATION );
138 int UtcDaliShaderConstraint01(void)
140 TestApplication application;
142 tet_infoline("Test that a non-uniform shader property can be constrained");
144 Shader shader = Shader::New(VertexSource, FragmentSource);
145 Geometry geometry = CreateQuadGeometry();
146 Renderer renderer = Renderer::New( geometry, shader );
148 Actor actor = Actor::New();
149 actor.AddRenderer(renderer);
150 actor.SetSize(400, 400);
151 Stage::GetCurrent().Add(actor);
153 Vector4 initialColor = Color::WHITE;
154 Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
156 application.SendNotification();
157 application.Render(0);
158 DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
161 Constraint constraint = Constraint::New<Vector4>( shader, colorIndex, TestConstraintNoBlue );
163 application.SendNotification();
164 application.Render(0);
166 // Expect no blue component in either buffer - yellow
167 DALI_TEST_EQUALS( shader.GetCurrentProperty< Vector4 >( colorIndex ), Color::YELLOW, TEST_LOCATION );
168 application.Render(0);
169 DALI_TEST_EQUALS( shader.GetCurrentProperty< Vector4 >( colorIndex ), Color::YELLOW, TEST_LOCATION );
171 shader.RemoveConstraints();
172 shader.SetProperty(colorIndex, Color::WHITE );
173 application.SendNotification();
174 application.Render(0);
175 DALI_TEST_EQUALS( shader.GetCurrentProperty< Vector4 >( colorIndex ), Color::WHITE, TEST_LOCATION );
180 int UtcDaliShaderConstraint02(void)
182 TestApplication application;
184 tet_infoline("Test that a uniform map shader property can be constrained");
186 Shader shader = Shader::New(VertexSource, FragmentSource);
187 Geometry geometry = CreateQuadGeometry();
188 Renderer renderer = Renderer::New( geometry, shader );
190 Actor actor = Actor::New();
191 actor.AddRenderer(renderer);
192 actor.SetSize(400, 400);
193 Stage::GetCurrent().Add(actor);
194 application.SendNotification();
195 application.Render(0);
197 Vector4 initialColor = Color::WHITE;
198 Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
200 TestGlAbstraction& gl = application.GetGlAbstraction();
202 application.SendNotification();
203 application.Render(0);
205 Vector4 actualValue(Vector4::ZERO);
206 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
207 DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
210 Constraint constraint = Constraint::New<Vector4>( shader, colorIndex, TestConstraintNoBlue );
212 application.SendNotification();
213 application.Render(0);
215 // Expect no blue component in either buffer - yellow
216 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
217 DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
219 application.Render(0);
220 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
221 DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
223 shader.RemoveConstraints();
224 shader.SetProperty(colorIndex, Color::WHITE );
225 application.SendNotification();
226 application.Render(0);
228 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
229 DALI_TEST_EQUALS( actualValue, Color::WHITE, TEST_LOCATION );
234 int UtcDaliShaderAnimatedProperty01(void)
236 TestApplication application;
238 tet_infoline("Test that a non-uniform shader property can be animated");
240 Shader shader = Shader::New(VertexSource, FragmentSource);
241 Geometry geometry = CreateQuadGeometry();
242 Renderer renderer = Renderer::New( geometry, shader );
244 Actor actor = Actor::New();
245 actor.AddRenderer(renderer);
246 actor.SetSize(400, 400);
247 Stage::GetCurrent().Add(actor);
249 Vector4 initialColor = Color::WHITE;
250 Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
252 application.SendNotification();
253 application.Render(0);
254 DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
256 Animation animation = Animation::New(1.0f);
257 KeyFrames keyFrames = KeyFrames::New();
258 keyFrames.Add(0.0f, initialColor);
259 keyFrames.Add(1.0f, Color::TRANSPARENT);
260 animation.AnimateBetween( Property( shader, colorIndex ), keyFrames );
263 application.SendNotification();
264 application.Render(500);
266 DALI_TEST_EQUALS( shader.GetCurrentProperty< Vector4 >( colorIndex ), Color::WHITE * 0.5f, TEST_LOCATION );
268 application.Render(500);
270 DALI_TEST_EQUALS( shader.GetCurrentProperty< Vector4 >( colorIndex ), Color::TRANSPARENT, TEST_LOCATION );
275 int UtcDaliShaderAnimatedProperty02(void)
277 TestApplication application;
279 tet_infoline("Test that a uniform map shader property can be animated");
281 Shader shader = Shader::New(VertexSource, FragmentSource);
282 Geometry geometry = CreateQuadGeometry();
283 Renderer renderer = Renderer::New( geometry, shader );
285 Actor actor = Actor::New();
286 actor.AddRenderer(renderer);
287 actor.SetSize(400, 400);
288 Stage::GetCurrent().Add(actor);
289 application.SendNotification();
290 application.Render(0);
292 Vector4 initialColor = Color::WHITE;
293 Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
295 TestGlAbstraction& gl = application.GetGlAbstraction();
297 application.SendNotification();
298 application.Render(0);
300 Vector4 actualValue(Vector4::ZERO);
301 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
302 DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
304 Animation animation = Animation::New(1.0f);
305 KeyFrames keyFrames = KeyFrames::New();
306 keyFrames.Add(0.0f, initialColor);
307 keyFrames.Add(1.0f, Color::TRANSPARENT);
308 animation.AnimateBetween( Property( shader, colorIndex ), keyFrames );
311 application.SendNotification();
312 application.Render(500);
314 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
315 DALI_TEST_EQUALS( actualValue, Color::WHITE * 0.5f, TEST_LOCATION );
317 application.Render(500);
318 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
319 DALI_TEST_EQUALS( actualValue, Color::TRANSPARENT, TEST_LOCATION );
321 // change shader program
323 map["vertex"] = VertexSource;
324 map["fragment"] = FragmentSource;
325 map["hints"] = "MODIFIES_GEOMETRY";
326 shader.SetProperty( Shader::Property::PROGRAM, Property::Value(map) );
327 application.SendNotification();
328 application.Render(100);
330 // register another custom property as well
331 Property::Index customIndex = shader.RegisterProperty( "uCustom", Vector3(1,2,3) );
332 DALI_TEST_EQUALS( shader.GetProperty<Vector3>( customIndex ), Vector3(1,2,3), TEST_LOCATION );
334 application.SendNotification();
335 application.Render(100);
337 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
338 DALI_TEST_EQUALS( actualValue, Color::TRANSPARENT, TEST_LOCATION );
341 DALI_TEST_CHECK( gl.GetUniformValue<Vector3>( "uCustom", customValue ) );
342 DALI_TEST_EQUALS( customValue, Vector3(1,2,3), TEST_LOCATION );
346 int UtcDaliShaderProgramProperty(void)
348 TestApplication application;
350 tet_infoline("Test get/set progam property");
352 Shader shader = Shader::New("", "");
353 std::string hintSet = "MODIFIES_GEOMETRY";
356 map["vertex"] = VertexSource;
357 map["fragment"] = FragmentSource;
358 map["hints"] = hintSet;
360 shader.SetProperty( Shader::Property::PROGRAM, Property::Value(map) );
361 // register a custom property as well
362 Property::Index customIndex = shader.RegisterProperty( "custom", Vector3(1,2,3) );
363 DALI_TEST_EQUALS( shader.GetProperty<Vector3>( customIndex ), Vector3(1,2,3), TEST_LOCATION );
365 Property::Value value = shader.GetProperty(Shader::Property::PROGRAM);
366 DALI_TEST_CHECK( value.GetType() == Property::MAP);
367 const Property::Map* outMap = value.GetMap();
369 std::string v = (*outMap)["vertex"].Get<std::string>();
370 std::string f = (*outMap)["fragment"].Get<std::string>();
371 std::string h = (*outMap)["hints"].Get<std::string>();
373 DALI_TEST_CHECK( v == VertexSource );
374 DALI_TEST_CHECK( f == FragmentSource );
375 DALI_TEST_CHECK( h == hintSet );
377 value = shader.GetCurrentProperty( Shader::Property::PROGRAM );
378 DALI_TEST_CHECK( value.GetType() == Property::MAP);
379 outMap = value.GetMap();
380 // check that changing the shader did not cause us to loose custom property
381 DALI_TEST_EQUALS( shader.GetProperty<Vector3>( customIndex ), Vector3(1,2,3), TEST_LOCATION );
382 using Dali::Animation;
383 Animation animation = Animation::New( 0.1f );
384 animation.AnimateTo( Property( shader, customIndex ), Vector3(4,5,6) );
386 application.SendNotification();
387 application.Render(100);
388 DALI_TEST_EQUALS( shader.GetProperty<Vector3>( customIndex ), Vector3(4,5,6), TEST_LOCATION );
390 v = (*outMap)["vertex"].Get<std::string>();
391 f = (*outMap)["fragment"].Get<std::string>();
392 h = (*outMap)["hints"].Get<std::string>();
394 DALI_TEST_CHECK( v == VertexSource );
395 DALI_TEST_CHECK( f == FragmentSource );
396 DALI_TEST_CHECK( h == hintSet );
400 hintSet = "OUTPUT_IS_TRANSPARENT,MODIFIES_GEOMETRY";
401 map["hints"] = hintSet;
402 shader.SetProperty( Shader::Property::PROGRAM, Property::Value(map) );
403 value = shader.GetProperty(Shader::Property::PROGRAM);
404 hintGot = (*value.GetMap())["hints"].Get<std::string>();
405 DALI_TEST_CHECK( hintGot == hintSet );
407 hintSet = "OUTPUT_IS_TRANSPARENT";
408 map["hints"] = hintSet;
409 shader.SetProperty( Shader::Property::PROGRAM, Property::Value(map) );
410 value = shader.GetProperty(Shader::Property::PROGRAM);
411 hintGot = (*value.GetMap())["hints"].Get<std::string>();
412 DALI_TEST_CHECK( hintGot == hintSet );
415 map["hints"] = hintSet;
416 shader.SetProperty( Shader::Property::PROGRAM, Property::Value(map) );
417 value = shader.GetProperty(Shader::Property::PROGRAM);
418 hintGot = (*value.GetMap())["hints"].Get<std::string>();
419 DALI_TEST_CHECK( hintGot == hintSet );
422 map["hints"] = hintSet;
423 shader.SetProperty( Shader::Property::PROGRAM, Property::Value(map) );
424 value = shader.GetProperty(Shader::Property::PROGRAM);
425 hintGot = (*value.GetMap())["hints"].Get<std::string>();
426 DALI_TEST_CHECK( hintGot == "NONE" );