2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
26 void utc_dali_shader_effect_startup(void)
28 test_return_value = TET_UNDEF;
31 void utc_dali_shader_effect_cleanup(void)
33 test_return_value = TET_PASS;
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";
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";
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";
51 const int GETSOURCE_BUFFER_SIZE = 0x10000;
54 struct TestConstraintToVector3
56 TestConstraintToVector3(Vector3 target)
61 Vector3 operator()(const Vector3& current)
69 struct TestConstraintFromPositionToVector3
71 TestConstraintFromPositionToVector3()
75 Vector3 operator()(const Vector3& current, const PropertyInput& position)
78 return position.GetVector3();
82 struct TestConstraintToVector3Double
84 TestConstraintToVector3Double(Vector3 target)
89 Vector3 operator()(const Vector3& current)
91 return mTarget * 2.0f;
97 class ShaderEffectExtension : public ShaderEffect::Extension {};
100 class TestExtension : public ShaderEffect::Extension
103 TestExtension( bool& deleted )
120 static const char* TestImageFilename = "icon_wrt.png";
122 Integration::Bitmap* CreateBitmap( unsigned int imageHeight, unsigned int imageWidth, unsigned int initialColor )
124 Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::RETAIN );
125 Integration::PixelBuffer* pixbuffer = bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, imageWidth,imageHeight,imageWidth,imageHeight );
126 unsigned int bytesPerPixel = GetBytesPerPixel( Pixel::RGBA8888 );
128 memset( pixbuffer, initialColor , imageHeight*imageWidth*bytesPerPixel);
136 int UtcDaliShaderEffectMethodNew01(void)
138 TestApplication application;
140 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
141 DALI_TEST_CHECK(effect);
145 int UtcDaliShaderEffectMethodNew02(void)
147 TestApplication application;
153 // New must be called to create a ShaderEffect or it wont be valid.
154 effect.SetUniform( "uUniform", 0 );
155 DALI_TEST_CHECK( false );
157 catch (Dali::DaliException& e)
159 // Tests that a negative test of an assertion succeeds
160 DALI_TEST_PRINT_ASSERT( e );
161 DALI_TEST_CHECK( !effect );
166 int UtcDaliShaderEffectMethodNew03(void)
168 TestApplication application;
170 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource, VertexSource, FragmentSource, ShaderEffect::HINT_NONE );
171 DALI_TEST_CHECK(effect);
175 int UtcDaliShaderEffectMethodNew04(void)
177 TestApplication application;
178 tet_infoline("Testing prefixed version of Dali::ShaderEffect::New()");
180 std::string fragmentShaderPrefix = "#define TEST_FS 1\n#extension GL_OES_standard_derivatives : enable";
181 std::string vertexShaderPrefix = "#define TEST_VS 1";
185 // Call render to compile default shaders.
186 application.SendNotification();
187 application.Render();
188 application.Render();
189 application.Render();
191 GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
192 ShaderEffect effect = ShaderEffect::NewWithPrefix( vertexShaderPrefix, VertexSource,
193 fragmentShaderPrefix, FragmentSourceUsingExtensions,
194 GEOMETRY_TYPE_IMAGE, ShaderEffect::HINT_NONE );
196 BufferImage image = CreateBufferImage();
197 ImageActor actor = ImageActor::New( image );
198 actor.SetSize( 100.0f, 100.0f );
199 actor.SetName("TestImageFilenameActor");
200 actor.SetShaderEffect(effect);
201 Stage::GetCurrent().Add(actor);
203 application.SendNotification();
204 application.Render();
205 GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
206 bool testResult = false;
208 // we should have compiled 2 shaders.
209 DALI_TEST_EQUALS( lastShaderCompiledAfter, lastShaderCompiledBefore + 2, TEST_LOCATION );
211 char testVertexSourceResult[GETSOURCE_BUFFER_SIZE];
212 char testFragmentSourceResult[GETSOURCE_BUFFER_SIZE];
214 // we are interested in the first two.
215 GLuint vertexShaderId = lastShaderCompiledBefore + 1;
216 GLuint fragmentShaderId = lastShaderCompiledBefore + 2;
218 GLsizei lengthVertexResult;
219 GLsizei lengthFragmentResult;
221 application.GetGlAbstraction().GetShaderSource(vertexShaderId, GETSOURCE_BUFFER_SIZE, &lengthVertexResult, testVertexSourceResult);
222 application.GetGlAbstraction().GetShaderSource(fragmentShaderId, GETSOURCE_BUFFER_SIZE, &lengthFragmentResult, testFragmentSourceResult);
224 int vertexShaderHasPrefix = strncmp(testVertexSourceResult, "#define ", strlen("#define "));
225 int fragmentShaderHasPrefix = strncmp(testFragmentSourceResult, "#define ", strlen("#define "));
226 testResult = (vertexShaderHasPrefix == 0) && (fragmentShaderHasPrefix == 0);
228 DALI_TEST_CHECK(testResult);
230 catch(Dali::DaliException& e)
232 DALI_TEST_PRINT_ASSERT( e );
233 tet_result( TET_FAIL );
238 int UtcDaliShaderEffectMethodNew05(void)
240 TestApplication application;
242 // heap constructor / destructor
243 DefaultFunctionCoverage<ShaderEffect> shaderEffect;
244 DefaultFunctionCoverage<ShaderEffectExtension> shaderEffectExtension;
249 int UtcDaliShaderEffectMethodNew06(void)
251 TestApplication application;
252 tet_infoline("Testing Dali::ShaderEffect::New() with shader sources for different geometry types");
254 ShaderEffect effect = ShaderEffect::New( "imageVertexShader", "imageFragmentShader",
255 "textVertexShader", "textFragmentShader",
256 "texturedMeshVertexShader", "texturedMeshFragmentShader",
257 "meshVertexShader", "meshFragmentShader",
258 ShaderEffect::HINT_NONE );
259 DALI_TEST_CHECK(effect);
264 int UtcDaliShaderEffectMethodDownCast(void)
266 TestApplication application;
267 tet_infoline("Testing Dali::ShaderEffect::DownCast()");
269 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
271 BaseHandle object(effect);
273 ShaderEffect effect2 = ShaderEffect::DownCast(object);
274 DALI_TEST_CHECK(effect2);
276 ShaderEffect effect3 = DownCast< ShaderEffect >(object);
277 DALI_TEST_CHECK(effect3);
279 BaseHandle unInitializedObject;
280 ShaderEffect effect4 = ShaderEffect::DownCast(unInitializedObject);
281 DALI_TEST_CHECK(!effect4);
283 ShaderEffect effect5 = DownCast< ShaderEffect >(unInitializedObject);
284 DALI_TEST_CHECK(!effect5);
288 int UtcDaliShaderEffectMethodDelete01(void)
290 TestApplication application;
292 // get the default shaders built, this is not required but makes it
293 // easier to debug the TET case and isolate the custom shader compilation.
294 application.SendNotification();
295 application.Render();
297 GLuint beforeShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
299 // create a new shader effect
300 // the vertex and fragment shader will be cached in the ShaderFactory
301 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
303 // destroy the shader effect
306 // Create the same shader effect again, this should now use the cached version
307 // held in the shader factory
308 effect= ShaderEffect::New( VertexSource, FragmentSource );
310 // Compile the shader effect
311 application.SendNotification();
312 application.Render();
314 GLuint lastShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
315 // no shaders were compiled as they are now compiled on demand and this shader was not used
316 DALI_TEST_EQUALS( beforeShaderCompiled, lastShaderCompiled, TEST_LOCATION );
321 int UtcDaliShaderEffectMethodSetUniformFloat(void)
323 TestApplication application;
325 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
326 DALI_TEST_CHECK( effect );
328 BufferImage image = CreateBufferImage();
330 effect.SetUniform( "uFloat", 1.0f );
332 ImageActor actor = ImageActor::New( image );
333 actor.SetSize( 100.0f, 100.0f );
334 actor.SetName("TestImageFilenameActor");
335 actor.SetShaderEffect(effect);
336 Stage::GetCurrent().Add(actor);
338 application.SendNotification();
339 application.Render();
342 application.GetGlAbstraction().CheckUniformValue(
347 int UtcDaliShaderEffectMethodSetUniformVector2(void)
349 TestApplication application;
351 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
352 DALI_TEST_CHECK( effect );
354 BufferImage image = CreateBufferImage();
356 effect.SetUniform( "uVec2", Vector2( 2.0f, 3.0f ) );
358 ImageActor actor = ImageActor::New( image );
359 actor.SetSize( 100.0f, 100.0f );
360 actor.SetName("TestImageFilenameActor");
361 actor.SetShaderEffect(effect);
362 Stage::GetCurrent().Add(actor);
364 application.SendNotification();
365 application.Render();
368 application.GetGlAbstraction().CheckUniformValue(
369 "uVec2", Vector2( 2.0f, 3.0f ) ) );
373 int UtcDaliShaderEffectMethodSetUniformVector3(void)
375 TestApplication application;
377 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
378 DALI_TEST_CHECK( effect );
380 BufferImage image = CreateBufferImage();
382 effect.SetUniform( "uVec3", Vector3( 4.0f, 5.0f, 6.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(
395 "uVec3", Vector3( 4.0f, 5.0f, 6.0f ) ) );
399 int UtcDaliShaderEffectMethodSetUniformVector4(void)
401 TestApplication application;
403 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
404 DALI_TEST_CHECK( effect );
406 BufferImage image = CreateBufferImage();
408 effect.SetUniform( "uVec4", Vector4( 7.0f, 8.0f, 9.0f, 10.0f ) );
410 ImageActor actor = ImageActor::New( image );
411 actor.SetSize( 100.0f, 100.0f );
412 actor.SetName("TestImageFilenameActor");
413 actor.SetShaderEffect(effect);
414 Stage::GetCurrent().Add(actor);
416 application.SendNotification();
417 application.Render();
420 application.GetGlAbstraction().CheckUniformValue(
421 "uVec4", Vector4( 7.0f, 8.0f, 9.0f, 10.0f ) ) );
425 int UtcDaliShaderEffectMethodSetUniformMatrix(void)
427 TestApplication application;
429 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
430 DALI_TEST_CHECK( effect );
432 BufferImage image = CreateBufferImage();
434 effect.SetUniform( "uModelView", Matrix::IDENTITY );
436 ImageActor actor = ImageActor::New( image );
437 actor.SetSize( 100.0f, 100.0f );
438 actor.SetName("TestImageFilenameActor");
439 actor.SetShaderEffect(effect);
440 Stage::GetCurrent().Add(actor);
442 application.SendNotification();
443 application.Render();
446 application.GetGlAbstraction().CheckUniformValue(
447 "uModelView", Matrix::IDENTITY ) );
451 int UtcDaliShaderEffectMethodSetUniformMatrix3(void)
453 TestApplication application;
455 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
456 DALI_TEST_CHECK( effect );
458 BufferImage image = CreateBufferImage();
460 Matrix3 matIdentity(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
461 effect.SetUniform( "uMatrix3", matIdentity );
463 ImageActor actor = ImageActor::New( image );
464 actor.SetSize( 100.0f, 100.0f );
465 actor.SetName("TestImageFilenameActor");
466 actor.SetShaderEffect(effect);
467 Stage::GetCurrent().Add(actor);
469 application.SendNotification();
470 application.Render();
472 DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue("uMatrix3", matIdentity) );
476 int UtcDaliShaderEffectMethodSetUniformViewport(void)
478 TestApplication application;
480 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
481 DALI_TEST_CHECK( effect );
483 BufferImage image = CreateBufferImage();
485 ImageActor actor = ImageActor::New( image );
486 actor.SetSize( 100.0f, 100.0f );
487 actor.SetName("TestImageFilenameActor");
488 actor.SetShaderEffect(effect);
489 Stage::GetCurrent().Add(actor);
491 effect.SetUniform( "uVec2", Vector2( 0.0f, 0.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
492 effect.SetUniform( "uVec2Dir", Vector2( 1.0f, 2.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_DIRECTION );
494 application.SendNotification();
495 application.Render();
497 const Vector2& stageSize(Stage::GetCurrent().GetSize());
499 DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "uVec2", Vector2( stageSize.x/2, -stageSize.y/2 ) ) );
501 DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "uVec2Dir", Vector2( -1.0f, 2.0f ) ) );
503 // change coordinate types
504 effect.SetUniform( "uVec2", Vector2( 0.1f, 0.2f ), ShaderEffect::COORDINATE_TYPE_DEFAULT );
505 effect.SetUniform( "uVec2Dir", Vector2( 1.0f, 2.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
506 actor.SetPixelArea( ImageActor::PixelArea( 0, 0, 10, 10 ) );
508 application.SendNotification();
509 application.Render();
512 application.GetGlAbstraction().GetUniformValue( "uVec2", outValue );
513 DALI_TEST_EQUALS( outValue, Vector2( 0.1f, 0.2f ), TEST_LOCATION );
515 application.GetGlAbstraction().GetUniformValue( "uVec2Dir", outValue );
516 DALI_TEST_EQUALS( outValue, Vector2( stageSize.x *.5f - 1.f, -stageSize.y * .5f + 2.f), TEST_LOCATION );
521 int UtcDaliShaderEffectMethodSetEffectImage(void)
523 TestApplication application;
525 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
526 DALI_TEST_CHECK( effect );
528 BufferImage image = CreateBufferImage();
530 effect.SetEffectImage(image);
532 ImageActor actor = ImageActor::New( image );
533 actor.SetSize( 100.0f, 100.0f );
534 actor.SetName("TestImageFilenameActor");
535 actor.SetShaderEffect(effect);
536 Stage::GetCurrent().Add(actor);
538 application.SendNotification();
539 application.Render(16);
540 application.SendNotification();
541 application.Render(16);
542 application.SendNotification();
544 GLuint programId, uniformId;
545 bool uniformWasSet = application.GetGlAbstraction().GetUniformIds( "sEffect", programId, uniformId );
546 // we dont care about the value of the sampler uniform as thats internal to DALi core and subject to change
547 DALI_TEST_CHECK( uniformWasSet );
551 int UtcDaliShaderEffectMethodSetEffectImageAndDelete(void)
553 TestApplication application;
555 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
557 BufferImage effectImage = CreateBufferImage();
558 effect.SetEffectImage(effectImage);
560 ImageActor actor = ImageActor::New();
562 actor.SetShaderEffect(effect);
565 Stage::GetCurrent().Add(actor);
567 // do an update / render cycle
568 application.SendNotification();
569 application.Render(16);
570 application.SendNotification();
571 application.Render(16);
572 application.SendNotification();
573 application.Render(16);
575 printf("removing image actor from stage and Reseting handle\n");
576 Stage::GetCurrent().Remove(actor);
579 tet_printf("### Update & Render \n");
581 application.SendNotification();
582 application.Render(16);
584 tet_printf("#### Update Only \n");
586 tet_printf("effectImage.Reset \n");
588 // this releases the effect texture resource,
589 // Update will send a DispatchDiscardTexture message to render
591 application.SendNotification();
592 application.UpdateOnly(16);
594 tet_printf("#### Update Only \n");
596 // at this point shader is deleted, during clear discard queue
597 // and it sends a Shader:: DispatchRemoveObserver message to render thread
598 application.UpdateOnly(16);
600 tet_printf("#### Render Only \n");
601 // This is where it used to crash, there is a message in the queue to perform DispatchDiscardTexture
602 // which tries to call observer->TextureDiscarded, where observer == shader that was deleted
603 // in previous update.
604 application.RenderOnly();
607 // process the discard texture message
608 application.RenderOnly();
609 application.SendNotification();
610 application.Render(16);
612 tet_result(TET_PASS);
617 int UtcDaliShaderEffectMethodApplyConstraint(void)
619 // Test whether Shader's uniform can be constrained to a stationary constraint.
620 TestApplication application;
622 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
623 DALI_TEST_CHECK( effect );
625 BufferImage image = CreateBufferImage();
627 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
629 ImageActor actor = ImageActor::New( image );
630 actor.SetSize( 100.0f, 100.0f );
631 actor.SetName("TestImageFilenameActor");
632 actor.SetShaderEffect(effect);
633 Stage::GetCurrent().Add(actor);
635 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
637 application.SendNotification();
638 application.Render();
640 // Test effects of SetUniform...
642 application.GetGlAbstraction().CheckUniformValue(
643 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
645 Constraint constraint = Constraint::New<Vector3>( uVecProperty,
646 TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
648 effect.ApplyConstraint(constraint);
650 application.SendNotification();
651 application.Render();
653 // Test effects of Constraint.
655 application.GetGlAbstraction().CheckUniformValue(
656 "uVec3", Vector3( 4.0f, 9.0f, 16.0f ) ) );
661 int UtcDaliShaderEffectMethodApplyConstraintFromActor(void)
663 // Test whether Shader's uniform can be constrained to Actor's position.
664 TestApplication application;
666 const Vector3 targetPosition = Vector3( 100.0f, 70.0f, 20.0f);
668 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
669 DALI_TEST_CHECK( effect );
671 BufferImage image = CreateBufferImage();
673 effect.SetUniform( "uVec3", Vector3( 50.0f, 25.0f, 0.0f ) );
675 ImageActor actor = ImageActor::New( image );
676 actor.SetPosition(targetPosition);
677 actor.SetSize( 100.0f, 100.0f );
678 actor.SetName("TestImageFilenameActor");
679 actor.SetShaderEffect(effect);
680 Stage::GetCurrent().Add(actor);
682 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
684 Constraint constraint = Constraint::New<Vector3>( uVecProperty,
685 Source(actor, Actor::Property::POSITION),
686 TestConstraintFromPositionToVector3() );
688 effect.ApplyConstraint(constraint);
690 application.SendNotification();
691 application.Render();
693 // Test effects of Constraint.
695 application.GetGlAbstraction().CheckUniformValue(
696 "uVec3", targetPosition ) );
700 int UtcDaliShaderEffectMethodApplyConstraintFromActor2(void)
702 // Test whether Shader's uniform can be constrained to Actor's position.
703 // While Actor's position is constrained to another point * 2.0f
704 TestApplication application;
706 const Vector3 targetPosition = Vector3( 25.0f, 36.0f, 49.0f );
708 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
709 DALI_TEST_CHECK( effect );
711 BufferImage image = CreateBufferImage();
713 effect.SetUniform( "uVec3", Vector3( 50.0f, 25.0f, 0.0f ) );
715 ImageActor actor = ImageActor::New( image );
716 actor.SetPosition(Vector3( 100.0f, 70.0f, 20.0f));
717 actor.SetSize( 100.0f, 100.0f );
718 actor.SetName("TestImageFilenameActor");
719 actor.SetShaderEffect(effect);
720 Stage::GetCurrent().Add(actor);
722 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
724 Constraint shaderConstraint = Constraint::New<Vector3>( uVecProperty,
725 Source(actor, Actor::Property::POSITION),
726 TestConstraintFromPositionToVector3() );
728 effect.ApplyConstraint(shaderConstraint);
730 Constraint actorConstraint = Constraint::New<Vector3>( Actor::Property::POSITION,
731 TestConstraintToVector3Double(targetPosition) );
733 actor.ApplyConstraint(actorConstraint);
735 application.SendNotification();
736 application.Render();
738 // Test effects of Constraint.
740 application.GetGlAbstraction().CheckUniformValue(
741 "uVec3", targetPosition * 2.0f ) );
745 int UtcDaliShaderEffectMethodApplyConstraintCallback(void)
747 // Test whether Shader's uniform can be constrained to a stationary constraint.
748 TestApplication application;
750 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
751 DALI_TEST_CHECK( effect );
753 BufferImage image = CreateBufferImage();
755 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
757 ImageActor actor = ImageActor::New( image );
758 actor.SetSize( 100.0f, 100.0f );
759 actor.SetName("TestImageFilenameActor");
760 actor.SetShaderEffect(effect);
761 Stage::GetCurrent().Add(actor);
763 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
765 application.SendNotification();
766 application.Render();
768 // Test effects of SetUniform...
770 application.GetGlAbstraction().CheckUniformValue(
771 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
773 Constraint constraint = Constraint::New<Vector3>( uVecProperty,
774 TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
776 constraint.SetApplyTime( 10.0f );
778 bool constraintCheck( false );
779 ConstraintAppliedCheck appliedCheck( constraintCheck );
781 // We should receive the "Applied" signal after 10 seconds
782 ActiveConstraint active = effect.ApplyConstraint(constraint);
783 active.AppliedSignal().Connect( &application, appliedCheck );
785 application.SendNotification();
786 application.Render(static_cast<unsigned int>(1000.0f)); // 1 elapsed second
788 // Check signal has not fired
789 application.SendNotification();
790 appliedCheck.CheckSignalNotReceived();
792 application.Render(static_cast<unsigned int>(4000.0f)); // 5 elapsed seconds
794 // Check signal has not fired
795 application.SendNotification();
796 appliedCheck.CheckSignalNotReceived();
798 application.Render(static_cast<unsigned int>(5000.0f - 1.0f)); // <10 elapsed seconds
800 // Check signal has not fired
801 application.SendNotification();
802 appliedCheck.CheckSignalNotReceived();
804 application.Render(static_cast<unsigned int>(2.0f)); // >10 elapsed seconds
806 // Signal should have fired
807 application.SendNotification();
808 appliedCheck.CheckSignalReceived();
810 // Test effects of Constraint.
812 application.GetGlAbstraction().CheckUniformValue(
813 "uVec3", Vector3( 4.0f, 9.0f, 16.0f ) ) );
817 int UtcDaliShaderEffectMethodRemoveConstraints(void)
819 // Test if constrains can be removed before they are ever applyed.
820 TestApplication application;
822 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
823 DALI_TEST_CHECK( effect );
825 BufferImage image = CreateBufferImage();
827 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
829 ImageActor actor = ImageActor::New( image );
830 actor.SetSize( 100.0f, 100.0f );
831 actor.SetName("TestImageFilenameActor");
832 actor.SetShaderEffect(effect);
833 Stage::GetCurrent().Add(actor);
835 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
837 application.SendNotification();
838 application.Render();
840 // Test effects of SetUniform...
842 application.GetGlAbstraction().CheckUniformValue(
843 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
845 Constraint constraint = Constraint::New<Vector3>( uVecProperty,
846 TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
848 effect.ApplyConstraint(constraint);
850 // Remove the constraints
851 effect.RemoveConstraints();
853 application.SendNotification();
854 application.Render();
856 // Test effects of Constraint.
858 application.GetGlAbstraction().CheckUniformValue(
859 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
863 int UtcDaliShaderEffectMethodRemoveConstraints2(void)
865 // Test whether Shader's uniform constrains can be removed after they are applyed.
866 TestApplication application;
868 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
869 DALI_TEST_CHECK( effect );
871 BufferImage image = CreateBufferImage();
873 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
875 ImageActor actor = ImageActor::New( image );
876 actor.SetSize( 100.0f, 100.0f );
877 actor.SetName("TestImageFilenameActor");
878 actor.SetShaderEffect(effect);
879 Stage::GetCurrent().Add(actor);
881 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
883 application.SendNotification();
884 application.Render();
886 // Test effects of SetUniform...
888 application.GetGlAbstraction().CheckUniformValue(
889 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
891 Constraint constraint = Constraint::New<Vector3>( uVecProperty,
892 TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
894 effect.ApplyConstraint(constraint);
896 application.SendNotification();
897 application.Render();
899 // Reset the value and remove the constraints
900 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
901 effect.RemoveConstraints();
903 application.SendNotification();
904 application.Render();
906 // Test effects of Constraint.
908 application.GetGlAbstraction().CheckUniformValue(
909 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
913 int UtcDaliShaderEffectMethodCreateExtension(void)
915 // Test creation of a shader extension
916 TestApplication aplication;
920 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
921 DALI_TEST_CHECK( effect );
923 TestExtension* extension = new TestExtension ( deleted );
925 effect.AttachExtension( extension );
927 DALI_TEST_CHECK( static_cast<TestExtension&>(effect.GetExtension()).IsAlive() );
930 DALI_TEST_CHECK( deleted );
934 int UtcDaliShaderEffectMethodCreateExtension2(void)
936 // Test creation of a shader extension
939 TestApplication application;
941 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
942 DALI_TEST_CHECK( effect );
944 BufferImage image = CreateBufferImage();
946 effect.SetUniform( "uFloat", 1.0f );
948 ImageActor actor = ImageActor::New( image );
949 actor.SetSize( 100.0f, 100.0f );
950 actor.SetName("TestImageFilenameActor");
951 actor.SetShaderEffect(effect);
952 Stage::GetCurrent().Add(actor);
954 application.SendNotification();
955 application.Render();
957 TestExtension* extension = new TestExtension ( deleted );
959 effect.AttachExtension( extension );
961 const ShaderEffect& constEffect(effect);
962 const TestExtension& ext( static_cast<const TestExtension&>(constEffect.GetExtension()) );
964 DALI_TEST_CHECK( ext.IsAlive() );
967 DALI_TEST_CHECK( deleted );
971 int UtcDaliShaderEffectMethodNoExtension(void)
973 TestApplication application;
979 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
980 DALI_TEST_CHECK( effect );
982 // Don't attach extension
983 ShaderEffect::Extension& extension = effect.GetExtension();
986 DALI_TEST_CHECK( false );
988 catch (Dali::DaliException& e)
990 // Tests that a negative test of an assertion succeeds
991 DALI_TEST_PRINT_ASSERT( e );
992 DALI_TEST_CHECK( !effect );
998 int UtcDaliShaderEffectPropertyIndices(void)
1000 TestApplication application;
1001 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
1003 Property::IndexContainer indices;
1004 effect.GetPropertyIndices( indices );
1005 DALI_TEST_CHECK( ! indices.empty() );
1006 DALI_TEST_EQUALS( indices.size(), effect.GetPropertyCount(), TEST_LOCATION );
1010 int UtcDaliShaderBinaries(void)
1012 TestApplication application;
1013 // these will not affect the "first round" of dali render as core is already initialized and it has queried the defaults
1014 application.GetGlAbstraction().SetBinaryFormats( 1 );
1015 application.GetGlAbstraction().SetNumBinaryFormats( 1 );
1016 application.GetGlAbstraction().SetProgramBinaryLength( 1 );
1018 GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
1020 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
1021 DALI_TEST_CHECK( effect );
1023 BufferImage image = CreateBufferImage();
1024 ImageActor actor = ImageActor::New( image );
1025 actor.SetSize( 100.0f, 100.0f );
1026 actor.SetName("TestImageFilenameActor");
1027 actor.SetShaderEffect(effect);
1028 Stage::GetCurrent().Add(actor);
1030 application.SendNotification();
1031 application.Render(16);
1032 // binary was not requested by DALi
1033 DALI_TEST_CHECK( !(application.GetGlAbstraction().GetProgramBinaryCalled()) );
1035 GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
1037 // check that the shader was compiled
1038 DALI_TEST_EQUALS( lastShaderCompiledAfter, lastShaderCompiledBefore + 2, TEST_LOCATION );
1040 // simulate context loss to get core to re-initialize its GL
1041 application.GetCore().ContextDestroyed();
1042 application.GetCore().ContextCreated();
1044 application.SendNotification();
1045 application.Render(16);
1047 // shader is recompiled
1048 GLuint finalShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
1049 // check that the shader was compiled
1050 DALI_TEST_EQUALS( lastShaderCompiledAfter + 2, finalShaderCompiled, TEST_LOCATION );
1052 // binary was requested by DALi
1053 DALI_TEST_CHECK( application.GetGlAbstraction().GetProgramBinaryCalled() );
1058 int UtcDaliShaderEffectFromProperties01(void)
1060 TestApplication application;
1061 tet_infoline("UtcDaliShaderEffectFromProperties01()");
1063 std::string fragmentShaderPrefix = "#define TEST_FS 1\n#extension GL_OES_standard_derivatives : enable";
1064 std::string vertexShaderPrefix = "#define TEST_VS 1";
1065 std::string vertexShader(VertexSource);
1066 std::string fragmentShader(FragmentSource);
1068 // Call render to compile default shaders.
1069 application.SendNotification();
1070 application.Render();
1072 GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
1074 // create from type registry
1076 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
1077 DALI_TEST_CHECK( typeInfo );
1078 ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
1079 DALI_TEST_CHECK( effect );
1081 Property::Value programMap = Property::Value(Property::MAP);
1083 programMap.SetValue("vertex", vertexShader);
1084 programMap.SetValue("fragment", fragmentShader);
1086 programMap.SetValue("vertex-prefix", vertexShaderPrefix);
1087 programMap.SetValue("fragment-prefix", fragmentShaderPrefix);
1089 programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
1091 effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
1093 Property::Value imageMap = Property::Value(Property::MAP);
1094 imageMap.SetValue("filename", Property::Value(TestImageFilename));
1095 effect.SetProperty(effect.GetPropertyIndex("image"), imageMap);
1097 // do a update & render to get the image request
1098 application.SendNotification();
1099 application.Render();
1101 Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
1103 Integration::Bitmap* bitmap = CreateBitmap( 10, 10, 0xFF );
1104 Integration::ResourcePointer resourcePtr(bitmap);
1105 TestPlatformAbstraction& platform = application.GetPlatform();
1106 platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resourcePtr);
1108 BufferImage image(CreateBufferImage());
1109 ImageActor actor = ImageActor::New( image );
1110 actor.SetSize( 100.0f, 100.0f );
1111 actor.SetName("TestImageFilenameActor");
1112 actor.SetShaderEffect(effect);
1113 Stage::GetCurrent().Add(actor);
1115 application.SendNotification();
1116 application.Render();
1117 GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
1119 // we should have compiled 2 shaders.
1120 DALI_TEST_EQUALS(lastShaderCompiledAfter, lastShaderCompiledBefore + 2, TEST_LOCATION );
1122 std::string actualVertexShader = application.GetGlAbstraction().GetShaderSource( lastShaderCompiledBefore + 1 );
1123 DALI_TEST_EQUALS( vertexShaderPrefix, actualVertexShader.substr( 0, vertexShaderPrefix.length() ), TEST_LOCATION );
1124 DALI_TEST_EQUALS( vertexShader, actualVertexShader.substr( actualVertexShader.length() - vertexShader.length() ), TEST_LOCATION );
1126 std::string actualFragmentShader = application.GetGlAbstraction().GetShaderSource( lastShaderCompiledBefore + 2 );
1127 DALI_TEST_EQUALS( fragmentShaderPrefix, actualFragmentShader.substr( 0, fragmentShaderPrefix.length() ), TEST_LOCATION );
1128 DALI_TEST_EQUALS( fragmentShader, actualFragmentShader.substr( actualFragmentShader.length() - fragmentShader.length() ), TEST_LOCATION );
1133 int UtcDaliShaderEffectFromProperties02(void)
1137 TestApplication application;
1138 tet_infoline("UtcDaliShaderEffectFromProperties02()");
1140 // Call render to compile default shaders.
1141 application.SendNotification();
1142 application.Render();
1143 application.Render();
1144 application.Render();
1146 // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
1147 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
1148 DALI_TEST_CHECK( typeInfo );
1149 ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
1150 DALI_TEST_CHECK( effect );
1152 Property::Value programMap = Property::Value(Property::MAP);
1154 programMap.SetValue("vertex", std::string(VertexSource));
1155 programMap.SetValue("fragment", std::string(FragmentSource));
1157 // programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
1158 // dont set by value
1159 programMap.SetValue("geometry-type", GeometryType( GEOMETRY_TYPE_IMAGE ));
1161 effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
1163 tet_result( TET_FAIL );
1165 catch(Dali::DaliException& e)
1167 DALI_TEST_PRINT_ASSERT( e );
1172 int UtcDaliShaderEffectFromProperties03(void)
1176 TestApplication application;
1177 tet_infoline("UtcDaliShaderEffectFromProperties03()");
1179 // Call render to compile default shaders.
1180 application.SendNotification();
1181 application.Render();
1182 application.Render();
1183 application.Render();
1185 // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
1186 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
1187 DALI_TEST_CHECK( typeInfo );
1188 ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
1189 DALI_TEST_CHECK( effect );
1192 effect.SetProperty( effect.GetPropertyIndex("geometry-hints"), "HINT_2" );
1194 tet_result( TET_FAIL );
1196 catch(Dali::DaliException& e)
1198 DALI_TEST_PRINT_ASSERT( e );