Dali 3D User Interface Engine
utc-Dali-Shader.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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>
22 #include <dali-test-suite-utils.h>
23 #include <mesh-builder.h>
24 
25 using namespace Dali;
26 
28 {
30 }
31 
33 {
35 }
36 
37 namespace
38 {
39 
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";
43 
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";
47 
48 
49 void TestConstraintNoBlue( Vector4& current, const PropertyInputContainer& inputs )
50 {
51  current.b = 0.0f;
52 }
53 
54 
55 } // anon namespace
56 
57 
59 {
60  TestApplication application;
61 
63  DALI_TEST_EQUALS((bool)shader, true, TEST_LOCATION);
64  END_TEST;
65 }
66 
68 {
69  TestApplication application;
70 
71  Shader shader;
72  DALI_TEST_EQUALS((bool)shader, false, TEST_LOCATION);
73  END_TEST;
74 }
75 
77 {
78  TestApplication application;
79 
81 
82  Shader shader2;
83 
84  DALI_TEST_CHECK(!(shader1 == shader2));
85 
86  shader2 = shader1;
87 
88  DALI_TEST_CHECK(shader1 == shader2);
89 
91 
92  DALI_TEST_CHECK(!(shader1 == shader2));
93 
94  END_TEST;
95 }
96 
98 {
99  TestApplication application;
100 
102 
103  BaseHandle handle(shader);
104  Shader shader2 = Shader::DownCast(handle);
105  DALI_TEST_EQUALS( (bool)shader2, true, TEST_LOCATION );
106  END_TEST;
107 }
108 
110 {
111  TestApplication application;
112 
113  Handle handle = Handle::New(); // Create a custom object
114  Shader shader = Shader::DownCast(handle);
115  DALI_TEST_EQUALS( (bool)shader, false, TEST_LOCATION );
116  END_TEST;
117 }
118 
120 {
121  TestApplication application;
122 
123  tet_infoline("Test that a non-uniform shader property can be constrained");
124 
126  Material material = Material::New( shader );
127 
128  Geometry geometry = CreateQuadGeometry();
129  Renderer renderer = Renderer::New( geometry, material );
130 
131  Actor actor = Actor::New();
132  actor.AddRenderer(renderer);
133  actor.SetSize(400, 400);
134  Stage::GetCurrent().Add(actor);
135 
136  Vector4 initialColor = Color::WHITE;
137  Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
138 
139  application.SendNotification();
140  application.Render(0);
141  DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
142 
143  // Apply constraint
144  Constraint constraint = Constraint::New<Vector4>( shader, colorIndex, TestConstraintNoBlue );
145  constraint.Apply();
146  application.SendNotification();
147  application.Render(0);
148 
149  // Expect no blue component in either buffer - yellow
150  DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
151  application.Render(0);
152  DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
153 
154  shader.RemoveConstraints();
155  shader.SetProperty(colorIndex, Color::WHITE );
156  application.SendNotification();
157  application.Render(0);
158  DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::WHITE, TEST_LOCATION );
159 
160  END_TEST;
161 }
162 
164 {
165  TestApplication application;
166 
167  tet_infoline("Test that a uniform map shader property can be constrained");
168 
170  Material material = Material::New( shader );
171 
172  Geometry geometry = CreateQuadGeometry();
173  Renderer renderer = Renderer::New( geometry, material );
174 
175  Actor actor = Actor::New();
176  actor.AddRenderer(renderer);
177  actor.SetSize(400, 400);
178  Stage::GetCurrent().Add(actor);
179  application.SendNotification();
180  application.Render(0);
181 
182  Vector4 initialColor = Color::WHITE;
183  Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
184 
185  TestGlAbstraction& gl = application.GetGlAbstraction();
186 
187  application.SendNotification();
188  application.Render(0);
189 
190  Vector4 actualValue(Vector4::ZERO);
191  DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
192  DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
193 
194  // Apply constraint
195  Constraint constraint = Constraint::New<Vector4>( shader, colorIndex, TestConstraintNoBlue );
196  constraint.Apply();
197  application.SendNotification();
198  application.Render(0);
199 
200  // Expect no blue component in either buffer - yellow
201  DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
203 
204  application.Render(0);
205  DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
207 
208  shader.RemoveConstraints();
209  shader.SetProperty(colorIndex, Color::WHITE );
210  application.SendNotification();
211  application.Render(0);
212 
213  DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
215 
216  END_TEST;
217 }
218 
220 {
221  TestApplication application;
222 
223  tet_infoline("Test that a non-uniform shader property can be animated");
224 
226  Material material = Material::New( shader );
227 
228  Geometry geometry = CreateQuadGeometry();
229  Renderer renderer = Renderer::New( geometry, material );
230 
231  Actor actor = Actor::New();
232  actor.AddRenderer(renderer);
233  actor.SetSize(400, 400);
234  Stage::GetCurrent().Add(actor);
235 
236  Vector4 initialColor = Color::WHITE;
237  Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
238 
239  application.SendNotification();
240  application.Render(0);
241  DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
242 
243  Animation animation = Animation::New(1.0f);
244  KeyFrames keyFrames = KeyFrames::New();
245  keyFrames.Add(0.0f, initialColor);
246  keyFrames.Add(1.0f, Color::TRANSPARENT);
247  animation.AnimateBetween( Property( shader, colorIndex ), keyFrames );
248  animation.Play();
249 
250  application.SendNotification();
251  application.Render(500);
252 
253  DALI_TEST_EQUALS( shader.GetProperty<Vector4>(colorIndex), Color::WHITE * 0.5f, TEST_LOCATION );
254 
255  application.Render(500);
256 
258 
259  END_TEST;
260 }
261 
263 {
264  TestApplication application;
265 
266  tet_infoline("Test that a uniform map shader property can be animated");
267 
269  Material material = Material::New( shader );
270 
271  Geometry geometry = CreateQuadGeometry();
272  Renderer renderer = Renderer::New( geometry, material );
273 
274  Actor actor = Actor::New();
275  actor.AddRenderer(renderer);
276  actor.SetSize(400, 400);
277  Stage::GetCurrent().Add(actor);
278  application.SendNotification();
279  application.Render(0);
280 
281  Vector4 initialColor = Color::WHITE;
282  Property::Index colorIndex = shader.RegisterProperty( "uFadeColor", initialColor );
283 
284  TestGlAbstraction& gl = application.GetGlAbstraction();
285 
286  application.SendNotification();
287  application.Render(0);
288 
289  Vector4 actualValue(Vector4::ZERO);
290  DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
291  DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
292 
293  Animation animation = Animation::New(1.0f);
294  KeyFrames keyFrames = KeyFrames::New();
295  keyFrames.Add(0.0f, initialColor);
296  keyFrames.Add(1.0f, Color::TRANSPARENT);
297  animation.AnimateBetween( Property( shader, colorIndex ), keyFrames );
298  animation.Play();
299 
300  application.SendNotification();
301  application.Render(500);
302 
303  DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
304  DALI_TEST_EQUALS( actualValue, Color::WHITE * 0.5f, TEST_LOCATION );
305 
306  application.Render(500);
307  DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
309 
310  END_TEST;
311 }
312 
314 {
315  TestApplication application;
316 
317  tet_infoline("Test get/set progam property");
318 
319  Shader shader = Shader::New("", "");
320  std::string hintSet = "HINT_REQUIRES_SELF_DEPTH_TEST,HINT_MODIFIES_GEOMETRY";
321 
322  Property::Map map;
323  map["vertex"] = VertexSource;
324  map["fragment"] = FragmentSource;
325  map["hints"] = hintSet;
326 
328 
330  DALI_TEST_CHECK( value.GetType() == Property::MAP);
331  const Property::Map* outMap = value.GetMap();
332 
333  std::string v = (*outMap)["vertex"].Get<std::string>();
334  std::string f = (*outMap)["fragment"].Get<std::string>();
335  std::string h = (*outMap)["hints"].Get<std::string>();
336 
339  DALI_TEST_CHECK( h == hintSet );
340 
341  std::string hintGot;
342 
343  hintSet = "HINT_REQUIRES_SELF_DEPTH_TEST,HINT_OUTPUT_IS_TRANSPARENT,HINT_OUTPUT_IS_OPAQUE,HINT_MODIFIES_GEOMETRY";
344  map["hints"] = hintSet;
346  value = shader.GetProperty(Shader::Property::PROGRAM);
347  hintGot = (*value.GetMap())["hints"].Get<std::string>();
348  DALI_TEST_CHECK( hintGot == hintSet );
349 
350  hintSet = "HINT_REQUIRES_SELF_DEPTH_TEST";
351  map["hints"] = hintSet;
353  value = shader.GetProperty(Shader::Property::PROGRAM);
354  hintGot = (*value.GetMap())["hints"].Get<std::string>();
355  DALI_TEST_CHECK( hintGot == hintSet );
356 
357  hintSet = "HINT_NONE";
358  map["hints"] = hintSet;
360  value = shader.GetProperty(Shader::Property::PROGRAM);
361  hintGot = (*value.GetMap())["hints"].Get<std::string>();
362  DALI_TEST_CHECK( hintGot == hintSet );
363 
364  hintSet = "";
365  map["hints"] = hintSet;
367  value = shader.GetProperty(Shader::Property::PROGRAM);
368  hintGot = (*value.GetMap())["hints"].Get<std::string>();
369  DALI_TEST_CHECK( hintGot == "HINT_NONE" );
370 
371  END_TEST;
372 }
Dali Docs Home
Read more about Dali