BsMesh.h 12 KB

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