184b6be58252d61544dda6083325f11150befabb
[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 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     DALI_TEST_PRINT_ASSERT( e );
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( application.GetGlAbstraction().CheckUniformValue( "uVec2", Vector2( stageSize.x/2, -stageSize.y/2 ) ) );
487
488   DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "uVec2Dir", Vector2( -1.0f, 2.0f ) ) );
489
490   // change coordinate types
491   effect.SetUniform( "uVec2", Vector2( 0.1f, 0.2f ), ShaderEffect::COORDINATE_TYPE_DEFAULT );
492   effect.SetUniform( "uVec2Dir", Vector2( 1.0f, 2.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
493   actor.SetPixelArea( ImageActor::PixelArea( 0, 0, 10, 10 ) );
494
495   application.SendNotification();
496   application.Render();
497
498   Vector2 outValue;
499   application.GetGlAbstraction().GetUniformValue( "uVec2", outValue );
500   DALI_TEST_EQUALS( outValue, Vector2( 0.1f, 0.2f ), TEST_LOCATION );
501
502   application.GetGlAbstraction().GetUniformValue( "uVec2Dir", outValue );
503   DALI_TEST_EQUALS( outValue, Vector2( stageSize.x *.5f - 1.f, -stageSize.y * .5f + 2.f), TEST_LOCATION );
504
505   END_TEST;
506 }
507
508 int UtcDaliShaderEffectMethodSetEffectImage(void)
509 {
510   TestApplication application;
511
512   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
513   DALI_TEST_CHECK( effect );
514
515   BitmapImage image = CreateBitmapImage();
516
517   effect.SetEffectImage(image);
518
519   ImageActor actor = ImageActor::New( image );
520   actor.SetSize( 100.0f, 100.0f );
521   actor.SetName("TestImageFilenameActor");
522   actor.SetShaderEffect(effect);
523   Stage::GetCurrent().Add(actor);
524
525   application.SendNotification();
526   application.Render(16);
527   application.SendNotification();
528   application.Render(16);
529   application.SendNotification();
530
531   GLuint programId, uniformId;
532   bool uniformWasSet = application.GetGlAbstraction().GetUniformIds( "sEffect", programId, uniformId );
533   // we dont care about the value of the sampler uniform as thats internal to DALi core and subject to change
534   DALI_TEST_CHECK( uniformWasSet );
535   END_TEST;
536 }
537
538 int UtcDaliShaderEffectMethodSetEffectImageAndDelete(void)
539 {
540   TestApplication application;
541
542   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
543
544   BitmapImage effectImage = CreateBitmapImage();
545   effect.SetEffectImage(effectImage);
546
547   ImageActor actor = ImageActor::New();
548
549   actor.SetShaderEffect(effect);
550   effect.Reset();
551
552   Stage::GetCurrent().Add(actor);
553
554   // do an update / render cycle
555   application.SendNotification();
556   application.Render(16);
557   application.SendNotification();
558   application.Render(16);
559   application.SendNotification();
560   application.Render(16);
561
562   printf("removing image actor from stage and Reseting handle\n");
563   Stage::GetCurrent().Remove(actor);
564   actor.Reset();
565
566   tet_printf("### Update & Render  \n");
567
568   application.SendNotification();
569   application.Render(16);
570
571   tet_printf("#### Update Only  \n");
572
573   tet_printf("effectImage.Reset \n");
574
575   // this releases the effect texture resource,
576   // Update will send a DispatchDiscardTexture message to render
577   effectImage.Reset();
578   application.SendNotification();
579   application.UpdateOnly(16);
580
581   tet_printf("#### Update Only \n");
582
583   // at this point shader is deleted, during clear discard queue
584   // and it sends a Shader:: DispatchRemoveObserver message to render thread
585   application.UpdateOnly(16);
586
587   tet_printf("#### Render Only  \n");
588   // This is where it used to crash, there is a message in the queue to perform DispatchDiscardTexture
589   // which tries to call observer->TextureDiscarded, where observer == shader that was deleted
590   // in previous update.
591   application.RenderOnly();
592
593
594   // process the discard texture message
595   application.RenderOnly();
596   application.SendNotification();
597   application.Render(16);
598
599   tet_result(TET_PASS);
600
601   END_TEST;
602 }
603
604 int UtcDaliShaderEffectMethodApplyConstraint(void)
605 {
606   // Test whether Shader's uniform can be constrained to a stationary constraint.
607   TestApplication application;
608
609   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
610   DALI_TEST_CHECK( effect );
611
612   BitmapImage image = CreateBitmapImage();
613
614   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
615
616   ImageActor actor = ImageActor::New( image );
617   actor.SetSize( 100.0f, 100.0f );
618   actor.SetName("TestImageFilenameActor");
619   actor.SetShaderEffect(effect);
620   Stage::GetCurrent().Add(actor);
621
622   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
623
624   application.SendNotification();
625   application.Render();
626
627   // Test effects of SetUniform...
628   DALI_TEST_CHECK(
629       application.GetGlAbstraction().CheckUniformValue(
630           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
631
632   Constraint constraint = Constraint::New<Vector3>( uVecProperty,
633                                                     TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
634
635   effect.ApplyConstraint(constraint);
636
637   application.SendNotification();
638   application.Render();
639
640   // Test effects of Constraint.
641   DALI_TEST_CHECK(
642       application.GetGlAbstraction().CheckUniformValue(
643           "uVec3", Vector3( 4.0f, 9.0f, 16.0f ) ) );
644   END_TEST;
645 }
646
647
648 int UtcDaliShaderEffectMethodApplyConstraintFromActor(void)
649 {
650   // Test whether Shader's uniform can be constrained to Actor's position.
651   TestApplication application;
652
653   const Vector3 targetPosition = Vector3( 100.0f, 70.0f, 20.0f);
654
655   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
656   DALI_TEST_CHECK( effect );
657
658   BitmapImage image = CreateBitmapImage();
659
660   effect.SetUniform( "uVec3", Vector3( 50.0f, 25.0f, 0.0f ) );
661
662   ImageActor actor = ImageActor::New( image );
663   actor.SetPosition(targetPosition);
664   actor.SetSize( 100.0f, 100.0f );
665   actor.SetName("TestImageFilenameActor");
666   actor.SetShaderEffect(effect);
667   Stage::GetCurrent().Add(actor);
668
669   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
670
671   Constraint constraint = Constraint::New<Vector3>( uVecProperty,
672                                                     Source(actor, Actor::POSITION),
673                                                     TestConstraintFromPositionToVector3() );
674
675   effect.ApplyConstraint(constraint);
676
677   application.SendNotification();
678   application.Render();
679
680   // Test effects of Constraint.
681   DALI_TEST_CHECK(
682       application.GetGlAbstraction().CheckUniformValue(
683           "uVec3", targetPosition ) );
684   END_TEST;
685 }
686
687 int UtcDaliShaderEffectMethodApplyConstraintFromActor2(void)
688 {
689   // Test whether Shader's uniform can be constrained to Actor's position.
690   // While Actor's position is constrained to another point * 2.0f
691   TestApplication application;
692
693   const Vector3 targetPosition = Vector3( 25.0f, 36.0f, 49.0f );
694
695   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
696   DALI_TEST_CHECK( effect );
697
698   BitmapImage image = CreateBitmapImage();
699
700   effect.SetUniform( "uVec3", Vector3( 50.0f, 25.0f, 0.0f ) );
701
702   ImageActor actor = ImageActor::New( image );
703   actor.SetPosition(Vector3( 100.0f, 70.0f, 20.0f));
704   actor.SetSize( 100.0f, 100.0f );
705   actor.SetName("TestImageFilenameActor");
706   actor.SetShaderEffect(effect);
707   Stage::GetCurrent().Add(actor);
708
709   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
710
711   Constraint shaderConstraint = Constraint::New<Vector3>( uVecProperty,
712                                                     Source(actor, Actor::POSITION),
713                                                     TestConstraintFromPositionToVector3() );
714
715   effect.ApplyConstraint(shaderConstraint);
716
717   Constraint actorConstraint = Constraint::New<Vector3>( Actor::POSITION,
718                                                          TestConstraintToVector3Double(targetPosition) );
719
720   actor.ApplyConstraint(actorConstraint);
721
722   application.SendNotification();
723   application.Render();
724
725   // Test effects of Constraint.
726   DALI_TEST_CHECK(
727       application.GetGlAbstraction().CheckUniformValue(
728           "uVec3", targetPosition * 2.0f ) );
729   END_TEST;
730 }
731
732 int UtcDaliShaderEffectMethodApplyConstraintCallback(void)
733 {
734   // Test whether Shader's uniform can be constrained to a stationary constraint.
735   TestApplication application;
736
737   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
738   DALI_TEST_CHECK( effect );
739
740   BitmapImage image = CreateBitmapImage();
741
742   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
743
744   ImageActor actor = ImageActor::New( image );
745   actor.SetSize( 100.0f, 100.0f );
746   actor.SetName("TestImageFilenameActor");
747   actor.SetShaderEffect(effect);
748   Stage::GetCurrent().Add(actor);
749
750   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
751
752   application.SendNotification();
753   application.Render();
754
755   // Test effects of SetUniform...
756   DALI_TEST_CHECK(
757       application.GetGlAbstraction().CheckUniformValue(
758           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
759
760   Constraint constraint = Constraint::New<Vector3>( uVecProperty,
761                                                     TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
762
763   constraint.SetApplyTime( 10.0f );
764
765   bool constraintCheck( false );
766   ConstraintAppliedCheck appliedCheck( constraintCheck );
767
768   // We should receive the "Applied" signal after 10 seconds
769   ActiveConstraint active = effect.ApplyConstraint(constraint);
770   active.AppliedSignal().Connect( &application, appliedCheck );
771
772   application.SendNotification();
773   application.Render(static_cast<unsigned int>(1000.0f)); // 1 elapsed second
774
775   // Check signal has not fired
776   application.SendNotification();
777   appliedCheck.CheckSignalNotReceived();
778
779   application.Render(static_cast<unsigned int>(4000.0f)); // 5 elapsed seconds
780
781   // Check signal has not fired
782   application.SendNotification();
783   appliedCheck.CheckSignalNotReceived();
784
785   application.Render(static_cast<unsigned int>(5000.0f - 1.0f)); // <10 elapsed seconds
786
787   // Check signal has not fired
788   application.SendNotification();
789   appliedCheck.CheckSignalNotReceived();
790
791   application.Render(static_cast<unsigned int>(2.0f)); // >10 elapsed seconds
792
793   // Signal should have fired
794   application.SendNotification();
795   appliedCheck.CheckSignalReceived();
796
797   // Test effects of Constraint.
798   DALI_TEST_CHECK(
799       application.GetGlAbstraction().CheckUniformValue(
800           "uVec3", Vector3( 4.0f, 9.0f, 16.0f ) ) );
801   END_TEST;
802 }
803
804 int UtcDaliShaderEffectMethodRemoveConstraints(void)
805 {
806   // Test if constrains can be removed before they are ever applyed.
807   TestApplication application;
808
809   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
810   DALI_TEST_CHECK( effect );
811
812   BitmapImage image = CreateBitmapImage();
813
814   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
815
816   ImageActor actor = ImageActor::New( image );
817   actor.SetSize( 100.0f, 100.0f );
818   actor.SetName("TestImageFilenameActor");
819   actor.SetShaderEffect(effect);
820   Stage::GetCurrent().Add(actor);
821
822   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
823
824   application.SendNotification();
825   application.Render();
826
827   // Test effects of SetUniform...
828   DALI_TEST_CHECK(
829       application.GetGlAbstraction().CheckUniformValue(
830           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
831
832   Constraint constraint = Constraint::New<Vector3>( uVecProperty,
833                                                     TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
834
835   effect.ApplyConstraint(constraint);
836
837   // Remove the constraints
838   effect.RemoveConstraints();
839
840   application.SendNotification();
841   application.Render();
842
843   // Test effects of Constraint.
844   DALI_TEST_CHECK(
845       application.GetGlAbstraction().CheckUniformValue(
846           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
847   END_TEST;
848 }
849
850 int UtcDaliShaderEffectMethodRemoveConstraints2(void)
851 {
852   // Test whether Shader's uniform constrains can be removed after they are applyed.
853   TestApplication application;
854
855   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
856   DALI_TEST_CHECK( effect );
857
858   BitmapImage image = CreateBitmapImage();
859
860   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
861
862   ImageActor actor = ImageActor::New( image );
863   actor.SetSize( 100.0f, 100.0f );
864   actor.SetName("TestImageFilenameActor");
865   actor.SetShaderEffect(effect);
866   Stage::GetCurrent().Add(actor);
867
868   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
869
870   application.SendNotification();
871   application.Render();
872
873   // Test effects of SetUniform...
874   DALI_TEST_CHECK(
875       application.GetGlAbstraction().CheckUniformValue(
876           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
877
878   Constraint constraint = Constraint::New<Vector3>( uVecProperty,
879                                                     TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
880
881   effect.ApplyConstraint(constraint);
882
883   application.SendNotification();
884   application.Render();
885
886   // Reset the value and remove the constraints
887   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
888   effect.RemoveConstraints();
889
890   application.SendNotification();
891   application.Render();
892
893   // Test effects of Constraint.
894   DALI_TEST_CHECK(
895       application.GetGlAbstraction().CheckUniformValue(
896           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
897   END_TEST;
898 }
899
900 int UtcDaliShaderEffectMethodCreateExtension(void)
901 {
902   // Test creation of a shader extension
903   TestApplication aplication;
904
905   bool deleted;
906   {
907     ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
908     DALI_TEST_CHECK( effect );
909
910     TestExtension* extension = new TestExtension ( deleted );
911
912     effect.AttachExtension( extension );
913
914     DALI_TEST_CHECK( static_cast<TestExtension&>(effect.GetExtension()).IsAlive() );
915   }
916
917   DALI_TEST_CHECK( deleted );
918   END_TEST;
919 }
920
921 int UtcDaliShaderEffectMethodCreateExtension2(void)
922 {
923   // Test creation of a shader extension
924   bool deleted;
925   {
926     TestApplication application;
927
928     ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
929     DALI_TEST_CHECK( effect );
930
931     BitmapImage image = CreateBitmapImage();
932
933     effect.SetUniform( "uFloat", 1.0f );
934
935     ImageActor actor = ImageActor::New( image );
936     actor.SetSize( 100.0f, 100.0f );
937     actor.SetName("TestImageFilenameActor");
938     actor.SetShaderEffect(effect);
939     Stage::GetCurrent().Add(actor);
940
941     application.SendNotification();
942     application.Render();
943
944     TestExtension* extension = new TestExtension ( deleted );
945
946     effect.AttachExtension( extension );
947
948     const ShaderEffect& constEffect(effect);
949     const TestExtension& ext( static_cast<const TestExtension&>(constEffect.GetExtension()) );
950
951     DALI_TEST_CHECK( ext.IsAlive() );
952   }
953
954   DALI_TEST_CHECK( deleted );
955   END_TEST;
956 }
957
958 int UtcDaliShaderEffectMethodNoExtension(void)
959 {
960   TestApplication application;
961
962   ShaderEffect effect;
963
964   try
965   {
966     ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
967     DALI_TEST_CHECK( effect );
968
969     // Don't attach extension
970     ShaderEffect::Extension& extension = effect.GetExtension();
971     (void) extension;
972
973     DALI_TEST_CHECK( false );
974   }
975   catch (Dali::DaliException& e)
976   {
977     // Tests that a negative test of an assertion succeeds
978     DALI_TEST_PRINT_ASSERT( e );
979     DALI_TEST_CHECK( !effect );
980   }
981   END_TEST;
982 }
983
984
985 int UtcDaliShaderEffectPropertyIndices(void)
986 {
987   TestApplication application;
988   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
989
990   Property::IndexContainer indices;
991   effect.GetPropertyIndices( indices );
992   DALI_TEST_CHECK( ! indices.empty() );
993   DALI_TEST_EQUALS( indices.size(), effect.GetPropertyCount(), TEST_LOCATION );
994   END_TEST;
995 }
996
997 int UtcDaliShaderBinaries(void)
998 {
999   TestApplication application;
1000   // these will not affect the "first round" of dali render as core is already initialized and it has queried the defaults
1001   application.GetGlAbstraction().SetBinaryFormats( 1 );
1002   application.GetGlAbstraction().SetNumBinaryFormats( 1 );
1003   application.GetGlAbstraction().SetProgramBinaryLength( 1 );
1004
1005   GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
1006
1007   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
1008   DALI_TEST_CHECK( effect );
1009
1010   BitmapImage image = CreateBitmapImage();
1011   ImageActor actor = ImageActor::New( image );
1012   actor.SetSize( 100.0f, 100.0f );
1013   actor.SetName("TestImageFilenameActor");
1014   actor.SetShaderEffect(effect);
1015   Stage::GetCurrent().Add(actor);
1016
1017   application.SendNotification();
1018   application.Render(16);
1019   // binary was not requested by DALi
1020   DALI_TEST_CHECK( !(application.GetGlAbstraction().GetProgramBinaryCalled()) );
1021
1022   GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
1023
1024   // check that the shader was compiled
1025   DALI_TEST_EQUALS( lastShaderCompiledAfter, lastShaderCompiledBefore + 2, TEST_LOCATION );
1026
1027   // simulate context loss to get core to re-initialize its GL
1028   application.GetCore().ContextDestroyed();
1029   application.GetCore().ContextCreated();
1030
1031   application.SendNotification();
1032   application.Render(16);
1033
1034   // shader is recompiled
1035   GLuint finalShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
1036   // check that the shader was compiled
1037   DALI_TEST_EQUALS( lastShaderCompiledAfter + 2, finalShaderCompiled, TEST_LOCATION );
1038
1039   // binary was requested by DALi
1040   DALI_TEST_CHECK( application.GetGlAbstraction().GetProgramBinaryCalled() );
1041
1042   END_TEST;
1043 }
1044