2
0

BsMesh.h 16 KB

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