10adc2d98d045a6fd5c0f41486bae9a404788ea3
[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), 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)
545                                      { value = std::min(max[i], value); })
546                                 : (max.empty() ? [](const float* min, const float* max, uint32_t i, float& value)
547                                      { value = std::max(min[i], value); }
548                                                : static_cast<ClampFn>([](const float* min, const float* max, uint32_t i, float& value)
549                                                                       { value = std::min(std::max(min[i], value), max[i]); }));
550
551   if(!clampFn)
552   {
553     return;
554   }
555
556   auto end = values + count * numComponents;
557   while(values != end)
558   {
559     auto     nextElement = values + numComponents;
560     uint32_t i           = 0;
561     while(values != nextElement)
562     {
563       clampFn(min.data(), max.data(), i, *values);
564       ++values;
565       ++i;
566     }
567   }
568 }
569
570 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)
571 : mOffset(offset),
572   mLength(length),
573   mStride(stride),
574   mElementSizeHint(elementSizeHint),
575   mMin(min),
576   mMax(max)
577 {
578 }
579
580 uint32_t MeshDefinition::Blob::GetBufferSize() const
581 {
582   return mLength;
583 }
584
585 void MeshDefinition::Blob::ComputeMinMax(uint32_t numComponents, uint32_t count, float* values)
586 {
587   ComputeMinMax(mMin, mMax, numComponents, count, values);
588 }
589
590 void MeshDefinition::Blob::ApplyMinMax(uint32_t count, float* values) const
591 {
592   ApplyMinMax(mMin, mMax, count, values);
593 }
594
595 void MeshDefinition::RawData::Attrib::AttachBuffer(Geometry& g) const
596 {
597   Property::Map attribMap;
598   attribMap[mName]          = mType;
599   VertexBuffer attribBuffer = VertexBuffer::New(attribMap);
600   attribBuffer.SetData(mData.data(), mNumElements);
601
602   g.AddVertexBuffer(attribBuffer);
603 }
604
605 bool MeshDefinition::IsQuad() const
606 {
607   return CaseInsensitiveStringCompare(QUAD, mUri);
608 }
609
610 bool MeshDefinition::IsSkinned() const
611 {
612   return mJoints0.IsDefined() && mWeights0.IsDefined();
613 }
614
615 bool MeshDefinition::HasBlendShapes() const
616 {
617   return !mBlendShapes.empty();
618 }
619
620 void MeshDefinition::RequestNormals()
621 {
622   mNormals.mBlob.mLength = mPositions.mBlob.GetBufferSize();
623 }
624
625 void MeshDefinition::RequestTangents()
626 {
627   mTangents.mBlob.mLength = mNormals.mBlob.GetBufferSize();
628 }
629
630 MeshDefinition::RawData
631 MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& buffers)
632 {
633   RawData raw;
634   if(IsQuad())
635   {
636     return raw;
637   }
638
639   std::string meshPath;
640   meshPath = modelsPath + mUri;
641   std::fstream fileStream;
642   if(!mUri.empty())
643   {
644     fileStream.open(meshPath, std::ios::in | std::ios::binary);
645     if(!fileStream.is_open())
646     {
647       DALI_LOG_ERROR("Fail to open buffer from %s.\n", meshPath.c_str());
648     }
649   }
650
651   if(mIndices.IsDefined())
652   {
653     if(MaskMatch(mFlags, U32_INDICES))
654     {
655       DALI_ASSERT_ALWAYS(((mIndices.mBlob.mLength % sizeof(uint32_t) == 0) ||
656                           mIndices.mBlob.mStride >= sizeof(uint32_t)) &&
657                          "Index buffer length not a multiple of element size");
658       const auto indexCount = mIndices.mBlob.GetBufferSize() / sizeof(uint32_t);
659       raw.mIndices.resize(indexCount * 2); // NOTE: we need space for uint32_ts initially.
660
661       std::string path;
662       auto&       stream = GetAvailableData(fileStream, meshPath, buffers[mIndices.mBufferIdx], path);
663       if(!ReadAccessor(mIndices, stream, reinterpret_cast<uint8_t*>(raw.mIndices.data())))
664       {
665         ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indices from '" << path << "'.";
666       }
667
668       auto u16s = raw.mIndices.data();
669       auto u32s = reinterpret_cast<uint32_t*>(raw.mIndices.data());
670       auto end  = u32s + indexCount;
671       while(u32s != end)
672       {
673         *u16s = static_cast<uint16_t>(*u32s);
674         ++u16s;
675         ++u32s;
676       }
677
678       raw.mIndices.resize(indexCount);
679     }
680     else if(MaskMatch(mFlags, U8_INDICES))
681     {
682       DALI_ASSERT_ALWAYS(((mIndices.mBlob.mLength % sizeof(uint8_t) == 0) ||
683                           mIndices.mBlob.mStride >= sizeof(uint8_t)) &&
684                          "Index buffer length not a multiple of element size");
685       const auto indexCount = mIndices.mBlob.GetBufferSize() / sizeof(uint8_t);
686       raw.mIndices.resize(indexCount); // NOTE: we need space for uint32_ts initially.
687
688       std::string path;
689       auto u8s = reinterpret_cast<uint8_t*>(raw.mIndices.data()) + indexCount;
690       auto&       stream = GetAvailableData(fileStream, meshPath, buffers[mIndices.mBufferIdx], path);
691       if(!ReadAccessor(mIndices, stream, u8s))
692       {
693         ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indices from '" << path << "'.";
694       }
695
696       auto u16s = raw.mIndices.data();
697       auto end  = u8s + indexCount;
698       while(u8s != end)
699       {
700         *u16s = static_cast<uint16_t>(*u8s);
701         ++u16s;
702         ++u8s;
703       }
704     }
705     else
706     {
707       DALI_ASSERT_ALWAYS(((mIndices.mBlob.mLength % sizeof(unsigned short) == 0) ||
708                           mIndices.mBlob.mStride >= sizeof(unsigned short)) &&
709                          "Index buffer length not a multiple of element size");
710       raw.mIndices.resize(mIndices.mBlob.mLength / sizeof(unsigned short));
711
712
713       std::string path;
714       auto&       stream = GetAvailableData(fileStream, meshPath, buffers[mIndices.mBufferIdx], path);
715       if(!ReadAccessor(mIndices, stream, reinterpret_cast<uint8_t*>(raw.mIndices.data())))
716       {
717         ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indices from '" << path << "'.";
718       }
719     }
720   }
721
722   std::vector<Vector3> positions;
723   if(mPositions.IsDefined())
724   {
725     DALI_ASSERT_ALWAYS(((mPositions.mBlob.mLength % sizeof(Vector3) == 0) ||
726                         mPositions.mBlob.mStride >= sizeof(Vector3)) &&
727                        "Position buffer length not a multiple of element size");
728     const auto           bufferSize = mPositions.mBlob.GetBufferSize();
729     std::vector<uint8_t> buffer(bufferSize);
730
731     std::string path;
732     auto&       stream = GetAvailableData(fileStream, meshPath, buffers[mPositions.mBufferIdx], path);
733     if(!ReadAccessor(mPositions, stream, buffer.data()))
734     {
735       ExceptionFlinger(ASSERT_LOCATION) << "Failed to read positions from '" << path << "'.";
736     }
737
738     uint32_t numVector3 = static_cast<uint32_t>(bufferSize / sizeof(Vector3));
739     if(mPositions.mBlob.mMin.size() != 3u || mPositions.mBlob.mMax.size() != 3u)
740     {
741       mPositions.mBlob.ComputeMinMax(3u, numVector3, reinterpret_cast<float*>(buffer.data()));
742     }
743     else
744     {
745       mPositions.mBlob.ApplyMinMax(numVector3, reinterpret_cast<float*>(buffer.data()));
746     }
747
748     if(HasBlendShapes())
749     {
750       positions.resize(numVector3);
751       std::copy(buffer.data(), buffer.data() + buffer.size(), reinterpret_cast<uint8_t*>(positions.data()));
752     }
753
754     raw.mAttribs.push_back({"aPosition", Property::VECTOR3, numVector3, std::move(buffer)});
755   }
756
757   const auto isTriangles = mPrimitiveType == Geometry::TRIANGLES;
758   auto       hasNormals  = mNormals.IsDefined();
759   if(hasNormals)
760   {
761     DALI_ASSERT_ALWAYS(((mNormals.mBlob.mLength % sizeof(Vector3) == 0) ||
762                         mNormals.mBlob.mStride >= sizeof(Vector3)) &&
763                        "Normal buffer length not a multiple of element size");
764     const auto           bufferSize = mNormals.mBlob.GetBufferSize();
765     std::vector<uint8_t> buffer(bufferSize);
766
767     std::string path;
768     auto&       stream = GetAvailableData(fileStream, meshPath, buffers[mNormals.mBufferIdx], path);
769     if(!ReadAccessor(mNormals, stream, buffer.data()))
770     {
771       ExceptionFlinger(ASSERT_LOCATION) << "Failed to read normals from '" << path << "'.";
772     }
773
774     mNormals.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()));
775
776     raw.mAttribs.push_back({"aNormal", Property::VECTOR3, static_cast<uint32_t>(bufferSize / sizeof(Vector3)), std::move(buffer)});
777   }
778   else if(mNormals.mBlob.mLength != 0 && isTriangles)
779   {
780     DALI_ASSERT_DEBUG(mNormals.mBlob.mLength == mPositions.mBlob.GetBufferSize());
781     GenerateNormals(raw);
782     hasNormals = true;
783   }
784
785   const auto hasUvs = mTexCoords.IsDefined();
786   if(hasUvs)
787   {
788     DALI_ASSERT_ALWAYS(((mTexCoords.mBlob.mLength % sizeof(Vector2) == 0) ||
789                         mTexCoords.mBlob.mStride >= sizeof(Vector2)) &&
790                        "Normal buffer length not a multiple of element size");
791     const auto           bufferSize = mTexCoords.mBlob.GetBufferSize();
792     std::vector<uint8_t> buffer(bufferSize);
793
794     std::string path;
795     auto&       stream = GetAvailableData(fileStream, meshPath, buffers[mTexCoords.mBufferIdx], path);
796     if(!ReadAccessor(mTexCoords, stream, buffer.data()))
797     {
798       ExceptionFlinger(ASSERT_LOCATION) << "Failed to read uv-s from '" << path << "'.";
799     }
800
801     const auto uvCount = bufferSize / sizeof(Vector2);
802     if(MaskMatch(mFlags, FLIP_UVS_VERTICAL))
803     {
804       auto uv    = reinterpret_cast<Vector2*>(buffer.data());
805       auto uvEnd = uv + uvCount;
806       while(uv != uvEnd)
807       {
808         uv->y = 1.0f - uv->y;
809         ++uv;
810       }
811     }
812
813     mTexCoords.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector2)), reinterpret_cast<float*>(buffer.data()));
814
815     raw.mAttribs.push_back({"aTexCoord", Property::VECTOR2, static_cast<uint32_t>(uvCount), std::move(buffer)});
816   }
817
818   if(mTangents.IsDefined())
819   {
820     uint32_t propertySize = static_cast<uint32_t>((mTangentType == Property::VECTOR4) ? sizeof(Vector4) : sizeof(Vector3));
821     DALI_ASSERT_ALWAYS(((mTangents.mBlob.mLength % propertySize == 0) ||
822                         mTangents.mBlob.mStride >= propertySize) &&
823                        "Tangents buffer length not a multiple of element size");
824     const auto           bufferSize = mTangents.mBlob.GetBufferSize();
825     std::vector<uint8_t> buffer(bufferSize);
826
827     std::string path;
828     auto&       stream = GetAvailableData(fileStream, meshPath, buffers[mTangents.mBufferIdx], path);
829     if(!ReadAccessor(mTangents, stream, buffer.data()))
830     {
831       ExceptionFlinger(ASSERT_LOCATION) << "Failed to read tangents from '" << path << "'.";
832     }
833     mTangents.mBlob.ApplyMinMax(bufferSize / propertySize, reinterpret_cast<float*>(buffer.data()));
834
835     raw.mAttribs.push_back({"aTangent", mTangentType, static_cast<uint32_t>(bufferSize / propertySize), std::move(buffer)});
836   }
837   else if(mTangents.mBlob.mLength != 0 && hasNormals && isTriangles)
838   {
839     DALI_ASSERT_DEBUG(mTangents.mBlob.mLength == mNormals.mBlob.GetBufferSize());
840     hasUvs ? GenerateTangentsWithUvs(raw) : GenerateTangents(raw);
841   }
842
843   if(mColors.IsDefined())
844   {
845     uint32_t       propertySize = mColors.mBlob.mElementSizeHint;
846     Property::Type propertyType = (propertySize == sizeof(Vector4)) ? Property::VECTOR4 : ((propertySize == sizeof(Vector3)) ? Property::VECTOR3 : Property::NONE);
847     if(propertyType != Property::NONE)
848     {
849       DALI_ASSERT_ALWAYS(((mColors.mBlob.mLength % propertySize == 0) ||
850                           mColors.mBlob.mStride >= propertySize) &&
851                          "Colors buffer length not a multiple of element size");
852       const auto           bufferSize = mColors.mBlob.GetBufferSize();
853       std::vector<uint8_t> buffer(bufferSize);
854
855       std::string path;
856       auto&       stream = GetAvailableData(fileStream, meshPath, buffers[mColors.mBufferIdx], path);
857       if(!ReadAccessor(mColors, stream, buffer.data()))
858       {
859         ExceptionFlinger(ASSERT_LOCATION) << "Failed to read colors from '" << path << "'.";
860       }
861       mColors.mBlob.ApplyMinMax(bufferSize / propertySize, reinterpret_cast<float*>(buffer.data()));
862
863       raw.mAttribs.push_back({"aVertexColor", propertyType, static_cast<uint32_t>(bufferSize / propertySize), std::move(buffer)});
864     }
865   }
866
867   if(IsSkinned())
868   {
869     std::string pathJoint;
870     auto&       streamJoint = GetAvailableData(fileStream, meshPath, buffers[mJoints0.mBufferIdx], pathJoint);
871     if(MaskMatch(mFlags, U16_JOINT_IDS))
872     {
873       ReadJointAccessor<uint16_t>(raw, mJoints0, streamJoint, pathJoint);
874     }
875     else if(MaskMatch(mFlags, U8_JOINT_IDS))
876     {
877       ReadJointAccessor<uint8_t>(raw, mJoints0, streamJoint, pathJoint);
878     }
879     else
880     {
881       ReadJointAccessor<float>(raw, mJoints0, streamJoint, pathJoint);
882     }
883
884     DALI_ASSERT_ALWAYS(((mWeights0.mBlob.mLength % sizeof(Vector4) == 0) ||
885                         mWeights0.mBlob.mStride >= sizeof(Vector4)) &&
886                        "Weights buffer length not a multiple of element size");
887     const auto           bufferSize = mWeights0.mBlob.GetBufferSize();
888     std::vector<uint8_t> buffer(bufferSize);
889
890     std::string pathWeight;
891     auto&       streamWeight = GetAvailableData(fileStream, meshPath, buffers[mWeights0.mBufferIdx], pathWeight);
892     if(!ReadAccessor(mWeights0, streamWeight, buffer.data()))
893     {
894       ExceptionFlinger(ASSERT_LOCATION) << "Failed to read weights from '" << pathWeight << "'.";
895     }
896
897     raw.mAttribs.push_back({"aWeights", Property::VECTOR4, static_cast<uint32_t>(bufferSize / sizeof(Vector4)), std::move(buffer)});
898   }
899
900   // Calculate the Blob for the blend shapes.
901   Blob blendShapesBlob;
902   blendShapesBlob.mOffset = std::numeric_limits<unsigned int>::max();
903   blendShapesBlob.mLength = 0u;
904
905   for(const auto& blendShape : mBlendShapes)
906   {
907     for(auto i : {&blendShape.deltas, &blendShape.normals, &blendShape.tangents})
908     {
909       if(i->IsDefined())
910       {
911         blendShapesBlob.mOffset = std::min(blendShapesBlob.mOffset, i->mBlob.mOffset);
912         blendShapesBlob.mLength += i->mBlob.mLength;
913       }
914     }
915   }
916
917   if(HasBlendShapes())
918   {
919     const uint32_t numberOfVertices = static_cast<uint32_t>(mPositions.mBlob.mLength / sizeof(Vector3));
920
921     // Calculate the size of one buffer inside the texture.
922     raw.mBlendShapeBufferOffset = numberOfVertices;
923
924     bool     calculateGltf2BlendShapes = false;
925     uint32_t textureWidth              = 0u;
926     uint32_t textureHeight             = 0u;
927
928     if(!mBlendShapeHeader.IsDefined())
929     {
930       CalculateTextureSize(static_cast<uint32_t>(blendShapesBlob.mLength / sizeof(Vector3)), textureWidth, textureHeight);
931       calculateGltf2BlendShapes = true;
932     }
933     else
934     {
935       uint16_t header[2u];
936       ReadBlob(mBlendShapeHeader, fileStream, reinterpret_cast<uint8_t*>(header));
937       textureWidth  = header[0u];
938       textureHeight = header[1u];
939     }
940
941     const uint32_t numberOfBlendShapes = mBlendShapes.size();
942     raw.mBlendShapeUnnormalizeFactor.Resize(numberOfBlendShapes);
943
944     Devel::PixelBuffer geometryPixelBuffer = Devel::PixelBuffer::New(textureWidth, textureHeight, Pixel::RGB32F);
945     uint8_t*           geometryBuffer      = geometryPixelBuffer.GetBuffer();
946
947     if(calculateGltf2BlendShapes)
948     {
949       CalculateGltf2BlendShapes(geometryBuffer, mBlendShapes, numberOfVertices, raw.mBlendShapeUnnormalizeFactor[0u], buffers);
950     }
951     else
952     {
953       Blob unnormalizeFactorBlob;
954       unnormalizeFactorBlob.mLength = static_cast<uint32_t>(sizeof(float) * ((BlendShapes::Version::VERSION_2_0 == mBlendShapeVersion) ? 1u : numberOfBlendShapes));
955
956       if(blendShapesBlob.IsDefined())
957       {
958         if(ReadBlob(blendShapesBlob, fileStream, geometryBuffer))
959         {
960           unnormalizeFactorBlob.mOffset = blendShapesBlob.mOffset + blendShapesBlob.mLength;
961         }
962       }
963
964       // Read the unnormalize factors.
965       if(unnormalizeFactorBlob.IsDefined())
966       {
967         ReadBlob(unnormalizeFactorBlob, fileStream, reinterpret_cast<uint8_t*>(&raw.mBlendShapeUnnormalizeFactor[0u]));
968       }
969     }
970     raw.mBlendShapeData = Devel::PixelBuffer::Convert(geometryPixelBuffer);
971   }
972
973   return raw;
974 }
975
976 MeshGeometry MeshDefinition::Load(RawData&& raw) const
977 {
978   MeshGeometry meshGeometry;
979   meshGeometry.geometry = Geometry::New();
980   meshGeometry.geometry.SetType(mPrimitiveType);
981
982   if(IsQuad()) // TODO: do this in raw data; provide MakeTexturedQuadGeometry() that only creates buffers.
983   {
984     auto options          = MaskMatch(mFlags, FLIP_UVS_VERTICAL) ? TexturedQuadOptions::FLIP_VERTICAL : 0;
985     meshGeometry.geometry = MakeTexturedQuadGeometry(options);
986   }
987   else
988   {
989     if(!raw.mIndices.empty())
990     {
991       meshGeometry.geometry.SetIndexBuffer(raw.mIndices.data(), raw.mIndices.size());
992     }
993
994     for(auto& a : raw.mAttribs)
995     {
996       a.AttachBuffer(meshGeometry.geometry);
997     }
998
999     if(HasBlendShapes())
1000     {
1001       meshGeometry.blendShapeBufferOffset      = raw.mBlendShapeBufferOffset;
1002       meshGeometry.blendShapeUnnormalizeFactor = std::move(raw.mBlendShapeUnnormalizeFactor);
1003
1004       meshGeometry.blendShapeGeometry = Texture::New(TextureType::TEXTURE_2D,
1005                                                      raw.mBlendShapeData.GetPixelFormat(),
1006                                                      raw.mBlendShapeData.GetWidth(),
1007                                                      raw.mBlendShapeData.GetHeight());
1008       meshGeometry.blendShapeGeometry.Upload(raw.mBlendShapeData);
1009     }
1010   }
1011
1012   return meshGeometry;
1013 }
1014
1015 } // namespace Loader
1016 } // namespace Scene3D
1017 } // namespace Dali