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 void operator()( Vector3& current, const PropertyInputContainer& /* inputs */ )
69 struct TestConstraintFromPositionToVector3
71 TestConstraintFromPositionToVector3()
75 void operator()( Vector3& current, const PropertyInputContainer& inputs )
77 current = inputs[0]->GetVector3();
81 struct TestConstraintToVector3Double
83 TestConstraintToVector3Double(Vector3 target)
88 void operator()( Vector3& current, const PropertyInputContainer& /* inputs */ )
90 current = mTarget * 2.0f;
96 class ShaderEffectExtension : public ShaderEffect::Extension {};
99 class TestExtension : public ShaderEffect::Extension
102 TestExtension( bool& deleted )
119 static const char* TestImageFilename = "icon_wrt.png";
121 Integration::Bitmap* CreateBitmap( unsigned int imageHeight, unsigned int imageWidth, unsigned int initialColor )
123 Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::RETAIN );
124 Integration::PixelBuffer* pixbuffer = bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, imageWidth,imageHeight,imageWidth,imageHeight );
125 unsigned int bytesPerPixel = GetBytesPerPixel( Pixel::RGBA8888 );
127 memset( pixbuffer, initialColor , imageHeight*imageWidth*bytesPerPixel);
135 int UtcDaliShaderEffectMethodNew01(void)
137 TestApplication application;
139 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
140 DALI_TEST_CHECK(effect);
144 int UtcDaliShaderEffectMethodNew02(void)
146 TestApplication application;
152 // New must be called to create a ShaderEffect or it wont be valid.
153 effect.SetUniform( "uUniform", 0 );
154 DALI_TEST_CHECK( false );
156 catch (Dali::DaliException& e)
158 // Tests that a negative test of an assertion succeeds
159 DALI_TEST_PRINT_ASSERT( e );
160 DALI_TEST_CHECK( !effect );
165 int UtcDaliShaderEffectMethodNew04(void)
167 TestApplication application;
168 tet_infoline("Testing prefixed version of Dali::ShaderEffect::New()");
170 std::string fragmentShaderPrefix = "#define TEST_FS 1\n#extension GL_OES_standard_derivatives : enable";
171 std::string vertexShaderPrefix = "#define TEST_VS 1";
175 // Call render to compile default shaders.
176 application.SendNotification();
177 application.Render();
178 application.Render();
179 application.Render();
181 GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
182 ShaderEffect effect = ShaderEffect::NewWithPrefix( vertexShaderPrefix, VertexSource,
183 fragmentShaderPrefix, FragmentSourceUsingExtensions,
184 GEOMETRY_TYPE_IMAGE, ShaderEffect::HINT_NONE );
186 BufferImage image = CreateBufferImage();
187 ImageActor actor = ImageActor::New( image );
188 actor.SetSize( 100.0f, 100.0f );
189 actor.SetName("TestImageFilenameActor");
190 actor.SetShaderEffect(effect);
191 Stage::GetCurrent().Add(actor);
193 application.SendNotification();
194 application.Render();
195 GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
196 bool testResult = false;
198 // we should have compiled 2 shaders.
199 DALI_TEST_EQUALS( lastShaderCompiledAfter, lastShaderCompiledBefore + 2, TEST_LOCATION );
201 char testVertexSourceResult[GETSOURCE_BUFFER_SIZE];
202 char testFragmentSourceResult[GETSOURCE_BUFFER_SIZE];
204 // we are interested in the first two.
205 GLuint vertexShaderId = lastShaderCompiledBefore + 1;
206 GLuint fragmentShaderId = lastShaderCompiledBefore + 2;
208 GLsizei lengthVertexResult;
209 GLsizei lengthFragmentResult;
211 application.GetGlAbstraction().GetShaderSource(vertexShaderId, GETSOURCE_BUFFER_SIZE, &lengthVertexResult, testVertexSourceResult);
212 application.GetGlAbstraction().GetShaderSource(fragmentShaderId, GETSOURCE_BUFFER_SIZE, &lengthFragmentResult, testFragmentSourceResult);
214 int vertexShaderHasPrefix = strncmp(testVertexSourceResult, "#define ", strlen("#define "));
215 int fragmentShaderHasPrefix = strncmp(testFragmentSourceResult, "#define ", strlen("#define "));
216 testResult = (vertexShaderHasPrefix == 0) && (fragmentShaderHasPrefix == 0);
218 DALI_TEST_CHECK(testResult);
220 catch(Dali::DaliException& e)
222 DALI_TEST_PRINT_ASSERT( e );
223 tet_result( TET_FAIL );
228 int UtcDaliShaderEffectMethodNew05(void)
230 TestApplication application;
232 // heap constructor / destructor
233 DefaultFunctionCoverage<ShaderEffect> shaderEffect;
234 DefaultFunctionCoverage<ShaderEffectExtension> shaderEffectExtension;
239 int UtcDaliShaderEffectMethodDownCast(void)
241 TestApplication application;
242 tet_infoline("Testing Dali::ShaderEffect::DownCast()");
244 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
246 BaseHandle object(effect);
248 ShaderEffect effect2 = ShaderEffect::DownCast(object);
249 DALI_TEST_CHECK(effect2);
251 ShaderEffect effect3 = DownCast< ShaderEffect >(object);
252 DALI_TEST_CHECK(effect3);
254 BaseHandle unInitializedObject;
255 ShaderEffect effect4 = ShaderEffect::DownCast(unInitializedObject);
256 DALI_TEST_CHECK(!effect4);
258 ShaderEffect effect5 = DownCast< ShaderEffect >(unInitializedObject);
259 DALI_TEST_CHECK(!effect5);
263 int UtcDaliShaderEffectMethodDelete01(void)
265 TestApplication application;
267 // get the default shaders built, this is not required but makes it
268 // easier to debug the TET case and isolate the custom shader compilation.
269 application.SendNotification();
270 application.Render();
272 GLuint beforeShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
274 // create a new shader effect
275 // the vertex and fragment shader will be cached in the ShaderFactory
276 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
278 // destroy the shader effect
281 // Create the same shader effect again, this should now use the cached version
282 // held in the shader factory
283 effect= ShaderEffect::New( VertexSource, FragmentSource );
285 // Compile the shader effect
286 application.SendNotification();
287 application.Render();
289 GLuint lastShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
290 // no shaders were compiled as they are now compiled on demand and this shader was not used
291 DALI_TEST_EQUALS( beforeShaderCompiled, lastShaderCompiled, TEST_LOCATION );
296 int UtcDaliShaderEffectMethodSetUniformFloat(void)
298 TestApplication application;
300 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
301 DALI_TEST_CHECK( effect );
303 BufferImage image = CreateBufferImage();
305 effect.SetUniform( "uFloat", 1.0f );
307 ImageActor actor = ImageActor::New( image );
308 actor.SetSize( 100.0f, 100.0f );
309 actor.SetName("TestImageFilenameActor");
310 actor.SetShaderEffect(effect);
311 Stage::GetCurrent().Add(actor);
313 application.SendNotification();
314 application.Render();
317 application.GetGlAbstraction().CheckUniformValue(
322 int UtcDaliShaderEffectMethodSetUniformVector2(void)
324 TestApplication application;
326 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
327 DALI_TEST_CHECK( effect );
329 BufferImage image = CreateBufferImage();
331 effect.SetUniform( "uVec2", Vector2( 2.0f, 3.0f ) );
333 ImageActor actor = ImageActor::New( image );
334 actor.SetSize( 100.0f, 100.0f );
335 actor.SetName("TestImageFilenameActor");
336 actor.SetShaderEffect(effect);
337 Stage::GetCurrent().Add(actor);
339 application.SendNotification();
340 application.Render();
343 application.GetGlAbstraction().CheckUniformValue(
344 "uVec2", Vector2( 2.0f, 3.0f ) ) );
348 int UtcDaliShaderEffectMethodSetUniformVector3(void)
350 TestApplication application;
352 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
353 DALI_TEST_CHECK( effect );
355 BufferImage image = CreateBufferImage();
357 effect.SetUniform( "uVec3", Vector3( 4.0f, 5.0f, 6.0f ) );
359 ImageActor actor = ImageActor::New( image );
360 actor.SetSize( 100.0f, 100.0f );
361 actor.SetName("TestImageFilenameActor");
362 actor.SetShaderEffect(effect);
363 Stage::GetCurrent().Add(actor);
365 application.SendNotification();
366 application.Render();
369 application.GetGlAbstraction().CheckUniformValue(
370 "uVec3", Vector3( 4.0f, 5.0f, 6.0f ) ) );
374 int UtcDaliShaderEffectMethodSetUniformVector4(void)
376 TestApplication application;
378 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
379 DALI_TEST_CHECK( effect );
381 BufferImage image = CreateBufferImage();
383 effect.SetUniform( "uVec4", Vector4( 7.0f, 8.0f, 9.0f, 10.0f ) );
385 ImageActor actor = ImageActor::New( image );
386 actor.SetSize( 100.0f, 100.0f );
387 actor.SetName("TestImageFilenameActor");
388 actor.SetShaderEffect(effect);
389 Stage::GetCurrent().Add(actor);
391 application.SendNotification();
392 application.Render();
395 application.GetGlAbstraction().CheckUniformValue(
396 "uVec4", Vector4( 7.0f, 8.0f, 9.0f, 10.0f ) ) );
400 int UtcDaliShaderEffectMethodSetUniformMatrix(void)
402 TestApplication application;
404 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
405 DALI_TEST_CHECK( effect );
407 BufferImage image = CreateBufferImage();
409 effect.SetUniform( "uModelView", Matrix::IDENTITY );
411 ImageActor actor = ImageActor::New( image );
412 actor.SetSize( 100.0f, 100.0f );
413 actor.SetName("TestImageFilenameActor");
414 actor.SetShaderEffect(effect);
415 Stage::GetCurrent().Add(actor);
417 application.SendNotification();
418 application.Render();
421 application.GetGlAbstraction().CheckUniformValue(
422 "uModelView", Matrix::IDENTITY ) );
426 int UtcDaliShaderEffectMethodSetUniformMatrix3(void)
428 TestApplication application;
430 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
431 DALI_TEST_CHECK( effect );
433 BufferImage image = CreateBufferImage();
435 Matrix3 matIdentity(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
436 effect.SetUniform( "uMatrix3", matIdentity );
438 ImageActor actor = ImageActor::New( image );
439 actor.SetSize( 100.0f, 100.0f );
440 actor.SetName("TestImageFilenameActor");
441 actor.SetShaderEffect(effect);
442 Stage::GetCurrent().Add(actor);
444 application.SendNotification();
445 application.Render();
447 DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue("uMatrix3", matIdentity) );
451 int UtcDaliShaderEffectMethodSetUniformViewport(void)
453 TestApplication application;
455 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
456 DALI_TEST_CHECK( effect );
458 BufferImage image = CreateBufferImage();
460 ImageActor actor = ImageActor::New( image );
461 actor.SetSize( 100.0f, 100.0f );
462 actor.SetName("TestImageFilenameActor");
463 actor.SetShaderEffect(effect);
464 Stage::GetCurrent().Add(actor);
466 effect.SetUniform( "uVec2", Vector2( 0.0f, 0.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
467 effect.SetUniform( "uVec2Dir", Vector2( 1.0f, 2.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_DIRECTION );
469 application.SendNotification();
470 application.Render();
472 const Vector2& stageSize(Stage::GetCurrent().GetSize());
474 DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "uVec2", Vector2( stageSize.x/2, -stageSize.y/2 ) ) );
476 DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "uVec2Dir", Vector2( -1.0f, 2.0f ) ) );
478 // change coordinate types
479 effect.SetUniform( "uVec2", Vector2( 0.1f, 0.2f ), ShaderEffect::COORDINATE_TYPE_DEFAULT );
480 effect.SetUniform( "uVec2Dir", Vector2( 1.0f, 2.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
481 actor.SetPixelArea( ImageActor::PixelArea( 0, 0, 10, 10 ) );
483 application.SendNotification();
484 application.Render();
487 application.GetGlAbstraction().GetUniformValue( "uVec2", outValue );
488 DALI_TEST_EQUALS( outValue, Vector2( 0.1f, 0.2f ), TEST_LOCATION );
490 application.GetGlAbstraction().GetUniformValue( "uVec2Dir", outValue );
491 DALI_TEST_EQUALS( outValue, Vector2( stageSize.x *.5f - 1.f, -stageSize.y * .5f + 2.f), TEST_LOCATION );
496 int UtcDaliShaderEffectMethodSetEffectImage(void)
498 TestApplication application;
500 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
501 DALI_TEST_CHECK( effect );
503 BufferImage image = CreateBufferImage();
505 effect.SetEffectImage(image);
507 ImageActor actor = ImageActor::New( image );
508 actor.SetSize( 100.0f, 100.0f );
509 actor.SetName("TestImageFilenameActor");
510 actor.SetShaderEffect(effect);
511 Stage::GetCurrent().Add(actor);
513 application.SendNotification();
514 application.Render(16);
515 application.SendNotification();
516 application.Render(16);
517 application.SendNotification();
519 GLuint programId, uniformId;
520 bool uniformWasSet = application.GetGlAbstraction().GetUniformIds( "sEffect", programId, uniformId );
521 // we dont care about the value of the sampler uniform as thats internal to DALi core and subject to change
522 DALI_TEST_CHECK( uniformWasSet );
526 int UtcDaliShaderEffectMethodSetEffectImageAndDelete(void)
528 TestApplication application;
530 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
532 BufferImage effectImage = CreateBufferImage();
533 effect.SetEffectImage(effectImage);
535 ImageActor actor = ImageActor::New();
537 actor.SetShaderEffect(effect);
540 Stage::GetCurrent().Add(actor);
542 // do an update / render cycle
543 application.SendNotification();
544 application.Render(16);
545 application.SendNotification();
546 application.Render(16);
547 application.SendNotification();
548 application.Render(16);
550 printf("removing image actor from stage and Reseting handle\n");
551 Stage::GetCurrent().Remove(actor);
554 tet_printf("### Update & Render \n");
556 application.SendNotification();
557 application.Render(16);
559 tet_printf("#### Update Only \n");
561 tet_printf("effectImage.Reset \n");
563 // this releases the effect texture resource,
564 // Update will send a DispatchDiscardTexture message to render
566 application.SendNotification();
567 application.UpdateOnly(16);
569 tet_printf("#### Update Only \n");
571 // at this point shader is deleted, during clear discard queue
572 // and it sends a Shader:: DispatchRemoveObserver message to render thread
573 application.UpdateOnly(16);
575 tet_printf("#### Render Only \n");
576 // This is where it used to crash, there is a message in the queue to perform DispatchDiscardTexture
577 // which tries to call observer->TextureDiscarded, where observer == shader that was deleted
578 // in previous update.
579 application.RenderOnly();
582 // process the discard texture message
583 application.RenderOnly();
584 application.SendNotification();
585 application.Render(16);
587 tet_result(TET_PASS);
592 int UtcDaliShaderEffectMethodApplyConstraint(void)
594 // Test whether Shader's uniform can be constrained to a stationary constraint.
595 TestApplication application;
597 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
598 DALI_TEST_CHECK( effect );
600 BufferImage image = CreateBufferImage();
602 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
604 ImageActor actor = ImageActor::New( image );
605 actor.SetSize( 100.0f, 100.0f );
606 actor.SetName("TestImageFilenameActor");
607 actor.SetShaderEffect(effect);
608 Stage::GetCurrent().Add(actor);
610 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
612 application.SendNotification();
613 application.Render();
615 // Test effects of SetUniform...
617 application.GetGlAbstraction().CheckUniformValue(
618 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
620 Constraint constraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
623 application.SendNotification();
624 application.Render();
626 // Test effects of Constraint.
628 application.GetGlAbstraction().CheckUniformValue(
629 "uVec3", Vector3( 4.0f, 9.0f, 16.0f ) ) );
634 int UtcDaliShaderEffectMethodApplyConstraintFromActor(void)
636 // Test whether Shader's uniform can be constrained to Actor's position.
637 TestApplication application;
639 const Vector3 targetPosition = Vector3( 100.0f, 70.0f, 20.0f);
641 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
642 DALI_TEST_CHECK( effect );
644 BufferImage image = CreateBufferImage();
646 effect.SetUniform( "uVec3", Vector3( 50.0f, 25.0f, 0.0f ) );
648 ImageActor actor = ImageActor::New( image );
649 actor.SetPosition(targetPosition);
650 actor.SetSize( 100.0f, 100.0f );
651 actor.SetName("TestImageFilenameActor");
652 actor.SetShaderEffect(effect);
653 Stage::GetCurrent().Add(actor);
655 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
657 Constraint constraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintFromPositionToVector3() );
658 constraint.AddSource( Source( actor, Actor::Property::POSITION ) );
661 application.SendNotification();
662 application.Render();
664 // Test effects of Constraint.
666 application.GetGlAbstraction().CheckUniformValue(
667 "uVec3", targetPosition ) );
671 int UtcDaliShaderEffectMethodApplyConstraintFromActor2(void)
673 // Test whether Shader's uniform can be constrained to Actor's position.
674 // While Actor's position is constrained to another point * 2.0f
675 TestApplication application;
677 const Vector3 targetPosition = Vector3( 25.0f, 36.0f, 49.0f );
679 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
680 DALI_TEST_CHECK( effect );
682 BufferImage image = CreateBufferImage();
684 effect.SetUniform( "uVec3", Vector3( 50.0f, 25.0f, 0.0f ) );
686 ImageActor actor = ImageActor::New( image );
687 actor.SetPosition(Vector3( 100.0f, 70.0f, 20.0f));
688 actor.SetSize( 100.0f, 100.0f );
689 actor.SetName("TestImageFilenameActor");
690 actor.SetShaderEffect(effect);
691 Stage::GetCurrent().Add(actor);
693 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
695 Constraint shaderConstraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintFromPositionToVector3() );
696 shaderConstraint.AddSource( Source(actor, Actor::Property::POSITION) );
697 shaderConstraint.Apply();
699 Constraint actorConstraint = Constraint::New<Vector3>( actor, Actor::Property::POSITION, TestConstraintToVector3Double(targetPosition) );
700 actorConstraint.Apply();
702 application.SendNotification();
703 application.Render();
705 // Test effects of Constraint.
707 application.GetGlAbstraction().CheckUniformValue(
708 "uVec3", targetPosition * 2.0f ) );
712 int UtcDaliShaderEffectMethodRemoveConstraints(void)
714 // Test if constrains can be removed before they are ever applyed.
715 TestApplication application;
717 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
718 DALI_TEST_CHECK( effect );
720 BufferImage image = CreateBufferImage();
722 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
724 ImageActor actor = ImageActor::New( image );
725 actor.SetSize( 100.0f, 100.0f );
726 actor.SetName("TestImageFilenameActor");
727 actor.SetShaderEffect(effect);
728 Stage::GetCurrent().Add(actor);
730 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
732 application.SendNotification();
733 application.Render();
735 // Test effects of SetUniform...
737 application.GetGlAbstraction().CheckUniformValue(
738 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
740 Constraint constraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
743 // Remove the constraints
744 effect.RemoveConstraints();
746 application.SendNotification();
747 application.Render();
749 // Test effects of Constraint.
751 application.GetGlAbstraction().CheckUniformValue(
752 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
756 int UtcDaliShaderEffectMethodRemoveConstraints2(void)
758 // Test whether Shader's uniform constrains can be removed after they are applyed.
759 TestApplication application;
761 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
762 DALI_TEST_CHECK( effect );
764 BufferImage image = CreateBufferImage();
766 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
768 ImageActor actor = ImageActor::New( image );
769 actor.SetSize( 100.0f, 100.0f );
770 actor.SetName("TestImageFilenameActor");
771 actor.SetShaderEffect(effect);
772 Stage::GetCurrent().Add(actor);
774 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
776 application.SendNotification();
777 application.Render();
779 // Test effects of SetUniform...
781 application.GetGlAbstraction().CheckUniformValue(
782 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
784 Constraint constraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
787 application.SendNotification();
788 application.Render();
790 // Reset the value and remove the constraints
791 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
792 effect.RemoveConstraints();
794 application.SendNotification();
795 application.Render();
797 // Test effects of Constraint.
799 application.GetGlAbstraction().CheckUniformValue(
800 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
804 int UtcDaliShaderEffectMethodCreateExtension(void)
806 // Test creation of a shader extension
807 TestApplication aplication;
811 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
812 DALI_TEST_CHECK( effect );
814 TestExtension* extension = new TestExtension ( deleted );
816 effect.AttachExtension( extension );
818 DALI_TEST_CHECK( static_cast<TestExtension&>(effect.GetExtension()).IsAlive() );
821 DALI_TEST_CHECK( deleted );
825 int UtcDaliShaderEffectMethodCreateExtension2(void)
827 // Test creation of a shader extension
830 TestApplication application;
832 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
833 DALI_TEST_CHECK( effect );
835 BufferImage image = CreateBufferImage();
837 effect.SetUniform( "uFloat", 1.0f );
839 ImageActor actor = ImageActor::New( image );
840 actor.SetSize( 100.0f, 100.0f );
841 actor.SetName("TestImageFilenameActor");
842 actor.SetShaderEffect(effect);
843 Stage::GetCurrent().Add(actor);
845 application.SendNotification();
846 application.Render();
848 TestExtension* extension = new TestExtension ( deleted );
850 effect.AttachExtension( extension );
852 const ShaderEffect& constEffect(effect);
853 const TestExtension& ext( static_cast<const TestExtension&>(constEffect.GetExtension()) );
855 DALI_TEST_CHECK( ext.IsAlive() );
858 DALI_TEST_CHECK( deleted );
862 int UtcDaliShaderEffectMethodNoExtension(void)
864 TestApplication application;
870 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
871 DALI_TEST_CHECK( effect );
873 // Don't attach extension
874 ShaderEffect::Extension& extension = effect.GetExtension();
877 DALI_TEST_CHECK( false );
879 catch (Dali::DaliException& e)
881 // Tests that a negative test of an assertion succeeds
882 DALI_TEST_PRINT_ASSERT( e );
883 DALI_TEST_CHECK( !effect );
889 int UtcDaliShaderEffectPropertyIndices(void)
891 TestApplication application;
892 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
894 Property::IndexContainer indices;
895 effect.GetPropertyIndices( indices );
896 DALI_TEST_CHECK( indices.Size() );
897 DALI_TEST_EQUALS( indices.Size(), effect.GetPropertyCount(), TEST_LOCATION );
901 int UtcDaliShaderBinaries(void)
903 TestApplication application;
904 // these will not affect the "first round" of dali render as core is already initialized and it has queried the defaults
905 application.GetGlAbstraction().SetBinaryFormats( 1 );
906 application.GetGlAbstraction().SetNumBinaryFormats( 1 );
907 application.GetGlAbstraction().SetProgramBinaryLength( 1 );
909 GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
911 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
912 DALI_TEST_CHECK( effect );
914 BufferImage image = CreateBufferImage();
915 ImageActor actor = ImageActor::New( image );
916 actor.SetSize( 100.0f, 100.0f );
917 actor.SetName("TestImageFilenameActor");
918 actor.SetShaderEffect(effect);
919 Stage::GetCurrent().Add(actor);
921 application.SendNotification();
922 application.Render(16);
923 // binary was not requested by DALi
924 DALI_TEST_CHECK( !(application.GetGlAbstraction().GetProgramBinaryCalled()) );
926 GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
928 // check that the shader was compiled
929 DALI_TEST_EQUALS( lastShaderCompiledAfter, lastShaderCompiledBefore + 2, TEST_LOCATION );
931 // simulate context loss to get core to re-initialize its GL
932 application.GetCore().ContextDestroyed();
933 application.GetCore().ContextCreated();
935 application.SendNotification();
936 application.Render(16);
938 // shader is recompiled
939 GLuint finalShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
940 // check that the shader was compiled
941 DALI_TEST_EQUALS( lastShaderCompiledAfter + 2, finalShaderCompiled, TEST_LOCATION );
943 // binary was requested by DALi
944 DALI_TEST_CHECK( application.GetGlAbstraction().GetProgramBinaryCalled() );
949 int UtcDaliShaderEffectFromProperties01(void)
951 TestApplication application;
952 tet_infoline("UtcDaliShaderEffectFromProperties01()");
954 std::string fragmentShaderPrefix = "#define TEST_FS 1\n#extension GL_OES_standard_derivatives : enable";
955 std::string vertexShaderPrefix = "#define TEST_VS 1";
956 std::string vertexShader(VertexSource);
957 std::string fragmentShader(FragmentSource);
959 // Call render to compile default shaders.
960 application.SendNotification();
961 application.Render();
963 GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
965 // create from type registry
967 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
968 DALI_TEST_CHECK( typeInfo );
969 ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
970 DALI_TEST_CHECK( effect );
972 Property::Value programMap = Property::Value(Property::MAP);
974 programMap.SetValue("vertex", vertexShader);
975 programMap.SetValue("fragment", fragmentShader);
977 programMap.SetValue("vertex-prefix", vertexShaderPrefix);
978 programMap.SetValue("fragment-prefix", fragmentShaderPrefix);
980 programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
982 effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
984 Property::Value imageMap = Property::Value(Property::MAP);
985 imageMap.SetValue("filename", Property::Value(TestImageFilename));
986 effect.SetProperty(effect.GetPropertyIndex("image"), imageMap);
988 // do a update & render to get the image request
989 application.SendNotification();
990 application.Render();
992 Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
994 Integration::Bitmap* bitmap = CreateBitmap( 10, 10, 0xFF );
995 Integration::ResourcePointer resourcePtr(bitmap);
996 TestPlatformAbstraction& platform = application.GetPlatform();
997 platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resourcePtr);
999 BufferImage image(CreateBufferImage());
1000 ImageActor actor = ImageActor::New( image );
1001 actor.SetSize( 100.0f, 100.0f );
1002 actor.SetName("TestImageFilenameActor");
1003 actor.SetShaderEffect(effect);
1004 Stage::GetCurrent().Add(actor);
1006 application.SendNotification();
1007 application.Render();
1008 GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
1010 // we should have compiled 2 shaders.
1011 DALI_TEST_EQUALS(lastShaderCompiledAfter, lastShaderCompiledBefore + 2, TEST_LOCATION );
1013 std::string actualVertexShader = application.GetGlAbstraction().GetShaderSource( lastShaderCompiledBefore + 1 );
1014 DALI_TEST_EQUALS( vertexShaderPrefix, actualVertexShader.substr( 0, vertexShaderPrefix.length() ), TEST_LOCATION );
1015 DALI_TEST_EQUALS( vertexShader, actualVertexShader.substr( actualVertexShader.length() - vertexShader.length() ), TEST_LOCATION );
1017 std::string actualFragmentShader = application.GetGlAbstraction().GetShaderSource( lastShaderCompiledBefore + 2 );
1018 DALI_TEST_EQUALS( fragmentShaderPrefix, actualFragmentShader.substr( 0, fragmentShaderPrefix.length() ), TEST_LOCATION );
1019 DALI_TEST_EQUALS( fragmentShader, actualFragmentShader.substr( actualFragmentShader.length() - fragmentShader.length() ), TEST_LOCATION );
1024 int UtcDaliShaderEffectFromProperties02(void)
1028 TestApplication application;
1029 tet_infoline("UtcDaliShaderEffectFromProperties02()");
1031 // Call render to compile default shaders.
1032 application.SendNotification();
1033 application.Render();
1034 application.Render();
1035 application.Render();
1037 // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
1038 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
1039 DALI_TEST_CHECK( typeInfo );
1040 ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
1041 DALI_TEST_CHECK( effect );
1043 Property::Value programMap = Property::Value(Property::MAP);
1045 programMap.SetValue("vertex", std::string(VertexSource));
1046 programMap.SetValue("fragment", std::string(FragmentSource));
1048 // programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
1049 // dont set by value
1050 programMap.SetValue("geometry-type", GeometryType( GEOMETRY_TYPE_IMAGE ));
1052 effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
1054 tet_result( TET_FAIL );
1056 catch(Dali::DaliException& e)
1058 DALI_TEST_PRINT_ASSERT( e );
1063 int UtcDaliShaderEffectFromProperties03(void)
1067 TestApplication application;
1068 tet_infoline("UtcDaliShaderEffectFromProperties03()");
1070 // Call render to compile default shaders.
1071 application.SendNotification();
1072 application.Render();
1073 application.Render();
1074 application.Render();
1076 // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
1077 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
1078 DALI_TEST_CHECK( typeInfo );
1079 ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
1080 DALI_TEST_CHECK( effect );
1083 effect.SetProperty( effect.GetPropertyIndex("geometry-hints"), "HINT_2" );
1085 tet_result( TET_FAIL );
1087 catch(Dali::DaliException& e)
1089 DALI_TEST_PRINT_ASSERT( e );