Fix svace issue (uint32_t to long or std::streamsize)
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / mesh-definition.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // INTERNAL INCLUDES
19 #include "dali-scene3d/public-api/loader/mesh-definition.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/adaptor-framework/file-stream.h>
23 #include <dali/integration-api/debug.h>
24 #include <cstring>
25 #include <fstream>
26 #include "dali/devel-api/adaptor-framework/pixel-buffer.h"
27
28 namespace Dali
29 {
30 namespace Scene3D
31 {
32 namespace Loader
33 {
34 namespace
35 {
36 class IndexProvider
37 {
38 public:
39   IndexProvider(const uint16_t* indices)
40   : mData(reinterpret_cast<uintptr_t>(indices)),
41     mFunc(indices ? IncrementPointer : Increment)
42   {
43   }
44
45   uint16_t operator()()
46   {
47     return mFunc(mData);
48   }
49
50 private:
51   static uint16_t Increment(uintptr_t& data)
52   {
53     return static_cast<uint16_t>(data++);
54   }
55
56   static uint16_t IncrementPointer(uintptr_t& data)
57   {
58     auto iPtr   = reinterpret_cast<const uint16_t*>(data);
59     auto result = *iPtr;
60     data        = reinterpret_cast<uintptr_t>(++iPtr);
61     return result;
62   }
63
64   uintptr_t mData;
65   uint16_t (*mFunc)(uintptr_t&);
66 };
67
68 const std::string QUAD("quad");
69
70 ///@brief Reads a blob from the given stream @a source into @a target, which must have
71 /// at least @a descriptor.length bytes.
72 bool ReadBlob(const MeshDefinition::Blob& descriptor, std::istream& source, uint8_t* target)
73 {
74   source.clear();
75   if(!source.seekg(descriptor.mOffset, std::istream::beg))
76   {
77     return false;
78   }
79
80   if(descriptor.IsConsecutive())
81   {
82     return !!source.read(reinterpret_cast<char*>(target), static_cast<std::streamsize>(static_cast<size_t>(descriptor.mLength)));
83   }
84   else
85   {
86     if(descriptor.mStride > descriptor.mElementSizeHint)
87     {
88       const uint32_t diff      = descriptor.mStride - descriptor.mElementSizeHint;
89       uint32_t       readSize  = 0;
90       uint32_t       totalSize = (descriptor.mLength / descriptor.mElementSizeHint) * descriptor.mStride;
91       while(readSize < totalSize &&
92             source.read(reinterpret_cast<char*>(target), descriptor.mElementSizeHint) &&
93             source.seekg(diff, std::istream::cur))
94       {
95         readSize += descriptor.mStride;
96         target += descriptor.mElementSizeHint;
97       }
98       return readSize == totalSize;
99     }
100   }
101   return false;
102 }
103
104 template<typename T>
105 void ReadValues(const std::vector<uint8_t>& valuesBuffer, const std::vector<uint8_t>& indicesBuffer, uint8_t* target, uint32_t count, uint32_t elementSizeHint)
106 {
107   const T* const indicesPtr = reinterpret_cast<const T* const>(indicesBuffer.data());
108   for(uint32_t index = 0u; index < count; ++index)
109   {
110     uint32_t valuesIndex = indicesPtr[index] * elementSizeHint;
111     memcpy(target + valuesIndex, &valuesBuffer[index * elementSizeHint], elementSizeHint);
112   }
113 }
114
115 bool ReadAccessor(const MeshDefinition::Accessor& accessor, std::istream& source, uint8_t* target)
116 {
117   bool success = false;
118
119   if(accessor.mBlob.IsDefined())
120   {
121     success = ReadBlob(accessor.mBlob, source, target);
122     if(!success)
123     {
124       return false;
125     }
126   }
127
128   if(accessor.mSparse)
129   {
130     const MeshDefinition::Blob& indices = accessor.mSparse->mIndices;
131     const MeshDefinition::Blob& values  = accessor.mSparse->mValues;
132
133     if(!indices.IsDefined() || !values.IsDefined())
134     {
135       return false;
136     }
137
138     const auto           indicesBufferSize = indices.GetBufferSize();
139     std::vector<uint8_t> indicesBuffer(indicesBufferSize);
140     success = ReadBlob(indices, source, indicesBuffer.data());
141     if(!success)
142     {
143       return false;
144     }
145
146     const auto           valuesBufferSize = values.GetBufferSize();
147     std::vector<uint8_t> valuesBuffer(valuesBufferSize);
148     success = ReadBlob(values, source, valuesBuffer.data());
149     if(!success)
150     {
151       return false;
152     }
153
154     switch(indices.mElementSizeHint)
155     {
156       case 1u:
157       {
158         ReadValues<uint8_t>(valuesBuffer, indicesBuffer, target, accessor.mSparse->mCount, values.mElementSizeHint);
159         break;
160       }
161       case 2u:
162       {
163         ReadValues<uint16_t>(valuesBuffer, indicesBuffer, target, accessor.mSparse->mCount, values.mElementSizeHint);
164         break;
165       }
166       case 4u:
167       {
168         ReadValues<uint32_t>(valuesBuffer, indicesBuffer, target, accessor.mSparse->mCount, values.mElementSizeHint);
169         break;
170       }
171       default:
172         DALI_ASSERT_DEBUG(!"Unsupported type for an index");
173     }
174   }
175
176   return success;
177 }
178
179 template<typename T>
180 void ReadJointAccessor(MeshDefinition::RawData& raw, const MeshDefinition::Accessor& accessor, std::istream& source, const std::string& meshPath)
181 {
182   constexpr auto sizeofBlobUnit = sizeof(T) * 4;
183
184   DALI_ASSERT_ALWAYS(((accessor.mBlob.mLength % sizeofBlobUnit == 0) ||
185                       accessor.mBlob.mStride >= sizeofBlobUnit) &&
186                      "Joints buffer length not a multiple of element size");
187   const auto inBufferSize  = accessor.mBlob.GetBufferSize();
188   const auto outBufferSize = (sizeof(Vector4) / sizeofBlobUnit) * inBufferSize;
189
190   std::vector<uint8_t> buffer(outBufferSize);
191   auto                 inBuffer = buffer.data() + outBufferSize - inBufferSize;
192   if(!ReadAccessor(accessor, source, inBuffer))
193   {
194     ExceptionFlinger(ASSERT_LOCATION) << "Failed to read joints from '" << meshPath << "'.";
195   }
196
197   if constexpr(sizeofBlobUnit != sizeof(Vector4))
198   {
199     auto       floats = reinterpret_cast<float*>(buffer.data());
200     const auto end    = inBuffer + inBufferSize;
201     while(inBuffer != end)
202     {
203       const auto value = *reinterpret_cast<T*>(inBuffer);
204       *floats          = static_cast<float>(value);
205
206       inBuffer += sizeof(T);
207       ++floats;
208     }
209   }
210   raw.mAttribs.push_back({"aJoints", Property::VECTOR4, static_cast<uint32_t>(outBufferSize / sizeof(Vector4)), std::move(buffer)});
211 }
212
213 void GenerateNormals(MeshDefinition::RawData& raw)
214 {
215   auto& attribs = raw.mAttribs;
216   DALI_ASSERT_DEBUG(attribs.size() > 0); // positions
217   IndexProvider getIndex(raw.mIndices.data());
218
219   const uint32_t numIndices = raw.mIndices.empty() ? attribs[0].mNumElements : static_cast<uint32_t>(raw.mIndices.size());
220
221   auto* positions = reinterpret_cast<const Vector3*>(attribs[0].mData.data());
222
223   std::vector<uint8_t> buffer(attribs[0].mNumElements * sizeof(Vector3));
224   auto                 normals = reinterpret_cast<Vector3*>(buffer.data());
225
226   for(uint32_t i = 0; i < numIndices; i += 3)
227   {
228     uint16_t indices[]{getIndex(), getIndex(), getIndex()};
229     Vector3  pos[]{positions[indices[0]], positions[indices[1]], positions[indices[2]]};
230
231     Vector3 a = pos[1] - pos[0];
232     Vector3 b = pos[2] - pos[0];
233
234     Vector3 normal(a.Cross(b));
235     normals[indices[0]] += normal;
236     normals[indices[1]] += normal;
237     normals[indices[2]] += normal;
238   }
239
240   auto iEnd = normals + attribs[0].mNumElements;
241   while(normals != iEnd)
242   {
243     normals->Normalize();
244     ++normals;
245   }
246
247   attribs.push_back({"aNormal", Property::VECTOR3, attribs[0].mNumElements, std::move(buffer)});
248 }
249
250 void GenerateTangentsWithUvs(MeshDefinition::RawData& raw)
251 {
252   auto& attribs = raw.mAttribs;
253   DALI_ASSERT_DEBUG(attribs.size() > 2); // positions, normals, uvs
254   IndexProvider getIndex(raw.mIndices.data());
255
256   const uint32_t numIndices = raw.mIndices.empty() ? attribs[0].mNumElements : static_cast<uint32_t>(raw.mIndices.size());
257
258   auto* positions = reinterpret_cast<const Vector3*>(attribs[0].mData.data());
259   auto* uvs       = reinterpret_cast<const Vector2*>(attribs[2].mData.data());
260
261   std::vector<uint8_t> buffer(attribs[0].mNumElements * sizeof(Vector3));
262   auto                 tangents = reinterpret_cast<Vector3*>(buffer.data());
263
264   for(uint32_t i = 0; i < numIndices; i += 3)
265   {
266     uint16_t indices[]{getIndex(), getIndex(), getIndex()};
267     Vector3  pos[]{positions[indices[0]], positions[indices[1]], positions[indices[2]]};
268     Vector2  uv[]{uvs[indices[0]], uvs[indices[1]], uvs[indices[2]]};
269
270     float x0 = pos[1].x - pos[0].x;
271     float y0 = pos[1].y - pos[0].y;
272     float z0 = pos[1].z - pos[0].z;
273
274     float x1 = pos[2].x - pos[0].x;
275     float y1 = pos[2].y - pos[0].y;
276     float z1 = pos[2].z - pos[0].z;
277
278     float s0 = uv[1].x - uv[0].x;
279     float t0 = uv[1].y - uv[0].y;
280
281     float s1 = uv[2].x - uv[0].x;
282     float t1 = uv[2].y - uv[0].y;
283
284     float   r = 1.f / (s0 * t1 - t0 * s1);
285     Vector3 tangent((x0 * t1 - t0 * x1) * r, (y0 * t1 - t0 * y1) * r, (z0 * t1 - t0 * z1) * r);
286     tangents[indices[0]] += tangent;
287     tangents[indices[1]] += tangent;
288     tangents[indices[2]] += tangent;
289   }
290
291   auto* normals = reinterpret_cast<const Vector3*>(attribs[1].mData.data());
292   auto  iEnd    = normals + attribs[1].mNumElements;
293   while(normals != iEnd)
294   {
295     *tangents -= *normals * normals->Dot(*tangents);
296     tangents->Normalize();
297
298     ++tangents;
299     ++normals;
300   }
301   attribs.push_back({"aTangent", Property::VECTOR3, attribs[0].mNumElements, std::move(buffer)});
302 }
303
304 void GenerateTangents(MeshDefinition::RawData& raw)
305 {
306   auto& attribs = raw.mAttribs;
307   DALI_ASSERT_DEBUG(attribs.size() > 1); // positions, normals
308
309   auto* normals = reinterpret_cast<const Vector3*>(attribs[1].mData.data());
310
311   std::vector<uint8_t> buffer(attribs[0].mNumElements * sizeof(Vector3));
312   auto                 tangents = reinterpret_cast<Vector3*>(buffer.data());
313
314   auto iEnd = normals + attribs[1].mNumElements;
315   while(normals != iEnd)
316   {
317     Vector3 t[]{normals->Cross(Vector3::XAXIS), normals->Cross(Vector3::YAXIS)};
318
319     *tangents = t[t[1].LengthSquared() > t[0].LengthSquared()];
320     *tangents -= *normals * normals->Dot(*tangents);
321     tangents->Normalize();
322
323     ++tangents;
324     ++normals;
325   }
326   attribs.push_back({"aTangent", Property::VECTOR3, attribs[0].mNumElements, std::move(buffer)});
327 }
328
329 void CalculateTextureSize(uint32_t totalTextureSize, uint32_t& textureWidth, uint32_t& textureHeight)
330 {
331   DALI_ASSERT_DEBUG(0u != totalTextureSize && "totalTextureSize is zero.")
332
333   // Calculate the dimensions of the texture.
334   // The total size of the texture is the length of the blend shapes blob.
335
336   textureWidth  = 0u;
337   textureHeight = 0u;
338
339   if(0u == totalTextureSize)
340   {
341     // nothing to do.
342     return;
343   }
344
345   const uint32_t pow2      = static_cast<uint32_t>(ceil(log2(totalTextureSize)));
346   const uint32_t powWidth  = pow2 >> 1u;
347   const uint32_t powHeight = pow2 - powWidth;
348
349   textureWidth  = 1u << powWidth;
350   textureHeight = 1u << powHeight;
351 }
352
353 void CalculateGltf2BlendShapes(uint8_t* geometryBuffer, const std::vector<MeshDefinition::BlendShape>& blendShapes, uint32_t numberOfVertices, float& blendShapeUnnormalizeFactor, BufferDefinition::Vector& buffers)
354 {
355   uint32_t geometryBufferIndex = 0u;
356   float    maxDistance         = 0.f;
357   Vector3* geometryBufferV3    = reinterpret_cast<Vector3*>(geometryBuffer);
358   for(const auto& blendShape : blendShapes)
359   {
360     if(blendShape.deltas.IsDefined())
361     {
362       DALI_ASSERT_ALWAYS(((blendShape.deltas.mBlob.mLength % sizeof(Vector3) == 0u) ||
363                           blendShape.deltas.mBlob.mStride >= sizeof(Vector3)) &&
364                          "Blend Shape position buffer length not a multiple of element size");
365
366       const auto           bufferSize = blendShape.deltas.mBlob.GetBufferSize();
367       std::vector<uint8_t> buffer(bufferSize);
368       if(ReadAccessor(blendShape.deltas, buffers[blendShape.deltas.mBufferIdx].GetBufferStream(), buffer.data()))
369       {
370         blendShape.deltas.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()));
371         // Calculate the difference with the original mesh.
372         // Find the max distance to normalize the deltas.
373         const Vector3* const deltasBuffer = reinterpret_cast<const Vector3* const>(buffer.data());
374
375         for(uint32_t index = 0u; index < numberOfVertices; ++index)
376         {
377           Vector3& delta = geometryBufferV3[geometryBufferIndex++];
378           delta          = deltasBuffer[index];
379
380           maxDistance = std::max(maxDistance, delta.LengthSquared());
381         }
382       }
383     }
384
385     if(blendShape.normals.IsDefined())
386     {
387       DALI_ASSERT_ALWAYS(((blendShape.normals.mBlob.mLength % sizeof(Vector3) == 0u) ||
388                           blendShape.normals.mBlob.mStride >= sizeof(Vector3)) &&
389                          "Blend Shape normals buffer length not a multiple of element size");
390
391       const auto           bufferSize = blendShape.normals.mBlob.GetBufferSize();
392       std::vector<uint8_t> buffer(bufferSize);
393       if(ReadAccessor(blendShape.normals, buffers[blendShape.normals.mBufferIdx].GetBufferStream(), buffer.data()))
394       {
395         blendShape.normals.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()));
396
397         // Calculate the difference with the original mesh, and translate to make all values positive.
398         const Vector3* const deltasBuffer = reinterpret_cast<const Vector3* const>(buffer.data());
399
400         for(uint32_t index = 0u; index < numberOfVertices; ++index)
401         {
402           Vector3& delta = geometryBufferV3[geometryBufferIndex++];
403           delta          = deltasBuffer[index];
404
405           delta.x *= 0.5f;
406           delta.y *= 0.5f;
407           delta.z *= 0.5f;
408
409           delta.x += 0.5f;
410           delta.y += 0.5f;
411           delta.z += 0.5f;
412         }
413       }
414     }
415
416     if(blendShape.tangents.IsDefined())
417     {
418       DALI_ASSERT_ALWAYS(((blendShape.tangents.mBlob.mLength % sizeof(Vector3) == 0u) ||
419                           blendShape.tangents.mBlob.mStride >= sizeof(Vector3)) &&
420                          "Blend Shape tangents buffer length not a multiple of element size");
421
422       const auto           bufferSize = blendShape.tangents.mBlob.GetBufferSize();
423       std::vector<uint8_t> buffer(bufferSize);
424       if(ReadAccessor(blendShape.tangents, buffers[blendShape.tangents.mBufferIdx].GetBufferStream(), buffer.data()))
425       {
426         blendShape.tangents.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()));
427
428         // Calculate the difference with the original mesh, and translate to make all values positive.
429         const Vector3* const deltasBuffer = reinterpret_cast<const Vector3* const>(buffer.data());
430
431         for(uint32_t index = 0u; index < numberOfVertices; ++index)
432         {
433           Vector3& delta = geometryBufferV3[geometryBufferIndex++];
434           delta          = deltasBuffer[index];
435
436           delta.x *= 0.5f;
437           delta.y *= 0.5f;
438           delta.z *= 0.5f;
439
440           delta.x += 0.5f;
441           delta.y += 0.5f;
442           delta.z += 0.5f;
443         }
444       }
445     }
446   }
447
448   geometryBufferIndex = 0u;
449   for(const auto& blendShape : blendShapes)
450   {
451     // Normalize all the deltas and translate to a possitive value.
452     // Deltas are going to be passed to the shader in a color texture
453     // whose values that are less than zero are clamped.
454     if(blendShape.deltas.IsDefined())
455     {
456       const float normalizeFactor = (fabsf(maxDistance) < Math::MACHINE_EPSILON_1000) ? 1.f : (0.5f / sqrtf(maxDistance));
457
458       for(uint32_t index = 0u; index < numberOfVertices; ++index)
459       {
460         Vector3& delta = geometryBufferV3[geometryBufferIndex++];
461         delta.x        = Clamp(((delta.x * normalizeFactor) + 0.5f), 0.f, 1.f);
462         delta.y        = Clamp(((delta.y * normalizeFactor) + 0.5f), 0.f, 1.f);
463         delta.z        = Clamp(((delta.z * normalizeFactor) + 0.5f), 0.f, 1.f);
464       }
465
466       // Calculate and store the unnormalize factor.
467       blendShapeUnnormalizeFactor = 1.f / normalizeFactor;
468     }
469
470     if(blendShape.normals.IsDefined())
471     {
472       geometryBufferIndex += numberOfVertices;
473     }
474
475     if(blendShape.tangents.IsDefined())
476     {
477       geometryBufferIndex += numberOfVertices;
478     }
479   }
480 }
481
482 std::iostream& GetAvailableData(std::fstream& meshStream, const std::string& meshPath, BufferDefinition& buffer, std::string& availablePath)
483 {
484   auto& stream  = (meshStream.is_open()) ? meshStream : buffer.GetBufferStream();
485   availablePath = (meshStream.is_open()) ? meshPath : buffer.GetUri();
486   return stream;
487 }
488
489 } // namespace
490
491 MeshDefinition::SparseBlob::SparseBlob(const Blob& indices, const Blob& values, uint32_t count)
492 : mIndices{indices},
493   mValues{values},
494   mCount{count}
495 {
496 }
497
498 MeshDefinition::SparseBlob::SparseBlob(Blob&& indices, Blob&& values, uint32_t count)
499 : mIndices(std::move(indices)),
500   mValues(std::move(values)),
501   mCount{count}
502 {
503 }
504
505 MeshDefinition::Accessor::Accessor(const MeshDefinition::Blob&       blob,
506                                    const MeshDefinition::SparseBlob& sparse,
507                                    Index                             bufferIndex)
508 : mBlob{blob},
509   mSparse{(sparse.mIndices.IsDefined() && sparse.mValues.IsDefined()) ? new SparseBlob{sparse} : nullptr},
510   mBufferIdx(bufferIndex)
511 {
512 }
513
514 MeshDefinition::Accessor::Accessor(MeshDefinition::Blob&&       blob,
515                                    MeshDefinition::SparseBlob&& sparse,
516                                    Index                        bufferIndex)
517 : mBlob{std::move(blob)},
518   mSparse{(sparse.mIndices.IsDefined() && sparse.mValues.IsDefined()) ? new SparseBlob{std::move(sparse)} : nullptr},
519   mBufferIdx(bufferIndex)
520 {
521 }
522
523 void MeshDefinition::Blob::ComputeMinMax(std::vector<float>& min, std::vector<float>& max, uint32_t numComponents, uint32_t count, const float* values)
524 {
525   min.assign(numComponents, MAXFLOAT);
526   max.assign(numComponents, -MAXFLOAT);
527   for(uint32_t i = 0; i < count; ++i)
528   {
529     for(uint32_t j = 0; j < numComponents; ++j)
530     {
531       min[j] = std::min(min[j], *values);
532       max[j] = std::max(max[j], *values);
533       values++;
534     }
535   }
536 }
537
538 void MeshDefinition::Blob::ApplyMinMax(const std::vector<float>& min, const std::vector<float>& max, uint32_t count, float* values)
539 {
540   DALI_ASSERT_DEBUG(max.size() == min.size() || max.size() * min.size() == 0);
541   const auto numComponents = std::max(min.size(), max.size());
542
543   using ClampFn   = void (*)(const float*, const float*, uint32_t, float&);
544   ClampFn clampFn = min.empty() ? (max.empty() ? static_cast<ClampFn>(nullptr) : [](const float* min, const float* max, uint32_t i, float& value) { value = std::min(max[i], value); })
545                                 : (max.empty() ? [](const float* min, const float* max, uint32_t i, float& value) { value = std::max(min[i], value); }
546                                                : static_cast<ClampFn>([](const float* min, const float* max, uint32_t i, float& value) { value = std::min(std::max(min[i], value), max[i]); }));
547
548   if(!clampFn)
549   {
550     return;
551   }
552
553   auto end = values + count * numComponents;
554   while(values != end)
555   {
556     auto     nextElement = values + numComponents;
557     uint32_t i           = 0;
558     while(values != nextElement)
559     {
560       clampFn(min.data(), max.data(), i, *values);
561       ++values;
562       ++i;
563     }
564   }
565 }
566
567 MeshDefinition::Blob::Blob(uint32_t offset, uint32_t length, uint16_t stride, uint16_t elementSizeHint, const std::vector<float>& min, const std::vector<float>& max)
568 : mOffset(offset),
569   mLength(length),
570   mStride(stride),
571   mElementSizeHint(elementSizeHint),
572   mMin(min),
573   mMax(max)
574 {
575 }
576
577 uint32_t MeshDefinition::Blob::GetBufferSize() const
578 {
579   return mLength;
580 }
581
582 void MeshDefinition::Blob::ComputeMinMax(uint32_t numComponents, uint32_t count, float* values)
583 {
584   ComputeMinMax(mMin, mMax, numComponents, count, values);
585 }
586
587 void MeshDefinition::Blob::ApplyMinMax(uint32_t count, float* values) const
588 {
589   ApplyMinMax(mMin, mMax, count, values);
590 }
591
592 void MeshDefinition::RawData::Attrib::AttachBuffer(Geometry& g) const
593 {
594   Property::Map attribMap;
595   attribMap[mName]          = mType;
596   VertexBuffer attribBuffer = VertexBuffer::New(attribMap);
597   attribBuffer.SetData(mData.data(), mNumElements);
598
599   g.AddVertexBuffer(attribBuffer);
600 }
601
602 bool MeshDefinition::IsQuad() const
603 {
604   return CaseInsensitiveStringCompare(QUAD, mUri);
605 }
606
607 bool MeshDefinition::IsSkinned() const
608 {
609   return mJoints0.IsDefined() && mWeights0.IsDefined();
610 }
611
612 bool MeshDefinition::HasBlendShapes() const
613 {
614   return !mBlendShapes.empty();
615 }
616
617 void MeshDefinition::RequestNormals()
618 {
619   mNormals.mBlob.mLength = mPositions.mBlob.GetBufferSize();
620 }
621
622 void MeshDefinition::RequestTangents()
623 {
624   mTangents.mBlob.mLength = mNormals.mBlob.GetBufferSize();
625 }
626
627 MeshDefinition::RawData
628 MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& buffers)
629 {
630   RawData raw;
631   if(IsQuad())
632   {
633     return raw;
634   }
635
636   std::string meshPath;
637   meshPath = modelsPath + mUri;
638   std::fstream fileStream;
639   if(!mUri.empty())
640   {
641     fileStream.open(meshPath, std::ios::in | std::ios::binary);
642     if(!fileStream.is_open())
643     {
644       DALI_LOG_ERROR("Fail to open buffer from %s.\n", meshPath.c_str());
645     }
646   }
647
648   if(mIndices.IsDefined())
649   {
650     if(MaskMatch(mFlags, U32_INDICES))
651     {
652       DALI_ASSERT_ALWAYS(((mIndices.mBlob.mLength % sizeof(uint32_t) == 0) ||
653                           mIndices.mBlob.mStride >= sizeof(uint32_t)) &&
654                          "Index buffer length not a multiple of element size");
655       const auto indexCount = mIndices.mBlob.GetBufferSize() / sizeof(uint32_t);
656       raw.mIndices.resize(indexCount * 2); // NOTE: we need space for uint32_ts initially.
657
658       std::string path;
659       auto&       stream = GetAvailableData(fileStream, meshPath, buffers[mIndices.mBufferIdx], path);
660       if(!ReadAccessor(mIndices, stream, reinterpret_cast<uint8_t*>(raw.mIndices.data())))
661       {
662         ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indices from '" << path << "'.";
663       }
664
665       auto u16s = raw.mIndices.data();
666       auto u32s = reinterpret_cast<uint32_t*>(raw.mIndices.data());
667       auto end  = u32s + indexCount;
668       while(u32s != end)
669       {
670         *u16s = static_cast<uint16_t>(*u32s);
671         ++u16s;
672         ++u32s;
673       }
674
675       raw.mIndices.resize(indexCount);
676     }
677     else if(MaskMatch(mFlags, U8_INDICES))
678     {
679       DALI_ASSERT_ALWAYS(((mIndices.mBlob.mLength % sizeof(uint8_t) == 0) ||
680                           mIndices.mBlob.mStride >= sizeof(uint8_t)) &&
681                          "Index buffer length not a multiple of element size");
682       const auto indexCount = mIndices.mBlob.GetBufferSize() / sizeof(uint8_t);
683       raw.mIndices.resize(indexCount); // NOTE: we need space for uint32_ts initially.
684
685       std::string path;
686       auto        u8s    = reinterpret_cast<uint8_t*>(raw.mIndices.data()) + indexCount;
687       auto&       stream = GetAvailableData(fileStream, meshPath, buffers[mIndices.mBufferIdx], path);
688       if(!ReadAccessor(mIndices, stream, u8s))
689       {
690         ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indices from '" << path << "'.";
691       }
692
693       auto u16s = raw.mIndices.data();
694       auto end  = u8s + indexCount;
695       while(u8s != end)
696       {
697         *u16s = static_cast<uint16_t>(*u8s);
698         ++u16s;
699         ++u8s;
700       }
701     }
702     else
703     {
704       DALI_ASSERT_ALWAYS(((mIndices.mBlob.mLength % sizeof(unsigned short) == 0) ||
705                           mIndices.mBlob.mStride >= sizeof(unsigned short)) &&
706                          "Index buffer length not a multiple of element size");
707       raw.mIndices.resize(mIndices.mBlob.mLength / sizeof(unsigned short));
708
709       std::string path;
710       auto&       stream = GetAvailableData(fileStream, meshPath, buffers[mIndices.mBufferIdx], path);
711       if(!ReadAccessor(mIndices, stream, reinterpret_cast<uint8_t*>(raw.mIndices.data())))
712       {
713         ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indices from '" << path << "'.";
714       }
715     }
716   }
717
718   std::vector<Vector3> positions;
719   if(mPositions.IsDefined())
720   {
721     DALI_ASSERT_ALWAYS(((mPositions.mBlob.mLength % sizeof(Vector3) == 0) ||
722                         mPositions.mBlob.mStride >= sizeof(Vector3)) &&
723                        "Position buffer length not a multiple of element size");
724     const auto           bufferSize = mPositions.mBlob.GetBufferSize();
725     std::vector<uint8_t> buffer(bufferSize);
726
727     std::string path;
728     auto&       stream = GetAvailableData(fileStream, meshPath, buffers[mPositions.mBufferIdx], path);
729     if(!ReadAccessor(mPositions, stream, buffer.data()))
730     {
731       ExceptionFlinger(ASSERT_LOCATION) << "Failed to read positions from '" << path << "'.";
732     }
733
734     uint32_t numVector3 = static_cast<uint32_t>(bufferSize / sizeof(Vector3));
735     if(mPositions.mBlob.mMin.size() != 3u || mPositions.mBlob.mMax.size() != 3u)
736     {
737       mPositions.mBlob.ComputeMinMax(3u, numVector3, reinterpret_cast<float*>(buffer.data()));
738     }
739     else
740     {
741       mPositions.mBlob.ApplyMinMax(numVector3, reinterpret_cast<float*>(buffer.data()));
742     }
743
744     if(HasBlendShapes())
745     {
746       positions.resize(numVector3);
747       std::copy(buffer.data(), buffer.data() + buffer.size(), reinterpret_cast<uint8_t*>(positions.data()));
748     }
749
750     raw.mAttribs.push_back({"aPosition", Property::VECTOR3, numVector3, std::move(buffer)});
751   }
752
753   const auto isTriangles = mPrimitiveType == Geometry::TRIANGLES;
754   auto       hasNormals  = mNormals.IsDefined();
755   if(hasNormals)
756   {
757     DALI_ASSERT_ALWAYS(((mNormals.mBlob.mLength % sizeof(Vector3) == 0) ||
758                         mNormals.mBlob.mStride >= sizeof(Vector3)) &&
759                        "Normal buffer length not a multiple of element size");
760     const auto           bufferSize = mNormals.mBlob.GetBufferSize();
761     std::vector<uint8_t> buffer(bufferSize);
762
763     std::string path;
764     auto&       stream = GetAvailableData(fileStream, meshPath, buffers[mNormals.mBufferIdx], path);
765     if(!ReadAccessor(mNormals, stream, buffer.data()))
766     {
767       ExceptionFlinger(ASSERT_LOCATION) << "Failed to read normals from '" << path << "'.";
768     }
769
770     mNormals.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()));
771
772     raw.mAttribs.push_back({"aNormal", Property::VECTOR3, static_cast<uint32_t>(bufferSize / sizeof(Vector3)), std::move(buffer)});
773   }
774   else if(mNormals.mBlob.mLength != 0 && isTriangles)
775   {
776     DALI_ASSERT_DEBUG(mNormals.mBlob.mLength == mPositions.mBlob.GetBufferSize());
777     GenerateNormals(raw);
778     hasNormals = true;
779   }
780
781   const auto hasUvs = mTexCoords.IsDefined();
782   if(hasUvs)
783   {
784     DALI_ASSERT_ALWAYS(((mTexCoords.mBlob.mLength % sizeof(Vector2) == 0) ||
785                         mTexCoords.mBlob.mStride >= sizeof(Vector2)) &&
786                        "Normal buffer length not a multiple of element size");
787     const auto           bufferSize = mTexCoords.mBlob.GetBufferSize();
788     std::vector<uint8_t> buffer(bufferSize);
789
790     std::string path;
791     auto&       stream = GetAvailableData(fileStream, meshPath, buffers[mTexCoords.mBufferIdx], path);
792     if(!ReadAccessor(mTexCoords, stream, buffer.data()))
793     {
794       ExceptionFlinger(ASSERT_LOCATION) << "Failed to read uv-s from '" << path << "'.";
795     }
796
797     const auto uvCount = bufferSize / sizeof(Vector2);
798     if(MaskMatch(mFlags, FLIP_UVS_VERTICAL))
799     {
800       auto uv    = reinterpret_cast<Vector2*>(buffer.data());
801       auto uvEnd = uv + uvCount;
802       while(uv != uvEnd)
803       {
804         uv->y = 1.0f - uv->y;
805         ++uv;
806       }
807     }
808
809     mTexCoords.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector2)), reinterpret_cast<float*>(buffer.data()));
810
811     raw.mAttribs.push_back({"aTexCoord", Property::VECTOR2, static_cast<uint32_t>(uvCount), std::move(buffer)});
812   }
813
814   if(mTangents.IsDefined())
815   {
816     uint32_t propertySize = static_cast<uint32_t>((mTangentType == Property::VECTOR4) ? sizeof(Vector4) : sizeof(Vector3));
817     DALI_ASSERT_ALWAYS(((mTangents.mBlob.mLength % propertySize == 0) ||
818                         mTangents.mBlob.mStride >= propertySize) &&
819                        "Tangents buffer length not a multiple of element size");
820     const auto           bufferSize = mTangents.mBlob.GetBufferSize();
821     std::vector<uint8_t> buffer(bufferSize);
822
823     std::string path;
824     auto&       stream = GetAvailableData(fileStream, meshPath, buffers[mTangents.mBufferIdx], path);
825     if(!ReadAccessor(mTangents, stream, buffer.data()))
826     {
827       ExceptionFlinger(ASSERT_LOCATION) << "Failed to read tangents from '" << path << "'.";
828     }
829     mTangents.mBlob.ApplyMinMax(bufferSize / propertySize, reinterpret_cast<float*>(buffer.data()));
830
831     raw.mAttribs.push_back({"aTangent", mTangentType, static_cast<uint32_t>(bufferSize / propertySize), std::move(buffer)});
832   }
833   else if(mTangents.mBlob.mLength != 0 && hasNormals && isTriangles)
834   {
835     DALI_ASSERT_DEBUG(mTangents.mBlob.mLength == mNormals.mBlob.GetBufferSize());
836     hasUvs ? GenerateTangentsWithUvs(raw) : GenerateTangents(raw);
837   }
838
839   if(mColors.IsDefined())
840   {
841     uint32_t       propertySize = mColors.mBlob.mElementSizeHint;
842     Property::Type propertyType = (propertySize == sizeof(Vector4)) ? Property::VECTOR4 : ((propertySize == sizeof(Vector3)) ? Property::VECTOR3 : Property::NONE);
843     if(propertyType != Property::NONE)
844     {
845       DALI_ASSERT_ALWAYS(((mColors.mBlob.mLength % propertySize == 0) ||
846                           mColors.mBlob.mStride >= propertySize) &&
847                          "Colors buffer length not a multiple of element size");
848       const auto           bufferSize = mColors.mBlob.GetBufferSize();
849       std::vector<uint8_t> buffer(bufferSize);
850
851       std::string path;
852       auto&       stream = GetAvailableData(fileStream, meshPath, buffers[mColors.mBufferIdx], path);
853       if(!ReadAccessor(mColors, stream, buffer.data()))
854       {
855         ExceptionFlinger(ASSERT_LOCATION) << "Failed to read colors from '" << path << "'.";
856       }
857       mColors.mBlob.ApplyMinMax(bufferSize / propertySize, reinterpret_cast<float*>(buffer.data()));
858
859       raw.mAttribs.push_back({"aVertexColor", propertyType, static_cast<uint32_t>(bufferSize / propertySize), std::move(buffer)});
860     }
861   }
862
863   if(IsSkinned())
864   {
865     std::string pathJoint;
866     auto&       streamJoint = GetAvailableData(fileStream, meshPath, buffers[mJoints0.mBufferIdx], pathJoint);
867     if(MaskMatch(mFlags, U16_JOINT_IDS))
868     {
869       ReadJointAccessor<uint16_t>(raw, mJoints0, streamJoint, pathJoint);
870     }
871     else if(MaskMatch(mFlags, U8_JOINT_IDS))
872     {
873       ReadJointAccessor<uint8_t>(raw, mJoints0, streamJoint, pathJoint);
874     }
875     else
876     {
877       ReadJointAccessor<float>(raw, mJoints0, streamJoint, pathJoint);
878     }
879
880     DALI_ASSERT_ALWAYS(((mWeights0.mBlob.mLength % sizeof(Vector4) == 0) ||
881                         mWeights0.mBlob.mStride >= sizeof(Vector4)) &&
882                        "Weights buffer length not a multiple of element size");
883     const auto           bufferSize = mWeights0.mBlob.GetBufferSize();
884     std::vector<uint8_t> buffer(bufferSize);
885
886     std::string pathWeight;
887     auto&       streamWeight = GetAvailableData(fileStream, meshPath, buffers[mWeights0.mBufferIdx], pathWeight);
888     if(!ReadAccessor(mWeights0, streamWeight, buffer.data()))
889     {
890       ExceptionFlinger(ASSERT_LOCATION) << "Failed to read weights from '" << pathWeight << "'.";
891     }
892
893     raw.mAttribs.push_back({"aWeights", Property::VECTOR4, static_cast<uint32_t>(bufferSize / sizeof(Vector4)), std::move(buffer)});
894   }
895
896   // Calculate the Blob for the blend shapes.
897   Blob blendShapesBlob;
898   blendShapesBlob.mOffset = std::numeric_limits<unsigned int>::max();
899   blendShapesBlob.mLength = 0u;
900
901   for(const auto& blendShape : mBlendShapes)
902   {
903     for(auto i : {&blendShape.deltas, &blendShape.normals, &blendShape.tangents})
904     {
905       if(i->IsDefined())
906       {
907         blendShapesBlob.mOffset = std::min(blendShapesBlob.mOffset, i->mBlob.mOffset);
908         blendShapesBlob.mLength += i->mBlob.mLength;
909       }
910     }
911   }
912
913   if(HasBlendShapes())
914   {
915     const uint32_t numberOfVertices = static_cast<uint32_t>(mPositions.mBlob.mLength / sizeof(Vector3));
916
917     // Calculate the size of one buffer inside the texture.
918     raw.mBlendShapeBufferOffset = numberOfVertices;
919
920     bool     calculateGltf2BlendShapes = false;
921     uint32_t textureWidth              = 0u;
922     uint32_t textureHeight             = 0u;
923
924     if(!mBlendShapeHeader.IsDefined())
925     {
926       CalculateTextureSize(static_cast<uint32_t>(blendShapesBlob.mLength / sizeof(Vector3)), textureWidth, textureHeight);
927       calculateGltf2BlendShapes = true;
928     }
929     else
930     {
931       uint16_t header[2u];
932       ReadBlob(mBlendShapeHeader, fileStream, reinterpret_cast<uint8_t*>(header));
933       textureWidth  = header[0u];
934       textureHeight = header[1u];
935     }
936
937     const uint32_t numberOfBlendShapes = mBlendShapes.size();
938     raw.mBlendShapeUnnormalizeFactor.Resize(numberOfBlendShapes);
939
940     Devel::PixelBuffer geometryPixelBuffer = Devel::PixelBuffer::New(textureWidth, textureHeight, Pixel::RGB32F);
941     uint8_t*           geometryBuffer      = geometryPixelBuffer.GetBuffer();
942
943     if(calculateGltf2BlendShapes)
944     {
945       CalculateGltf2BlendShapes(geometryBuffer, mBlendShapes, numberOfVertices, raw.mBlendShapeUnnormalizeFactor[0u], buffers);
946     }
947     else
948     {
949       Blob unnormalizeFactorBlob;
950       unnormalizeFactorBlob.mLength = static_cast<uint32_t>(sizeof(float) * ((BlendShapes::Version::VERSION_2_0 == mBlendShapeVersion) ? 1u : numberOfBlendShapes));
951
952       if(blendShapesBlob.IsDefined())
953       {
954         if(ReadBlob(blendShapesBlob, fileStream, geometryBuffer))
955         {
956           unnormalizeFactorBlob.mOffset = blendShapesBlob.mOffset + blendShapesBlob.mLength;
957         }
958       }
959
960       // Read the unnormalize factors.
961       if(unnormalizeFactorBlob.IsDefined())
962       {
963         ReadBlob(unnormalizeFactorBlob, fileStream, reinterpret_cast<uint8_t*>(&raw.mBlendShapeUnnormalizeFactor[0u]));
964       }
965     }
966     raw.mBlendShapeData = Devel::PixelBuffer::Convert(geometryPixelBuffer);
967   }
968
969   return raw;
970 }
971
972 MeshGeometry MeshDefinition::Load(RawData&& raw) const
973 {
974   MeshGeometry meshGeometry;
975   meshGeometry.geometry = Geometry::New();
976   meshGeometry.geometry.SetType(mPrimitiveType);
977
978   if(IsQuad()) // TODO: do this in raw data; provide MakeTexturedQuadGeometry() that only creates buffers.
979   {
980     auto options          = MaskMatch(mFlags, FLIP_UVS_VERTICAL) ? TexturedQuadOptions::FLIP_VERTICAL : 0;
981     meshGeometry.geometry = MakeTexturedQuadGeometry(options);
982   }
983   else
984   {
985     if(!raw.mIndices.empty())
986     {
987       meshGeometry.geometry.SetIndexBuffer(raw.mIndices.data(), raw.mIndices.size());
988     }
989
990     for(auto& a : raw.mAttribs)
991     {
992       a.AttachBuffer(meshGeometry.geometry);
993     }
994
995     if(HasBlendShapes())
996     {
997       meshGeometry.blendShapeBufferOffset      = raw.mBlendShapeBufferOffset;
998       meshGeometry.blendShapeUnnormalizeFactor = std::move(raw.mBlendShapeUnnormalizeFactor);
999
1000       meshGeometry.blendShapeGeometry = Texture::New(TextureType::TEXTURE_2D,
1001                                                      raw.mBlendShapeData.GetPixelFormat(),
1002                                                      raw.mBlendShapeData.GetWidth(),
1003                                                      raw.mBlendShapeData.GetHeight());
1004       meshGeometry.blendShapeGeometry.Upload(raw.mBlendShapeData);
1005     }
1006   }
1007
1008   return meshGeometry;
1009 }
1010
1011 } // namespace Loader
1012 } // namespace Scene3D
1013 } // namespace Dali