2 * Copyright (c) 2021 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-scene-loader/public-api/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;
36 const char* const RESOURCE_TYPE_NAMES[] = {
45 const char* GetResourceTypeName(ResourceType::Value type)
47 return RESOURCE_TYPE_NAMES[static_cast<int>(type)];
50 ResourceRefCounts ResourceBundle::CreateRefCounter() const
52 ResourceRefCounts refCounts(4);
53 refCounts[ResourceType::Environment].Resize(mEnvironmentMaps.size(), 0u);
54 refCounts[ResourceType::Shader].Resize(mShaders.size(), 0u);
55 refCounts[ResourceType::Mesh].Resize(mMeshes.size(), 0u);
56 refCounts[ResourceType::Material].Resize(mMaterials.size(), 0u);
60 void ResourceBundle::CountEnvironmentReferences(ResourceRefCounts& refCounts) const
62 auto& environmentRefCounts = refCounts[ResourceType::Environment];
64 const auto& materialRefs = refCounts[ResourceType::Material];
65 for(uint32_t i = 0, iEnd = materialRefs.Size(); i != iEnd; ++i)
67 if(materialRefs[i] > 0)
69 ++environmentRefCounts[mMaterials[i].first.mEnvironmentIdx];
74 void ResourceBundle::LoadResources(const ResourceRefCounts& refCounts, PathProvider pathProvider, Options::Type options)
76 const auto kForceLoad = MaskMatch(options, Options::ForceReload);
77 const auto kKeepUnused = MaskMatch(options, Options::KeepUnused);
79 const auto& refCountEnvMaps = refCounts[ResourceType::Environment];
80 auto environmentsPath = pathProvider(ResourceType::Environment);
81 for(uint32_t i = 0, iEnd = refCountEnvMaps.Size(); i != iEnd; ++i)
83 auto refCount = refCountEnvMaps[i];
84 auto& iEnvMap = mEnvironmentMaps[i];
85 if(refCount > 0 && (kForceLoad || !iEnvMap.second.IsLoaded()))
87 auto raw = iEnvMap.first.LoadRaw(environmentsPath);
88 iEnvMap.second = iEnvMap.first.Load(std::move(raw));
90 else if(!kKeepUnused && refCount == 0 && iEnvMap.second.IsLoaded())
92 iEnvMap.second.mDiffuse = Texture();
93 iEnvMap.second.mSpecular = Texture();
97 const auto& refCountShaders = refCounts[ResourceType::Shader];
98 auto shadersPath = pathProvider(ResourceType::Shader);
99 for(uint32_t i = 0, iEnd = refCountShaders.Size(); i != iEnd; ++i)
101 auto refCount = refCountShaders[i];
102 auto& iShader = mShaders[i];
103 if(refCount > 0 && (kForceLoad || !iShader.second))
105 auto raw = iShader.first.LoadRaw(shadersPath);
106 iShader.second = iShader.first.Load(std::move(raw));
108 else if(!kKeepUnused && refCount == 0 && iShader.second)
110 iShader.second = Shader();
114 const auto& refCountMeshes = refCounts[ResourceType::Mesh];
115 auto modelsPath = pathProvider(ResourceType::Mesh);
116 for(uint32_t i = 0, iEnd = refCountMeshes.Size(); i != iEnd; ++i)
118 auto refCount = refCountMeshes[i];
119 auto& iMesh = mMeshes[i];
120 if(refCount > 0 && (kForceLoad || !iMesh.second.geometry))
122 auto raw = iMesh.first.LoadRaw(modelsPath);
123 iMesh.second = iMesh.first.Load(std::move(raw));
125 else if(!kKeepUnused && refCount == 0 && iMesh.second.geometry)
127 iMesh.second.geometry = Geometry();
131 const auto& refCountMaterials = refCounts[ResourceType::Material];
132 auto imagesPath = pathProvider(ResourceType::Material);
133 for(uint32_t i = 0, iEnd = refCountMaterials.Size(); i != iEnd; ++i)
135 auto refCount = refCountMaterials[i];
136 auto& iMaterial = mMaterials[i];
137 if(refCount > 0 && (kForceLoad || !iMaterial.second))
139 auto raw = iMaterial.first.LoadRaw(imagesPath);
140 iMaterial.second = iMaterial.first.Load(mEnvironmentMaps, std::move(raw));
142 else if(!kKeepUnused && refCount == 0 && iMaterial.second)
144 iMaterial.second = TextureSet();
149 } // namespace SceneLoader