[dali_1.0.18] Merger branch 'tizen'
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-ShaderEffect.cpp
1 /*
2  * Copyright (c) 2014 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>
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23
24 using namespace Dali;
25
26 void utc_dali_shader_effect_startup(void)
27 {
28   test_return_value = TET_UNDEF;
29 }
30
31 void utc_dali_shader_effect_cleanup(void)
32 {
33   test_return_value = TET_PASS;
34 }
35
36 namespace
37 {
38
39 static const char* VertexSource =
40 "This is a custom vertex shader\n"
41 "made on purpose to look nothing like a normal vertex shader inside dali\n";
42
43 static const char* FragmentSource =
44 "This is a custom fragment shader\n"
45 "made on purpose to look nothing like a normal fragment shader inside dali\n";
46
47 static const char* FragmentSourceUsingExtensions =
48 "This is a custom fragment shader using extensions\n"
49 "made on purpose to look nothing like a normal fragment shader inside dali\n";
50
51 const int GETSOURCE_BUFFER_SIZE = 0x10000;
52
53
54 struct TestConstraintToVector3
55 {
56   TestConstraintToVector3(Vector3 target)
57   : mTarget(target)
58   {
59   }
60
61   Vector3 operator()(const Vector3& current)
62   {
63     return mTarget;
64   }
65
66   Vector3 mTarget;
67 };
68
69 struct TestConstraintFromPositionToVector3
70 {
71   TestConstraintFromPositionToVector3()
72   {
73   }
74
75   Vector3 operator()(const Vector3& current, const PropertyInput& position)
76   {
77
78     return position.GetVector3();
79   }
80 };
81
82 struct TestConstraintToVector3Double
83 {
84   TestConstraintToVector3Double(Vector3 target)
85   : mTarget(target)
86   {
87   }
88
89   Vector3 operator()(const Vector3& current)
90   {
91     return mTarget * 2.0f;
92   }
93
94   Vector3 mTarget;
95 };
96
97 class ShaderEffectExtension : public ShaderEffect::Extension {};
98
99
100 class TestExtension : public ShaderEffect::Extension
101 {
102 public:
103   TestExtension( bool& deleted )
104   : mDeleted(deleted)
105   {
106     mDeleted = false;
107   }
108   ~TestExtension()
109   {
110     mDeleted = true;
111   }
112   bool IsAlive() const
113   {
114     return !mDeleted;
115   }
116 private:
117   bool& mDeleted;
118 };
119
120 } // anon namespace
121
122
123 int UtcDaliShaderEffectMethodNew01(void)
124 {
125   TestApplication application;
126
127   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
128   DALI_TEST_CHECK(effect);
129   END_TEST;
130 }
131
132 int UtcDaliShaderEffectMethodNew02(void)
133 {
134   TestApplication application;
135
136   ShaderEffect effect;
137
138   try
139   {
140     // New must be called to create a ShaderEffect or it wont be valid.
141     effect.SetUniform( "uUniform", 0 );
142     DALI_TEST_CHECK( false );
143   }
144   catch (Dali::DaliException& e)
145   {
146     // Tests that a negative test of an assertion succeeds
147     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
148     DALI_TEST_CHECK( !effect );
149   }
150   END_TEST;
151 }
152
153 int UtcDaliShaderEffectMethodNew03(void)
154 {
155   TestApplication application;
156
157   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource, VertexSource, FragmentSource, ShaderEffect::HINT_NONE );
158   DALI_TEST_CHECK(effect);
159   END_TEST;
160 }
161
162 int UtcDaliShaderEffectMethodNew04(void)
163 {
164   TestApplication application;
165   tet_infoline("Testing prefixed version of Dali::ShaderEffect::New()");
166
167   std::string fragmentShaderPrefix = "#define TEST_FS 1\n#extension GL_OES_standard_derivatives : enable";
168   std::string vertexShaderPrefix = "#define TEST_VS 1";
169
170   try
171   {
172     // Call render to compile default shaders.
173     application.SendNotification();
174     application.Render();
175     application.Render();
176     application.Render();
177
178     GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
179     ShaderEffect effect = ShaderEffect::NewWithPrefix( vertexShaderPrefix, VertexSource,
180                                                        fragmentShaderPrefix, FragmentSourceUsingExtensions,
181                                                        GEOMETRY_TYPE_IMAGE, ShaderEffect::HINT_NONE );
182
183     BitmapImage image = CreateBitmapImage();
184     ImageActor actor = ImageActor::New( image );
185     actor.SetSize( 100.0f, 100.0f );
186     actor.SetName("TestImageFilenameActor");
187     actor.SetShaderEffect(effect);
188     Stage::GetCurrent().Add(actor);
189
190     application.SendNotification();
191     application.Render();
192     GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
193     bool testResult = false;
194
195     // we should have compiled 2 shaders.
196     DALI_TEST_EQUALS( lastShaderCompiledAfter, lastShaderCompiledBefore + 2, TEST_LOCATION );
197
198     char testVertexSourceResult[GETSOURCE_BUFFER_SIZE];
199     char testFragmentSourceResult[GETSOURCE_BUFFER_SIZE];
200
201      // we are interested in the first two.
202     GLuint vertexShaderId = lastShaderCompiledBefore + 1;
203     GLuint fragmentShaderId = lastShaderCompiledBefore + 2;
204
205     GLsizei lengthVertexResult;
206     GLsizei lengthFragmentResult;
207
208     application.GetGlAbstraction().GetShaderSource(vertexShaderId, GETSOURCE_BUFFER_SIZE, &lengthVertexResult, testVertexSourceResult);
209     application.GetGlAbstraction().GetShaderSource(fragmentShaderId, GETSOURCE_BUFFER_SIZE, &lengthFragmentResult, testFragmentSourceResult);
210
211     int vertexShaderHasPrefix = strncmp(testVertexSourceResult, "#define ", strlen("#define "));
212     int fragmentShaderHasPrefix = strncmp(testFragmentSourceResult, "#define ", strlen("#define "));
213     testResult = (vertexShaderHasPrefix == 0) && (fragmentShaderHasPrefix == 0);
214
215     DALI_TEST_CHECK(testResult);
216   }
217   catch(Dali::DaliException& e)
218   {
219     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
220     tet_result( TET_FAIL );
221   }
222   END_TEST;
223 }
224
225 int UtcDaliShaderEffectMethodNew05(void)
226 {
227   TestApplication application;
228
229   // heap constructor / destructor
230   DefaultFunctionCoverage<ShaderEffect> shaderEffect;
231   DefaultFunctionCoverage<ShaderEffectExtension> shaderEffectExtension;
232
233   END_TEST;
234 }
235
236 int UtcDaliShaderEffectMethodNew06(void)
237 {
238   TestApplication application;
239   tet_infoline("Testing Dali::ShaderEffect::New() with shader sources for different geometry types");
240
241   ShaderEffect effect = ShaderEffect::New( "imageVertexShader", "imageFragmentShader",
242                                            "textVertexShader", "textFragmentShader",
243                                            "texturedMeshVertexShader", "texturedMeshFragmentShader",
244                                            "meshVertexShader", "meshFragmentShader",
245                                            ShaderEffect::HINT_NONE );
246   DALI_TEST_CHECK(effect);
247   END_TEST;
248 }
249
250
251 int UtcDaliShaderEffectMethodDownCast(void)
252 {
253   TestApplication application;
254   tet_infoline("Testing Dali::ShaderEffect::DownCast()");
255
256   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
257
258   BaseHandle object(effect);
259
260   ShaderEffect effect2 = ShaderEffect::DownCast(object);
261   DALI_TEST_CHECK(effect2);
262
263   ShaderEffect effect3 = DownCast< ShaderEffect >(object);
264   DALI_TEST_CHECK(effect3);
265
266   BaseHandle unInitializedObject;
267   ShaderEffect effect4 = ShaderEffect::DownCast(unInitializedObject);
268   DALI_TEST_CHECK(!effect4);
269
270   ShaderEffect effect5 = DownCast< ShaderEffect >(unInitializedObject);
271   DALI_TEST_CHECK(!effect5);
272   END_TEST;
273 }
274
275 int UtcDaliShaderEffectMethodDelete01(void)
276 {
277    TestApplication application;
278
279    // get the default shaders built, this is not required but makes it
280    // easier to debug the TET case and isolate the custom shader compilation.
281    application.SendNotification();
282    application.Render();
283
284    GLuint beforeShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
285
286    // create a new shader effect
287    // the vertex and fragment shader will be cached in the ShaderFactory
288    ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
289
290    // destroy the shader effect
291    effect.Reset();
292
293    // Create the same shader effect again, this should now use the cached version
294    // held in the shader factory
295    effect= ShaderEffect::New( VertexSource, FragmentSource );
296
297    // Compile the shader effect
298    application.SendNotification();
299    application.Render();
300
301    GLuint lastShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
302    // no shaders were compiled as they are now compiled on demand and this shader was not used
303    DALI_TEST_EQUALS( beforeShaderCompiled, lastShaderCompiled, TEST_LOCATION );
304
305    END_TEST;
306 }
307
308 int UtcDaliShaderEffectMethodSetUniformFloat(void)
309 {
310   TestApplication application;
311
312   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
313   DALI_TEST_CHECK( effect );
314
315   BitmapImage image = CreateBitmapImage();
316
317   effect.SetUniform( "uFloat", 1.0f );
318
319   ImageActor actor = ImageActor::New( image );
320   actor.SetSize( 100.0f, 100.0f );
321   actor.SetName("TestImageFilenameActor");
322   actor.SetShaderEffect(effect);
323   Stage::GetCurrent().Add(actor);
324
325   application.SendNotification();
326   application.Render();
327
328   DALI_TEST_CHECK(
329       application.GetGlAbstraction().CheckUniformValue(
330           "uFloat", 1.0f ) );
331   END_TEST;
332 }
333
334 int UtcDaliShaderEffectMethodSetUniformVector2(void)
335 {
336   TestApplication application;
337
338   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
339   DALI_TEST_CHECK( effect );
340
341   BitmapImage image = CreateBitmapImage();
342
343   effect.SetUniform( "uVec2", Vector2( 2.0f, 3.0f ) );
344
345   ImageActor actor = ImageActor::New( image );
346   actor.SetSize( 100.0f, 100.0f );
347   actor.SetName("TestImageFilenameActor");
348   actor.SetShaderEffect(effect);
349   Stage::GetCurrent().Add(actor);
350
351   application.SendNotification();
352   application.Render();
353
354   DALI_TEST_CHECK(
355       application.GetGlAbstraction().CheckUniformValue(
356           "uVec2", Vector2( 2.0f, 3.0f ) ) );
357   END_TEST;
358 }
359
360 int UtcDaliShaderEffectMethodSetUniformVector3(void)
361 {
362   TestApplication application;
363
364   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
365   DALI_TEST_CHECK( effect );
366
367   BitmapImage image = CreateBitmapImage();
368
369   effect.SetUniform( "uVec3", Vector3( 4.0f, 5.0f, 6.0f ) );
370
371   ImageActor actor = ImageActor::New( image );
372   actor.SetSize( 100.0f, 100.0f );
373   actor.SetName("TestImageFilenameActor");
374   actor.SetShaderEffect(effect);
375   Stage::GetCurrent().Add(actor);
376
377   application.SendNotification();
378   application.Render();
379
380   DALI_TEST_CHECK(
381       application.GetGlAbstraction().CheckUniformValue(
382           "uVec3", Vector3( 4.0f, 5.0f, 6.0f ) ) );
383   END_TEST;
384 }
385
386 int UtcDaliShaderEffectMethodSetUniformVector4(void)
387 {
388   TestApplication application;
389
390   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
391   DALI_TEST_CHECK( effect );
392
393   BitmapImage image = CreateBitmapImage();
394
395   effect.SetUniform( "uVec4", Vector4( 7.0f, 8.0f, 9.0f, 10.0f ) );
396
397   ImageActor actor = ImageActor::New( image );
398   actor.SetSize( 100.0f, 100.0f );
399   actor.SetName("TestImageFilenameActor");
400   actor.SetShaderEffect(effect);
401   Stage::GetCurrent().Add(actor);
402
403   application.SendNotification();
404   application.Render();
405
406   DALI_TEST_CHECK(
407       application.GetGlAbstraction().CheckUniformValue(
408           "uVec4", Vector4( 7.0f, 8.0f, 9.0f, 10.0f ) ) );
409   END_TEST;
410 }
411
412 int UtcDaliShaderEffectMethodSetUniformMatrix(void)
413 {
414   TestApplication application;
415
416   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
417   DALI_TEST_CHECK( effect );
418
419   BitmapImage image = CreateBitmapImage();
420
421   effect.SetUniform( "uModelView", Matrix::IDENTITY );
422
423   ImageActor actor = ImageActor::New( image );
424   actor.SetSize( 100.0f, 100.0f );
425   actor.SetName("TestImageFilenameActor");
426   actor.SetShaderEffect(effect);
427   Stage::GetCurrent().Add(actor);
428
429   application.SendNotification();
430   application.Render();
431
432   DALI_TEST_CHECK(
433       application.GetGlAbstraction().CheckUniformValue(
434           "uModelView", Matrix::IDENTITY ) );
435   END_TEST;
436 }
437
438 int UtcDaliShaderEffectMethodSetUniformMatrix3(void)
439 {
440   TestApplication application;
441
442   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
443   DALI_TEST_CHECK( effect );
444
445   BitmapImage image = CreateBitmapImage();
446
447   Matrix3 matIdentity(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
448   effect.SetUniform( "uMatrix3", matIdentity );
449
450   ImageActor actor = ImageActor::New( image );
451   actor.SetSize( 100.0f, 100.0f );
452   actor.SetName("TestImageFilenameActor");
453   actor.SetShaderEffect(effect);
454   Stage::GetCurrent().Add(actor);
455
456   application.SendNotification();
457   application.Render();
458
459   DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue("uMatrix3", matIdentity) );
460   END_TEST;
461 }
462
463 int UtcDaliShaderEffectMethodSetUniformViewport(void)
464 {
465   TestApplication application;
466
467   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
468   DALI_TEST_CHECK( effect );
469
470   BitmapImage image = CreateBitmapImage();
471
472   ImageActor actor = ImageActor::New( image );
473   actor.SetSize( 100.0f, 100.0f );
474   actor.SetName("TestImageFilenameActor");
475   actor.SetShaderEffect(effect);
476   Stage::GetCurrent().Add(actor);
477
478   effect.SetUniform( "uVec2", Vector2( 0.0f, 0.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
479   effect.SetUniform( "uVec2Dir", Vector2( 1.0f, 2.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_DIRECTION );
480
481   application.SendNotification();
482   application.Render();
483
484   const Vector2& stageSize(Stage::GetCurrent().GetSize());
485
486   DALI_TEST_CHECK(
487       application.GetGlAbstraction().CheckUniformValue(
488           "uVec2", Vector2( stageSize.x/2, -stageSize.y/2 ) ) );
489
490   DALI_TEST_CHECK(
491       application.GetGlAbstraction().CheckUniformValue(
492           "uVec2Dir", Vector2( -1.0f, 2.0f ) ) );
493   END_TEST;
494 }
495
496 int UtcDaliShaderEffectMethodSetEffectImage(void)
497 {
498   TestApplication application;
499
500   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
501   DALI_TEST_CHECK( effect );
502
503   BitmapImage image = CreateBitmapImage();
504
505   effect.SetEffectImage(image);
506
507   ImageActor actor = ImageActor::New( image );
508   actor.SetSize( 100.0f, 100.0f );
509   actor.SetName("TestImageFilenameActor");
510   actor.SetShaderEffect(effect);
511   Stage::GetCurrent().Add(actor);
512
513   application.SendNotification();
514   application.Render(16);
515   application.SendNotification();
516   application.Render(16);
517   application.SendNotification();
518
519   GLuint programId, uniformId;
520   bool uniformWasSet = application.GetGlAbstraction().GetUniformIds( "sEffect", programId, uniformId );
521   // we dont care about the value of the sampler uniform as thats internal to DALi core and subject to change
522   DALI_TEST_CHECK( uniformWasSet );
523   END_TEST;
524 }
525
526 int UtcDaliShaderEffectMethodSetEffectImageAndDelete(void)
527 {
528   TestApplication application;
529
530   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
531
532   BitmapImage effectImage = CreateBitmapImage();
533   effect.SetEffectImage(effectImage);
534
535   ImageActor actor = ImageActor::New();
536
537   actor.SetShaderEffect(effect);
538   effect.Reset();
539
540   Stage::GetCurrent().Add(actor);
541
542   // do an update / render cycle
543   application.SendNotification();
544   application.Render(16);
545   application.SendNotification();
546   application.Render(16);
547   application.SendNotification();
548   application.Render(16);
549
550   printf("removing image actor from stage and Reseting handle\n");
551   Stage::GetCurrent().Remove(actor);
552   actor.Reset();
553
554   tet_printf("### Update & Render  \n");
555
556   application.SendNotification();
557   application.Render(16);
558
559   tet_printf("#### Update Only  \n");
560
561   tet_printf("effectImage.Reset \n");
562
563   // this releases the effect texture resource,
564   // Update will send a DispatchDiscardTexture message to render
565   effectImage.Reset();
566   application.SendNotification();
567   application.UpdateOnly(16);
568
569   tet_printf("#### Update Only \n");
570
571   // at this point shader is deleted, during clear discard queue
572   // and it sends a Shader:: DispatchRemoveObserver message to render thread
573   application.UpdateOnly(16);
574
575   tet_printf("#### Render Only  \n");
576   // This is where it used to crash, there is a message in the queue to perform DispatchDiscardTexture
577   // which tries to call observer->TextureDiscarded, where observer == shader that was deleted
578   // in previous update.
579   application.RenderOnly();
580
581
582   // process the discard texture message
583   application.RenderOnly();
584   application.SendNotification();
585   application.Render(16);
586
587   tet_result(TET_PASS);
588
589   END_TEST;
590 }
591
592 int UtcDaliShaderEffectMethodApplyConstraint(void)
593 {
594   // Test whether Shader's uniform can be constrained to a stationary constraint.
595   TestApplication application;
596
597   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
598   DALI_TEST_CHECK( effect );
599
600   BitmapImage image = CreateBitmapImage();
601
602   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
603
604   ImageActor actor = ImageActor::New( image );
605   actor.SetSize( 100.0f, 100.0f );
606   actor.SetName("TestImageFilenameActor");
607   actor.SetShaderEffect(effect);
608   Stage::GetCurrent().Add(actor);
609
610   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
611
612   application.SendNotification();
613   application.Render();
614
615   // Test effects of SetUniform...
616   DALI_TEST_CHECK(
617       application.GetGlAbstraction().CheckUniformValue(
618           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
619
620   Constraint constraint = Constraint::New<Vector3>( uVecProperty,
621                                                     TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
622
623   effect.ApplyConstraint(constraint);
624
625   application.SendNotification();
626   application.Render();
627
628   // Test effects of Constraint.
629   DALI_TEST_CHECK(
630       application.GetGlAbstraction().CheckUniformValue(
631           "uVec3", Vector3( 4.0f, 9.0f, 16.0f ) ) );
632   END_TEST;
633 }
634
635
636 int UtcDaliShaderEffectMethodApplyConstraintFromActor(void)
637 {
638   // Test whether Shader's uniform can be constrained to Actor's position.
639   TestApplication application;
640
641   const Vector3 targetPosition = Vector3( 100.0f, 70.0f, 20.0f);
642
643   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
644   DALI_TEST_CHECK( effect );
645
646   BitmapImage image = CreateBitmapImage();
647
648   effect.SetUniform( "uVec3", Vector3( 50.0f, 25.0f, 0.0f ) );
649
650   ImageActor actor = ImageActor::New( image );
651   actor.SetPosition(targetPosition);
652   actor.SetSize( 100.0f, 100.0f );
653   actor.SetName("TestImageFilenameActor");
654   actor.SetShaderEffect(effect);
655   Stage::GetCurrent().Add(actor);
656
657   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
658
659   Constraint constraint = Constraint::New<Vector3>( uVecProperty,
660                                                     Source(actor, Actor::POSITION),
661                                                     TestConstraintFromPositionToVector3() );
662
663   effect.ApplyConstraint(constraint);
664
665   application.SendNotification();
666   application.Render();
667
668   // Test effects of Constraint.
669   DALI_TEST_CHECK(
670       application.GetGlAbstraction().CheckUniformValue(
671           "uVec3", targetPosition ) );
672   END_TEST;
673 }
674
675 int UtcDaliShaderEffectMethodApplyConstraintFromActor2(void)
676 {
677   // Test whether Shader's uniform can be constrained to Actor's position.
678   // While Actor's position is constrained to another point * 2.0f
679   TestApplication application;
680
681   const Vector3 targetPosition = Vector3( 25.0f, 36.0f, 49.0f );
682
683   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
684   DALI_TEST_CHECK( effect );
685
686   BitmapImage image = CreateBitmapImage();
687
688   effect.SetUniform( "uVec3", Vector3( 50.0f, 25.0f, 0.0f ) );
689
690   ImageActor actor = ImageActor::New( image );
691   actor.SetPosition(Vector3( 100.0f, 70.0f, 20.0f));
692   actor.SetSize( 100.0f, 100.0f );
693   actor.SetName("TestImageFilenameActor");
694   actor.SetShaderEffect(effect);
695   Stage::GetCurrent().Add(actor);
696
697   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
698
699   Constraint shaderConstraint = Constraint::New<Vector3>( uVecProperty,
700                                                     Source(actor, Actor::POSITION),
701                                                     TestConstraintFromPositionToVector3() );
702
703   effect.ApplyConstraint(shaderConstraint);
704
705   Constraint actorConstraint = Constraint::New<Vector3>( Actor::POSITION,
706                                                          TestConstraintToVector3Double(targetPosition) );
707
708   actor.ApplyConstraint(actorConstraint);
709
710   application.SendNotification();
711   application.Render();
712
713   // Test effects of Constraint.
714   DALI_TEST_CHECK(
715       application.GetGlAbstraction().CheckUniformValue(
716           "uVec3", targetPosition * 2.0f ) );
717   END_TEST;
718 }
719
720 int UtcDaliShaderEffectMethodApplyConstraintCallback(void)
721 {
722   // Test whether Shader's uniform can be constrained to a stationary constraint.
723   TestApplication application;
724
725   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
726   DALI_TEST_CHECK( effect );
727
728   BitmapImage image = CreateBitmapImage();
729
730   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
731
732   ImageActor actor = ImageActor::New( image );
733   actor.SetSize( 100.0f, 100.0f );
734   actor.SetName("TestImageFilenameActor");
735   actor.SetShaderEffect(effect);
736   Stage::GetCurrent().Add(actor);
737
738   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
739
740   application.SendNotification();
741   application.Render();
742
743   // Test effects of SetUniform...
744   DALI_TEST_CHECK(
745       application.GetGlAbstraction().CheckUniformValue(
746           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
747
748   Constraint constraint = Constraint::New<Vector3>( uVecProperty,
749                                                     TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
750
751   constraint.SetApplyTime( 10.0f );
752
753   bool constraintCheck( false );
754   ConstraintAppliedCheck appliedCheck( constraintCheck );
755
756   // We should receive the "Applied" signal after 10 seconds
757   ActiveConstraint active = effect.ApplyConstraint(constraint);
758   active.AppliedSignal().Connect( &application, appliedCheck );
759
760   application.SendNotification();
761   application.Render(static_cast<unsigned int>(1000.0f)); // 1 elapsed second
762
763   // Check signal has not fired
764   application.SendNotification();
765   appliedCheck.CheckSignalNotReceived();
766
767   application.Render(static_cast<unsigned int>(4000.0f)); // 5 elapsed seconds
768
769   // Check signal has not fired
770   application.SendNotification();
771   appliedCheck.CheckSignalNotReceived();
772
773   application.Render(static_cast<unsigned int>(5000.0f - 1.0f)); // <10 elapsed seconds
774
775   // Check signal has not fired
776   application.SendNotification();
777   appliedCheck.CheckSignalNotReceived();
778
779   application.Render(static_cast<unsigned int>(2.0f)); // >10 elapsed seconds
780
781   // Signal should have fired
782   application.SendNotification();
783   appliedCheck.CheckSignalReceived();
784
785   // Test effects of Constraint.
786   DALI_TEST_CHECK(
787       application.GetGlAbstraction().CheckUniformValue(
788           "uVec3", Vector3( 4.0f, 9.0f, 16.0f ) ) );
789   END_TEST;
790 }
791
792 int UtcDaliShaderEffectMethodRemoveConstraints(void)
793 {
794   // Test if constrains can be removed before they are ever applyed.
795   TestApplication application;
796
797   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
798   DALI_TEST_CHECK( effect );
799
800   BitmapImage image = CreateBitmapImage();
801
802   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
803
804   ImageActor actor = ImageActor::New( image );
805   actor.SetSize( 100.0f, 100.0f );
806   actor.SetName("TestImageFilenameActor");
807   actor.SetShaderEffect(effect);
808   Stage::GetCurrent().Add(actor);
809
810   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
811
812   application.SendNotification();
813   application.Render();
814
815   // Test effects of SetUniform...
816   DALI_TEST_CHECK(
817       application.GetGlAbstraction().CheckUniformValue(
818           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
819
820   Constraint constraint = Constraint::New<Vector3>( uVecProperty,
821                                                     TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
822
823   effect.ApplyConstraint(constraint);
824
825   // Remove the constraints
826   effect.RemoveConstraints();
827
828   application.SendNotification();
829   application.Render();
830
831   // Test effects of Constraint.
832   DALI_TEST_CHECK(
833       application.GetGlAbstraction().CheckUniformValue(
834           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
835   END_TEST;
836 }
837
838 int UtcDaliShaderEffectMethodRemoveConstraints2(void)
839 {
840   // Test whether Shader's uniform constrains can be removed after they are applyed.
841   TestApplication application;
842
843   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
844   DALI_TEST_CHECK( effect );
845
846   BitmapImage image = CreateBitmapImage();
847
848   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
849
850   ImageActor actor = ImageActor::New( image );
851   actor.SetSize( 100.0f, 100.0f );
852   actor.SetName("TestImageFilenameActor");
853   actor.SetShaderEffect(effect);
854   Stage::GetCurrent().Add(actor);
855
856   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
857
858   application.SendNotification();
859   application.Render();
860
861   // Test effects of SetUniform...
862   DALI_TEST_CHECK(
863       application.GetGlAbstraction().CheckUniformValue(
864           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
865
866   Constraint constraint = Constraint::New<Vector3>( uVecProperty,
867                                                     TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
868
869   effect.ApplyConstraint(constraint);
870
871   application.SendNotification();
872   application.Render();
873
874   // Reset the value and remove the constraints
875   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
876   effect.RemoveConstraints();
877
878   application.SendNotification();
879   application.Render();
880
881   // Test effects of Constraint.
882   DALI_TEST_CHECK(
883       application.GetGlAbstraction().CheckUniformValue(
884           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
885   END_TEST;
886 }
887
888 int UtcDaliShaderEffectMethodCreateExtension(void)
889 {
890   // Test creation of a shader extension
891   TestApplication aplication;
892
893   bool deleted;
894   {
895     ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
896     DALI_TEST_CHECK( effect );
897
898     TestExtension* extension = new TestExtension ( deleted );
899
900     effect.AttachExtension( extension );
901
902     DALI_TEST_CHECK( static_cast<TestExtension&>(effect.GetExtension()).IsAlive() );
903   }
904
905   DALI_TEST_CHECK( deleted );
906   END_TEST;
907 }
908
909 int UtcDaliShaderEffectMethodCreateExtension2(void)
910 {
911   // Test creation of a shader extension
912   bool deleted;
913   {
914     TestApplication application;
915
916     ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
917     DALI_TEST_CHECK( effect );
918
919     BitmapImage image = CreateBitmapImage();
920
921     effect.SetUniform( "uFloat", 1.0f );
922
923     ImageActor actor = ImageActor::New( image );
924     actor.SetSize( 100.0f, 100.0f );
925     actor.SetName("TestImageFilenameActor");
926     actor.SetShaderEffect(effect);
927     Stage::GetCurrent().Add(actor);
928
929     application.SendNotification();
930     application.Render();
931
932     TestExtension* extension = new TestExtension ( deleted );
933
934     effect.AttachExtension( extension );
935
936     const ShaderEffect& constEffect(effect);
937     const TestExtension& ext( static_cast<const TestExtension&>(constEffect.GetExtension()) );
938
939     DALI_TEST_CHECK( ext.IsAlive() );
940   }
941
942   DALI_TEST_CHECK( deleted );
943   END_TEST;
944 }
945
946 int UtcDaliShaderEffectMethodNoExtension(void)
947 {
948   TestApplication application;
949
950   ShaderEffect effect;
951
952   try
953   {
954     ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
955     DALI_TEST_CHECK( effect );
956
957     // Don't attach extension
958     ShaderEffect::Extension& extension = effect.GetExtension();
959     (void) extension;
960
961     DALI_TEST_CHECK( false );
962   }
963   catch (Dali::DaliException& e)
964   {
965     // Tests that a negative test of an assertion succeeds
966     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
967     DALI_TEST_CHECK( !effect );
968   }
969   END_TEST;
970 }
971
972
973 int UtcDaliShaderEffectPropertyIndices(void)
974 {
975   TestApplication application;
976   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
977
978   Property::IndexContainer indices;
979   effect.GetPropertyIndices( indices );
980   DALI_TEST_CHECK( ! indices.empty() );
981   DALI_TEST_EQUALS( indices.size(), effect.GetPropertyCount(), TEST_LOCATION );
982   END_TEST;
983 }