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 )
123 int UtcDaliShaderEffectMethodNew01(void)
125 TestApplication application;
127 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
128 DALI_TEST_CHECK(effect);
132 int UtcDaliShaderEffectMethodNew02(void)
134 TestApplication application;
140 // New must be called to create a ShaderEffect or it wont be valid.
141 effect.SetUniform( "uUniform", 0 );
142 DALI_TEST_CHECK( false );
144 catch (Dali::DaliException& e)
146 // Tests that a negative test of an assertion succeeds
147 DALI_TEST_PRINT_ASSERT( e );
148 DALI_TEST_CHECK( !effect );
153 int UtcDaliShaderEffectMethodNew04(void)
155 TestApplication application;
156 tet_infoline("Testing prefixed version of Dali::ShaderEffect::New()");
158 std::string fragmentShaderPrefix = "#define TEST_FS 1\n#extension GL_OES_standard_derivatives : enable";
159 std::string vertexShaderPrefix = "#define TEST_VS 1";
163 // Call render to compile default shaders.
164 application.SendNotification();
165 application.Render();
166 application.Render();
167 application.Render();
169 GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
170 ShaderEffect effect = ShaderEffect::NewWithPrefix( vertexShaderPrefix, VertexSource,
171 fragmentShaderPrefix, FragmentSourceUsingExtensions,
172 GEOMETRY_TYPE_IMAGE, ShaderEffect::HINT_NONE );
174 BitmapImage image = CreateBitmapImage();
175 ImageActor actor = ImageActor::New( image );
176 actor.SetSize( 100.0f, 100.0f );
177 actor.SetName("TestImageFilenameActor");
178 actor.SetShaderEffect(effect);
179 Stage::GetCurrent().Add(actor);
181 application.SendNotification();
182 application.Render();
183 GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
184 bool testResult = false;
186 // we should have compiled 2 shaders.
187 DALI_TEST_EQUALS( lastShaderCompiledAfter, lastShaderCompiledBefore + 2, TEST_LOCATION );
189 char testVertexSourceResult[GETSOURCE_BUFFER_SIZE];
190 char testFragmentSourceResult[GETSOURCE_BUFFER_SIZE];
192 // we are interested in the first two.
193 GLuint vertexShaderId = lastShaderCompiledBefore + 1;
194 GLuint fragmentShaderId = lastShaderCompiledBefore + 2;
196 GLsizei lengthVertexResult;
197 GLsizei lengthFragmentResult;
199 application.GetGlAbstraction().GetShaderSource(vertexShaderId, GETSOURCE_BUFFER_SIZE, &lengthVertexResult, testVertexSourceResult);
200 application.GetGlAbstraction().GetShaderSource(fragmentShaderId, GETSOURCE_BUFFER_SIZE, &lengthFragmentResult, testFragmentSourceResult);
202 int vertexShaderHasPrefix = strncmp(testVertexSourceResult, "#define ", strlen("#define "));
203 int fragmentShaderHasPrefix = strncmp(testFragmentSourceResult, "#define ", strlen("#define "));
204 testResult = (vertexShaderHasPrefix == 0) && (fragmentShaderHasPrefix == 0);
206 DALI_TEST_CHECK(testResult);
208 catch(Dali::DaliException& e)
210 DALI_TEST_PRINT_ASSERT( e );
211 tet_result( TET_FAIL );
216 int UtcDaliShaderEffectMethodNew05(void)
218 TestApplication application;
220 // heap constructor / destructor
221 DefaultFunctionCoverage<ShaderEffect> shaderEffect;
222 DefaultFunctionCoverage<ShaderEffectExtension> shaderEffectExtension;
227 int UtcDaliShaderEffectMethodDownCast(void)
229 TestApplication application;
230 tet_infoline("Testing Dali::ShaderEffect::DownCast()");
232 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
234 BaseHandle object(effect);
236 ShaderEffect effect2 = ShaderEffect::DownCast(object);
237 DALI_TEST_CHECK(effect2);
239 ShaderEffect effect3 = DownCast< ShaderEffect >(object);
240 DALI_TEST_CHECK(effect3);
242 BaseHandle unInitializedObject;
243 ShaderEffect effect4 = ShaderEffect::DownCast(unInitializedObject);
244 DALI_TEST_CHECK(!effect4);
246 ShaderEffect effect5 = DownCast< ShaderEffect >(unInitializedObject);
247 DALI_TEST_CHECK(!effect5);
251 int UtcDaliShaderEffectMethodDelete01(void)
253 TestApplication application;
255 // get the default shaders built, this is not required but makes it
256 // easier to debug the TET case and isolate the custom shader compilation.
257 application.SendNotification();
258 application.Render();
260 GLuint beforeShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
262 // create a new shader effect
263 // the vertex and fragment shader will be cached in the ShaderFactory
264 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
266 // destroy the shader effect
269 // Create the same shader effect again, this should now use the cached version
270 // held in the shader factory
271 effect= ShaderEffect::New( VertexSource, FragmentSource );
273 // Compile the shader effect
274 application.SendNotification();
275 application.Render();
277 GLuint lastShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
278 // no shaders were compiled as they are now compiled on demand and this shader was not used
279 DALI_TEST_EQUALS( beforeShaderCompiled, lastShaderCompiled, TEST_LOCATION );
284 int UtcDaliShaderEffectMethodSetUniformFloat(void)
286 TestApplication application;
288 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
289 DALI_TEST_CHECK( effect );
291 BitmapImage image = CreateBitmapImage();
293 effect.SetUniform( "uFloat", 1.0f );
295 ImageActor actor = ImageActor::New( image );
296 actor.SetSize( 100.0f, 100.0f );
297 actor.SetName("TestImageFilenameActor");
298 actor.SetShaderEffect(effect);
299 Stage::GetCurrent().Add(actor);
301 application.SendNotification();
302 application.Render();
305 application.GetGlAbstraction().CheckUniformValue(
310 int UtcDaliShaderEffectMethodSetUniformVector2(void)
312 TestApplication application;
314 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
315 DALI_TEST_CHECK( effect );
317 BitmapImage image = CreateBitmapImage();
319 effect.SetUniform( "uVec2", Vector2( 2.0f, 3.0f ) );
321 ImageActor actor = ImageActor::New( image );
322 actor.SetSize( 100.0f, 100.0f );
323 actor.SetName("TestImageFilenameActor");
324 actor.SetShaderEffect(effect);
325 Stage::GetCurrent().Add(actor);
327 application.SendNotification();
328 application.Render();
331 application.GetGlAbstraction().CheckUniformValue(
332 "uVec2", Vector2( 2.0f, 3.0f ) ) );
336 int UtcDaliShaderEffectMethodSetUniformVector3(void)
338 TestApplication application;
340 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
341 DALI_TEST_CHECK( effect );
343 BitmapImage image = CreateBitmapImage();
345 effect.SetUniform( "uVec3", Vector3( 4.0f, 5.0f, 6.0f ) );
347 ImageActor actor = ImageActor::New( image );
348 actor.SetSize( 100.0f, 100.0f );
349 actor.SetName("TestImageFilenameActor");
350 actor.SetShaderEffect(effect);
351 Stage::GetCurrent().Add(actor);
353 application.SendNotification();
354 application.Render();
357 application.GetGlAbstraction().CheckUniformValue(
358 "uVec3", Vector3( 4.0f, 5.0f, 6.0f ) ) );
362 int UtcDaliShaderEffectMethodSetUniformVector4(void)
364 TestApplication application;
366 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
367 DALI_TEST_CHECK( effect );
369 BitmapImage image = CreateBitmapImage();
371 effect.SetUniform( "uVec4", Vector4( 7.0f, 8.0f, 9.0f, 10.0f ) );
373 ImageActor actor = ImageActor::New( image );
374 actor.SetSize( 100.0f, 100.0f );
375 actor.SetName("TestImageFilenameActor");
376 actor.SetShaderEffect(effect);
377 Stage::GetCurrent().Add(actor);
379 application.SendNotification();
380 application.Render();
383 application.GetGlAbstraction().CheckUniformValue(
384 "uVec4", Vector4( 7.0f, 8.0f, 9.0f, 10.0f ) ) );
388 int UtcDaliShaderEffectMethodSetUniformMatrix(void)
390 TestApplication application;
392 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
393 DALI_TEST_CHECK( effect );
395 BitmapImage image = CreateBitmapImage();
397 effect.SetUniform( "uModelView", Matrix::IDENTITY );
399 ImageActor actor = ImageActor::New( image );
400 actor.SetSize( 100.0f, 100.0f );
401 actor.SetName("TestImageFilenameActor");
402 actor.SetShaderEffect(effect);
403 Stage::GetCurrent().Add(actor);
405 application.SendNotification();
406 application.Render();
409 application.GetGlAbstraction().CheckUniformValue(
410 "uModelView", Matrix::IDENTITY ) );
414 int UtcDaliShaderEffectMethodSetUniformMatrix3(void)
416 TestApplication application;
418 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
419 DALI_TEST_CHECK( effect );
421 BitmapImage image = CreateBitmapImage();
423 Matrix3 matIdentity(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
424 effect.SetUniform( "uMatrix3", matIdentity );
426 ImageActor actor = ImageActor::New( image );
427 actor.SetSize( 100.0f, 100.0f );
428 actor.SetName("TestImageFilenameActor");
429 actor.SetShaderEffect(effect);
430 Stage::GetCurrent().Add(actor);
432 application.SendNotification();
433 application.Render();
435 DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue("uMatrix3", matIdentity) );
439 int UtcDaliShaderEffectMethodSetUniformViewport(void)
441 TestApplication application;
443 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
444 DALI_TEST_CHECK( effect );
446 BitmapImage image = CreateBitmapImage();
448 ImageActor actor = ImageActor::New( image );
449 actor.SetSize( 100.0f, 100.0f );
450 actor.SetName("TestImageFilenameActor");
451 actor.SetShaderEffect(effect);
452 Stage::GetCurrent().Add(actor);
454 effect.SetUniform( "uVec2", Vector2( 0.0f, 0.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
455 effect.SetUniform( "uVec2Dir", Vector2( 1.0f, 2.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_DIRECTION );
457 application.SendNotification();
458 application.Render();
460 const Vector2& stageSize(Stage::GetCurrent().GetSize());
462 DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "uVec2", Vector2( stageSize.x/2, -stageSize.y/2 ) ) );
464 DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "uVec2Dir", Vector2( -1.0f, 2.0f ) ) );
466 // change coordinate types
467 effect.SetUniform( "uVec2", Vector2( 0.1f, 0.2f ), ShaderEffect::COORDINATE_TYPE_DEFAULT );
468 effect.SetUniform( "uVec2Dir", Vector2( 1.0f, 2.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
469 actor.SetPixelArea( ImageActor::PixelArea( 0, 0, 10, 10 ) );
471 application.SendNotification();
472 application.Render();
475 application.GetGlAbstraction().GetUniformValue( "uVec2", outValue );
476 DALI_TEST_EQUALS( outValue, Vector2( 0.1f, 0.2f ), TEST_LOCATION );
478 application.GetGlAbstraction().GetUniformValue( "uVec2Dir", outValue );
479 DALI_TEST_EQUALS( outValue, Vector2( stageSize.x *.5f - 1.f, -stageSize.y * .5f + 2.f), TEST_LOCATION );
484 int UtcDaliShaderEffectMethodSetEffectImage(void)
486 TestApplication application;
488 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
489 DALI_TEST_CHECK( effect );
491 BitmapImage image = CreateBitmapImage();
493 effect.SetEffectImage(image);
495 ImageActor actor = ImageActor::New( image );
496 actor.SetSize( 100.0f, 100.0f );
497 actor.SetName("TestImageFilenameActor");
498 actor.SetShaderEffect(effect);
499 Stage::GetCurrent().Add(actor);
501 application.SendNotification();
502 application.Render(16);
503 application.SendNotification();
504 application.Render(16);
505 application.SendNotification();
507 GLuint programId, uniformId;
508 bool uniformWasSet = application.GetGlAbstraction().GetUniformIds( "sEffect", programId, uniformId );
509 // we dont care about the value of the sampler uniform as thats internal to DALi core and subject to change
510 DALI_TEST_CHECK( uniformWasSet );
514 int UtcDaliShaderEffectMethodSetEffectImageAndDelete(void)
516 TestApplication application;
518 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
520 BitmapImage effectImage = CreateBitmapImage();
521 effect.SetEffectImage(effectImage);
523 ImageActor actor = ImageActor::New();
525 actor.SetShaderEffect(effect);
528 Stage::GetCurrent().Add(actor);
530 // do an update / render cycle
531 application.SendNotification();
532 application.Render(16);
533 application.SendNotification();
534 application.Render(16);
535 application.SendNotification();
536 application.Render(16);
538 printf("removing image actor from stage and Reseting handle\n");
539 Stage::GetCurrent().Remove(actor);
542 tet_printf("### Update & Render \n");
544 application.SendNotification();
545 application.Render(16);
547 tet_printf("#### Update Only \n");
549 tet_printf("effectImage.Reset \n");
551 // this releases the effect texture resource,
552 // Update will send a DispatchDiscardTexture message to render
554 application.SendNotification();
555 application.UpdateOnly(16);
557 tet_printf("#### Update Only \n");
559 // at this point shader is deleted, during clear discard queue
560 // and it sends a Shader:: DispatchRemoveObserver message to render thread
561 application.UpdateOnly(16);
563 tet_printf("#### Render Only \n");
564 // This is where it used to crash, there is a message in the queue to perform DispatchDiscardTexture
565 // which tries to call observer->TextureDiscarded, where observer == shader that was deleted
566 // in previous update.
567 application.RenderOnly();
570 // process the discard texture message
571 application.RenderOnly();
572 application.SendNotification();
573 application.Render(16);
575 tet_result(TET_PASS);
580 int UtcDaliShaderEffectMethodApplyConstraint(void)
582 // Test whether Shader's uniform can be constrained to a stationary constraint.
583 TestApplication application;
585 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
586 DALI_TEST_CHECK( effect );
588 BitmapImage image = CreateBitmapImage();
590 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
592 ImageActor actor = ImageActor::New( image );
593 actor.SetSize( 100.0f, 100.0f );
594 actor.SetName("TestImageFilenameActor");
595 actor.SetShaderEffect(effect);
596 Stage::GetCurrent().Add(actor);
598 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
600 application.SendNotification();
601 application.Render();
603 // Test effects of SetUniform...
605 application.GetGlAbstraction().CheckUniformValue(
606 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
608 Constraint constraint = Constraint::New<Vector3>( uVecProperty,
609 TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
611 effect.ApplyConstraint(constraint);
613 application.SendNotification();
614 application.Render();
616 // Test effects of Constraint.
618 application.GetGlAbstraction().CheckUniformValue(
619 "uVec3", Vector3( 4.0f, 9.0f, 16.0f ) ) );
624 int UtcDaliShaderEffectMethodApplyConstraintFromActor(void)
626 // Test whether Shader's uniform can be constrained to Actor's position.
627 TestApplication application;
629 const Vector3 targetPosition = Vector3( 100.0f, 70.0f, 20.0f);
631 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
632 DALI_TEST_CHECK( effect );
634 BitmapImage image = CreateBitmapImage();
636 effect.SetUniform( "uVec3", Vector3( 50.0f, 25.0f, 0.0f ) );
638 ImageActor actor = ImageActor::New( image );
639 actor.SetPosition(targetPosition);
640 actor.SetSize( 100.0f, 100.0f );
641 actor.SetName("TestImageFilenameActor");
642 actor.SetShaderEffect(effect);
643 Stage::GetCurrent().Add(actor);
645 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
647 Constraint constraint = Constraint::New<Vector3>( uVecProperty,
648 Source(actor, Actor::POSITION),
649 TestConstraintFromPositionToVector3() );
651 effect.ApplyConstraint(constraint);
653 application.SendNotification();
654 application.Render();
656 // Test effects of Constraint.
658 application.GetGlAbstraction().CheckUniformValue(
659 "uVec3", targetPosition ) );
663 int UtcDaliShaderEffectMethodApplyConstraintFromActor2(void)
665 // Test whether Shader's uniform can be constrained to Actor's position.
666 // While Actor's position is constrained to another point * 2.0f
667 TestApplication application;
669 const Vector3 targetPosition = Vector3( 25.0f, 36.0f, 49.0f );
671 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
672 DALI_TEST_CHECK( effect );
674 BitmapImage image = CreateBitmapImage();
676 effect.SetUniform( "uVec3", Vector3( 50.0f, 25.0f, 0.0f ) );
678 ImageActor actor = ImageActor::New( image );
679 actor.SetPosition(Vector3( 100.0f, 70.0f, 20.0f));
680 actor.SetSize( 100.0f, 100.0f );
681 actor.SetName("TestImageFilenameActor");
682 actor.SetShaderEffect(effect);
683 Stage::GetCurrent().Add(actor);
685 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
687 Constraint shaderConstraint = Constraint::New<Vector3>( uVecProperty,
688 Source(actor, Actor::POSITION),
689 TestConstraintFromPositionToVector3() );
691 effect.ApplyConstraint(shaderConstraint);
693 Constraint actorConstraint = Constraint::New<Vector3>( Actor::POSITION,
694 TestConstraintToVector3Double(targetPosition) );
696 actor.ApplyConstraint(actorConstraint);
698 application.SendNotification();
699 application.Render();
701 // Test effects of Constraint.
703 application.GetGlAbstraction().CheckUniformValue(
704 "uVec3", targetPosition * 2.0f ) );
708 int UtcDaliShaderEffectMethodApplyConstraintCallback(void)
710 // Test whether Shader's uniform can be constrained to a stationary constraint.
711 TestApplication application;
713 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
714 DALI_TEST_CHECK( effect );
716 BitmapImage image = CreateBitmapImage();
718 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
720 ImageActor actor = ImageActor::New( image );
721 actor.SetSize( 100.0f, 100.0f );
722 actor.SetName("TestImageFilenameActor");
723 actor.SetShaderEffect(effect);
724 Stage::GetCurrent().Add(actor);
726 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
728 application.SendNotification();
729 application.Render();
731 // Test effects of SetUniform...
733 application.GetGlAbstraction().CheckUniformValue(
734 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
736 Constraint constraint = Constraint::New<Vector3>( uVecProperty,
737 TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
739 constraint.SetApplyTime( 10.0f );
741 bool constraintCheck( false );
742 ConstraintAppliedCheck appliedCheck( constraintCheck );
744 // We should receive the "Applied" signal after 10 seconds
745 ActiveConstraint active = effect.ApplyConstraint(constraint);
746 active.AppliedSignal().Connect( &application, appliedCheck );
748 application.SendNotification();
749 application.Render(static_cast<unsigned int>(1000.0f)); // 1 elapsed second
751 // Check signal has not fired
752 application.SendNotification();
753 appliedCheck.CheckSignalNotReceived();
755 application.Render(static_cast<unsigned int>(4000.0f)); // 5 elapsed seconds
757 // Check signal has not fired
758 application.SendNotification();
759 appliedCheck.CheckSignalNotReceived();
761 application.Render(static_cast<unsigned int>(5000.0f - 1.0f)); // <10 elapsed seconds
763 // Check signal has not fired
764 application.SendNotification();
765 appliedCheck.CheckSignalNotReceived();
767 application.Render(static_cast<unsigned int>(2.0f)); // >10 elapsed seconds
769 // Signal should have fired
770 application.SendNotification();
771 appliedCheck.CheckSignalReceived();
773 // Test effects of Constraint.
775 application.GetGlAbstraction().CheckUniformValue(
776 "uVec3", Vector3( 4.0f, 9.0f, 16.0f ) ) );
780 int UtcDaliShaderEffectMethodRemoveConstraints(void)
782 // Test if constrains can be removed before they are ever applyed.
783 TestApplication application;
785 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
786 DALI_TEST_CHECK( effect );
788 BitmapImage image = CreateBitmapImage();
790 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
792 ImageActor actor = ImageActor::New( image );
793 actor.SetSize( 100.0f, 100.0f );
794 actor.SetName("TestImageFilenameActor");
795 actor.SetShaderEffect(effect);
796 Stage::GetCurrent().Add(actor);
798 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
800 application.SendNotification();
801 application.Render();
803 // Test effects of SetUniform...
805 application.GetGlAbstraction().CheckUniformValue(
806 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
808 Constraint constraint = Constraint::New<Vector3>( uVecProperty,
809 TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
811 effect.ApplyConstraint(constraint);
813 // Remove the constraints
814 effect.RemoveConstraints();
816 application.SendNotification();
817 application.Render();
819 // Test effects of Constraint.
821 application.GetGlAbstraction().CheckUniformValue(
822 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
826 int UtcDaliShaderEffectMethodRemoveConstraints2(void)
828 // Test whether Shader's uniform constrains can be removed after they are applyed.
829 TestApplication application;
831 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
832 DALI_TEST_CHECK( effect );
834 BitmapImage image = CreateBitmapImage();
836 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
838 ImageActor actor = ImageActor::New( image );
839 actor.SetSize( 100.0f, 100.0f );
840 actor.SetName("TestImageFilenameActor");
841 actor.SetShaderEffect(effect);
842 Stage::GetCurrent().Add(actor);
844 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
846 application.SendNotification();
847 application.Render();
849 // Test effects of SetUniform...
851 application.GetGlAbstraction().CheckUniformValue(
852 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
854 Constraint constraint = Constraint::New<Vector3>( uVecProperty,
855 TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
857 effect.ApplyConstraint(constraint);
859 application.SendNotification();
860 application.Render();
862 // Reset the value and remove the constraints
863 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
864 effect.RemoveConstraints();
866 application.SendNotification();
867 application.Render();
869 // Test effects of Constraint.
871 application.GetGlAbstraction().CheckUniformValue(
872 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
876 int UtcDaliShaderEffectMethodCreateExtension(void)
878 // Test creation of a shader extension
879 TestApplication aplication;
883 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
884 DALI_TEST_CHECK( effect );
886 TestExtension* extension = new TestExtension ( deleted );
888 effect.AttachExtension( extension );
890 DALI_TEST_CHECK( static_cast<TestExtension&>(effect.GetExtension()).IsAlive() );
893 DALI_TEST_CHECK( deleted );
897 int UtcDaliShaderEffectMethodCreateExtension2(void)
899 // Test creation of a shader extension
902 TestApplication application;
904 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
905 DALI_TEST_CHECK( effect );
907 BitmapImage image = CreateBitmapImage();
909 effect.SetUniform( "uFloat", 1.0f );
911 ImageActor actor = ImageActor::New( image );
912 actor.SetSize( 100.0f, 100.0f );
913 actor.SetName("TestImageFilenameActor");
914 actor.SetShaderEffect(effect);
915 Stage::GetCurrent().Add(actor);
917 application.SendNotification();
918 application.Render();
920 TestExtension* extension = new TestExtension ( deleted );
922 effect.AttachExtension( extension );
924 const ShaderEffect& constEffect(effect);
925 const TestExtension& ext( static_cast<const TestExtension&>(constEffect.GetExtension()) );
927 DALI_TEST_CHECK( ext.IsAlive() );
930 DALI_TEST_CHECK( deleted );
934 int UtcDaliShaderEffectMethodNoExtension(void)
936 TestApplication application;
942 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
943 DALI_TEST_CHECK( effect );
945 // Don't attach extension
946 ShaderEffect::Extension& extension = effect.GetExtension();
949 DALI_TEST_CHECK( false );
951 catch (Dali::DaliException& e)
953 // Tests that a negative test of an assertion succeeds
954 DALI_TEST_PRINT_ASSERT( e );
955 DALI_TEST_CHECK( !effect );
961 int UtcDaliShaderEffectPropertyIndices(void)
963 TestApplication application;
964 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
966 Property::IndexContainer indices;
967 effect.GetPropertyIndices( indices );
968 DALI_TEST_CHECK( ! indices.empty() );
969 DALI_TEST_EQUALS( indices.size(), effect.GetPropertyCount(), TEST_LOCATION );
973 int UtcDaliShaderBinaries(void)
975 TestApplication application;
976 // these will not affect the "first round" of dali render as core is already initialized and it has queried the defaults
977 application.GetGlAbstraction().SetBinaryFormats( 1 );
978 application.GetGlAbstraction().SetNumBinaryFormats( 1 );
979 application.GetGlAbstraction().SetProgramBinaryLength( 1 );
981 GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
983 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
984 DALI_TEST_CHECK( effect );
986 BitmapImage image = CreateBitmapImage();
987 ImageActor actor = ImageActor::New( image );
988 actor.SetSize( 100.0f, 100.0f );
989 actor.SetName("TestImageFilenameActor");
990 actor.SetShaderEffect(effect);
991 Stage::GetCurrent().Add(actor);
993 application.SendNotification();
994 application.Render(16);
995 // binary was not requested by DALi
996 DALI_TEST_CHECK( !(application.GetGlAbstraction().GetProgramBinaryCalled()) );
998 GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
1000 // check that the shader was compiled
1001 DALI_TEST_EQUALS( lastShaderCompiledAfter, lastShaderCompiledBefore + 2, TEST_LOCATION );
1003 // simulate context loss to get core to re-initialize its GL
1004 application.GetCore().ContextDestroyed();
1005 application.GetCore().ContextCreated();
1007 application.SendNotification();
1008 application.Render(16);
1010 // shader is recompiled
1011 GLuint finalShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
1012 // check that the shader was compiled
1013 DALI_TEST_EQUALS( lastShaderCompiledAfter + 2, finalShaderCompiled, TEST_LOCATION );
1015 // binary was requested by DALi
1016 DALI_TEST_CHECK( application.GetGlAbstraction().GetProgramBinaryCalled() );