BsMesh.h 17 KB

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