Remove extension from shader.
[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::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   const Vector2& stageSize(Stage::GetCurrent().GetSize());
382
383   DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "uVec2", Vector2( stageSize.x/2, -stageSize.y/2 ) ) );
384
385   DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "uVec2Dir", Vector2( -1.0f, 2.0f ) ) );
386
387   // change coordinate types
388   effect.SetUniform( "uVec2", Vector2( 0.1f, 0.2f ), ShaderEffect::COORDINATE_TYPE_DEFAULT );
389   effect.SetUniform( "uVec2Dir", Vector2( 1.0f, 2.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
390   actor.SetPixelArea( ImageActor::PixelArea( 0, 0, 10, 10 ) );
391
392   application.SendNotification();
393   application.Render();
394
395   Vector2 outValue;
396   application.GetGlAbstraction().GetUniformValue( "uVec2", outValue );
397   DALI_TEST_EQUALS( outValue, Vector2( 0.1f, 0.2f ), TEST_LOCATION );
398
399   application.GetGlAbstraction().GetUniformValue( "uVec2Dir", outValue );
400   DALI_TEST_EQUALS( outValue, Vector2( stageSize.x *.5f - 1.f, -stageSize.y * .5f + 2.f), TEST_LOCATION );
401
402   END_TEST;
403 }
404
405 int UtcDaliShaderEffectMethodSetEffectImage(void)
406 {
407   TestApplication application;
408
409   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
410   DALI_TEST_CHECK( effect );
411
412   BufferImage image = CreateBufferImage();
413
414   effect.SetEffectImage(image);
415
416   ImageActor actor = ImageActor::New( image );
417   actor.SetSize( 100.0f, 100.0f );
418   actor.SetName("TestImageFilenameActor");
419   actor.SetShaderEffect(effect);
420   Stage::GetCurrent().Add(actor);
421
422   application.SendNotification();
423   application.Render(16);
424   application.SendNotification();
425   application.Render(16);
426   application.SendNotification();
427
428   GLuint programId, uniformId;
429   bool uniformWasSet = application.GetGlAbstraction().GetUniformIds( "sEffect", programId, uniformId );
430   // we dont care about the value of the sampler uniform as thats internal to DALi core and subject to change
431   DALI_TEST_CHECK( uniformWasSet );
432   END_TEST;
433 }
434
435 int UtcDaliShaderEffectMethodSetEffectImageAndDelete(void)
436 {
437   TestApplication application;
438
439   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
440
441   BufferImage effectImage = CreateBufferImage();
442   effect.SetEffectImage(effectImage);
443
444   ImageActor actor = ImageActor::New();
445
446   actor.SetShaderEffect(effect);
447   effect.Reset();
448
449   Stage::GetCurrent().Add(actor);
450
451   // do an update / render cycle
452   application.SendNotification();
453   application.Render(16);
454   application.SendNotification();
455   application.Render(16);
456   application.SendNotification();
457   application.Render(16);
458
459   printf("removing image actor from stage and Reseting handle\n");
460   Stage::GetCurrent().Remove(actor);
461   actor.Reset();
462
463   tet_printf("### Update & Render  \n");
464
465   application.SendNotification();
466   application.Render(16);
467
468   tet_printf("#### Update Only  \n");
469
470   tet_printf("effectImage.Reset \n");
471
472   // this releases the effect texture resource,
473   // Update will send a DispatchDiscardTexture message to render
474   effectImage.Reset();
475   application.SendNotification();
476   application.UpdateOnly(16);
477
478   tet_printf("#### Update Only \n");
479
480   // at this point shader is deleted, during clear discard queue
481   // and it sends a Shader:: DispatchRemoveObserver message to render thread
482   application.UpdateOnly(16);
483
484   tet_printf("#### Render Only  \n");
485   // This is where it used to crash, there is a message in the queue to perform DispatchDiscardTexture
486   // which tries to call observer->TextureDiscarded, where observer == shader that was deleted
487   // in previous update.
488   application.RenderOnly();
489
490
491   // process the discard texture message
492   application.RenderOnly();
493   application.SendNotification();
494   application.Render(16);
495
496   tet_result(TET_PASS);
497
498   END_TEST;
499 }
500
501 int UtcDaliShaderEffectMethodApplyConstraint(void)
502 {
503   // Test whether Shader's uniform can be constrained to a stationary constraint.
504   TestApplication application;
505
506   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
507   DALI_TEST_CHECK( effect );
508
509   BufferImage image = CreateBufferImage();
510
511   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
512
513   ImageActor actor = ImageActor::New( image );
514   actor.SetSize( 100.0f, 100.0f );
515   actor.SetName("TestImageFilenameActor");
516   actor.SetShaderEffect(effect);
517   Stage::GetCurrent().Add(actor);
518
519   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
520
521   application.SendNotification();
522   application.Render();
523
524   // Test effects of SetUniform...
525   DALI_TEST_CHECK(
526       application.GetGlAbstraction().CheckUniformValue(
527           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
528
529   Constraint constraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
530   constraint.Apply();
531
532   application.SendNotification();
533   application.Render();
534
535   // Test effects of Constraint.
536   DALI_TEST_CHECK(
537       application.GetGlAbstraction().CheckUniformValue(
538           "uVec3", Vector3( 4.0f, 9.0f, 16.0f ) ) );
539   END_TEST;
540 }
541
542
543 int UtcDaliShaderEffectMethodApplyConstraintFromActor(void)
544 {
545   // Test whether Shader's uniform can be constrained to Actor's position.
546   TestApplication application;
547
548   const Vector3 targetPosition = Vector3( 100.0f, 70.0f, 20.0f);
549
550   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
551   DALI_TEST_CHECK( effect );
552
553   BufferImage image = CreateBufferImage();
554
555   effect.SetUniform( "uVec3", Vector3( 50.0f, 25.0f, 0.0f ) );
556
557   ImageActor actor = ImageActor::New( image );
558   actor.SetPosition(targetPosition);
559   actor.SetSize( 100.0f, 100.0f );
560   actor.SetName("TestImageFilenameActor");
561   actor.SetShaderEffect(effect);
562   Stage::GetCurrent().Add(actor);
563
564   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
565
566   Constraint constraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintFromPositionToVector3() );
567   constraint.AddSource( Source( actor, Actor::Property::POSITION ) );
568   constraint.Apply();
569
570   application.SendNotification();
571   application.Render();
572
573   // Test effects of Constraint.
574   DALI_TEST_CHECK(
575       application.GetGlAbstraction().CheckUniformValue(
576           "uVec3", targetPosition ) );
577   END_TEST;
578 }
579
580 int UtcDaliShaderEffectMethodApplyConstraintFromActor2(void)
581 {
582   // Test whether Shader's uniform can be constrained to Actor's position.
583   // While Actor's position is constrained to another point * 2.0f
584   TestApplication application;
585
586   const Vector3 targetPosition = Vector3( 25.0f, 36.0f, 49.0f );
587
588   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
589   DALI_TEST_CHECK( effect );
590
591   BufferImage image = CreateBufferImage();
592
593   effect.SetUniform( "uVec3", Vector3( 50.0f, 25.0f, 0.0f ) );
594
595   ImageActor actor = ImageActor::New( image );
596   actor.SetPosition(Vector3( 100.0f, 70.0f, 20.0f));
597   actor.SetSize( 100.0f, 100.0f );
598   actor.SetName("TestImageFilenameActor");
599   actor.SetShaderEffect(effect);
600   Stage::GetCurrent().Add(actor);
601
602   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
603
604   Constraint shaderConstraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintFromPositionToVector3() );
605   shaderConstraint.AddSource( Source(actor, Actor::Property::POSITION) );
606   shaderConstraint.Apply();
607
608   Constraint actorConstraint = Constraint::New<Vector3>( actor, Actor::Property::POSITION, TestConstraintToVector3Double(targetPosition) );
609   actorConstraint.Apply();
610
611   application.SendNotification();
612   application.Render();
613
614   // Test effects of Constraint.
615   DALI_TEST_CHECK(
616       application.GetGlAbstraction().CheckUniformValue(
617           "uVec3", targetPosition * 2.0f ) );
618   END_TEST;
619 }
620
621 int UtcDaliShaderEffectMethodRemoveConstraints(void)
622 {
623   // Test if constrains can be removed before they are ever applyed.
624   TestApplication application;
625
626   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
627   DALI_TEST_CHECK( effect );
628
629   BufferImage image = CreateBufferImage();
630
631   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
632
633   ImageActor actor = ImageActor::New( image );
634   actor.SetSize( 100.0f, 100.0f );
635   actor.SetName("TestImageFilenameActor");
636   actor.SetShaderEffect(effect);
637   Stage::GetCurrent().Add(actor);
638
639   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
640
641   application.SendNotification();
642   application.Render();
643
644   // Test effects of SetUniform...
645   DALI_TEST_CHECK(
646       application.GetGlAbstraction().CheckUniformValue(
647           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
648
649   Constraint constraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
650   constraint.Apply();
651
652   // Remove the constraints
653   effect.RemoveConstraints();
654
655   application.SendNotification();
656   application.Render();
657
658   // Test effects of Constraint.
659   DALI_TEST_CHECK(
660       application.GetGlAbstraction().CheckUniformValue(
661           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
662   END_TEST;
663 }
664
665 int UtcDaliShaderEffectMethodRemoveConstraints2(void)
666 {
667   // Test whether Shader's uniform constrains can be removed after they are applyed.
668   TestApplication application;
669
670   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
671   DALI_TEST_CHECK( effect );
672
673   BufferImage image = CreateBufferImage();
674
675   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
676
677   ImageActor actor = ImageActor::New( image );
678   actor.SetSize( 100.0f, 100.0f );
679   actor.SetName("TestImageFilenameActor");
680   actor.SetShaderEffect(effect);
681   Stage::GetCurrent().Add(actor);
682
683   Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
684
685   application.SendNotification();
686   application.Render();
687
688   // Test effects of SetUniform...
689   DALI_TEST_CHECK(
690       application.GetGlAbstraction().CheckUniformValue(
691           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
692
693   Constraint constraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
694   constraint.Apply();
695
696   application.SendNotification();
697   application.Render();
698
699   // Reset the value and remove the constraints
700   effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
701   effect.RemoveConstraints();
702
703   application.SendNotification();
704   application.Render();
705
706   // Test effects of Constraint.
707   DALI_TEST_CHECK(
708       application.GetGlAbstraction().CheckUniformValue(
709           "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
710   END_TEST;
711 }
712
713 int UtcDaliShaderEffectPropertyIndices(void)
714 {
715   TestApplication application;
716   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
717
718   Property::IndexContainer indices;
719   effect.GetPropertyIndices( indices );
720   DALI_TEST_CHECK( ! indices.empty() );
721   DALI_TEST_EQUALS( indices.size(), effect.GetPropertyCount(), TEST_LOCATION );
722   END_TEST;
723 }
724
725 int UtcDaliShaderBinaries(void)
726 {
727   TestApplication application;
728   // these will not affect the "first round" of dali render as core is already initialized and it has queried the defaults
729   application.GetGlAbstraction().SetBinaryFormats( 1 );
730   application.GetGlAbstraction().SetNumBinaryFormats( 1 );
731   application.GetGlAbstraction().SetProgramBinaryLength( 1 );
732
733   GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
734
735   ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
736   DALI_TEST_CHECK( effect );
737
738   BufferImage image = CreateBufferImage();
739   ImageActor actor = ImageActor::New( image );
740   actor.SetSize( 100.0f, 100.0f );
741   actor.SetName("TestImageFilenameActor");
742   actor.SetShaderEffect(effect);
743   Stage::GetCurrent().Add(actor);
744
745   application.SendNotification();
746   application.Render(16);
747   // binary was not requested by DALi
748   DALI_TEST_CHECK( !(application.GetGlAbstraction().GetProgramBinaryCalled()) );
749
750   GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
751
752   // check that the shader was compiled
753   DALI_TEST_EQUALS( lastShaderCompiledAfter, lastShaderCompiledBefore + 2, TEST_LOCATION );
754
755   // simulate context loss to get core to re-initialize its GL
756   application.GetCore().ContextDestroyed();
757   application.GetCore().ContextCreated();
758
759   application.SendNotification();
760   application.Render(16);
761
762   // shader is recompiled
763   GLuint finalShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
764   // check that the shader was compiled
765   DALI_TEST_EQUALS( lastShaderCompiledAfter + 2, finalShaderCompiled, TEST_LOCATION );
766
767   // binary was requested by DALi
768   DALI_TEST_CHECK( application.GetGlAbstraction().GetProgramBinaryCalled() );
769
770   END_TEST;
771 }
772
773 int UtcDaliShaderEffectFromProperties01(void)
774 {
775   TestApplication application;
776   tet_infoline("UtcDaliShaderEffectFromProperties01()");
777
778   std::string fragmentShaderPrefix = "#define TEST_FS 1\n#extension GL_OES_standard_derivatives : enable";
779   std::string vertexShaderPrefix = "#define TEST_VS 1";
780   std::string vertexShader(VertexSource);
781   std::string fragmentShader(FragmentSource);
782
783   // Call render to compile default shaders.
784   application.SendNotification();
785   application.Render();
786
787   GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
788
789   // create from type registry
790
791   TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
792   DALI_TEST_CHECK( typeInfo );
793   ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
794   DALI_TEST_CHECK( effect );
795
796   Property::Value programMap = Property::Value(Property::MAP);
797
798   programMap.SetValue("vertex", vertexShader);
799   programMap.SetValue("fragment", fragmentShader);
800
801   programMap.SetValue("vertex-prefix", vertexShaderPrefix);
802   programMap.SetValue("fragment-prefix", fragmentShaderPrefix);
803
804   programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
805
806   effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
807
808   Property::Value imageMap = Property::Value(Property::MAP);
809   imageMap.SetValue("filename", Property::Value(TestImageFilename));
810   effect.SetProperty(effect.GetPropertyIndex("image"), imageMap);
811
812   // do a update & render to get the image request
813   application.SendNotification();
814   application.Render();
815
816   Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
817   // create the image
818   Integration::Bitmap* bitmap = CreateBitmap( 10, 10, 0xFF );
819   Integration::ResourcePointer resourcePtr(bitmap);
820   TestPlatformAbstraction& platform = application.GetPlatform();
821   platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resourcePtr);
822
823   BufferImage image(CreateBufferImage());
824   ImageActor actor = ImageActor::New( image );
825   actor.SetSize( 100.0f, 100.0f );
826   actor.SetName("TestImageFilenameActor");
827   actor.SetShaderEffect(effect);
828   Stage::GetCurrent().Add(actor);
829
830   application.SendNotification();
831   application.Render();
832   GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
833
834   // we should have compiled 2 shaders.
835   DALI_TEST_EQUALS(lastShaderCompiledAfter, lastShaderCompiledBefore + 2, TEST_LOCATION );
836
837   std::string actualVertexShader = application.GetGlAbstraction().GetShaderSource( lastShaderCompiledBefore + 1 );
838   DALI_TEST_EQUALS( vertexShaderPrefix, actualVertexShader.substr( 0, vertexShaderPrefix.length() ), TEST_LOCATION );
839   DALI_TEST_EQUALS( vertexShader, actualVertexShader.substr( actualVertexShader.length() - vertexShader.length() ), TEST_LOCATION );
840
841   std::string actualFragmentShader = application.GetGlAbstraction().GetShaderSource( lastShaderCompiledBefore + 2 );
842   DALI_TEST_EQUALS( fragmentShaderPrefix, actualFragmentShader.substr( 0, fragmentShaderPrefix.length() ), TEST_LOCATION );
843   DALI_TEST_EQUALS( fragmentShader, actualFragmentShader.substr( actualFragmentShader.length() - fragmentShader.length() ), TEST_LOCATION );
844
845   END_TEST;
846 }
847
848 int UtcDaliShaderEffectFromProperties02(void)
849 {
850   try
851   {
852     TestApplication application;
853     tet_infoline("UtcDaliShaderEffectFromProperties02()");
854
855     // Call render to compile default shaders.
856     application.SendNotification();
857     application.Render();
858     application.Render();
859     application.Render();
860
861     // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
862     TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
863     DALI_TEST_CHECK( typeInfo );
864     ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
865     DALI_TEST_CHECK( effect );
866
867     Property::Value programMap = Property::Value(Property::MAP);
868
869     programMap.SetValue("vertex",   std::string(VertexSource));
870     programMap.SetValue("fragment", std::string(FragmentSource));
871
872     // programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
873     // dont set by value
874     programMap.SetValue("geometry-type", GeometryType( GEOMETRY_TYPE_IMAGE ));
875
876     effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
877
878     tet_result( TET_FAIL );
879   }
880   catch(Dali::DaliException& e)
881   {
882     DALI_TEST_PRINT_ASSERT( e );
883   }
884   END_TEST;
885 }
886
887 int UtcDaliShaderEffectFromProperties03(void)
888 {
889   try
890   {
891     TestApplication application;
892     tet_infoline("UtcDaliShaderEffectFromProperties03()");
893
894     // Call render to compile default shaders.
895     application.SendNotification();
896     application.Render();
897     application.Render();
898     application.Render();
899
900     // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
901     TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
902     DALI_TEST_CHECK( typeInfo );
903     ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
904     DALI_TEST_CHECK( effect );
905
906     // dont set unknown
907     effect.SetProperty( effect.GetPropertyIndex("geometry-hints"), "HINT_2" );
908
909     tet_result( TET_FAIL );
910   }
911   catch(Dali::DaliException& e)
912   {
913     DALI_TEST_PRINT_ASSERT( e );
914   }
915   END_TEST;
916 }