2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-scene3d/public-api/loader/mesh-definition.h>
22 #include <dali/devel-api/adaptor-framework/file-stream.h>
23 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/public-api/math/compile-time-math.h>
28 #include <type_traits>
30 namespace Dali::Scene3D::Loader
34 template<bool use32BitIndices>
38 using IndexType = typename std::conditional_t<use32BitIndices, uint32_t, uint16_t>;
39 IndexProvider(const uint16_t* indices)
40 : mData(reinterpret_cast<uintptr_t>(indices)),
41 mFunc(indices ? IncrementPointer : Increment)
45 IndexType operator()()
51 static IndexType Increment(uintptr_t& data)
53 // mData was 'zero' at construct time. Just simply return counter start with 0.
54 return static_cast<IndexType>(data++);
57 static IndexType IncrementPointer(uintptr_t& data)
59 auto iPtr = reinterpret_cast<const IndexType*>(data);
61 data = reinterpret_cast<uintptr_t>(++iPtr);
66 IndexType (*mFunc)(uintptr_t&);
69 const char* QUAD("quad");
71 ///@brief Reads a blob from the given stream @a source into @a target, which must have
72 /// at least @a descriptor.length bytes.
73 bool ReadBlob(const MeshDefinition::Blob& descriptor, std::istream& source, uint8_t* target)
76 if(!source.seekg(descriptor.mOffset, std::istream::beg))
81 if(descriptor.IsConsecutive())
83 return !!source.read(reinterpret_cast<char*>(target), static_cast<std::streamsize>(static_cast<size_t>(descriptor.mLength)));
87 if(descriptor.mStride > descriptor.mElementSizeHint)
89 const uint32_t diff = descriptor.mStride - descriptor.mElementSizeHint;
90 uint32_t readSize = 0;
91 uint32_t totalSize = (descriptor.mLength / descriptor.mElementSizeHint) * descriptor.mStride;
92 while(readSize < totalSize &&
93 source.read(reinterpret_cast<char*>(target), descriptor.mElementSizeHint))
95 readSize += descriptor.mStride;
96 target += descriptor.mElementSizeHint;
97 source.seekg(diff, std::istream::cur);
99 return readSize == totalSize;
106 void ReadValues(const std::vector<uint8_t>& valuesBuffer, const std::vector<uint8_t>& indicesBuffer, uint8_t* target, uint32_t count, uint32_t elementSizeHint)
108 const T* const indicesPtr = reinterpret_cast<const T* const>(indicesBuffer.data());
109 for(uint32_t index = 0u; index < count; ++index)
111 uint32_t valuesIndex = indicesPtr[index] * elementSizeHint;
112 memcpy(target + valuesIndex, &valuesBuffer[index * elementSizeHint], elementSizeHint);
116 bool ReadAccessor(const MeshDefinition::Accessor& accessor, std::istream& source, uint8_t* target, std::vector<uint32_t>* sparseIndices)
118 bool success = false;
120 if(accessor.mBlob.IsDefined())
122 success = ReadBlob(accessor.mBlob, source, target);
131 const MeshDefinition::Blob& indices = accessor.mSparse->mIndices;
132 const MeshDefinition::Blob& values = accessor.mSparse->mValues;
134 if(!indices.IsDefined() || !values.IsDefined())
139 const auto indicesBufferSize = indices.GetBufferSize();
140 std::vector<uint8_t> indicesBuffer(indicesBufferSize);
141 success = ReadBlob(indices, source, indicesBuffer.data());
147 const auto valuesBufferSize = values.GetBufferSize();
148 std::vector<uint8_t> valuesBuffer(valuesBufferSize);
149 success = ReadBlob(values, source, valuesBuffer.data());
155 // If non-null sparse indices vector, prepare it for output
158 sparseIndices->resize(accessor.mSparse->mCount);
161 switch(indices.mElementSizeHint)
165 ReadValues<uint8_t>(valuesBuffer, indicesBuffer, target, accessor.mSparse->mCount, values.mElementSizeHint);
168 // convert 8-bit indices into 32-bit
169 std::transform(indicesBuffer.begin(), indicesBuffer.end(), sparseIndices->begin(), [](const uint8_t& value) { return uint32_t(value); });
175 ReadValues<uint16_t>(valuesBuffer, indicesBuffer, target, accessor.mSparse->mCount, values.mElementSizeHint);
178 // convert 16-bit indices into 32-bit
179 std::transform(reinterpret_cast<uint16_t*>(indicesBuffer.data()),
180 reinterpret_cast<uint16_t*>(indicesBuffer.data()) + accessor.mSparse->mCount,
181 sparseIndices->begin(),
182 [](const uint16_t& value) {
183 return uint32_t(value);
190 ReadValues<uint32_t>(valuesBuffer, indicesBuffer, target, accessor.mSparse->mCount, values.mElementSizeHint);
193 std::copy(indicesBuffer.begin(), indicesBuffer.end(), reinterpret_cast<uint8_t*>(sparseIndices->data()));
199 DALI_ASSERT_DEBUG(!"Unsupported type for an index");
207 bool ReadAccessor(const MeshDefinition::Accessor& accessor, std::istream& source, uint8_t* target)
209 return ReadAccessor(accessor, source, target, nullptr);
213 void ReadJointAccessor(MeshDefinition::RawData& raw, const MeshDefinition::Accessor& accessor, std::istream& source, const std::string& meshPath)
215 constexpr auto sizeofBlobUnit = sizeof(T) * 4;
217 DALI_ASSERT_ALWAYS(((accessor.mBlob.mLength % sizeofBlobUnit == 0) ||
218 accessor.mBlob.mStride >= sizeofBlobUnit) &&
219 "Joints buffer length not a multiple of element size");
220 const auto inBufferSize = accessor.mBlob.GetBufferSize();
221 const auto outBufferSize = (sizeof(Vector4) / sizeofBlobUnit) * inBufferSize;
223 std::vector<uint8_t> buffer(outBufferSize);
224 auto inBuffer = buffer.data() + outBufferSize - inBufferSize;
225 if(!ReadAccessor(accessor, source, inBuffer))
227 ExceptionFlinger(ASSERT_LOCATION) << "Failed to read joints from '" << meshPath << "'.";
230 if constexpr(sizeofBlobUnit != sizeof(Vector4))
232 auto floats = reinterpret_cast<float*>(buffer.data());
233 const auto end = inBuffer + inBufferSize;
234 while(inBuffer != end)
236 const auto value = *reinterpret_cast<T*>(inBuffer);
237 *floats = static_cast<float>(value);
239 inBuffer += sizeof(T);
243 raw.mAttribs.push_back({"aJoints", Property::VECTOR4, static_cast<uint32_t>(outBufferSize / sizeof(Vector4)), std::move(buffer)});
247 void ReadWeightAccessor(MeshDefinition::RawData& raw, const MeshDefinition::Accessor& accessor, std::istream& source, const std::string& meshPath)
249 constexpr auto sizeofBlobUnit = sizeof(T) * 4;
251 DALI_ASSERT_ALWAYS(((accessor.mBlob.mLength % sizeofBlobUnit == 0) ||
252 accessor.mBlob.mStride >= sizeofBlobUnit) &&
253 "weights buffer length not a multiple of element size");
254 const auto inBufferSize = accessor.mBlob.GetBufferSize();
255 const auto outBufferSize = (sizeof(Vector4) / sizeofBlobUnit) * inBufferSize;
257 std::vector<uint8_t> buffer(outBufferSize);
258 auto inBuffer = buffer.data() + outBufferSize - inBufferSize;
259 if(!ReadAccessor(accessor, source, inBuffer))
261 ExceptionFlinger(ASSERT_LOCATION) << "Failed to read weights from '" << meshPath << "'.";
264 if constexpr(sizeofBlobUnit != sizeof(Vector4))
266 auto floats = reinterpret_cast<float*>(buffer.data());
267 const auto end = inBuffer + inBufferSize;
268 while(inBuffer != end)
270 const auto value = *reinterpret_cast<T*>(inBuffer);
271 // Normalize weight value. value /= 255 for uint8_t weight, and value /= 65535 for uint16_t weight.
272 *floats = static_cast<float>(value) / static_cast<float>((1 << (sizeof(T) * 8)) - 1);
274 inBuffer += sizeof(T);
278 raw.mAttribs.push_back({"aWeights", Property::VECTOR4, static_cast<uint32_t>(outBufferSize / sizeof(Vector4)), std::move(buffer)});
281 template<bool use32BitsIndices, typename IndexProviderType = IndexProvider<use32BitsIndices>>
282 bool GenerateNormals(MeshDefinition::RawData& raw)
284 using IndexType = typename IndexProviderType::IndexType;
286 // mIndicies size must be even if we use 32bit indices.
287 if(DALI_UNLIKELY(use32BitsIndices && !raw.mIndices.empty() && !(raw.mIndices.size() % (sizeof(IndexType) / sizeof(uint16_t)) == 0)))
292 auto& attribs = raw.mAttribs;
293 DALI_ASSERT_DEBUG(attribs.size() > 0); // positions
295 IndexProviderType getIndex(raw.mIndices.data());
297 const uint32_t numIndices = raw.mIndices.empty() ? attribs[0].mNumElements : static_cast<uint32_t>(raw.mIndices.size() / (sizeof(IndexType) / sizeof(uint16_t)));
299 auto* positions = reinterpret_cast<const Vector3*>(attribs[0].mData.data());
301 std::vector<uint8_t> buffer(attribs[0].mNumElements * sizeof(Vector3));
302 auto normals = reinterpret_cast<Vector3*>(buffer.data());
304 for(uint32_t i = 0; i < numIndices; i += 3)
306 IndexType indices[]{getIndex(), getIndex(), getIndex()};
307 Vector3 pos[]{positions[indices[0]], positions[indices[1]], positions[indices[2]]};
309 Vector3 a = pos[1] - pos[0];
310 Vector3 b = pos[2] - pos[0];
312 Vector3 normal(a.Cross(b));
313 normals[indices[0]] += normal;
314 normals[indices[1]] += normal;
315 normals[indices[2]] += normal;
318 auto iEnd = normals + attribs[0].mNumElements;
319 while(normals != iEnd)
321 normals->Normalize();
325 attribs.push_back({"aNormal", Property::VECTOR3, attribs[0].mNumElements, std::move(buffer)});
330 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>>
331 bool GenerateTangents(MeshDefinition::RawData& raw)
333 using IndexType = typename IndexProviderType::IndexType;
335 // mIndicies size must be even if we use 32bit indices.
336 if(DALI_UNLIKELY(use32BitsIndices && !raw.mIndices.empty() && !(raw.mIndices.size() % (sizeof(IndexType) / sizeof(uint16_t)) == 0)))
341 auto& attribs = raw.mAttribs;
342 // Required positions, normals, uvs (if we have). If not, skip generation
343 if(DALI_UNLIKELY(attribs.size() < (2 + static_cast<size_t>(hasUvs))))
348 std::vector<uint8_t> buffer(attribs[0].mNumElements * sizeof(T));
349 auto tangents = reinterpret_cast<T*>(buffer.data());
353 IndexProviderType getIndex(raw.mIndices.data());
355 const uint32_t numIndices = raw.mIndices.empty() ? attribs[0].mNumElements : static_cast<uint32_t>(raw.mIndices.size() / (sizeof(IndexType) / sizeof(uint16_t)));
357 auto* positions = reinterpret_cast<const Vector3*>(attribs[0].mData.data());
358 auto* uvs = reinterpret_cast<const Vector2*>(attribs[2].mData.data());
360 for(uint32_t i = 0; i < numIndices; i += 3)
362 IndexType indices[]{getIndex(), getIndex(), getIndex()};
363 Vector3 pos[]{positions[indices[0]], positions[indices[1]], positions[indices[2]]};
364 Vector2 uv[]{uvs[indices[0]], uvs[indices[1]], uvs[indices[2]]};
366 float x0 = pos[1].x - pos[0].x;
367 float y0 = pos[1].y - pos[0].y;
368 float z0 = pos[1].z - pos[0].z;
370 float x1 = pos[2].x - pos[0].x;
371 float y1 = pos[2].y - pos[0].y;
372 float z1 = pos[2].z - pos[0].z;
374 float s0 = uv[1].x - uv[0].x;
375 float t0 = uv[1].y - uv[0].y;
377 float s1 = uv[2].x - uv[0].x;
378 float t1 = uv[2].y - uv[0].y;
380 float det = (s0 * t1 - t0 * s1);
381 float r = 1.f / ((std::abs(det) < Dali::Epsilon<1000>::value) ? (Dali::Epsilon<1000>::value * (det > 0.0f ? 1.f : -1.f)) : det);
382 Vector3 tangent((x0 * t1 - t0 * x1) * r, (y0 * t1 - t0 * y1) * r, (z0 * t1 - t0 * z1) * r);
383 tangents[indices[0]] += T(tangent);
384 tangents[indices[1]] += T(tangent);
385 tangents[indices[2]] += T(tangent);
389 auto* normals = reinterpret_cast<const Vector3*>(attribs[1].mData.data());
390 auto iEnd = normals + attribs[1].mNumElements;
391 while(normals != iEnd)
396 // Calculated by indexs
397 tangentVec3 = Vector3((*tangents).x, (*tangents).y, (*tangents).z);
401 // Only choiced by normal vector. by indexs
402 Vector3 t[]{normals->Cross(Vector3::XAXIS), normals->Cross(Vector3::YAXIS)};
403 tangentVec3 = t[t[1].LengthSquared() > t[0].LengthSquared()];
406 tangentVec3 -= *normals * normals->Dot(tangentVec3);
407 tangentVec3.Normalize();
408 if constexpr(useVec3)
410 *tangents = tangentVec3;
414 *tangents = Vector4(tangentVec3.x, tangentVec3.y, tangentVec3.z, 1.0f);
420 attribs.push_back({"aTangent", useVec3 ? Property::VECTOR3 : Property::VECTOR4, attribs[0].mNumElements, std::move(buffer)});
425 void CalculateTextureSize(uint32_t totalTextureSize, uint32_t& textureWidth, uint32_t& textureHeight)
427 DALI_ASSERT_DEBUG(0u != totalTextureSize && "totalTextureSize is zero.")
429 // Calculate the dimensions of the texture.
430 // The total size of the texture is the length of the blend shapes blob.
435 if(0u == totalTextureSize)
441 const uint32_t pow2 = static_cast<uint32_t>(ceil(log2(totalTextureSize)));
442 const uint32_t powWidth = pow2 >> 1u;
443 const uint32_t powHeight = pow2 - powWidth;
445 textureWidth = 1u << powWidth;
446 textureHeight = 1u << powHeight;
449 void CalculateGltf2BlendShapes(uint8_t* geometryBuffer, const std::vector<MeshDefinition::BlendShape>& blendShapes, uint32_t numberOfVertices, float& blendShapeUnnormalizeFactor, BufferDefinition::Vector& buffers)
451 uint32_t geometryBufferIndex = 0u;
452 float maxDistanceSquared = 0.f;
453 Vector3* geometryBufferV3 = reinterpret_cast<Vector3*>(geometryBuffer);
454 for(const auto& blendShape : blendShapes)
456 if(blendShape.deltas.IsDefined())
458 DALI_ASSERT_ALWAYS(((blendShape.deltas.mBlob.mLength % sizeof(Vector3) == 0u) ||
459 blendShape.deltas.mBlob.mStride >= sizeof(Vector3)) &&
460 "Blend Shape position buffer length not a multiple of element size");
462 const auto bufferSize = blendShape.deltas.mBlob.GetBufferSize();
463 std::vector<uint8_t> buffer(bufferSize);
464 std::vector<uint32_t> sparseIndices{};
466 if(ReadAccessor(blendShape.deltas, buffers[blendShape.deltas.mBufferIdx].GetBufferStream(), buffer.data(), &sparseIndices))
468 blendShape.deltas.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()), &sparseIndices);
470 // Calculate the difference with the original mesh.
471 // Find the max distance to normalize the deltas.
472 const auto* const deltasBuffer = reinterpret_cast<const Vector3* const>(buffer.data());
474 auto ProcessVertex = [&geometryBufferV3, &deltasBuffer, &maxDistanceSquared](uint32_t geometryBufferIndex, uint32_t deltaIndex) {
475 Vector3& delta = geometryBufferV3[geometryBufferIndex] = deltasBuffer[deltaIndex];
476 delta = deltasBuffer[deltaIndex];
477 return std::max(maxDistanceSquared, delta.LengthSquared());
480 if(sparseIndices.empty())
482 for(uint32_t index = 0u; index < numberOfVertices; ++index)
484 maxDistanceSquared = ProcessVertex(geometryBufferIndex++, index);
489 // initialize blendshape texture
490 // TODO: there may be a case when sparse accessor uses a base buffer view for initial values.
491 std::fill(geometryBufferV3 + geometryBufferIndex, geometryBufferV3 + geometryBufferIndex + numberOfVertices, Vector3::ZERO);
492 for(auto index : sparseIndices)
494 maxDistanceSquared = ProcessVertex(geometryBufferIndex + index, index);
496 geometryBufferIndex += numberOfVertices;
501 if(blendShape.normals.IsDefined())
503 DALI_ASSERT_ALWAYS(((blendShape.normals.mBlob.mLength % sizeof(Vector3) == 0u) ||
504 blendShape.normals.mBlob.mStride >= sizeof(Vector3)) &&
505 "Blend Shape normals buffer length not a multiple of element size");
507 const auto bufferSize = blendShape.normals.mBlob.GetBufferSize();
508 std::vector<uint8_t> buffer(bufferSize);
509 std::vector<uint32_t> sparseIndices;
511 if(ReadAccessor(blendShape.normals, buffers[blendShape.normals.mBufferIdx].GetBufferStream(), buffer.data(), &sparseIndices))
513 blendShape.normals.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()), &sparseIndices);
515 // Calculate the difference with the original mesh, and translate to make all values positive.
516 const Vector3* const deltasBuffer = reinterpret_cast<const Vector3* const>(buffer.data());
517 auto ProcessVertex = [&geometryBufferV3, &deltasBuffer, &maxDistanceSquared](uint32_t geometryBufferIndex, uint32_t deltaIndex) {
518 Vector3& delta = geometryBufferV3[geometryBufferIndex] = deltasBuffer[deltaIndex];
528 if(sparseIndices.empty())
530 for(uint32_t index = 0u; index < numberOfVertices; ++index)
532 ProcessVertex(geometryBufferIndex++, index);
537 std::fill(geometryBufferV3 + geometryBufferIndex, geometryBufferV3 + geometryBufferIndex + numberOfVertices, Vector3(0.5, 0.5, 0.5));
538 for(auto index : sparseIndices)
540 ProcessVertex(geometryBufferIndex + index, index);
542 geometryBufferIndex += numberOfVertices;
547 if(blendShape.tangents.IsDefined())
549 DALI_ASSERT_ALWAYS(((blendShape.tangents.mBlob.mLength % sizeof(Vector3) == 0u) ||
550 blendShape.tangents.mBlob.mStride >= sizeof(Vector3)) &&
551 "Blend Shape tangents buffer length not a multiple of element size");
553 const auto bufferSize = blendShape.tangents.mBlob.GetBufferSize();
554 std::vector<uint8_t> buffer(bufferSize);
555 std::vector<uint32_t> sparseIndices;
557 if(ReadAccessor(blendShape.tangents, buffers[blendShape.tangents.mBufferIdx].GetBufferStream(), buffer.data(), &sparseIndices))
559 blendShape.tangents.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()), &sparseIndices);
561 // Calculate the difference with the original mesh, and translate to make all values positive.
562 const Vector3* const deltasBuffer = reinterpret_cast<const Vector3* const>(buffer.data());
563 auto ProcessVertex = [&geometryBufferV3, &deltasBuffer, &maxDistanceSquared](uint32_t geometryBufferIndex, uint32_t deltaIndex) {
564 Vector3& delta = geometryBufferV3[geometryBufferIndex] = deltasBuffer[deltaIndex];
574 if(sparseIndices.empty())
576 for(uint32_t index = 0u; index < numberOfVertices; ++index)
578 ProcessVertex(geometryBufferIndex++, index);
583 std::fill(geometryBufferV3 + geometryBufferIndex, geometryBufferV3 + geometryBufferIndex + numberOfVertices, Vector3(0.5, 0.5, 0.5));
584 for(auto index : sparseIndices)
586 ProcessVertex(geometryBufferIndex + index, index);
588 geometryBufferIndex += numberOfVertices;
594 geometryBufferIndex = 0u;
596 const float maxDistance = sqrtf(maxDistanceSquared);
598 const float normalizeFactor = (maxDistanceSquared < Math::MACHINE_EPSILON_100) ? 1.f : (0.5f / maxDistance);
600 // Calculate and store the unnormalize factor.
601 blendShapeUnnormalizeFactor = maxDistance * 2.0f;
603 for(const auto& blendShape : blendShapes)
605 // Normalize all the deltas and translate to a possitive value.
606 // Deltas are going to be passed to the shader in a color texture
607 // whose values that are less than zero are clamped.
608 if(blendShape.deltas.IsDefined())
610 for(uint32_t index = 0u; index < numberOfVertices; ++index)
612 Vector3& delta = geometryBufferV3[geometryBufferIndex++];
613 delta.x = Clamp(((delta.x * normalizeFactor) + 0.5f), 0.f, 1.f);
614 delta.y = Clamp(((delta.y * normalizeFactor) + 0.5f), 0.f, 1.f);
615 delta.z = Clamp(((delta.z * normalizeFactor) + 0.5f), 0.f, 1.f);
619 if(blendShape.normals.IsDefined())
621 geometryBufferIndex += numberOfVertices;
624 if(blendShape.tangents.IsDefined())
626 geometryBufferIndex += numberOfVertices;
631 std::iostream& GetAvailableData(std::fstream& meshStream, const std::string& meshPath, BufferDefinition& buffer, std::string& availablePath)
633 auto& stream = (meshStream.is_open()) ? meshStream : buffer.GetBufferStream();
634 availablePath = (meshStream.is_open()) ? meshPath : buffer.GetUri();
640 MeshDefinition::SparseBlob::SparseBlob(const Blob& indices, const Blob& values, uint32_t count)
647 MeshDefinition::SparseBlob::SparseBlob(Blob&& indices, Blob&& values, uint32_t count)
648 : mIndices(std::move(indices)),
649 mValues(std::move(values)),
654 MeshDefinition::Accessor::Accessor(const MeshDefinition::Blob& blob,
655 const MeshDefinition::SparseBlob& sparse,
658 mSparse{(sparse.mIndices.IsDefined() && sparse.mValues.IsDefined()) ? new SparseBlob{sparse} : nullptr},
659 mBufferIdx(bufferIndex)
663 MeshDefinition::Accessor::Accessor(MeshDefinition::Blob&& blob,
664 MeshDefinition::SparseBlob&& sparse,
666 : mBlob{std::move(blob)},
667 mSparse{(sparse.mIndices.IsDefined() && sparse.mValues.IsDefined()) ? new SparseBlob{std::move(sparse)} : nullptr},
668 mBufferIdx(bufferIndex)
672 void MeshDefinition::Blob::ComputeMinMax(std::vector<float>& min, std::vector<float>& max, uint32_t numComponents, uint32_t count, const float* values)
674 min.assign(numComponents, MAXFLOAT);
675 max.assign(numComponents, -MAXFLOAT);
676 for(uint32_t i = 0; i < count; ++i)
678 for(uint32_t j = 0; j < numComponents; ++j)
680 min[j] = std::min(min[j], *values);
681 max[j] = std::max(max[j], *values);
687 void MeshDefinition::Blob::ApplyMinMax(const std::vector<float>& min, const std::vector<float>& max, uint32_t count, float* values, std::vector<uint32_t>* sparseIndices)
689 DALI_ASSERT_DEBUG(max.size() == min.size() || max.size() * min.size() == 0);
690 const auto numComponents = std::max(min.size(), max.size());
692 using ClampFn = void (*)(const float*, const float*, uint32_t, float&);
693 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); })
694 : (max.empty() ? [](const float* min, const float* max, uint32_t i, float& value) { value = std::max(min[i], value); }
695 : static_cast<ClampFn>([](const float* min, const float* max, uint32_t i, float& value) { value = std::min(std::max(min[i], value), max[i]); }));
702 auto end = values + count * numComponents;
705 auto nextElement = values + numComponents;
707 while(values != nextElement)
709 clampFn(min.data(), max.data(), i, *values);
716 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)
720 mElementSizeHint(elementSizeHint),
726 uint32_t MeshDefinition::Blob::GetBufferSize() const
731 void MeshDefinition::Blob::ComputeMinMax(uint32_t numComponents, uint32_t count, float* values)
733 ComputeMinMax(mMin, mMax, numComponents, count, values);
736 void MeshDefinition::Blob::ApplyMinMax(uint32_t count, float* values, std::vector<uint32_t>* sparseIndices) const
738 ApplyMinMax(mMin, mMax, count, values, sparseIndices);
741 void MeshDefinition::RawData::Attrib::AttachBuffer(Geometry& g) const
743 Property::Map attribMap;
744 attribMap[mName] = mType;
745 VertexBuffer attribBuffer = VertexBuffer::New(attribMap);
746 attribBuffer.SetData(mData.data(), mNumElements);
748 g.AddVertexBuffer(attribBuffer);
751 bool MeshDefinition::IsQuad() const
753 return CaseInsensitiveStringCompare(QUAD, mUri);
756 bool MeshDefinition::IsSkinned() const
758 return mJoints0.IsDefined() && mWeights0.IsDefined();
761 bool MeshDefinition::HasBlendShapes() const
763 return !mBlendShapes.empty();
766 void MeshDefinition::RequestNormals()
768 mNormals.mBlob.mLength = mPositions.mBlob.GetBufferSize();
771 void MeshDefinition::RequestTangents()
773 mTangents.mBlob.mLength = mNormals.mBlob.GetBufferSize();
776 MeshDefinition::RawData
777 MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& buffers)
785 std::string meshPath;
786 meshPath = modelsPath + mUri;
787 std::fstream fileStream;
790 fileStream.open(meshPath, std::ios::in | std::ios::binary);
791 if(!fileStream.is_open())
793 DALI_LOG_ERROR("Fail to open buffer from %s.\n", meshPath.c_str());
797 if(mIndices.IsDefined())
799 if(MaskMatch(mFlags, U32_INDICES))
801 DALI_ASSERT_ALWAYS(((mIndices.mBlob.mLength % sizeof(uint32_t) == 0) ||
802 mIndices.mBlob.mStride >= sizeof(uint32_t)) &&
803 "Index buffer length not a multiple of element size");
804 const auto indexCount = mIndices.mBlob.GetBufferSize() / sizeof(uint32_t);
805 raw.mIndices.resize(indexCount * 2); // NOTE: we need space for uint32_ts initially.
808 auto& stream = GetAvailableData(fileStream, meshPath, buffers[mIndices.mBufferIdx], path);
809 if(!ReadAccessor(mIndices, stream, reinterpret_cast<uint8_t*>(raw.mIndices.data())))
811 ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indices from '" << path << "'.";
814 else if(MaskMatch(mFlags, U8_INDICES))
816 DALI_ASSERT_ALWAYS(((mIndices.mBlob.mLength % sizeof(uint8_t) == 0) ||
817 mIndices.mBlob.mStride >= sizeof(uint8_t)) &&
818 "Index buffer length not a multiple of element size");
819 const auto indexCount = mIndices.mBlob.GetBufferSize() / sizeof(uint8_t);
820 raw.mIndices.resize(indexCount); // NOTE: we need space for uint16_ts initially.
823 auto u8s = reinterpret_cast<uint8_t*>(raw.mIndices.data()) + indexCount;
824 auto& stream = GetAvailableData(fileStream, meshPath, buffers[mIndices.mBufferIdx], path);
825 if(!ReadAccessor(mIndices, stream, u8s))
827 ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indices from '" << path << "'.";
830 auto u16s = raw.mIndices.data();
831 auto end = u8s + indexCount;
834 *u16s = static_cast<uint16_t>(*u8s);
841 DALI_ASSERT_ALWAYS(((mIndices.mBlob.mLength % sizeof(unsigned short) == 0) ||
842 mIndices.mBlob.mStride >= sizeof(unsigned short)) &&
843 "Index buffer length not a multiple of element size");
844 raw.mIndices.resize(mIndices.mBlob.mLength / sizeof(unsigned short));
847 auto& stream = GetAvailableData(fileStream, meshPath, buffers[mIndices.mBufferIdx], path);
848 if(!ReadAccessor(mIndices, stream, reinterpret_cast<uint8_t*>(raw.mIndices.data())))
850 ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indices from '" << path << "'.";
855 std::vector<Vector3> positions;
856 if(mPositions.IsDefined())
858 DALI_ASSERT_ALWAYS(((mPositions.mBlob.mLength % sizeof(Vector3) == 0) ||
859 mPositions.mBlob.mStride >= sizeof(Vector3)) &&
860 "Position buffer length not a multiple of element size");
861 const auto bufferSize = mPositions.mBlob.GetBufferSize();
862 std::vector<uint8_t> buffer(bufferSize);
865 auto& stream = GetAvailableData(fileStream, meshPath, buffers[mPositions.mBufferIdx], path);
866 if(!ReadAccessor(mPositions, stream, buffer.data()))
868 ExceptionFlinger(ASSERT_LOCATION) << "Failed to read positions from '" << path << "'.";
871 uint32_t numVector3 = static_cast<uint32_t>(bufferSize / sizeof(Vector3));
872 if(mPositions.mBlob.mMin.size() != 3u || mPositions.mBlob.mMax.size() != 3u)
874 mPositions.mBlob.ComputeMinMax(3u, numVector3, reinterpret_cast<float*>(buffer.data()));
878 mPositions.mBlob.ApplyMinMax(numVector3, reinterpret_cast<float*>(buffer.data()));
883 positions.resize(numVector3);
884 std::copy(buffer.data(), buffer.data() + buffer.size(), reinterpret_cast<uint8_t*>(positions.data()));
887 raw.mAttribs.push_back({"aPosition", Property::VECTOR3, numVector3, std::move(buffer)});
890 const auto isTriangles = mPrimitiveType == Geometry::TRIANGLES;
891 auto hasNormals = mNormals.IsDefined();
894 DALI_ASSERT_ALWAYS(((mNormals.mBlob.mLength % sizeof(Vector3) == 0) ||
895 mNormals.mBlob.mStride >= sizeof(Vector3)) &&
896 "Normal buffer length not a multiple of element size");
897 const auto bufferSize = mNormals.mBlob.GetBufferSize();
898 std::vector<uint8_t> buffer(bufferSize);
901 auto& stream = GetAvailableData(fileStream, meshPath, buffers[mNormals.mBufferIdx], path);
902 if(!ReadAccessor(mNormals, stream, buffer.data()))
904 ExceptionFlinger(ASSERT_LOCATION) << "Failed to read normals from '" << path << "'.";
907 mNormals.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()));
909 raw.mAttribs.push_back({"aNormal", Property::VECTOR3, static_cast<uint32_t>(bufferSize / sizeof(Vector3)), std::move(buffer)});
911 else if(mNormals.mBlob.mLength != 0 && isTriangles)
913 DALI_ASSERT_DEBUG(mNormals.mBlob.mLength == mPositions.mBlob.GetBufferSize());
914 static const std::function<bool(RawData&)> GenerateNormalsFunction[2] =
916 GenerateNormals<false>,
917 GenerateNormals<true>,
919 const bool generateSuccessed = GenerateNormalsFunction[MaskMatch(mFlags, U32_INDICES)](raw);
920 if(!generateSuccessed)
922 DALI_LOG_ERROR("Failed to generate normal\n");
930 const auto hasUvs = mTexCoords.IsDefined();
933 DALI_ASSERT_ALWAYS(((mTexCoords.mBlob.mLength % sizeof(Vector2) == 0) ||
934 mTexCoords.mBlob.mStride >= sizeof(Vector2)) &&
935 "Normal buffer length not a multiple of element size");
936 const auto bufferSize = mTexCoords.mBlob.GetBufferSize();
937 std::vector<uint8_t> buffer(bufferSize);
940 auto& stream = GetAvailableData(fileStream, meshPath, buffers[mTexCoords.mBufferIdx], path);
941 if(!ReadAccessor(mTexCoords, stream, buffer.data()))
943 ExceptionFlinger(ASSERT_LOCATION) << "Failed to read uv-s from '" << path << "'.";
946 const auto uvCount = bufferSize / sizeof(Vector2);
947 if(MaskMatch(mFlags, FLIP_UVS_VERTICAL))
949 auto uv = reinterpret_cast<Vector2*>(buffer.data());
950 auto uvEnd = uv + uvCount;
953 uv->y = 1.0f - uv->y;
958 mTexCoords.mBlob.ApplyMinMax(static_cast<uint32_t>(uvCount), reinterpret_cast<float*>(buffer.data()));
960 raw.mAttribs.push_back({"aTexCoord", Property::VECTOR2, static_cast<uint32_t>(uvCount), std::move(buffer)});
963 if(mTangents.IsDefined())
965 uint32_t propertySize = static_cast<uint32_t>((mTangentType == Property::VECTOR4) ? sizeof(Vector4) : sizeof(Vector3));
966 DALI_ASSERT_ALWAYS(((mTangents.mBlob.mLength % propertySize == 0) ||
967 mTangents.mBlob.mStride >= propertySize) &&
968 "Tangents buffer length not a multiple of element size");
969 const auto bufferSize = mTangents.mBlob.GetBufferSize();
970 std::vector<uint8_t> buffer(bufferSize);
973 auto& stream = GetAvailableData(fileStream, meshPath, buffers[mTangents.mBufferIdx], path);
974 if(!ReadAccessor(mTangents, stream, buffer.data()))
976 ExceptionFlinger(ASSERT_LOCATION) << "Failed to read tangents from '" << path << "'.";
978 mTangents.mBlob.ApplyMinMax(bufferSize / propertySize, reinterpret_cast<float*>(buffer.data()));
980 raw.mAttribs.push_back({"aTangent", mTangentType, static_cast<uint32_t>(bufferSize / propertySize), std::move(buffer)});
982 else if(mTangents.mBlob.mLength != 0 && hasNormals && isTriangles)
984 DALI_ASSERT_DEBUG(mTangents.mBlob.mLength == mNormals.mBlob.GetBufferSize());
985 static const std::function<bool(RawData&)> GenerateTangentsFunction[2][2][2] =
989 GenerateTangents<false, false, false>,
990 GenerateTangents<false, false, true>,
993 GenerateTangents<false, true, false>,
994 GenerateTangents<false, true, true>,
999 GenerateTangents<true, false, false>,
1000 GenerateTangents<true, false, true>,
1003 GenerateTangents<true, true, false>,
1004 GenerateTangents<true, true, true>,
1007 const bool generateSuccessed = GenerateTangentsFunction[MaskMatch(mFlags, U32_INDICES)][mTangentType == Property::VECTOR3][hasUvs](raw);
1008 if(!generateSuccessed)
1010 DALI_LOG_ERROR("Failed to generate tangents\n");
1014 if(mColors.IsDefined())
1016 uint32_t propertySize = mColors.mBlob.mElementSizeHint;
1017 Property::Type propertyType = (propertySize == sizeof(Vector4)) ? Property::VECTOR4 : ((propertySize == sizeof(Vector3)) ? Property::VECTOR3 : Property::NONE);
1018 if(propertyType != Property::NONE)
1020 DALI_ASSERT_ALWAYS(((mColors.mBlob.mLength % propertySize == 0) ||
1021 mColors.mBlob.mStride >= propertySize) &&
1022 "Colors buffer length not a multiple of element size");
1023 const auto bufferSize = mColors.mBlob.GetBufferSize();
1024 std::vector<uint8_t> buffer(bufferSize);
1027 auto& stream = GetAvailableData(fileStream, meshPath, buffers[mColors.mBufferIdx], path);
1028 if(!ReadAccessor(mColors, stream, buffer.data()))
1030 ExceptionFlinger(ASSERT_LOCATION) << "Failed to read colors from '" << path << "'.";
1032 mColors.mBlob.ApplyMinMax(bufferSize / propertySize, reinterpret_cast<float*>(buffer.data()));
1034 raw.mAttribs.push_back({"aVertexColor", propertyType, static_cast<uint32_t>(bufferSize / propertySize), std::move(buffer)});
1039 std::vector<uint8_t> buffer(raw.mAttribs[0].mNumElements * sizeof(Vector4));
1040 auto colors = reinterpret_cast<Vector4*>(buffer.data());
1042 for(uint32_t i = 0; i < raw.mAttribs[0].mNumElements; i++)
1044 colors[i] = Vector4::ONE;
1047 raw.mAttribs.push_back({"aVertexColor", Property::VECTOR4, raw.mAttribs[0].mNumElements, std::move(buffer)});
1052 std::string pathJoint;
1053 auto& streamJoint = GetAvailableData(fileStream, meshPath, buffers[mJoints0.mBufferIdx], pathJoint);
1054 if(MaskMatch(mFlags, U16_JOINT_IDS))
1056 ReadJointAccessor<uint16_t>(raw, mJoints0, streamJoint, pathJoint);
1058 else if(MaskMatch(mFlags, U8_JOINT_IDS))
1060 ReadJointAccessor<uint8_t>(raw, mJoints0, streamJoint, pathJoint);
1064 ReadJointAccessor<float>(raw, mJoints0, streamJoint, pathJoint);
1067 std::string pathWeight;
1068 auto& streamWeight = GetAvailableData(fileStream, meshPath, buffers[mWeights0.mBufferIdx], pathWeight);
1069 if(MaskMatch(mFlags, U16_WEIGHT))
1071 ReadWeightAccessor<uint16_t>(raw, mWeights0, streamWeight, pathWeight);
1073 else if(MaskMatch(mFlags, U8_WEIGHT))
1075 ReadWeightAccessor<uint8_t>(raw, mWeights0, streamWeight, pathWeight);
1079 ReadWeightAccessor<float>(raw, mWeights0, streamWeight, pathWeight);
1083 // Calculate the Blob for the blend shapes.
1084 Blob blendShapesBlob;
1085 blendShapesBlob.mOffset = std::numeric_limits<unsigned int>::max();
1086 blendShapesBlob.mLength = 0u;
1088 for(const auto& blendShape : mBlendShapes)
1090 for(auto i : {&blendShape.deltas, &blendShape.normals, &blendShape.tangents})
1094 blendShapesBlob.mOffset = std::min(blendShapesBlob.mOffset, i->mBlob.mOffset);
1095 blendShapesBlob.mLength += i->mBlob.mLength;
1100 if(HasBlendShapes())
1102 const uint32_t numberOfVertices = static_cast<uint32_t>(mPositions.mBlob.mLength / sizeof(Vector3));
1104 // Calculate the size of one buffer inside the texture.
1105 raw.mBlendShapeBufferOffset = numberOfVertices;
1107 bool calculateGltf2BlendShapes = false;
1108 uint32_t textureWidth = 0u;
1109 uint32_t textureHeight = 0u;
1111 if(!mBlendShapeHeader.IsDefined())
1113 CalculateTextureSize(static_cast<uint32_t>(blendShapesBlob.mLength / sizeof(Vector3)), textureWidth, textureHeight);
1114 calculateGltf2BlendShapes = true;
1118 uint16_t header[2u];
1119 ReadBlob(mBlendShapeHeader, fileStream, reinterpret_cast<uint8_t*>(header));
1120 textureWidth = header[0u];
1121 textureHeight = header[1u];
1124 const uint32_t numberOfBlendShapes = mBlendShapes.size();
1125 raw.mBlendShapeUnnormalizeFactor.Resize(numberOfBlendShapes);
1127 Devel::PixelBuffer geometryPixelBuffer = Devel::PixelBuffer::New(textureWidth, textureHeight, Pixel::RGB32F);
1128 uint8_t* geometryBuffer = geometryPixelBuffer.GetBuffer();
1130 if(calculateGltf2BlendShapes)
1132 CalculateGltf2BlendShapes(geometryBuffer, mBlendShapes, numberOfVertices, raw.mBlendShapeUnnormalizeFactor[0u], buffers);
1136 Blob unnormalizeFactorBlob;
1137 unnormalizeFactorBlob.mLength = static_cast<uint32_t>(sizeof(float) * ((BlendShapes::Version::VERSION_2_0 == mBlendShapeVersion) ? 1u : numberOfBlendShapes));
1139 if(blendShapesBlob.IsDefined())
1141 if(ReadBlob(blendShapesBlob, fileStream, geometryBuffer))
1143 unnormalizeFactorBlob.mOffset = blendShapesBlob.mOffset + blendShapesBlob.mLength;
1147 // Read the unnormalize factors.
1148 if(unnormalizeFactorBlob.IsDefined())
1150 ReadBlob(unnormalizeFactorBlob, fileStream, reinterpret_cast<uint8_t*>(&raw.mBlendShapeUnnormalizeFactor[0u]));
1153 raw.mBlendShapeData = Devel::PixelBuffer::Convert(geometryPixelBuffer);
1159 MeshGeometry MeshDefinition::Load(RawData&& raw) const
1161 MeshGeometry meshGeometry;
1162 meshGeometry.geometry = Geometry::New();
1163 meshGeometry.geometry.SetType(mPrimitiveType);
1165 if(IsQuad()) // TODO: do this in raw data; provide MakeTexturedQuadGeometry() that only creates buffers.
1167 auto options = MaskMatch(mFlags, FLIP_UVS_VERTICAL) ? TexturedQuadOptions::FLIP_VERTICAL : 0;
1168 meshGeometry.geometry = MakeTexturedQuadGeometry(options);
1172 if(!raw.mIndices.empty())
1174 if(MaskMatch(mFlags, U32_INDICES))
1176 // TODO : We can only store indeces as uint16_type. Send Dali::Geometry that we use it as uint32_t actual.
1177 meshGeometry.geometry.SetIndexBuffer(reinterpret_cast<const uint32_t*>(raw.mIndices.data()), raw.mIndices.size() / 2);
1181 meshGeometry.geometry.SetIndexBuffer(raw.mIndices.data(), raw.mIndices.size());
1185 for(auto& a : raw.mAttribs)
1187 a.AttachBuffer(meshGeometry.geometry);
1190 if(HasBlendShapes())
1192 meshGeometry.blendShapeBufferOffset = raw.mBlendShapeBufferOffset;
1193 meshGeometry.blendShapeUnnormalizeFactor = std::move(raw.mBlendShapeUnnormalizeFactor);
1195 meshGeometry.blendShapeGeometry = Texture::New(TextureType::TEXTURE_2D,
1196 raw.mBlendShapeData.GetPixelFormat(),
1197 raw.mBlendShapeData.GetWidth(),
1198 raw.mBlendShapeData.GetHeight());
1199 meshGeometry.blendShapeGeometry.Upload(raw.mBlendShapeData);
1203 return meshGeometry;
1206 void MeshDefinition::RetrieveBlendShapeComponents(bool& hasPositions, bool& hasNormals, bool& hasTangents) const
1208 for(const auto& blendShape : mBlendShapes)
1210 hasPositions = hasPositions || blendShape.deltas.IsDefined();
1211 hasNormals = hasNormals || blendShape.normals.IsDefined();
1212 hasTangents = hasTangents || blendShape.tangents.IsDefined();
1216 } // namespace Dali::Scene3D::Loader