2 * Copyright (c) 2022 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 // Enable debug log for test coverage
19 #define DEBUG_ENABLED 1
21 #include <dali-test-suite-utils.h>
22 #include <string_view>
23 #include "dali-scene3d/public-api/loader/gltf2-loader.h"
24 #include "dali-scene3d/public-api/loader/load-result.h"
25 #include "dali-scene3d/public-api/loader/resource-bundle.h"
26 #include "dali-scene3d/public-api/loader/scene-definition.h"
27 #include "dali-scene3d/public-api/loader/shader-definition-factory.h"
30 using namespace Dali::Scene3D::Loader;
32 #define DALI_TEST_THROW(expression, exception, predicate) \
34 bool daliTestThrowSuccess__ = false; \
41 printf("No exception was thrown.\n"); \
43 catch(std::decay<exception>::type & ex) \
45 daliTestThrowSuccess__ = predicate(ex); \
49 printf("Wrong type of exception thrown.\n"); \
51 DALI_TEST_CHECK(daliTestThrowSuccess__); \
58 ResourceBundle::PathProvider pathProvider = [](ResourceType::Value type) {
59 return TEST_RESOURCE_DIR "/";
62 ResourceBundle resources;
63 SceneDefinition scene;
65 std::vector<AnimationDefinition> animations;
66 std::vector<AnimationGroupDefinition> animationGroups;
67 std::vector<CameraParameters> cameras;
68 std::vector<LightParameters> lights;
70 LoadResult loadResult{
79 struct ExceptionMessageStartsWith
81 const std::string_view expected;
83 bool operator()(const std::runtime_error& e)
85 const bool success = (0 == strncmp(e.what(), expected.data(), expected.size()));
88 printf("Expected: %s, got: %s.\n", expected.data(), e.what());
96 int UtcDaliGltfLoaderFailedToLoad(void)
100 ShaderDefinitionFactory sdf;
101 sdf.SetResources(ctx.resources);
103 DALI_TEST_THROW(LoadGltfScene("non-existent.gltf", sdf, ctx.loadResult),
105 ExceptionMessageStartsWith{"Failed to load"});
107 DALI_TEST_EQUAL(0, ctx.scene.GetRoots().size());
108 DALI_TEST_EQUAL(0, ctx.scene.GetNodeCount());
110 DALI_TEST_EQUAL(0, ctx.resources.mEnvironmentMaps.size());
111 DALI_TEST_EQUAL(0, ctx.resources.mMaterials.size());
112 DALI_TEST_EQUAL(0, ctx.resources.mMeshes.size());
113 DALI_TEST_EQUAL(0, ctx.resources.mShaders.size());
114 DALI_TEST_EQUAL(0, ctx.resources.mSkeletons.size());
116 DALI_TEST_EQUAL(0, ctx.cameras.size());
117 DALI_TEST_EQUAL(0, ctx.lights.size());
118 DALI_TEST_EQUAL(0, ctx.animations.size());
119 DALI_TEST_EQUAL(0, ctx.animationGroups.size());
124 int UtcDaliGltfLoaderFailedToParse(void)
128 ShaderDefinitionFactory sdf;
129 sdf.SetResources(ctx.resources);
131 DALI_TEST_THROW(LoadGltfScene(TEST_RESOURCE_DIR "/invalid.gltf", sdf, ctx.loadResult),
133 ExceptionMessageStartsWith{"Failed to parse"});
135 DALI_TEST_EQUAL(0, ctx.scene.GetRoots().size());
136 DALI_TEST_EQUAL(0, ctx.scene.GetNodeCount());
138 DALI_TEST_EQUAL(0, ctx.resources.mEnvironmentMaps.size());
139 DALI_TEST_EQUAL(0, ctx.resources.mMaterials.size());
140 DALI_TEST_EQUAL(0, ctx.resources.mMeshes.size());
141 DALI_TEST_EQUAL(0, ctx.resources.mShaders.size());
142 DALI_TEST_EQUAL(0, ctx.resources.mSkeletons.size());
144 DALI_TEST_EQUAL(0, ctx.cameras.size());
145 DALI_TEST_EQUAL(0, ctx.lights.size());
146 DALI_TEST_EQUAL(0, ctx.animations.size());
147 DALI_TEST_EQUAL(0, ctx.animationGroups.size());
152 int UtcDaliGltfLoaderSuccess1(void)
156 ShaderDefinitionFactory sdf;
157 sdf.SetResources(ctx.resources);
159 LoadGltfScene(TEST_RESOURCE_DIR "/AnimatedCube.gltf", sdf, ctx.loadResult);
161 DALI_TEST_EQUAL(1u, ctx.scene.GetRoots().size());
162 DALI_TEST_EQUAL(6u, ctx.scene.GetNodeCount());
164 // Default envmap is used
165 DALI_TEST_EQUAL(1u, ctx.resources.mEnvironmentMaps.size());
167 auto& materials = ctx.resources.mMaterials;
168 DALI_TEST_EQUAL(2u, materials.size());
169 const MaterialDefinition materialGroundTruth[]{
170 {MaterialDefinition::ALBEDO | MaterialDefinition::EMISSIVE | MaterialDefinition::OCCLUSION |
171 MaterialDefinition::NORMAL | MaterialDefinition::TRANSPARENCY |
172 (0x80 << MaterialDefinition::ALPHA_CUTOFF_SHIFT),
177 Vector4(1.000, 0.766, 0.336, 1.0),
180 Vector3(0.2, 0.1, 0.0),
185 {MaterialDefinition::ALBEDO,
186 {"AnimatedCube_BaseColor.png",
187 SamplerFlags::Encode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR, WrapMode::CLAMP_TO_EDGE, WrapMode::REPEAT)}},
188 {MaterialDefinition::NORMAL,
189 {"AnimatedCube_BaseColor.png",
190 SamplerFlags::Encode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR, WrapMode::CLAMP_TO_EDGE, WrapMode::REPEAT)}},
191 {MaterialDefinition::OCCLUSION,
192 {"AnimatedCube_BaseColor.png",
193 SamplerFlags::Encode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR, WrapMode::CLAMP_TO_EDGE, WrapMode::REPEAT)}},
194 {MaterialDefinition::EMISSIVE,
195 {"AnimatedCube_BaseColor.png",
196 SamplerFlags::Encode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR, WrapMode::CLAMP_TO_EDGE, WrapMode::REPEAT)}},
198 {MaterialDefinition::ALBEDO | MaterialDefinition::METALLIC | MaterialDefinition::ROUGHNESS |
199 MaterialDefinition::EMISSIVE | MaterialDefinition::OCCLUSION |
200 MaterialDefinition::NORMAL | MaterialDefinition::GLTF_CHANNELS,
205 Vector4(1.000, 0.766, 0.336, 1.0),
208 Vector3(0.2, 0.1, 0.0),
213 {MaterialDefinition::ALBEDO,
214 {"AnimatedCube_BaseColor.png",
215 SamplerFlags::Encode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR, WrapMode::CLAMP_TO_EDGE, WrapMode::REPEAT)}},
216 {MaterialDefinition::METALLIC | MaterialDefinition::ROUGHNESS | MaterialDefinition::GLTF_CHANNELS,
217 {"AnimatedCube_MetallicRoughness.png",
218 SamplerFlags::Encode(FilterMode::NEAREST_MIPMAP_LINEAR, FilterMode::NEAREST, WrapMode::CLAMP_TO_EDGE, WrapMode::MIRRORED_REPEAT)}},
219 {MaterialDefinition::NORMAL,
220 {"AnimatedCube_BaseColor.png",
221 SamplerFlags::Encode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR, WrapMode::CLAMP_TO_EDGE, WrapMode::REPEAT)}},
222 {MaterialDefinition::OCCLUSION,
223 {"AnimatedCube_BaseColor.png",
224 SamplerFlags::Encode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR, WrapMode::CLAMP_TO_EDGE, WrapMode::REPEAT)}},
225 {MaterialDefinition::EMISSIVE,
226 {"AnimatedCube_BaseColor.png",
227 SamplerFlags::Encode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR, WrapMode::CLAMP_TO_EDGE, WrapMode::REPEAT)}},
231 auto iMaterial = materials.begin();
232 for(auto& m : materialGroundTruth)
234 printf("material %ld\n", iMaterial - materials.begin());
235 auto& md = iMaterial->first;
236 DALI_TEST_EQUAL(md.mFlags, m.mFlags);
237 DALI_TEST_EQUAL(md.mEnvironmentIdx, m.mEnvironmentIdx);
238 DALI_TEST_EQUAL(md.mColor, m.mColor);
239 DALI_TEST_EQUAL(md.mMetallic, m.mMetallic);
240 DALI_TEST_EQUAL(md.mRoughness, m.mRoughness);
241 DALI_TEST_EQUAL(md.mBaseColorFactor, m.mBaseColorFactor);
242 DALI_TEST_EQUAL(md.mNormalScale, m.mNormalScale);
243 DALI_TEST_EQUAL(md.mOcclusionStrength, m.mOcclusionStrength);
244 DALI_TEST_EQUAL(md.mEmissiveFactor, m.mEmissiveFactor);
245 DALI_TEST_EQUAL(md.mNeedAlbedoTexture, m.mNeedAlbedoTexture);
246 DALI_TEST_EQUAL(md.mNeedMetallicRoughnessTexture, m.mNeedMetallicRoughnessTexture);
247 DALI_TEST_EQUAL(md.mNeedNormalTexture, m.mNeedNormalTexture);
249 DALI_TEST_EQUAL(md.mTextureStages.size(), m.mTextureStages.size());
250 auto iTexture = md.mTextureStages.begin();
251 for(auto& ts : m.mTextureStages)
253 printf("texture %ld\n", iTexture - md.mTextureStages.begin());
254 DALI_TEST_EQUAL(iTexture->mSemantic, ts.mSemantic);
255 DALI_TEST_EQUAL(iTexture->mTexture.mImageUri, ts.mTexture.mImageUri);
256 DALI_TEST_EQUAL(uint32_t(iTexture->mTexture.mSamplerFlags), uint32_t(ts.mTexture.mSamplerFlags)); // don't interpret it as a character
262 auto& meshes = ctx.resources.mMeshes;
263 DALI_TEST_EQUAL(2u, meshes.size());
265 using Blob = MeshDefinition::Blob;
266 using Accessor = MeshDefinition::Accessor;
267 const MeshDefinition meshGroundTruth[]{
272 Accessor{Blob{0, 0}, {}},
273 Accessor{Blob{0, 0}, {}},
274 Accessor{Blob{0, 0}, {}},
275 Accessor{Blob{0, 0}, {}},
276 Accessor{Blob{0, 0}, {}},
277 Accessor{Blob{0, 0}, {}},
283 Accessor{Blob{0, 0}, {}},
284 Accessor{Blob{0, 0}, {}},
285 Accessor{Blob{0, 0}, {}},
286 Accessor{Blob{0, 0}, {}},
287 Accessor{Blob{0, 0}, {}},
288 Accessor{Blob{0, 0}, {}},
292 auto iMesh = meshes.begin();
293 for(auto& m : meshGroundTruth)
295 printf("mesh %ld\n", iMesh - meshes.begin());
297 auto& md = iMesh->first;
298 DALI_TEST_EQUAL(md.mFlags, m.mFlags);
299 DALI_TEST_EQUAL(md.mPrimitiveType, m.mPrimitiveType);
301 &MeshDefinition::mIndices,
302 &MeshDefinition::mPositions,
303 &MeshDefinition::mNormals,
304 &MeshDefinition::mTexCoords,
305 &MeshDefinition::mColors,
306 &MeshDefinition::mTangents,
307 &MeshDefinition::mJoints0,
308 &MeshDefinition::mWeights0})
310 DALI_TEST_EQUAL((md.*mp).IsDefined(), (m.*mp).IsDefined());
311 DALI_TEST_EQUAL((md.*mp).mBlob.IsDefined(), (m.*mp).mBlob.IsDefined());
314 DALI_TEST_EQUAL(md.mBlendShapeHeader.IsDefined(), m.mBlendShapeHeader.IsDefined());
319 DALI_TEST_EQUAL(2u, ctx.resources.mShaders.size());
320 DALI_TEST_EQUAL(0u, ctx.resources.mSkeletons.size());
322 DALI_TEST_EQUAL(3u, ctx.cameras.size());
323 DALI_TEST_EQUAL(0u, ctx.lights.size());
324 DALI_TEST_EQUAL(1u, ctx.animations.size());
325 DALI_TEST_EQUAL(0u, ctx.animationGroups.size());
330 int UtcDaliGltfLoaderSuccessShort(void)
334 const std::string resourcePath = TEST_RESOURCE_DIR "/";
335 auto pathProvider = [resourcePath](ResourceType::Value) {
339 Customization::Choices choices;
340 for(auto modelName : {
343 "AnimatedMorphSphere",
350 "MorphPrimitivesTest",
352 "SimpleSparseAccessor",
358 ShaderDefinitionFactory sdf;
360 auto& resources = ctx.resources;
361 resources.mEnvironmentMaps.push_back({});
363 sdf.SetResources(resources);
365 printf("%s\n", modelName);
366 LoadGltfScene(resourcePath + modelName + ".gltf", sdf, ctx.loadResult);
367 DALI_TEST_CHECK(ctx.scene.GetNodeCount() > 0);
369 auto& scene = ctx.scene;
370 for(auto iRoot : scene.GetRoots())
372 struct Visitor : NodeDefinition::IVisitor
374 struct ResourceReceiver : IResourceReceiver
376 std::vector<bool> mCounts;
378 void Register(ResourceType::Value type, Index id) override
380 if(type == ResourceType::Mesh)
387 void Start(NodeDefinition& n) override
391 n.mRenderable->RegisterResources(receiver);
395 void Finish(NodeDefinition& n) override
399 visitor.receiver.mCounts.resize(resources.mMeshes.size(), false);
401 scene.Visit(iRoot, choices, visitor);
402 for(uint32_t i0 = 0, i1 = resources.mMeshes.size(); i0 < i1; ++i0)
404 if(visitor.receiver.mCounts[i0])
406 auto raw = resources.mMeshes[i0].first.LoadRaw(resourcePath);
407 DALI_TEST_CHECK(!raw.mAttribs.empty());
409 resources.mMeshes[i0].second = resources.mMeshes[i0].first.Load(std::move(raw));
410 DALI_TEST_CHECK(resources.mMeshes[i0].second.geometry);
419 int UtcDaliGltfLoaderMRendererTest(void)
423 ShaderDefinitionFactory sdf;
424 sdf.SetResources(ctx.resources);
425 auto& resources = ctx.resources;
427 LoadGltfScene(TEST_RESOURCE_DIR "/MRendererTest.gltf", sdf, ctx.loadResult);
429 auto& scene = ctx.scene;
430 auto& roots = scene.GetRoots();
431 DALI_TEST_EQUAL(roots.size(), 1u);
432 DALI_TEST_EQUAL(scene.GetNode(roots[0])->mName, "RootNode");
433 DALI_TEST_EQUAL(scene.GetNode(roots[0])->mScale, Vector3(1.0f, 1.0f, 1.0f));
435 DALI_TEST_EQUAL(scene.GetNodeCount(), 1u);
437 ViewProjection viewProjection;
441 NodeDefinition::CreateParams nodeParams{
446 Customization::Choices choices;
450 Actor root = Actor::New();
451 SetActorCentered(root);
452 for(auto iRoot : roots)
454 auto resourceRefs = resources.CreateRefCounter();
455 scene.CountResourceRefs(iRoot, choices, resourceRefs);
456 resources.CountEnvironmentReferences(resourceRefs);
457 resources.LoadResources(resourceRefs, ctx.pathProvider);
458 if(auto actor = scene.CreateNodes(iRoot, choices, nodeParams))
460 scene.ConfigureSkeletonJoints(iRoot, resources.mSkeletons, actor);
461 scene.ConfigureSkinningShaders(resources, actor, std::move(nodeParams.mSkinnables));
462 scene.ApplyConstraints(actor, std::move(nodeParams.mConstrainables));
467 DALI_TEST_EQUAL(root.GetChildCount(), 1u);
468 Actor child = root.GetChildAt(0);
470 DALI_TEST_EQUAL(child.GetProperty(Actor::Property::NAME).Get<std::string>(), "RootNode");
471 DALI_TEST_EQUAL(child.GetProperty(Actor::Property::SCALE).Get<Vector3>(), Vector3(1.0f, 1.0f, 1.0f));
472 DALI_TEST_EQUAL(child.GetRendererCount(), 1u);
473 DALI_TEST_EQUAL(child.GetRendererAt(0).GetTextures().GetTextureCount(), 4u);