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>
29 #include <type_traits>
31 namespace Dali::Scene3D::Loader
35 template<bool use32BitIndices>
39 using IndexType = typename std::conditional_t<use32BitIndices, uint32_t, uint16_t>;
40 IndexProvider(const uint16_t* indices)
41 : mData(reinterpret_cast<uintptr_t>(indices)),
42 mFunc(indices ? IncrementPointer : Increment)
46 IndexType operator()()
52 static IndexType Increment(uintptr_t& data)
54 // mData was 'zero' at construct time. Just simply return counter start with 0.
55 return static_cast<IndexType>(data++);
58 static IndexType IncrementPointer(uintptr_t& data)
60 auto iPtr = reinterpret_cast<const IndexType*>(data);
62 data = reinterpret_cast<uintptr_t>(++iPtr);
67 IndexType (*mFunc)(uintptr_t&);
70 const char* QUAD("quad");
72 ///@brief Reads a blob from the given stream @a source into @a target, which must have
73 /// at least @a descriptor.length bytes.
74 bool ReadBlob(const MeshDefinition::Blob& descriptor, std::istream& source, uint8_t* target)
77 if(!source.seekg(descriptor.mOffset, std::istream::beg))
82 if(descriptor.IsConsecutive())
84 return !!source.read(reinterpret_cast<char*>(target), static_cast<std::streamsize>(static_cast<size_t>(descriptor.mLength)));
88 if(descriptor.mStride > descriptor.mElementSizeHint)
90 const uint32_t diff = descriptor.mStride - descriptor.mElementSizeHint;
91 uint32_t readSize = 0;
92 uint32_t totalSize = (descriptor.mLength / descriptor.mElementSizeHint) * descriptor.mStride;
93 while(readSize < totalSize &&
94 source.read(reinterpret_cast<char*>(target), descriptor.mElementSizeHint))
96 readSize += descriptor.mStride;
97 target += descriptor.mElementSizeHint;
98 source.seekg(diff, std::istream::cur);
100 return readSize == totalSize;
107 void ReadValues(const std::vector<uint8_t>& valuesBuffer, const std::vector<uint8_t>& indicesBuffer, uint8_t* target, uint32_t count, uint32_t elementSizeHint)
109 const T* const indicesPtr = reinterpret_cast<const T* const>(indicesBuffer.data());
110 for(uint32_t index = 0u; index < count; ++index)
112 uint32_t valuesIndex = indicesPtr[index] * elementSizeHint;
113 memcpy(target + valuesIndex, &valuesBuffer[index * elementSizeHint], elementSizeHint);
117 bool ReadAccessor(const MeshDefinition::Accessor& accessor, std::istream& source, uint8_t* target, std::vector<uint32_t>* sparseIndices)
119 bool success = false;
121 if(accessor.mBlob.IsDefined())
123 success = ReadBlob(accessor.mBlob, source, target);
132 const MeshDefinition::Blob& indices = accessor.mSparse->mIndices;
133 const MeshDefinition::Blob& values = accessor.mSparse->mValues;
135 if(!indices.IsDefined() || !values.IsDefined())
140 const auto indicesBufferSize = indices.GetBufferSize();
141 std::vector<uint8_t> indicesBuffer(indicesBufferSize);
142 success = ReadBlob(indices, source, indicesBuffer.data());
148 const auto valuesBufferSize = values.GetBufferSize();
149 std::vector<uint8_t> valuesBuffer(valuesBufferSize);
150 success = ReadBlob(values, source, valuesBuffer.data());
156 // If non-null sparse indices vector, prepare it for output
159 sparseIndices->resize(accessor.mSparse->mCount);
162 switch(indices.mElementSizeHint)
166 ReadValues<uint8_t>(valuesBuffer, indicesBuffer, target, accessor.mSparse->mCount, values.mElementSizeHint);
169 // convert 8-bit indices into 32-bit
170 std::transform(indicesBuffer.begin(), indicesBuffer.end(), sparseIndices->begin(), [](const uint8_t& value) { return uint32_t(value); });
176 ReadValues<uint16_t>(valuesBuffer, indicesBuffer, target, accessor.mSparse->mCount, values.mElementSizeHint);
179 // convert 16-bit indices into 32-bit
180 std::transform(reinterpret_cast<uint16_t*>(indicesBuffer.data()),
181 reinterpret_cast<uint16_t*>(indicesBuffer.data()) + accessor.mSparse->mCount,
182 sparseIndices->begin(),
183 [](const uint16_t& value) {
184 return uint32_t(value);
191 ReadValues<uint32_t>(valuesBuffer, indicesBuffer, target, accessor.mSparse->mCount, values.mElementSizeHint);
194 std::copy(indicesBuffer.begin(), indicesBuffer.end(), reinterpret_cast<uint8_t*>(sparseIndices->data()));
200 DALI_ASSERT_DEBUG(!"Unsupported type for an index");
208 bool ReadAccessor(const MeshDefinition::Accessor& accessor, std::istream& source, uint8_t* target)
210 return ReadAccessor(accessor, source, target, nullptr);
214 void ReadJointAccessor(MeshDefinition::RawData& raw, const MeshDefinition::Accessor& accessor, std::istream& source, const std::string& meshPath)
216 constexpr auto sizeofBlobUnit = sizeof(T) * 4;
218 DALI_ASSERT_ALWAYS(((accessor.mBlob.mLength % sizeofBlobUnit == 0) ||
219 accessor.mBlob.mStride >= sizeofBlobUnit) &&
220 "Joints buffer length not a multiple of element size");
221 const auto inBufferSize = accessor.mBlob.GetBufferSize();
222 const auto outBufferSize = (sizeof(Vector4) / sizeofBlobUnit) * inBufferSize;
224 std::vector<uint8_t> buffer(outBufferSize);
225 auto inBuffer = buffer.data() + outBufferSize - inBufferSize;
226 if(!ReadAccessor(accessor, source, inBuffer))
228 ExceptionFlinger(ASSERT_LOCATION) << "Failed to read joints from '" << meshPath << "'.";
231 if constexpr(sizeofBlobUnit != sizeof(Vector4))
233 auto floats = reinterpret_cast<float*>(buffer.data());
234 const auto end = inBuffer + inBufferSize;
235 while(inBuffer != end)
237 const auto value = *reinterpret_cast<T*>(inBuffer);
238 *floats = static_cast<float>(value);
240 inBuffer += sizeof(T);
244 raw.mAttribs.push_back({"aJoints", Property::VECTOR4, static_cast<uint32_t>(outBufferSize / sizeof(Vector4)), std::move(buffer)});
248 void ReadWeightAccessor(MeshDefinition::RawData& raw, const MeshDefinition::Accessor& accessor, std::istream& source, const std::string& meshPath)
250 constexpr auto sizeofBlobUnit = sizeof(T) * 4;
252 DALI_ASSERT_ALWAYS(((accessor.mBlob.mLength % sizeofBlobUnit == 0) ||
253 accessor.mBlob.mStride >= sizeofBlobUnit) &&
254 "weights buffer length not a multiple of element size");
255 const auto inBufferSize = accessor.mBlob.GetBufferSize();
256 const auto outBufferSize = (sizeof(Vector4) / sizeofBlobUnit) * inBufferSize;
258 std::vector<uint8_t> buffer(outBufferSize);
259 auto inBuffer = buffer.data() + outBufferSize - inBufferSize;
260 if(!ReadAccessor(accessor, source, inBuffer))
262 ExceptionFlinger(ASSERT_LOCATION) << "Failed to read weights from '" << meshPath << "'.";
265 if constexpr(sizeofBlobUnit != sizeof(Vector4))
267 auto floats = reinterpret_cast<float*>(buffer.data());
268 const auto end = inBuffer + inBufferSize;
269 while(inBuffer != end)
271 const auto value = *reinterpret_cast<T*>(inBuffer);
272 // Normalize weight value. value /= 255 for uint8_t weight, and value /= 65535 for uint16_t weight.
273 *floats = static_cast<float>(value) / static_cast<float>((1 << (sizeof(T) * 8)) - 1);
275 inBuffer += sizeof(T);
279 raw.mAttribs.push_back({"aWeights", Property::VECTOR4, static_cast<uint32_t>(outBufferSize / sizeof(Vector4)), std::move(buffer)});
282 template<bool use32BitsIndices, typename IndexProviderType = IndexProvider<use32BitsIndices>>
283 bool GenerateNormals(MeshDefinition::RawData& raw)
285 using IndexType = typename IndexProviderType::IndexType;
287 // mIndicies size must be even if we use 32bit indices.
288 if(DALI_UNLIKELY(use32BitsIndices && !raw.mIndices.empty() && !(raw.mIndices.size() % (sizeof(IndexType) / sizeof(uint16_t)) == 0)))
293 auto& attribs = raw.mAttribs;
294 DALI_ASSERT_DEBUG(attribs.size() > 0); // positions
296 IndexProviderType getIndex(raw.mIndices.data());
298 const uint32_t numIndices = raw.mIndices.empty() ? attribs[0].mNumElements : static_cast<uint32_t>(raw.mIndices.size() / (sizeof(IndexType) / sizeof(uint16_t)));
300 auto* positions = reinterpret_cast<const Vector3*>(attribs[0].mData.data());
302 std::vector<uint8_t> buffer(attribs[0].mNumElements * sizeof(Vector3));
303 auto normals = reinterpret_cast<Vector3*>(buffer.data());
305 for(uint32_t i = 0; i < numIndices; i += 3)
307 IndexType indices[]{getIndex(), getIndex(), getIndex()};
308 Vector3 pos[]{positions[indices[0]], positions[indices[1]], positions[indices[2]]};
310 Vector3 a = pos[1] - pos[0];
311 Vector3 b = pos[2] - pos[0];
313 Vector3 normal(a.Cross(b));
314 normals[indices[0]] += normal;
315 normals[indices[1]] += normal;
316 normals[indices[2]] += normal;
319 auto iEnd = normals + attribs[0].mNumElements;
320 while(normals != iEnd)
322 normals->Normalize();
326 attribs.push_back({"aNormal", Property::VECTOR3, attribs[0].mNumElements, std::move(buffer)});
331 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>>
332 bool GenerateTangents(MeshDefinition::RawData& raw)
334 using IndexType = typename IndexProviderType::IndexType;
336 // mIndicies size must be even if we use 32bit indices.
337 if(DALI_UNLIKELY(use32BitsIndices && !raw.mIndices.empty() && !(raw.mIndices.size() % (sizeof(IndexType) / sizeof(uint16_t)) == 0)))
342 auto& attribs = raw.mAttribs;
343 // Required positions, normals, uvs (if we have). If not, skip generation
344 if(DALI_UNLIKELY(attribs.size() < (2 + static_cast<size_t>(hasUvs))))
349 std::vector<uint8_t> buffer(attribs[0].mNumElements * sizeof(T));
350 auto tangents = reinterpret_cast<T*>(buffer.data());
354 IndexProviderType getIndex(raw.mIndices.data());
356 const uint32_t numIndices = raw.mIndices.empty() ? attribs[0].mNumElements : static_cast<uint32_t>(raw.mIndices.size() / (sizeof(IndexType) / sizeof(uint16_t)));
358 auto* positions = reinterpret_cast<const Vector3*>(attribs[0].mData.data());
359 auto* uvs = reinterpret_cast<const Vector2*>(attribs[2].mData.data());
361 for(uint32_t i = 0; i < numIndices; i += 3)
363 IndexType indices[]{getIndex(), getIndex(), getIndex()};
364 Vector3 pos[]{positions[indices[0]], positions[indices[1]], positions[indices[2]]};
365 Vector2 uv[]{uvs[indices[0]], uvs[indices[1]], uvs[indices[2]]};
367 float x0 = pos[1].x - pos[0].x;
368 float y0 = pos[1].y - pos[0].y;
369 float z0 = pos[1].z - pos[0].z;
371 float x1 = pos[2].x - pos[0].x;
372 float y1 = pos[2].y - pos[0].y;
373 float z1 = pos[2].z - pos[0].z;
375 float s0 = uv[1].x - uv[0].x;
376 float t0 = uv[1].y - uv[0].y;
378 float s1 = uv[2].x - uv[0].x;
379 float t1 = uv[2].y - uv[0].y;
381 float det = (s0 * t1 - t0 * s1);
382 float r = 1.f / ((std::abs(det) < Dali::Epsilon<1000>::value) ? (Dali::Epsilon<1000>::value * (det > 0.0f ? 1.f : -1.f)) : det);
383 Vector3 tangent((x0 * t1 - t0 * x1) * r, (y0 * t1 - t0 * y1) * r, (z0 * t1 - t0 * z1) * r);
384 tangents[indices[0]] += T(tangent);
385 tangents[indices[1]] += T(tangent);
386 tangents[indices[2]] += T(tangent);
390 auto* normals = reinterpret_cast<const Vector3*>(attribs[1].mData.data());
391 auto iEnd = normals + attribs[1].mNumElements;
392 while(normals != iEnd)
397 // Calculated by indexs
398 tangentVec3 = Vector3((*tangents).x, (*tangents).y, (*tangents).z);
402 // Only choiced by normal vector. by indexs
403 Vector3 t[]{normals->Cross(Vector3::XAXIS), normals->Cross(Vector3::YAXIS)};
404 tangentVec3 = t[t[1].LengthSquared() > t[0].LengthSquared()];
407 tangentVec3 -= *normals * normals->Dot(tangentVec3);
408 tangentVec3.Normalize();
409 if constexpr(useVec3)
411 *tangents = tangentVec3;
415 *tangents = Vector4(tangentVec3.x, tangentVec3.y, tangentVec3.z, 1.0f);
421 attribs.push_back({"aTangent", useVec3 ? Property::VECTOR3 : Property::VECTOR4, attribs[0].mNumElements, std::move(buffer)});
426 void CalculateTextureSize(uint32_t totalTextureSize, uint32_t& textureWidth, uint32_t& textureHeight)
428 DALI_ASSERT_DEBUG(0u != totalTextureSize && "totalTextureSize is zero.")
430 // Calculate the dimensions of the texture.
431 // The total size of the texture is the length of the blend shapes blob.
436 if(0u == totalTextureSize)
442 const uint32_t pow2 = static_cast<uint32_t>(ceil(log2(totalTextureSize)));
443 const uint32_t powWidth = pow2 >> 1u;
444 const uint32_t powHeight = pow2 - powWidth;
446 textureWidth = 1u << powWidth;
447 textureHeight = 1u << powHeight;
450 void CalculateGltf2BlendShapes(uint8_t* geometryBuffer, const std::vector<MeshDefinition::BlendShape>& blendShapes, uint32_t numberOfVertices, float& blendShapeUnnormalizeFactor, BufferDefinition::Vector& buffers)
452 uint32_t geometryBufferIndex = 0u;
453 float maxDistanceSquared = 0.f;
454 Vector3* geometryBufferV3 = reinterpret_cast<Vector3*>(geometryBuffer);
455 for(const auto& blendShape : blendShapes)
457 if(blendShape.deltas.IsDefined())
459 DALI_ASSERT_ALWAYS(((blendShape.deltas.mBlob.mLength % sizeof(Vector3) == 0u) ||
460 blendShape.deltas.mBlob.mStride >= sizeof(Vector3)) &&
461 "Blend Shape position buffer length not a multiple of element size");
463 const auto bufferSize = blendShape.deltas.mBlob.GetBufferSize();
464 std::vector<uint8_t> buffer(bufferSize);
465 std::vector<uint32_t> sparseIndices{};
467 if(ReadAccessor(blendShape.deltas, buffers[blendShape.deltas.mBufferIdx].GetBufferStream(), buffer.data(), &sparseIndices))
469 blendShape.deltas.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()), &sparseIndices);
471 // Calculate the difference with the original mesh.
472 // Find the max distance to normalize the deltas.
473 const auto* const deltasBuffer = reinterpret_cast<const Vector3* const>(buffer.data());
475 auto ProcessVertex = [&geometryBufferV3, &deltasBuffer, &maxDistanceSquared](uint32_t geometryBufferIndex, uint32_t deltaIndex) {
476 Vector3& delta = geometryBufferV3[geometryBufferIndex] = deltasBuffer[deltaIndex];
477 delta = deltasBuffer[deltaIndex];
478 return std::max(maxDistanceSquared, delta.LengthSquared());
481 if(sparseIndices.empty())
483 for(uint32_t index = 0u; index < numberOfVertices; ++index)
485 maxDistanceSquared = ProcessVertex(geometryBufferIndex++, index);
490 // initialize blendshape texture
491 // TODO: there may be a case when sparse accessor uses a base buffer view for initial values.
492 std::fill(geometryBufferV3 + geometryBufferIndex, geometryBufferV3 + geometryBufferIndex + numberOfVertices, Vector3::ZERO);
493 for(auto index : sparseIndices)
495 maxDistanceSquared = ProcessVertex(geometryBufferIndex + index, index);
497 geometryBufferIndex += numberOfVertices;
502 if(blendShape.normals.IsDefined())
504 DALI_ASSERT_ALWAYS(((blendShape.normals.mBlob.mLength % sizeof(Vector3) == 0u) ||
505 blendShape.normals.mBlob.mStride >= sizeof(Vector3)) &&
506 "Blend Shape normals buffer length not a multiple of element size");
508 const auto bufferSize = blendShape.normals.mBlob.GetBufferSize();
509 std::vector<uint8_t> buffer(bufferSize);
510 std::vector<uint32_t> sparseIndices;
512 if(ReadAccessor(blendShape.normals, buffers[blendShape.normals.mBufferIdx].GetBufferStream(), buffer.data(), &sparseIndices))
514 blendShape.normals.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()), &sparseIndices);
516 // Calculate the difference with the original mesh, and translate to make all values positive.
517 const Vector3* const deltasBuffer = reinterpret_cast<const Vector3* const>(buffer.data());
518 auto ProcessVertex = [&geometryBufferV3, &deltasBuffer, &maxDistanceSquared](uint32_t geometryBufferIndex, uint32_t deltaIndex) {
519 Vector3& delta = geometryBufferV3[geometryBufferIndex] = deltasBuffer[deltaIndex];
529 if(sparseIndices.empty())
531 for(uint32_t index = 0u; index < numberOfVertices; ++index)
533 ProcessVertex(geometryBufferIndex++, index);
538 std::fill(geometryBufferV3 + geometryBufferIndex, geometryBufferV3 + geometryBufferIndex + numberOfVertices, Vector3(0.5, 0.5, 0.5));
539 for(auto index : sparseIndices)
541 ProcessVertex(geometryBufferIndex + index, index);
543 geometryBufferIndex += numberOfVertices;
548 if(blendShape.tangents.IsDefined())
550 DALI_ASSERT_ALWAYS(((blendShape.tangents.mBlob.mLength % sizeof(Vector3) == 0u) ||
551 blendShape.tangents.mBlob.mStride >= sizeof(Vector3)) &&
552 "Blend Shape tangents buffer length not a multiple of element size");
554 const auto bufferSize = blendShape.tangents.mBlob.GetBufferSize();
555 std::vector<uint8_t> buffer(bufferSize);
556 std::vector<uint32_t> sparseIndices;
558 if(ReadAccessor(blendShape.tangents, buffers[blendShape.tangents.mBufferIdx].GetBufferStream(), buffer.data(), &sparseIndices))
560 blendShape.tangents.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()), &sparseIndices);
562 // Calculate the difference with the original mesh, and translate to make all values positive.
563 const Vector3* const deltasBuffer = reinterpret_cast<const Vector3* const>(buffer.data());
564 auto ProcessVertex = [&geometryBufferV3, &deltasBuffer, &maxDistanceSquared](uint32_t geometryBufferIndex, uint32_t deltaIndex) {
565 Vector3& delta = geometryBufferV3[geometryBufferIndex] = deltasBuffer[deltaIndex];
575 if(sparseIndices.empty())
577 for(uint32_t index = 0u; index < numberOfVertices; ++index)
579 ProcessVertex(geometryBufferIndex++, index);
584 std::fill(geometryBufferV3 + geometryBufferIndex, geometryBufferV3 + geometryBufferIndex + numberOfVertices, Vector3(0.5, 0.5, 0.5));
585 for(auto index : sparseIndices)
587 ProcessVertex(geometryBufferIndex + index, index);
589 geometryBufferIndex += numberOfVertices;
595 geometryBufferIndex = 0u;
597 const float maxDistance = sqrtf(maxDistanceSquared);
599 const float normalizeFactor = (maxDistanceSquared < Math::MACHINE_EPSILON_100) ? 1.f : (0.5f / maxDistance);
601 // Calculate and store the unnormalize factor.
602 blendShapeUnnormalizeFactor = maxDistance * 2.0f;
604 for(const auto& blendShape : blendShapes)
606 // Normalize all the deltas and translate to a possitive value.
607 // Deltas are going to be passed to the shader in a color texture
608 // whose values that are less than zero are clamped.
609 if(blendShape.deltas.IsDefined())
611 for(uint32_t index = 0u; index < numberOfVertices; ++index)
613 Vector3& delta = geometryBufferV3[geometryBufferIndex++];
614 delta.x = Clamp(((delta.x * normalizeFactor) + 0.5f), 0.f, 1.f);
615 delta.y = Clamp(((delta.y * normalizeFactor) + 0.5f), 0.f, 1.f);
616 delta.z = Clamp(((delta.z * normalizeFactor) + 0.5f), 0.f, 1.f);
620 if(blendShape.normals.IsDefined())
622 geometryBufferIndex += numberOfVertices;
625 if(blendShape.tangents.IsDefined())
627 geometryBufferIndex += numberOfVertices;
632 std::iostream& GetAvailableData(std::fstream& meshStream, const std::string& meshPath, BufferDefinition& buffer, std::string& availablePath)
634 auto& stream = (meshStream.is_open()) ? meshStream : buffer.GetBufferStream();
635 availablePath = (meshStream.is_open()) ? meshPath : buffer.GetUri();
641 MeshDefinition::SparseBlob::SparseBlob(const Blob& indices, const Blob& values, uint32_t count)
648 MeshDefinition::SparseBlob::SparseBlob(Blob&& indices, Blob&& values, uint32_t count)
649 : mIndices(std::move(indices)),
650 mValues(std::move(values)),
655 MeshDefinition::Accessor::Accessor(const MeshDefinition::Blob& blob,
656 const MeshDefinition::SparseBlob& sparse,
659 mSparse{(sparse.mIndices.IsDefined() && sparse.mValues.IsDefined()) ? new SparseBlob{sparse} : nullptr},
660 mBufferIdx(bufferIndex)
664 MeshDefinition::Accessor::Accessor(MeshDefinition::Blob&& blob,
665 MeshDefinition::SparseBlob&& sparse,
667 : mBlob{std::move(blob)},
668 mSparse{(sparse.mIndices.IsDefined() && sparse.mValues.IsDefined()) ? new SparseBlob{std::move(sparse)} : nullptr},
669 mBufferIdx(bufferIndex)
673 void MeshDefinition::Blob::ComputeMinMax(std::vector<float>& min, std::vector<float>& max, uint32_t numComponents, uint32_t count, const float* values)
675 min.assign(numComponents, MAXFLOAT);
676 max.assign(numComponents, -MAXFLOAT);
677 for(uint32_t i = 0; i < count; ++i)
679 for(uint32_t j = 0; j < numComponents; ++j)
681 min[j] = std::min(min[j], *values);
682 max[j] = std::max(max[j], *values);
688 void MeshDefinition::Blob::ApplyMinMax(const std::vector<float>& min, const std::vector<float>& max, uint32_t count, float* values, std::vector<uint32_t>* sparseIndices)
690 DALI_ASSERT_DEBUG(max.size() == min.size() || max.size() * min.size() == 0);
691 const auto numComponents = std::max(min.size(), max.size());
693 using ClampFn = void (*)(const float*, const float*, uint32_t, float&);
694 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); })
695 : (max.empty() ? [](const float* min, const float* max, uint32_t i, float& value) { value = std::max(min[i], value); }
696 : static_cast<ClampFn>([](const float* min, const float* max, uint32_t i, float& value) { value = std::min(std::max(min[i], value), max[i]); }));
703 auto end = values + count * numComponents;
706 auto nextElement = values + numComponents;
708 while(values != nextElement)
710 clampFn(min.data(), max.data(), i, *values);
717 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)
721 mElementSizeHint(elementSizeHint),
727 uint32_t MeshDefinition::Blob::GetBufferSize() const
732 void MeshDefinition::Blob::ComputeMinMax(uint32_t numComponents, uint32_t count, float* values)
734 ComputeMinMax(mMin, mMax, numComponents, count, values);
737 void MeshDefinition::Blob::ApplyMinMax(uint32_t count, float* values, std::vector<uint32_t>* sparseIndices) const
739 ApplyMinMax(mMin, mMax, count, values, sparseIndices);
742 void MeshDefinition::RawData::Attrib::AttachBuffer(Geometry& g) const
744 Property::Map attribMap;
745 attribMap[mName] = mType;
746 VertexBuffer attribBuffer = VertexBuffer::New(attribMap);
747 attribBuffer.SetData(mData.data(), mNumElements);
749 g.AddVertexBuffer(attribBuffer);
752 bool MeshDefinition::IsQuad() const
754 return CaseInsensitiveStringCompare(QUAD, mUri);
757 bool MeshDefinition::IsSkinned() const
759 return mJoints0.IsDefined() && mWeights0.IsDefined();
762 bool MeshDefinition::HasBlendShapes() const
764 return !mBlendShapes.empty();
767 void MeshDefinition::RequestNormals()
769 mNormals.mBlob.mLength = mPositions.mBlob.GetBufferSize();
772 void MeshDefinition::RequestTangents()
774 mTangents.mBlob.mLength = mNormals.mBlob.GetBufferSize();
777 MeshDefinition::RawData
778 MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& buffers)
786 std::string meshPath;
787 meshPath = modelsPath + mUri;
788 std::fstream fileStream;
791 fileStream.open(meshPath, std::ios::in | std::ios::binary);
792 if(!fileStream.is_open())
794 DALI_LOG_ERROR("Fail to open buffer from %s.\n", meshPath.c_str());
798 if(mIndices.IsDefined())
800 if(MaskMatch(mFlags, U32_INDICES))
802 DALI_ASSERT_ALWAYS(((mIndices.mBlob.mLength % sizeof(uint32_t) == 0) ||
803 mIndices.mBlob.mStride >= sizeof(uint32_t)) &&
804 "Index buffer length not a multiple of element size");
805 const auto indexCount = mIndices.mBlob.GetBufferSize() / sizeof(uint32_t);
806 raw.mIndices.resize(indexCount * 2); // NOTE: we need space for uint32_ts initially.
809 auto& stream = GetAvailableData(fileStream, meshPath, buffers[mIndices.mBufferIdx], path);
810 if(!ReadAccessor(mIndices, stream, reinterpret_cast<uint8_t*>(raw.mIndices.data())))
812 ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indices from '" << path << "'.";
815 else if(MaskMatch(mFlags, U8_INDICES))
817 DALI_ASSERT_ALWAYS(((mIndices.mBlob.mLength % sizeof(uint8_t) == 0) ||
818 mIndices.mBlob.mStride >= sizeof(uint8_t)) &&
819 "Index buffer length not a multiple of element size");
820 const auto indexCount = mIndices.mBlob.GetBufferSize() / sizeof(uint8_t);
821 raw.mIndices.resize(indexCount); // NOTE: we need space for uint16_ts initially.
824 auto u8s = reinterpret_cast<uint8_t*>(raw.mIndices.data()) + indexCount;
825 auto& stream = GetAvailableData(fileStream, meshPath, buffers[mIndices.mBufferIdx], path);
826 if(!ReadAccessor(mIndices, stream, u8s))
828 ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indices from '" << path << "'.";
831 auto u16s = raw.mIndices.data();
832 auto end = u8s + indexCount;
835 *u16s = static_cast<uint16_t>(*u8s);
842 DALI_ASSERT_ALWAYS(((mIndices.mBlob.mLength % sizeof(unsigned short) == 0) ||
843 mIndices.mBlob.mStride >= sizeof(unsigned short)) &&
844 "Index buffer length not a multiple of element size");
845 raw.mIndices.resize(mIndices.mBlob.mLength / sizeof(unsigned short));
848 auto& stream = GetAvailableData(fileStream, meshPath, buffers[mIndices.mBufferIdx], path);
849 if(!ReadAccessor(mIndices, stream, reinterpret_cast<uint8_t*>(raw.mIndices.data())))
851 ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indices from '" << path << "'.";
856 std::vector<Vector3> positions;
857 if(mPositions.IsDefined())
859 DALI_ASSERT_ALWAYS(((mPositions.mBlob.mLength % sizeof(Vector3) == 0) ||
860 mPositions.mBlob.mStride >= sizeof(Vector3)) &&
861 "Position buffer length not a multiple of element size");
862 const auto bufferSize = mPositions.mBlob.GetBufferSize();
863 std::vector<uint8_t> buffer(bufferSize);
866 auto& stream = GetAvailableData(fileStream, meshPath, buffers[mPositions.mBufferIdx], path);
867 if(!ReadAccessor(mPositions, stream, buffer.data()))
869 ExceptionFlinger(ASSERT_LOCATION) << "Failed to read positions from '" << path << "'.";
872 uint32_t numVector3 = static_cast<uint32_t>(bufferSize / sizeof(Vector3));
873 if(mPositions.mBlob.mMin.size() != 3u || mPositions.mBlob.mMax.size() != 3u)
875 mPositions.mBlob.ComputeMinMax(3u, numVector3, reinterpret_cast<float*>(buffer.data()));
879 mPositions.mBlob.ApplyMinMax(numVector3, reinterpret_cast<float*>(buffer.data()));
884 positions.resize(numVector3);
885 std::copy(buffer.data(), buffer.data() + buffer.size(), reinterpret_cast<uint8_t*>(positions.data()));
888 raw.mAttribs.push_back({"aPosition", Property::VECTOR3, numVector3, std::move(buffer)});
891 const auto isTriangles = mPrimitiveType == Geometry::TRIANGLES;
892 auto hasNormals = mNormals.IsDefined();
895 DALI_ASSERT_ALWAYS(((mNormals.mBlob.mLength % sizeof(Vector3) == 0) ||
896 mNormals.mBlob.mStride >= sizeof(Vector3)) &&
897 "Normal buffer length not a multiple of element size");
898 const auto bufferSize = mNormals.mBlob.GetBufferSize();
899 std::vector<uint8_t> buffer(bufferSize);
902 auto& stream = GetAvailableData(fileStream, meshPath, buffers[mNormals.mBufferIdx], path);
903 if(!ReadAccessor(mNormals, stream, buffer.data()))
905 ExceptionFlinger(ASSERT_LOCATION) << "Failed to read normals from '" << path << "'.";
908 mNormals.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()));
910 raw.mAttribs.push_back({"aNormal", Property::VECTOR3, static_cast<uint32_t>(bufferSize / sizeof(Vector3)), std::move(buffer)});
912 else if(mNormals.mBlob.mLength != 0 && isTriangles)
914 DALI_ASSERT_DEBUG(mNormals.mBlob.mLength == mPositions.mBlob.GetBufferSize());
915 static const std::function<bool(RawData&)> GenerateNormalsFunction[2] =
917 GenerateNormals<false>,
918 GenerateNormals<true>,
920 const bool generateSuccessed = GenerateNormalsFunction[MaskMatch(mFlags, U32_INDICES)](raw);
921 if(!generateSuccessed)
923 DALI_LOG_ERROR("Failed to generate normal\n");
931 const auto hasUvs = mTexCoords.IsDefined();
934 DALI_ASSERT_ALWAYS(((mTexCoords.mBlob.mLength % sizeof(Vector2) == 0) ||
935 mTexCoords.mBlob.mStride >= sizeof(Vector2)) &&
936 "Normal buffer length not a multiple of element size");
937 const auto bufferSize = mTexCoords.mBlob.GetBufferSize();
938 std::vector<uint8_t> buffer(bufferSize);
941 auto& stream = GetAvailableData(fileStream, meshPath, buffers[mTexCoords.mBufferIdx], path);
942 if(!ReadAccessor(mTexCoords, stream, buffer.data()))
944 ExceptionFlinger(ASSERT_LOCATION) << "Failed to read uv-s from '" << path << "'.";
947 const auto uvCount = bufferSize / sizeof(Vector2);
948 if(MaskMatch(mFlags, FLIP_UVS_VERTICAL))
950 auto uv = reinterpret_cast<Vector2*>(buffer.data());
951 auto uvEnd = uv + uvCount;
954 uv->y = 1.0f - uv->y;
959 mTexCoords.mBlob.ApplyMinMax(static_cast<uint32_t>(uvCount), reinterpret_cast<float*>(buffer.data()));
961 raw.mAttribs.push_back({"aTexCoord", Property::VECTOR2, static_cast<uint32_t>(uvCount), std::move(buffer)});
964 if(mTangents.IsDefined())
966 uint32_t propertySize = static_cast<uint32_t>((mTangentType == Property::VECTOR4) ? sizeof(Vector4) : sizeof(Vector3));
967 DALI_ASSERT_ALWAYS(((mTangents.mBlob.mLength % propertySize == 0) ||
968 mTangents.mBlob.mStride >= propertySize) &&
969 "Tangents buffer length not a multiple of element size");
970 const auto bufferSize = mTangents.mBlob.GetBufferSize();
971 std::vector<uint8_t> buffer(bufferSize);
974 auto& stream = GetAvailableData(fileStream, meshPath, buffers[mTangents.mBufferIdx], path);
975 if(!ReadAccessor(mTangents, stream, buffer.data()))
977 ExceptionFlinger(ASSERT_LOCATION) << "Failed to read tangents from '" << path << "'.";
979 mTangents.mBlob.ApplyMinMax(bufferSize / propertySize, reinterpret_cast<float*>(buffer.data()));
981 raw.mAttribs.push_back({"aTangent", mTangentType, static_cast<uint32_t>(bufferSize / propertySize), std::move(buffer)});
983 else if(mTangents.mBlob.mLength != 0 && hasNormals && isTriangles)
985 DALI_ASSERT_DEBUG(mTangents.mBlob.mLength == mNormals.mBlob.GetBufferSize());
986 static const std::function<bool(RawData&)> GenerateTangentsFunction[2][2][2] =
990 GenerateTangents<false, false, false>,
991 GenerateTangents<false, false, true>,
994 GenerateTangents<false, true, false>,
995 GenerateTangents<false, true, true>,
1000 GenerateTangents<true, false, false>,
1001 GenerateTangents<true, false, true>,
1004 GenerateTangents<true, true, false>,
1005 GenerateTangents<true, true, true>,
1008 const bool generateSuccessed = GenerateTangentsFunction[MaskMatch(mFlags, U32_INDICES)][mTangentType == Property::VECTOR3][hasUvs](raw);
1009 if(!generateSuccessed)
1011 DALI_LOG_ERROR("Failed to generate tangents\n");
1015 if(mColors.IsDefined())
1017 uint32_t propertySize = mColors.mBlob.mElementSizeHint;
1018 Property::Type propertyType = (propertySize == sizeof(Vector4)) ? Property::VECTOR4 : ((propertySize == sizeof(Vector3)) ? Property::VECTOR3 : Property::NONE);
1019 if(propertyType != Property::NONE)
1021 DALI_ASSERT_ALWAYS(((mColors.mBlob.mLength % propertySize == 0) ||
1022 mColors.mBlob.mStride >= propertySize) &&
1023 "Colors buffer length not a multiple of element size");
1024 const auto bufferSize = mColors.mBlob.GetBufferSize();
1025 std::vector<uint8_t> buffer(bufferSize);
1028 auto& stream = GetAvailableData(fileStream, meshPath, buffers[mColors.mBufferIdx], path);
1029 if(!ReadAccessor(mColors, stream, buffer.data()))
1031 ExceptionFlinger(ASSERT_LOCATION) << "Failed to read colors from '" << path << "'.";
1033 mColors.mBlob.ApplyMinMax(bufferSize / propertySize, reinterpret_cast<float*>(buffer.data()));
1035 raw.mAttribs.push_back({"aVertexColor", propertyType, static_cast<uint32_t>(bufferSize / propertySize), std::move(buffer)});
1040 std::vector<uint8_t> buffer(raw.mAttribs[0].mNumElements * sizeof(Vector4));
1041 auto colors = reinterpret_cast<Vector4*>(buffer.data());
1043 for(uint32_t i = 0; i < raw.mAttribs[0].mNumElements; i++)
1045 colors[i] = Vector4::ONE;
1048 raw.mAttribs.push_back({"aVertexColor", Property::VECTOR4, raw.mAttribs[0].mNumElements, std::move(buffer)});
1053 std::string pathJoint;
1054 auto& streamJoint = GetAvailableData(fileStream, meshPath, buffers[mJoints0.mBufferIdx], pathJoint);
1055 if(MaskMatch(mFlags, U16_JOINT_IDS))
1057 ReadJointAccessor<uint16_t>(raw, mJoints0, streamJoint, pathJoint);
1059 else if(MaskMatch(mFlags, U8_JOINT_IDS))
1061 ReadJointAccessor<uint8_t>(raw, mJoints0, streamJoint, pathJoint);
1065 ReadJointAccessor<float>(raw, mJoints0, streamJoint, pathJoint);
1068 std::string pathWeight;
1069 auto& streamWeight = GetAvailableData(fileStream, meshPath, buffers[mWeights0.mBufferIdx], pathWeight);
1070 if(MaskMatch(mFlags, U16_WEIGHT))
1072 ReadWeightAccessor<uint16_t>(raw, mWeights0, streamWeight, pathWeight);
1074 else if(MaskMatch(mFlags, U8_WEIGHT))
1076 ReadWeightAccessor<uint8_t>(raw, mWeights0, streamWeight, pathWeight);
1080 ReadWeightAccessor<float>(raw, mWeights0, streamWeight, pathWeight);
1084 // Calculate the Blob for the blend shapes.
1085 Blob blendShapesBlob;
1086 blendShapesBlob.mOffset = std::numeric_limits<unsigned int>::max();
1087 blendShapesBlob.mLength = 0u;
1089 for(const auto& blendShape : mBlendShapes)
1091 for(auto i : {&blendShape.deltas, &blendShape.normals, &blendShape.tangents})
1095 blendShapesBlob.mOffset = std::min(blendShapesBlob.mOffset, i->mBlob.mOffset);
1096 blendShapesBlob.mLength += i->mBlob.mLength;
1101 if(HasBlendShapes())
1103 const uint32_t numberOfVertices = static_cast<uint32_t>(mPositions.mBlob.mLength / sizeof(Vector3));
1105 // Calculate the size of one buffer inside the texture.
1106 raw.mBlendShapeBufferOffset = numberOfVertices;
1108 bool calculateGltf2BlendShapes = false;
1109 uint32_t textureWidth = 0u;
1110 uint32_t textureHeight = 0u;
1112 if(!mBlendShapeHeader.IsDefined())
1114 CalculateTextureSize(static_cast<uint32_t>(blendShapesBlob.mLength / sizeof(Vector3)), textureWidth, textureHeight);
1115 calculateGltf2BlendShapes = true;
1119 uint16_t header[2u];
1120 ReadBlob(mBlendShapeHeader, fileStream, reinterpret_cast<uint8_t*>(header));
1121 textureWidth = header[0u];
1122 textureHeight = header[1u];
1125 const uint32_t numberOfBlendShapes = mBlendShapes.size();
1126 raw.mBlendShapeUnnormalizeFactor.Resize(numberOfBlendShapes);
1128 Devel::PixelBuffer geometryPixelBuffer = Devel::PixelBuffer::New(textureWidth, textureHeight, Pixel::RGB32F);
1129 uint8_t* geometryBuffer = geometryPixelBuffer.GetBuffer();
1131 if(calculateGltf2BlendShapes)
1133 CalculateGltf2BlendShapes(geometryBuffer, mBlendShapes, numberOfVertices, raw.mBlendShapeUnnormalizeFactor[0u], buffers);
1137 Blob unnormalizeFactorBlob;
1138 unnormalizeFactorBlob.mLength = static_cast<uint32_t>(sizeof(float) * ((BlendShapes::Version::VERSION_2_0 == mBlendShapeVersion) ? 1u : numberOfBlendShapes));
1140 if(blendShapesBlob.IsDefined())
1142 if(ReadBlob(blendShapesBlob, fileStream, geometryBuffer))
1144 unnormalizeFactorBlob.mOffset = blendShapesBlob.mOffset + blendShapesBlob.mLength;
1148 // Read the unnormalize factors.
1149 if(unnormalizeFactorBlob.IsDefined())
1151 ReadBlob(unnormalizeFactorBlob, fileStream, reinterpret_cast<uint8_t*>(&raw.mBlendShapeUnnormalizeFactor[0u]));
1154 raw.mBlendShapeData = Devel::PixelBuffer::Convert(geometryPixelBuffer);
1160 MeshGeometry MeshDefinition::Load(RawData&& raw) const
1162 MeshGeometry meshGeometry;
1163 meshGeometry.geometry = Geometry::New();
1164 meshGeometry.geometry.SetType(mPrimitiveType);
1166 if(IsQuad()) // TODO: do this in raw data; provide MakeTexturedQuadGeometry() that only creates buffers.
1168 auto options = MaskMatch(mFlags, FLIP_UVS_VERTICAL) ? TexturedQuadOptions::FLIP_VERTICAL : 0;
1169 meshGeometry.geometry = MakeTexturedQuadGeometry(options);
1173 if(!raw.mIndices.empty())
1175 if(MaskMatch(mFlags, U32_INDICES))
1177 // TODO : We can only store indeces as uint16_type. Send Dali::Geometry that we use it as uint32_t actual.
1178 meshGeometry.geometry.SetIndexBuffer(reinterpret_cast<const uint32_t*>(raw.mIndices.data()), raw.mIndices.size() / 2);
1182 meshGeometry.geometry.SetIndexBuffer(raw.mIndices.data(), raw.mIndices.size());
1186 for(auto& a : raw.mAttribs)
1188 a.AttachBuffer(meshGeometry.geometry);
1191 if(HasBlendShapes())
1193 meshGeometry.blendShapeBufferOffset = raw.mBlendShapeBufferOffset;
1194 meshGeometry.blendShapeUnnormalizeFactor = std::move(raw.mBlendShapeUnnormalizeFactor);
1196 meshGeometry.blendShapeGeometry = Texture::New(TextureType::TEXTURE_2D,
1197 raw.mBlendShapeData.GetPixelFormat(),
1198 raw.mBlendShapeData.GetWidth(),
1199 raw.mBlendShapeData.GetHeight());
1200 meshGeometry.blendShapeGeometry.Upload(raw.mBlendShapeData);
1204 return meshGeometry;
1207 void MeshDefinition::RetrieveBlendShapeComponents(bool& hasPositions, bool& hasNormals, bool& hasTangents) const
1209 for(const auto& blendShape : mBlendShapes)
1211 hasPositions = hasPositions || blendShape.deltas.IsDefined();
1212 hasNormals = hasNormals || blendShape.normals.IsDefined();
1213 hasTangents = hasTangents || blendShape.tangents.IsDefined();
1217 } // namespace Dali::Scene3D::Loader