BsMesh.h 16 KB

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