2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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
8 // http://floralicense.org/license/
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.
22 #include <dali/public-api/dali-core.h>
23 #include <dali/internal/event/images/image-impl.h>
24 #include <dali-test-suite-utils.h>
25 #include "../../../build/slp/dali-core/dali-shaders.h"
29 static void Startup();
30 static void Cleanup();
33 void (*tet_startup)() = Startup;
34 void (*tet_cleanup)() = Cleanup;
37 static const char* VertexSource =
40 " gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);\n"
41 " vTexCoord = aTexCoord;\n"
44 static const char* FragmentSource =
47 " gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n"
50 static const char* FragmentSourceUsingExtensions =
53 " float floatValue = 0.5f;\n"
54 " float test = fwidth(floatValue);\n"
55 " gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n"
56 " gl_FragColor.a *= test;\n"
59 const int GETSOURCE_BUFFER_SIZE = 0x10000;
62 POSITIVE_TC_IDX = 0x01,
66 #define MAX_NUMBER_OF_TESTS 10000
68 struct tet_testlist tet_testlist[MAX_NUMBER_OF_TESTS];
71 static const char* TestImageFilename = "icon_wrt.png";
73 // Add test functionality for all APIs in the class (Positive and Negative)
74 TEST_FUNCTION( UtcDaliShaderEffectMethodNew01, POSITIVE_TC_IDX );
75 TEST_FUNCTION( UtcDaliShaderEffectMethodNew02, NEGATIVE_TC_IDX );
76 TEST_FUNCTION( UtcDaliShaderEffectMethodNew03, POSITIVE_TC_IDX );
77 TEST_FUNCTION( UtcDaliShaderEffectMethodNew04, POSITIVE_TC_IDX );
78 TEST_FUNCTION( UtcDaliShaderEffectMethodNew05, POSITIVE_TC_IDX );
79 TEST_FUNCTION( UtcDaliShaderEffectMethodNew06, POSITIVE_TC_IDX );
80 TEST_FUNCTION( UtcDaliShaderEffectMethodDelete01, POSITIVE_TC_IDX );
81 TEST_FUNCTION( UtcDaliShaderEffectMethodDownCast, POSITIVE_TC_IDX );
82 TEST_FUNCTION( UtcDaliShaderEffectMethodSetUniformFloat, POSITIVE_TC_IDX );
83 TEST_FUNCTION( UtcDaliShaderEffectMethodSetUniformVector2, POSITIVE_TC_IDX );
84 TEST_FUNCTION( UtcDaliShaderEffectMethodSetUniformVector3, POSITIVE_TC_IDX );
85 TEST_FUNCTION( UtcDaliShaderEffectMethodSetUniformVector4, POSITIVE_TC_IDX );
86 TEST_FUNCTION( UtcDaliShaderEffectMethodSetUniformMatrix, POSITIVE_TC_IDX );
88 TEST_FUNCTION( UtcDaliShaderEffectMethodSetUniformMatrix3, POSITIVE_TC_IDX );
89 TEST_FUNCTION( UtcDaliShaderEffectMethodSetUniformViewport, POSITIVE_TC_IDX );
90 TEST_FUNCTION( UtcDaliShaderEffectMethodSetEffectImage, POSITIVE_TC_IDX );
91 TEST_FUNCTION( UtcDaliShaderEffectMethodSetEffectImageAndDelete, POSITIVE_TC_IDX );
92 TEST_FUNCTION( UtcDaliShaderEffectMethodApplyConstraint, POSITIVE_TC_IDX );
93 TEST_FUNCTION( UtcDaliShaderEffectMethodApplyConstraintFromActor, POSITIVE_TC_IDX );
94 TEST_FUNCTION( UtcDaliShaderEffectMethodApplyConstraintFromActor2, POSITIVE_TC_IDX );
95 TEST_FUNCTION( UtcDaliShaderEffectMethodApplyConstraintCallback, POSITIVE_TC_IDX );
96 TEST_FUNCTION( UtcDaliShaderEffectMethodRemoveConstraints, POSITIVE_TC_IDX );
97 TEST_FUNCTION( UtcDaliShaderEffectMethodRemoveConstraints2, POSITIVE_TC_IDX );
99 TEST_FUNCTION( UtcDaliShaderEffectMethodCreateExtension, POSITIVE_TC_IDX );
100 TEST_FUNCTION( UtcDaliShaderEffectMethodCreateExtension2, POSITIVE_TC_IDX );
101 TEST_FUNCTION( UtcDaliShaderEffectMethodNoExtension, NEGATIVE_TC_IDX );
102 TEST_FUNCTION( UtcDaliShaderEffectFromProperties01, POSITIVE_TC_IDX );
103 TEST_FUNCTION( UtcDaliShaderEffectFromProperties02, NEGATIVE_TC_IDX );
104 TEST_FUNCTION( UtcDaliShaderEffectFromProperties03, NEGATIVE_TC_IDX );
106 TEST_FUNCTION( UtcDaliShaderEffectPropertyIndices, POSITIVE_TC_IDX );
108 // Called only once before first test is run.
109 static void Startup()
113 // Called only once after last test is run
114 static void Cleanup()
118 // Create bitmap image
119 BitmapImage CreateBitmapImage()
121 BitmapImage image = BitmapImage::New(4,4,Pixel::RGBA8888);
123 PixelBuffer* pixbuf = image.GetBuffer();
125 // Using a 4x4 image gives a better blend with the GL implementation
127 for(size_t i=0; i<16; i++)
129 pixbuf[i*4+0] = 0xFF;
130 pixbuf[i*4+1] = 0xFF;
131 pixbuf[i*4+2] = 0xFF;
132 pixbuf[i*4+3] = 0xFF;
138 struct TestConstraintToVector3
140 TestConstraintToVector3(Vector3 target)
145 Vector3 operator()(const Vector3& current)
153 struct TestConstraintFromPositionToVector3
155 TestConstraintFromPositionToVector3()
159 Vector3 operator()(const Vector3& current, const PropertyInput& position)
162 return position.GetVector3();
166 struct TestConstraintToVector3Double
168 TestConstraintToVector3Double(Vector3 target)
173 Vector3 operator()(const Vector3& current)
175 return mTarget * 2.0f;
181 static void UtcDaliShaderEffectMethodNew01()
183 TestApplication application;
185 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
186 DALI_TEST_CHECK(effect);
189 static void UtcDaliShaderEffectMethodNew02()
191 TestApplication application;
197 // New must be called to create a ShaderEffect or it wont be valid.
198 effect.SetUniform( "uUniform", 0 );
199 DALI_TEST_CHECK( false );
201 catch (Dali::DaliException& e)
203 // Tests that a negative test of an assertion succeeds
204 tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
205 DALI_TEST_CHECK( !effect );
209 static void UtcDaliShaderEffectMethodNew03()
211 TestApplication application;
213 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource, VertexSource, FragmentSource, ShaderEffect::HINT_NONE );
214 DALI_TEST_CHECK(effect);
217 static void UtcDaliShaderEffectMethodNew04()
219 TestApplication application;
220 tet_infoline("Testing prefixed version of Dali::ShaderEffect::New()");
222 std::string fragmentShaderPrefix = "#define TEST_FS 1\n#extension GL_OES_standard_derivatives : enable";
223 std::string vertexShaderPrefix = "#define TEST_VS 1";
227 // Call render to compile default shaders.
228 application.SendNotification();
229 application.Render();
230 application.Render();
231 application.Render();
233 GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
234 ShaderEffect effect = ShaderEffect::NewWithPrefix( vertexShaderPrefix, VertexSource,
235 fragmentShaderPrefix, FragmentSourceUsingExtensions,
236 GEOMETRY_TYPE_IMAGE, ShaderEffect::HINT_NONE );
238 BitmapImage image = CreateBitmapImage();
239 ImageActor actor = ImageActor::New( image );
240 actor.SetSize( 100.0f, 100.0f );
241 actor.SetName("TestImageFilenameActor");
242 actor.SetShaderEffect(effect);
243 Stage::GetCurrent().Add(actor);
245 application.SendNotification();
246 application.Render();
247 GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
248 bool testResult = false;
250 // we should have compiled 4 shaders.
251 if (lastShaderCompiledAfter - lastShaderCompiledBefore == 4)
253 char testVertexSourceResult[GETSOURCE_BUFFER_SIZE];
254 char testFragmentSourceResult[GETSOURCE_BUFFER_SIZE];
256 // we are interested in the first two.
257 GLuint vertexShaderId = lastShaderCompiledBefore + 1;
258 GLuint fragmentShaderId = lastShaderCompiledBefore + 2;
260 GLsizei lengthVertexResult;
261 GLsizei lengthFragmentResult;
263 application.GetGlAbstraction().GetShaderSource(vertexShaderId, GETSOURCE_BUFFER_SIZE, &lengthVertexResult, testVertexSourceResult);
264 application.GetGlAbstraction().GetShaderSource(fragmentShaderId, GETSOURCE_BUFFER_SIZE, &lengthFragmentResult, testFragmentSourceResult);
266 int vertexShaderHasPrefix = strncmp(testVertexSourceResult, "#define ", strlen("#define "));
267 int fragmentShaderHasPrefix = strncmp(testFragmentSourceResult, "#define ", strlen("#define "));
268 testResult = (vertexShaderHasPrefix == 0) && (fragmentShaderHasPrefix == 0);
271 DALI_TEST_CHECK(testResult);
273 catch(Dali::DaliException& e)
275 tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
276 tet_result( TET_FAIL );
280 class ShaderEffectExtension : public ShaderEffect::Extension {};
281 static void UtcDaliShaderEffectMethodNew05()
283 TestApplication application;
285 // heap constructor / destructor
286 DefaultFunctionCoverage<ShaderEffect> shaderEffect;
287 DefaultFunctionCoverage<ShaderEffectExtension> shaderEffectExtension;
291 static void UtcDaliShaderEffectMethodNew06()
293 TestApplication application;
294 tet_infoline("Testing Dali::ShaderEffect::New() with shader sources for different geometry types");
296 ShaderEffect effect = ShaderEffect::New( "imageVertexShader", "imageFragmentShader",
297 "textVertexShader", "textFragmentShader",
298 "texturedMeshVertexShader", "texturedMeshFragmentShader",
299 "meshVertexShader", "meshFragmentShader",
300 ShaderEffect::HINT_NONE );
301 DALI_TEST_CHECK(effect);
305 static void UtcDaliShaderEffectMethodDownCast()
307 TestApplication application;
308 tet_infoline("Testing Dali::ShaderEffect::DownCast()");
310 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
312 BaseHandle object(effect);
314 ShaderEffect effect2 = ShaderEffect::DownCast(object);
315 DALI_TEST_CHECK(effect2);
317 ShaderEffect effect3 = DownCast< ShaderEffect >(object);
318 DALI_TEST_CHECK(effect3);
320 BaseHandle unInitializedObject;
321 ShaderEffect effect4 = ShaderEffect::DownCast(unInitializedObject);
322 DALI_TEST_CHECK(!effect4);
324 ShaderEffect effect5 = DownCast< ShaderEffect >(unInitializedObject);
325 DALI_TEST_CHECK(!effect5);
328 static void UtcDaliShaderEffectMethodDelete01()
330 TestApplication application;
332 // get the default shaders built, this is not required but makes it
333 // easier to debug the TET case and isolate the custom shader compilation.
334 application.SendNotification();
335 application.Render();
337 application.SendNotification();
338 application.Render();
340 // create a new shader effect
341 // the vertex and fragment shader will be cached in the ShaderFactory
342 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource);
344 // destroy the shader effect
347 // Create the same shader effect again, this should now use the cached version
348 // held in the shader factory
349 effect= ShaderEffect::New( VertexSource, FragmentSource );
351 // Compile the shader effect
352 application.SendNotification();
353 application.Render();
355 GLuint lastShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
357 // get the vertex shader (compiled before fragment shader).
358 // this last shaders compiled is for text.
359 GLuint vertexShaderId = lastShaderCompiled-1;
360 GLsizei lengthVertexResult;
362 char testVertexSourceResult[GETSOURCE_BUFFER_SIZE];
363 application.GetGlAbstraction().GetShaderSource(vertexShaderId, GETSOURCE_BUFFER_SIZE, &lengthVertexResult, testVertexSourceResult);
365 // compare the first 10 bytes of the vertex shader sent to be compiled, with
366 // the shader string that ended up being compiled (in the render task)
367 // this is to confirm the string hasn't been deleted / corrupted.
368 int testPassed = strncmp( testVertexSourceResult , Dali::Internal::CustomFontPrefixVertex.c_str(), 10 ) ;
370 DALI_TEST_CHECK( testPassed == 0 );
373 static void UtcDaliShaderEffectMethodSetUniformFloat()
375 TestApplication application;
377 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
378 DALI_TEST_CHECK( effect );
380 BitmapImage image = CreateBitmapImage();
382 effect.SetUniform( "uFloat", 1.0f );
384 ImageActor actor = ImageActor::New( image );
385 actor.SetSize( 100.0f, 100.0f );
386 actor.SetName("TestImageFilenameActor");
387 actor.SetShaderEffect(effect);
388 Stage::GetCurrent().Add(actor);
390 application.SendNotification();
391 application.Render();
394 application.GetGlAbstraction().CheckUniformValue(
398 static void UtcDaliShaderEffectMethodSetUniformVector2()
400 TestApplication application;
402 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
403 DALI_TEST_CHECK( effect );
405 BitmapImage image = CreateBitmapImage();
407 effect.SetUniform( "uVec2", Vector2( 2.0f, 3.0f ) );
409 ImageActor actor = ImageActor::New( image );
410 actor.SetSize( 100.0f, 100.0f );
411 actor.SetName("TestImageFilenameActor");
412 actor.SetShaderEffect(effect);
413 Stage::GetCurrent().Add(actor);
415 application.SendNotification();
416 application.Render();
419 application.GetGlAbstraction().CheckUniformValue(
420 "uVec2", Vector2( 2.0f, 3.0f ) ) );
423 static void UtcDaliShaderEffectMethodSetUniformVector3()
425 TestApplication application;
427 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
428 DALI_TEST_CHECK( effect );
430 BitmapImage image = CreateBitmapImage();
432 effect.SetUniform( "uVec3", Vector3( 4.0f, 5.0f, 6.0f ) );
434 ImageActor actor = ImageActor::New( image );
435 actor.SetSize( 100.0f, 100.0f );
436 actor.SetName("TestImageFilenameActor");
437 actor.SetShaderEffect(effect);
438 Stage::GetCurrent().Add(actor);
440 application.SendNotification();
441 application.Render();
444 application.GetGlAbstraction().CheckUniformValue(
445 "uVec3", Vector3( 4.0f, 5.0f, 6.0f ) ) );
448 static void UtcDaliShaderEffectMethodSetUniformVector4()
450 TestApplication application;
452 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
453 DALI_TEST_CHECK( effect );
455 BitmapImage image = CreateBitmapImage();
457 effect.SetUniform( "uVec4", Vector4( 7.0f, 8.0f, 9.0f, 10.0f ) );
459 ImageActor actor = ImageActor::New( image );
460 actor.SetSize( 100.0f, 100.0f );
461 actor.SetName("TestImageFilenameActor");
462 actor.SetShaderEffect(effect);
463 Stage::GetCurrent().Add(actor);
465 application.SendNotification();
466 application.Render();
469 application.GetGlAbstraction().CheckUniformValue(
470 "uVec4", Vector4( 7.0f, 8.0f, 9.0f, 10.0f ) ) );
473 static void UtcDaliShaderEffectMethodSetUniformMatrix()
475 TestApplication application;
477 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
478 DALI_TEST_CHECK( effect );
480 BitmapImage image = CreateBitmapImage();
482 effect.SetUniform( "uModelView", Matrix::IDENTITY );
484 ImageActor actor = ImageActor::New( image );
485 actor.SetSize( 100.0f, 100.0f );
486 actor.SetName("TestImageFilenameActor");
487 actor.SetShaderEffect(effect);
488 Stage::GetCurrent().Add(actor);
490 application.SendNotification();
491 application.Render();
494 application.GetGlAbstraction().CheckUniformValue(
495 "uModelView", Matrix::IDENTITY ) );
498 static void UtcDaliShaderEffectMethodSetUniformMatrix3()
500 TestApplication application;
502 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
503 DALI_TEST_CHECK( effect );
505 BitmapImage image = CreateBitmapImage();
507 Matrix3 matIdentity(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
508 effect.SetUniform( "uMatrix3", matIdentity );
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);
516 application.SendNotification();
517 application.Render();
519 DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue("uMatrix3", matIdentity) );
522 static void UtcDaliShaderEffectMethodSetUniformViewport()
524 TestApplication application;
526 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
527 DALI_TEST_CHECK( effect );
529 BitmapImage image = CreateBitmapImage();
531 ImageActor actor = ImageActor::New( image );
532 actor.SetSize( 100.0f, 100.0f );
533 actor.SetName("TestImageFilenameActor");
534 actor.SetShaderEffect(effect);
535 Stage::GetCurrent().Add(actor);
537 effect.SetUniform( "uVec2", Vector2( 0.0f, 0.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
538 effect.SetUniform( "uVec2Dir", Vector2( 1.0f, 2.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_DIRECTION );
540 application.SendNotification();
541 application.Render();
543 const Vector2& stageSize(Stage::GetCurrent().GetSize());
546 application.GetGlAbstraction().CheckUniformValue(
547 "uVec2", Vector2( stageSize.x/2, -stageSize.y/2 ) ) );
549 application.GetGlAbstraction().CheckUniformValue(
550 "uVec2Dir", Vector2( -1.0f, 2.0f ) ) );
553 static void UtcDaliShaderEffectMethodSetEffectImage()
555 TestApplication application;
557 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
558 DALI_TEST_CHECK( effect );
560 BitmapImage image = CreateBitmapImage();
562 effect.SetEffectImage(image);
564 ImageActor actor = ImageActor::New( image );
565 actor.SetSize( 100.0f, 100.0f );
566 actor.SetName("TestImageFilenameActor");
567 actor.SetShaderEffect(effect);
568 Stage::GetCurrent().Add(actor);
570 application.SendNotification();
571 application.Render(16);
572 application.SendNotification();
573 application.Render(16);
574 application.SendNotification();
576 DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "sEffect", 1 ) );
579 static void UtcDaliShaderEffectMethodSetEffectImageAndDelete()
581 TestApplication application;
583 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
585 BitmapImage effectImage = CreateBitmapImage();
586 effect.SetEffectImage(effectImage);
588 Integration::ResourceId imageId = GetImplementation(effectImage).GetResourceId();
589 tet_printf("Effect Image id %d \n ",imageId);
592 ImageActor actor = ImageActor::New();
594 actor.SetShaderEffect(effect);
597 Stage::GetCurrent().Add(actor);
599 // do an update / render cycle
600 application.SendNotification();
601 application.Render(16);
602 application.SendNotification();
603 application.Render(16);
604 application.SendNotification();
605 application.Render(16);
607 printf("removing image actor from stage and Reseting handle\n");
608 Stage::GetCurrent().Remove(actor);
611 tet_printf("### Update & Render \n");
613 application.SendNotification();
614 application.Render(16);
616 tet_printf("#### Update Only \n");
618 tet_printf("effectImage.Reset \n");
620 // this releases the effect texture resource,
621 // Update will send a DispatchDiscardTexture message to render
623 application.SendNotification();
624 application.UpdateOnly(16);
626 tet_printf("#### Update Only \n");
628 // at this point shader is deleted, during clear discard queue
629 // and it sends a Shader:: DispatchRemoveObserver message to render thread
630 application.UpdateOnly(16);
632 tet_printf("#### Render Only \n");
633 // This is where it used to crash, there is a message in the queue to perform DispatchDiscardTexture
634 // which tries to call observer->TextureDiscarded, where observer == shader that was deleted
635 // in previous update.
636 application.RenderOnly();
639 // process the discard texture message
640 application.RenderOnly();
641 application.SendNotification();
642 application.Render(16);
644 tet_result(TET_PASS);
647 static void UtcDaliShaderEffectMethodApplyConstraint()
649 // Test whether Shader's uniform can be constrained to a stationary constraint.
650 TestApplication application;
652 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
653 DALI_TEST_CHECK( effect );
655 BitmapImage image = CreateBitmapImage();
657 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
659 ImageActor actor = ImageActor::New( image );
660 actor.SetSize( 100.0f, 100.0f );
661 actor.SetName("TestImageFilenameActor");
662 actor.SetShaderEffect(effect);
663 Stage::GetCurrent().Add(actor);
665 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
667 application.SendNotification();
668 application.Render();
670 // Test effects of SetUniform...
672 application.GetGlAbstraction().CheckUniformValue(
673 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
675 Constraint constraint = Constraint::New<Vector3>( uVecProperty,
676 TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
678 effect.ApplyConstraint(constraint);
680 application.SendNotification();
681 application.Render();
683 // Test effects of Constraint.
685 application.GetGlAbstraction().CheckUniformValue(
686 "uVec3", Vector3( 4.0f, 9.0f, 16.0f ) ) );
690 static void UtcDaliShaderEffectMethodApplyConstraintFromActor()
692 // Test whether Shader's uniform can be constrained to Actor's position.
693 TestApplication application;
695 const Vector3 targetPosition = Vector3( 100.0f, 70.0f, 20.0f);
697 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
698 DALI_TEST_CHECK( effect );
700 BitmapImage image = CreateBitmapImage();
702 effect.SetUniform( "uVec3", Vector3( 50.0f, 25.0f, 0.0f ) );
704 ImageActor actor = ImageActor::New( image );
705 actor.SetPosition(targetPosition);
706 actor.SetSize( 100.0f, 100.0f );
707 actor.SetName("TestImageFilenameActor");
708 actor.SetShaderEffect(effect);
709 Stage::GetCurrent().Add(actor);
711 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
713 Constraint constraint = Constraint::New<Vector3>( uVecProperty,
714 Source(actor, Actor::POSITION),
715 TestConstraintFromPositionToVector3() );
717 effect.ApplyConstraint(constraint);
719 application.SendNotification();
720 application.Render();
722 // Test effects of Constraint.
724 application.GetGlAbstraction().CheckUniformValue(
725 "uVec3", targetPosition ) );
728 static void UtcDaliShaderEffectMethodApplyConstraintFromActor2()
730 // Test whether Shader's uniform can be constrained to Actor's position.
731 // While Actor's position is constrained to another point * 2.0f
732 TestApplication application;
734 const Vector3 targetPosition = Vector3( 25.0f, 36.0f, 49.0f );
736 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
737 DALI_TEST_CHECK( effect );
739 BitmapImage image = CreateBitmapImage();
741 effect.SetUniform( "uVec3", Vector3( 50.0f, 25.0f, 0.0f ) );
743 ImageActor actor = ImageActor::New( image );
744 actor.SetPosition(Vector3( 100.0f, 70.0f, 20.0f));
745 actor.SetSize( 100.0f, 100.0f );
746 actor.SetName("TestImageFilenameActor");
747 actor.SetShaderEffect(effect);
748 Stage::GetCurrent().Add(actor);
750 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
752 Constraint shaderConstraint = Constraint::New<Vector3>( uVecProperty,
753 Source(actor, Actor::POSITION),
754 TestConstraintFromPositionToVector3() );
756 effect.ApplyConstraint(shaderConstraint);
758 Constraint actorConstraint = Constraint::New<Vector3>( Actor::POSITION,
759 TestConstraintToVector3Double(targetPosition) );
761 actor.ApplyConstraint(actorConstraint);
763 application.SendNotification();
764 application.Render();
766 // Test effects of Constraint.
768 application.GetGlAbstraction().CheckUniformValue(
769 "uVec3", targetPosition * 2.0f ) );
772 static void UtcDaliShaderEffectMethodApplyConstraintCallback()
774 // Test whether Shader's uniform can be constrained to a stationary constraint.
775 TestApplication application;
777 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
778 DALI_TEST_CHECK( effect );
780 BitmapImage image = CreateBitmapImage();
782 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
784 ImageActor actor = ImageActor::New( image );
785 actor.SetSize( 100.0f, 100.0f );
786 actor.SetName("TestImageFilenameActor");
787 actor.SetShaderEffect(effect);
788 Stage::GetCurrent().Add(actor);
790 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
792 application.SendNotification();
793 application.Render();
795 // Test effects of SetUniform...
797 application.GetGlAbstraction().CheckUniformValue(
798 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
800 Constraint constraint = Constraint::New<Vector3>( uVecProperty,
801 TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
803 constraint.SetApplyTime( 10.0f );
805 bool constraintCheck( false );
806 ConstraintAppliedCheck appliedCheck( constraintCheck );
808 // We should receive the "Applied" signal after 10 seconds
809 ActiveConstraint active = effect.ApplyConstraint(constraint);
810 active.AppliedSignal().Connect( &application, appliedCheck );
812 application.SendNotification();
813 application.Render(static_cast<unsigned int>(1000.0f)); // 1 elapsed second
815 // Check signal has not fired
816 application.SendNotification();
817 appliedCheck.CheckSignalNotReceived();
819 application.Render(static_cast<unsigned int>(4000.0f)); // 5 elapsed seconds
821 // Check signal has not fired
822 application.SendNotification();
823 appliedCheck.CheckSignalNotReceived();
825 application.Render(static_cast<unsigned int>(5000.0f - 1.0f)); // <10 elapsed seconds
827 // Check signal has not fired
828 application.SendNotification();
829 appliedCheck.CheckSignalNotReceived();
831 application.Render(static_cast<unsigned int>(2.0f)); // >10 elapsed seconds
833 // Signal should have fired
834 application.SendNotification();
835 appliedCheck.CheckSignalReceived();
837 // Test effects of Constraint.
839 application.GetGlAbstraction().CheckUniformValue(
840 "uVec3", Vector3( 4.0f, 9.0f, 16.0f ) ) );
843 static void UtcDaliShaderEffectMethodRemoveConstraints()
845 // Test if constrains can be removed before they are ever applyed.
846 TestApplication application;
848 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
849 DALI_TEST_CHECK( effect );
851 BitmapImage image = CreateBitmapImage();
853 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
855 ImageActor actor = ImageActor::New( image );
856 actor.SetSize( 100.0f, 100.0f );
857 actor.SetName("TestImageFilenameActor");
858 actor.SetShaderEffect(effect);
859 Stage::GetCurrent().Add(actor);
861 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
863 application.SendNotification();
864 application.Render();
866 // Test effects of SetUniform...
868 application.GetGlAbstraction().CheckUniformValue(
869 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
871 Constraint constraint = Constraint::New<Vector3>( uVecProperty,
872 TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
874 effect.ApplyConstraint(constraint);
876 // Remove the constraints
877 effect.RemoveConstraints();
879 application.SendNotification();
880 application.Render();
882 // Test effects of Constraint.
884 application.GetGlAbstraction().CheckUniformValue(
885 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
888 static void UtcDaliShaderEffectMethodRemoveConstraints2()
890 // Test whether Shader's uniform constrains can be removed after they are applyed.
891 TestApplication application;
893 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
894 DALI_TEST_CHECK( effect );
896 BitmapImage image = CreateBitmapImage();
898 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
900 ImageActor actor = ImageActor::New( image );
901 actor.SetSize( 100.0f, 100.0f );
902 actor.SetName("TestImageFilenameActor");
903 actor.SetShaderEffect(effect);
904 Stage::GetCurrent().Add(actor);
906 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
908 application.SendNotification();
909 application.Render();
911 // Test effects of SetUniform...
913 application.GetGlAbstraction().CheckUniformValue(
914 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
916 Constraint constraint = Constraint::New<Vector3>( uVecProperty,
917 TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
919 effect.ApplyConstraint(constraint);
921 application.SendNotification();
922 application.Render();
924 // Reset the value and remove the constraints
925 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
926 effect.RemoveConstraints();
928 application.SendNotification();
929 application.Render();
931 // Test effects of Constraint.
933 application.GetGlAbstraction().CheckUniformValue(
934 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
938 class TestExtension : public ShaderEffect::Extension
941 TestExtension( bool& deleted )
958 static void UtcDaliShaderEffectMethodCreateExtension()
960 // Test creation of a shader extension
961 TestApplication aplication;
965 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
966 DALI_TEST_CHECK( effect );
968 TestExtension* extension = new TestExtension ( deleted );
970 effect.AttachExtension( extension );
972 DALI_TEST_CHECK( static_cast<TestExtension&>(effect.GetExtension()).IsAlive() );
975 DALI_TEST_CHECK( deleted );
978 static void UtcDaliShaderEffectMethodCreateExtension2()
980 // Test creation of a shader extension
983 TestApplication application;
985 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
986 DALI_TEST_CHECK( effect );
988 BitmapImage image = CreateBitmapImage();
990 effect.SetUniform( "uFloat", 1.0f );
992 ImageActor actor = ImageActor::New( image );
993 actor.SetSize( 100.0f, 100.0f );
994 actor.SetName("TestImageFilenameActor");
995 actor.SetShaderEffect(effect);
996 Stage::GetCurrent().Add(actor);
998 application.SendNotification();
999 application.Render();
1001 TestExtension* extension = new TestExtension ( deleted );
1003 effect.AttachExtension( extension );
1005 const ShaderEffect& constEffect(effect);
1006 const TestExtension& ext( static_cast<const TestExtension&>(constEffect.GetExtension()) );
1008 DALI_TEST_CHECK( ext.IsAlive() );
1011 DALI_TEST_CHECK( deleted );
1014 static void UtcDaliShaderEffectMethodNoExtension()
1016 TestApplication application;
1018 ShaderEffect effect;
1022 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
1023 DALI_TEST_CHECK( effect );
1025 // Don't attach extension
1026 ShaderEffect::Extension& extension = effect.GetExtension();
1029 DALI_TEST_CHECK( false );
1031 catch (Dali::DaliException& e)
1033 // Tests that a negative test of an assertion succeeds
1034 tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
1035 DALI_TEST_CHECK( !effect );
1039 static void UtcDaliShaderEffectFromProperties01()
1041 TestApplication application;
1042 tet_infoline("UtcDaliShaderEffectFromProperties01()");
1044 std::string fragmentShaderPrefix = "#define TEST_FS 1\n#extension GL_OES_standard_derivatives : enable";
1045 std::string vertexShaderPrefix = "#define TEST_VS 1";
1047 // Call render to compile default shaders.
1048 application.SendNotification();
1049 application.Render();
1050 application.Render();
1051 application.Render();
1053 GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
1055 // create from type registry
1057 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
1058 DALI_TEST_CHECK( typeInfo );
1059 ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
1060 DALI_TEST_CHECK( effect );
1062 Property::Value programMap = Property::Value(Property::MAP);
1064 programMap.SetValue("vertex", std::string(VertexSource));
1065 programMap.SetValue("fragment", std::string(FragmentSource));
1067 programMap.SetValue("vertex-prefix", std::string(fragmentShaderPrefix));
1068 programMap.SetValue("fragment-prefix", std::string(vertexShaderPrefix));
1070 programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
1072 effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
1074 Property::Value imageMap = Property::Value(Property::MAP);
1075 imageMap.SetValue("filename", Property::Value(TestImageFilename));
1077 effect.SetProperty(effect.GetPropertyIndex("image"), imageMap);
1079 BitmapImage image = CreateBitmapImage();
1080 ImageActor actor = ImageActor::New( image );
1081 actor.SetSize( 100.0f, 100.0f );
1082 actor.SetName("TestImageFilenameActor");
1083 actor.SetShaderEffect(effect);
1084 Stage::GetCurrent().Add(actor);
1086 application.SendNotification();
1087 application.Render();
1088 GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
1089 bool testResult = false;
1091 // we should have compiled 4 shaders.
1092 DALI_TEST_CHECK(lastShaderCompiledAfter - lastShaderCompiledBefore == 4);
1093 if (lastShaderCompiledAfter - lastShaderCompiledBefore == 4)
1095 char testVertexSourceResult[GETSOURCE_BUFFER_SIZE];
1096 char testFragmentSourceResult[GETSOURCE_BUFFER_SIZE];
1098 // we are interested in the first two.
1099 GLuint vertexShaderId = lastShaderCompiledBefore + 1;
1100 GLuint fragmentShaderId = lastShaderCompiledBefore + 2;
1102 GLsizei lengthVertexResult;
1103 GLsizei lengthFragmentResult;
1105 application.GetGlAbstraction().GetShaderSource(vertexShaderId, GETSOURCE_BUFFER_SIZE, &lengthVertexResult, testVertexSourceResult);
1106 application.GetGlAbstraction().GetShaderSource(fragmentShaderId, GETSOURCE_BUFFER_SIZE, &lengthFragmentResult, testFragmentSourceResult);
1108 int vertexShaderHasPrefix = strncmp(testVertexSourceResult, "#define ", strlen("#define "));
1109 int fragmentShaderHasPrefix = strncmp(testFragmentSourceResult, "#define ", strlen("#define "));
1110 testResult = (vertexShaderHasPrefix == 0) && (fragmentShaderHasPrefix == 0);
1112 DALI_TEST_CHECK(testResult);
1116 static void UtcDaliShaderEffectFromProperties02()
1120 TestApplication application;
1121 tet_infoline("UtcDaliShaderEffectFromProperties02()");
1123 // Call render to compile default shaders.
1124 application.SendNotification();
1125 application.Render();
1126 application.Render();
1127 application.Render();
1129 // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
1130 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
1131 DALI_TEST_CHECK( typeInfo );
1132 ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
1133 DALI_TEST_CHECK( effect );
1135 Property::Value programMap = Property::Value(Property::MAP);
1137 programMap.SetValue("vertex", std::string(VertexSource));
1138 programMap.SetValue("fragment", std::string(FragmentSource));
1140 // programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
1141 // dont set by value
1142 programMap.SetValue("geometry-type", GeometryType( GEOMETRY_TYPE_IMAGE ));
1144 effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
1146 tet_result( TET_FAIL );
1148 catch(Dali::DaliException& e)
1150 tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
1154 static void UtcDaliShaderEffectFromProperties03()
1158 TestApplication application;
1159 tet_infoline("UtcDaliShaderEffectFromProperties03()");
1161 // Call render to compile default shaders.
1162 application.SendNotification();
1163 application.Render();
1164 application.Render();
1165 application.Render();
1167 // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
1168 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
1169 DALI_TEST_CHECK( typeInfo );
1170 ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
1171 DALI_TEST_CHECK( effect );
1174 effect.SetProperty( effect.GetPropertyIndex("geometry-hints"), "HINT_2" );
1176 tet_result( TET_FAIL );
1178 catch(Dali::DaliException& e)
1180 tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
1184 void UtcDaliShaderEffectPropertyIndices()
1186 TestApplication application;
1187 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
1189 Property::IndexContainer indices;
1190 effect.GetPropertyIndices( indices );
1191 DALI_TEST_CHECK( ! indices.empty() );
1192 DALI_TEST_EQUALS( indices.size(), effect.GetPropertyCount(), TEST_LOCATION );