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::EMISSIVE | MaterialDefinition::OCCLUSION |
171 MaterialDefinition::NORMAL | MaterialDefinition::TRANSPARENCY | MaterialDefinition::GLTF_CHANNELS |
172 (0x80 << MaterialDefinition::ALPHA_CUTOFF_SHIFT),
174 Vector4(1.f, .766f, .336f, 1.f),
175 Vector3(0.2, 0.1, 0.0),
180 {MaterialDefinition::ALBEDO,
181 {"AnimatedCube_BaseColor.png",
182 SamplerFlags::Encode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR, WrapMode::CLAMP_TO_EDGE, WrapMode::REPEAT)}},
183 {MaterialDefinition::METALLIC | MaterialDefinition::ROUGHNESS | MaterialDefinition::GLTF_CHANNELS,
184 {"AnimatedCube_MetallicRoughness.png",
185 SamplerFlags::Encode(FilterMode::NEAREST_MIPMAP_LINEAR, FilterMode::NEAREST, WrapMode::CLAMP_TO_EDGE, WrapMode::MIRRORED_REPEAT)}},
186 {MaterialDefinition::NORMAL,
187 {"AnimatedCube_BaseColor.png",
188 SamplerFlags::Encode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR, WrapMode::CLAMP_TO_EDGE, WrapMode::REPEAT)}},
189 {MaterialDefinition::OCCLUSION,
190 {"AnimatedCube_BaseColor.png",
191 SamplerFlags::Encode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR, WrapMode::CLAMP_TO_EDGE, WrapMode::REPEAT)}},
192 {MaterialDefinition::EMISSIVE,
193 {"AnimatedCube_BaseColor.png",
194 SamplerFlags::Encode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR, WrapMode::CLAMP_TO_EDGE, WrapMode::REPEAT)}},
196 {MaterialDefinition::ALBEDO | MaterialDefinition::METALLIC | MaterialDefinition::ROUGHNESS |
197 MaterialDefinition::EMISSIVE | MaterialDefinition::OCCLUSION |
198 MaterialDefinition::NORMAL | MaterialDefinition::GLTF_CHANNELS,
200 Vector4(1.f, .766f, .336f, 1.f),
201 Vector3(0.2, 0.1, 0.0),
206 {MaterialDefinition::ALBEDO,
207 {"AnimatedCube_BaseColor.png",
208 SamplerFlags::Encode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR, WrapMode::CLAMP_TO_EDGE, WrapMode::REPEAT)}},
209 {MaterialDefinition::METALLIC | MaterialDefinition::ROUGHNESS | MaterialDefinition::GLTF_CHANNELS,
210 {"AnimatedCube_MetallicRoughness.png",
211 SamplerFlags::Encode(FilterMode::NEAREST_MIPMAP_LINEAR, FilterMode::NEAREST, WrapMode::CLAMP_TO_EDGE, WrapMode::MIRRORED_REPEAT)}},
212 {MaterialDefinition::NORMAL,
213 {"AnimatedCube_BaseColor.png",
214 SamplerFlags::Encode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR, WrapMode::CLAMP_TO_EDGE, WrapMode::REPEAT)}},
215 {MaterialDefinition::OCCLUSION,
216 {"AnimatedCube_BaseColor.png",
217 SamplerFlags::Encode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR, WrapMode::CLAMP_TO_EDGE, WrapMode::REPEAT)}},
218 {MaterialDefinition::EMISSIVE,
219 {"AnimatedCube_BaseColor.png",
220 SamplerFlags::Encode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR, WrapMode::CLAMP_TO_EDGE, WrapMode::REPEAT)}},
224 auto iMaterial = materials.begin();
225 for(auto& m : materialGroundTruth)
227 printf("material %ld\n", iMaterial - materials.begin());
228 auto& md = iMaterial->first;
229 DALI_TEST_EQUAL(md.mFlags, m.mFlags);
230 DALI_TEST_EQUAL(md.mEnvironmentIdx, m.mEnvironmentIdx);
231 DALI_TEST_EQUAL(md.mColor, m.mColor);
232 DALI_TEST_EQUAL(md.mMetallic, m.mMetallic);
233 DALI_TEST_EQUAL(md.mRoughness, m.mRoughness);
235 DALI_TEST_EQUAL(md.mTextureStages.size(), m.mTextureStages.size());
236 auto iTexture = md.mTextureStages.begin();
237 for(auto& ts : m.mTextureStages)
239 printf("texture %ld\n", iTexture - md.mTextureStages.begin());
240 DALI_TEST_EQUAL(iTexture->mSemantic, ts.mSemantic);
241 DALI_TEST_EQUAL(iTexture->mTexture.mImageUri, ts.mTexture.mImageUri);
242 DALI_TEST_EQUAL(uint32_t(iTexture->mTexture.mSamplerFlags), uint32_t(ts.mTexture.mSamplerFlags)); // don't interpret it as a character
248 auto& meshes = ctx.resources.mMeshes;
249 DALI_TEST_EQUAL(2u, meshes.size());
251 using Blob = MeshDefinition::Blob;
252 using Accessor = MeshDefinition::Accessor;
253 const MeshDefinition meshGroundTruth[]{
258 Accessor{Blob{0, 0}, {}},
259 Accessor{Blob{0, 0}, {}},
260 Accessor{Blob{0, 0}, {}},
261 Accessor{Blob{0, 0}, {}},
262 Accessor{Blob{0, 0}, {}},
268 Accessor{Blob{0, 0}, {}},
269 Accessor{Blob{0, 0}, {}},
270 Accessor{Blob{0, 0}, {}},
271 Accessor{Blob{0, 0}, {}},
272 Accessor{Blob{0, 0}, {}},
276 auto iMesh = meshes.begin();
277 for(auto& m : meshGroundTruth)
279 printf("mesh %ld\n", iMesh - meshes.begin());
281 auto& md = iMesh->first;
282 DALI_TEST_EQUAL(md.mFlags, m.mFlags);
283 DALI_TEST_EQUAL(md.mPrimitiveType, m.mPrimitiveType);
285 &MeshDefinition::mIndices,
286 &MeshDefinition::mPositions,
287 &MeshDefinition::mNormals,
288 &MeshDefinition::mTexCoords,
289 &MeshDefinition::mTangents,
290 &MeshDefinition::mJoints0,
291 &MeshDefinition::mWeights0})
293 DALI_TEST_EQUAL((md.*mp).IsDefined(), (m.*mp).IsDefined());
294 DALI_TEST_EQUAL((md.*mp).mBlob.IsDefined(), (m.*mp).mBlob.IsDefined());
297 DALI_TEST_EQUAL(md.mBlendShapeHeader.IsDefined(), m.mBlendShapeHeader.IsDefined());
302 DALI_TEST_EQUAL(2u, ctx.resources.mShaders.size());
303 DALI_TEST_EQUAL(0u, ctx.resources.mSkeletons.size());
305 DALI_TEST_EQUAL(3u, ctx.cameras.size());
306 DALI_TEST_EQUAL(0u, ctx.lights.size());
307 DALI_TEST_EQUAL(1u, ctx.animations.size());
308 DALI_TEST_EQUAL(0u, ctx.animationGroups.size());
313 int UtcDaliGltfLoaderSuccessShort(void)
317 const std::string resourcePath = TEST_RESOURCE_DIR "/";
318 auto pathProvider = [resourcePath](ResourceType::Value) {
322 Customization::Choices choices;
323 for(auto modelName : {
326 "AnimatedMorphSphere",
333 "MorphPrimitivesTest",
335 "SimpleSparseAccessor",
340 ShaderDefinitionFactory sdf;
342 auto& resources = ctx.resources;
343 resources.mEnvironmentMaps.push_back({});
345 sdf.SetResources(resources);
347 printf("%s\n", modelName);
348 LoadGltfScene(resourcePath + modelName + ".gltf", sdf, ctx.loadResult);
349 DALI_TEST_CHECK(ctx.scene.GetNodeCount() > 0);
351 auto& scene = ctx.scene;
352 for(auto iRoot : scene.GetRoots())
354 struct Visitor : NodeDefinition::IVisitor
356 struct ResourceReceiver : IResourceReceiver
358 std::vector<bool> mCounts;
360 void Register(ResourceType::Value type, Index id) override
362 if(type == ResourceType::Mesh)
369 void Start(NodeDefinition& n) override
373 n.mRenderable->RegisterResources(receiver);
377 void Finish(NodeDefinition& n) override
381 visitor.receiver.mCounts.resize(resources.mMeshes.size(), false);
383 scene.Visit(iRoot, choices, visitor);
384 for(uint32_t i0 = 0, i1 = resources.mMeshes.size(); i0 < i1; ++i0)
386 if(visitor.receiver.mCounts[i0])
388 auto raw = resources.mMeshes[i0].first.LoadRaw(resourcePath);
389 DALI_TEST_CHECK(!raw.mAttribs.empty());
391 resources.mMeshes[i0].second = resources.mMeshes[i0].first.Load(std::move(raw));
392 DALI_TEST_CHECK(resources.mMeshes[i0].second.geometry);
401 int UtcDaliGltfLoaderMRendererTest(void)
405 ShaderDefinitionFactory sdf;
406 sdf.SetResources(ctx.resources);
407 auto& resources = ctx.resources;
408 resources.mEnvironmentMaps.push_back({});
410 LoadGltfScene(TEST_RESOURCE_DIR "/MRendererTest.gltf", sdf, ctx.loadResult);
412 auto& scene = ctx.scene;
413 auto& roots = scene.GetRoots();
414 DALI_TEST_EQUAL(roots.size(), 1u);
415 DALI_TEST_EQUAL(scene.GetNode(roots[0])->mName, "RootNode");
416 DALI_TEST_EQUAL(scene.GetNode(roots[0])->mScale, Vector3(1.0f, 1.0f, 1.0f));
418 DALI_TEST_EQUAL(scene.GetNodeCount(), 1u);
420 ViewProjection viewProjection;
424 NodeDefinition::CreateParams nodeParams{
429 Customization::Choices choices;
433 Actor root = Actor::New();
434 SetActorCentered(root);
435 for(auto iRoot : roots)
437 auto resourceRefs = resources.CreateRefCounter();
438 scene.CountResourceRefs(iRoot, choices, resourceRefs);
439 resources.CountEnvironmentReferences(resourceRefs);
440 resources.LoadResources(resourceRefs, ctx.pathProvider);
441 if(auto actor = scene.CreateNodes(iRoot, choices, nodeParams))
443 scene.ConfigureSkeletonJoints(iRoot, resources.mSkeletons, actor);
444 scene.ConfigureSkinningShaders(resources, actor, std::move(nodeParams.mSkinnables));
445 scene.ApplyConstraints(actor, std::move(nodeParams.mConstrainables));
450 DALI_TEST_EQUAL(root.GetChildCount(), 1u);
451 DALI_TEST_EQUAL(root.GetChildAt(0).GetProperty(Actor::Property::NAME).Get<std::string>(), "RootNode");
452 DALI_TEST_EQUAL(root.GetChildAt(0).GetProperty(Actor::Property::SCALE).Get<Vector3>(), Vector3(1.0f, 1.0f, 1.0f));