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>
29 static const char* VertexSource =
32 " gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);\n"
33 " vTexCoord = aTexCoord;\n"
36 static const char* FragmentSource =
39 " gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n"
42 const int GETSOURCE_BUFFER_SIZE = 0x10000;
44 static const char* TestImageFilename = "icon_wrt.png";
46 } // Anonymous namespace
49 int UtcDaliShaderEffectFromProperties01(void)
51 TestApplication application;
52 tet_infoline("UtcDaliShaderEffectFromProperties01()");
54 std::string fragmentShaderPrefix = "#define TEST_FS 1\n#extension GL_OES_standard_derivatives : enable";
55 std::string vertexShaderPrefix = "#define TEST_VS 1";
57 // Call render to compile default shaders.
58 application.SendNotification();
63 GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
65 // create from type registry
67 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
68 DALI_TEST_CHECK( typeInfo );
69 ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
70 DALI_TEST_CHECK( effect );
72 Property::Value programMap = Property::Value(Property::MAP);
74 programMap.SetValue("vertex", std::string(VertexSource));
75 programMap.SetValue("fragment", std::string(FragmentSource));
77 programMap.SetValue("vertex-prefix", std::string(fragmentShaderPrefix));
78 programMap.SetValue("fragment-prefix", std::string(vertexShaderPrefix));
80 programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
82 effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
84 Property::Value imageMap = Property::Value(Property::MAP);
85 imageMap.SetValue("filename", Property::Value(TestImageFilename));
87 effect.SetProperty(effect.GetPropertyIndex("image"), imageMap);
89 BitmapImage image(CreateBitmapImage());
91 ImageActor actor = ImageActor::New( image );
92 actor.SetSize( 100.0f, 100.0f );
93 actor.SetName("TestImageFilenameActor");
94 actor.SetShaderEffect(effect);
95 Stage::GetCurrent().Add(actor);
97 application.SendNotification();
99 GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
100 bool testResult = false;
102 // we should have compiled 4 shaders.
103 DALI_TEST_CHECK(lastShaderCompiledAfter - lastShaderCompiledBefore == 4);
104 if (lastShaderCompiledAfter - lastShaderCompiledBefore == 4)
106 char testVertexSourceResult[GETSOURCE_BUFFER_SIZE];
107 char testFragmentSourceResult[GETSOURCE_BUFFER_SIZE];
109 // we are interested in the first two.
110 GLuint vertexShaderId = lastShaderCompiledBefore + 1;
111 GLuint fragmentShaderId = lastShaderCompiledBefore + 2;
113 GLsizei lengthVertexResult;
114 GLsizei lengthFragmentResult;
116 application.GetGlAbstraction().GetShaderSource(vertexShaderId, GETSOURCE_BUFFER_SIZE, &lengthVertexResult, testVertexSourceResult);
117 application.GetGlAbstraction().GetShaderSource(fragmentShaderId, GETSOURCE_BUFFER_SIZE, &lengthFragmentResult, testFragmentSourceResult);
119 int vertexShaderHasPrefix = strncmp(testVertexSourceResult, "#define ", strlen("#define "));
120 int fragmentShaderHasPrefix = strncmp(testFragmentSourceResult, "#define ", strlen("#define "));
121 testResult = (vertexShaderHasPrefix == 0) && (fragmentShaderHasPrefix == 0);
123 DALI_TEST_CHECK(testResult);
127 int UtcDaliShaderEffectFromProperties02(void)
131 TestApplication application;
132 tet_infoline("UtcDaliShaderEffectFromProperties02()");
134 // Call render to compile default shaders.
135 application.SendNotification();
136 application.Render();
137 application.Render();
138 application.Render();
140 // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
141 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
142 DALI_TEST_CHECK( typeInfo );
143 ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
144 DALI_TEST_CHECK( effect );
146 Property::Value programMap = Property::Value(Property::MAP);
148 programMap.SetValue("vertex", std::string(VertexSource));
149 programMap.SetValue("fragment", std::string(FragmentSource));
151 // programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
153 programMap.SetValue("geometry-type", GeometryType( GEOMETRY_TYPE_IMAGE ));
155 effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
157 tet_result( TET_FAIL );
159 catch(Dali::DaliException& e)
161 tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
166 int UtcDaliShaderEffectFromProperties03(void)
170 TestApplication application;
171 tet_infoline("UtcDaliShaderEffectFromProperties03()");
173 // Call render to compile default shaders.
174 application.SendNotification();
175 application.Render();
176 application.Render();
177 application.Render();
179 // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
180 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
181 DALI_TEST_CHECK( typeInfo );
182 ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
183 DALI_TEST_CHECK( effect );
186 effect.SetProperty( effect.GetPropertyIndex("geometry-hints"), "HINT_2" );
188 tet_result( TET_FAIL );
190 catch(Dali::DaliException& e)
192 tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());