BsMesh.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsMeshBase.h"
  4. #include "BsMeshData.h"
  5. #include "BsVertexData.h"
  6. #include "BsDrawOps.h"
  7. #include "BsSubMesh.h"
  8. #include "BsBounds.h"
  9. namespace BansheeEngine
  10. {
  11. /**
  12. * @brief Core thread portion of a Mesh.
  13. *
  14. * @see Mesh.
  15. *
  16. * @note Core thread.
  17. */
  18. class BS_CORE_EXPORT MeshCore : public MeshCoreBase
  19. {
  20. public:
  21. MeshCore(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc,
  22. const Vector<SubMesh>& subMeshes, MeshBufferType bufferType, IndexType indexType,
  23. MeshDataPtr initialMeshData);
  24. /**
  25. * @brief CoreObjectCore::initialize
  26. */
  27. virtual void initialize();
  28. /**
  29. * @brief CoreObjectCore::destroy
  30. */
  31. virtual void destroy();
  32. /**
  33. * @copydoc MeshCoreBase::getVertexData
  34. */
  35. virtual SPtr<VertexData> getVertexData() const;
  36. /**
  37. * @copydoc MeshCoreBase::getIndexBuffer
  38. */
  39. virtual SPtr<IndexBufferCore> getIndexBuffer() const;
  40. /**
  41. * @brief Updates a part of the current mesh with the provided data.
  42. *
  43. * @param subresourceIdx Index of the subresource to update, if the mesh has more than one.
  44. * @param data Data to update the mesh with.
  45. * @param discardEntireBuffer When true the existing contents of the resource you are updating will be discarded. This can make the
  46. * operation faster. Resources with certain buffer types might require this flag to be in a specific state
  47. * otherwise the operation will fail.
  48. * @param updateBounds If true the internal bounds of the mesh will be recalculated based on the provided data.
  49. */
  50. virtual void writeSubresource(UINT32 subresourceIdx, const MeshData& data, bool discardEntireBuffer, bool updateBounds = true);
  51. /**
  52. * @brief Reads a part of the current resource into the provided "data" parameter.
  53. * Data buffer needs to be pre-allocated.
  54. *
  55. * @param subresourceIdx Index of the subresource to update, if the mesh has more than one.
  56. * @param data Buffer that will receive the data. Should be allocated with "allocateSubresourceBuffer"
  57. * to ensure it is of valid type and size.
  58. */
  59. virtual void readSubresource(UINT32 subresourceIdx, MeshData& data);
  60. protected:
  61. friend class Mesh;
  62. /**
  63. * @brief Updates bounds by calculating them from the vertices in the provided mesh data object.
  64. */
  65. void updateBounds(const MeshData& meshData);
  66. std::shared_ptr<VertexData> mVertexData;
  67. SPtr<IndexBufferCore> mIndexBuffer;
  68. VertexDataDescPtr mVertexDesc;
  69. MeshBufferType mBufferType;
  70. IndexType mIndexType;
  71. MeshDataPtr mTempInitialMeshData;
  72. };
  73. /**
  74. * @brief Primary class for holding geometry. Stores data in the form of a vertex
  75. * buffers and optionally index buffer, which may be bound to the pipeline for drawing.
  76. * May contain multiple sub-meshes.
  77. *
  78. * @note Sim thread.
  79. */
  80. class BS_CORE_EXPORT Mesh : public MeshBase
  81. {
  82. public:
  83. virtual ~Mesh();
  84. /**
  85. * @copydoc MeshBase::initialize
  86. */
  87. virtual void initialize();
  88. /**
  89. * @brief Updates the mesh with new data. The actual write will be queued for later execution on the core thread.
  90. * Provided data buffer will be locked until the operation completes.
  91. *
  92. * @param accessor Accessor to queue the operation on.
  93. *
  94. * @return Async operation object you can use to track operation completion.
  95. *
  96. * @see MeshCore::writeSubresource
  97. */
  98. AsyncOp writeSubresource(CoreAccessor& accessor, UINT32 subresourceIdx, const MeshDataPtr& data, bool discardEntireBuffer);
  99. /**
  100. * @brief Reads internal mesh data to the provided previously allocated buffer. The read is queued for execution
  101. * on the core thread and not executed immediately. Provided data buffer will be locked until the
  102. * operation completes.
  103. *
  104. * @param accessor Accessor to queue the operation on.
  105. *
  106. * @return Async operation object you can use to track operation completion.
  107. *
  108. * @see MeshCore::readSubresource
  109. */
  110. AsyncOp readSubresource(CoreAccessor& accessor, UINT32 subresourceIdx, const MeshDataPtr& data);
  111. /**
  112. * @brief Allocates a buffer you may use for storage when reading a subresource. You
  113. * need to allocate such a buffer if you are calling "readSubresource".
  114. *
  115. * @param subresourceIdx Only 0 is supported. You can only update entire mesh at once.
  116. *
  117. * @note Thread safe.
  118. */
  119. MeshDataPtr allocateSubresourceBuffer(UINT32 subresourceIdx) const;
  120. /**
  121. * @brief Retrieves a core implementation of a mesh usable only from the
  122. * core thread.
  123. */
  124. SPtr<MeshCore> getCore() const;
  125. /**
  126. * @brief Returns a dummy mesh, containing just one triangle. Don't modify the returned mesh.
  127. */
  128. static HMesh dummy();
  129. protected:
  130. friend class MeshManager;
  131. Mesh(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc,
  132. MeshBufferType bufferType = MeshBufferType::Static, DrawOperationType drawOp = DOT_TRIANGLE_LIST,
  133. IndexType indexType = IT_32BIT);
  134. Mesh(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc,
  135. const Vector<SubMesh>& subMeshes, MeshBufferType bufferType = MeshBufferType::Static,
  136. IndexType indexType = IT_32BIT);
  137. Mesh(const MeshDataPtr& initialMeshData, MeshBufferType bufferType = MeshBufferType::Static,
  138. DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  139. Mesh(const MeshDataPtr& initialMeshData, const Vector<SubMesh>& subMeshes, MeshBufferType bufferType = MeshBufferType::Static);
  140. /**
  141. * @brief Updates bounds by calculating them from the vertices in the provided mesh data object.
  142. */
  143. void updateBounds(const MeshData& meshData);
  144. /**
  145. * @copydoc RenderTarget::createCore
  146. */
  147. SPtr<CoreObjectCore> createCore() const;
  148. mutable MeshDataPtr mTempInitialMeshData;
  149. VertexDataDescPtr mVertexDesc;
  150. MeshBufferType mBufferType;
  151. IndexType mIndexType;
  152. /************************************************************************/
  153. /* SERIALIZATION */
  154. /************************************************************************/
  155. private:
  156. Mesh(); // Serialization only
  157. public:
  158. friend class MeshRTTI;
  159. static RTTITypeBase* getRTTIStatic();
  160. virtual RTTITypeBase* getRTTI() const;
  161. /************************************************************************/
  162. /* STATICS */
  163. /************************************************************************/
  164. public:
  165. /**
  166. * @brief Creates a new empty mesh. Created mesh will have no sub-meshes.
  167. *
  168. * @param numVertices Number of vertices in the mesh.
  169. * @param numIndices Number of indices in the mesh.
  170. * @param vertexDesc Vertex description structure that describes how are vertices organized in the
  171. * vertex buffer. When binding a mesh to the pipeline you must ensure vertex description
  172. * at least partially matches the input description of the currently bound vertex GPU program.
  173. * @param bufferType Specify static for buffers you don't plan on updating other reading from often. Otherwise specify
  174. * dynamic. This parameter affects performance.
  175. * @param drawOp Determines how should the provided indices be interpreted by the pipeline. Default option is triangles,
  176. * where three indices represent a single triangle.
  177. * @param indexType Size of indices, use smaller size for better performance, however be careful not to go over
  178. * the number of vertices limited by the size.
  179. */
  180. static HMesh create(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc, MeshBufferType bufferType = MeshBufferType::Static,
  181. DrawOperationType drawOp = DOT_TRIANGLE_LIST, IndexType indexType = IT_32BIT);
  182. /**
  183. * @brief Creates a new empty mesh. Created mesh will have specified sub-meshes you may render independently.
  184. *
  185. * @param numVertices Number of vertices in the mesh.
  186. * @param numIndices Number of indices in the mesh.
  187. * @param vertexDesc Vertex description structure that describes how are vertices organized in the
  188. * vertex buffer. When binding a mesh to the pipeline you must ensure vertex description
  189. * at least partially matches the input description of the currently bound vertex GPU program.
  190. * @param subMeshes Defines how are indices separated into sub-meshes, and how are those sub-meshes rendered.
  191. * Sub-meshes may be rendered independently.
  192. * @param bufferType Specify static for buffers you don't plan on updating other reading from often. Otherwise specify
  193. * dynamic. This parameter affects performance.
  194. * @param indexType Size of indices, use smaller size for better performance, however be careful not to go over
  195. * the number of vertices limited by the size.
  196. */
  197. static HMesh create(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc, const Vector<SubMesh>& subMeshes,
  198. MeshBufferType bufferType = MeshBufferType::Static, IndexType indexType = IT_32BIT);
  199. /**
  200. * @brief Creates a new mesh from an existing mesh data. Created mesh will match the vertex and index buffers described
  201. * by the mesh data exactly. Created mesh will have no sub-meshes.
  202. *
  203. * @param initialMeshData Vertex and index data used for initializing the mesh.
  204. * @param bufferType Specify static for buffers you don't plan on updating other reading from often. Otherwise specify
  205. * dynamic. This parameter affects performance.
  206. * @param drawOp Determines how should the provided indices be interpreted by the pipeline. Default option is triangles,
  207. * where three indices represent a single triangle.
  208. */
  209. static HMesh create(const MeshDataPtr& initialMeshData, MeshBufferType bufferType = MeshBufferType::Static,
  210. DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  211. /**
  212. * @brief Creates a new mesh from an existing mesh data. Created mesh will match the vertex and index buffers described
  213. * by the mesh data exactly. Created mesh will have specified sub-meshes you may render independently.
  214. *
  215. * @param initialMeshData Vertex and index data used for initializing the mesh.
  216. * @param subMeshes Defines how are indices separated into sub-meshes, and how are those sub-meshes rendered.
  217. * Sub-meshes may be rendered independently.
  218. * @param bufferType Specify static for buffers you don't plan on updating other reading from often. Otherwise specify
  219. * dynamic. This parameter affects performance.
  220. */
  221. static HMesh create(const MeshDataPtr& initialMeshData, const Vector<SubMesh>& subMeshes, MeshBufferType bufferType = MeshBufferType::Static);
  222. /**
  223. * @copydoc create(UINT32, UINT32, const VertexDataDescPtr&, MeshBufferType, DrawOperationType, IndexType)
  224. *
  225. * @note Internal method. Use "create" for normal use.
  226. */
  227. static MeshPtr _createPtr(UINT32 numVertices, UINT32 numIndices,
  228. const VertexDataDescPtr& vertexDesc, MeshBufferType bufferType = MeshBufferType::Static,
  229. DrawOperationType drawOp = DOT_TRIANGLE_LIST, IndexType indexType = IT_32BIT);
  230. /**
  231. * @copydoc create(UINT32, UINT32, const VertexDataDescPtr&, const Vector<SubMesh>&, MeshBufferType, IndexType)
  232. *
  233. * @note Internal method. Use "create" for normal use.
  234. */
  235. static MeshPtr _createPtr(UINT32 numVertices, UINT32 numIndices,
  236. const VertexDataDescPtr& vertexDesc, const Vector<SubMesh>& subMeshes,
  237. MeshBufferType bufferType = MeshBufferType::Static, IndexType indexType = IT_32BIT);
  238. /**
  239. * @copydoc create(const MeshDataPtr&, MeshBufferType, DrawOperationType)
  240. *
  241. * @note Internal method. Use "create" for normal use.
  242. */
  243. static MeshPtr _createPtr(const MeshDataPtr& initialMeshData, MeshBufferType bufferType = MeshBufferType::Static,
  244. DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  245. /**
  246. * @copydoc create(const MeshDataPtr&, const Vector<SubMesh>&, MeshBufferType)
  247. *
  248. * @note Internal method. Use "create" for normal use.
  249. */
  250. static MeshPtr _createPtr(const MeshDataPtr& initialMeshData, const Vector<SubMesh>& subMeshes,
  251. MeshBufferType bufferType = MeshBufferType::Static);
  252. };
  253. }