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-scene-loader/public-api/gltf2-loader.h"
24 #include "dali-scene-loader/public-api/load-result.h"
25 #include "dali-scene-loader/public-api/resource-bundle.h"
26 #include "dali-scene-loader/public-api/scene-definition.h"
27 #include "dali-scene-loader/public-api/shader-definition-factory.h"
30 using namespace Dali::SceneLoader;
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 DALI_TEST_EQUAL(0u, ctx.resources.mEnvironmentMaps.size());
166 auto& materials = ctx.resources.mMaterials;
167 DALI_TEST_EQUAL(2u, materials.size());
168 const MaterialDefinition materialGroundTruth[]{
169 {MaterialDefinition::ALBEDO | MaterialDefinition::METALLIC | MaterialDefinition::ROUGHNESS |
170 MaterialDefinition::NORMAL | MaterialDefinition::TRANSPARENCY | MaterialDefinition::GLTF_CHANNELS |
171 (0x80 << MaterialDefinition::ALPHA_CUTOFF_SHIFT),
173 Vector4(1.f, .766f, .336f, 1.f),
177 {MaterialDefinition::ALBEDO,
178 {"AnimatedCube_BaseColor.png",
179 SamplerFlags::Encode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR, WrapMode::CLAMP_TO_EDGE, WrapMode::REPEAT)}},
180 {MaterialDefinition::METALLIC | MaterialDefinition::ROUGHNESS | MaterialDefinition::GLTF_CHANNELS,
181 {"AnimatedCube_MetallicRoughness.png",
182 SamplerFlags::Encode(FilterMode::NEAREST_MIPMAP_LINEAR, FilterMode::NEAREST, WrapMode::CLAMP_TO_EDGE, WrapMode::MIRRORED_REPEAT)}},
183 {MaterialDefinition::NORMAL,
184 {"AnimatedCube_BaseColor.png",
185 SamplerFlags::Encode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR, WrapMode::CLAMP_TO_EDGE, WrapMode::REPEAT)}},
187 {MaterialDefinition::ALBEDO | MaterialDefinition::METALLIC | MaterialDefinition::ROUGHNESS |
188 MaterialDefinition::NORMAL | MaterialDefinition::GLTF_CHANNELS,
190 Vector4(1.f, .766f, .336f, 1.f),
194 {MaterialDefinition::ALBEDO,
195 {"AnimatedCube_BaseColor.png",
196 SamplerFlags::Encode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR, WrapMode::CLAMP_TO_EDGE, WrapMode::REPEAT)}},
197 {MaterialDefinition::METALLIC | MaterialDefinition::ROUGHNESS | MaterialDefinition::GLTF_CHANNELS,
198 {"AnimatedCube_MetallicRoughness.png",
199 SamplerFlags::Encode(FilterMode::NEAREST_MIPMAP_LINEAR, FilterMode::NEAREST, WrapMode::CLAMP_TO_EDGE, WrapMode::MIRRORED_REPEAT)}},
200 {MaterialDefinition::NORMAL,
201 {"AnimatedCube_BaseColor.png",
202 SamplerFlags::Encode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR, WrapMode::CLAMP_TO_EDGE, WrapMode::REPEAT)}},
206 auto iMaterial = materials.begin();
207 for(auto& m : materialGroundTruth)
209 printf("material %ld\n", iMaterial - materials.begin());
210 auto& md = iMaterial->first;
211 DALI_TEST_EQUAL(md.mFlags, m.mFlags);
212 DALI_TEST_EQUAL(md.mEnvironmentIdx, m.mEnvironmentIdx);
213 DALI_TEST_EQUAL(md.mColor, m.mColor);
214 DALI_TEST_EQUAL(md.mMetallic, m.mMetallic);
215 DALI_TEST_EQUAL(md.mRoughness, m.mRoughness);
217 DALI_TEST_EQUAL(md.mTextureStages.size(), m.mTextureStages.size());
218 auto iTexture = md.mTextureStages.begin();
219 for(auto& ts : m.mTextureStages)
221 printf("texture %ld\n", iTexture - md.mTextureStages.begin());
222 DALI_TEST_EQUAL(iTexture->mSemantic, ts.mSemantic);
223 DALI_TEST_EQUAL(iTexture->mTexture.mImageUri, ts.mTexture.mImageUri);
224 DALI_TEST_EQUAL(uint32_t(iTexture->mTexture.mSamplerFlags), uint32_t(ts.mTexture.mSamplerFlags)); // don't interpret it as a character
230 auto& meshes = ctx.resources.mMeshes;
231 DALI_TEST_EQUAL(2u, meshes.size());
233 using Blob = MeshDefinition::Blob;
234 using Accessor = MeshDefinition::Accessor;
235 const MeshDefinition meshGroundTruth[]{
240 Accessor{Blob{0, 0}, {}},
241 Accessor{Blob{0, 0}, {}},
242 Accessor{Blob{0, 0}, {}},
243 Accessor{Blob{0, 0}, {}},
244 Accessor{Blob{0, 0}, {}},
250 Accessor{Blob{0, 0}, {}},
251 Accessor{Blob{0, 0}, {}},
252 Accessor{Blob{0, 0}, {}},
253 Accessor{Blob{0, 0}, {}},
254 Accessor{Blob{0, 0}, {}},
258 auto iMesh = meshes.begin();
259 for(auto& m : meshGroundTruth)
261 printf("mesh %ld\n", iMesh - meshes.begin());
263 auto& md = iMesh->first;
264 DALI_TEST_EQUAL(md.mFlags, m.mFlags);
265 DALI_TEST_EQUAL(md.mPrimitiveType, m.mPrimitiveType);
267 &MeshDefinition::mIndices,
268 &MeshDefinition::mPositions,
269 &MeshDefinition::mNormals,
270 &MeshDefinition::mTexCoords,
271 &MeshDefinition::mTangents,
272 &MeshDefinition::mJoints0,
273 &MeshDefinition::mWeights0})
275 DALI_TEST_EQUAL((md.*mp).IsDefined(), (m.*mp).IsDefined());
276 DALI_TEST_EQUAL((md.*mp).mBlob.IsDefined(), (m.*mp).mBlob.IsDefined());
279 DALI_TEST_EQUAL(md.mBlendShapeHeader.IsDefined(), m.mBlendShapeHeader.IsDefined());
284 DALI_TEST_EQUAL(2u, ctx.resources.mShaders.size());
285 DALI_TEST_EQUAL(0u, ctx.resources.mSkeletons.size());
287 DALI_TEST_EQUAL(3u, ctx.cameras.size());
288 DALI_TEST_EQUAL(0u, ctx.lights.size());
289 DALI_TEST_EQUAL(1u, ctx.animations.size());
290 DALI_TEST_EQUAL(0u, ctx.animationGroups.size());
295 int UtcDaliGltfLoaderSuccessShort(void)
299 const std::string resourcePath = TEST_RESOURCE_DIR "/";
300 auto pathProvider = [resourcePath](ResourceType::Value) {
304 Customization::Choices choices;
305 for(auto modelName : {
308 "AnimatedMorphSphere",
315 "MorphPrimitivesTest",
317 "SimpleSparseAccessor",
322 ShaderDefinitionFactory sdf;
324 auto& resources = ctx.resources;
325 resources.mEnvironmentMaps.push_back({});
327 sdf.SetResources(resources);
329 printf("%s\n", modelName);
330 LoadGltfScene(resourcePath + modelName + ".gltf", sdf, ctx.loadResult);
331 DALI_TEST_CHECK(ctx.scene.GetNodeCount() > 0);
333 auto& scene = ctx.scene;
334 for(auto iRoot : scene.GetRoots())
336 struct Visitor : NodeDefinition::IVisitor
338 struct ResourceReceiver : IResourceReceiver
340 std::vector<bool> mCounts;
342 void Register(ResourceType::Value type, Index id) override
344 if(type == ResourceType::Mesh)
351 void Start(NodeDefinition& n) override
355 n.mRenderable->RegisterResources(receiver);
359 void Finish(NodeDefinition& n) override
363 visitor.receiver.mCounts.resize(resources.mMeshes.size(), false);
365 scene.Visit(iRoot, choices, visitor);
366 for(uint32_t i0 = 0, i1 = resources.mMeshes.size(); i0 < i1; ++i0)
368 if(visitor.receiver.mCounts[i0])
370 auto raw = resources.mMeshes[i0].first.LoadRaw(resourcePath);
371 DALI_TEST_CHECK(!raw.mAttribs.empty());
373 resources.mMeshes[i0].second = resources.mMeshes[i0].first.Load(std::move(raw));
374 DALI_TEST_CHECK(resources.mMeshes[i0].second.geometry);
383 int UtcDaliGltfLoaderMRendererTest(void)
387 ShaderDefinitionFactory sdf;
388 sdf.SetResources(ctx.resources);
389 auto& resources = ctx.resources;
390 resources.mEnvironmentMaps.push_back({});
392 LoadGltfScene(TEST_RESOURCE_DIR "/MRendererTest.gltf", sdf, ctx.loadResult);
394 auto& scene = ctx.scene;
395 auto& roots = scene.GetRoots();
396 DALI_TEST_EQUAL(roots.size(), 1u);
397 DALI_TEST_EQUAL(scene.GetNode(roots[0])->mName, "RootNode");
398 DALI_TEST_EQUAL(scene.GetNode(roots[0])->mScale, Vector3(1.0f, 1.0f, 1.0f));
400 DALI_TEST_EQUAL(scene.GetNodeCount(), 1u);
402 ViewProjection viewProjection;
406 NodeDefinition::CreateParams nodeParams{
411 Customization::Choices choices;
415 Actor root = Actor::New();
416 SetActorCentered(root);
417 for(auto iRoot : roots)
419 auto resourceRefs = resources.CreateRefCounter();
420 scene.CountResourceRefs(iRoot, choices, resourceRefs);
421 resources.CountEnvironmentReferences(resourceRefs);
422 resources.LoadResources(resourceRefs, ctx.pathProvider);
423 if(auto actor = scene.CreateNodes(iRoot, choices, nodeParams))
425 scene.ConfigureSkeletonJoints(iRoot, resources.mSkeletons, actor);
426 scene.ConfigureSkinningShaders(resources, actor, std::move(nodeParams.mSkinnables));
427 scene.ApplyConstraints(actor, std::move(nodeParams.mConstrainables));
432 DALI_TEST_EQUAL(root.GetChildCount(), 1u);
433 DALI_TEST_EQUAL(root.GetChildAt(0).GetProperty(Actor::Property::NAME).Get<std::string>(), "RootNode");
434 DALI_TEST_EQUAL(root.GetChildAt(0).GetProperty(Actor::Property::SCALE).Get<Vector3>(), Vector3(1.0f, 1.0f, 1.0f));