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