2 * Copyright (c) 2015 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 UtcDaliShaderConstraint01(void)
121 TestApplication application;
123 tet_infoline("Test that a non-uniform shader property can be constrained");
125 Shader shader = Shader::New(VertexSource, FragmentSource);
126 Geometry geometry = CreateQuadGeometry();
127 Renderer renderer = Renderer::New( geometry, shader );
129 Actor actor = Actor::New();
130 actor.AddRenderer(renderer);
131 actor.SetSize(400, 400);
132 Stage::GetCurrent().Add(actor);
134 Vector4 initialColor = Color::WHITE;
135 Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
137 application.SendNotification();
138 application.Render(0);
139 DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
142 Constraint constraint = Constraint::New<Vector4>( shader, colorIndex, TestConstraintNoBlue );
144 application.SendNotification();
145 application.Render(0);
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 );
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 );
161 int UtcDaliShaderConstraint02(void)
163 TestApplication application;
165 tet_infoline("Test that a uniform map shader property can be constrained");
167 Shader shader = Shader::New(VertexSource, FragmentSource);
168 Geometry geometry = CreateQuadGeometry();
169 Renderer renderer = Renderer::New( geometry, shader );
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);
178 Vector4 initialColor = Color::WHITE;
179 Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
181 TestGlAbstraction& gl = application.GetGlAbstraction();
183 application.SendNotification();
184 application.Render(0);
186 Vector4 actualValue(Vector4::ZERO);
187 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
188 DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
191 Constraint constraint = Constraint::New<Vector4>( shader, colorIndex, TestConstraintNoBlue );
193 application.SendNotification();
194 application.Render(0);
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 );
200 application.Render(0);
201 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
202 DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
204 shader.RemoveConstraints();
205 shader.SetProperty(colorIndex, Color::WHITE );
206 application.SendNotification();
207 application.Render(0);
209 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
210 DALI_TEST_EQUALS( actualValue, Color::WHITE, TEST_LOCATION );
215 int UtcDaliShaderAnimatedProperty01(void)
217 TestApplication application;
219 tet_infoline("Test that a non-uniform shader property can be animated");
221 Shader shader = Shader::New(VertexSource, FragmentSource);
222 Geometry geometry = CreateQuadGeometry();
223 Renderer renderer = Renderer::New( geometry, shader );
225 Actor actor = Actor::New();
226 actor.AddRenderer(renderer);
227 actor.SetSize(400, 400);
228 Stage::GetCurrent().Add(actor);
230 Vector4 initialColor = Color::WHITE;
231 Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
233 application.SendNotification();
234 application.Render(0);
235 DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
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 );
244 application.SendNotification();
245 application.Render(500);
247 DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::WHITE * 0.5f, TEST_LOCATION );
249 application.Render(500);
251 DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::TRANSPARENT, TEST_LOCATION );
256 int UtcDaliShaderAnimatedProperty02(void)
258 TestApplication application;
260 tet_infoline("Test that a uniform map shader property can be animated");
262 Shader shader = Shader::New(VertexSource, FragmentSource);
263 Geometry geometry = CreateQuadGeometry();
264 Renderer renderer = Renderer::New( geometry, shader );
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);
273 Vector4 initialColor = Color::WHITE;
274 Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
276 TestGlAbstraction& gl = application.GetGlAbstraction();
278 application.SendNotification();
279 application.Render(0);
281 Vector4 actualValue(Vector4::ZERO);
282 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
283 DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
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 );
292 application.SendNotification();
293 application.Render(500);
295 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
296 DALI_TEST_EQUALS( actualValue, Color::WHITE * 0.5f, TEST_LOCATION );
298 application.Render(500);
299 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
300 DALI_TEST_EQUALS( actualValue, Color::TRANSPARENT, TEST_LOCATION );
305 int UtcDaliShaderProgramProperty(void)
307 TestApplication application;
309 tet_infoline("Test get/set progam property");
311 Shader shader = Shader::New("", "");
312 std::string hintSet = "MODIFIES_GEOMETRY";
315 map["vertex"] = VertexSource;
316 map["fragment"] = FragmentSource;
317 map["hints"] = hintSet;
319 shader.SetProperty( Shader::Property::PROGRAM, Property::Value(map) );
321 Property::Value value = shader.GetProperty(Shader::Property::PROGRAM);
322 DALI_TEST_CHECK( value.GetType() == Property::MAP);
323 const Property::Map* outMap = value.GetMap();
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>();
329 DALI_TEST_CHECK( v == VertexSource );
330 DALI_TEST_CHECK( f == FragmentSource );
331 DALI_TEST_CHECK( h == hintSet );
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 );
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 );
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 );
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" );