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 UtcDaliShaderDownCast01(void)
78 TestApplication application;
80 Shader shader = Shader::New(VertexSource, FragmentSource);
82 BaseHandle handle(shader);
83 Shader shader2 = Shader::DownCast(handle);
84 DALI_TEST_EQUALS( (bool)shader2, true, TEST_LOCATION );
88 int UtcDaliShaderDownCast02(void)
90 TestApplication application;
92 Handle handle = Handle::New(); // Create a custom object
93 Shader shader = Shader::DownCast(handle);
94 DALI_TEST_EQUALS( (bool)shader, false, TEST_LOCATION );
98 int UtcDaliShaderConstraint01(void)
100 TestApplication application;
102 tet_infoline("Test that a non-uniform shader property can be constrained");
104 Shader shader = Shader::New(VertexSource, FragmentSource);
105 Material material = Material::New( shader );
106 material.SetProperty(Material::Property::COLOR, Color::WHITE);
108 Geometry geometry = CreateQuadGeometry();
109 Renderer renderer = Renderer::New( geometry, material );
111 Actor actor = Actor::New();
112 actor.AddRenderer(renderer);
113 actor.SetSize(400, 400);
114 Stage::GetCurrent().Add(actor);
116 Vector4 initialColor = Color::WHITE;
117 Property::Index colorIndex = shader.RegisterProperty( "fade-color", initialColor );
119 application.SendNotification();
120 application.Render(0);
121 DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
124 Constraint constraint = Constraint::New<Vector4>( shader, colorIndex, TestConstraintNoBlue );
126 application.SendNotification();
127 application.Render(0);
129 // Expect no blue component in either buffer - yellow
130 DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
131 application.Render(0);
132 DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
134 shader.RemoveConstraints();
135 shader.SetProperty(colorIndex, Color::WHITE );
136 application.SendNotification();
137 application.Render(0);
138 DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::WHITE, TEST_LOCATION );
143 int UtcDaliShaderConstraint02(void)
145 TestApplication application;
147 tet_infoline("Test that a uniform map shader property can be constrained");
149 Shader shader = Shader::New(VertexSource, FragmentSource);
150 Material material = Material::New( shader );
151 material.SetProperty(Material::Property::COLOR, Color::WHITE);
153 Geometry geometry = CreateQuadGeometry();
154 Renderer renderer = Renderer::New( geometry, material );
156 Actor actor = Actor::New();
157 actor.AddRenderer(renderer);
158 actor.SetSize(400, 400);
159 Stage::GetCurrent().Add(actor);
160 application.SendNotification();
161 application.Render(0);
163 Vector4 initialColor = Color::WHITE;
164 Property::Index colorIndex = shader.RegisterProperty( "fade-color", initialColor );
165 shader.AddUniformMapping( colorIndex, std::string("uFadeColor") );
167 TestGlAbstraction& gl = application.GetGlAbstraction();
169 application.SendNotification();
170 application.Render(0);
172 Vector4 actualValue(Vector4::ZERO);
173 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
174 DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
177 Constraint constraint = Constraint::New<Vector4>( shader, colorIndex, TestConstraintNoBlue );
179 application.SendNotification();
180 application.Render(0);
182 // Expect no blue component in either buffer - yellow
183 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
184 DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
186 application.Render(0);
187 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
188 DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
190 shader.RemoveConstraints();
191 shader.SetProperty(colorIndex, Color::WHITE );
192 application.SendNotification();
193 application.Render(0);
195 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
196 DALI_TEST_EQUALS( actualValue, Color::WHITE, TEST_LOCATION );
203 int UtcDaliShaderAnimatedProperty01(void)
205 TestApplication application;
207 tet_infoline("Test that a non-uniform shader property can be animated");
209 Shader shader = Shader::New(VertexSource, FragmentSource);
210 Material material = Material::New( shader );
211 material.SetProperty(Material::Property::COLOR, Color::WHITE);
213 Geometry geometry = CreateQuadGeometry();
214 Renderer renderer = Renderer::New( geometry, material );
216 Actor actor = Actor::New();
217 actor.AddRenderer(renderer);
218 actor.SetSize(400, 400);
219 Stage::GetCurrent().Add(actor);
221 Vector4 initialColor = Color::WHITE;
222 Property::Index colorIndex = shader.RegisterProperty( "fade-color", initialColor );
224 application.SendNotification();
225 application.Render(0);
226 DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
228 Animation animation = Animation::New(1.0f);
229 KeyFrames keyFrames = KeyFrames::New();
230 keyFrames.Add(0.0f, initialColor);
231 keyFrames.Add(1.0f, Color::TRANSPARENT);
232 animation.AnimateBetween( Property( shader, colorIndex ), keyFrames );
235 application.SendNotification();
236 application.Render(500);
238 DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::WHITE * 0.5f, TEST_LOCATION );
240 application.Render(500);
242 DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::TRANSPARENT, TEST_LOCATION );
247 int UtcDaliShaderAnimatedProperty02(void)
249 TestApplication application;
251 tet_infoline("Test that a uniform map shader property can be animated");
253 Shader shader = Shader::New(VertexSource, FragmentSource);
254 Material material = Material::New( shader );
255 material.SetProperty(Material::Property::COLOR, Color::WHITE);
257 Geometry geometry = CreateQuadGeometry();
258 Renderer renderer = Renderer::New( geometry, material );
260 Actor actor = Actor::New();
261 actor.AddRenderer(renderer);
262 actor.SetSize(400, 400);
263 Stage::GetCurrent().Add(actor);
264 application.SendNotification();
265 application.Render(0);
267 Vector4 initialColor = Color::WHITE;
268 Property::Index colorIndex = shader.RegisterProperty( "fade-color", initialColor );
269 shader.AddUniformMapping( colorIndex, std::string("uFadeColor") );
271 TestGlAbstraction& gl = application.GetGlAbstraction();
273 application.SendNotification();
274 application.Render(0);
276 Vector4 actualValue(Vector4::ZERO);
277 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
278 DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
280 Animation animation = Animation::New(1.0f);
281 KeyFrames keyFrames = KeyFrames::New();
282 keyFrames.Add(0.0f, initialColor);
283 keyFrames.Add(1.0f, Color::TRANSPARENT);
284 animation.AnimateBetween( Property( shader, colorIndex ), keyFrames );
287 application.SendNotification();
288 application.Render(500);
290 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
291 DALI_TEST_EQUALS( actualValue, Color::WHITE * 0.5f, TEST_LOCATION );
293 application.Render(500);
294 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
295 DALI_TEST_EQUALS( actualValue, Color::TRANSPARENT, TEST_LOCATION );