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=6b2feb6fa57b8ea844a9ba179e9ddc3407b5214b;hpb=5b4975dbe58f19abab674161884f338f549ee81d;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 6b2feb6..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. @@ -32,6 +32,33 @@ namespace Dali::Scene3D::Loader { namespace { +enum class LoadDataType +{ + 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 { @@ -74,7 +101,7 @@ const char* QUAD("quad"); 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; } @@ -95,7 +122,7 @@ bool ReadBlob(const MeshDefinition::Blob& descriptor, std::istream& source, uint { readSize += descriptor.mStride; target += descriptor.mElementSizeHint; - source.seekg(diff, std::istream::cur); + source.seekg(static_cast(static_cast(diff)), std::istream::cur); } return readSize == totalSize; } @@ -210,104 +237,59 @@ bool ReadAccessor(const MeshDefinition::Accessor& accessor, std::istream& source return ReadAccessor(accessor, source, target, nullptr); } -template -void ReadJointAccessor(MeshDefinition::RawData& raw, const MeshDefinition::Accessor& accessor, std::istream& source, const std::string& meshPath, const std::string& name) +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({name, Property::VECTOR4, static_cast(outBufferSize / sizeof(Vector4)), std::move(buffer)}); } -void ReadTypedJointAccessor(MeshDefinition::RawData& raw, uint32_t flags, MeshDefinition::Accessor& accessor, std::iostream& stream, std::string& path, const std::string& name) +template +void ReadTypedVectorAccessor(LoadDataType loadDataType, MeshDefinition::Accessor& accessor, std::iostream& stream, std::vector& buffer) { - if(MaskMatch(flags, MeshDefinition::U16_JOINT_IDS)) - { - ReadJointAccessor(raw, accessor, stream, path, name); - } - else if(MaskMatch(flags, MeshDefinition::U8_JOINT_IDS)) - { - ReadJointAccessor(raw, accessor, stream, path, name); - } - else - { - ReadJointAccessor(raw, accessor, stream, path, name); - } -} - -template -void ReadWeightAccessor(MeshDefinition::RawData& raw, const MeshDefinition::Accessor& accessor, std::istream& source, const std::string& meshPath, const std::string& name) -{ - constexpr auto sizeofBlobUnit = sizeof(T) * 4; - - DALI_ASSERT_ALWAYS(((accessor.mBlob.mLength % sizeofBlobUnit == 0) || - accessor.mBlob.mStride >= sizeofBlobUnit) && - "weights 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; - if(!ReadAccessor(accessor, source, inBuffer)) + switch(loadDataType) { - ExceptionFlinger(ASSERT_LOCATION) << "Failed to read weights from '" << meshPath << "'."; - } - - if constexpr(sizeofBlobUnit != sizeof(Vector4)) - { - auto floats = reinterpret_cast(buffer.data()); - const auto end = inBuffer + inBufferSize; - while(inBuffer != end) + case LoadDataType::UNSIGNED_SHORT: { - const auto value = *reinterpret_cast(inBuffer); - // Normalize weight value. value /= 255 for uint8_t weight, and value /= 65535 for uint16_t weight. - *floats = static_cast(value) / static_cast((1 << (sizeof(T) * 8)) - 1); - - inBuffer += sizeof(T); - ++floats; + ReadVectorAccessor(accessor, stream, buffer); + break; + } + case LoadDataType::UNSIGNED_BYTE: + { + ReadVectorAccessor(accessor, stream, buffer); + break; + } + default: + { + ReadVectorAccessor(accessor, stream, buffer); + break; } - } - raw.mAttribs.push_back({name, Property::VECTOR4, static_cast(outBufferSize / sizeof(Vector4)), std::move(buffer)}); -} - -void ReadTypedWeightAccessor(MeshDefinition::RawData& raw, uint32_t flags, MeshDefinition::Accessor& accessor, std::iostream& stream, std::string& path, std::string name) -{ - if(MaskMatch(flags, MeshDefinition::U16_WEIGHT)) - { - ReadWeightAccessor(raw, accessor, stream, path, name); - } - else if(MaskMatch(flags, MeshDefinition::U8_WEIGHT)) - { - ReadWeightAccessor(raw, accessor, stream, path, name); - } - else - { - ReadWeightAccessor(raw, accessor, stream, path, name); } } @@ -841,218 +823,58 @@ std::iostream& GetAvailableData(std::fstream& meshStream, const std::string& mes return stream; } -} // 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) +template +void ReadTypedVectorAccessors(LoadAccessorListInputs loadAccessorListInputs, LoadDataType loadDataType, std::string attributeName) { -} - -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) + int setIndex = 0; + for(auto& accessor : loadAccessorListInputs.accessors) { - number = MeshDefinition::MAX_NUMBER_OF_JOINT_SETS; + 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)}); } - 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) +void LoadIndices(LoadAccessorInputs indicesInput) { - 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()); - } - } - - if(mIndices.IsDefined()) + if(indicesInput.accessor.IsDefined()) { - if(MaskMatch(mFlags, U32_INDICES)) + if(MaskMatch(indicesInput.flags, MeshDefinition::Flags::U32_INDICES)) { - DALI_ASSERT_ALWAYS(((mIndices.mBlob.mLength % sizeof(uint32_t) == 0) || - mIndices.mBlob.mStride >= sizeof(uint32_t)) && + 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 = mIndices.mBlob.GetBufferSize() / sizeof(uint32_t); - raw.mIndices.resize(indexCount * 2); // NOTE: we need space for uint32_ts initially. + const auto indexCount = indicesInput.accessor.mBlob.GetBufferSize() / sizeof(uint32_t); + indicesInput.rawData.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()))) + 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(mFlags, U8_INDICES)) + else if(MaskMatch(indicesInput.flags, MeshDefinition::Flags::U8_INDICES)) { - DALI_ASSERT_ALWAYS(((mIndices.mBlob.mLength % sizeof(uint8_t) == 0) || - mIndices.mBlob.mStride >= sizeof(uint8_t)) && + 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 = mIndices.mBlob.GetBufferSize() / sizeof(uint8_t); - raw.mIndices.resize(indexCount); // NOTE: we need space for uint16_ts initially. + const auto indexCount = indicesInput.accessor.mBlob.GetBufferSize() / sizeof(uint8_t); + indicesInput.rawData.mIndices.resize(indexCount); // NOTE: we need space for uint16_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)) + 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 << "'."; } - auto u16s = raw.mIndices.data(); + auto u16s = indicesInput.rawData.mIndices.data(); auto end = u8s + indexCount; while(u8s != end) { @@ -1063,111 +885,113 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& } else { - DALI_ASSERT_ALWAYS(((mIndices.mBlob.mLength % sizeof(unsigned short) == 0) || - mIndices.mBlob.mStride >= sizeof(unsigned short)) && + 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"); - raw.mIndices.resize(mIndices.mBlob.mLength / sizeof(unsigned short)); + indicesInput.rawData.mIndices.resize(indicesInput.accessor.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()))) + 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 << "'."; + ExceptionFlinger(ASSERT_LOCATION) << "Failed to read indicesInput.accessor from '" << path << "'."; } } } +} - uint32_t numberOfVertices = 0u; - +uint32_t LoadPositions(LoadAccessorInputs positionsInput, bool hasBlendShape) +{ + uint32_t numVector3 = 0u; std::vector positions; - if(mPositions.IsDefined()) + if(positionsInput.accessor.IsDefined()) { - const auto bufferSize = mPositions.mBlob.GetBufferSize(); - uint32_t numVector3; + const auto bufferSize = positionsInput.accessor.mBlob.GetBufferSize(); - if(MaskMatch(mFlags, S8_POSITION) || MaskMatch(mFlags, U8_POSITION)) + if(MaskMatch(positionsInput.flags, MeshDefinition::Flags::S8_POSITION) || MaskMatch(positionsInput.flags, MeshDefinition::Flags::U8_POSITION)) { - DALI_ASSERT_ALWAYS(((mPositions.mBlob.mLength % (sizeof(uint8_t) * 3) == 0) || - mPositions.mBlob.mStride >= (sizeof(uint8_t) * 3)) && + 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)); } - else if(MaskMatch(mFlags, S16_POSITION) || MaskMatch(mFlags, U16_POSITION)) + else if(MaskMatch(positionsInput.flags, MeshDefinition::Flags::S16_POSITION) || MaskMatch(positionsInput.flags, MeshDefinition::Flags::U16_POSITION)) { - DALI_ASSERT_ALWAYS(((mPositions.mBlob.mLength % (sizeof(uint16_t) * 3) == 0) || - mPositions.mBlob.mStride >= (sizeof(uint16_t) * 3)) && + 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(((mPositions.mBlob.mLength % sizeof(Vector3) == 0) || - mPositions.mBlob.mStride >= sizeof(Vector3)) && + 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)); } - numberOfVertices = numVector3; - 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 << "'."; } - GetDequantizedData(buffer, 3u, numVector3, mFlags & POSITIONS_MASK, mPositions.mNormalized); + GetDequantizedData(buffer, 3u, numVector3, positionsInput.flags & MeshDefinition::FlagMasks::POSITIONS_MASK, positionsInput.accessor.mNormalized); - if(mPositions.mNormalized) + if(positionsInput.accessor.mNormalized) { - GetDequantizedMinMax(mPositions.mBlob.mMin, mPositions.mBlob.mMax, mFlags & POSITIONS_MASK); + GetDequantizedMinMax(positionsInput.accessor.mBlob.mMin, positionsInput.accessor.mBlob.mMax, positionsInput.flags & MeshDefinition::FlagMasks::POSITIONS_MASK); } - if(mPositions.mBlob.mMin.size() != 3u || mPositions.mBlob.mMax.size() != 3u) + if(positionsInput.accessor.mBlob.mMin.size() != 3u || positionsInput.accessor.mBlob.mMax.size() != 3u) { - mPositions.mBlob.ComputeMinMax(3u, numVector3, reinterpret_cast(buffer.data())); + 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) { - const auto bufferSize = mNormals.mBlob.GetBufferSize(); + const auto bufferSize = normalsInput.accessor.mBlob.GetBufferSize(); uint32_t numVector3; - if(MaskMatch(mFlags, S8_NORMAL)) + if(MaskMatch(normalsInput.flags, MeshDefinition::Flags::S8_NORMAL)) { - DALI_ASSERT_ALWAYS(((mNormals.mBlob.mLength % (sizeof(int8_t) * 3) == 0) || - mNormals.mBlob.mStride >= (sizeof(int8_t) * 3)) && + 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(mFlags, S16_NORMAL)) + else if(MaskMatch(normalsInput.flags, MeshDefinition::Flags::S16_NORMAL)) { - DALI_ASSERT_ALWAYS(((mNormals.mBlob.mLength % (sizeof(int16_t) * 3) == 0) || - mNormals.mBlob.mStride >= (sizeof(int16_t) * 3)) && + 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(((mNormals.mBlob.mLength % sizeof(Vector3) == 0) || - mNormals.mBlob.mStride >= sizeof(Vector3)) && + 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)); } @@ -1175,32 +999,32 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& 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 << "'."; } - GetDequantizedData(buffer, 3u, numVector3, mFlags & NORMALS_MASK, mNormals.mNormalized); + GetDequantizedData(buffer, 3u, numVector3, normalsInput.flags & MeshDefinition::FlagMasks::NORMALS_MASK, normalsInput.accessor.mNormalized); - if(mNormals.mNormalized) + if(normalsInput.accessor.mNormalized) { - GetDequantizedMinMax(mNormals.mBlob.mMin, mNormals.mBlob.mMax, mFlags & NORMALS_MASK); + GetDequantizedMinMax(normalsInput.accessor.mBlob.mMin, normalsInput.accessor.mBlob.mMax, normalsInput.flags & MeshDefinition::FlagMasks::NORMALS_MASK); } - mNormals.mBlob.ApplyMinMax(numVector3, reinterpret_cast(buffer.data())); + normalsInput.accessor.mBlob.ApplyMinMax(numVector3, reinterpret_cast(buffer.data())); - raw.mAttribs.push_back({"aNormal", Property::VECTOR3, numVector3, 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()); - static const std::function GenerateNormalsFunction[2] = + DALI_ASSERT_DEBUG(normalsInput.accessor.mBlob.mLength == positionBufferSize); + static const std::function GenerateNormalsFunction[2] = { GenerateNormals, GenerateNormals, }; - const bool generateSuccessed = GenerateNormalsFunction[MaskMatch(mFlags, U32_INDICES)](raw); + const bool generateSuccessed = GenerateNormalsFunction[MaskMatch(normalsInput.flags, MeshDefinition::Flags::U32_INDICES)](normalsInput.rawData); if(!generateSuccessed) { DALI_LOG_ERROR("Failed to generate normal\n"); @@ -1210,21 +1034,25 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& hasNormals = true; } } + return hasNormals; +} - if(!mTexCoords.empty() && mTexCoords[0].IsDefined()) +void LoadTextureCoordinates(LoadAccessorListInputs textureCoordinatesInput) +{ + if(!textureCoordinatesInput.accessors.empty() && textureCoordinatesInput.accessors[0].IsDefined()) { - auto& texCoords = mTexCoords[0]; + auto& texCoords = textureCoordinatesInput.accessors[0]; const auto bufferSize = texCoords.mBlob.GetBufferSize(); - uint32_t uvCount; + uint32_t uvCount; - if(MaskMatch(mFlags, S8_TEXCOORD) || MaskMatch(mFlags, U8_TEXCOORD)) + 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(mFlags, S16_TEXCOORD) || MaskMatch(mFlags, U16_TEXCOORD)) + 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)) && @@ -1242,15 +1070,15 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& std::vector buffer(bufferSize); std::string path; - auto& stream = GetAvailableData(fileStream, meshPath, buffers[texCoords.mBufferIdx], path); + 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 << "'."; } - GetDequantizedData(buffer, 2u, uvCount, mFlags & TEXCOORDS_MASK, texCoords.mNormalized); + GetDequantizedData(buffer, 2u, uvCount, textureCoordinatesInput.flags & MeshDefinition::FlagMasks::TEXCOORDS_MASK, texCoords.mNormalized); - if(MaskMatch(mFlags, FLIP_UVS_VERTICAL)) + if(MaskMatch(textureCoordinatesInput.flags, MeshDefinition::Flags::FLIP_UVS_VERTICAL)) { auto uv = reinterpret_cast(buffer.data()); auto uvEnd = uv + uvCount; @@ -1263,40 +1091,43 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& if(texCoords.mNormalized) { - GetDequantizedMinMax(texCoords.mBlob.mMin, texCoords.mBlob.mMax, mFlags & TEXCOORDS_MASK); + GetDequantizedMinMax(texCoords.mBlob.mMin, texCoords.mBlob.mMax, textureCoordinatesInput.flags & MeshDefinition::FlagMasks::TEXCOORDS_MASK); } texCoords.mBlob.ApplyMinMax(static_cast(uvCount), reinterpret_cast(buffer.data())); - raw.mAttribs.push_back({"aTexCoord", Property::VECTOR2, static_cast(uvCount), std::move(buffer)}); + 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()) { - const auto bufferSize = mTangents.mBlob.GetBufferSize(); + const auto bufferSize = tangentsInput.accessor.mBlob.GetBufferSize(); - uint32_t propertySize = static_cast((mTangentType == Property::VECTOR4) ? sizeof(Vector4) : sizeof(Vector3)); + 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(mFlags, S8_TANGENT)) + if(MaskMatch(tangentsInput.flags, MeshDefinition::Flags::S8_TANGENT)) { - DALI_ASSERT_ALWAYS(((mTangents.mBlob.mLength % (sizeof(int8_t) * componentCount) == 0) || - mTangents.mBlob.mStride >= (sizeof(int8_t) * componentCount)) && + 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(mFlags, S16_TANGENT)) + else if(MaskMatch(tangentsInput.flags, MeshDefinition::Flags::S16_TANGENT)) { - DALI_ASSERT_ALWAYS(((mTangents.mBlob.mLength % (sizeof(int16_t) * componentCount) == 0) || - mTangents.mBlob.mStride >= (sizeof(int16_t) * componentCount)) && + 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(((mTangents.mBlob.mLength % propertySize == 0) || - mTangents.mBlob.mStride >= propertySize) && + 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); } @@ -1304,27 +1135,27 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& 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 << "'."; } - GetDequantizedData(buffer, componentCount, numTangents, mFlags & TANGENTS_MASK, mTangents.mNormalized); + GetDequantizedData(buffer, componentCount, numTangents, tangentsInput.flags & MeshDefinition::FlagMasks::TANGENTS_MASK, tangentsInput.accessor.mNormalized); - if(mTangents.mNormalized) + if(tangentsInput.accessor.mNormalized) { - GetDequantizedMinMax(mTangents.mBlob.mMin, mTangents.mBlob.mMax, mFlags & TANGENTS_MASK); + GetDequantizedMinMax(tangentsInput.accessor.mBlob.mMin, tangentsInput.accessor.mBlob.mMax, tangentsInput.flags & MeshDefinition::FlagMasks::TANGENTS_MASK); } - mTangents.mBlob.ApplyMinMax(numTangents, reinterpret_cast(buffer.data())); + tangentsInput.accessor.mBlob.ApplyMinMax(numTangents, reinterpret_cast(buffer.data())); - raw.mAttribs.push_back({"aTangent", mTangentType, static_cast(numTangents), std::move(buffer)}); + 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()); - static const std::function GenerateTangentsFunction[2][2][2] = + DALI_ASSERT_DEBUG(tangentsInput.accessor.mBlob.mLength == normalBufferSize); + static const std::function GenerateTangentsFunction[2][2][2] = { { { @@ -1346,83 +1177,64 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& GenerateTangents, }, }}; - const bool hasUvs = !mTexCoords.empty() && mTexCoords[0].IsDefined(); - const bool generateSuccessed = GenerateTangentsFunction[MaskMatch(mFlags, U32_INDICES)][mTangentType == Property::VECTOR3][hasUvs](raw); + 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"); } } +} +void LoadColors(LoadAccessorListInputs colorsInput) +{ // Only support 1 vertex color - if(!mColors.empty() && mColors[0].IsDefined()) + if(!colorsInput.accessors.empty() && colorsInput.accessors[0].IsDefined()) { - uint32_t propertySize = mColors[0].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[0].mBlob.mLength % propertySize == 0) || - mColors[0].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[0].mBlob.GetBufferSize(); + const auto bufferSize = colorsInput.accessors[0].mBlob.GetBufferSize(); std::vector buffer(bufferSize); std::string path; - auto& stream = GetAvailableData(fileStream, meshPath, buffers[mColors[0].mBufferIdx], path); - if(!ReadAccessor(mColors[0], 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[0].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)}); } } else { - std::vector buffer(raw.mAttribs[0].mNumElements * sizeof(Vector4)); + std::vector buffer(colorsInput.rawData.mAttribs[0].mNumElements * sizeof(Vector4)); auto colors = reinterpret_cast(buffer.data()); - for(uint32_t i = 0; i < raw.mAttribs[0].mNumElements; i++) + for(uint32_t i = 0; i < colorsInput.rawData.mAttribs[0].mNumElements; i++) { colors[i] = Vector4::ONE; } - raw.mAttribs.push_back({"aVertexColor", Property::VECTOR4, raw.mAttribs[0].mNumElements, std::move(buffer)}); - } - - if(IsSkinned()) - { - int setIndex = 0; - for(auto& accessor : mJoints) - { - std::string pathJoint; - auto& streamJoint = GetAvailableData(fileStream, meshPath, buffers[accessor.mBufferIdx], pathJoint); - std::ostringstream jointName; - jointName << "aJoints" << setIndex; - ++setIndex; - ReadTypedJointAccessor(raw, mFlags, accessor, streamJoint, pathJoint, jointName.str()); - } - setIndex = 0; - for(auto& accessor : mWeights) - { - std::string pathWeight; - auto& streamWeight = GetAvailableData(fileStream, meshPath, buffers[accessor.mBufferIdx], pathWeight); - std::ostringstream weightName; - weightName << "aWeights" << setIndex; - ++setIndex; - ReadTypedWeightAccessor(raw, mFlags, accessor, streamWeight, pathWeight, weightName.str()); - } + 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; uint32_t totalTextureSize(0u); - auto processAccessor = [&](const Accessor& accessor, uint32_t vector3Size) { + auto processAccessor = [&](const MeshDefinition::Accessor& accessor, uint32_t vector3Size) { if(accessor.IsDefined()) { blendShapesBlob.mOffset = std::min(blendShapesBlob.mOffset, accessor.mBlob.mOffset); @@ -1432,27 +1244,27 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& } }; - for(const auto& blendShape : mBlendShapes) + for(const auto& blendShape : blendShapes) { - const auto positionMask = blendShape.mFlags & POSITIONS_MASK; - const auto normalMask = blendShape.mFlags & NORMALS_MASK; - const auto tangentMask = blendShape.mFlags & TANGENTS_MASK; + 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, S8_POSITION) ? sizeof(uint8_t) * 3 : (MaskMatch(positionMask, S16_POSITION) ? sizeof(uint16_t) * 3 : sizeof(Vector3))); - processAccessor(blendShape.normals, MaskMatch(normalMask, S8_NORMAL) ? sizeof(uint8_t) * 3 : (MaskMatch(normalMask, S16_NORMAL) ? sizeof(uint16_t) * 3 : sizeof(Vector3))); - processAccessor(blendShape.tangents, MaskMatch(tangentMask, S8_TANGENT) ? sizeof(uint8_t) * 3 : (MaskMatch(tangentMask, S16_TANGENT) ? sizeof(uint16_t) * 3 : sizeof(Vector3))); + 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(HasBlendShapes()) + 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(totalTextureSize, textureWidth, textureHeight); calculateGltf2BlendShapes = true; @@ -1460,25 +1272,25 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& 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()) { @@ -1491,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; } @@ -1526,6 +1551,29 @@ MeshGeometry MeshDefinition::Load(RawData&& raw) const } } + 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) { a.AttachBuffer(meshGeometry.geometry);