2 * Copyright (c) 2023 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.
18 #include <toolkit-event-thread-callback.h>
19 #include <dali-toolkit-test-suite-utils.h>
20 #include <dali-toolkit/dali-toolkit.h>
24 #include <dali-scene3d/internal/model-components/material-impl.h>
25 #include <dali-scene3d/internal/graphics/builtin-shader-extern-gen.h>
28 using namespace Dali::Toolkit;
30 void model_components_material_impl_startup(void)
32 test_return_value = TET_UNDEF;
35 void model_components_material_impl_cleanup(void)
37 test_return_value = TET_PASS;
42 const char* TEST_IMAGE_FILE_NAME = TEST_RESOURCE_DIR "/gallery-small-1.jpg";
46 int UtcDaliMaterialImplSetGetTextureInformation(void)
48 tet_infoline("UtcDaliMaterialImplSetGetTextureInformation.");
50 ToolkitTestApplication application;
52 Scene3D::Material material = Scene3D::Material::New();
54 Scene3D::Internal::Material::TextureInformation baseColor;
55 Dali::Texture baseColorTexture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 100, 100);
56 Dali::Sampler baseColorSampler = Dali::Sampler::New();
57 baseColor.mTexture = baseColorTexture;
58 baseColor.mSampler = baseColorSampler;
59 baseColor.mUrl = "baseColor";
60 GetImplementation(material).SetTextureInformation(Scene3D::Material::TextureType::BASE_COLOR, std::move(baseColor));
61 DALI_TEST_EQUALS(baseColorTexture, material.GetTexture(Scene3D::Material::TextureType::BASE_COLOR), TEST_LOCATION);
62 DALI_TEST_EQUALS(baseColorSampler, material.GetSampler(Scene3D::Material::TextureType::BASE_COLOR), TEST_LOCATION);
63 DALI_TEST_EQUALS("baseColor", material.GetProperty<std::string>(Scene3D::Material::Property::BASE_COLOR_URL), TEST_LOCATION);
65 Scene3D::Internal::Material::TextureInformation metallicRoughness;
66 Dali::Texture metallicRoughnessTexture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 100, 100);
67 Dali::Sampler metallicRoughnessSampler = Dali::Sampler::New();
68 metallicRoughness.mTexture = metallicRoughnessTexture;
69 metallicRoughness.mSampler = metallicRoughnessSampler;
70 metallicRoughness.mUrl = "metallicRoughness";
71 GetImplementation(material).SetTextureInformation(Scene3D::Material::TextureType::METALLIC_ROUGHNESS, std::move(metallicRoughness));
72 DALI_TEST_EQUALS(metallicRoughnessTexture, material.GetTexture(Scene3D::Material::TextureType::METALLIC_ROUGHNESS), TEST_LOCATION);
73 DALI_TEST_EQUALS(metallicRoughnessSampler, material.GetSampler(Scene3D::Material::TextureType::METALLIC_ROUGHNESS), TEST_LOCATION);
74 DALI_TEST_EQUALS("metallicRoughness", material.GetProperty<std::string>(Scene3D::Material::Property::METALLIC_ROUGHNESS_URL), TEST_LOCATION);
76 Scene3D::Internal::Material::TextureInformation normal;
77 Dali::Texture normalTexture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 100, 100);
78 Dali::Sampler normalSampler = Dali::Sampler::New();
79 normal.mTexture = normalTexture;
80 normal.mSampler = normalSampler;
81 normal.mUrl = "normal";
82 GetImplementation(material).SetTextureInformation(Scene3D::Material::TextureType::NORMAL, std::move(normal));
83 DALI_TEST_EQUALS(normalTexture, material.GetTexture(Scene3D::Material::TextureType::NORMAL), TEST_LOCATION);
84 DALI_TEST_EQUALS(normalSampler, material.GetSampler(Scene3D::Material::TextureType::NORMAL), TEST_LOCATION);
85 DALI_TEST_EQUALS("normal", material.GetProperty<std::string>(Scene3D::Material::Property::NORMAL_URL), TEST_LOCATION);
87 Dali::TextureSet textureSet = GetImplementation(material).GetTextureSet();
88 DALI_TEST_EQUALS(3, textureSet.GetTextureCount(), TEST_LOCATION);
89 DALI_TEST_EQUALS(baseColorTexture, textureSet.GetTexture(0u), TEST_LOCATION);
90 DALI_TEST_EQUALS(metallicRoughnessTexture, textureSet.GetTexture(1u), TEST_LOCATION);
91 DALI_TEST_EQUALS(normalTexture, textureSet.GetTexture(2u), TEST_LOCATION);
92 DALI_TEST_EQUALS(baseColorSampler, textureSet.GetSampler(0u), TEST_LOCATION);
93 DALI_TEST_EQUALS(metallicRoughnessSampler, textureSet.GetSampler(1u), TEST_LOCATION);
95 DALI_TEST_EQUALS(true, GetImplementation(material).IsResourceReady(), TEST_LOCATION);
96 GetImplementation(material).UpdateMaterialData();
98 std::vector<std::string> defines;
99 defines.push_back("THREE_TEX");
100 defines.push_back("GLTF_CHANNELS");
101 defines.push_back("BASECOLOR_TEX");
102 defines.push_back("METALLIC_ROUGHNESS_TEX");
103 defines.push_back("NORMAL_TEX");
105 std::string fragmentShader = SHADER_DEFAULT_PHYSICALLY_BASED_SHADER_FRAG.data();
106 for(const auto& define : defines)
108 Scene3D::Loader::ShaderDefinition::ApplyDefine(fragmentShader, define);
111 DALI_TEST_EQUALS(fragmentShader, GetImplementation(material).GetFragmentShader(), TEST_LOCATION);
113 Scene3D ::Internal ::Material ::TextureInformation occlusion;
114 Dali ::Texture occlusiontexture = Dali ::Texture ::New(TextureType ::TEXTURE_2D, Pixel ::RGBA8888, 100, 100);
115 Dali ::Sampler occlusionSampler = Dali ::Sampler ::New();
116 occlusion.mTexture = occlusiontexture;
117 occlusion.mSampler = occlusionSampler;
118 occlusion.mUrl = "occlusion";
119 GetImplementation(material).SetTextureInformation(Scene3D ::Material ::TextureType ::OCCLUSION, std ::move(occlusion));
120 DALI_TEST_EQUALS(occlusiontexture, material.GetTexture(Scene3D ::Material ::TextureType ::OCCLUSION), TEST_LOCATION);
121 DALI_TEST_EQUALS(occlusionSampler, material.GetSampler(Scene3D ::Material ::TextureType ::OCCLUSION), TEST_LOCATION);
122 DALI_TEST_EQUALS("occlusion", material.GetProperty<std ::string>(Scene3D ::Material ::Property ::OCCLUSION_URL), TEST_LOCATION);
124 Scene3D::Internal::Material::TextureInformation emissive;
125 Dali::Texture emissiveTexture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 100, 100);
126 Dali::Sampler emissiveSampler = Dali::Sampler::New();
127 emissive.mTexture = emissiveTexture;
128 emissive.mSampler = emissiveSampler;
129 emissive.mUrl = "emissive";
130 GetImplementation(material).SetTextureInformation(Scene3D::Material::TextureType::EMISSIVE, std::move(emissive));
131 DALI_TEST_EQUALS(emissiveTexture, material.GetTexture(Scene3D::Material::TextureType::EMISSIVE), TEST_LOCATION);
132 DALI_TEST_EQUALS(emissiveSampler, material.GetSampler(Scene3D::Material::TextureType::EMISSIVE), TEST_LOCATION);
133 DALI_TEST_EQUALS("emissive", material.GetProperty<std::string>(Scene3D::Material::Property::EMISSIVE_URL), TEST_LOCATION);
135 Scene3D ::Internal ::Material ::TextureInformation specular;
136 Dali ::Texture specularTexture = Dali ::Texture ::New(TextureType ::TEXTURE_2D, Pixel ::RGBA8888, 100, 100);
137 Dali ::Sampler specularSampler = Dali ::Sampler ::New();
138 specular.mTexture = specularTexture;
139 specular.mSampler = specularSampler;
140 specular.mUrl = "specular";
141 GetImplementation(material).SetTextureInformation(Scene3D ::Material ::TextureType ::SPECULAR, std ::move(specular));
142 DALI_TEST_EQUALS(specularTexture, material.GetTexture(Scene3D ::Material ::TextureType ::SPECULAR), TEST_LOCATION);
143 DALI_TEST_EQUALS(specularSampler, material.GetSampler(Scene3D ::Material ::TextureType ::SPECULAR), TEST_LOCATION);
144 DALI_TEST_EQUALS("specular", material.GetProperty<std ::string>(Scene3D ::Material ::Property ::SPECULAR_URL), TEST_LOCATION);
146 Scene3D::Internal::Material::TextureInformation specularColor;
147 Dali::Texture specularColorTexture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 100, 100);
148 Dali::Sampler specularColorSampler = Dali::Sampler::New();
149 specularColor.mTexture = specularColorTexture;
150 specularColor.mSampler = specularColorSampler;
151 specularColor.mUrl = "specularColor";
152 GetImplementation(material).SetTextureInformation(Scene3D::Material::TextureType::SPECULAR_COLOR, std::move(specularColor));
153 DALI_TEST_EQUALS(specularColorTexture, material.GetTexture(Scene3D::Material::TextureType::SPECULAR_COLOR), TEST_LOCATION);
154 DALI_TEST_EQUALS(specularColorSampler, material.GetSampler(Scene3D::Material::TextureType::SPECULAR_COLOR), TEST_LOCATION);
155 DALI_TEST_EQUALS("specularColor", material.GetProperty<std::string>(Scene3D::Material::Property::SPECULAR_COLOR_URL), TEST_LOCATION);
157 textureSet = GetImplementation(material).GetTextureSet();
158 DALI_TEST_EQUALS(7, textureSet.GetTextureCount(), TEST_LOCATION);
159 DALI_TEST_EQUALS(baseColorTexture, textureSet.GetTexture(0u), TEST_LOCATION);
160 DALI_TEST_EQUALS(metallicRoughnessTexture, textureSet.GetTexture(1u), TEST_LOCATION);
161 DALI_TEST_EQUALS(normalTexture, textureSet.GetTexture(2u), TEST_LOCATION);
162 DALI_TEST_EQUALS(occlusiontexture, textureSet.GetTexture(3u), TEST_LOCATION);
163 DALI_TEST_EQUALS(emissiveTexture, textureSet.GetTexture(4u), TEST_LOCATION);
164 DALI_TEST_EQUALS(specularTexture, textureSet.GetTexture(5u), TEST_LOCATION);
165 DALI_TEST_EQUALS(specularColorTexture, textureSet.GetTexture(6u), TEST_LOCATION);
167 DALI_TEST_EQUALS(baseColorSampler, textureSet.GetSampler(0u), TEST_LOCATION);
168 DALI_TEST_EQUALS(metallicRoughnessSampler, textureSet.GetSampler(1u), TEST_LOCATION);
169 DALI_TEST_EQUALS(normalSampler, textureSet.GetSampler(2u), TEST_LOCATION);
170 DALI_TEST_EQUALS(occlusionSampler, textureSet.GetSampler(3u), TEST_LOCATION);
171 DALI_TEST_EQUALS(emissiveSampler, textureSet.GetSampler(4u), TEST_LOCATION);
172 DALI_TEST_EQUALS(specularSampler, textureSet.GetSampler(5u), TEST_LOCATION);
173 DALI_TEST_EQUALS(specularColorSampler, textureSet.GetSampler(6u), TEST_LOCATION);
175 DALI_TEST_EQUALS(true, GetImplementation(material).IsResourceReady(), TEST_LOCATION);
176 GetImplementation(material).UpdateMaterialData();
178 defines.push_back("OCCLUSION");
179 defines.push_back("EMISSIVE");
180 defines.push_back("MATERIAL_SPECULAR_TEXTURE");
181 defines.push_back("MATERIAL_SPECULAR_COLOR_TEXTURE");
183 fragmentShader = SHADER_DEFAULT_PHYSICALLY_BASED_SHADER_FRAG.data();
184 for(const auto& define : defines)
186 Scene3D::Loader::ShaderDefinition::ApplyDefine(fragmentShader, define);
189 DALI_TEST_EQUALS(fragmentShader, GetImplementation(material).GetFragmentShader(), TEST_LOCATION);
195 int UtcDaliMaterialCheckUniform(void)
197 tet_infoline("UtcDaliMaterialCheckUniform.");
199 ToolkitTestApplication application;
201 Scene3D::Material material = Scene3D::Material::New();
203 Vector4 baseColorFactor(0.1f, 0.2f, 0.3f, 0.4f);
204 material.SetProperty(Scene3D::Material::Property::BASE_COLOR_FACTOR, baseColorFactor);
205 DALI_TEST_EQUALS(baseColorFactor, material.GetProperty<Vector4>(Scene3D::Material::Property::BASE_COLOR_FACTOR), TEST_LOCATION);
207 float metallicFactor = 0.5f;
208 material.SetProperty(Scene3D::Material::Property::METALLIC_FACTOR, metallicFactor);
209 DALI_TEST_EQUALS(metallicFactor, material.GetProperty<float>(Scene3D::Material::Property::METALLIC_FACTOR), TEST_LOCATION);
211 float roughnessFactor = 0.6f;
212 material.SetProperty(Scene3D::Material::Property::ROUGHNESS_FACTOR, roughnessFactor);
213 DALI_TEST_EQUALS(roughnessFactor, material.GetProperty<float>(Scene3D::Material::Property::ROUGHNESS_FACTOR), TEST_LOCATION);
215 float normalScale = 0.7f;
216 material.SetProperty(Scene3D::Material::Property::NORMAL_SCALE, normalScale);
217 DALI_TEST_EQUALS(normalScale, material.GetProperty<float>(Scene3D::Material::Property::NORMAL_SCALE), TEST_LOCATION);
219 float occlusionStrength = 0.8f;
220 material.SetProperty(Scene3D::Material::Property::OCCLUSION_STRENGTH, occlusionStrength);
221 DALI_TEST_EQUALS(occlusionStrength, material.GetProperty<float>(Scene3D::Material::Property::OCCLUSION_STRENGTH), TEST_LOCATION);
223 Vector3 emissiveFactor(0.9f, 0.1f, 0.2f);
224 material.SetProperty(Scene3D::Material::Property::EMISSIVE_FACTOR, emissiveFactor);
225 DALI_TEST_EQUALS(emissiveFactor, material.GetProperty<Vector3>(Scene3D::Material::Property::EMISSIVE_FACTOR), TEST_LOCATION);
227 Dali::Scene3D::Material::AlphaModeType alphaMode = Dali::Scene3D::Material::AlphaModeType::BLEND;
228 material.SetProperty(Scene3D::Material::Property::ALPHA_MODE, alphaMode);
229 DALI_TEST_EQUALS(alphaMode, material.GetProperty<Dali::Scene3D::Material::AlphaModeType>(Scene3D::Material ::Property ::ALPHA_MODE), TEST_LOCATION);
231 float alphaCutoff = 0.9f;
232 material.SetProperty(Scene3D ::Material ::Property ::ALPHA_CUTOFF, alphaCutoff);
233 DALI_TEST_EQUALS(alphaCutoff, material.GetProperty<float>(Scene3D ::Material ::Property ::ALPHA_CUTOFF), TEST_LOCATION);
235 bool doubleSided = false;
236 material.SetProperty(Scene3D ::Material ::Property ::DOUBLE_SIDED, doubleSided);
237 DALI_TEST_EQUALS(doubleSided, material.GetProperty<bool>(Scene3D ::Material ::Property ::DOUBLE_SIDED), TEST_LOCATION);
240 material.SetProperty(Scene3D::Material::Property::IOR, ior);
241 DALI_TEST_EQUALS(ior, material.GetProperty<float>(Scene3D::Material::Property::IOR), TEST_LOCATION);
243 float specularFactor = 1.0f;
244 material.SetProperty(Scene3D::Material::Property::SPECULAR_FACTOR, specularFactor);
245 DALI_TEST_EQUALS(specularFactor, material.GetProperty<float>(Scene3D::Material::Property::SPECULAR_FACTOR), TEST_LOCATION);
247 Vector3 specularColorFactor(1.0f, 1.0f, 1.0f);
248 material.SetProperty(Scene3D::Material::Property::SPECULAR_COLOR_FACTOR, specularColorFactor);
249 DALI_TEST_EQUALS(specularColorFactor, material.GetProperty<Vector3>(Scene3D::Material::Property::SPECULAR_COLOR_FACTOR), TEST_LOCATION);
251 DALI_TEST_EQUALS(true, GetImplementation(material).IsResourceReady(), TEST_LOCATION);
252 GetImplementation(material).UpdateMaterialData();
254 Dali::Shader shader = Dali::Shader::New(SHADER_DEFAULT_PHYSICALLY_BASED_SHADER_VERT.data(), SHADER_DEFAULT_PHYSICALLY_BASED_SHADER_FRAG.data());
255 Dali::Geometry geometry = Dali::Geometry::New();
256 Dali::Renderer renderer = Dali::Renderer::New(geometry, shader);
258 GetImplementation(material).SetRendererUniform(renderer);
260 DALI_TEST_EQUALS(baseColorFactor, renderer.GetProperty<Vector4>(renderer.GetPropertyIndex("uColorFactor")), TEST_LOCATION);
261 DALI_TEST_EQUALS(metallicFactor, renderer.GetProperty<float>(renderer.GetPropertyIndex("uMetallicFactor")), TEST_LOCATION);
262 DALI_TEST_EQUALS(roughnessFactor, renderer.GetProperty<float>(renderer.GetPropertyIndex("uRoughnessFactor")), TEST_LOCATION);
263 DALI_TEST_EQUALS(normalScale, renderer.GetProperty<float>(renderer.GetPropertyIndex("uNormalScale")), TEST_LOCATION);
264 DALI_TEST_EQUALS(emissiveFactor, renderer.GetProperty<Vector3>(renderer.GetPropertyIndex("uEmissiveFactor")), TEST_LOCATION);
265 float dielectricSpecular = (Dali::Equals(ior, -1.0)) ? 0.04f : powf((ior - 1.0f) / (ior + 1.0f), 2.0f);
266 DALI_TEST_EQUALS(dielectricSpecular, renderer.GetProperty<float>(renderer.GetPropertyIndex("uDielectricSpecular")), TEST_LOCATION);
267 DALI_TEST_EQUALS(specularFactor, renderer.GetProperty<float>(renderer.GetPropertyIndex("uSpecularFactor")), TEST_LOCATION);
268 DALI_TEST_EQUALS(specularColorFactor, renderer.GetProperty<Vector3>(renderer.GetPropertyIndex("uSpecularColorFactor")), TEST_LOCATION);
269 float opaque = alphaMode == Dali::Scene3D::Material::AlphaModeType::BLEND ? 0.0f : 1.0f;
270 float mask = alphaMode == Dali::Scene3D::Material::AlphaModeType::MASK ? 1.0f : 0.0f;
271 DALI_TEST_EQUALS(opaque, renderer.GetProperty<float>(renderer.GetPropertyIndex("uOpaque")), TEST_LOCATION);
272 DALI_TEST_EQUALS(mask, renderer.GetProperty<float>(renderer.GetPropertyIndex("uMask")), TEST_LOCATION);
273 DALI_TEST_EQUALS(alphaCutoff, renderer.GetProperty<float>(renderer.GetPropertyIndex("uAlphaThreshold")), TEST_LOCATION);
275 DALI_TEST_EQUALS(DepthTestMode::ON, renderer.GetProperty<DepthTestMode::Type>(Dali::Renderer::Property::DEPTH_TEST_MODE), TEST_LOCATION);
276 DALI_TEST_EQUALS(FaceCullingMode::BACK, renderer.GetProperty<FaceCullingMode::Type>(Dali::Renderer::Property::FACE_CULLING_MODE), TEST_LOCATION);
277 DALI_TEST_EQUALS(DepthTestMode::ON, renderer.GetProperty<DepthTestMode::Type>(Dali::Renderer::Property::DEPTH_TEST_MODE), TEST_LOCATION);
282 int UtcDaliMaterialImplLoadTexture_1(void)
284 tet_infoline("UtcDaliMaterialLoadTexture.");
286 ToolkitTestApplication application;
288 Scene3D::Material material = Scene3D::Material::New();
290 DALI_TEST_EQUALS(true, GetImplementation(material).IsResourceReady(), TEST_LOCATION);
291 DALI_TEST_CHECK(!material.GetTexture(Scene3D::Material::TextureType::BASE_COLOR));
292 material.SetProperty(Scene3D::Material::Property::BASE_COLOR_URL, TEST_IMAGE_FILE_NAME);
293 DALI_TEST_EQUALS(false, GetImplementation(material).IsResourceReady(), TEST_LOCATION);
295 application.SendNotification();
296 application.Render(16);
298 DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
300 application.SendNotification();
301 application.Render(16);
303 DALI_TEST_EQUALS(true, GetImplementation(material).IsResourceReady(), TEST_LOCATION);
304 DALI_TEST_CHECK(material.GetTexture(Scene3D::Material::TextureType::BASE_COLOR));
305 Dali::Texture baseColorTexture = material.GetTexture(Scene3D::Material::TextureType::BASE_COLOR);
307 Dali::TextureSet textureSet = GetImplementation(material).GetTextureSet();
308 DALI_TEST_EQUALS(1u, textureSet.GetTextureCount(), TEST_LOCATION);
309 DALI_TEST_EQUALS(baseColorTexture, textureSet.GetTexture(0u), TEST_LOCATION);
310 DALI_TEST_CHECK(textureSet.GetSampler(0u));
312 material.SetProperty(Scene3D::Material::Property::BASE_COLOR_URL, TEST_IMAGE_FILE_NAME);
313 DALI_TEST_EQUALS(true, GetImplementation(material).IsResourceReady(), TEST_LOCATION);
314 DALI_TEST_EQUALS(baseColorTexture, material.GetTexture(Scene3D::Material::TextureType::BASE_COLOR), TEST_LOCATION);
316 material.SetProperty(Scene3D::Material::Property::BASE_COLOR_URL, "");
317 DALI_TEST_CHECK(!material.GetTexture(Scene3D::Material::TextureType::BASE_COLOR));
322 int UtcDaliMaterialImplLoadTexture_2(void)
324 tet_infoline("UtcDaliMaterialImplLoadTexture_2.");
326 ToolkitTestApplication application;
328 Scene3D::Material material = Scene3D::Material::New();
330 DALI_TEST_EQUALS(true, GetImplementation(material).IsResourceReady(), TEST_LOCATION);
331 DALI_TEST_CHECK(!material.GetTexture(Scene3D::Material::TextureType::BASE_COLOR));
332 material.SetProperty(Scene3D::Material::Property::BASE_COLOR_URL, TEST_IMAGE_FILE_NAME);
333 DALI_TEST_EQUALS(false, GetImplementation(material).IsResourceReady(), TEST_LOCATION);
334 material.SetProperty(Scene3D::Material::Property::BASE_COLOR_URL, "");
335 DALI_TEST_EQUALS(true, GetImplementation(material).IsResourceReady(), TEST_LOCATION);
336 DALI_TEST_CHECK(!material.GetTexture(Scene3D::Material::TextureType::BASE_COLOR));