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/public-api/loader/shader-option.h>
25 namespace Dali::Scene3D::Loader
29 static constexpr std::string_view OPTION_KEYWORD[] =
34 "METALLIC_ROUGHNESS_TEX",
40 "MATERIAL_SPECULAR_TEXTURE",
41 "MATERIAL_SPECULAR_COLOR_TEXTURE",
51 static constexpr uint32_t NUMBER_OF_OPTIONS = sizeof(OPTION_KEYWORD) / sizeof(OPTION_KEYWORD[0]);
53 inline void HashString(std::uint64_t& hash, const char* string)
56 while((c = *string++))
63 ShaderOption::ShaderOption(const ShaderOption& rhs)
65 mOptionHash = rhs.mOptionHash;
66 for(auto& macroDef : rhs.mMacros)
68 mMacros.emplace_back(macroDef);
72 ShaderOption& ShaderOption::operator=(const ShaderOption& rhs)
76 mOptionHash = rhs.mOptionHash;
77 for(auto& macroDef : rhs.mMacros)
79 mMacros.emplace_back(macroDef);
85 void ShaderOption::SetTransparency()
87 mOptionHash |= (1 << NUMBER_OF_OPTIONS);
90 void ShaderOption::AddOption(Type shaderOptionType)
92 mOptionHash |= (1 << static_cast<uint32_t>(shaderOptionType));
95 void ShaderOption::AddMacroDefinition(std::string macro, std::string definition)
97 auto iter = std::find_if(mMacros.begin(), mMacros.end(), [macro](ShaderOption::MacroDefinition& md) { return md.macro == macro; });
98 if(iter != mMacros.end())
100 iter->definition = definition;
104 mMacros.emplace_back(MacroDefinition{macro, definition});
108 const std::vector<ShaderOption::MacroDefinition>& ShaderOption::GetMacroDefinitions() const
113 uint64_t ShaderOption::GetOptionHash() const
115 uint64_t optionHash = mOptionHash;
118 uint64_t hash = 5381;
119 for(auto& macroDef : mMacros)
121 HashString(hash, macroDef.macro.c_str());
122 HashString(hash, macroDef.definition.c_str());
124 optionHash |= (hash << 32 & 0xFFFFFFFF00000000);
129 void ShaderOption::GetDefines(std::vector<std::string>& defines) const
132 for(uint32_t i = 0; i < NUMBER_OF_OPTIONS; ++i)
134 if(mOptionHash & 1 << i)
136 defines.push_back(OPTION_KEYWORD[i].data());
141 std::string_view ShaderOption::GetDefineKeyword(Type shaderOptionType)
143 return OPTION_KEYWORD[static_cast<uint32_t>(shaderOptionType)];
146 } // namespace Dali::Scene3D::Loader