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