BsMesh.h 15 KB

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