Remove uniform mappings.
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-PropertyBuffer.cpp
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 <dali/public-api/dali-core.h>
19 #include <dali-test-suite-utils.h>
20
21 using namespace Dali;
22
23 #include <mesh-builder.h>
24
25 namespace
26 {
27 void TestConstraintNoBlue( Vector4& current, const PropertyInputContainer& inputs )
28 {
29   current.b = 0.0f;
30 }
31 }
32
33 void propertyBuffer_test_startup(void)
34 {
35   test_return_value = TET_UNDEF;
36 }
37
38 void propertyBuffer_test_cleanup(void)
39 {
40   test_return_value = TET_PASS;
41 }
42
43 int UtcDaliPropertyBufferNew01(void)
44 {
45   TestApplication application;
46
47   Property::Map texturedQuadVertexFormat;
48   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
49   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
50
51   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat, 4 );
52
53   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
54   END_TEST;
55 }
56
57 int UtcDaliPropertyBufferNew02(void)
58 {
59   TestApplication application;
60   PropertyBuffer propertyBuffer;
61   DALI_TEST_EQUALS( (bool)propertyBuffer, false, TEST_LOCATION );
62   END_TEST;
63 }
64
65 int UtcDaliPropertyBufferDownCast01(void)
66 {
67   TestApplication application;
68
69   Property::Map texturedQuadVertexFormat;
70   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
71   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
72
73   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat, 4 );
74
75   BaseHandle handle(propertyBuffer);
76   PropertyBuffer propertyBuffer2 = PropertyBuffer::DownCast(handle);
77   DALI_TEST_EQUALS( (bool)propertyBuffer2, true, TEST_LOCATION );
78   END_TEST;
79 }
80
81 int UtcDaliPropertyBufferDownCast02(void)
82 {
83   TestApplication application;
84
85   Handle handle = Handle::New(); // Create a custom object
86   PropertyBuffer propertyBuffer = PropertyBuffer::DownCast(handle);
87   DALI_TEST_EQUALS( (bool)propertyBuffer, false, TEST_LOCATION );
88   END_TEST;
89 }
90
91
92 int UtcDaliPropertyBufferConstraint01(void)
93 {
94   TestApplication application;
95
96   tet_infoline("Test that a non-uniform propertyBuffer property can be constrained");
97
98   Shader shader = Shader::New("VertexSource", "FragmentSource");
99   Material material = Material::New( shader );
100   material.SetProperty(Material::Property::COLOR, Color::WHITE);
101
102   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
103   Geometry geometry = CreateQuadGeometryFromBuffer(propertyBuffer);
104   Renderer renderer = Renderer::New( geometry, material );
105
106   Actor actor = Actor::New();
107   actor.AddRenderer(renderer);
108   actor.SetSize(400, 400);
109   Stage::GetCurrent().Add(actor);
110
111   Vector4 initialColor = Color::WHITE;
112   Property::Index colorIndex = propertyBuffer.RegisterProperty( "uFadeColor", initialColor );
113
114   application.SendNotification();
115   application.Render(0);
116   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
117
118   // Apply constraint
119   Constraint constraint = Constraint::New<Vector4>( propertyBuffer, colorIndex, TestConstraintNoBlue );
120   constraint.Apply();
121   application.SendNotification();
122   application.Render(0);
123
124   // Expect no blue component in either buffer - yellow
125   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
126   application.Render(0);
127   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
128
129   propertyBuffer.RemoveConstraints();
130   propertyBuffer.SetProperty(colorIndex, Color::WHITE );
131   application.SendNotification();
132   application.Render(0);
133   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), Color::WHITE, TEST_LOCATION );
134
135   END_TEST;
136 }
137
138 int UtcDaliPropertyBufferConstraint02(void)
139 {
140   TestApplication application;
141
142   tet_infoline("Test that a uniform map propertyBuffer property can be constrained");
143
144   Shader shader = Shader::New( "VertexSource", "FragmentSource" );
145   Material material = Material::New( shader );
146   material.SetProperty(Material::Property::COLOR, Color::WHITE);
147
148   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
149   Geometry geometry = CreateQuadGeometryFromBuffer(propertyBuffer);
150   Renderer renderer = Renderer::New( geometry, material );
151
152   Actor actor = Actor::New();
153   actor.AddRenderer(renderer);
154   actor.SetSize(400, 400);
155   Stage::GetCurrent().Add(actor);
156   application.SendNotification();
157   application.Render(0);
158
159   Vector4 initialColor = Color::WHITE;
160   Property::Index colorIndex = propertyBuffer.RegisterProperty( "uFadeColor", initialColor );
161
162   TestGlAbstraction& gl = application.GetGlAbstraction();
163
164   application.SendNotification();
165   application.Render(0);
166
167   Vector4 actualValue(Vector4::ZERO);
168   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
169   DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
170
171   // Apply constraint
172   Constraint constraint = Constraint::New<Vector4>( propertyBuffer, colorIndex, TestConstraintNoBlue );
173   constraint.Apply();
174   application.SendNotification();
175   application.Render(0);
176
177    // Expect no blue component in either buffer - yellow
178   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
179   DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
180
181   application.Render(0);
182   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
183   DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
184
185   propertyBuffer.RemoveConstraints();
186   propertyBuffer.SetProperty(colorIndex, Color::WHITE );
187   application.SendNotification();
188   application.Render(0);
189
190   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
191   DALI_TEST_EQUALS( actualValue, Color::WHITE, TEST_LOCATION );
192
193   END_TEST;
194 }
195
196
197
198 int UtcDaliPropertyBufferAnimatedProperty01(void)
199 {
200   TestApplication application;
201
202   tet_infoline("Test that a non-uniform propertyBuffer property can be animated");
203
204   Shader shader = Shader::New("VertexSource", "FragmentSource");
205   Material material = Material::New( shader );
206   material.SetProperty(Material::Property::COLOR, Color::WHITE);
207
208   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
209   Geometry geometry = CreateQuadGeometryFromBuffer(propertyBuffer);
210   Renderer renderer = Renderer::New( geometry, material );
211
212   Actor actor = Actor::New();
213   actor.AddRenderer(renderer);
214   actor.SetSize(400, 400);
215   Stage::GetCurrent().Add(actor);
216
217   Vector4 initialColor = Color::WHITE;
218   Property::Index colorIndex = propertyBuffer.RegisterProperty( "uFadeColor", initialColor );
219
220   application.SendNotification();
221   application.Render(0);
222   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
223
224   Animation  animation = Animation::New(1.0f);
225   KeyFrames keyFrames = KeyFrames::New();
226   keyFrames.Add(0.0f, initialColor);
227   keyFrames.Add(1.0f, Color::TRANSPARENT);
228   animation.AnimateBetween( Property( propertyBuffer, colorIndex ), keyFrames );
229   animation.Play();
230
231   application.SendNotification();
232   application.Render(500);
233
234   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), Color::WHITE * 0.5f, TEST_LOCATION );
235
236   application.Render(500);
237
238   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), Color::TRANSPARENT, TEST_LOCATION );
239
240   END_TEST;
241 }
242
243 int UtcDaliPropertyBufferAnimatedProperty02(void)
244 {
245   TestApplication application;
246
247   tet_infoline("Test that a uniform map propertyBuffer property can be animated");
248
249   Shader shader = Shader::New("VertexSource", "FragmentSource");
250   Material material = Material::New( shader );
251   material.SetProperty(Material::Property::COLOR, Color::WHITE);
252
253   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
254   Geometry geometry = CreateQuadGeometryFromBuffer(propertyBuffer);
255   Renderer renderer = Renderer::New( geometry, material );
256
257   Actor actor = Actor::New();
258   actor.AddRenderer(renderer);
259   actor.SetSize(400, 400);
260   Stage::GetCurrent().Add(actor);
261   application.SendNotification();
262   application.Render(0);
263
264   Vector4 initialColor = Color::WHITE;
265   Property::Index colorIndex = propertyBuffer.RegisterProperty( "uFadeColor", initialColor );
266
267   TestGlAbstraction& gl = application.GetGlAbstraction();
268
269   application.SendNotification();
270   application.Render(0);
271
272   Vector4 actualValue(Vector4::ZERO);
273   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
274   DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
275
276   Animation  animation = Animation::New(1.0f);
277   KeyFrames keyFrames = KeyFrames::New();
278   keyFrames.Add(0.0f, initialColor);
279   keyFrames.Add(1.0f, Color::TRANSPARENT);
280   animation.AnimateBetween( Property( propertyBuffer, colorIndex ), keyFrames );
281   animation.Play();
282
283   application.SendNotification();
284   application.Render(500);
285
286   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
287   DALI_TEST_EQUALS( actualValue, Color::WHITE * 0.5f, TEST_LOCATION );
288
289   application.Render(500);
290   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
291   DALI_TEST_EQUALS( actualValue, Color::TRANSPARENT, TEST_LOCATION );
292
293   END_TEST;
294 }
295
296 int UtcDaliPropertyBufferSetData01(void)
297 {
298   TestApplication application;
299
300   Property::Map texturedQuadVertexFormat;
301   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
302   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
303
304   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat, 4 );
305   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
306
307   const float halfQuadSize = .5f;
308   struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
309   TexturedQuadVertex texturedQuadVertexData[4] = {
310     { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
311     { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
312     { Vector2(-halfQuadSize,  halfQuadSize), Vector2(0.f, 1.f) },
313     { Vector2( halfQuadSize,  halfQuadSize), Vector2(1.f, 1.f) } };
314
315   propertyBuffer.SetData( texturedQuadVertexData );
316
317   Geometry geometry = Geometry::New();
318   geometry.AddVertexBuffer( propertyBuffer );
319
320   Material material = CreateMaterial(1.f);
321   Renderer renderer = Renderer::New(geometry, material);
322   Actor actor = Actor::New();
323   actor.SetSize(Vector3::ONE * 100.f);
324   actor.AddRenderer(renderer);
325   Stage::GetCurrent().Add(actor);
326
327   application.SendNotification();
328   application.Render(0);
329   application.Render();
330   application.SendNotification();
331
332   const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
333       application.GetGlAbstraction().GetBufferDataCalls();
334
335   DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
336
337   DALI_TEST_EQUALS( bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
338
339   END_TEST;
340 }
341
342 int UtcDaliPropertyBufferSetData02(void)
343 {
344   TestApplication application;
345
346   Property::Map texturedQuadVertexFormat;
347   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
348   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
349
350   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat, 4 );
351   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
352
353   const float halfQuadSize = .5f;
354   struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
355   TexturedQuadVertex texturedQuadVertexData[4] = {
356     { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
357     { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
358     { Vector2(-halfQuadSize,  halfQuadSize), Vector2(0.f, 1.f) },
359     { Vector2( halfQuadSize,  halfQuadSize), Vector2(1.f, 1.f) } };
360
361   propertyBuffer.SetData( texturedQuadVertexData );
362
363   Geometry geometry = Geometry::New();
364   geometry.AddVertexBuffer( propertyBuffer );
365
366   Material material = CreateMaterial(1.f);
367   Renderer renderer = Renderer::New(geometry, material);
368   Actor actor = Actor::New();
369   actor.SetSize(Vector3::ONE * 100.f);
370   actor.AddRenderer(renderer);
371   Stage::GetCurrent().Add(actor);
372
373   application.SendNotification();
374   application.Render(0);
375   application.Render();
376   application.SendNotification();
377
378   {
379     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
380       application.GetGlAbstraction().GetBufferDataCalls();
381
382     DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
383
384     DALI_TEST_EQUALS( bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
385   }
386
387   // Re-upload the data on the propertyBuffer
388   propertyBuffer.SetData( texturedQuadVertexData );
389
390   application.SendNotification();
391   application.Render(0);
392   application.Render();
393   application.SendNotification();
394
395   {
396     const TestGlAbstraction::BufferSubDataCalls& bufferSubDataCalls =
397       application.GetGlAbstraction().GetBufferSubDataCalls();
398
399     DALI_TEST_EQUALS( bufferSubDataCalls.size(), 1u, TEST_LOCATION );
400
401     if ( bufferSubDataCalls.size() )
402     {
403       DALI_TEST_EQUALS( bufferSubDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
404     }
405   }
406
407   END_TEST;
408 }
409
410 int UtcDaliPropertyBufferSetSize01(void)
411 {
412   TestApplication application;
413
414   Property::Map texturedQuadVertexFormat;
415   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
416   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
417
418   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat, 4u );
419   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
420
421   size_t size = propertyBuffer.GetSize();
422   DALI_TEST_EQUALS( size, 4u, TEST_LOCATION );
423
424   propertyBuffer.SetSize( 10u );
425   size = propertyBuffer.GetSize();
426   DALI_TEST_EQUALS( size, 10u, TEST_LOCATION );
427
428   END_TEST;
429 }
430