BsMesh.h 19 KB

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