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.
19 #include <dali-scene3d/internal/common/model-load-task.h>
22 #include <dali/integration-api/debug.h>
26 #include <dali-scene3d/internal/common/model-cache-manager.h>
27 #include <dali-scene3d/public-api/loader/animation-definition.h>
28 #include <dali-scene3d/public-api/loader/camera-parameters.h>
29 #include <dali-scene3d/public-api/loader/dli-loader.h>
30 #include <dali-scene3d/public-api/loader/gltf2-loader.h>
31 #include <dali-scene3d/public-api/loader/light-parameters.h>
32 #include <dali-scene3d/public-api/loader/node-definition.h>
33 #include <dali-scene3d/public-api/loader/shader-definition-factory.h>
43 static constexpr Vector3 Y_DIRECTION(1.0f, -1.0f, 1.0f);
45 static constexpr std::string_view OBJ_EXTENSION = ".obj";
46 static constexpr std::string_view GLTF_EXTENSION = ".gltf";
47 static constexpr std::string_view DLI_EXTENSION = ".dli";
48 static constexpr std::string_view METADATA_EXTENSION = "metadata";
51 ModelLoadTask::ModelLoadTask(const std::string& modelUrl, const std::string& resourceDirectoryUrl, CallbackBase* callback)
52 : AsyncTask(callback),
54 mResourceDirectoryUrl(resourceDirectoryUrl),
56 mModelCacheManager(ModelCacheManager::Get()),
57 mLoadResult(mModelCacheManager.GetModelLoadResult(modelUrl))
61 ModelLoadTask::~ModelLoadTask()
65 void ModelLoadTask::Process()
67 uint32_t cacheRefCount = mModelCacheManager.GetModelCacheRefCount(mModelUrl);
68 Dali::ConditionalWait& loadSceneConditionalWait = mModelCacheManager.GetLoadSceneConditionalWaitInstance(mModelUrl);
69 Dali::ConditionalWait& loadRawResourceConditionalWait = mModelCacheManager.GetLoadRawResourceConditionalWaitInstance(mModelUrl);
71 std::filesystem::path modelUrl(mModelUrl);
72 if(mResourceDirectoryUrl.empty())
74 mResourceDirectoryUrl = std::string(modelUrl.parent_path()) + "/";
76 std::string extension = modelUrl.extension();
77 std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
79 Dali::Scene3D::Loader::ResourceBundle::PathProvider pathProvider = [&](Dali::Scene3D::Loader::ResourceType::Value type) {
80 return mResourceDirectoryUrl;
84 ConditionalWait::ScopedLock lock(loadSceneConditionalWait);
86 while(cacheRefCount > 1 && mModelCacheManager.IsSceneLoading(mModelUrl))
88 loadSceneConditionalWait.Wait();
93 ConditionalWait::ScopedLock lock(loadSceneConditionalWait);
95 if(!mModelCacheManager.IsSceneLoaded(mModelUrl))
97 mModelCacheManager.SetSceneLoading(mModelUrl, true);
99 std::filesystem::path metaDataUrl = modelUrl;
100 metaDataUrl.replace_extension(METADATA_EXTENSION.data());
102 Dali::Scene3D::Loader::LoadSceneMetadata(metaDataUrl.c_str(), mLoadResult.mSceneMetadata);
104 mLoadResult.mAnimationDefinitions.clear();
106 if(extension == DLI_EXTENSION)
108 Dali::Scene3D::Loader::DliLoader loader;
109 Dali::Scene3D::Loader::DliLoader::InputParams input{
110 pathProvider(Dali::Scene3D::Loader::ResourceType::Mesh),
116 Dali::Scene3D::Loader::DliLoader::LoadParams loadParams{input, mLoadResult};
117 if(!loader.LoadScene(mModelUrl, loadParams))
119 DALI_LOG_ERROR("Failed to load scene from '%s': %s\n", mModelUrl.c_str(), loader.GetParseError().c_str());
121 mModelCacheManager.SetSceneLoaded(mModelUrl, false);
122 mModelCacheManager.SetSceneLoading(mModelUrl, false);
123 mModelCacheManager.UnreferenceModelCache(mModelUrl);
128 else if(extension == GLTF_EXTENSION)
130 Dali::Scene3D::Loader::ShaderDefinitionFactory sdf;
131 sdf.SetResources(mLoadResult.mResources);
132 Dali::Scene3D::Loader::LoadGltfScene(mModelUrl, sdf, mLoadResult);
136 DALI_LOG_ERROR("Unsupported model type.\n");
138 mModelCacheManager.SetSceneLoaded(mModelUrl, false);
139 mModelCacheManager.SetSceneLoading(mModelUrl, false);
140 mModelCacheManager.UnreferenceModelCache(mModelUrl);
145 mModelCacheManager.SetSceneLoaded(mModelUrl, true);
146 mModelCacheManager.SetSceneLoading(mModelUrl, false);
150 loadSceneConditionalWait.Notify();
153 ConditionalWait::ScopedLock lock(loadRawResourceConditionalWait);
155 while(cacheRefCount > 1 && mLoadResult.mResources.mRawResourcesLoading)
157 loadRawResourceConditionalWait.Wait();
162 ConditionalWait::ScopedLock lock(loadRawResourceConditionalWait);
164 for(auto iRoot : mLoadResult.mScene.GetRoots())
166 mResourceRefCounts.push_back(mLoadResult.mResources.CreateRefCounter());
167 mLoadResult.mScene.CountResourceRefs(iRoot, mResourceChoices, mResourceRefCounts.back());
168 mLoadResult.mResources.CountEnvironmentReferences(mResourceRefCounts.back());
170 mLoadResult.mResources.LoadRawResources(mResourceRefCounts.back(), pathProvider);
172 // glTF Mesh is defined in right hand coordinate system, with positive Y for Up direction.
173 // Because DALi uses left hand system, Y direciton will be flipped for environment map sampling.
174 for(auto&& env : mLoadResult.mResources.mEnvironmentMaps)
176 env.first.mYDirection = Y_DIRECTION;
181 loadRawResourceConditionalWait.Notify();
183 mHasSucceeded = true;
186 bool ModelLoadTask::IsReady()
191 bool ModelLoadTask::HasSucceeded() const
193 return mHasSucceeded;
196 } // namespace Internal
198 } // namespace Scene3D