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.
19 #include "dali-scene3d/public-api/loader/resource-bundle.h"
25 #include "dali-toolkit/public-api/image-loader/sync-image-loader.h"
26 #include "dali/public-api/rendering/sampler.h"
30 using namespace Toolkit;
38 const char* const RESOURCE_TYPE_NAMES[] = {
47 const char* GetResourceTypeName(ResourceType::Value type)
49 return RESOURCE_TYPE_NAMES[static_cast<int>(type)];
52 ResourceRefCounts ResourceBundle::CreateRefCounter() const
54 ResourceRefCounts refCounts(4);
55 refCounts[ResourceType::Environment].Resize(mEnvironmentMaps.size(), 0u);
56 refCounts[ResourceType::Shader].Resize(mShaders.size(), 0u);
57 refCounts[ResourceType::Mesh].Resize(mMeshes.size(), 0u);
58 refCounts[ResourceType::Material].Resize(mMaterials.size(), 0u);
62 void ResourceBundle::CountEnvironmentReferences(ResourceRefCounts& refCounts) const
64 auto& environmentRefCounts = refCounts[ResourceType::Environment];
66 const auto& materialRefs = refCounts[ResourceType::Material];
67 for(uint32_t i = 0, iEnd = materialRefs.Size(); i != iEnd; ++i)
69 if(materialRefs[i] > 0)
71 ++environmentRefCounts[mMaterials[i].first.mEnvironmentIdx];
76 void ResourceBundle::LoadResources(const ResourceRefCounts& refCounts, PathProvider pathProvider, Options::Type options)
78 const auto kForceLoad = MaskMatch(options, Options::ForceReload);
79 const auto kKeepUnused = MaskMatch(options, Options::KeepUnused);
81 const auto& refCountEnvMaps = refCounts[ResourceType::Environment];
82 auto environmentsPath = pathProvider(ResourceType::Environment);
83 for(uint32_t i = 0, iEnd = refCountEnvMaps.Size(); i != iEnd; ++i)
85 auto refCount = refCountEnvMaps[i];
86 auto& iEnvMap = mEnvironmentMaps[i];
87 if(refCount > 0 && (kForceLoad || !iEnvMap.second.IsLoaded()))
89 auto raw = iEnvMap.first.LoadRaw(environmentsPath);
90 iEnvMap.second = iEnvMap.first.Load(std::move(raw));
92 else if(!kKeepUnused && refCount == 0 && iEnvMap.second.IsLoaded())
94 iEnvMap.second.mDiffuse = Texture();
95 iEnvMap.second.mSpecular = Texture();
99 const auto& refCountShaders = refCounts[ResourceType::Shader];
100 auto shadersPath = pathProvider(ResourceType::Shader);
101 for(uint32_t i = 0, iEnd = refCountShaders.Size(); i != iEnd; ++i)
103 auto refCount = refCountShaders[i];
104 auto& iShader = mShaders[i];
105 if(refCount > 0 && (kForceLoad || !iShader.second))
107 auto raw = iShader.first.LoadRaw(shadersPath);
108 iShader.second = iShader.first.Load(std::move(raw));
110 else if(!kKeepUnused && refCount == 0 && iShader.second)
112 iShader.second = Shader();
116 const auto& refCountMeshes = refCounts[ResourceType::Mesh];
117 auto modelsPath = pathProvider(ResourceType::Mesh);
118 for(uint32_t i = 0, iEnd = refCountMeshes.Size(); i != iEnd; ++i)
120 auto refCount = refCountMeshes[i];
121 auto& iMesh = mMeshes[i];
122 if(refCount > 0 && (kForceLoad || !iMesh.second.geometry))
124 auto raw = iMesh.first.LoadRaw(modelsPath);
125 iMesh.second = iMesh.first.Load(std::move(raw));
127 else if(!kKeepUnused && refCount == 0 && iMesh.second.geometry)
129 iMesh.second.geometry = Geometry();
133 const auto& refCountMaterials = refCounts[ResourceType::Material];
134 auto imagesPath = pathProvider(ResourceType::Material);
135 for(uint32_t i = 0, iEnd = refCountMaterials.Size(); i != iEnd; ++i)
137 auto refCount = refCountMaterials[i];
138 auto& iMaterial = mMaterials[i];
139 if(refCount > 0 && (kForceLoad || !iMaterial.second))
141 auto raw = iMaterial.first.LoadRaw(imagesPath);
142 iMaterial.second = iMaterial.first.Load(mEnvironmentMaps, std::move(raw));
144 else if(!kKeepUnused && refCount == 0 && iMaterial.second)
146 iMaterial.second = TextureSet();
151 } // namespace Loader
152 } // namespace Scene3D