Allow to load uint32_t as indices 37/288537/11
authorEunki Hong <eunkiki.hong@samsung.com>
Sat, 18 Feb 2023 10:22:52 +0000 (19:22 +0900)
committerEunki Hong <eunkiki.hong@samsung.com>
Fri, 24 Feb 2023 04:59:25 +0000 (04:59 +0000)
Dali's default indices use uint16_t. But if heavy 3D model who need more than
65536 indexes loaded, It will break index.

This patch make Scene3D can allow to use uint32_t type indeces load
and generete Geometry by it.

Change-Id: I6b8096df3fcf855443e6b407342121b9374d9ff8
Signed-off-by: Eunki Hong <eunkiki.hong@samsung.com>
automated-tests/resources/EnvironmentTest.gltf
automated-tests/resources/EnvironmentTest_images/roughness_metallic_0.jpg [new file with mode: 0644]
automated-tests/resources/EnvironmentTest_images/roughness_metallic_1.jpg [new file with mode: 0644]
dali-scene3d/public-api/loader/mesh-definition.cpp

index 962e475..b9d85b3 100644 (file)
                 {\r
                     "attributes": {\r
                         "POSITION": 0,\r
                 {\r
                     "attributes": {\r
                         "POSITION": 0,\r
-                        "NORMAL": 1,\r
                         "TEXCOORD_0": 2\r
                     },\r
                     "indices": 3,\r
                         "TEXCOORD_0": 2\r
                     },\r
                     "indices": 3,\r
diff --git a/automated-tests/resources/EnvironmentTest_images/roughness_metallic_0.jpg b/automated-tests/resources/EnvironmentTest_images/roughness_metallic_0.jpg
new file mode 100644 (file)
index 0000000..8ba03d3
Binary files /dev/null and b/automated-tests/resources/EnvironmentTest_images/roughness_metallic_0.jpg differ
diff --git a/automated-tests/resources/EnvironmentTest_images/roughness_metallic_1.jpg b/automated-tests/resources/EnvironmentTest_images/roughness_metallic_1.jpg
new file mode 100644 (file)
index 0000000..d695618
Binary files /dev/null and b/automated-tests/resources/EnvironmentTest_images/roughness_metallic_1.jpg differ
index b23140f..18093a5 100644 (file)
@@ -35,36 +35,39 @@ namespace Loader
 {
 namespace
 {
 {
 namespace
 {
+template<bool use32BitIndices>
 class IndexProvider
 {
 public:
 class IndexProvider
 {
 public:
+  using IndexType = typename std::conditional_t<use32BitIndices, uint32_t, uint16_t>;
   IndexProvider(const uint16_t* indices)
   : mData(reinterpret_cast<uintptr_t>(indices)),
     mFunc(indices ? IncrementPointer : Increment)
   {
   }
 
   IndexProvider(const uint16_t* indices)
   : mData(reinterpret_cast<uintptr_t>(indices)),
     mFunc(indices ? IncrementPointer : Increment)
   {
   }
 
-  uint16_t operator()()
+  IndexType operator()()
   {
     return mFunc(mData);
   }
 
 private:
   {
     return mFunc(mData);
   }
 
 private:
-  static uint16_t Increment(uintptr_t& data)
+  static IndexType Increment(uintptr_t& data)
   {
   {
-    return static_cast<uint16_t>(data++);
+    // mData was 'zero' at construct time. Just simply return counter start with 0.
+    return static_cast<IndexType>(data++);
   }
 
   }
 
-  static uint16_t IncrementPointer(uintptr_t& data)
+  static IndexType IncrementPointer(uintptr_t& data)
   {
   {
-    auto iPtr   = reinterpret_cast<const uint16_t*>(data);
+    auto iPtr   = reinterpret_cast<const IndexType*>(data);
     auto result = *iPtr;
     data        = reinterpret_cast<uintptr_t>(++iPtr);
     return result;
   }
 
   uintptr_t mData;
     auto result = *iPtr;
     data        = reinterpret_cast<uintptr_t>(++iPtr);
     return result;
   }
 
   uintptr_t mData;
-  uint16_t (*mFunc)(uintptr_t&);
+  IndexType (*mFunc)(uintptr_t&);
 };
 
 const char* QUAD("quad");
 };
 
 const char* QUAD("quad");
@@ -212,13 +215,23 @@ void ReadJointAccessor(MeshDefinition::RawData& raw, const MeshDefinition::Acces
   raw.mAttribs.push_back({"aJoints", Property::VECTOR4, static_cast<uint32_t>(outBufferSize / sizeof(Vector4)), std::move(buffer)});
 }
 
   raw.mAttribs.push_back({"aJoints", Property::VECTOR4, static_cast<uint32_t>(outBufferSize / sizeof(Vector4)), std::move(buffer)});
 }
 
-void GenerateNormals(MeshDefinition::RawData& raw)
+template<bool use32BitsIndices, typename IndexProviderType = IndexProvider<use32BitsIndices>>
+bool GenerateNormals(MeshDefinition::RawData& raw)
 {
 {
+  using IndexType = typename IndexProviderType::IndexType;
+
+  // mIndicies size must be even if we use 32bit indices.
+  if(DALI_UNLIKELY(use32BitsIndices && !raw.mIndices.empty() && !(raw.mIndices.size() % (sizeof(IndexType) / sizeof(uint16_t)) == 0)))
+  {
+    return false;
+  }
+
   auto& attribs = raw.mAttribs;
   DALI_ASSERT_DEBUG(attribs.size() > 0); // positions
   auto& attribs = raw.mAttribs;
   DALI_ASSERT_DEBUG(attribs.size() > 0); // positions
-  IndexProvider getIndex(raw.mIndices.data());
 
 
-  const uint32_t numIndices = raw.mIndices.empty() ? attribs[0].mNumElements : static_cast<uint32_t>(raw.mIndices.size());
+  IndexProviderType getIndex(raw.mIndices.data());
+
+  const uint32_t numIndices = raw.mIndices.empty() ? attribs[0].mNumElements : static_cast<uint32_t>(raw.mIndices.size() / (sizeof(IndexType) / sizeof(uint16_t)));
 
   auto* positions = reinterpret_cast<const Vector3*>(attribs[0].mData.data());
 
 
   auto* positions = reinterpret_cast<const Vector3*>(attribs[0].mData.data());
 
@@ -227,8 +240,8 @@ void GenerateNormals(MeshDefinition::RawData& raw)
 
   for(uint32_t i = 0; i < numIndices; i += 3)
   {
 
   for(uint32_t i = 0; i < numIndices; i += 3)
   {
-    uint16_t indices[]{getIndex(), getIndex(), getIndex()};
-    Vector3  pos[]{positions[indices[0]], positions[indices[1]], positions[indices[2]]};
+    IndexType indices[]{getIndex(), getIndex(), getIndex()};
+    Vector3   pos[]{positions[indices[0]], positions[indices[1]], positions[indices[2]]};
 
     Vector3 a = pos[1] - pos[0];
     Vector3 b = pos[2] - pos[0];
 
     Vector3 a = pos[1] - pos[0];
     Vector3 b = pos[2] - pos[0];
@@ -247,14 +260,24 @@ void GenerateNormals(MeshDefinition::RawData& raw)
   }
 
   attribs.push_back({"aNormal", Property::VECTOR3, attribs[0].mNumElements, std::move(buffer)});
   }
 
   attribs.push_back({"aNormal", Property::VECTOR3, attribs[0].mNumElements, std::move(buffer)});
+
+  return true;
 }
 
 }
 
-template<bool useVec3, bool hasUvs, typename T = std::conditional_t<useVec3, Vector3, Vector4>, typename = std::enable_if_t<(std::is_same<T, Vector3>::value || std::is_same<T, Vector4>::value)>>
+template<bool use32BitsIndices, bool useVec3, bool hasUvs, typename T = std::conditional_t<useVec3, Vector3, Vector4>, typename = std::enable_if_t<(std::is_same<T, Vector3>::value || std::is_same<T, Vector4>::value)>, typename IndexProviderType = IndexProvider<use32BitsIndices>>
 bool GenerateTangents(MeshDefinition::RawData& raw)
 {
 bool GenerateTangents(MeshDefinition::RawData& raw)
 {
+  using IndexType = typename IndexProviderType::IndexType;
+
+  // mIndicies size must be even if we use 32bit indices.
+  if(DALI_UNLIKELY(use32BitsIndices && !raw.mIndices.empty() && !(raw.mIndices.size() % (sizeof(IndexType) / sizeof(uint16_t)) == 0)))
+  {
+    return false;
+  }
+
   auto& attribs = raw.mAttribs;
   // Required positions, normals, uvs (if we have). If not, skip generation
   auto& attribs = raw.mAttribs;
   // Required positions, normals, uvs (if we have). If not, skip generation
-  if(attribs.size() < (2 + static_cast<size_t>(hasUvs)))
+  if(DALI_UNLIKELY(attribs.size() < (2 + static_cast<size_t>(hasUvs))))
   {
     return false;
   }
   {
     return false;
   }
@@ -264,17 +287,18 @@ bool GenerateTangents(MeshDefinition::RawData& raw)
 
   if constexpr(hasUvs)
   {
 
   if constexpr(hasUvs)
   {
-    IndexProvider  getIndex(raw.mIndices.data());
-    const uint32_t numIndices = raw.mIndices.empty() ? attribs[0].mNumElements : static_cast<uint32_t>(raw.mIndices.size());
+    IndexProviderType getIndex(raw.mIndices.data());
+
+    const uint32_t numIndices = raw.mIndices.empty() ? attribs[0].mNumElements : static_cast<uint32_t>(raw.mIndices.size() / (sizeof(IndexType) / sizeof(uint16_t)));
 
     auto* positions = reinterpret_cast<const Vector3*>(attribs[0].mData.data());
     auto* uvs       = reinterpret_cast<const Vector2*>(attribs[2].mData.data());
 
     for(uint32_t i = 0; i < numIndices; i += 3)
     {
 
     auto* positions = reinterpret_cast<const Vector3*>(attribs[0].mData.data());
     auto* uvs       = reinterpret_cast<const Vector2*>(attribs[2].mData.data());
 
     for(uint32_t i = 0; i < numIndices; i += 3)
     {
-      uint16_t indices[]{getIndex(), getIndex(), getIndex()};
-      Vector3  pos[]{positions[indices[0]], positions[indices[1]], positions[indices[2]]};
-      Vector2  uv[]{uvs[indices[0]], uvs[indices[1]], uvs[indices[2]]};
+      IndexType indices[]{getIndex(), getIndex(), getIndex()};
+      Vector3   pos[]{positions[indices[0]], positions[indices[1]], positions[indices[2]]};
+      Vector2   uv[]{uvs[indices[0]], uvs[indices[1]], uvs[indices[2]]};
 
       float x0 = pos[1].x - pos[0].x;
       float y0 = pos[1].y - pos[0].y;
 
       float x0 = pos[1].x - pos[0].x;
       float y0 = pos[1].y - pos[0].y;
@@ -670,18 +694,6 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector&
       {
         ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indices from '" << path << "'.";
       }
       {
         ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indices from '" << path << "'.";
       }
-
-      auto u16s = raw.mIndices.data();
-      auto u32s = reinterpret_cast<uint32_t*>(raw.mIndices.data());
-      auto end  = u32s + indexCount;
-      while(u32s != end)
-      {
-        *u16s = static_cast<uint16_t>(*u32s);
-        ++u16s;
-        ++u32s;
-      }
-
-      raw.mIndices.resize(indexCount);
     }
     else if(MaskMatch(mFlags, U8_INDICES))
     {
     }
     else if(MaskMatch(mFlags, U8_INDICES))
     {
@@ -689,7 +701,7 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector&
                           mIndices.mBlob.mStride >= sizeof(uint8_t)) &&
                          "Index buffer length not a multiple of element size");
       const auto indexCount = mIndices.mBlob.GetBufferSize() / sizeof(uint8_t);
                           mIndices.mBlob.mStride >= sizeof(uint8_t)) &&
                          "Index buffer length not a multiple of element size");
       const auto indexCount = mIndices.mBlob.GetBufferSize() / sizeof(uint8_t);
-      raw.mIndices.resize(indexCount); // NOTE: we need space for uint32_ts initially.
+      raw.mIndices.resize(indexCount); // NOTE: we need space for uint16_ts initially.
 
       std::string path;
       auto        u8s    = reinterpret_cast<uint8_t*>(raw.mIndices.data()) + indexCount;
 
       std::string path;
       auto        u8s    = reinterpret_cast<uint8_t*>(raw.mIndices.data()) + indexCount;
@@ -783,8 +795,20 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector&
   else if(mNormals.mBlob.mLength != 0 && isTriangles)
   {
     DALI_ASSERT_DEBUG(mNormals.mBlob.mLength == mPositions.mBlob.GetBufferSize());
   else if(mNormals.mBlob.mLength != 0 && isTriangles)
   {
     DALI_ASSERT_DEBUG(mNormals.mBlob.mLength == mPositions.mBlob.GetBufferSize());
-    GenerateNormals(raw);
-    hasNormals = true;
+    static const std::function<bool(RawData&)> GenerateNormalsFunction[2] =
+      {
+        GenerateNormals<false>,
+        GenerateNormals<true>,
+      };
+    const bool generateSuccessed = GenerateNormalsFunction[MaskMatch(mFlags, U32_INDICES)](raw);
+    if(!generateSuccessed)
+    {
+      DALI_LOG_ERROR("Failed to generate normal\n");
+    }
+    else
+    {
+      hasNormals = true;
+    }
   }
 
   const auto hasUvs = mTexCoords.IsDefined();
   }
 
   const auto hasUvs = mTexCoords.IsDefined();
@@ -815,7 +839,7 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector&
       }
     }
 
       }
     }
 
-    mTexCoords.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector2)), reinterpret_cast<float*>(buffer.data()));
+    mTexCoords.mBlob.ApplyMinMax(static_cast<uint32_t>(uvCount), reinterpret_cast<float*>(buffer.data()));
 
     raw.mAttribs.push_back({"aTexCoord", Property::VECTOR2, static_cast<uint32_t>(uvCount), std::move(buffer)});
   }
 
     raw.mAttribs.push_back({"aTexCoord", Property::VECTOR2, static_cast<uint32_t>(uvCount), std::move(buffer)});
   }
@@ -842,18 +866,29 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector&
   else if(mTangents.mBlob.mLength != 0 && hasNormals && isTriangles)
   {
     DALI_ASSERT_DEBUG(mTangents.mBlob.mLength == mNormals.mBlob.GetBufferSize());
   else if(mTangents.mBlob.mLength != 0 && hasNormals && isTriangles)
   {
     DALI_ASSERT_DEBUG(mTangents.mBlob.mLength == mNormals.mBlob.GetBufferSize());
-    static const std::function<bool(RawData&)> GenerateTangentsFunction[2][2] =
+    static const std::function<bool(RawData&)> GenerateTangentsFunction[2][2][2] =
       {
         {
       {
         {
-          GenerateTangents<false, false>,
-          GenerateTangents<false, true>,
+          {
+            GenerateTangents<false, false, false>,
+            GenerateTangents<false, false, true>,
+          },
+          {
+            GenerateTangents<false, true, false>,
+            GenerateTangents<false, true, true>,
+          },
         },
         {
         },
         {
-          GenerateTangents<true, false>,
-          GenerateTangents<true, true>,
-        },
-      };
-    const bool generateSuccessed = GenerateTangentsFunction[mTangentType == Property::VECTOR3][hasUvs](raw);
+          {
+            GenerateTangents<true, false, false>,
+            GenerateTangents<true, false, true>,
+          },
+          {
+            GenerateTangents<true, true, false>,
+            GenerateTangents<true, true, true>,
+          },
+        }};
+    const bool generateSuccessed = GenerateTangentsFunction[MaskMatch(mFlags, U32_INDICES)][mTangentType == Property::VECTOR3][hasUvs](raw);
     if(!generateSuccessed)
     {
       DALI_LOG_ERROR("Failed to generate tangents\n");
     if(!generateSuccessed)
     {
       DALI_LOG_ERROR("Failed to generate tangents\n");
@@ -1008,7 +1043,15 @@ MeshGeometry MeshDefinition::Load(RawData&& raw) const
   {
     if(!raw.mIndices.empty())
     {
   {
     if(!raw.mIndices.empty())
     {
-      meshGeometry.geometry.SetIndexBuffer(raw.mIndices.data(), raw.mIndices.size());
+      if(MaskMatch(mFlags, U32_INDICES))
+      {
+        // TODO : We can only store indeces as uint16_type. Send Dali::Geometry that we use it as uint32_t actual.
+        meshGeometry.geometry.SetIndexBuffer(reinterpret_cast<const uint32_t*>(raw.mIndices.data()), raw.mIndices.size() / 2);
+      }
+      else
+      {
+        meshGeometry.geometry.SetIndexBuffer(raw.mIndices.data(), raw.mIndices.size());
+      }
     }
 
     for(auto& a : raw.mAttribs)
     }
 
     for(auto& a : raw.mAttribs)