BsMeshData.h 12 KB

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