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/devel-api/object/handle-devel.h>
23 #include <dali-test-suite-utils.h>
24 #include <mesh-builder.h>
28 void utc_dali_shader_startup(void)
30 test_return_value = TET_UNDEF;
33 void utc_dali_shader_cleanup(void)
35 test_return_value = TET_PASS;
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";
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";
50 void TestConstraintNoBlue( Vector4& current, const PropertyInputContainer& inputs )
59 int UtcDaliShaderMethodNew01(void)
61 TestApplication application;
63 Shader shader = Shader::New( VertexSource, FragmentSource );
64 DALI_TEST_EQUALS((bool)shader, true, TEST_LOCATION);
68 int UtcDaliShaderMethodNew02(void)
70 TestApplication application;
73 DALI_TEST_EQUALS((bool)shader, false, TEST_LOCATION);
77 int UtcDaliShaderAssignmentOperator(void)
79 TestApplication application;
81 Shader shader1 = Shader::New(VertexSource, FragmentSource);
85 DALI_TEST_CHECK(!(shader1 == shader2));
89 DALI_TEST_CHECK(shader1 == shader2);
91 shader2 = Shader::New(VertexSource, FragmentSource);;
93 DALI_TEST_CHECK(!(shader1 == shader2));
98 int UtcDaliShaderDownCast01(void)
100 TestApplication application;
102 Shader shader = Shader::New(VertexSource, FragmentSource);
104 BaseHandle handle(shader);
105 Shader shader2 = Shader::DownCast(handle);
106 DALI_TEST_EQUALS( (bool)shader2, true, TEST_LOCATION );
110 int UtcDaliShaderDownCast02(void)
112 TestApplication application;
114 Handle handle = Handle::New(); // Create a custom object
115 Shader shader = Shader::DownCast(handle);
116 DALI_TEST_EQUALS( (bool)shader, false, TEST_LOCATION );
120 int UtcDaliShaderConstraint01(void)
122 TestApplication application;
124 tet_infoline("Test that a non-uniform shader property can be constrained");
126 Shader shader = Shader::New(VertexSource, FragmentSource);
127 Geometry geometry = CreateQuadGeometry();
128 Renderer renderer = Renderer::New( geometry, shader );
130 Actor actor = Actor::New();
131 actor.AddRenderer(renderer);
132 actor.SetSize(400, 400);
133 Stage::GetCurrent().Add(actor);
135 Vector4 initialColor = Color::WHITE;
136 Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
138 application.SendNotification();
139 application.Render(0);
140 DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
143 Constraint constraint = Constraint::New<Vector4>( shader, colorIndex, TestConstraintNoBlue );
145 application.SendNotification();
146 application.Render(0);
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 );
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 );
162 int UtcDaliShaderConstraint02(void)
164 TestApplication application;
166 tet_infoline("Test that a uniform map shader property can be constrained");
168 Shader shader = Shader::New(VertexSource, FragmentSource);
169 Geometry geometry = CreateQuadGeometry();
170 Renderer renderer = Renderer::New( geometry, shader );
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);
179 Vector4 initialColor = Color::WHITE;
180 Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
182 TestGlAbstraction& gl = application.GetGlAbstraction();
184 application.SendNotification();
185 application.Render(0);
187 Vector4 actualValue(Vector4::ZERO);
188 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
189 DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
192 Constraint constraint = Constraint::New<Vector4>( shader, colorIndex, TestConstraintNoBlue );
194 application.SendNotification();
195 application.Render(0);
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 );
201 application.Render(0);
202 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
203 DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
205 shader.RemoveConstraints();
206 shader.SetProperty(colorIndex, Color::WHITE );
207 application.SendNotification();
208 application.Render(0);
210 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
211 DALI_TEST_EQUALS( actualValue, Color::WHITE, TEST_LOCATION );
216 int UtcDaliShaderAnimatedProperty01(void)
218 TestApplication application;
220 tet_infoline("Test that a non-uniform shader property can be animated");
222 Shader shader = Shader::New(VertexSource, FragmentSource);
223 Geometry geometry = CreateQuadGeometry();
224 Renderer renderer = Renderer::New( geometry, shader );
226 Actor actor = Actor::New();
227 actor.AddRenderer(renderer);
228 actor.SetSize(400, 400);
229 Stage::GetCurrent().Add(actor);
231 Vector4 initialColor = Color::WHITE;
232 Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
234 application.SendNotification();
235 application.Render(0);
236 DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
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 );
245 application.SendNotification();
246 application.Render(500);
248 DALI_TEST_EQUALS( DevelHandle::GetCurrentProperty< Vector4 >( shader, colorIndex ), Color::WHITE * 0.5f, TEST_LOCATION );
250 application.Render(500);
252 DALI_TEST_EQUALS( DevelHandle::GetCurrentProperty< Vector4 >( shader, colorIndex ), Color::TRANSPARENT, TEST_LOCATION );
257 int UtcDaliShaderAnimatedProperty02(void)
259 TestApplication application;
261 tet_infoline("Test that a uniform map shader property can be animated");
263 Shader shader = Shader::New(VertexSource, FragmentSource);
264 Geometry geometry = CreateQuadGeometry();
265 Renderer renderer = Renderer::New( geometry, shader );
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);
274 Vector4 initialColor = Color::WHITE;
275 Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
277 TestGlAbstraction& gl = application.GetGlAbstraction();
279 application.SendNotification();
280 application.Render(0);
282 Vector4 actualValue(Vector4::ZERO);
283 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
284 DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
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 );
293 application.SendNotification();
294 application.Render(500);
296 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
297 DALI_TEST_EQUALS( actualValue, Color::WHITE * 0.5f, TEST_LOCATION );
299 application.Render(500);
300 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
301 DALI_TEST_EQUALS( actualValue, Color::TRANSPARENT, TEST_LOCATION );
306 int UtcDaliShaderProgramProperty(void)
308 TestApplication application;
310 tet_infoline("Test get/set progam property");
312 Shader shader = Shader::New("", "");
313 std::string hintSet = "MODIFIES_GEOMETRY";
316 map["vertex"] = VertexSource;
317 map["fragment"] = FragmentSource;
318 map["hints"] = hintSet;
320 shader.SetProperty( Shader::Property::PROGRAM, Property::Value(map) );
322 Property::Value value = shader.GetProperty(Shader::Property::PROGRAM);
323 DALI_TEST_CHECK( value.GetType() == Property::MAP);
324 const Property::Map* outMap = value.GetMap();
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>();
330 DALI_TEST_CHECK( v == VertexSource );
331 DALI_TEST_CHECK( f == FragmentSource );
332 DALI_TEST_CHECK( h == hintSet );
334 value = DevelHandle::GetCurrentProperty( shader, Shader::Property::PROGRAM );
335 DALI_TEST_CHECK( value.GetType() == Property::MAP);
336 outMap = value.GetMap();
338 v = (*outMap)["vertex"].Get<std::string>();
339 f = (*outMap)["fragment"].Get<std::string>();
340 h = (*outMap)["hints"].Get<std::string>();
342 DALI_TEST_CHECK( v == VertexSource );
343 DALI_TEST_CHECK( f == FragmentSource );
344 DALI_TEST_CHECK( h == hintSet );
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 );
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 );
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 );
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" );