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 const int GETSOURCE_BUFFER_SIZE = 0x10000;
50 struct TestConstraintToVector3
52 TestConstraintToVector3(Vector3 target)
57 void operator()( Vector3& current, const PropertyInputContainer& /* inputs */ )
65 struct TestConstraintFromPositionToVector3
67 TestConstraintFromPositionToVector3()
71 void operator()( Vector3& current, const PropertyInputContainer& inputs )
73 current = inputs[0]->GetVector3();
77 struct TestConstraintToVector3Double
79 TestConstraintToVector3Double(Vector3 target)
84 void operator()( Vector3& current, const PropertyInputContainer& /* inputs */ )
86 current = mTarget * 2.0f;
92 static const char* TestImageFilename = "icon_wrt.png";
94 Integration::Bitmap* CreateBitmap( unsigned int imageHeight, unsigned int imageWidth, unsigned int initialColor )
96 Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
97 Integration::PixelBuffer* pixbuffer = bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, imageWidth,imageHeight,imageWidth,imageHeight );
98 unsigned int bytesPerPixel = GetBytesPerPixel( Pixel::RGBA8888 );
100 memset( pixbuffer, initialColor , imageHeight*imageWidth*bytesPerPixel);
108 int UtcDaliShaderEffectMethodNew01(void)
110 TestApplication application;
112 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
113 DALI_TEST_CHECK(effect);
117 int UtcDaliShaderEffectMethodNew02(void)
119 TestApplication application;
125 // New must be called to create a ShaderEffect or it wont be valid.
126 effect.SetUniform( "uUniform", 0 );
127 DALI_TEST_CHECK( false );
129 catch (Dali::DaliException& e)
131 // Tests that a negative test of an assertion succeeds
132 DALI_TEST_PRINT_ASSERT( e );
133 DALI_TEST_CHECK( !effect );
138 int UtcDaliShaderEffectMethodNew05(void)
140 TestApplication application;
142 // heap constructor / destructor
143 DefaultFunctionCoverage<ShaderEffect> shaderEffect;
148 int UtcDaliShaderEffectMethodDownCast(void)
150 TestApplication application;
151 tet_infoline("Testing Dali::ShaderEffect::DownCast()");
153 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
155 BaseHandle object(effect);
157 ShaderEffect effect2 = ShaderEffect::DownCast(object);
158 DALI_TEST_CHECK(effect2);
160 ShaderEffect effect3 = DownCast< ShaderEffect >(object);
161 DALI_TEST_CHECK(effect3);
163 BaseHandle unInitializedObject;
164 ShaderEffect effect4 = ShaderEffect::DownCast(unInitializedObject);
165 DALI_TEST_CHECK(!effect4);
167 ShaderEffect effect5 = DownCast< ShaderEffect >(unInitializedObject);
168 DALI_TEST_CHECK(!effect5);
172 int UtcDaliShaderEffectMethodDelete01(void)
174 TestApplication application;
176 // get the default shaders built, this is not required but makes it
177 // easier to debug the TET case and isolate the custom shader compilation.
178 application.SendNotification();
179 application.Render();
181 GLuint beforeShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
183 // create a new shader effect
184 // the vertex and fragment shader will be cached in the ShaderFactory
185 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
187 // destroy the shader effect
190 // Create the same shader effect again, this should now use the cached version
191 // held in the shader factory
192 effect= ShaderEffect::New( VertexSource, FragmentSource );
194 // Compile the shader effect
195 application.SendNotification();
196 application.Render();
198 GLuint lastShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
199 // no shaders were compiled as they are now compiled on demand and this shader was not used
200 DALI_TEST_EQUALS( beforeShaderCompiled, lastShaderCompiled, TEST_LOCATION );
205 int UtcDaliShaderEffectMethodSetUniformFloat(void)
207 TestApplication application;
209 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
210 DALI_TEST_CHECK( effect );
212 BufferImage image = CreateBufferImage();
214 effect.SetUniform( "uFloat", 1.0f );
216 ImageActor actor = ImageActor::New( image );
217 actor.SetSize( 100.0f, 100.0f );
218 actor.SetName("TestImageFilenameActor");
219 actor.SetShaderEffect(effect);
220 Stage::GetCurrent().Add(actor);
222 application.SendNotification();
223 application.Render();
226 application.GetGlAbstraction().CheckUniformValue(
231 int UtcDaliShaderEffectMethodSetUniformVector2(void)
233 TestApplication application;
235 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
236 DALI_TEST_CHECK( effect );
238 BufferImage image = CreateBufferImage();
240 effect.SetUniform( "uVec2", Vector2( 2.0f, 3.0f ) );
242 ImageActor actor = ImageActor::New( image );
243 actor.SetSize( 100.0f, 100.0f );
244 actor.SetName("TestImageFilenameActor");
245 actor.SetShaderEffect(effect);
246 Stage::GetCurrent().Add(actor);
248 application.SendNotification();
249 application.Render();
252 application.GetGlAbstraction().CheckUniformValue(
253 "uVec2", Vector2( 2.0f, 3.0f ) ) );
257 int UtcDaliShaderEffectMethodSetUniformVector3(void)
259 TestApplication application;
261 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
262 DALI_TEST_CHECK( effect );
264 BufferImage image = CreateBufferImage();
266 effect.SetUniform( "uVec3", Vector3( 4.0f, 5.0f, 6.0f ) );
268 ImageActor actor = ImageActor::New( image );
269 actor.SetSize( 100.0f, 100.0f );
270 actor.SetName("TestImageFilenameActor");
271 actor.SetShaderEffect(effect);
272 Stage::GetCurrent().Add(actor);
274 application.SendNotification();
275 application.Render();
278 application.GetGlAbstraction().CheckUniformValue(
279 "uVec3", Vector3( 4.0f, 5.0f, 6.0f ) ) );
283 int UtcDaliShaderEffectMethodSetUniformVector4(void)
285 TestApplication application;
287 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
288 DALI_TEST_CHECK( effect );
290 BufferImage image = CreateBufferImage();
292 effect.SetUniform( "uVec4", Vector4( 7.0f, 8.0f, 9.0f, 10.0f ) );
294 ImageActor actor = ImageActor::New( image );
295 actor.SetSize( 100.0f, 100.0f );
296 actor.SetName("TestImageFilenameActor");
297 actor.SetShaderEffect(effect);
298 Stage::GetCurrent().Add(actor);
300 application.SendNotification();
301 application.Render();
304 application.GetGlAbstraction().CheckUniformValue(
305 "uVec4", Vector4( 7.0f, 8.0f, 9.0f, 10.0f ) ) );
309 int UtcDaliShaderEffectMethodSetUniformMatrix(void)
311 TestApplication application;
313 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
314 DALI_TEST_CHECK( effect );
316 BufferImage image = CreateBufferImage();
318 effect.SetUniform( "uModelView", Matrix::IDENTITY );
320 ImageActor actor = ImageActor::New( image );
321 actor.SetSize( 100.0f, 100.0f );
322 actor.SetName("TestImageFilenameActor");
323 actor.SetShaderEffect(effect);
324 Stage::GetCurrent().Add(actor);
326 application.SendNotification();
327 application.Render();
330 application.GetGlAbstraction().CheckUniformValue(
331 "uModelView", Matrix::IDENTITY ) );
335 int UtcDaliShaderEffectMethodSetUniformMatrix3(void)
337 TestApplication application;
339 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
340 DALI_TEST_CHECK( effect );
342 BufferImage image = CreateBufferImage();
344 Matrix3 matIdentity(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
345 effect.SetUniform( "uMatrix3", matIdentity );
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();
356 DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue("uMatrix3", matIdentity) );
360 int UtcDaliShaderEffectMethodSetUniformViewport(void)
362 TestApplication application;
364 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
365 DALI_TEST_CHECK( effect );
367 BufferImage image = CreateBufferImage();
369 ImageActor actor = ImageActor::New( image );
370 actor.SetSize( 100.0f, 100.0f );
371 actor.SetName("TestImageFilenameActor");
372 actor.SetShaderEffect(effect);
373 Stage::GetCurrent().Add(actor);
375 effect.SetUniform( "uVec2", Vector2( 0.0f, 0.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
376 effect.SetUniform( "uVec2Dir", Vector2( 1.0f, 2.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_DIRECTION );
378 application.SendNotification();
379 application.Render();
381 DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "uVec2", Vector2( 0.0f, 0.0f ) ) ); //ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION deprecated
382 DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "uVec2Dir", Vector2( 1.0f, 2.0f ) ) ); //ShaderEffect::COORDINATE_TYPE_VIEWPORT_DIRECTION deprecated
384 // change coordinate types
385 effect.SetUniform( "uVec2", Vector2( 0.1f, 0.2f ), ShaderEffect::COORDINATE_TYPE_DEFAULT );
386 effect.SetUniform( "uVec2Dir", Vector2( 1.0f, 2.0f ), ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
387 actor.SetPixelArea( ImageActor::PixelArea( 0, 0, 10, 10 ) );
389 application.SendNotification();
390 application.Render();
393 application.GetGlAbstraction().GetUniformValue( "uVec2", outValue );
394 DALI_TEST_EQUALS( outValue, Vector2( 0.1f, 0.2f ), TEST_LOCATION );
396 application.GetGlAbstraction().GetUniformValue( "uVec2Dir", outValue );
397 DALI_TEST_EQUALS( outValue, Vector2( 1.0f, 2.0f ), TEST_LOCATION ); //ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION deprecated
402 int UtcDaliShaderEffectMethodSetEffectImage(void)
404 TestApplication application;
406 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
407 DALI_TEST_CHECK( effect );
409 BufferImage image = CreateBufferImage();
411 effect.SetEffectImage(image);
413 ImageActor actor = ImageActor::New( image );
414 actor.SetSize( 100.0f, 100.0f );
415 actor.SetName("TestImageFilenameActor");
416 actor.SetShaderEffect(effect);
417 Stage::GetCurrent().Add(actor);
419 application.SendNotification();
420 application.Render(16);
421 application.SendNotification();
422 application.Render(16);
423 application.SendNotification();
425 GLuint programId, uniformId;
426 bool uniformWasSet = application.GetGlAbstraction().GetUniformIds( "sEffect", programId, uniformId );
427 // we dont care about the value of the sampler uniform as thats internal to DALi core and subject to change
428 DALI_TEST_CHECK( uniformWasSet );
432 int UtcDaliShaderEffectMethodSetEffectImageAndDelete(void)
434 TestApplication application;
436 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
438 BufferImage effectImage = CreateBufferImage();
439 effect.SetEffectImage(effectImage);
441 ImageActor actor = ImageActor::New();
443 actor.SetShaderEffect(effect);
446 Stage::GetCurrent().Add(actor);
448 // do an update / render cycle
449 application.SendNotification();
450 application.Render(16);
451 application.SendNotification();
452 application.Render(16);
453 application.SendNotification();
454 application.Render(16);
456 printf("removing image actor from stage and Reseting handle\n");
457 Stage::GetCurrent().Remove(actor);
460 tet_printf("### Update & Render \n");
462 application.SendNotification();
463 application.Render(16);
465 tet_printf("#### Update Only \n");
467 tet_printf("effectImage.Reset \n");
469 // this releases the effect texture resource,
470 // Update will send a DispatchDiscardTexture message to render
472 application.SendNotification();
473 application.UpdateOnly(16);
475 tet_printf("#### Update Only \n");
477 // at this point shader is deleted, during clear discard queue
478 // and it sends a Shader:: DispatchRemoveObserver message to render thread
479 application.UpdateOnly(16);
481 tet_printf("#### Render Only \n");
482 // This is where it used to crash, there is a message in the queue to perform DispatchDiscardTexture
483 // which tries to call observer->TextureDiscarded, where observer == shader that was deleted
484 // in previous update.
485 application.RenderOnly();
488 // process the discard texture message
489 application.RenderOnly();
490 application.SendNotification();
491 application.Render(16);
493 tet_result(TET_PASS);
498 int UtcDaliShaderEffectMethodApplyConstraint(void)
500 // Test whether Shader's uniform can be constrained to a stationary constraint.
501 TestApplication application;
503 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
504 DALI_TEST_CHECK( effect );
506 BufferImage image = CreateBufferImage();
508 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
510 ImageActor actor = ImageActor::New( image );
511 actor.SetSize( 100.0f, 100.0f );
512 actor.SetName("TestImageFilenameActor");
513 actor.SetShaderEffect(effect);
514 Stage::GetCurrent().Add(actor);
516 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
518 application.SendNotification();
519 application.Render();
521 // Test effects of SetUniform...
523 application.GetGlAbstraction().CheckUniformValue(
524 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
526 Constraint constraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
529 application.SendNotification();
530 application.Render();
532 // Test effects of Constraint.
534 application.GetGlAbstraction().CheckUniformValue(
535 "uVec3", Vector3( 4.0f, 9.0f, 16.0f ) ) );
539 int UtcDaliShaderEffectMethodApplyConstraintOffStage(void)
541 // The same test as UtcDaliShaderEffectMethodApplyConstraint,
542 // except the Actor is off-stage when the constraint is applied to the shader
543 TestApplication application;
545 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
546 DALI_TEST_CHECK( effect );
548 BufferImage image = CreateBufferImage();
550 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
552 ImageActor actor = ImageActor::New( image );
553 actor.SetSize( 100.0f, 100.0f );
554 actor.SetName("TestImageFilenameActor");
555 actor.SetShaderEffect(effect);
556 // Note - Do not add actor to stage here
558 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
560 Constraint constraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
563 // Note - Now we add the actor (after constraint was applied to the shader)
564 Stage::GetCurrent().Add(actor);
566 application.SendNotification();
567 application.Render();
569 // Test effects of Constraint.
571 application.GetGlAbstraction().CheckUniformValue(
572 "uVec3", Vector3( 4.0f, 9.0f, 16.0f ) ) );
576 int UtcDaliShaderEffectMethodApplyConstraintFromActor(void)
578 // Test whether Shader's uniform can be constrained to Actor's position.
579 TestApplication application;
581 const Vector3 targetPosition = Vector3( 100.0f, 70.0f, 20.0f);
583 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
584 DALI_TEST_CHECK( effect );
586 BufferImage image = CreateBufferImage();
588 effect.SetUniform( "uVec3", Vector3( 50.0f, 25.0f, 0.0f ) );
590 ImageActor actor = ImageActor::New( image );
591 actor.SetPosition(targetPosition);
592 actor.SetSize( 100.0f, 100.0f );
593 actor.SetName("TestImageFilenameActor");
594 actor.SetShaderEffect(effect);
595 Stage::GetCurrent().Add(actor);
597 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
599 Constraint constraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintFromPositionToVector3() );
600 constraint.AddSource( Source( actor, Actor::Property::POSITION ) );
603 application.SendNotification();
604 application.Render();
606 // Test effects of Constraint.
608 application.GetGlAbstraction().CheckUniformValue(
609 "uVec3", targetPosition ) );
613 int UtcDaliShaderEffectMethodApplyConstraintFromActor2(void)
615 // Test whether Shader's uniform can be constrained to Actor's position.
616 // While Actor's position is constrained to another point * 2.0f
617 TestApplication application;
619 const Vector3 targetPosition = Vector3( 25.0f, 36.0f, 49.0f );
621 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
622 DALI_TEST_CHECK( effect );
624 BufferImage image = CreateBufferImage();
626 effect.SetUniform( "uVec3", Vector3( 50.0f, 25.0f, 0.0f ) );
628 ImageActor actor = ImageActor::New( image );
629 actor.SetPosition(Vector3( 100.0f, 70.0f, 20.0f));
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 Constraint shaderConstraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintFromPositionToVector3() );
638 shaderConstraint.AddSource( Source(actor, Actor::Property::POSITION) );
639 shaderConstraint.Apply();
641 Constraint actorConstraint = Constraint::New<Vector3>( actor, Actor::Property::POSITION, TestConstraintToVector3Double(targetPosition) );
642 actorConstraint.Apply();
644 application.SendNotification();
645 application.Render();
647 // Test effects of Constraint.
649 application.GetGlAbstraction().CheckUniformValue(
650 "uVec3", targetPosition * 2.0f ) );
654 int UtcDaliShaderEffectMethodRemoveConstraints(void)
656 // Test if constrains can be removed before they are ever applyed.
657 TestApplication application;
659 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
660 DALI_TEST_CHECK( effect );
662 BufferImage image = CreateBufferImage();
664 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
666 ImageActor actor = ImageActor::New( image );
667 actor.SetSize( 100.0f, 100.0f );
668 actor.SetName("TestImageFilenameActor");
669 actor.SetShaderEffect(effect);
670 Stage::GetCurrent().Add(actor);
672 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
674 application.SendNotification();
675 application.Render();
677 // Test effects of SetUniform...
679 application.GetGlAbstraction().CheckUniformValue(
680 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
682 Constraint constraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
685 // Remove the constraints
686 effect.RemoveConstraints();
688 application.SendNotification();
689 application.Render();
691 // Test effects of Constraint.
693 application.GetGlAbstraction().CheckUniformValue(
694 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
698 int UtcDaliShaderEffectMethodRemoveConstraints2(void)
700 // Test whether Shader's uniform constrains can be removed after they are applyed.
701 TestApplication application;
703 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
704 DALI_TEST_CHECK( effect );
706 BufferImage image = CreateBufferImage();
708 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
710 ImageActor actor = ImageActor::New( image );
711 actor.SetSize( 100.0f, 100.0f );
712 actor.SetName("TestImageFilenameActor");
713 actor.SetShaderEffect(effect);
714 Stage::GetCurrent().Add(actor);
716 Property::Index uVecProperty = effect.GetPropertyIndex("uVec3");
718 application.SendNotification();
719 application.Render();
721 // Test effects of SetUniform...
723 application.GetGlAbstraction().CheckUniformValue(
724 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
726 Constraint constraint = Constraint::New<Vector3>( effect, uVecProperty, TestConstraintToVector3(Vector3(4.0f, 9.0f, 16.0f)) );
729 application.SendNotification();
730 application.Render();
732 // Reset the value and remove the constraints
733 effect.SetUniform( "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) );
734 effect.RemoveConstraints();
736 application.SendNotification();
737 application.Render();
739 // Test effects of Constraint.
741 application.GetGlAbstraction().CheckUniformValue(
742 "uVec3", Vector3( 1.0f, 2.0f, 3.0f ) ) );
746 int UtcDaliShaderEffectPropertyIndices(void)
748 TestApplication application;
749 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
751 Property::IndexContainer indices;
752 effect.GetPropertyIndices( indices );
753 DALI_TEST_CHECK( indices.Size() );
754 DALI_TEST_EQUALS( indices.Size(), effect.GetPropertyCount(), TEST_LOCATION );
758 int UtcDaliShaderBinaries(void)
760 TestApplication application;
761 // these will not affect the "first round" of dali render as core is already initialized and it has queried the defaults
762 application.GetGlAbstraction().SetBinaryFormats( 1 );
763 application.GetGlAbstraction().SetNumBinaryFormats( 1 );
764 application.GetGlAbstraction().SetProgramBinaryLength( 1 );
766 GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
768 ShaderEffect effect = ShaderEffect::New( VertexSource, FragmentSource );
769 DALI_TEST_CHECK( effect );
771 BufferImage image = CreateBufferImage();
772 ImageActor actor = ImageActor::New( image );
773 actor.SetSize( 100.0f, 100.0f );
774 actor.SetName("TestImageFilenameActor");
775 actor.SetShaderEffect(effect);
776 Stage::GetCurrent().Add(actor);
778 application.SendNotification();
779 application.Render(16);
780 // binary was not requested by DALi
781 DALI_TEST_CHECK( !(application.GetGlAbstraction().GetProgramBinaryCalled()) );
783 GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
785 // check that the shader was compiled
786 DALI_TEST_EQUALS( lastShaderCompiledAfter, lastShaderCompiledBefore + 2, TEST_LOCATION );
788 // simulate context loss to get core to re-initialize its GL
789 application.GetCore().ContextDestroyed();
790 application.GetCore().ContextCreated();
792 application.SendNotification();
793 application.Render(16);
795 // shader is recompiled
796 GLuint finalShaderCompiled = application.GetGlAbstraction().GetLastShaderCompiled();
797 // check that the shader was compiled
798 DALI_TEST_EQUALS( lastShaderCompiledAfter + 2, finalShaderCompiled, TEST_LOCATION );
800 // binary was requested by DALi
801 DALI_TEST_CHECK( application.GetGlAbstraction().GetProgramBinaryCalled() );
806 int UtcDaliShaderEffectFromPropertiesP(void)
808 TestApplication application;
809 tet_infoline("UtcDaliShaderEffectFromProperties01()");
811 std::string fragmentShaderPrefix = "#define TEST_FS 1\n#extension GL_OES_standard_derivatives : enable";
812 std::string vertexShaderPrefix = "#define TEST_VS 1";
813 std::string vertexShader(VertexSource);
814 std::string fragmentShader(FragmentSource);
816 // Call render to compile default shaders.
817 application.SendNotification();
818 application.Render();
820 GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
822 // create from type registry
824 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
825 DALI_TEST_CHECK( typeInfo );
826 ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
827 DALI_TEST_CHECK( effect );
829 Property::Value programValue = Property::Value(Property::MAP);
830 Property::Map* programMap = programValue.GetMap();
831 DALI_TEST_CHECK( programMap );
833 programMap->Insert("vertex", vertexShader);
834 programMap->Insert("fragment", fragmentShader);
836 programMap->Insert("vertexPrefix", vertexShaderPrefix);
837 programMap->Insert("fragmentPrefix", fragmentShaderPrefix);
839 effect.SetProperty(effect.GetPropertyIndex("program"), programValue);
841 Property::Value imageValue = Property::Value(Property::MAP);
842 Property::Map* imageMap = imageValue.GetMap();
843 DALI_TEST_CHECK( imageMap );
844 imageMap->Insert("filename", Property::Value(TestImageFilename));
845 effect.SetProperty(effect.GetPropertyIndex("image"), imageValue);
847 // do a update & render to get the image request
848 application.SendNotification();
849 application.Render();
851 Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
853 Integration::Bitmap* bitmap = CreateBitmap( 10, 10, 0xFF );
854 Integration::ResourcePointer resourcePtr(bitmap);
855 TestPlatformAbstraction& platform = application.GetPlatform();
856 platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resourcePtr);
858 BufferImage image(CreateBufferImage());
859 ImageActor actor = ImageActor::New( image );
860 actor.SetSize( 100.0f, 100.0f );
861 actor.SetName("TestImageFilenameActor");
862 actor.SetShaderEffect(effect);
863 Stage::GetCurrent().Add(actor);
865 application.SendNotification();
866 application.Render();
867 GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
869 // we should have compiled 2 shaders.
870 DALI_TEST_EQUALS(lastShaderCompiledAfter, lastShaderCompiledBefore + 2, TEST_LOCATION );
872 std::string actualVertexShader = application.GetGlAbstraction().GetShaderSource( lastShaderCompiledBefore + 1 );
873 DALI_TEST_EQUALS( vertexShaderPrefix, actualVertexShader.substr( 0, vertexShaderPrefix.length() ), TEST_LOCATION );
874 DALI_TEST_EQUALS( vertexShader, actualVertexShader.substr( actualVertexShader.length() - vertexShader.length() ), TEST_LOCATION );
876 std::string actualFragmentShader = application.GetGlAbstraction().GetShaderSource( lastShaderCompiledBefore + 2 );
877 DALI_TEST_EQUALS( fragmentShaderPrefix, actualFragmentShader.substr( 0, fragmentShaderPrefix.length() ), TEST_LOCATION );
878 DALI_TEST_EQUALS( fragmentShader, actualFragmentShader.substr( actualFragmentShader.length() - fragmentShader.length() ), TEST_LOCATION );
883 int UtcDaliShaderEffectFromPropertiesN(void)
887 TestApplication application;
888 tet_infoline("UtcDaliShaderEffectFromProperties02()");
890 // Call render to compile default shaders.
891 application.SendNotification();
892 application.Render();
894 // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
895 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
896 DALI_TEST_CHECK( typeInfo );
897 ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
898 DALI_TEST_CHECK( effect );
900 Property::Value programValue = Property::Value(Property::MAP);
901 Property::Map* programMap = programValue.GetMap();
902 DALI_TEST_CHECK( programMap );
904 programMap->Insert("vertex", std::string(VertexSource));
905 programMap->Insert("fragment", std::string(FragmentSource));
907 // use wrong index on purpose
908 effect.SetProperty(effect.GetPropertyIndex("program") + 1, programValue );
910 tet_result( TET_FAIL );
912 catch(Dali::DaliException& e)
914 DALI_TEST_PRINT_ASSERT( e );
919 int UtcDaliShaderEffectFromProperties2N(void)
923 TestApplication application;
924 tet_infoline("UtcDaliShaderEffectFromProperties03()");
926 // Call render to compile default shaders.
927 application.SendNotification();
928 application.Render();
930 // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
931 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
932 DALI_TEST_CHECK( typeInfo );
933 ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
934 DALI_TEST_CHECK( effect );
937 effect.SetProperty( effect.GetPropertyIndex("geometryHints"), "HINT_2" );
939 tet_result( TET_FAIL );
941 catch(Dali::DaliException& e)
943 DALI_TEST_PRINT_ASSERT( e );