BsMeshData.h 10 KB

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