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