BsMesh.h 16 KB

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