Merge "Compute min/max value if min/max is not defined." into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-scene-loader / public-api / mesh-definition.h
1 #ifndef DALI_SCENE_LOADER_MESH_DEFINITION_H
2 #define DALI_SCENE_LOADER_MESH_DEFINITION_H
3 /*
4  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 // INTERNAL INCLUDES
21 #include "dali-scene-loader/public-api/api.h"
22 #include "dali-scene-loader/public-api/blend-shape-details.h"
23 #include "dali-scene-loader/public-api/index.h"
24 #include "dali-scene-loader/public-api/mesh-geometry.h"
25 #include "dali-scene-loader/public-api/utils.h"
26
27 // EXTERNAL INCLUDES
28 #include <memory>
29 #include "dali/public-api/common/vector-wrapper.h"
30
31 namespace Dali
32 {
33 namespace SceneLoader
34 {
35 /**
36  * @brief Defines a mesh with its attributes, the primitive type to render it as,
37  *  and the file to load it from with the offset and length information for the
38  *  individual attribute buffers.
39  */
40 struct DALI_SCENE_LOADER_API MeshDefinition
41 {
42   using Vector = std::vector<std::pair<MeshDefinition, MeshGeometry>>;
43
44   enum : uint32_t
45   {
46     INVALID = std::numeric_limits<uint32_t>::max()
47   };
48
49   enum Flags : uint16_t
50   {
51     FLIP_UVS_VERTICAL = NthBit(0),
52     U32_INDICES       = NthBit(1), // default is unsigned short
53     U16_JOINT_IDS     = NthBit(2), // default is floats
54   };
55
56   enum Attributes
57   {
58     INDICES           = NthBit(0),
59     POSITIONS         = NthBit(1),
60     NORMALS           = NthBit(2),
61     TEX_COORDS        = NthBit(3),
62     TANGENTS          = NthBit(4),
63     LEGACY_BITANGENTS = NthBit(5), // these are ignored; we're calculating them in the (PBR) shader.
64     JOINTS_0          = NthBit(6),
65     WEIGHTS_0         = NthBit(7),
66   };
67
68   /**
69    * @brief Describes raw data in terms of its position and size in a buffer.
70    *  All units in bytes.
71    */
72   struct Blob
73   {
74     uint32_t           mOffset          = INVALID; // the default means that the blob is undefined.
75     uint32_t           mLength          = 0;       // if the blob is undefined, its data may still be generated. This is enabled by setting length to some non-0 value. Refer to MeshDefinition for details.
76     uint16_t           mStride          = 0;       // ignore if 0
77     uint16_t           mElementSizeHint = 0;       // ignore if 0 or stride == 0
78     std::vector<float> mMin;
79     std::vector<float> mMax;
80
81     static void ComputeMinMax(std::vector<float>& min, std::vector<float>& max, uint32_t numComponents, uint32_t count, const float* values);
82
83     static void ApplyMinMax(const std::vector<float>& min, const std::vector<float>& max, uint32_t count, float* values);
84
85     Blob() = default;
86
87     Blob(uint32_t offset, uint32_t length, uint16_t stride = 0, uint16_t elementSizeHint = 0, const std::vector<float>& min = {}, const std::vector<float>& max = {});
88
89     /**
90      * @brief Calculates the size of a tightly-packed buffer for the elements from the blob.
91      */
92     uint32_t GetBufferSize() const;
93
94     /**
95      * @brief Convenience method to tell whether a Blob has meaningful data.
96      */
97     bool IsDefined() const
98     {
99       return mOffset != INVALID;
100     }
101
102     /**
103      * @brief Convenience method to tell whether the elements stored in the blob follow each
104      *  other tightly. The opposite would be interleaving.
105      */
106     bool IsConsecutive() const
107     {
108       return mStride == 0 || mStride == mElementSizeHint;
109     }
110
111     /**
112      * @brief Computes the min / max of the input value data.
113      * The min and max are stored in mMin and mMax.
114      *
115      * @param[in] numComponents number of components of data type. e.g., 3 for Vector3.
116      * @param[in] count The number of data.
117      * @param[in] values Data for the mesh.
118      */
119     void ComputeMinMax(uint32_t numComponents, uint32_t count, float* values);
120
121     /**
122      * @brief Applies the min / max values, if they're defined in the model
123      *
124      * @param[in] count The number of data.
125      * @param[in] values Data for the mesh that min / max values will be applied.
126      */
127     void ApplyMinMax(uint32_t count, float* values) const;
128   };
129
130   /**
131    * @brief A sparse blob describes a change in a reference Blob.
132    * @p indices describe what positions of the reference Blob change and
133    * @p values describe the new values.
134    */
135   struct SparseBlob
136   {
137     SparseBlob() = default;
138
139     SparseBlob(const Blob& indices, const Blob& values, uint32_t count);
140
141     Blob     mIndices;
142     Blob     mValues;
143     uint32_t mCount = 0u;
144   };
145
146   struct Accessor
147   {
148     Blob                        mBlob;
149     std::unique_ptr<SparseBlob> mSparse;
150
151     Accessor() = default;
152
153     Accessor(const Accessor&) = delete;
154     Accessor& operator=(const Accessor&) = delete;
155
156     Accessor(Accessor&&) = default;
157     Accessor& operator=(Accessor&&) = default;
158
159     Accessor(const MeshDefinition::Blob&       blob,
160              const MeshDefinition::SparseBlob& sparse);
161
162     bool IsDefined() const
163     {
164       return mBlob.IsDefined() || (mSparse && (mSparse->mIndices.IsDefined() && mSparse->mValues.IsDefined()));
165     }
166   };
167
168   /**
169    * @brief Stores a blend shape.
170    */
171   struct BlendShape
172   {
173     std::string name;
174     Accessor    deltas;
175     Accessor    normals;
176     Accessor    tangents;
177     float       weight = 0.f;
178   };
179
180   struct RawData
181   {
182     struct Attrib
183     {
184       std::string          mName;
185       Property::Type       mType;
186       uint32_t             mNumElements;
187       std::vector<uint8_t> mData;
188
189       void AttachBuffer(Geometry& g) const;
190     };
191
192     std::vector<uint16_t> mIndices;
193     std::vector<Attrib>   mAttribs;
194
195     unsigned int        mBlendShapeBufferOffset{0};
196     Dali::Vector<float> mBlendShapeUnnormalizeFactor;
197     PixelData           mBlendShapeData;
198   };
199
200   MeshDefinition() = default;
201
202   MeshDefinition(const MeshDefinition&) = delete;
203   MeshDefinition& operator=(const MeshDefinition&) = delete;
204
205   MeshDefinition(MeshDefinition&&) = default;
206   MeshDefinition& operator=(MeshDefinition&&) = default;
207
208   /**
209    * @brief Determines whether the mesh definition is that of a quad.
210    */
211   bool IsQuad() const;
212
213   /**
214    * @brief Determines whether the mesh is used for skeletal animation.
215    */
216   bool IsSkinned() const;
217
218   /**
219    * @brief Whether the mesh has blend shapes.
220    */
221   bool HasBlendShapes() const;
222
223   /**
224    * @brief Requests normals to be generated.
225    * @note Generation happens in LoadRaw().
226    * @note Must have Vector3 positions defined.
227    */
228   void RequestNormals();
229
230   /**
231    * @brief Requests tangents to be generated.
232    * @note Generation happens in LoadRaw().
233    * @note Must have Vector3 normals defined.
234    */
235   void RequestTangents();
236
237   /**
238    * @brief Loads raw geometry data, which includes index (optional) and
239    *  attribute buffers, as well as blend shape data. This is then returned.
240    * @note This can be done on any thread.
241    */
242   RawData LoadRaw(const std::string& modelsPath);
243
244   /**
245    * @brief Creates a MeshGeometry based firstly on the value of the uri member:
246    *  if it is "quad", a textured quad is created; otherwise it uses the
247    *  attribute (and index) buffers and blend shape information (if available)
248    *  from @a raw.
249    *  If mFlipVertical was set, the UVs are flipped in Y, i.e. v = 1.0 - v.
250    */
251   MeshGeometry Load(RawData&& raw) const;
252
253 public: // DATA
254   uint32_t       mFlags         = 0x0;
255   Geometry::Type mPrimitiveType = Geometry::TRIANGLES;
256   std::string    mUri;
257   Accessor       mIndices;
258   Accessor       mPositions;
259   Accessor       mNormals; // data can be generated based on positions
260   Accessor       mTexCoords;
261   Accessor       mColors;
262   Accessor       mTangents; // data can be generated based on normals and texCoords (the latter isn't mandatory; the results will be better if available)
263   Accessor       mJoints0;
264   Accessor       mWeights0;
265   Property::Type mTangentType{Property::VECTOR3};
266
267   Blob                    mBlendShapeHeader;
268   std::vector<BlendShape> mBlendShapes;
269   BlendShapes::Version    mBlendShapeVersion = BlendShapes::Version::INVALID;
270
271   Index mSkeletonIdx = INVALID_INDEX;
272 };
273
274 } // namespace SceneLoader
275 } // namespace Dali
276
277 #endif //DALI_SCENE_LOADER_MESH_DEFINITION_H