Changed ImageActor and to use new renderers and removed ImageAttachement.
[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 const int GETSOURCE_BUFFER_SIZE = 0x10000;
48
49
50 struct TestConstraintToVector3
51 {
52   TestConstraintToVector3(Vector3 target)
53   : mTarget(target)
54   {
55   }
56
57   void operator()( Vector3& current, const PropertyInputContainer& /* inputs */ )
58   {
59     current = mTarget;
60   }
61
62   Vector3 mTarget;
63 };
64
65 struct TestConstraintFromPositionToVector3
66 {
67   TestConstraintFromPositionToVector3()
68   {
69   }
70
71   void operator()( Vector3& current, const PropertyInputContainer& inputs )
72   {
73     current = inputs[0]->GetVector3();
74   }
75 };
76
77 struct TestConstraintToVector3Double
78 {
79   TestConstraintToVector3Double(Vector3 target)
80   : mTarget(target)
81   {
82   }
83
84   void operator()( Vector3& current, const PropertyInputContainer& /* inputs */ )
85   {
86     current = mTarget * 2.0f;
87   }
88
89   Vector3 mTarget;
90 };
91
92 static const char* TestImageFilename = "icon_wrt.png";
93
94 Integration::Bitmap* CreateBitmap( unsigned int imageHeight, unsigned int imageWidth, unsigned int initialColor )
95 {
96   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
97   Integration::PixelBuffer* pixbuffer = bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888,  imageWidth,imageHeight,imageWidth,imageHeight );
98   unsigned int bytesPerPixel = GetBytesPerPixel(  Pixel::RGBA8888 );
99
100   memset( pixbuffer,  initialColor , imageHeight*imageWidth*bytesPerPixel);
101
102   return bitmap;
103 }
104
105 } // anon namespace
106
107
108 int UtcDaliShaderEffectMethodNew01(void)
109 {
110   TestApplication application;
111
112   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
113   DALI_TEST_CHECK(effect);
114   END_TEST;
115 }
116
117 int UtcDaliShaderEffectMethodNew02(void)
118 {
119   TestApplication application;
120
121   ShaderEffect effect;
122
123   try
124   {
125     // New must be called to create a ShaderEffect or it wont be valid.
126     effect.SetUniform( "uUniform", 0 );
127     DALI_TEST_CHECK( false );
128   }
129   catch (Dali::DaliException& e)
130   {
131     // Tests that a negative test of an assertion succeeds
132     DALI_TEST_PRINT_ASSERT( e );
133     DALI_TEST_CHECK( !effect );
134   }
135   END_TEST;
136 }
137
138 int UtcDaliShaderEffectMethodNew05(void)
139 {
140   TestApplication application;
141
142   // heap constructor / destructor
143   DefaultFunctionCoverage<ShaderEffect> shaderEffect;
144
145   END_TEST;
146 }
147
148 int UtcDaliShaderEffectMethodDownCast(void)
149 {
150   TestApplication application;
151   tet_infoline("Testing Dali::ShaderEffect::DownCast()");
152
153   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
154
155   BaseHandle object(effect);
156
157   ShaderEffect effect2 = ShaderEffect::DownCast(object);
158   DALI_TEST_CHECK(effect2);
159
160   ShaderEffect effect3 = DownCast< ShaderEffect >(object);
161   DALI_TEST_CHECK(effect3);
162
163   BaseHandle unInitializedObject;
164   ShaderEffect effect4 = ShaderEffect::DownCast(unInitializedObject);
165   DALI_TEST_CHECK(!effect4);
166
167   ShaderEffect effect5 = DownCast< ShaderEffect >(unInitializedObject);
168   DALI_TEST_CHECK(!effect5);
169   END_TEST;
170 }
171
172 int UtcDaliShaderEffectMethodDelete01(void)
173 {
174    TestApplication application;
175
176    // get the default shaders built, this is not required but makes it
177    // easier to debug the TET case and isolate the custom shader compilation.
178    application.SendNotification();
179    application.Render();
180
181    GLuint beforeShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
182
183    // create a new shader effect
184    // the vertex and fragment shader will be cached in the ShaderFactory
185    ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
186
187    // destroy the shader effect
188    effect.Reset();
189
190    // Create the same shader effect again, this should now use the cached version
191    // held in the shader factory
192    effect= ShaderEffect::New( VertexSource, FragmentSource );
193
194    // Compile the shader effect
195    application.SendNotification();
196    application.Render();
197
198    GLuint lastShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
199    // no shaders were compiled as they are now compiled on demand and this shader was not used
200    DALI_TEST_EQUALS( beforeShaderCompiled, lastShaderCompiled, TEST_LOCATION );
201
202    END_TEST;
203 }
204
205 int UtcDaliShaderEffectMethodSetUniformFloat(void)
206 {
207   TestApplication application;
208
209   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
210   DALI_TEST_CHECK( effect );
211
212   BufferImage image = CreateBufferImage();
213
214   effect.SetUniform( "uFloat", 1.0f );
215
216   ImageActor actor = ImageActor::New( image );
217   actor.SetSize( 100.0f, 100.0f );
218   actor.SetName("TestImageFilenameActor");
219   actor.SetShaderEffect(effect);
220   Stage::GetCurrent().Add(actor);
221
222   application.SendNotification();
223   application.Render();
224
225   DALI_TEST_CHECK(
226       application.GetGlAbstraction().CheckUniformValue(
227           "uFloat", 1.0f ) );
228   END_TEST;
229 }
230
231 int UtcDaliShaderEffectMethodSetUniformVector2(void)
232 {
233   TestApplication application;
234
235   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
236   DALI_TEST_CHECK( effect );
237
238   BufferImage image = CreateBufferImage();
239
240   effect.SetUniform( "uVec2", Vector2( 2.0f, 3.0f ) );
241
242   ImageActor actor = ImageActor::New( image );
243   actor.SetSize( 100.0f, 100.0f );
244   actor.SetName("TestImageFilenameActor");
245   actor.SetShaderEffect(effect);
246   Stage::GetCurrent().Add(actor);
247
248   application.SendNotification();
249   application.Render();
250
251   DALI_TEST_CHECK(
252       application.GetGlAbstraction().CheckUniformValue(
253           "uVec2", Vector2( 2.0f, 3.0f ) ) );
254   END_TEST;
255 }
256
257 int UtcDaliShaderEffectMethodSetUniformVector3(void)
258 {
259   TestApplication application;
260
261   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
262   DALI_TEST_CHECK( effect );
263
264   BufferImage image = CreateBufferImage();
265
266   effect.SetUniform( "uVec3", Vector3( 4.0f, 5.0f, 6.0f ) );
267
268   ImageActor actor = ImageActor::New( image );
269   actor.SetSize( 100.0f, 100.0f );
270   actor.SetName("TestImageFilenameActor");
271   actor.SetShaderEffect(effect);
272   Stage::GetCurrent().Add(actor);
273
274   application.SendNotification();
275   application.Render();
276
277   DALI_TEST_CHECK(
278       application.GetGlAbstraction().CheckUniformValue(
279           "uVec3", Vector3( 4.0f, 5.0f, 6.0f ) ) );
280   END_TEST;
281 }
282
283 int UtcDaliShaderEffectMethodSetUniformVector4(void)
284 {
285   TestApplication application;
286
287   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
288   DALI_TEST_CHECK( effect );
289
290   BufferImage image = CreateBufferImage();
291
292   effect.SetUniform( "uVec4", Vector4( 7.0f, 8.0f, 9.0f, 10.0f ) );
293
294   ImageActor actor = ImageActor::New( image );
295   actor.SetSize( 100.0f, 100.0f );
296   actor.SetName("TestImageFilenameActor");
297   actor.SetShaderEffect(effect);
298   Stage::GetCurrent().Add(actor);
299
300   application.SendNotification();
301   application.Render();
302
303   DALI_TEST_CHECK(
304       application.GetGlAbstraction().CheckUniformValue(
305           "uVec4", Vector4( 7.0f, 8.0f, 9.0f, 10.0f ) ) );
306   END_TEST;
307 }
308
309 int UtcDaliShaderEffectMethodSetUniformMatrix(void)
310 {
311   TestApplication application;
312
313   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
314   DALI_TEST_CHECK( effect );
315
316   BufferImage image = CreateBufferImage();
317
318   effect.SetUniform( "uModelView", Matrix::IDENTITY );
319
320   ImageActor actor = ImageActor::New( image );
321   actor.SetSize( 100.0f, 100.0f );
322   actor.SetName("TestImageFilenameActor");
323   actor.SetShaderEffect(effect);
324   Stage::GetCurrent().Add(actor);
325
326   application.SendNotification();
327   application.Render();
328
329   DALI_TEST_CHECK(
330       application.GetGlAbstraction().CheckUniformValue(
331           "uModelView", Matrix::IDENTITY ) );
332   END_TEST;
333 }
334
335 int UtcDaliShaderEffectMethodSetUniformMatrix3(void)
336 {
337   TestApplication application;
338
339   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
340   DALI_TEST_CHECK( effect );
341
342   BufferImage image = CreateBufferImage();
343
344   Matrix3 matIdentity(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
345   effect.SetUniform( "uMatrix3", matIdentity );
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( application.GetGlAbstraction().CheckUniformValue("uMatrix3", matIdentity) );
357   END_TEST;
358 }
359
360 int UtcDaliShaderEffectMethodSetUniformViewport(void)
361 {
362   TestApplication application;
363
364   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
365   DALI_TEST_CHECK( effect );
366
367   BufferImage image = CreateBufferImage();
368
369   ImageActor actor = ImageActor::New( image );
370   actor.SetSize( 100.0f, 100.0f );
371   actor.SetName("TestImageFilenameActor");
372   actor.SetShaderEffect(effect);
373   Stage::GetCurrent().Add(actor);
374
375   effect.SetUniform( "uVec2", Vector2( 0.0f, 0.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
376   effect.SetUniform( "uVec2Dir", Vector2( 1.0f, 2.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_DIRECTION );
377
378   application.SendNotification();
379   application.Render();
380
381   DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "uVec2", Vector2( 0.0f, 0.0f ) ) ); //ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION deprecated
382   DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "uVec2Dir", Vector2( 1.0f, 2.0f ) ) ); //ShaderEffect::COORDINATE_TYPE_VIEWPORT_DIRECTION deprecated
383
384   // change coordinate types
385   effect.SetUniform( "uVec2", Vector2( 0.1f, 0.2f ), ShaderEffect::COORDINATE_TYPE_DEFAULT );
386   effect.SetUniform( "uVec2Dir", Vector2( 1.0f, 2.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
387   actor.SetPixelArea( ImageActor::PixelArea( 0, 0, 10, 10 ) );
388
389   application.SendNotification();
390   application.Render();
391
392   Vector2 outValue;
393   application.GetGlAbstraction().GetUniformValue( "uVec2", outValue );
394   DALI_TEST_EQUALS( outValue, Vector2( 0.1f, 0.2f ), TEST_LOCATION );
395
396   application.GetGlAbstraction().GetUniformValue( "uVec2Dir", outValue );
397   DALI_TEST_EQUALS( outValue, Vector2( 1.0f, 2.0f ), TEST_LOCATION ); //ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION deprecated
398
399   END_TEST;
400 }
401
402 int UtcDaliShaderEffectMethodSetEffectImage(void)
403 {
404   TestApplication application;
405
406   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
407   DALI_TEST_CHECK( effect );
408
409   BufferImage image = CreateBufferImage();
410
411   effect.SetEffectImage(image);
412
413   ImageActor actor = ImageActor::New( image );
414   actor.SetSize( 100.0f, 100.0f );
415   actor.SetName("TestImageFilenameActor");
416   actor.SetShaderEffect(effect);
417   Stage::GetCurrent().Add(actor);
418
419   application.SendNotification();
420   application.Render(16);
421   application.SendNotification();
422   application.Render(16);
423   application.SendNotification();
424
425   GLuint programId, uniformId;
426   bool uniformWasSet = application.GetGlAbstraction().GetUniformIds( "sEffect", programId, uniformId );
427   // we dont care about the value of the sampler uniform as thats internal to DALi core and subject to change
428   DALI_TEST_CHECK( uniformWasSet );
429   END_TEST;
430 }
431
432 int UtcDaliShaderEffectMethodSetEffectImageAndDelete(void)
433 {
434   TestApplication application;
435
436   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
437
438   BufferImage effectImage = CreateBufferImage();
439   effect.SetEffectImage(effectImage);
440
441   ImageActor actor = ImageActor::New();
442
443   actor.SetShaderEffect(effect);
444   effect.Reset();
445
446   Stage::GetCurrent().Add(actor);
447
448   // do an update / render cycle
449   application.SendNotification();
450   application.Render(16);
451   application.SendNotification();
452   application.Render(16);
453   application.SendNotification();
454   application.Render(16);
455
456   printf("removing image actor from stage and Reseting handle\n");
457   Stage::GetCurrent().Remove(actor);
458   actor.Reset();
459
460   tet_printf("### Update & Render  \n");
461
462   application.SendNotification();
463   application.Render(16);
464
465   tet_printf("#### Update Only  \n");
466
467   tet_printf("effectImage.Reset \n");
468
469   // this releases the effect texture resource,
470   // Update will send a DispatchDiscardTexture message to render
471   effectImage.Reset();
472   application.SendNotification();
473   application.UpdateOnly(16);
474
475   tet_printf("#### Update Only \n");
476
477   // at this point shader is deleted, during clear discard queue
478   // and it sends a Shader:: DispatchRemoveObserver message to render thread
479   application.UpdateOnly(16);
480
481   tet_printf("#### Render Only  \n");
482   // This is where it used to crash, there is a message in the queue to perform DispatchDiscardTexture
483   // which tries to call observer->TextureDiscarded, where observer == shader that was deleted
484   // in previous update.
485   application.RenderOnly();
486
487
488   // process the discard texture message
489   application.RenderOnly();
490   application.SendNotification();
491   application.Render(16);
492
493   tet_result(TET_PASS);
494
495   END_TEST;
496 }
497
498 int UtcDaliShaderEffectMethodApplyConstraint(void)
499 {
500   // Test whether Shader's uniform can be constrained to a stationary constraint.
501   TestApplication application;
502
503   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
504   DALI_TEST_CHECK( effect );
505
506   BufferImage image = CreateBufferImage();
507
508   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
509
510   ImageActor actor = ImageActor::New( image );
511   actor.SetSize( 100.0f, 100.0f );
512   actor.SetName("TestImageFilenameActor");
513   actor.SetShaderEffect(effect);
514   Stage::GetCurrent().Add(actor);
515
516   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
517
518   application.SendNotification();
519   application.Render();
520
521   // Test effects of SetUniform...
522   DALI_TEST_CHECK(
523       application.GetGlAbstraction().CheckUniformValue(
524           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
525
526   Constraint constraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
527   constraint.Apply();
528
529   application.SendNotification();
530   application.Render();
531
532   // Test effects of Constraint.
533   DALI_TEST_CHECK(
534       application.GetGlAbstraction().CheckUniformValue(
535           "uVec3", Vector3( 4.0f, 9.0f, 16.0f ) ) );
536   END_TEST;
537 }
538
539 int UtcDaliShaderEffectMethodApplyConstraintOffStage(void)
540 {
541   // The same test as UtcDaliShaderEffectMethodApplyConstraint,
542   // except the Actor is off-stage when the constraint is applied to the shader
543   TestApplication application;
544
545   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
546   DALI_TEST_CHECK( effect );
547
548   BufferImage image = CreateBufferImage();
549
550   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
551
552   ImageActor actor = ImageActor::New( image );
553   actor.SetSize( 100.0f, 100.0f );
554   actor.SetName("TestImageFilenameActor");
555   actor.SetShaderEffect(effect);
556   // Note - Do not add actor to stage here
557
558   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
559
560   Constraint constraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
561   constraint.Apply();
562
563   // Note - Now we add the actor (after constraint was applied to the shader)
564   Stage::GetCurrent().Add(actor);
565
566   application.SendNotification();
567   application.Render();
568
569   // Test effects of Constraint.
570   DALI_TEST_CHECK(
571       application.GetGlAbstraction().CheckUniformValue(
572           "uVec3", Vector3( 4.0f, 9.0f, 16.0f ) ) );
573   END_TEST;
574 }
575
576 int UtcDaliShaderEffectMethodApplyConstraintFromActor(void)
577 {
578   // Test whether Shader's uniform can be constrained to Actor's position.
579   TestApplication application;
580
581   const Vector3 targetPosition = Vector3( 100.0f, 70.0f, 20.0f);
582
583   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
584   DALI_TEST_CHECK( effect );
585
586   BufferImage image = CreateBufferImage();
587
588   effect.SetUniform( "uVec3", Vector3( 50.0f, 25.0f, 0.0f ) );
589
590   ImageActor actor = ImageActor::New( image );
591   actor.SetPosition(targetPosition);
592   actor.SetSize( 100.0f, 100.0f );
593   actor.SetName("TestImageFilenameActor");
594   actor.SetShaderEffect(effect);
595   Stage::GetCurrent().Add(actor);
596
597   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
598
599   Constraint constraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintFromPositionToVector3() );
600   constraint.AddSource( Source( actor, Actor::Property::POSITION ) );
601   constraint.Apply();
602
603   application.SendNotification();
604   application.Render();
605
606   // Test effects of Constraint.
607   DALI_TEST_CHECK(
608       application.GetGlAbstraction().CheckUniformValue(
609           "uVec3", targetPosition ) );
610   END_TEST;
611 }
612
613 int UtcDaliShaderEffectMethodApplyConstraintFromActor2(void)
614 {
615   // Test whether Shader's uniform can be constrained to Actor's position.
616   // While Actor's position is constrained to another point * 2.0f
617   TestApplication application;
618
619   const Vector3 targetPosition = Vector3( 25.0f, 36.0f, 49.0f );
620
621   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
622   DALI_TEST_CHECK( effect );
623
624   BufferImage image = CreateBufferImage();
625
626   effect.SetUniform( "uVec3", Vector3( 50.0f, 25.0f, 0.0f ) );
627
628   ImageActor actor = ImageActor::New( image );
629   actor.SetPosition(Vector3( 100.0f, 70.0f, 20.0f));
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   Constraint shaderConstraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintFromPositionToVector3() );
638   shaderConstraint.AddSource( Source(actor, Actor::Property::POSITION) );
639   shaderConstraint.Apply();
640
641   Constraint actorConstraint = Constraint::New<Vector3>( actor, Actor::Property::POSITION, TestConstraintToVector3Double(targetPosition) );
642   actorConstraint.Apply();
643
644   application.SendNotification();
645   application.Render();
646
647   // Test effects of Constraint.
648   DALI_TEST_CHECK(
649       application.GetGlAbstraction().CheckUniformValue(
650           "uVec3", targetPosition * 2.0f ) );
651   END_TEST;
652 }
653
654 int UtcDaliShaderEffectMethodRemoveConstraints(void)
655 {
656   // Test if constrains can be removed before they are ever applyed.
657   TestApplication application;
658
659   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
660   DALI_TEST_CHECK( effect );
661
662   BufferImage image = CreateBufferImage();
663
664   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
665
666   ImageActor actor = ImageActor::New( image );
667   actor.SetSize( 100.0f, 100.0f );
668   actor.SetName("TestImageFilenameActor");
669   actor.SetShaderEffect(effect);
670   Stage::GetCurrent().Add(actor);
671
672   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
673
674   application.SendNotification();
675   application.Render();
676
677   // Test effects of SetUniform...
678   DALI_TEST_CHECK(
679       application.GetGlAbstraction().CheckUniformValue(
680           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
681
682   Constraint constraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
683   constraint.Apply();
684
685   // Remove the constraints
686   effect.RemoveConstraints();
687
688   application.SendNotification();
689   application.Render();
690
691   // Test effects of Constraint.
692   DALI_TEST_CHECK(
693       application.GetGlAbstraction().CheckUniformValue(
694           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
695   END_TEST;
696 }
697
698 int UtcDaliShaderEffectMethodRemoveConstraints2(void)
699 {
700   // Test whether Shader's uniform constrains can be removed after they are applyed.
701   TestApplication application;
702
703   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
704   DALI_TEST_CHECK( effect );
705
706   BufferImage image = CreateBufferImage();
707
708   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
709
710   ImageActor actor = ImageActor::New( image );
711   actor.SetSize( 100.0f, 100.0f );
712   actor.SetName("TestImageFilenameActor");
713   actor.SetShaderEffect(effect);
714   Stage::GetCurrent().Add(actor);
715
716   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
717
718   application.SendNotification();
719   application.Render();
720
721   // Test effects of SetUniform...
722   DALI_TEST_CHECK(
723       application.GetGlAbstraction().CheckUniformValue(
724           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
725
726   Constraint constraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
727   constraint.Apply();
728
729   application.SendNotification();
730   application.Render();
731
732   // Reset the value and remove the constraints
733   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
734   effect.RemoveConstraints();
735
736   application.SendNotification();
737   application.Render();
738
739   // Test effects of Constraint.
740   DALI_TEST_CHECK(
741       application.GetGlAbstraction().CheckUniformValue(
742           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
743   END_TEST;
744 }
745
746 int UtcDaliShaderEffectPropertyIndices(void)
747 {
748   TestApplication application;
749   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
750
751   Property::IndexContainer indices;
752   effect.GetPropertyIndices( indices );
753   DALI_TEST_CHECK( indices.Size() );
754   DALI_TEST_EQUALS( indices.Size(), effect.GetPropertyCount(), TEST_LOCATION );
755   END_TEST;
756 }
757
758 int UtcDaliShaderBinaries(void)
759 {
760   TestApplication application;
761   // these will not affect the "first round" of dali render as core is already initialized and it has queried the defaults
762   application.GetGlAbstraction().SetBinaryFormats( 1 );
763   application.GetGlAbstraction().SetNumBinaryFormats( 1 );
764   application.GetGlAbstraction().SetProgramBinaryLength( 1 );
765
766   GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
767
768   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
769   DALI_TEST_CHECK( effect );
770
771   BufferImage image = CreateBufferImage();
772   ImageActor actor = ImageActor::New( image );
773   actor.SetSize( 100.0f, 100.0f );
774   actor.SetName("TestImageFilenameActor");
775   actor.SetShaderEffect(effect);
776   Stage::GetCurrent().Add(actor);
777
778   application.SendNotification();
779   application.Render(16);
780   // binary was not requested by DALi
781   DALI_TEST_CHECK( !(application.GetGlAbstraction().GetProgramBinaryCalled()) );
782
783   GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
784
785   // check that the shader was compiled
786   DALI_TEST_EQUALS( lastShaderCompiledAfter, lastShaderCompiledBefore + 2, TEST_LOCATION );
787
788   // simulate context loss to get core to re-initialize its GL
789   application.GetCore().ContextDestroyed();
790   application.GetCore().ContextCreated();
791
792   application.SendNotification();
793   application.Render(16);
794
795   // shader is recompiled
796   GLuint finalShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
797   // check that the shader was compiled
798   DALI_TEST_EQUALS( lastShaderCompiledAfter + 2, finalShaderCompiled, TEST_LOCATION );
799
800   // binary was requested by DALi
801   DALI_TEST_CHECK( application.GetGlAbstraction().GetProgramBinaryCalled() );
802
803   END_TEST;
804 }
805
806 int UtcDaliShaderEffectFromPropertiesP(void)
807 {
808   TestApplication application;
809   tet_infoline("UtcDaliShaderEffectFromProperties01()");
810
811   std::string fragmentShaderPrefix = "#define TEST_FS 1\n#extension GL_OES_standard_derivatives : enable";
812   std::string vertexShaderPrefix = "#define TEST_VS 1";
813   std::string vertexShader(VertexSource);
814   std::string fragmentShader(FragmentSource);
815
816   // Call render to compile default shaders.
817   application.SendNotification();
818   application.Render();
819
820   GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
821
822   // create from type registry
823
824   TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
825   DALI_TEST_CHECK( typeInfo );
826   ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
827   DALI_TEST_CHECK( effect );
828
829   Property::Value programValue = Property::Value(Property::MAP);
830   Property::Map* programMap = programValue.GetMap();
831   DALI_TEST_CHECK( programMap );
832
833   programMap->Insert("vertex", vertexShader);
834   programMap->Insert("fragment", fragmentShader);
835
836   programMap->Insert("vertexPrefix",  vertexShaderPrefix);
837   programMap->Insert("fragmentPrefix",  fragmentShaderPrefix);
838
839   effect.SetProperty(effect.GetPropertyIndex("program"), programValue);
840
841   Property::Value imageValue = Property::Value(Property::MAP);
842   Property::Map* imageMap = imageValue.GetMap();
843   DALI_TEST_CHECK( imageMap );
844   imageMap->Insert("filename", Property::Value(TestImageFilename));
845   effect.SetProperty(effect.GetPropertyIndex("image"), imageValue);
846
847   // do a update & render to get the image request
848   application.SendNotification();
849   application.Render();
850
851   Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
852   // create the image
853   Integration::Bitmap* bitmap = CreateBitmap( 10, 10, 0xFF );
854   Integration::ResourcePointer resourcePtr(bitmap);
855   TestPlatformAbstraction& platform = application.GetPlatform();
856   platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resourcePtr);
857
858   BufferImage image(CreateBufferImage());
859   ImageActor actor = ImageActor::New( image );
860   actor.SetSize( 100.0f, 100.0f );
861   actor.SetName("TestImageFilenameActor");
862   actor.SetShaderEffect(effect);
863   Stage::GetCurrent().Add(actor);
864
865   application.SendNotification();
866   application.Render();
867   GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
868
869   // we should have compiled 2 shaders.
870   DALI_TEST_EQUALS(lastShaderCompiledAfter, lastShaderCompiledBefore + 2, TEST_LOCATION );
871
872   std::string actualVertexShader = application.GetGlAbstraction().GetShaderSource( lastShaderCompiledBefore + 1 );
873   DALI_TEST_EQUALS( vertexShaderPrefix, actualVertexShader.substr( 0, vertexShaderPrefix.length() ), TEST_LOCATION );
874   DALI_TEST_EQUALS( vertexShader, actualVertexShader.substr( actualVertexShader.length() - vertexShader.length() ), TEST_LOCATION );
875
876   std::string actualFragmentShader = application.GetGlAbstraction().GetShaderSource( lastShaderCompiledBefore + 2 );
877   DALI_TEST_EQUALS( fragmentShaderPrefix, actualFragmentShader.substr( 0, fragmentShaderPrefix.length() ), TEST_LOCATION );
878   DALI_TEST_EQUALS( fragmentShader, actualFragmentShader.substr( actualFragmentShader.length() - fragmentShader.length() ), TEST_LOCATION );
879
880   END_TEST;
881 }
882
883 int UtcDaliShaderEffectFromPropertiesN(void)
884 {
885   try
886   {
887     TestApplication application;
888     tet_infoline("UtcDaliShaderEffectFromProperties02()");
889
890     // Call render to compile default shaders.
891     application.SendNotification();
892     application.Render();
893
894     // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
895     TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
896     DALI_TEST_CHECK( typeInfo );
897     ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
898     DALI_TEST_CHECK( effect );
899
900     Property::Value programValue = Property::Value(Property::MAP);
901     Property::Map* programMap = programValue.GetMap();
902     DALI_TEST_CHECK( programMap );
903
904     programMap->Insert("vertex",   std::string(VertexSource));
905     programMap->Insert("fragment", std::string(FragmentSource));
906
907     // use wrong index on purpose
908     effect.SetProperty(effect.GetPropertyIndex("program") + 1, programValue );
909
910     tet_result( TET_FAIL );
911   }
912   catch(Dali::DaliException& e)
913   {
914     DALI_TEST_PRINT_ASSERT( e );
915   }
916   END_TEST;
917 }
918
919 int UtcDaliShaderEffectFromProperties2N(void)
920 {
921   try
922   {
923     TestApplication application;
924     tet_infoline("UtcDaliShaderEffectFromProperties03()");
925
926     // Call render to compile default shaders.
927     application.SendNotification();
928     application.Render();
929
930     // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
931     TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
932     DALI_TEST_CHECK( typeInfo );
933     ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
934     DALI_TEST_CHECK( effect );
935
936     // dont set unknown
937     effect.SetProperty( effect.GetPropertyIndex("geometryHints"), "HINT_2" );
938
939     tet_result( TET_FAIL );
940   }
941   catch(Dali::DaliException& e)
942   {
943     DALI_TEST_PRINT_ASSERT( e );
944   }
945   END_TEST;
946 }