2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://floralicense.org/license/
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
20 #include <dali/dali.h>
21 #include <dali-test-suite-utils.h>
28 static const char* VertexSource =
31 " gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);\n"
32 " vTexCoord = aTexCoord;\n"
35 static const char* FragmentSource =
38 " gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n"
41 static const char* FragmentSourceUsingExtensions =
44 " float floatValue = 0.5f;\n"
45 " float test = fwidth(floatValue);\n"
46 " gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n"
47 " gl_FragColor.a *= test;\n"
50 const int GETSOURCE_BUFFER_SIZE = 0x10000;
52 static const char* TestImageFilename = "icon_wrt.png";
54 } // Anonymous namespace
57 int UtcDaliShaderEffectFromProperties01(void)
59 TestApplication application;
60 tet_infoline("UtcDaliShaderEffectFromProperties01()");
62 std::string fragmentShaderPrefix = "#define TEST_FS 1\n#extension GL_OES_standard_derivatives : enable";
63 std::string vertexShaderPrefix = "#define TEST_VS 1";
65 // Call render to compile default shaders.
66 application.SendNotification();
71 GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
73 // create from type registry
75 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
76 DALI_TEST_CHECK( typeInfo );
77 ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
78 DALI_TEST_CHECK( effect );
80 Property::Value programMap = Property::Value(Property::MAP);
82 programMap.SetValue("vertex", std::string(VertexSource));
83 programMap.SetValue("fragment", std::string(FragmentSource));
85 programMap.SetValue("vertex-prefix", std::string(fragmentShaderPrefix));
86 programMap.SetValue("fragment-prefix", std::string(vertexShaderPrefix));
88 programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
90 effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
92 Property::Value imageMap = Property::Value(Property::MAP);
93 imageMap.SetValue("filename", Property::Value(TestImageFilename));
95 effect.SetProperty(effect.GetPropertyIndex("image"), imageMap);
97 BitmapImage image(CreateBitmapImage());
99 ImageActor actor = ImageActor::New( image );
100 actor.SetSize( 100.0f, 100.0f );
101 actor.SetName("TestImageFilenameActor");
102 actor.SetShaderEffect(effect);
103 Stage::GetCurrent().Add(actor);
105 application.SendNotification();
106 application.Render();
107 GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
108 bool testResult = false;
110 // we should have compiled 4 shaders.
111 DALI_TEST_CHECK(lastShaderCompiledAfter - lastShaderCompiledBefore == 4);
112 if (lastShaderCompiledAfter - lastShaderCompiledBefore == 4)
114 char testVertexSourceResult[GETSOURCE_BUFFER_SIZE];
115 char testFragmentSourceResult[GETSOURCE_BUFFER_SIZE];
117 // we are interested in the first two.
118 GLuint vertexShaderId = lastShaderCompiledBefore + 1;
119 GLuint fragmentShaderId = lastShaderCompiledBefore + 2;
121 GLsizei lengthVertexResult;
122 GLsizei lengthFragmentResult;
124 application.GetGlAbstraction().GetShaderSource(vertexShaderId, GETSOURCE_BUFFER_SIZE, &lengthVertexResult, testVertexSourceResult);
125 application.GetGlAbstraction().GetShaderSource(fragmentShaderId, GETSOURCE_BUFFER_SIZE, &lengthFragmentResult, testFragmentSourceResult);
127 int vertexShaderHasPrefix = strncmp(testVertexSourceResult, "#define ", strlen("#define "));
128 int fragmentShaderHasPrefix = strncmp(testFragmentSourceResult, "#define ", strlen("#define "));
129 testResult = (vertexShaderHasPrefix == 0) && (fragmentShaderHasPrefix == 0);
131 DALI_TEST_CHECK(testResult);
135 int UtcDaliShaderEffectFromProperties02(void)
139 TestApplication application;
140 tet_infoline("UtcDaliShaderEffectFromProperties02()");
142 // Call render to compile default shaders.
143 application.SendNotification();
144 application.Render();
145 application.Render();
146 application.Render();
148 // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
149 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
150 DALI_TEST_CHECK( typeInfo );
151 ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
152 DALI_TEST_CHECK( effect );
154 Property::Value programMap = Property::Value(Property::MAP);
156 programMap.SetValue("vertex", std::string(VertexSource));
157 programMap.SetValue("fragment", std::string(FragmentSource));
159 // programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
161 programMap.SetValue("geometry-type", GeometryType( GEOMETRY_TYPE_IMAGE ));
163 effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
165 tet_result( TET_FAIL );
167 catch(Dali::DaliException& e)
169 tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
174 int UtcDaliShaderEffectFromProperties03(void)
178 TestApplication application;
179 tet_infoline("UtcDaliShaderEffectFromProperties03()");
181 // Call render to compile default shaders.
182 application.SendNotification();
183 application.Render();
184 application.Render();
185 application.Render();
187 // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
188 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
189 DALI_TEST_CHECK( typeInfo );
190 ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
191 DALI_TEST_CHECK( effect );
194 effect.SetProperty( effect.GetPropertyIndex("geometry-hints"), "HINT_2" );
196 tet_result( TET_FAIL );
198 catch(Dali::DaliException& e)
200 tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());