[dali_2.3.24] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / loader / gltf2-asset.cpp
index 8297045..bbc092e 100644 (file)
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2022 Samsung Electronics Co., Ltd.
+* Copyright (c) 2023 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * limitations under the License.
 *
 */
-#include "dali-scene3d/internal/loader/gltf2-asset.h"
+
+// EXTERNAL INCLUDES
+#include <dali/public-api/math/matrix.h>
 #include <algorithm>
 #include <map>
-#include "dali/public-api/math/matrix.h"
 
-#define ENUM_STRING_MAPPING(t, x) \
-  {                               \
-#x, t::x                      \
-  }
+// INTERNAL INCLUDES
+#include <dali-scene3d/internal/loader/gltf2-asset.h>
 
 using namespace Dali;
 
@@ -40,133 +39,151 @@ constexpr uint32_t ACCESSOR_TYPE_ELEMENT_COUNT[]{
   16,
   static_cast<uint32_t>(-1)};
 
-const std::map<std::string_view, AccessorType::Type> ACCESSOR_TYPES{
-  ENUM_STRING_MAPPING(AccessorType, SCALAR),
-  ENUM_STRING_MAPPING(AccessorType, VEC2),
-  ENUM_STRING_MAPPING(AccessorType, VEC3),
-  ENUM_STRING_MAPPING(AccessorType, VEC4),
-  ENUM_STRING_MAPPING(AccessorType, MAT2),
-  ENUM_STRING_MAPPING(AccessorType, MAT3),
-  ENUM_STRING_MAPPING(AccessorType, MAT4),
-};
-
-const std::map<std::string_view, AlphaMode::Type> ALPHA_MODE_TYPES{
-  ENUM_STRING_MAPPING(AlphaMode::Type, OPAQUE),
-  ENUM_STRING_MAPPING(AlphaMode::Type, MASK),
-  ENUM_STRING_MAPPING(AlphaMode::Type, BLEND),
-};
-
-const std::map<std::string_view, Attribute::Type> ATTRIBUTE_TYPES{
-  ENUM_STRING_MAPPING(Attribute::Type, POSITION),
-  ENUM_STRING_MAPPING(Attribute::Type, NORMAL),
-  ENUM_STRING_MAPPING(Attribute::Type, TANGENT),
-  ENUM_STRING_MAPPING(Attribute::Type, TEXCOORD_0),
-  ENUM_STRING_MAPPING(Attribute::Type, TEXCOORD_1),
-  ENUM_STRING_MAPPING(Attribute::Type, COLOR_0),
-  ENUM_STRING_MAPPING(Attribute::Type, JOINTS_0),
-  ENUM_STRING_MAPPING(Attribute::Type, WEIGHTS_0),
-};
-
-const std::map<std::string_view, Animation::Sampler::Interpolation::Type> ANIMATION_SAMPLER_INTERPOLATION{
-  ENUM_STRING_MAPPING(Animation::Sampler::Interpolation::Type, STEP),
-  ENUM_STRING_MAPPING(Animation::Sampler::Interpolation::Type, LINEAR),
-  ENUM_STRING_MAPPING(Animation::Sampler::Interpolation::Type, CUBICSPLINE),
-};
-
-const std::map<std::string_view, Animation::Channel::Target::Type> ANIMATION_CHANNEL_TARGET_PATH_TYPES{
-  ENUM_STRING_MAPPING(Animation::Channel::Target::Type, TRANSLATION),
-  ENUM_STRING_MAPPING(Animation::Channel::Target::Type, ROTATION),
-  ENUM_STRING_MAPPING(Animation::Channel::Target::Type, SCALE),
-  ENUM_STRING_MAPPING(Animation::Channel::Target::Type, WEIGHTS),
-};
+const std::map<std::string_view, AccessorType::Type>& GetAccessorTypes()
+{
+  static const std::map<std::string_view, AccessorType::Type> ACCESSOR_TYPES{
+    ENUM_STRING_MAPPING(AccessorType, SCALAR),
+    ENUM_STRING_MAPPING(AccessorType, VEC2),
+    ENUM_STRING_MAPPING(AccessorType, VEC3),
+    ENUM_STRING_MAPPING(AccessorType, VEC4),
+    ENUM_STRING_MAPPING(AccessorType, MAT2),
+    ENUM_STRING_MAPPING(AccessorType, MAT3),
+    ENUM_STRING_MAPPING(AccessorType, MAT4),
+  };
+  return ACCESSOR_TYPES;
+}
 
-} // namespace
+const std::map<std::string_view, AlphaMode::Type>& GetAlphaModeTypes()
+{
+  static const std::map<std::string_view, AlphaMode::Type> ALPHA_MODE_TYPES{
+    ENUM_STRING_MAPPING(AlphaMode::Type, OPAQUE),
+    ENUM_STRING_MAPPING(AlphaMode::Type, MASK),
+    ENUM_STRING_MAPPING(AlphaMode::Type, BLEND),
+  };
+  return ALPHA_MODE_TYPES;
+}
 
-bool Component::IsUnsigned(Type t)
+const std::map<std::string_view, Animation::Sampler::Interpolation::Type>& GetAnimationSamplerInterpolation()
 {
-  return t == UNSIGNED_BYTE || t == UNSIGNED_SHORT || t == UNSIGNED_INT;
+  static const std::map<std::string_view, Animation::Sampler::Interpolation::Type> ANIMATION_SAMPLER_INTERPOLATION{
+    ENUM_STRING_MAPPING(Animation::Sampler::Interpolation::Type, STEP),
+    ENUM_STRING_MAPPING(Animation::Sampler::Interpolation::Type, LINEAR),
+    ENUM_STRING_MAPPING(Animation::Sampler::Interpolation::Type, CUBICSPLINE),
+  };
+  return ANIMATION_SAMPLER_INTERPOLATION;
 }
 
-uint32_t Component::Size(Type t)
+const std::map<std::string_view, Animation::Channel::Target::Type>& GetAnimationChannelTargetPathTypes()
 {
-  switch(t)
-  {
-    case BYTE:
-    case UNSIGNED_BYTE:
-      return 1;
-    case SHORT:
-    case UNSIGNED_SHORT:
-      return 2;
-    case UNSIGNED_INT:
-    case FLOAT:
-      return 4;
-    default:
-      return -1;
-  }
+  static const std::map<std::string_view, Animation::Channel::Target::Type> ANIMATION_CHANNEL_TARGET_PATH_TYPES{
+    ENUM_STRING_MAPPING(Animation::Channel::Target::Type, TRANSLATION),
+    ENUM_STRING_MAPPING(Animation::Channel::Target::Type, ROTATION),
+    ENUM_STRING_MAPPING(Animation::Channel::Target::Type, SCALE),
+    ENUM_STRING_MAPPING(Animation::Channel::Target::Type, WEIGHTS),
+  };
+  return ANIMATION_CHANNEL_TARGET_PATH_TYPES;
 }
 
-uint32_t AccessorType::ElementCount(Type t)
+} // namespace
+
+ENUM_TYPE_FROM_STRING(AccessorType, GetAccessorTypes())
+ENUM_TYPE_FROM_STRING(AlphaMode, GetAlphaModeTypes())
+ENUM_TYPE_FROM_STRING(Animation::Sampler::Interpolation, GetAnimationSamplerInterpolation())
+ENUM_TYPE_FROM_STRING(Animation::Channel::Target, GetAnimationChannelTargetPathTypes())
+
+const std::map<std::string_view, Attribute::Type>& GetTargetTypes()
 {
-  return ACCESSOR_TYPE_ELEMENT_COUNT[t];
+  static const std::map<std::string_view, Attribute::Type> TARGET_TYPES{
+    ENUM_STRING_MAPPING(Attribute::Type, POSITION),
+    ENUM_STRING_MAPPING(Attribute::Type, TANGENT),
+    ENUM_STRING_MAPPING(Attribute::Type, NORMAL),
+  };
+  return TARGET_TYPES;
 }
 
-AccessorType::Type AccessorType::FromString(const char* s, size_t len)
+const std::map<Attribute::Type, const char*>& GetAttributeSetTypes()
 {
-  auto iFind = ACCESSOR_TYPES.find(std::string_view(s, len));
-  if(iFind != ACCESSOR_TYPES.end())
-  {
-    return iFind->second;
-  }
-  return AccessorType::INVALID;
+  static const std::map<Attribute::Type, const char*> ATTRIBUTE_SET_TYPES{
+    {Attribute::Type::TEXCOORD_N, "TEXCOORD_%u"},
+    {Attribute::Type::COLOR_N, "COLOR_%u"},
+    {Attribute::Type::JOINTS_N, "JOINTS_%u"},
+    {Attribute::Type::WEIGHTS_N, "WEIGHTS_%u"},
+  };
+  return ATTRIBUTE_SET_TYPES;
 }
 
-AlphaMode::Type AlphaMode::FromString(const char* s, size_t len)
+uint32_t Attribute::HashFromString(const char* token, size_t length)
 {
-  auto iFind = ALPHA_MODE_TYPES.find(std::string_view(s, len));
-  if(iFind != ALPHA_MODE_TYPES.end())
+  auto& table1 = GetTargetTypes();
+  auto& table2 = GetAttributeSetTypes();
+
+  std::string target(token, length);
+  std::transform(target.begin(), target.end(), target.begin(), ::toupper);
+
+  auto iFind = table1.find(std::string_view(target.c_str(), length));
+  if(iFind != table1.end())
   {
-    return iFind->second;
+    return Attribute::ToHash(iFind->second, false, 0);
   }
-  return AlphaMode::INVALID;
+
+  uint32_t hash = Attribute::ToHash(Attribute::INVALID, false, 0);
+  for(const auto& [key, match] : table2)
+  {
+    int setIndex;
+    if(sscanf(target.c_str(), match, &setIndex) > 0)
+    {
+      hash = Attribute::ToHash(key, true, setIndex);
+      break;
+    }
+  }
+  return hash;
 }
 
-Attribute::Type Attribute::FromString(const char* s, size_t len)
+Attribute::Type Attribute::TargetFromString(const char* token, size_t length)
 {
-  auto iFind = ATTRIBUTE_TYPES.find(std::string_view(s, len));
-  if(iFind != ATTRIBUTE_TYPES.end())
+  std::string target(token, length);
+  std::transform(target.begin(), target.end(), target.begin(), ::toupper);
+
+  auto iFind = GetTargetTypes().find(std::string_view(target.c_str(), length));
+  if(iFind != GetTargetTypes().end())
   {
     return iFind->second;
   }
   return Attribute::INVALID;
 }
 
-Animation::Sampler::Interpolation::Type Animation::Sampler::Interpolation::FromString(const char* s, size_t len)
+bool Component::IsUnsigned(Type t)
+{
+  return t == UNSIGNED_BYTE || t == UNSIGNED_SHORT || t == UNSIGNED_INT;
+}
+
+uint32_t Component::Size(Type t)
 {
-  auto iFind = ANIMATION_SAMPLER_INTERPOLATION.find(std::string_view(s, len));
-  if(iFind != ANIMATION_SAMPLER_INTERPOLATION.end())
+  switch(t)
   {
-    return iFind->second;
+    case BYTE:
+    case UNSIGNED_BYTE:
+      return 1;
+    case SHORT:
+    case UNSIGNED_SHORT:
+      return 2;
+    case UNSIGNED_INT:
+    case FLOAT:
+      return 4;
+    default:
+      return -1;
   }
-  return Animation::Sampler::Interpolation::Type::INVALID;
 }
 
-uint32_t ComponentTypedBufferViewClient::GetBytesPerComponent() const
+uint32_t AccessorType::ElementCount(Type t)
 {
-  return Component::Size(mComponentType);
+  return ACCESSOR_TYPE_ELEMENT_COUNT[t];
 }
 
-Animation::Channel::Target::Type Animation::Channel::Target::FromString(const char* s, size_t len)
+uint32_t ComponentTypedBufferViewClient::GetBytesPerComponent() const
 {
-  std::string target(s, len);
-  std::transform(target.begin(), target.end(), target.begin(), ::toupper);
-
-  auto iFind = ANIMATION_CHANNEL_TARGET_PATH_TYPES.find(std::string_view(target.c_str(), len));
-  if(iFind != ANIMATION_CHANNEL_TARGET_PATH_TYPES.end())
-  {
-    return iFind->second;
-  }
-  return Animation::Channel::Target::INVALID;
-};
+  return Component::Size(mComponentType);
+}
 
 void Node::SetMatrix(const Matrix& m)
 {