BsMesh.h 16 KB

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