X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali-scene3d%2Fpublic-api%2Floader%2Fmesh-definition.cpp;h=67287903a12cfb963bf100530e866e4780e30db8;hb=HEAD;hp=3beaf83ab9abd39d74d8e1066d71657262957063;hpb=26494b2d0fe9c0e1b7296bfc6f8c5228a36a5b07;p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git diff --git a/dali-scene3d/public-api/loader/mesh-definition.cpp b/dali-scene3d/public-api/loader/mesh-definition.cpp index 3beaf83..6728790 100644 --- a/dali-scene3d/public-api/loader/mesh-definition.cpp +++ b/dali-scene3d/public-api/loader/mesh-definition.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * Copyright (c) 2024 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,64 +15,93 @@ * */ -// INTERNAL INCLUDES -#include "dali-scene3d/public-api/loader/mesh-definition.h" +// CLASS HEADER +#include // EXTERNAL INCLUDES #include +#include #include +#include #include #include -#include "dali/devel-api/adaptor-framework/pixel-buffer.h" +#include +#include -namespace Dali +namespace Dali::Scene3D::Loader { -namespace Scene3D +namespace { -namespace Loader +enum class LoadDataType { -namespace + UNSIGNED_BYTE = 0, + UNSIGNED_SHORT, + FLOAT +}; + +struct LoadAccessorInputs { + MeshDefinition::RawData& rawData; + MeshDefinition::Accessor& accessor; + uint32_t flags; + std::fstream& fileStream; + std::string& meshPath; + BufferDefinition::Vector& buffers; +}; + +struct LoadAccessorListInputs +{ + MeshDefinition::RawData& rawData; + std::vector& accessors; + uint32_t flags; + std::fstream& fileStream; + std::string& meshPath; + BufferDefinition::Vector& buffers; +}; + +template class IndexProvider { public: + using IndexType = typename std::conditional_t; IndexProvider(const uint16_t* indices) : mData(reinterpret_cast(indices)), mFunc(indices ? IncrementPointer : Increment) { } - uint16_t operator()() + IndexType operator()() { return mFunc(mData); } private: - static uint16_t Increment(uintptr_t& data) + static IndexType Increment(uintptr_t& data) { - return static_cast(data++); + // mData was 'zero' at construct time. Just simply return counter start with 0. + return static_cast(data++); } - static uint16_t IncrementPointer(uintptr_t& data) + static IndexType IncrementPointer(uintptr_t& data) { - auto iPtr = reinterpret_cast(data); + auto iPtr = reinterpret_cast(data); auto result = *iPtr; data = reinterpret_cast(++iPtr); return result; } uintptr_t mData; - uint16_t (*mFunc)(uintptr_t&); + IndexType (*mFunc)(uintptr_t&); }; -const std::string QUAD("quad"); +const char* QUAD("quad"); ///@brief Reads a blob from the given stream @a source into @a target, which must have /// at least @a descriptor.length bytes. bool ReadBlob(const MeshDefinition::Blob& descriptor, std::istream& source, uint8_t* target) { source.clear(); - if(!source.seekg(descriptor.mOffset, std::istream::beg)) + if(!source.seekg(static_cast(static_cast(descriptor.mOffset)), std::istream::beg)) { return false; } @@ -89,11 +118,11 @@ bool ReadBlob(const MeshDefinition::Blob& descriptor, std::istream& source, uint uint32_t readSize = 0; uint32_t totalSize = (descriptor.mLength / descriptor.mElementSizeHint) * descriptor.mStride; while(readSize < totalSize && - source.read(reinterpret_cast(target), descriptor.mElementSizeHint) && - source.seekg(diff, std::istream::cur)) + source.read(reinterpret_cast(target), descriptor.mElementSizeHint)) { readSize += descriptor.mStride; target += descriptor.mElementSizeHint; + source.seekg(static_cast(static_cast(diff)), std::istream::cur); } return readSize == totalSize; } @@ -112,7 +141,7 @@ void ReadValues(const std::vector& valuesBuffer, const std::vector* sparseIndices) { bool success = false; @@ -151,72 +180,136 @@ bool ReadAccessor(const MeshDefinition::Accessor& accessor, std::istream& source return false; } + // If non-null sparse indices vector, prepare it for output + if(sparseIndices) + { + sparseIndices->resize(accessor.mSparse->mCount); + } + switch(indices.mElementSizeHint) { case 1u: { ReadValues(valuesBuffer, indicesBuffer, target, accessor.mSparse->mCount, values.mElementSizeHint); + if(sparseIndices) + { + // convert 8-bit indices into 32-bit + std::transform(indicesBuffer.begin(), indicesBuffer.end(), sparseIndices->begin(), [](const uint8_t& value) { return uint32_t(value); }); + } break; } case 2u: { ReadValues(valuesBuffer, indicesBuffer, target, accessor.mSparse->mCount, values.mElementSizeHint); + if(sparseIndices) + { + // convert 16-bit indices into 32-bit + std::transform(reinterpret_cast(indicesBuffer.data()), + reinterpret_cast(indicesBuffer.data()) + accessor.mSparse->mCount, + sparseIndices->begin(), + [](const uint16_t& value) { + return uint32_t(value); + }); + } break; } case 4u: { ReadValues(valuesBuffer, indicesBuffer, target, accessor.mSparse->mCount, values.mElementSizeHint); + if(sparseIndices) + { + std::copy(indicesBuffer.begin(), indicesBuffer.end(), reinterpret_cast(sparseIndices->data())); + } break; } default: + { DALI_ASSERT_DEBUG(!"Unsupported type for an index"); + } } } return success; } -template -void ReadJointAccessor(MeshDefinition::RawData& raw, const MeshDefinition::Accessor& accessor, std::istream& source, const std::string& meshPath) +bool ReadAccessor(const MeshDefinition::Accessor& accessor, std::istream& source, uint8_t* target) +{ + return ReadAccessor(accessor, source, target, nullptr); +} + +template +void ReadVectorAccessor(const MeshDefinition::Accessor& accessor, std::istream& source, std::vector& buffer) { constexpr auto sizeofBlobUnit = sizeof(T) * 4; DALI_ASSERT_ALWAYS(((accessor.mBlob.mLength % sizeofBlobUnit == 0) || accessor.mBlob.mStride >= sizeofBlobUnit) && - "Joints buffer length not a multiple of element size"); + "Buffer length not a multiple of element size"); const auto inBufferSize = accessor.mBlob.GetBufferSize(); const auto outBufferSize = (sizeof(Vector4) / sizeofBlobUnit) * inBufferSize; - std::vector buffer(outBufferSize); - auto inBuffer = buffer.data() + outBufferSize - inBufferSize; + buffer.resize(outBufferSize); + auto inBuffer = buffer.data() + outBufferSize - inBufferSize; if(!ReadAccessor(accessor, source, inBuffer)) { - ExceptionFlinger(ASSERT_LOCATION) << "Failed to read joints from '" << meshPath << "'."; + ExceptionFlinger(ASSERT_LOCATION) << "Failed to read vector data from Accessor."; } - if constexpr(sizeofBlobUnit != sizeof(Vector4)) + if(sizeofBlobUnit != sizeof(Vector4)) { auto floats = reinterpret_cast(buffer.data()); const auto end = inBuffer + inBufferSize; while(inBuffer != end) { const auto value = *reinterpret_cast(inBuffer); - *floats = static_cast(value); + *floats = (needsNormalize) ? static_cast(value) / static_cast((1 << (sizeof(T) * 8)) - 1) : static_cast(value); inBuffer += sizeof(T); ++floats; } } - raw.mAttribs.push_back({"aJoints", Property::VECTOR4, static_cast(outBufferSize / sizeof(Vector4)), std::move(buffer)}); } -void GenerateNormals(MeshDefinition::RawData& raw) +template +void ReadTypedVectorAccessor(LoadDataType loadDataType, MeshDefinition::Accessor& accessor, std::iostream& stream, std::vector& buffer) +{ + switch(loadDataType) + { + case LoadDataType::UNSIGNED_SHORT: + { + ReadVectorAccessor(accessor, stream, buffer); + break; + } + case LoadDataType::UNSIGNED_BYTE: + { + ReadVectorAccessor(accessor, stream, buffer); + break; + } + default: + { + ReadVectorAccessor(accessor, stream, buffer); + break; + } + } +} + +template> +bool GenerateNormals(MeshDefinition::RawData& raw) { + using IndexType = typename IndexProviderType::IndexType; + + // mIndicies size must be even if we use 32bit indices. + if(DALI_UNLIKELY(use32BitsIndices && !raw.mIndices.empty() && !(raw.mIndices.size() % (sizeof(IndexType) / sizeof(uint16_t)) == 0))) + { + return false; + } + auto& attribs = raw.mAttribs; DALI_ASSERT_DEBUG(attribs.size() > 0); // positions - IndexProvider getIndex(raw.mIndices.data()); - const uint32_t numIndices = raw.mIndices.empty() ? attribs[0].mNumElements : static_cast(raw.mIndices.size()); + IndexProviderType getIndex(raw.mIndices.data()); + + const uint32_t numIndices = raw.mIndices.empty() ? attribs[0].mNumElements : static_cast(raw.mIndices.size() / (sizeof(IndexType) / sizeof(uint16_t))); auto* positions = reinterpret_cast(attribs[0].mData.data()); @@ -225,8 +318,8 @@ void GenerateNormals(MeshDefinition::RawData& raw) for(uint32_t i = 0; i < numIndices; i += 3) { - uint16_t indices[]{getIndex(), getIndex(), getIndex()}; - Vector3 pos[]{positions[indices[0]], positions[indices[1]], positions[indices[2]]}; + IndexType indices[]{getIndex(), getIndex(), getIndex()}; + Vector3 pos[]{positions[indices[0]], positions[indices[1]], positions[indices[2]]}; Vector3 a = pos[1] - pos[0]; Vector3 b = pos[2] - pos[0]; @@ -245,85 +338,103 @@ void GenerateNormals(MeshDefinition::RawData& raw) } attribs.push_back({"aNormal", Property::VECTOR3, attribs[0].mNumElements, std::move(buffer)}); + + return true; } -void GenerateTangentsWithUvs(MeshDefinition::RawData& raw) +template, typename = std::enable_if_t<(std::is_same::value || std::is_same::value)>, typename IndexProviderType = IndexProvider> +bool GenerateTangents(MeshDefinition::RawData& raw) { - auto& attribs = raw.mAttribs; - DALI_ASSERT_DEBUG(attribs.size() > 2); // positions, normals, uvs - IndexProvider getIndex(raw.mIndices.data()); - - const uint32_t numIndices = raw.mIndices.empty() ? attribs[0].mNumElements : static_cast(raw.mIndices.size()); - - auto* positions = reinterpret_cast(attribs[0].mData.data()); - auto* uvs = reinterpret_cast(attribs[2].mData.data()); + using IndexType = typename IndexProviderType::IndexType; - std::vector buffer(attribs[0].mNumElements * sizeof(Vector3)); - auto tangents = reinterpret_cast(buffer.data()); + // mIndicies size must be even if we use 32bit indices. + if(DALI_UNLIKELY(use32BitsIndices && !raw.mIndices.empty() && !(raw.mIndices.size() % (sizeof(IndexType) / sizeof(uint16_t)) == 0))) + { + return false; + } - for(uint32_t i = 0; i < numIndices; i += 3) + auto& attribs = raw.mAttribs; + // Required positions, normals, uvs (if we have). If not, skip generation + if(DALI_UNLIKELY(attribs.size() < (2 + static_cast(hasUvs)))) { - uint16_t indices[]{getIndex(), getIndex(), getIndex()}; - Vector3 pos[]{positions[indices[0]], positions[indices[1]], positions[indices[2]]}; - Vector2 uv[]{uvs[indices[0]], uvs[indices[1]], uvs[indices[2]]}; + return false; + } - float x0 = pos[1].x - pos[0].x; - float y0 = pos[1].y - pos[0].y; - float z0 = pos[1].z - pos[0].z; + std::vector buffer(attribs[0].mNumElements * sizeof(T)); + auto tangents = reinterpret_cast(buffer.data()); - float x1 = pos[2].x - pos[0].x; - float y1 = pos[2].y - pos[0].y; - float z1 = pos[2].z - pos[0].z; + if constexpr(hasUvs) + { + IndexProviderType getIndex(raw.mIndices.data()); - float s0 = uv[1].x - uv[0].x; - float t0 = uv[1].y - uv[0].y; + const uint32_t numIndices = raw.mIndices.empty() ? attribs[0].mNumElements : static_cast(raw.mIndices.size() / (sizeof(IndexType) / sizeof(uint16_t))); - float s1 = uv[2].x - uv[0].x; - float t1 = uv[2].y - uv[0].y; + auto* positions = reinterpret_cast(attribs[0].mData.data()); + auto* uvs = reinterpret_cast(attribs[2].mData.data()); - float r = 1.f / (s0 * t1 - t0 * s1); - Vector3 tangent((x0 * t1 - t0 * x1) * r, (y0 * t1 - t0 * y1) * r, (z0 * t1 - t0 * z1) * r); - tangents[indices[0]] += tangent; - tangents[indices[1]] += tangent; - tangents[indices[2]] += tangent; + for(uint32_t i = 0; i < numIndices; i += 3) + { + IndexType indices[]{getIndex(), getIndex(), getIndex()}; + Vector3 pos[]{positions[indices[0]], positions[indices[1]], positions[indices[2]]}; + Vector2 uv[]{uvs[indices[0]], uvs[indices[1]], uvs[indices[2]]}; + + float x0 = pos[1].x - pos[0].x; + float y0 = pos[1].y - pos[0].y; + float z0 = pos[1].z - pos[0].z; + + float x1 = pos[2].x - pos[0].x; + float y1 = pos[2].y - pos[0].y; + float z1 = pos[2].z - pos[0].z; + + float s0 = uv[1].x - uv[0].x; + float t0 = uv[1].y - uv[0].y; + + float s1 = uv[2].x - uv[0].x; + float t1 = uv[2].y - uv[0].y; + + float det = (s0 * t1 - t0 * s1); + float r = 1.f / ((std::abs(det) < Dali::Epsilon<1000>::value) ? (Dali::Epsilon<1000>::value * (det > 0.0f ? 1.f : -1.f)) : det); + Vector3 tangent((x0 * t1 - t0 * x1) * r, (y0 * t1 - t0 * y1) * r, (z0 * t1 - t0 * z1) * r); + tangents[indices[0]] += T(tangent); + tangents[indices[1]] += T(tangent); + tangents[indices[2]] += T(tangent); + } } auto* normals = reinterpret_cast(attribs[1].mData.data()); auto iEnd = normals + attribs[1].mNumElements; while(normals != iEnd) { - *tangents -= *normals * normals->Dot(*tangents); - tangents->Normalize(); - - ++tangents; - ++normals; - } - attribs.push_back({"aTangent", Property::VECTOR3, attribs[0].mNumElements, std::move(buffer)}); -} - -void GenerateTangents(MeshDefinition::RawData& raw) -{ - auto& attribs = raw.mAttribs; - DALI_ASSERT_DEBUG(attribs.size() > 1); // positions, normals - - auto* normals = reinterpret_cast(attribs[1].mData.data()); - - std::vector buffer(attribs[0].mNumElements * sizeof(Vector3)); - auto tangents = reinterpret_cast(buffer.data()); - - auto iEnd = normals + attribs[1].mNumElements; - while(normals != iEnd) - { - Vector3 t[]{normals->Cross(Vector3::XAXIS), normals->Cross(Vector3::YAXIS)}; + Vector3 tangentVec3; + if constexpr(hasUvs) + { + // Calculated by indexs + tangentVec3 = Vector3((*tangents).x, (*tangents).y, (*tangents).z); + } + else + { + // Only choiced by normal vector. by indexs + Vector3 t[]{normals->Cross(Vector3::XAXIS), normals->Cross(Vector3::YAXIS)}; + tangentVec3 = t[t[1].LengthSquared() > t[0].LengthSquared()]; + } - *tangents = t[t[1].LengthSquared() > t[0].LengthSquared()]; - *tangents -= *normals * normals->Dot(*tangents); - tangents->Normalize(); + tangentVec3 -= *normals * normals->Dot(tangentVec3); + tangentVec3.Normalize(); + if constexpr(useVec3) + { + *tangents = tangentVec3; + } + else + { + *tangents = Vector4(tangentVec3.x, tangentVec3.y, tangentVec3.z, 1.0f); + } ++tangents; ++normals; } - attribs.push_back({"aTangent", Property::VECTOR3, attribs[0].mNumElements, std::move(buffer)}); + attribs.push_back({"aTangent", useVec3 ? Property::VECTOR3 : Property::VECTOR4, attribs[0].mNumElements, std::move(buffer)}); + + return true; } void CalculateTextureSize(uint32_t totalTextureSize, uint32_t& textureWidth, uint32_t& textureHeight) @@ -350,58 +461,221 @@ void CalculateTextureSize(uint32_t totalTextureSize, uint32_t& textureWidth, uin textureHeight = 1u << powHeight; } -void CalculateGltf2BlendShapes(uint8_t* geometryBuffer, const std::vector& blendShapes, uint32_t numberOfVertices, float& blendShapeUnnormalizeFactor, BufferDefinition::Vector& buffers) +template +float GetNormalizedScale() +{ + return 1.0f / (std::numeric_limits::max()); +} + +template +void DequantizeData(std::vector& buffer, float* dequantizedValues, uint32_t numValues, bool normalized) +{ + // see https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_mesh_quantization#encoding-quantized-data + + T* values = reinterpret_cast(buffer.data()); + + for(uint32_t i = 0; i < numValues; ++i) + { + *dequantizedValues = normalized ? std::max((*values) * GetNormalizedScale(), -1.0f) : *values; + + values++; + dequantizedValues++; + } +} + +void GetDequantizedData(std::vector& buffer, uint32_t numComponents, uint32_t count, uint32_t flags, bool normalized) +{ + bool dequantized = false; + + std::vector dequantizedBuffer(count * numComponents * sizeof(float)); + float* dequantizedValues = reinterpret_cast(dequantizedBuffer.data()); + + if(MaskMatch(flags, MeshDefinition::Flags::S8_POSITION) || MaskMatch(flags, MeshDefinition::Flags::S8_NORMAL) || MaskMatch(flags, MeshDefinition::Flags::S8_TANGENT) || MaskMatch(flags, MeshDefinition::Flags::S8_TEXCOORD)) + { + DequantizeData(buffer, dequantizedValues, numComponents * count, normalized); + dequantized = true; + } + else if(MaskMatch(flags, MeshDefinition::Flags::U8_POSITION) || MaskMatch(flags, MeshDefinition::Flags::U8_TEXCOORD)) + { + DequantizeData(buffer, dequantizedValues, numComponents * count, normalized); + dequantized = true; + } + else if(MaskMatch(flags, MeshDefinition::Flags::S16_POSITION) || MaskMatch(flags, MeshDefinition::Flags::S16_NORMAL) || MaskMatch(flags, MeshDefinition::Flags::S16_TANGENT) || MaskMatch(flags, MeshDefinition::Flags::S16_TEXCOORD)) + { + DequantizeData(buffer, dequantizedValues, numComponents * count, normalized); + dequantized = true; + } + else if(MaskMatch(flags, MeshDefinition::Flags::U16_POSITION) || MaskMatch(flags, MeshDefinition::Flags::U16_TEXCOORD)) + { + DequantizeData(buffer, dequantizedValues, numComponents * count, normalized); + dequantized = true; + } + + if(dequantized) + { + buffer = std::move(dequantizedBuffer); + } +} + +void GetDequantizedMinMax(std::vector& min, std::vector& max, uint32_t flags) +{ + float scale = 1.0f; + + if(MaskMatch(flags, MeshDefinition::Flags::S8_POSITION) || MaskMatch(flags, MeshDefinition::Flags::S8_NORMAL) || MaskMatch(flags, MeshDefinition::Flags::S8_TANGENT) || MaskMatch(flags, MeshDefinition::Flags::S8_TEXCOORD)) + { + scale = GetNormalizedScale(); + } + else if(MaskMatch(flags, MeshDefinition::Flags::U8_POSITION) || MaskMatch(flags, MeshDefinition::Flags::U8_TEXCOORD)) + { + scale = GetNormalizedScale(); + } + else if(MaskMatch(flags, MeshDefinition::Flags::S16_POSITION) || MaskMatch(flags, MeshDefinition::Flags::S16_NORMAL) || MaskMatch(flags, MeshDefinition::Flags::S16_TANGENT) || MaskMatch(flags, MeshDefinition::Flags::S16_TEXCOORD)) + { + scale = GetNormalizedScale(); + } + else if(MaskMatch(flags, MeshDefinition::Flags::U16_POSITION) || MaskMatch(flags, MeshDefinition::Flags::U16_TEXCOORD)) + { + scale = GetNormalizedScale(); + } + + if(scale != 1.0f) + { + for(float& value : min) + { + value = std::max(value * scale, -1.0f); + } + + for(float& value : max) + { + value = std::min(value * scale, 1.0f); + } + } +} + +void CalculateGltf2BlendShapes(uint8_t* geometryBuffer, std::vector& blendShapes, uint32_t numberOfVertices, float& blendShapeUnnormalizeFactor, BufferDefinition::Vector& buffers) { uint32_t geometryBufferIndex = 0u; - float maxDistance = 0.f; + float maxDistanceSquared = 0.f; Vector3* geometryBufferV3 = reinterpret_cast(geometryBuffer); - for(const auto& blendShape : blendShapes) + for(auto& blendShape : blendShapes) { if(blendShape.deltas.IsDefined()) { - DALI_ASSERT_ALWAYS(((blendShape.deltas.mBlob.mLength % sizeof(Vector3) == 0u) || - blendShape.deltas.mBlob.mStride >= sizeof(Vector3)) && - "Blend Shape position buffer length not a multiple of element size"); + const auto bufferSize = blendShape.deltas.mBlob.GetBufferSize(); + uint32_t numVector3; - const auto bufferSize = blendShape.deltas.mBlob.GetBufferSize(); - std::vector buffer(bufferSize); - if(ReadAccessor(blendShape.deltas, buffers[blendShape.deltas.mBufferIdx].GetBufferStream(), buffer.data())) + if(MaskMatch(blendShape.mFlags, MeshDefinition::S8_POSITION)) + { + DALI_ASSERT_ALWAYS(((blendShape.deltas.mBlob.mLength % (sizeof(uint8_t) * 3) == 0) || + blendShape.deltas.mBlob.mStride >= (sizeof(uint8_t) * 3)) && + "Blend Shape position buffer length not a multiple of element size"); + numVector3 = static_cast(bufferSize / (sizeof(uint8_t) * 3)); + } + else if(MaskMatch(blendShape.mFlags, MeshDefinition::S16_POSITION)) + { + DALI_ASSERT_ALWAYS(((blendShape.deltas.mBlob.mLength % (sizeof(uint16_t) * 3) == 0) || + blendShape.deltas.mBlob.mStride >= (sizeof(uint16_t) * 3)) && + "Blend Shape position buffer length not a multiple of element size"); + numVector3 = static_cast(bufferSize / (sizeof(uint16_t) * 3)); + } + else + { + DALI_ASSERT_ALWAYS(((blendShape.deltas.mBlob.mLength % sizeof(Vector3) == 0) || + blendShape.deltas.mBlob.mStride >= sizeof(Vector3)) && + "Blend Shape position buffer length not a multiple of element size"); + numVector3 = static_cast(bufferSize / sizeof(Vector3)); + } + + std::vector buffer(bufferSize); + std::vector sparseIndices{}; + + if(ReadAccessor(blendShape.deltas, buffers[blendShape.deltas.mBufferIdx].GetBufferStream(), buffer.data(), &sparseIndices)) { - blendShape.deltas.mBlob.ApplyMinMax(static_cast(bufferSize / sizeof(Vector3)), reinterpret_cast(buffer.data())); + GetDequantizedData(buffer, 3u, numVector3, blendShape.mFlags & MeshDefinition::POSITIONS_MASK, blendShape.deltas.mNormalized); + + if(blendShape.deltas.mNormalized) + { + GetDequantizedMinMax(blendShape.deltas.mBlob.mMin, blendShape.deltas.mBlob.mMax, blendShape.mFlags & MeshDefinition::POSITIONS_MASK); + } + + blendShape.deltas.mBlob.ApplyMinMax(numVector3, reinterpret_cast(buffer.data()), &sparseIndices); + // Calculate the difference with the original mesh. // Find the max distance to normalize the deltas. - const Vector3* const deltasBuffer = reinterpret_cast(buffer.data()); + const auto* const deltasBuffer = reinterpret_cast(buffer.data()); - for(uint32_t index = 0u; index < numberOfVertices; ++index) - { - Vector3& delta = geometryBufferV3[geometryBufferIndex++]; - delta = deltasBuffer[index]; + auto ProcessVertex = [&geometryBufferV3, &deltasBuffer, &maxDistanceSquared](uint32_t geometryBufferIndex, uint32_t deltaIndex) { + Vector3& delta = geometryBufferV3[geometryBufferIndex] = deltasBuffer[deltaIndex]; + delta = deltasBuffer[deltaIndex]; + return std::max(maxDistanceSquared, delta.LengthSquared()); + }; - maxDistance = std::max(maxDistance, delta.LengthSquared()); + if(sparseIndices.empty()) + { + for(uint32_t index = 0u; index < numberOfVertices; ++index) + { + maxDistanceSquared = ProcessVertex(geometryBufferIndex++, index); + } + } + else + { + // initialize blendshape texture + // TODO: there may be a case when sparse accessor uses a base buffer view for initial values. + std::fill(geometryBufferV3 + geometryBufferIndex, geometryBufferV3 + geometryBufferIndex + numberOfVertices, Vector3::ZERO); + for(auto index : sparseIndices) + { + maxDistanceSquared = ProcessVertex(geometryBufferIndex + index, index); + } + geometryBufferIndex += numberOfVertices; } } } if(blendShape.normals.IsDefined()) { - DALI_ASSERT_ALWAYS(((blendShape.normals.mBlob.mLength % sizeof(Vector3) == 0u) || - blendShape.normals.mBlob.mStride >= sizeof(Vector3)) && - "Blend Shape normals buffer length not a multiple of element size"); + const auto bufferSize = blendShape.normals.mBlob.GetBufferSize(); + uint32_t numVector3; - const auto bufferSize = blendShape.normals.mBlob.GetBufferSize(); - std::vector buffer(bufferSize); - if(ReadAccessor(blendShape.normals, buffers[blendShape.normals.mBufferIdx].GetBufferStream(), buffer.data())) + if(MaskMatch(blendShape.mFlags, MeshDefinition::S8_NORMAL)) + { + DALI_ASSERT_ALWAYS(((blendShape.normals.mBlob.mLength % (sizeof(int8_t) * 3) == 0) || + blendShape.normals.mBlob.mStride >= (sizeof(int8_t) * 3)) && + "Blend Shape normals buffer length not a multiple of element size"); + numVector3 = static_cast(bufferSize / (sizeof(int8_t) * 3)); + } + else if(MaskMatch(blendShape.mFlags, MeshDefinition::S16_NORMAL)) { - blendShape.normals.mBlob.ApplyMinMax(static_cast(bufferSize / sizeof(Vector3)), reinterpret_cast(buffer.data())); + DALI_ASSERT_ALWAYS(((blendShape.normals.mBlob.mLength % (sizeof(int16_t) * 3) == 0) || + blendShape.normals.mBlob.mStride >= (sizeof(int16_t) * 3)) && + "Blend Shape normals buffer length not a multiple of element size"); + numVector3 = static_cast(bufferSize / (sizeof(int16_t) * 3)); + } + else + { + DALI_ASSERT_ALWAYS(((blendShape.normals.mBlob.mLength % sizeof(Vector3) == 0) || + blendShape.normals.mBlob.mStride >= sizeof(Vector3)) && + "Blend Shape normals buffer length not a multiple of element size"); + numVector3 = static_cast(bufferSize / sizeof(Vector3)); + } - // Calculate the difference with the original mesh, and translate to make all values positive. - const Vector3* const deltasBuffer = reinterpret_cast(buffer.data()); + std::vector buffer(bufferSize); + std::vector sparseIndices; - for(uint32_t index = 0u; index < numberOfVertices; ++index) + if(ReadAccessor(blendShape.normals, buffers[blendShape.normals.mBufferIdx].GetBufferStream(), buffer.data(), &sparseIndices)) + { + GetDequantizedData(buffer, 3u, numVector3, blendShape.mFlags & MeshDefinition::NORMALS_MASK, blendShape.normals.mNormalized); + + if(blendShape.normals.mNormalized) { - Vector3& delta = geometryBufferV3[geometryBufferIndex++]; - delta = deltasBuffer[index]; + GetDequantizedMinMax(blendShape.normals.mBlob.mMin, blendShape.normals.mBlob.mMax, blendShape.mFlags & MeshDefinition::NORMALS_MASK); + } + + blendShape.normals.mBlob.ApplyMinMax(numVector3, reinterpret_cast(buffer.data()), &sparseIndices); + // Calculate the difference with the original mesh, and translate to make all values positive. + const Vector3* const deltasBuffer = reinterpret_cast(buffer.data()); + auto ProcessVertex = [&geometryBufferV3, &deltasBuffer, &maxDistanceSquared](uint32_t geometryBufferIndex, uint32_t deltaIndex) { + Vector3& delta = geometryBufferV3[geometryBufferIndex] = deltasBuffer[deltaIndex]; delta.x *= 0.5f; delta.y *= 0.5f; delta.z *= 0.5f; @@ -409,30 +683,73 @@ void CalculateGltf2BlendShapes(uint8_t* geometryBuffer, const std::vector= sizeof(Vector3)) && - "Blend Shape tangents buffer length not a multiple of element size"); + const auto bufferSize = blendShape.tangents.mBlob.GetBufferSize(); - const auto bufferSize = blendShape.tangents.mBlob.GetBufferSize(); - std::vector buffer(bufferSize); - if(ReadAccessor(blendShape.tangents, buffers[blendShape.tangents.mBufferIdx].GetBufferStream(), buffer.data())) + uint32_t numVector3; + + if(MaskMatch(blendShape.mFlags, MeshDefinition::S8_TANGENT)) + { + DALI_ASSERT_ALWAYS(((blendShape.tangents.mBlob.mLength % (sizeof(int8_t) * 3) == 0) || + blendShape.tangents.mBlob.mStride >= (sizeof(int8_t) * 3)) && + "Blend Shape tangents buffer length not a multiple of element size"); + numVector3 = static_cast(bufferSize / (sizeof(int8_t) * 3)); + } + else if(MaskMatch(blendShape.mFlags, MeshDefinition::S16_TANGENT)) + { + DALI_ASSERT_ALWAYS(((blendShape.tangents.mBlob.mLength % (sizeof(int16_t) * 3) == 0) || + blendShape.tangents.mBlob.mStride >= (sizeof(int16_t) * 3)) && + "Blend Shape tangents buffer length not a multiple of element size"); + numVector3 = static_cast(bufferSize / (sizeof(int16_t) * 3)); + } + else { - blendShape.tangents.mBlob.ApplyMinMax(static_cast(bufferSize / sizeof(Vector3)), reinterpret_cast(buffer.data())); + DALI_ASSERT_ALWAYS(((blendShape.tangents.mBlob.mLength % sizeof(Vector3) == 0) || + blendShape.tangents.mBlob.mStride >= sizeof(Vector3)) && + "Blend Shape tangents buffer length not a multiple of element size"); + numVector3 = static_cast(bufferSize / sizeof(Vector3)); + } - // Calculate the difference with the original mesh, and translate to make all values positive. - const Vector3* const deltasBuffer = reinterpret_cast(buffer.data()); + std::vector buffer(bufferSize); + std::vector sparseIndices; + + if(ReadAccessor(blendShape.tangents, buffers[blendShape.tangents.mBufferIdx].GetBufferStream(), buffer.data(), &sparseIndices)) + { + GetDequantizedData(buffer, 3u, numVector3, blendShape.mFlags & MeshDefinition::TANGENTS_MASK, blendShape.tangents.mNormalized); - for(uint32_t index = 0u; index < numberOfVertices; ++index) + if(blendShape.tangents.mNormalized) { - Vector3& delta = geometryBufferV3[geometryBufferIndex++]; - delta = deltasBuffer[index]; + GetDequantizedMinMax(blendShape.tangents.mBlob.mMin, blendShape.tangents.mBlob.mMax, blendShape.mFlags & MeshDefinition::TANGENTS_MASK); + } + blendShape.tangents.mBlob.ApplyMinMax(numVector3, reinterpret_cast(buffer.data()), &sparseIndices); + + // Calculate the difference with the original mesh, and translate to make all values positive. + const Vector3* const deltasBuffer = reinterpret_cast(buffer.data()); + auto ProcessVertex = [&geometryBufferV3, &deltasBuffer, &maxDistanceSquared](uint32_t geometryBufferIndex, uint32_t deltaIndex) { + Vector3& delta = geometryBufferV3[geometryBufferIndex] = deltasBuffer[deltaIndex]; delta.x *= 0.5f; delta.y *= 0.5f; delta.z *= 0.5f; @@ -440,12 +757,37 @@ void CalculateGltf2BlendShapes(uint8_t* geometryBuffer, const std::vector +void ReadTypedVectorAccessors(LoadAccessorListInputs loadAccessorListInputs, LoadDataType loadDataType, std::string attributeName) { + int setIndex = 0; + for(auto& accessor : loadAccessorListInputs.accessors) + { + std::string pathJoint; + auto& dataStream = GetAvailableData(loadAccessorListInputs.fileStream, loadAccessorListInputs.meshPath, loadAccessorListInputs.buffers[accessor.mBufferIdx], pathJoint); + std::ostringstream name; + name << attributeName << setIndex++; + std::vector buffer; + ReadTypedVectorAccessor(loadDataType, accessor, dataStream, buffer); + loadAccessorListInputs.rawData.mAttribs.push_back({name.str(), Property::VECTOR4, static_cast(buffer.size() / sizeof(Vector4)), std::move(buffer)}); + } } -MeshDefinition::SparseBlob::SparseBlob(Blob&& indices, Blob&& values, uint32_t count) -: mIndices(std::move(indices)), - mValues(std::move(values)), - mCount{count} +void LoadIndices(LoadAccessorInputs indicesInput) { -} + if(indicesInput.accessor.IsDefined()) + { + if(MaskMatch(indicesInput.flags, MeshDefinition::Flags::U32_INDICES)) + { + DALI_ASSERT_ALWAYS(((indicesInput.accessor.mBlob.mLength % sizeof(uint32_t) == 0) || + indicesInput.accessor.mBlob.mStride >= sizeof(uint32_t)) && + "Index buffer length not a multiple of element size"); + const auto indexCount = indicesInput.accessor.mBlob.GetBufferSize() / sizeof(uint32_t); + indicesInput.rawData.mIndices.resize(indexCount * 2); // NOTE: we need space for uint32_ts initially. -MeshDefinition::Accessor::Accessor(const MeshDefinition::Blob& blob, - const MeshDefinition::SparseBlob& sparse, - Index bufferIndex) -: mBlob{blob}, - mSparse{(sparse.mIndices.IsDefined() && sparse.mValues.IsDefined()) ? new SparseBlob{sparse} : nullptr}, - mBufferIdx(bufferIndex) -{ -} + std::string path; + auto& stream = GetAvailableData(indicesInput.fileStream, indicesInput.meshPath, indicesInput.buffers[indicesInput.accessor.mBufferIdx], path); + if(!ReadAccessor(indicesInput.accessor, stream, reinterpret_cast(indicesInput.rawData.mIndices.data()))) + { + ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indices from '" << path << "'."; + } + } + else if(MaskMatch(indicesInput.flags, MeshDefinition::Flags::U8_INDICES)) + { + DALI_ASSERT_ALWAYS(((indicesInput.accessor.mBlob.mLength % sizeof(uint8_t) == 0) || + indicesInput.accessor.mBlob.mStride >= sizeof(uint8_t)) && + "Index buffer length not a multiple of element size"); + const auto indexCount = indicesInput.accessor.mBlob.GetBufferSize() / sizeof(uint8_t); + indicesInput.rawData.mIndices.resize(indexCount); // NOTE: we need space for uint16_ts initially. -MeshDefinition::Accessor::Accessor(MeshDefinition::Blob&& blob, - MeshDefinition::SparseBlob&& sparse, - Index bufferIndex) -: mBlob{std::move(blob)}, - mSparse{(sparse.mIndices.IsDefined() && sparse.mValues.IsDefined()) ? new SparseBlob{std::move(sparse)} : nullptr}, - mBufferIdx(bufferIndex) -{ -} + std::string path; + auto u8s = reinterpret_cast(indicesInput.rawData.mIndices.data()) + indexCount; + auto& stream = GetAvailableData(indicesInput.fileStream, indicesInput.meshPath, indicesInput.buffers[indicesInput.accessor.mBufferIdx], path); + if(!ReadAccessor(indicesInput.accessor, stream, u8s)) + { + ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indices from '" << path << "'."; + } -void MeshDefinition::Blob::ComputeMinMax(std::vector& min, std::vector& max, uint32_t numComponents, uint32_t count, const float* values) -{ - min.assign(numComponents, MAXFLOAT); - max.assign(numComponents, -MAXFLOAT); - for(uint32_t i = 0; i < count; ++i) - { - for(uint32_t j = 0; j < numComponents; ++j) + auto u16s = indicesInput.rawData.mIndices.data(); + auto end = u8s + indexCount; + while(u8s != end) + { + *u16s = static_cast(*u8s); + ++u16s; + ++u8s; + } + } + else { - min[j] = std::min(min[j], *values); - max[j] = std::max(max[j], *values); - values++; + DALI_ASSERT_ALWAYS(((indicesInput.accessor.mBlob.mLength % sizeof(unsigned short) == 0) || + indicesInput.accessor.mBlob.mStride >= sizeof(unsigned short)) && + "Index buffer length not a multiple of element size"); + indicesInput.rawData.mIndices.resize(indicesInput.accessor.mBlob.mLength / sizeof(unsigned short)); + + std::string path; + auto& stream = GetAvailableData(indicesInput.fileStream, indicesInput.meshPath, indicesInput.buffers[indicesInput.accessor.mBufferIdx], path); + if(!ReadAccessor(indicesInput.accessor, stream, reinterpret_cast(indicesInput.rawData.mIndices.data()))) + { + ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indicesInput.accessor from '" << path << "'."; + } } } } -void MeshDefinition::Blob::ApplyMinMax(const std::vector& min, const std::vector& max, uint32_t count, float* values) +uint32_t LoadPositions(LoadAccessorInputs positionsInput, bool hasBlendShape) { - DALI_ASSERT_DEBUG(max.size() == min.size() || max.size() * min.size() == 0); - const auto numComponents = std::max(min.size(), max.size()); - - using ClampFn = void (*)(const float*, const float*, uint32_t, float&); - ClampFn clampFn = min.empty() ? (max.empty() ? static_cast(nullptr) : [](const float* min, const float* max, uint32_t i, float& value) { value = std::min(max[i], value); }) - : (max.empty() ? [](const float* min, const float* max, uint32_t i, float& value) { value = std::max(min[i], value); } - : static_cast([](const float* min, const float* max, uint32_t i, float& value) { value = std::min(std::max(min[i], value), max[i]); })); - - if(!clampFn) + uint32_t numVector3 = 0u; + std::vector positions; + if(positionsInput.accessor.IsDefined()) { - return; - } + const auto bufferSize = positionsInput.accessor.mBlob.GetBufferSize(); - auto end = values + count * numComponents; - while(values != end) - { - auto nextElement = values + numComponents; - uint32_t i = 0; - while(values != nextElement) + if(MaskMatch(positionsInput.flags, MeshDefinition::Flags::S8_POSITION) || MaskMatch(positionsInput.flags, MeshDefinition::Flags::U8_POSITION)) { - clampFn(min.data(), max.data(), i, *values); - ++values; - ++i; + DALI_ASSERT_ALWAYS(((positionsInput.accessor.mBlob.mLength % (sizeof(uint8_t) * 3) == 0) || + positionsInput.accessor.mBlob.mStride >= (sizeof(uint8_t) * 3)) && + "Position buffer length not a multiple of element size"); + numVector3 = static_cast(bufferSize / (sizeof(uint8_t) * 3)); } - } -} - -MeshDefinition::Blob::Blob(uint32_t offset, uint32_t length, uint16_t stride, uint16_t elementSizeHint, const std::vector& min, const std::vector& max) -: mOffset(offset), - mLength(length), - mStride(stride), - mElementSizeHint(elementSizeHint), - mMin(min), - mMax(max) -{ -} - -uint32_t MeshDefinition::Blob::GetBufferSize() const -{ - return mLength; -} - -void MeshDefinition::Blob::ComputeMinMax(uint32_t numComponents, uint32_t count, float* values) -{ - ComputeMinMax(mMin, mMax, numComponents, count, values); -} - -void MeshDefinition::Blob::ApplyMinMax(uint32_t count, float* values) const -{ - ApplyMinMax(mMin, mMax, count, values); -} - -void MeshDefinition::RawData::Attrib::AttachBuffer(Geometry& g) const -{ - Property::Map attribMap; - attribMap[mName] = mType; - VertexBuffer attribBuffer = VertexBuffer::New(attribMap); - attribBuffer.SetData(mData.data(), mNumElements); - - g.AddVertexBuffer(attribBuffer); -} - -bool MeshDefinition::IsQuad() const -{ - return CaseInsensitiveStringCompare(QUAD, mUri); -} - -bool MeshDefinition::IsSkinned() const -{ - return mJoints0.IsDefined() && mWeights0.IsDefined(); -} - -bool MeshDefinition::HasBlendShapes() const -{ - return !mBlendShapes.empty(); -} - -void MeshDefinition::RequestNormals() -{ - mNormals.mBlob.mLength = mPositions.mBlob.GetBufferSize(); -} - -void MeshDefinition::RequestTangents() -{ - mTangents.mBlob.mLength = mNormals.mBlob.GetBufferSize(); -} - -MeshDefinition::RawData -MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& buffers) -{ - RawData raw; - if(IsQuad()) - { - return raw; - } - - std::string meshPath; - meshPath = modelsPath + mUri; - std::fstream fileStream; - if(!mUri.empty()) - { - fileStream.open(meshPath, std::ios::in | std::ios::binary); - if(!fileStream.is_open()) + else if(MaskMatch(positionsInput.flags, MeshDefinition::Flags::S16_POSITION) || MaskMatch(positionsInput.flags, MeshDefinition::Flags::U16_POSITION)) { - DALI_LOG_ERROR("Fail to open buffer from %s.\n", meshPath.c_str()); - } - } - - if(mIndices.IsDefined()) - { - if(MaskMatch(mFlags, U32_INDICES)) - { - DALI_ASSERT_ALWAYS(((mIndices.mBlob.mLength % sizeof(uint32_t) == 0) || - mIndices.mBlob.mStride >= sizeof(uint32_t)) && - "Index buffer length not a multiple of element size"); - const auto indexCount = mIndices.mBlob.GetBufferSize() / sizeof(uint32_t); - raw.mIndices.resize(indexCount * 2); // NOTE: we need space for uint32_ts initially. - - std::string path; - auto& stream = GetAvailableData(fileStream, meshPath, buffers[mIndices.mBufferIdx], path); - if(!ReadAccessor(mIndices, stream, reinterpret_cast(raw.mIndices.data()))) - { - ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indices from '" << path << "'."; - } - - auto u16s = raw.mIndices.data(); - auto u32s = reinterpret_cast(raw.mIndices.data()); - auto end = u32s + indexCount; - while(u32s != end) - { - *u16s = static_cast(*u32s); - ++u16s; - ++u32s; - } - - raw.mIndices.resize(indexCount); - } - else if(MaskMatch(mFlags, U8_INDICES)) - { - DALI_ASSERT_ALWAYS(((mIndices.mBlob.mLength % sizeof(uint8_t) == 0) || - mIndices.mBlob.mStride >= sizeof(uint8_t)) && - "Index buffer length not a multiple of element size"); - const auto indexCount = mIndices.mBlob.GetBufferSize() / sizeof(uint8_t); - raw.mIndices.resize(indexCount); // NOTE: we need space for uint32_ts initially. - - std::string path; - auto u8s = reinterpret_cast(raw.mIndices.data()) + indexCount; - auto& stream = GetAvailableData(fileStream, meshPath, buffers[mIndices.mBufferIdx], path); - if(!ReadAccessor(mIndices, stream, u8s)) - { - ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indices from '" << path << "'."; - } - - auto u16s = raw.mIndices.data(); - auto end = u8s + indexCount; - while(u8s != end) - { - *u16s = static_cast(*u8s); - ++u16s; - ++u8s; - } + DALI_ASSERT_ALWAYS(((positionsInput.accessor.mBlob.mLength % (sizeof(uint16_t) * 3) == 0) || + positionsInput.accessor.mBlob.mStride >= (sizeof(uint16_t) * 3)) && + "Position buffer length not a multiple of element size"); + numVector3 = static_cast(bufferSize / (sizeof(uint16_t) * 3)); } else { - DALI_ASSERT_ALWAYS(((mIndices.mBlob.mLength % sizeof(unsigned short) == 0) || - mIndices.mBlob.mStride >= sizeof(unsigned short)) && - "Index buffer length not a multiple of element size"); - raw.mIndices.resize(mIndices.mBlob.mLength / sizeof(unsigned short)); - - std::string path; - auto& stream = GetAvailableData(fileStream, meshPath, buffers[mIndices.mBufferIdx], path); - if(!ReadAccessor(mIndices, stream, reinterpret_cast(raw.mIndices.data()))) - { - ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indices from '" << path << "'."; - } + DALI_ASSERT_ALWAYS(((positionsInput.accessor.mBlob.mLength % sizeof(Vector3) == 0) || + positionsInput.accessor.mBlob.mStride >= sizeof(Vector3)) && + "Position buffer length not a multiple of element size"); + numVector3 = static_cast(bufferSize / sizeof(Vector3)); } - } - std::vector positions; - if(mPositions.IsDefined()) - { - DALI_ASSERT_ALWAYS(((mPositions.mBlob.mLength % sizeof(Vector3) == 0) || - mPositions.mBlob.mStride >= sizeof(Vector3)) && - "Position buffer length not a multiple of element size"); - const auto bufferSize = mPositions.mBlob.GetBufferSize(); std::vector buffer(bufferSize); std::string path; - auto& stream = GetAvailableData(fileStream, meshPath, buffers[mPositions.mBufferIdx], path); - if(!ReadAccessor(mPositions, stream, buffer.data())) + auto& stream = GetAvailableData(positionsInput.fileStream, positionsInput.meshPath, positionsInput.buffers[positionsInput.accessor.mBufferIdx], path); + if(!ReadAccessor(positionsInput.accessor, stream, buffer.data())) { ExceptionFlinger(ASSERT_LOCATION) << "Failed to read positions from '" << path << "'."; } - uint32_t numVector3 = static_cast(bufferSize / sizeof(Vector3)); - if(mPositions.mBlob.mMin.size() != 3u || mPositions.mBlob.mMax.size() != 3u) + GetDequantizedData(buffer, 3u, numVector3, positionsInput.flags & MeshDefinition::FlagMasks::POSITIONS_MASK, positionsInput.accessor.mNormalized); + + if(positionsInput.accessor.mNormalized) { - mPositions.mBlob.ComputeMinMax(3u, numVector3, reinterpret_cast(buffer.data())); + GetDequantizedMinMax(positionsInput.accessor.mBlob.mMin, positionsInput.accessor.mBlob.mMax, positionsInput.flags & MeshDefinition::FlagMasks::POSITIONS_MASK); + } + + if(positionsInput.accessor.mBlob.mMin.size() != 3u || positionsInput.accessor.mBlob.mMax.size() != 3u) + { + positionsInput.accessor.mBlob.ComputeMinMax(3u, numVector3, reinterpret_cast(buffer.data())); } else { - mPositions.mBlob.ApplyMinMax(numVector3, reinterpret_cast(buffer.data())); + positionsInput.accessor.mBlob.ApplyMinMax(numVector3, reinterpret_cast(buffer.data())); } - if(HasBlendShapes()) + if(hasBlendShape) { positions.resize(numVector3); std::copy(buffer.data(), buffer.data() + buffer.size(), reinterpret_cast(positions.data())); } - raw.mAttribs.push_back({"aPosition", Property::VECTOR3, numVector3, std::move(buffer)}); + positionsInput.rawData.mAttribs.push_back({"aPosition", Property::VECTOR3, numVector3, std::move(buffer)}); } + return numVector3; +} - const auto isTriangles = mPrimitiveType == Geometry::TRIANGLES; - auto hasNormals = mNormals.IsDefined(); +bool LoadNormals(LoadAccessorInputs normalsInput, bool isTriangles, uint32_t positionBufferSize) +{ + auto hasNormals = normalsInput.accessor.IsDefined(); if(hasNormals) { - DALI_ASSERT_ALWAYS(((mNormals.mBlob.mLength % sizeof(Vector3) == 0) || - mNormals.mBlob.mStride >= sizeof(Vector3)) && - "Normal buffer length not a multiple of element size"); - const auto bufferSize = mNormals.mBlob.GetBufferSize(); + const auto bufferSize = normalsInput.accessor.mBlob.GetBufferSize(); + uint32_t numVector3; + + if(MaskMatch(normalsInput.flags, MeshDefinition::Flags::S8_NORMAL)) + { + DALI_ASSERT_ALWAYS(((normalsInput.accessor.mBlob.mLength % (sizeof(int8_t) * 3) == 0) || + normalsInput.accessor.mBlob.mStride >= (sizeof(int8_t) * 3)) && + "Normal buffer length not a multiple of element size"); + numVector3 = static_cast(bufferSize / (sizeof(int8_t) * 3)); + } + else if(MaskMatch(normalsInput.flags, MeshDefinition::Flags::S16_NORMAL)) + { + DALI_ASSERT_ALWAYS(((normalsInput.accessor.mBlob.mLength % (sizeof(int16_t) * 3) == 0) || + normalsInput.accessor.mBlob.mStride >= (sizeof(int16_t) * 3)) && + "Normal buffer length not a multiple of element size"); + numVector3 = static_cast(bufferSize / (sizeof(int16_t) * 3)); + } + else + { + DALI_ASSERT_ALWAYS(((normalsInput.accessor.mBlob.mLength % sizeof(Vector3) == 0) || + normalsInput.accessor.mBlob.mStride >= sizeof(Vector3)) && + "Normal buffer length not a multiple of element size"); + numVector3 = static_cast(bufferSize / sizeof(Vector3)); + } + std::vector buffer(bufferSize); std::string path; - auto& stream = GetAvailableData(fileStream, meshPath, buffers[mNormals.mBufferIdx], path); - if(!ReadAccessor(mNormals, stream, buffer.data())) + auto& stream = GetAvailableData(normalsInput.fileStream, normalsInput.meshPath, normalsInput.buffers[normalsInput.accessor.mBufferIdx], path); + if(!ReadAccessor(normalsInput.accessor, stream, buffer.data())) { ExceptionFlinger(ASSERT_LOCATION) << "Failed to read normals from '" << path << "'."; } - mNormals.mBlob.ApplyMinMax(static_cast(bufferSize / sizeof(Vector3)), reinterpret_cast(buffer.data())); + GetDequantizedData(buffer, 3u, numVector3, normalsInput.flags & MeshDefinition::FlagMasks::NORMALS_MASK, normalsInput.accessor.mNormalized); + + if(normalsInput.accessor.mNormalized) + { + GetDequantizedMinMax(normalsInput.accessor.mBlob.mMin, normalsInput.accessor.mBlob.mMax, normalsInput.flags & MeshDefinition::FlagMasks::NORMALS_MASK); + } + + normalsInput.accessor.mBlob.ApplyMinMax(numVector3, reinterpret_cast(buffer.data())); - raw.mAttribs.push_back({"aNormal", Property::VECTOR3, static_cast(bufferSize / sizeof(Vector3)), std::move(buffer)}); + normalsInput.rawData.mAttribs.push_back({"aNormal", Property::VECTOR3, numVector3, std::move(buffer)}); } - else if(mNormals.mBlob.mLength != 0 && isTriangles) + else if(normalsInput.accessor.mBlob.mLength != 0 && isTriangles) { - DALI_ASSERT_DEBUG(mNormals.mBlob.mLength == mPositions.mBlob.GetBufferSize()); - GenerateNormals(raw); - hasNormals = true; + DALI_ASSERT_DEBUG(normalsInput.accessor.mBlob.mLength == positionBufferSize); + static const std::function GenerateNormalsFunction[2] = + { + GenerateNormals, + GenerateNormals, + }; + const bool generateSuccessed = GenerateNormalsFunction[MaskMatch(normalsInput.flags, MeshDefinition::Flags::U32_INDICES)](normalsInput.rawData); + if(!generateSuccessed) + { + DALI_LOG_ERROR("Failed to generate normal\n"); + } + else + { + hasNormals = true; + } } + return hasNormals; +} - const auto hasUvs = mTexCoords.IsDefined(); - if(hasUvs) +void LoadTextureCoordinates(LoadAccessorListInputs textureCoordinatesInput) +{ + if(!textureCoordinatesInput.accessors.empty() && textureCoordinatesInput.accessors[0].IsDefined()) { - DALI_ASSERT_ALWAYS(((mTexCoords.mBlob.mLength % sizeof(Vector2) == 0) || - mTexCoords.mBlob.mStride >= sizeof(Vector2)) && - "Normal buffer length not a multiple of element size"); - const auto bufferSize = mTexCoords.mBlob.GetBufferSize(); + auto& texCoords = textureCoordinatesInput.accessors[0]; + const auto bufferSize = texCoords.mBlob.GetBufferSize(); + uint32_t uvCount; + + if(MaskMatch(textureCoordinatesInput.flags, MeshDefinition::Flags::S8_TEXCOORD) || MaskMatch(textureCoordinatesInput.flags, MeshDefinition::Flags::U8_TEXCOORD)) + { + DALI_ASSERT_ALWAYS(((texCoords.mBlob.mLength % (sizeof(uint8_t) * 2) == 0) || + texCoords.mBlob.mStride >= (sizeof(uint8_t) * 2)) && + "TexCoords buffer length not a multiple of element size"); + uvCount = static_cast(bufferSize / (sizeof(uint8_t) * 2)); + } + else if(MaskMatch(textureCoordinatesInput.flags, MeshDefinition::Flags::S16_TEXCOORD) || MaskMatch(textureCoordinatesInput.flags, MeshDefinition::Flags::U16_TEXCOORD)) + { + DALI_ASSERT_ALWAYS(((texCoords.mBlob.mLength % (sizeof(uint16_t) * 2) == 0) || + texCoords.mBlob.mStride >= (sizeof(uint16_t) * 2)) && + "TexCoords buffer length not a multiple of element size"); + uvCount = static_cast(bufferSize / (sizeof(uint16_t) * 2)); + } + else + { + DALI_ASSERT_ALWAYS(((texCoords.mBlob.mLength % sizeof(Vector2) == 0) || + texCoords.mBlob.mStride >= sizeof(Vector2)) && + "TexCoords buffer length not a multiple of element size"); + uvCount = static_cast(bufferSize / sizeof(Vector2)); + } + std::vector buffer(bufferSize); std::string path; - auto& stream = GetAvailableData(fileStream, meshPath, buffers[mTexCoords.mBufferIdx], path); - if(!ReadAccessor(mTexCoords, stream, buffer.data())) + auto& stream = GetAvailableData(textureCoordinatesInput.fileStream, textureCoordinatesInput.meshPath, textureCoordinatesInput.buffers[texCoords.mBufferIdx], path); + if(!ReadAccessor(texCoords, stream, buffer.data())) { ExceptionFlinger(ASSERT_LOCATION) << "Failed to read uv-s from '" << path << "'."; } - const auto uvCount = bufferSize / sizeof(Vector2); - if(MaskMatch(mFlags, FLIP_UVS_VERTICAL)) + GetDequantizedData(buffer, 2u, uvCount, textureCoordinatesInput.flags & MeshDefinition::FlagMasks::TEXCOORDS_MASK, texCoords.mNormalized); + + if(MaskMatch(textureCoordinatesInput.flags, MeshDefinition::Flags::FLIP_UVS_VERTICAL)) { auto uv = reinterpret_cast(buffer.data()); auto uvEnd = uv + uvCount; @@ -806,148 +1089,208 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& } } - mTexCoords.mBlob.ApplyMinMax(static_cast(bufferSize / sizeof(Vector2)), reinterpret_cast(buffer.data())); + if(texCoords.mNormalized) + { + GetDequantizedMinMax(texCoords.mBlob.mMin, texCoords.mBlob.mMax, textureCoordinatesInput.flags & MeshDefinition::FlagMasks::TEXCOORDS_MASK); + } - raw.mAttribs.push_back({"aTexCoord", Property::VECTOR2, static_cast(uvCount), std::move(buffer)}); + texCoords.mBlob.ApplyMinMax(static_cast(uvCount), reinterpret_cast(buffer.data())); + textureCoordinatesInput.rawData.mAttribs.push_back({"aTexCoord", Property::VECTOR2, static_cast(uvCount), std::move(buffer)}); } +} - if(mTangents.IsDefined()) +void LoadTangents(LoadAccessorInputs tangentsInput, bool hasNormals, bool hasUvs, bool isTriangles, Property::Type tangentType, uint32_t normalBufferSize) +{ + if(tangentsInput.accessor.IsDefined()) { - uint32_t propertySize = static_cast((mTangentType == Property::VECTOR4) ? sizeof(Vector4) : sizeof(Vector3)); - DALI_ASSERT_ALWAYS(((mTangents.mBlob.mLength % propertySize == 0) || - mTangents.mBlob.mStride >= propertySize) && - "Tangents buffer length not a multiple of element size"); - const auto bufferSize = mTangents.mBlob.GetBufferSize(); + const auto bufferSize = tangentsInput.accessor.mBlob.GetBufferSize(); + + uint32_t propertySize = static_cast((tangentType == Property::VECTOR4) ? sizeof(Vector4) : sizeof(Vector3)); + uint32_t componentCount = static_cast(propertySize / sizeof(float)); + + uint32_t numTangents; + + if(MaskMatch(tangentsInput.flags, MeshDefinition::Flags::S8_TANGENT)) + { + DALI_ASSERT_ALWAYS(((tangentsInput.accessor.mBlob.mLength % (sizeof(int8_t) * componentCount) == 0) || + tangentsInput.accessor.mBlob.mStride >= (sizeof(int8_t) * componentCount)) && + "Tangents buffer length not a multiple of element size"); + numTangents = static_cast(bufferSize / (sizeof(int8_t) * componentCount)); + } + else if(MaskMatch(tangentsInput.flags, MeshDefinition::Flags::S16_TANGENT)) + { + DALI_ASSERT_ALWAYS(((tangentsInput.accessor.mBlob.mLength % (sizeof(int16_t) * componentCount) == 0) || + tangentsInput.accessor.mBlob.mStride >= (sizeof(int16_t) * componentCount)) && + "Tangents buffer length not a multiple of element size"); + numTangents = static_cast(bufferSize / (sizeof(int16_t) * componentCount)); + } + else + { + DALI_ASSERT_ALWAYS(((tangentsInput.accessor.mBlob.mLength % propertySize == 0) || + tangentsInput.accessor.mBlob.mStride >= propertySize) && + "Tangents buffer length not a multiple of element size"); + numTangents = static_cast(bufferSize / propertySize); + } + std::vector buffer(bufferSize); std::string path; - auto& stream = GetAvailableData(fileStream, meshPath, buffers[mTangents.mBufferIdx], path); - if(!ReadAccessor(mTangents, stream, buffer.data())) + auto& stream = GetAvailableData(tangentsInput.fileStream, tangentsInput.meshPath, tangentsInput.buffers[tangentsInput.accessor.mBufferIdx], path); + if(!ReadAccessor(tangentsInput.accessor, stream, buffer.data())) { ExceptionFlinger(ASSERT_LOCATION) << "Failed to read tangents from '" << path << "'."; } - mTangents.mBlob.ApplyMinMax(bufferSize / propertySize, reinterpret_cast(buffer.data())); - raw.mAttribs.push_back({"aTangent", mTangentType, static_cast(bufferSize / propertySize), std::move(buffer)}); + GetDequantizedData(buffer, componentCount, numTangents, tangentsInput.flags & MeshDefinition::FlagMasks::TANGENTS_MASK, tangentsInput.accessor.mNormalized); + + if(tangentsInput.accessor.mNormalized) + { + GetDequantizedMinMax(tangentsInput.accessor.mBlob.mMin, tangentsInput.accessor.mBlob.mMax, tangentsInput.flags & MeshDefinition::FlagMasks::TANGENTS_MASK); + } + + tangentsInput.accessor.mBlob.ApplyMinMax(numTangents, reinterpret_cast(buffer.data())); + + tangentsInput.rawData.mAttribs.push_back({"aTangent", tangentType, static_cast(numTangents), std::move(buffer)}); } - else if(mTangents.mBlob.mLength != 0 && hasNormals && isTriangles) + else if(tangentsInput.accessor.mBlob.mLength != 0 && hasNormals && isTriangles) { - DALI_ASSERT_DEBUG(mTangents.mBlob.mLength == mNormals.mBlob.GetBufferSize()); - hasUvs ? GenerateTangentsWithUvs(raw) : GenerateTangents(raw); + DALI_ASSERT_DEBUG(tangentsInput.accessor.mBlob.mLength == normalBufferSize); + static const std::function GenerateTangentsFunction[2][2][2] = + { + { + { + GenerateTangents, + GenerateTangents, + }, + { + GenerateTangents, + GenerateTangents, + }, + }, + { + { + GenerateTangents, + GenerateTangents, + }, + { + GenerateTangents, + GenerateTangents, + }, + }}; + const bool generateSuccessed = GenerateTangentsFunction[MaskMatch(tangentsInput.flags, MeshDefinition::Flags::U32_INDICES)][tangentType == Property::VECTOR3][hasUvs](tangentsInput.rawData); + if(!generateSuccessed) + { + DALI_LOG_ERROR("Failed to generate tangents\n"); + } } +} - if(mColors.IsDefined()) +void LoadColors(LoadAccessorListInputs colorsInput) +{ + // Only support 1 vertex color + if(!colorsInput.accessors.empty() && colorsInput.accessors[0].IsDefined()) { - uint32_t propertySize = mColors.mBlob.mElementSizeHint; + uint32_t propertySize = colorsInput.accessors[0].mBlob.mElementSizeHint; Property::Type propertyType = (propertySize == sizeof(Vector4)) ? Property::VECTOR4 : ((propertySize == sizeof(Vector3)) ? Property::VECTOR3 : Property::NONE); if(propertyType != Property::NONE) { - DALI_ASSERT_ALWAYS(((mColors.mBlob.mLength % propertySize == 0) || - mColors.mBlob.mStride >= propertySize) && + DALI_ASSERT_ALWAYS(((colorsInput.accessors[0].mBlob.mLength % propertySize == 0) || + colorsInput.accessors[0].mBlob.mStride >= propertySize) && "Colors buffer length not a multiple of element size"); - const auto bufferSize = mColors.mBlob.GetBufferSize(); + const auto bufferSize = colorsInput.accessors[0].mBlob.GetBufferSize(); std::vector buffer(bufferSize); std::string path; - auto& stream = GetAvailableData(fileStream, meshPath, buffers[mColors.mBufferIdx], path); - if(!ReadAccessor(mColors, stream, buffer.data())) + auto& stream = GetAvailableData(colorsInput.fileStream, colorsInput.meshPath, colorsInput.buffers[colorsInput.accessors[0].mBufferIdx], path); + if(!ReadAccessor(colorsInput.accessors[0], stream, buffer.data())) { ExceptionFlinger(ASSERT_LOCATION) << "Failed to read colors from '" << path << "'."; } - mColors.mBlob.ApplyMinMax(bufferSize / propertySize, reinterpret_cast(buffer.data())); + colorsInput.accessors[0].mBlob.ApplyMinMax(bufferSize / propertySize, reinterpret_cast(buffer.data())); - raw.mAttribs.push_back({"aVertexColor", propertyType, static_cast(bufferSize / propertySize), std::move(buffer)}); + colorsInput.rawData.mAttribs.push_back({"aVertexColor", propertyType, static_cast(bufferSize / propertySize), std::move(buffer)}); } } - - if(IsSkinned()) + else { - std::string pathJoint; - auto& streamJoint = GetAvailableData(fileStream, meshPath, buffers[mJoints0.mBufferIdx], pathJoint); - if(MaskMatch(mFlags, U16_JOINT_IDS)) - { - ReadJointAccessor(raw, mJoints0, streamJoint, pathJoint); - } - else if(MaskMatch(mFlags, U8_JOINT_IDS)) - { - ReadJointAccessor(raw, mJoints0, streamJoint, pathJoint); - } - else - { - ReadJointAccessor(raw, mJoints0, streamJoint, pathJoint); - } - - DALI_ASSERT_ALWAYS(((mWeights0.mBlob.mLength % sizeof(Vector4) == 0) || - mWeights0.mBlob.mStride >= sizeof(Vector4)) && - "Weights buffer length not a multiple of element size"); - const auto bufferSize = mWeights0.mBlob.GetBufferSize(); - std::vector buffer(bufferSize); + std::vector buffer(colorsInput.rawData.mAttribs[0].mNumElements * sizeof(Vector4)); + auto colors = reinterpret_cast(buffer.data()); - std::string pathWeight; - auto& streamWeight = GetAvailableData(fileStream, meshPath, buffers[mWeights0.mBufferIdx], pathWeight); - if(!ReadAccessor(mWeights0, streamWeight, buffer.data())) + for(uint32_t i = 0; i < colorsInput.rawData.mAttribs[0].mNumElements; i++) { - ExceptionFlinger(ASSERT_LOCATION) << "Failed to read weights from '" << pathWeight << "'."; + colors[i] = Vector4::ONE; } - raw.mAttribs.push_back({"aWeights", Property::VECTOR4, static_cast(bufferSize / sizeof(Vector4)), std::move(buffer)}); + colorsInput.rawData.mAttribs.push_back({"aVertexColor", Property::VECTOR4, colorsInput.rawData.mAttribs[0].mNumElements, std::move(buffer)}); } +} +void LoadBlendShapes(MeshDefinition::RawData& rawData, std::vector& blendShapes, MeshDefinition::Blob& blendShapeHeader, BlendShapes::Version blendShapeVersion, uint32_t numberOfVertices, std::fstream& fileStream, BufferDefinition::Vector& buffers) +{ // Calculate the Blob for the blend shapes. - Blob blendShapesBlob; + MeshDefinition::Blob blendShapesBlob; blendShapesBlob.mOffset = std::numeric_limits::max(); blendShapesBlob.mLength = 0u; - for(const auto& blendShape : mBlendShapes) - { - for(auto i : {&blendShape.deltas, &blendShape.normals, &blendShape.tangents}) + uint32_t totalTextureSize(0u); + + auto processAccessor = [&](const MeshDefinition::Accessor& accessor, uint32_t vector3Size) { + if(accessor.IsDefined()) { - if(i->IsDefined()) - { - blendShapesBlob.mOffset = std::min(blendShapesBlob.mOffset, i->mBlob.mOffset); - blendShapesBlob.mLength += i->mBlob.mLength; - } + blendShapesBlob.mOffset = std::min(blendShapesBlob.mOffset, accessor.mBlob.mOffset); + blendShapesBlob.mLength += accessor.mBlob.mLength; + + totalTextureSize += accessor.mBlob.mLength / vector3Size; } - } + }; - if(HasBlendShapes()) + for(const auto& blendShape : blendShapes) { - const uint32_t numberOfVertices = static_cast(mPositions.mBlob.mLength / sizeof(Vector3)); + const auto positionMask = blendShape.mFlags & MeshDefinition::FlagMasks::POSITIONS_MASK; + const auto normalMask = blendShape.mFlags & MeshDefinition::FlagMasks::NORMALS_MASK; + const auto tangentMask = blendShape.mFlags & MeshDefinition::FlagMasks::TANGENTS_MASK; + processAccessor(blendShape.deltas, MaskMatch(positionMask, MeshDefinition::S8_POSITION) ? sizeof(uint8_t) * 3 : (MaskMatch(positionMask, MeshDefinition::S16_POSITION) ? sizeof(uint16_t) * 3 : sizeof(Vector3))); + processAccessor(blendShape.normals, MaskMatch(normalMask, MeshDefinition::S8_NORMAL) ? sizeof(uint8_t) * 3 : (MaskMatch(normalMask, MeshDefinition::S16_NORMAL) ? sizeof(uint16_t) * 3 : sizeof(Vector3))); + processAccessor(blendShape.tangents, MaskMatch(tangentMask, MeshDefinition::S8_TANGENT) ? sizeof(uint8_t) * 3 : (MaskMatch(tangentMask, MeshDefinition::S16_TANGENT) ? sizeof(uint16_t) * 3 : sizeof(Vector3))); + } + + if(!blendShapes.empty()) + { // Calculate the size of one buffer inside the texture. - raw.mBlendShapeBufferOffset = numberOfVertices; + rawData.mBlendShapeBufferOffset = numberOfVertices; bool calculateGltf2BlendShapes = false; uint32_t textureWidth = 0u; uint32_t textureHeight = 0u; - if(!mBlendShapeHeader.IsDefined()) + if(!blendShapeHeader.IsDefined()) { - CalculateTextureSize(static_cast(blendShapesBlob.mLength / sizeof(Vector3)), textureWidth, textureHeight); + CalculateTextureSize(totalTextureSize, textureWidth, textureHeight); calculateGltf2BlendShapes = true; } else { uint16_t header[2u]; - ReadBlob(mBlendShapeHeader, fileStream, reinterpret_cast(header)); + ReadBlob(blendShapeHeader, fileStream, reinterpret_cast(header)); textureWidth = header[0u]; textureHeight = header[1u]; } - const uint32_t numberOfBlendShapes = mBlendShapes.size(); - raw.mBlendShapeUnnormalizeFactor.Resize(numberOfBlendShapes); + const uint32_t numberOfBlendShapes = blendShapes.size(); + rawData.mBlendShapeUnnormalizeFactor.Resize(numberOfBlendShapes); Devel::PixelBuffer geometryPixelBuffer = Devel::PixelBuffer::New(textureWidth, textureHeight, Pixel::RGB32F); uint8_t* geometryBuffer = geometryPixelBuffer.GetBuffer(); if(calculateGltf2BlendShapes) { - CalculateGltf2BlendShapes(geometryBuffer, mBlendShapes, numberOfVertices, raw.mBlendShapeUnnormalizeFactor[0u], buffers); + CalculateGltf2BlendShapes(geometryBuffer, blendShapes, numberOfVertices, rawData.mBlendShapeUnnormalizeFactor[0u], buffers); } else { - Blob unnormalizeFactorBlob; - unnormalizeFactorBlob.mLength = static_cast(sizeof(float) * ((BlendShapes::Version::VERSION_2_0 == mBlendShapeVersion) ? 1u : numberOfBlendShapes)); + MeshDefinition::Blob unnormalizeFactorBlob; + unnormalizeFactorBlob.mLength = static_cast(sizeof(float) * ((BlendShapes::Version::VERSION_2_0 == blendShapeVersion) ? 1u : numberOfBlendShapes)); if(blendShapesBlob.IsDefined()) { @@ -960,12 +1303,225 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& // Read the unnormalize factors. if(unnormalizeFactorBlob.IsDefined()) { - ReadBlob(unnormalizeFactorBlob, fileStream, reinterpret_cast(&raw.mBlendShapeUnnormalizeFactor[0u])); + ReadBlob(unnormalizeFactorBlob, fileStream, reinterpret_cast(&rawData.mBlendShapeUnnormalizeFactor[0u])); } } - raw.mBlendShapeData = Devel::PixelBuffer::Convert(geometryPixelBuffer); + rawData.mBlendShapeData = Devel::PixelBuffer::Convert(geometryPixelBuffer); + } +} + +constexpr uint32_t MINIMUM_SHADER_VERSION_SUPPORT_VERTEX_ID = 300; + +} // namespace + +MeshDefinition::SparseBlob::SparseBlob(const Blob& indices, const Blob& values, uint32_t count) +: mIndices{indices}, + mValues{values}, + mCount{count} +{ +} + +MeshDefinition::SparseBlob::SparseBlob(Blob&& indices, Blob&& values, uint32_t count) +: mIndices(std::move(indices)), + mValues(std::move(values)), + mCount{count} +{ +} + +MeshDefinition::Accessor::Accessor(const MeshDefinition::Blob& blob, + const MeshDefinition::SparseBlob& sparse, + Index bufferIndex, + bool normalized) +: mBlob{blob}, + mSparse{(sparse.mIndices.IsDefined() && sparse.mValues.IsDefined()) ? new SparseBlob{sparse} : nullptr}, + mBufferIdx(bufferIndex), + mNormalized(normalized) +{ +} + +MeshDefinition::Accessor::Accessor(MeshDefinition::Blob&& blob, + MeshDefinition::SparseBlob&& sparse, + Index bufferIndex, + bool normalized) +: mBlob{std::move(blob)}, + mSparse{(sparse.mIndices.IsDefined() && sparse.mValues.IsDefined()) ? new SparseBlob{std::move(sparse)} : nullptr}, + mBufferIdx(bufferIndex), + mNormalized(normalized) +{ +} + +void MeshDefinition::Blob::ComputeMinMax(std::vector& min, std::vector& max, uint32_t numComponents, uint32_t count, const float* values) +{ + min.assign(numComponents, MAXFLOAT); + max.assign(numComponents, -MAXFLOAT); + for(uint32_t i = 0; i < count; ++i) + { + for(uint32_t j = 0; j < numComponents; ++j) + { + min[j] = std::min(min[j], *values); + max[j] = std::max(max[j], *values); + values++; + } + } +} + +void MeshDefinition::Blob::ApplyMinMax(const std::vector& min, const std::vector& max, uint32_t count, float* values, std::vector* sparseIndices) +{ + DALI_ASSERT_DEBUG(max.size() == min.size() || max.size() * min.size() == 0); + const auto numComponents = std::max(min.size(), max.size()); + + using ClampFn = void (*)(const float*, const float*, uint32_t, float&); + ClampFn clampFn = min.empty() ? (max.empty() ? static_cast(nullptr) : [](const float* min, const float* max, uint32_t i, float& value) { value = std::min(max[i], value); }) + : (max.empty() ? [](const float* min, const float* max, uint32_t i, float& value) { value = std::max(min[i], value); } + : static_cast([](const float* min, const float* max, uint32_t i, float& value) { value = std::min(std::max(min[i], value), max[i]); })); + + if(!clampFn) + { + return; + } + + auto end = values + count * numComponents; + while(values != end) + { + auto nextElement = values + numComponents; + uint32_t i = 0; + while(values != nextElement) + { + clampFn(min.data(), max.data(), i, *values); + ++values; + ++i; + } + } +} + +MeshDefinition::Blob::Blob(uint32_t offset, uint32_t length, uint16_t stride, uint16_t elementSizeHint, const std::vector& min, const std::vector& max) +: mOffset(offset), + mLength(length), + mStride(stride), + mElementSizeHint(elementSizeHint), + mMin(min), + mMax(max) +{ +} + +uint32_t MeshDefinition::Blob::GetBufferSize() const +{ + return mLength; +} + +void MeshDefinition::Blob::ComputeMinMax(uint32_t numComponents, uint32_t count, float* values) +{ + ComputeMinMax(mMin, mMax, numComponents, count, values); +} + +void MeshDefinition::Blob::ApplyMinMax(uint32_t count, float* values, std::vector* sparseIndices) const +{ + ApplyMinMax(mMin, mMax, count, values, sparseIndices); +} + +void MeshDefinition::RawData::Attrib::AttachBuffer(Geometry& g) const +{ + Property::Map attribMap; + attribMap[mName] = mType; + VertexBuffer attribBuffer = VertexBuffer::New(attribMap); + attribBuffer.SetData(mData.data(), mNumElements); + + g.AddVertexBuffer(attribBuffer); +} + +bool MeshDefinition::IsQuad() const +{ + return CaseInsensitiveStringCompare(QUAD, mUri); +} + +bool MeshDefinition::IsSkinned() const +{ + return !mJoints.empty() && !mWeights.empty(); +} + +bool MeshDefinition::HasVertexColor() const +{ + return !mColors.empty(); +} + +uint32_t MeshDefinition::GetNumberOfJointSets() const +{ + uint32_t number = static_cast(mJoints.size()); + if(number > MeshDefinition::MAX_NUMBER_OF_JOINT_SETS) + { + number = MeshDefinition::MAX_NUMBER_OF_JOINT_SETS; + } + return number; +} + +bool MeshDefinition::HasBlendShapes() const +{ + return !mBlendShapes.empty(); +} + +void MeshDefinition::RequestNormals() +{ + mNormals.mBlob.mLength = mPositions.mBlob.GetBufferSize(); +} + +void MeshDefinition::RequestTangents() +{ + mTangents.mBlob.mLength = mNormals.mBlob.GetBufferSize(); +} + +MeshDefinition::RawData +MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& buffers) +{ + RawData raw; + if(IsQuad()) + { + return raw; + } + + std::string meshPath; + meshPath = modelsPath + mUri; + std::fstream fileStream; + if(!mUri.empty()) + { + fileStream.open(meshPath, std::ios::in | std::ios::binary); + if(!fileStream.is_open()) + { + DALI_LOG_ERROR("Fail to open buffer from %s.\n", meshPath.c_str()); + } + } + + LoadAccessorInputs indicesInput = {raw, mIndices, mFlags, fileStream, meshPath, buffers}; + LoadIndices(indicesInput); + + LoadAccessorInputs positionsInput = {raw, mPositions, mFlags, fileStream, meshPath, buffers}; + uint32_t numberOfVertices = LoadPositions(positionsInput, HasBlendShapes()); + + const auto isTriangles = mPrimitiveType == Geometry::TRIANGLES; + LoadAccessorInputs normalsInput = {raw, mNormals, mFlags, fileStream, meshPath, buffers}; + auto hasNormals = LoadNormals(normalsInput, isTriangles, mPositions.mBlob.GetBufferSize()); + + LoadAccessorListInputs textureCoordinatesInput = {raw, mTexCoords, mFlags, fileStream, meshPath, buffers}; + LoadTextureCoordinates(textureCoordinatesInput); + + const bool hasUvs = !mTexCoords.empty() && mTexCoords[0].IsDefined(); + LoadAccessorInputs tangentsInput = {raw, mTangents, mFlags, fileStream, meshPath, buffers}; + LoadTangents(tangentsInput, hasNormals, hasUvs, isTriangles, mTangentType, mNormals.mBlob.GetBufferSize()); + + LoadAccessorListInputs colorsInput = {raw, mColors, mFlags, fileStream, meshPath, buffers}; + LoadColors(colorsInput); + + if(IsSkinned()) + { + LoadDataType loadDataType = (MaskMatch(mFlags, MeshDefinition::U16_JOINT_IDS)) ? LoadDataType::UNSIGNED_SHORT : (MaskMatch(mFlags, MeshDefinition::U8_JOINT_IDS) ? LoadDataType::UNSIGNED_BYTE : LoadDataType::FLOAT); + LoadAccessorListInputs jointsInput = {raw, mJoints, mFlags, fileStream, meshPath, buffers}; + ReadTypedVectorAccessors(jointsInput, loadDataType, "aJoints"); + + loadDataType = (MaskMatch(mFlags, MeshDefinition::U16_WEIGHT)) ? LoadDataType::UNSIGNED_SHORT : (MaskMatch(mFlags, MeshDefinition::U8_WEIGHT) ? LoadDataType::UNSIGNED_BYTE : LoadDataType::FLOAT); + LoadAccessorListInputs weightsInput = {raw, mWeights, mFlags, fileStream, meshPath, buffers}; + ReadTypedVectorAccessors(weightsInput, loadDataType, "aWeights"); } + LoadBlendShapes(raw, mBlendShapes, mBlendShapeHeader, mBlendShapeVersion, numberOfVertices, fileStream, buffers); return raw; } @@ -984,7 +1540,38 @@ MeshGeometry MeshDefinition::Load(RawData&& raw) const { if(!raw.mIndices.empty()) { - meshGeometry.geometry.SetIndexBuffer(raw.mIndices.data(), raw.mIndices.size()); + if(MaskMatch(mFlags, U32_INDICES)) + { + // TODO : We can only store indeces as uint16_type. Send Dali::Geometry that we use it as uint32_t actual. + meshGeometry.geometry.SetIndexBuffer(reinterpret_cast(raw.mIndices.data()), raw.mIndices.size() / 2); + } + else + { + meshGeometry.geometry.SetIndexBuffer(raw.mIndices.data(), raw.mIndices.size()); + } + } + + if(DALI_UNLIKELY(Dali::Shader::GetShaderLanguageVersion() < MINIMUM_SHADER_VERSION_SUPPORT_VERTEX_ID && !raw.mAttribs.empty())) + { + auto numElements = raw.mAttribs[0].mNumElements; + + // gl_VertexID not support. We should add buffer hard. + Property::Map attribMap; + attribMap["aVertexID"] = Property::FLOAT; + + VertexBuffer attribBuffer = VertexBuffer::New(attribMap); + + std::vector buffer(numElements * sizeof(float)); + auto ids = reinterpret_cast(buffer.data()); + + for(uint32_t i = 0; i < numElements; i++) + { + ids[i] = static_cast(i); + } + + attribBuffer.SetData(buffer.data(), numElements); + + meshGeometry.geometry.AddVertexBuffer(attribBuffer); } for(auto& a : raw.mAttribs) @@ -1008,6 +1595,14 @@ MeshGeometry MeshDefinition::Load(RawData&& raw) const return meshGeometry; } -} // namespace Loader -} // namespace Scene3D -} // namespace Dali +void MeshDefinition::RetrieveBlendShapeComponents(bool& hasPositions, bool& hasNormals, bool& hasTangents) const +{ + for(const auto& blendShape : mBlendShapes) + { + hasPositions = hasPositions || blendShape.deltas.IsDefined(); + hasNormals = hasNormals || blendShape.normals.IsDefined(); + hasTangents = hasTangents || blendShape.tangents.IsDefined(); + } +} + +} // namespace Dali::Scene3D::Loader