2
0

BsMesh.h 12 KB

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