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=6cc3d6a11de22ed2b9fb16a1b5634ba59684034d;hpb=9bbca96c9151fb1a96b47e17f09c720b7c05adc2;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 6cc3d6a..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. @@ -25,12 +25,40 @@ #include #include #include +#include #include 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 { @@ -73,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; } @@ -94,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; } @@ -209,73 +237,60 @@ 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) +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)}); } -template -void ReadWeightAccessor(MeshDefinition::RawData& raw, const MeshDefinition::Accessor& accessor, std::istream& source, const std::string& meshPath) +template +void ReadTypedVectorAccessor(LoadDataType loadDataType, MeshDefinition::Accessor& accessor, std::iostream& stream, std::vector& buffer) { - 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)) - { - ExceptionFlinger(ASSERT_LOCATION) << "Failed to read weights from '" << meshPath << "'."; - } - - if constexpr(sizeofBlobUnit != sizeof(Vector4)) + switch(loadDataType) { - 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({"aWeights", Property::VECTOR4, static_cast(outBufferSize / sizeof(Vector4)), std::move(buffer)}); } template> @@ -446,26 +461,144 @@ 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 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; + + 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)); + } - const auto bufferSize = blendShape.deltas.mBlob.GetBufferSize(); 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()), &sparseIndices); + 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. @@ -500,17 +633,44 @@ void CalculateGltf2BlendShapes(uint8_t* geometryBuffer, const std::vector= sizeof(Vector3)) && - "Blend Shape normals buffer length not a multiple of element size"); + const auto bufferSize = blendShape.normals.mBlob.GetBufferSize(); + uint32_t numVector3; + + 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)) + { + 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)); + } - const auto bufferSize = blendShape.normals.mBlob.GetBufferSize(); std::vector buffer(bufferSize); std::vector sparseIndices; if(ReadAccessor(blendShape.normals, buffers[blendShape.normals.mBufferIdx].GetBufferStream(), buffer.data(), &sparseIndices)) { - blendShape.normals.mBlob.ApplyMinMax(static_cast(bufferSize / sizeof(Vector3)), reinterpret_cast(buffer.data()), &sparseIndices); + GetDequantizedData(buffer, 3u, numVector3, blendShape.mFlags & MeshDefinition::NORMALS_MASK, blendShape.normals.mNormalized); + + if(blendShape.normals.mNormalized) + { + 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()); @@ -546,17 +706,45 @@ 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(); + + 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 + { + 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)); + } - const auto bufferSize = blendShape.tangents.mBlob.GetBufferSize(); std::vector buffer(bufferSize); std::vector sparseIndices; if(ReadAccessor(blendShape.tangents, buffers[blendShape.tangents.mBufferIdx].GetBufferStream(), buffer.data(), &sparseIndices)) { - blendShape.tangents.mBlob.ApplyMinMax(static_cast(bufferSize / sizeof(Vector3)), reinterpret_cast(buffer.data()), &sparseIndices); + GetDequantizedData(buffer, 3u, numVector3, blendShape.mFlags & MeshDefinition::TANGENTS_MASK, blendShape.tangents.mNormalized); + + if(blendShape.tangents.mNormalized) + { + 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()); @@ -635,288 +823,208 @@ 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} +template +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, std::vector* sparseIndices) +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()); + uint32_t numVector3 = 0u; + std::vector positions; + if(positionsInput.accessor.IsDefined()) + { + const auto bufferSize = positionsInput.accessor.mBlob.GetBufferSize(); - 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(MaskMatch(positionsInput.flags, MeshDefinition::Flags::S8_POSITION) || MaskMatch(positionsInput.flags, MeshDefinition::Flags::U8_POSITION)) + { + 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(positionsInput.flags, MeshDefinition::Flags::S16_POSITION) || MaskMatch(positionsInput.flags, MeshDefinition::Flags::U16_POSITION)) + { + 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(((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)); + } - if(!clampFn) - { - return; - } + std::vector buffer(bufferSize); - auto end = values + count * numComponents; - while(values != end) - { - auto nextElement = values + numComponents; - uint32_t i = 0; - while(values != nextElement) + std::string path; + auto& stream = GetAvailableData(positionsInput.fileStream, positionsInput.meshPath, positionsInput.buffers[positionsInput.accessor.mBufferIdx], path); + if(!ReadAccessor(positionsInput.accessor, stream, buffer.data())) { - clampFn(min.data(), max.data(), i, *values); - ++values; - ++i; + ExceptionFlinger(ASSERT_LOCATION) << "Failed to read positions from '" << path << "'."; } - } -} -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) -{ -} + GetDequantizedData(buffer, 3u, numVector3, positionsInput.flags & MeshDefinition::FlagMasks::POSITIONS_MASK, positionsInput.accessor.mNormalized); -uint32_t MeshDefinition::Blob::GetBufferSize() const -{ - return mLength; -} + if(positionsInput.accessor.mNormalized) + { + GetDequantizedMinMax(positionsInput.accessor.mBlob.mMin, positionsInput.accessor.mBlob.mMax, positionsInput.flags & MeshDefinition::FlagMasks::POSITIONS_MASK); + } -void MeshDefinition::Blob::ComputeMinMax(uint32_t numComponents, uint32_t count, float* values) -{ - ComputeMinMax(mMin, mMax, numComponents, count, values); -} + if(positionsInput.accessor.mBlob.mMin.size() != 3u || positionsInput.accessor.mBlob.mMax.size() != 3u) + { + positionsInput.accessor.mBlob.ComputeMinMax(3u, numVector3, reinterpret_cast(buffer.data())); + } + else + { + positionsInput.accessor.mBlob.ApplyMinMax(numVector3, reinterpret_cast(buffer.data())); + } -void MeshDefinition::Blob::ApplyMinMax(uint32_t count, float* values, std::vector* sparseIndices) const -{ - ApplyMinMax(mMin, mMax, count, values, sparseIndices); + if(hasBlendShape) + { + positions.resize(numVector3); + std::copy(buffer.data(), buffer.data() + buffer.size(), reinterpret_cast(positions.data())); + } + + positionsInput.rawData.mAttribs.push_back({"aPosition", Property::VECTOR3, numVector3, std::move(buffer)}); + } + return numVector3; } -void MeshDefinition::RawData::Attrib::AttachBuffer(Geometry& g) const +bool LoadNormals(LoadAccessorInputs normalsInput, bool isTriangles, uint32_t positionBufferSize) { - 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()) + auto hasNormals = normalsInput.accessor.IsDefined(); + if(hasNormals) { - 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()); - } - } + const auto bufferSize = normalsInput.accessor.mBlob.GetBufferSize(); + uint32_t numVector3; - if(mIndices.IsDefined()) - { - if(MaskMatch(mFlags, U32_INDICES)) + if(MaskMatch(normalsInput.flags, MeshDefinition::Flags::S8_NORMAL)) { - 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 << "'."; - } + 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, U8_INDICES)) + else if(MaskMatch(normalsInput.flags, MeshDefinition::Flags::S16_NORMAL)) { - 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 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)) - { - 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(((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(((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(((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 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())) - { - 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) - { - mPositions.mBlob.ComputeMinMax(3u, numVector3, reinterpret_cast(buffer.data())); - } - else - { - mPositions.mBlob.ApplyMinMax(numVector3, reinterpret_cast(buffer.data())); - } - - if(HasBlendShapes()) + auto& stream = GetAvailableData(normalsInput.fileStream, normalsInput.meshPath, normalsInput.buffers[normalsInput.accessor.mBufferIdx], path); + if(!ReadAccessor(normalsInput.accessor, stream, buffer.data())) { - positions.resize(numVector3); - std::copy(buffer.data(), buffer.data() + buffer.size(), reinterpret_cast(positions.data())); + ExceptionFlinger(ASSERT_LOCATION) << "Failed to read normals from '" << path << "'."; } - raw.mAttribs.push_back({"aPosition", Property::VECTOR3, numVector3, std::move(buffer)}); - } + GetDequantizedData(buffer, 3u, numVector3, normalsInput.flags & MeshDefinition::FlagMasks::NORMALS_MASK, normalsInput.accessor.mNormalized); - const auto isTriangles = mPrimitiveType == Geometry::TRIANGLES; - auto hasNormals = mNormals.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(); - std::vector buffer(bufferSize); - - std::string path; - auto& stream = GetAvailableData(fileStream, meshPath, buffers[mNormals.mBufferIdx], path); - if(!ReadAccessor(mNormals, stream, buffer.data())) + if(normalsInput.accessor.mNormalized) { - ExceptionFlinger(ASSERT_LOCATION) << "Failed to read normals from '" << path << "'."; + GetDequantizedMinMax(normalsInput.accessor.mBlob.mMin, normalsInput.accessor.mBlob.mMax, normalsInput.flags & MeshDefinition::FlagMasks::NORMALS_MASK); } - mNormals.mBlob.ApplyMinMax(static_cast(bufferSize / sizeof(Vector3)), reinterpret_cast(buffer.data())); + 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()); - 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"); @@ -926,25 +1034,51 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& 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; @@ -955,34 +1089,73 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& } } - mTexCoords.mBlob.ApplyMinMax(static_cast(uvCount), 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()); - static const std::function GenerateTangentsFunction[2][2][2] = + DALI_ASSERT_DEBUG(tangentsInput.accessor.mBlob.mLength == normalBufferSize); + static const std::function GenerateTangentsFunction[2][2][2] = { { { @@ -1004,137 +1177,120 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& GenerateTangents, }, }}; - 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"); } } +} - 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)}); } } 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()) - { - 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); - } - - std::string pathWeight; - auto& streamWeight = GetAvailableData(fileStream, meshPath, buffers[mWeights0.mBufferIdx], pathWeight); - if(MaskMatch(mFlags, U16_WEIGHT)) - { - ReadWeightAccessor(raw, mWeights0, streamWeight, pathWeight); - } - else if(MaskMatch(mFlags, U8_WEIGHT)) - { - ReadWeightAccessor(raw, mWeights0, streamWeight, pathWeight); - } - else - { - ReadWeightAccessor(raw, mWeights0, streamWeight, pathWeight); - } + 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()) { @@ -1147,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; } @@ -1182,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);