BsMesh.h 19 KB

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