BsMeshData.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsGpuResourceData.h"
  4. #include "BsVertexBuffer.h"
  5. #include "BsIndexBuffer.h"
  6. #include "BsVertexDeclaration.h"
  7. #include "BsDrawOps.h"
  8. #include "BsSubMesh.h"
  9. #include "BsBounds.h"
  10. namespace BansheeEngine
  11. {
  12. /** @addtogroup Resources
  13. * @{
  14. */
  15. /** Iterator that allows you to easily populate or read vertex elements in MeshData. */
  16. template<class T>
  17. class VertexElemIter
  18. {
  19. public:
  20. VertexElemIter()
  21. :mData(nullptr), mEnd(nullptr), mByteStride(0), mNumElements(0)
  22. {
  23. }
  24. VertexElemIter(UINT8* data, UINT32 byteStride, UINT32 numElements)
  25. :mData(data), mByteStride(byteStride), mNumElements(numElements)
  26. {
  27. mEnd = mData + byteStride * numElements;
  28. }
  29. /** Adds a new value to the iterators current position and advances the iterator. */
  30. void addValue(const T& value)
  31. {
  32. setValue(value);
  33. moveNext();
  34. }
  35. /** Sets a new value at the iterators current position. */
  36. void setValue(const T& value)
  37. {
  38. memcpy(mData, &value, sizeof(T));
  39. }
  40. /** Returns the value at the iterators current position. */
  41. T& getValue()
  42. {
  43. return *((T*)mData);
  44. }
  45. /** Moves the iterator to the next position. Returns true if there are more elements. */
  46. bool moveNext()
  47. {
  48. #ifdef BS_DEBUG_MODE
  49. if(mData >= mEnd)
  50. {
  51. BS_EXCEPT(InternalErrorException, "Vertex element iterator out of buffer bounds.");
  52. }
  53. #endif
  54. mData += mByteStride;
  55. return mData < mEnd;
  56. }
  57. /** Returns the number of elements this iterator can iterate over. */
  58. UINT32 getNumElements() const { return mNumElements; }
  59. private:
  60. UINT8* mData;
  61. UINT8* mEnd;
  62. UINT32 mByteStride;
  63. UINT32 mNumElements;
  64. };
  65. /** Contains per-vertex bone weights and indexes used for skinning, for up to four bones. */
  66. struct BoneWeight
  67. {
  68. int index0;
  69. int index1;
  70. int index2;
  71. int index3;
  72. float weight0;
  73. float weight1;
  74. float weight2;
  75. float weight3;
  76. };
  77. /** Contains mesh vertex and index data used for initializing, updating and reading mesh data from Mesh. */
  78. class BS_CORE_EXPORT MeshData : public GpuResourceData
  79. {
  80. public:
  81. /**
  82. * Constructs a new object that can hold number of vertices described by the provided vertex data description. As
  83. * well as a number of indices of the provided type.
  84. */
  85. MeshData(UINT32 numVertices, UINT32 numIndexes, const VertexDataDescPtr& vertexData, IndexType indexType = IT_32BIT);
  86. ~MeshData();
  87. /**
  88. * Copies data from @p data parameter into the internal buffer for the specified semantic.
  89. *
  90. * @param[in] semantic Semantic that allows the engine to connect the data to a shader input slot.
  91. * @param[in] data Vertex data, containing at least @p size bytes.
  92. * @param[in] size The size of the data. Must be the size of the vertex element type * number of
  93. * vertices.
  94. * @param[in] semanticIdx (optional) If there are multiple semantics with the same name, use different index
  95. * to differentiate between them.
  96. * @param[in] streamIdx (optional) Zero-based index of the stream. Each stream will internally be
  97. * represented as a single vertex buffer.
  98. */
  99. void setVertexData(VertexElementSemantic semantic, UINT8* data, UINT32 size, UINT32 semanticIdx = 0, UINT32 streamIdx = 0);
  100. /**
  101. * Copies data from the internal buffer to the pre-allocated buffer for the specified semantic.
  102. *
  103. * @param[in] semantic Semantic that allows the engine to connect the data to a shader input slot.
  104. * @param[in] data Buffer that will receive vertex data, of at least @p size bytes.
  105. * @param[in] size The size of the data. Must be the size of the vertex element type * number of
  106. * vertices.
  107. * @param[in] semanticIdx (optional) If there are multiple semantics with the same name, use different index
  108. * to differentiate between them.
  109. * @param[in] streamIdx (optional) Zero-based index of the stream. Each stream will internally be
  110. * represented as a single vertex buffer.
  111. */
  112. void getVertexData(VertexElementSemantic semantic, UINT8* data, UINT32 size, UINT32 semanticIdx = 0, UINT32 streamIdx = 0);
  113. /**
  114. * Returns an iterator you can use for easily retrieving or setting Vector2 vertex elements. This is the preferred
  115. * method of assigning or reading vertex data.
  116. *
  117. * @note If vertex data of this type/semantic/index/stream doesn't exist and exception will be thrown.
  118. */
  119. VertexElemIter<Vector2> getVec2DataIter(VertexElementSemantic semantic, UINT32 semanticIdx = 0, UINT32 streamIdx = 0);
  120. /**
  121. * Returns an iterator you can use for easily retrieving or setting Vector3 vertex elements. This is the preferred
  122. * method of assigning or reading vertex data.
  123. *
  124. * @note If vertex data of this type/semantic/index/stream doesn't exist and exception will be thrown.
  125. */
  126. VertexElemIter<Vector3> getVec3DataIter(VertexElementSemantic semantic, UINT32 semanticIdx = 0, UINT32 streamIdx = 0);
  127. /**
  128. * Returns an iterator you can use for easily retrieving or setting Vector4 vertex elements. This is the preferred
  129. * method of assigning or reading vertex data.
  130. *
  131. * @note If vertex data of this type/semantic/index/stream doesn't exist and exception will be thrown.
  132. */
  133. VertexElemIter<Vector4> getVec4DataIter(VertexElementSemantic semantic, UINT32 semanticIdx = 0, UINT32 streamIdx = 0);
  134. /**
  135. * Returns an iterator you can use for easily retrieving or setting DWORD vertex elements. This is the preferred
  136. * method of assigning or reading vertex data.
  137. *
  138. * @note If vertex data of this type/semantic/index/stream doesn't exist and exception will be thrown.
  139. */
  140. VertexElemIter<UINT32> getDWORDDataIter(VertexElementSemantic semantic, UINT32 semanticIdx = 0, UINT32 streamIdx = 0);
  141. /** Returns the total number of vertices this object can hold. */
  142. UINT32 getNumVertices() const { return mNumVertices; }
  143. /** Returns the total number of indices this object can hold. */
  144. UINT32 getNumIndices() const;
  145. /** Returns a 16-bit pointer to the start of the internal index buffer. */
  146. UINT16* getIndices16() const;
  147. /** Returns a 32-bit pointer to the start of the internal index buffer. */
  148. UINT32* getIndices32() const;
  149. /** Returns the size of an index element in bytes. */
  150. UINT32 getIndexElementSize() const;
  151. /** Returns the type of an index element. */
  152. IndexType getIndexType() const { return mIndexType; }
  153. /**
  154. * Returns the pointer to the first element of the specified type. If you want to iterate over all elements you
  155. * need to call getVertexStride() to get the number of bytes you need to advance between each element.
  156. *
  157. * @param[in] semantic Semantic that allows the engine to connect the data to a shader input slot.
  158. * @param[in] semanticIdx (optional) If there are multiple semantics with the same name, use different index
  159. * to differentiate between them.
  160. * @param[in] streamIdx (optional) Zero-based index of the stream. Each stream will internally be
  161. * represented as a single vertex buffer.
  162. * @return null if it fails, else the element data.
  163. */
  164. UINT8* getElementData(VertexElementSemantic semantic, UINT32 semanticIdx = 0, UINT32 streamIdx = 0) const;
  165. /**
  166. * Returns an offset into the internal buffer where this element with the provided semantic starts. Offset is
  167. * provided in number of bytes.
  168. *
  169. * @param[in] semantic Semantic that allows the engine to connect the data to a shader input slot.
  170. * @param[in] semanticIdx (optional) If there are multiple semantics with the same name, use different index
  171. * to differentiate between them.
  172. * @param[in] streamIdx (optional) Zero-based index of the stream. Each stream will internally be
  173. * represented as a single vertex buffer.
  174. */
  175. UINT32 getElementOffset(VertexElementSemantic semantic, UINT32 semanticIdx = 0, UINT32 streamIdx = 0) const;
  176. /** Returns an object that describes data contained in a single vertex. */
  177. const VertexDataDescPtr& getVertexDesc() const { return mVertexData; }
  178. /** Return the size (in bytes) of the entire buffer. */
  179. UINT32 getSize() const { return getInternalBufferSize(); }
  180. /** Calculates the bounds of all vertices stored in the internal buffer. */
  181. Bounds calculateBounds() const;
  182. /**
  183. * Combines a number of submeshes and their mesh data into one large mesh data buffer.
  184. *
  185. * @param[in] elements Data containing vertices and indices referenced by the submeshes. Number of elements
  186. * must be the same as number of submeshes.
  187. * @param[in] subMeshes Submeshes representing vertex and index range to take from mesh data and combine.
  188. * Number of submeshes must match the number of provided MeshData elements.
  189. * @param[out] subMeshes Outputs all combined sub-meshes with their new index and vertex offsets referencing
  190. * the newly created MeshData.
  191. * @return Combined mesh data containing all vertices and indexes references by the provided
  192. * sub-meshes.
  193. */
  194. static MeshDataPtr combine(const Vector<MeshDataPtr>& elements, const Vector<Vector<SubMesh>>& allSubMeshes,
  195. Vector<SubMesh>& subMeshes);
  196. protected:
  197. /** Returns the size of the internal buffer in bytes. */
  198. UINT32 getInternalBufferSize() const override;
  199. private:
  200. /** Returns a pointer to the start of the index buffer. */
  201. UINT8* getIndexData() const { return getData(); }
  202. /** Returns a pointer to the start of the specified vertex stream. */
  203. UINT8* getStreamData(UINT32 streamIdx) const;
  204. /** Returns an offset in bytes to the start of the index buffer from the start of the internal buffer. */
  205. UINT32 getIndexBufferOffset() const;
  206. /** Returns an offset in bytes to the start of the stream from the start of the internal buffer. */
  207. UINT32 getStreamOffset(UINT32 streamIdx = 0) const;
  208. /** Returns the size of the index buffer in bytes. */
  209. UINT32 getIndexBufferSize() const;
  210. /** Returns the size of the specified stream in bytes. */
  211. UINT32 getStreamSize(UINT32 streamIdx) const;
  212. /** Returns the size of all the streams in bytes. */
  213. UINT32 getStreamSize() const;
  214. /**
  215. * Returns the data needed for iterating over the requested vertex element.
  216. *
  217. * @param[in] semantic Semantic of the element we are looking for.
  218. * @param[in] semanticIdx If there are multiple semantics with the same name, use different index to
  219. * differentiate between them.
  220. * @param[in] streamIdx Zero-based index of the stream the element resides in.
  221. * @param[out] data Pointer to the start of this elements data.
  222. * @param[out] stride Number of bytes between vertex elements of this type.
  223. */
  224. void getDataForIterator(VertexElementSemantic semantic, UINT32 semanticIdx, UINT32 streamIdx, UINT8*& data, UINT32& stride) const;
  225. private:
  226. friend class Mesh;
  227. friend class MeshCore;
  228. friend class MeshHeap;
  229. friend class MeshHeapCore;
  230. UINT32 mDescBuilding;
  231. UINT32 mNumVertices;
  232. UINT32 mNumIndices;
  233. IndexType mIndexType;
  234. VertexDataDescPtr mVertexData;
  235. /************************************************************************/
  236. /* SERIALIZATION */
  237. /************************************************************************/
  238. private:
  239. MeshData(); // Serialization only
  240. public:
  241. friend class MeshDataRTTI;
  242. static RTTITypeBase* getRTTIStatic();
  243. virtual RTTITypeBase* getRTTI() const override;
  244. };
  245. /** @} */
  246. }