BsMesh.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 Primary class for holding geometry. Stores data in the form of a vertex
  13. * buffers and optionally index buffer, which may be bound to the pipeline for drawing.
  14. * May contain multiple sub-meshes.
  15. */
  16. class BS_CORE_EXPORT Mesh : public MeshBase
  17. {
  18. public:
  19. virtual ~Mesh();
  20. /**
  21. * @copydoc MeshBase::initialize
  22. */
  23. virtual void initialize();
  24. /**
  25. * @copydoc GpuResource::_writeSubresourceSim
  26. */
  27. virtual void _writeSubresourceSim(UINT32 subresourceIdx, const GpuResourceData& data, bool discardEntireBuffer);
  28. /**
  29. * @copydoc GpuResource::writeSubresource
  30. */
  31. virtual void writeSubresource(UINT32 subresourceIdx, const GpuResourceData& data, bool discardEntireBuffer);
  32. /**
  33. * @copydoc GpuResource::readSubresource
  34. */
  35. virtual void readSubresource(UINT32 subresourceIdx, GpuResourceData& data);
  36. /**
  37. * @brief Allocates a buffer you may use for storage when reading a subresource. You
  38. * need to allocate such a buffer if you are calling "readSubresource".
  39. *
  40. * @param subresourceIdx Only 0 is supported. You can only update entire mesh at once.
  41. *
  42. * @note This method is thread safe.
  43. */
  44. MeshDataPtr allocateSubresourceBuffer(UINT32 subresourceIdx) const;
  45. /**
  46. * @brief Returns bounds of the geometry contained in the vertex buffers for all sub-meshes.
  47. */
  48. const Bounds& getBounds() const { return mBounds; }
  49. /**
  50. * @copydoc MeshBase::getVertexData
  51. */
  52. virtual SPtr<VertexData> _getVertexData() const;
  53. /**
  54. * @copydoc MeshBase::getIndexData
  55. */
  56. virtual SPtr<IndexBufferCore> _getIndexBuffer() const;
  57. /**
  58. * @brief Returns a dummy mesh, containing just one triangle. Don't modify the returned mesh.
  59. */
  60. static HMesh dummy();
  61. /************************************************************************/
  62. /* CORE PROXY */
  63. /************************************************************************/
  64. /**
  65. * @copydoc MeshBase::_createProxy
  66. */
  67. MeshProxyPtr _createProxy(UINT32 subMeshIdx);
  68. protected:
  69. friend class MeshManager;
  70. Mesh(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc,
  71. MeshBufferType bufferType = MeshBufferType::Static, DrawOperationType drawOp = DOT_TRIANGLE_LIST,
  72. IndexType indexType = IT_32BIT);
  73. Mesh(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc,
  74. const Vector<SubMesh>& subMeshes, MeshBufferType bufferType = MeshBufferType::Static,
  75. IndexType indexType = IT_32BIT);
  76. Mesh(const MeshDataPtr& initialMeshData, MeshBufferType bufferType = MeshBufferType::Static,
  77. DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  78. Mesh(const MeshDataPtr& initialMeshData, const Vector<SubMesh>& subMeshes, MeshBufferType bufferType = MeshBufferType::Static);
  79. std::shared_ptr<VertexData> mVertexData; // Core thread
  80. SPtr<IndexBufferCore> mIndexBuffer; // Core thread
  81. Bounds mBounds; // Core thread
  82. VertexDataDescPtr mVertexDesc; // Immutable
  83. MeshBufferType mBufferType; // Immutable
  84. IndexType mIndexType; // Immutable
  85. MeshDataPtr mTempInitialMeshData; // Immutable
  86. /**
  87. * @copydoc Resource::initialize_internal()
  88. */
  89. virtual void initialize_internal();
  90. /**
  91. * @copydoc Resource::destroy_internal()
  92. */
  93. virtual void destroy_internal();
  94. /**
  95. * @brief Updates bounds by calculating them from the vertices in the provided mesh data object.
  96. */
  97. void updateBounds(const MeshData& meshData);
  98. /**
  99. * @brief Calculates bounds surrounding the vertices in the provided buffer.
  100. *
  101. * @param verticesPtr Pointer to the buffer containing the positions of vertices to calculate bounds for.
  102. * @param numVertices Number of vertices in the provided buffer.
  103. * @param stride How many bytes are needed to advance from one vertex to another.
  104. */
  105. Bounds calculateBounds(UINT8* verticesPtr, UINT32 numVertices, UINT32 stride) const;
  106. /************************************************************************/
  107. /* SERIALIZATION */
  108. /************************************************************************/
  109. private:
  110. Mesh(); // Serialization only
  111. public:
  112. friend class MeshRTTI;
  113. static RTTITypeBase* getRTTIStatic();
  114. virtual RTTITypeBase* getRTTI() const;
  115. /************************************************************************/
  116. /* STATICS */
  117. /************************************************************************/
  118. public:
  119. /**
  120. * @brief Creates a new empty mesh. Created mesh will have no sub-meshes.
  121. *
  122. * @param numVertices Number of vertices in the mesh.
  123. * @param numIndices Number of indices in the mesh.
  124. * @param vertexDesc Vertex description structure that describes how are vertices organized in the
  125. * vertex buffer. When binding a mesh to the pipeline you must ensure vertex description
  126. * at least partially matches the input description of the currently bound vertex GPU program.
  127. * @param bufferType Specify static for buffers you don't plan on updating other reading from often. Otherwise specify
  128. * dynamic. This parameter affects performance.
  129. * @param drawOp Determines how should the provided indices be interpreted by the pipeline. Default option is triangles,
  130. * where three indices represent a single triangle.
  131. * @param indexType Size of indices, use smaller size for better performance, however be careful not to go over
  132. * the number of vertices limited by the size.
  133. */
  134. static HMesh create(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc, MeshBufferType bufferType = MeshBufferType::Static,
  135. DrawOperationType drawOp = DOT_TRIANGLE_LIST, IndexType indexType = IT_32BIT);
  136. /**
  137. * @brief Creates a new empty mesh. Created mesh will have specified sub-meshes you may render independently.
  138. *
  139. * @param numVertices Number of vertices in the mesh.
  140. * @param numIndices Number of indices in the mesh.
  141. * @param vertexDesc Vertex description structure that describes how are vertices organized in the
  142. * vertex buffer. When binding a mesh to the pipeline you must ensure vertex description
  143. * at least partially matches the input description of the currently bound vertex GPU program.
  144. * @param subMeshes Defines how are indices separated into sub-meshes, and how are those sub-meshes rendered.
  145. * Sub-meshes may be rendered independently.
  146. * @param bufferType Specify static for buffers you don't plan on updating other reading from often. Otherwise specify
  147. * dynamic. This parameter affects performance.
  148. * @param indexType Size of indices, use smaller size for better performance, however be careful not to go over
  149. * the number of vertices limited by the size.
  150. */
  151. static HMesh create(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc, const Vector<SubMesh>& subMeshes,
  152. MeshBufferType bufferType = MeshBufferType::Static, IndexType indexType = IT_32BIT);
  153. /**
  154. * @brief Creates a new mesh from an existing mesh data. Created mesh will match the vertex and index buffers described
  155. * by the mesh data exactly. Created mesh will have no sub-meshes.
  156. *
  157. * @param initialMeshData Vertex and index data used for initializing the mesh.
  158. * @param bufferType Specify static for buffers you don't plan on updating other reading from often. Otherwise specify
  159. * dynamic. This parameter affects performance.
  160. * @param drawOp Determines how should the provided indices be interpreted by the pipeline. Default option is triangles,
  161. * where three indices represent a single triangle.
  162. */
  163. static HMesh create(const MeshDataPtr& initialMeshData, MeshBufferType bufferType = MeshBufferType::Static,
  164. DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  165. /**
  166. * @brief Creates a new mesh from an existing mesh data. Created mesh will match the vertex and index buffers described
  167. * by the mesh data exactly. Created mesh will have specified sub-meshes you may render independently.
  168. *
  169. * @param initialMeshData Vertex and index data used for initializing the mesh.
  170. * @param subMeshes Defines how are indices separated into sub-meshes, and how are those sub-meshes rendered.
  171. * Sub-meshes may be rendered independently.
  172. * @param bufferType Specify static for buffers you don't plan on updating other reading from often. Otherwise specify
  173. * dynamic. This parameter affects performance.
  174. */
  175. static HMesh create(const MeshDataPtr& initialMeshData, const Vector<SubMesh>& subMeshes, MeshBufferType bufferType = MeshBufferType::Static);
  176. /**
  177. * @copydoc create(UINT32, UINT32, const VertexDataDescPtr&, MeshBufferType, DrawOperationType, IndexType)
  178. *
  179. * @note Internal method. Use "create" for normal use.
  180. */
  181. static MeshPtr _createPtr(UINT32 numVertices, UINT32 numIndices,
  182. const VertexDataDescPtr& vertexDesc, MeshBufferType bufferType = MeshBufferType::Static,
  183. DrawOperationType drawOp = DOT_TRIANGLE_LIST, IndexType indexType = IT_32BIT);
  184. /**
  185. * @copydoc create(UINT32, UINT32, const VertexDataDescPtr&, const Vector<SubMesh>&, MeshBufferType, IndexType)
  186. *
  187. * @note Internal method. Use "create" for normal use.
  188. */
  189. static MeshPtr _createPtr(UINT32 numVertices, UINT32 numIndices,
  190. const VertexDataDescPtr& vertexDesc, const Vector<SubMesh>& subMeshes,
  191. MeshBufferType bufferType = MeshBufferType::Static, IndexType indexType = IT_32BIT);
  192. /**
  193. * @copydoc create(const MeshDataPtr&, MeshBufferType, DrawOperationType)
  194. *
  195. * @note Internal method. Use "create" for normal use.
  196. */
  197. static MeshPtr _createPtr(const MeshDataPtr& initialMeshData, MeshBufferType bufferType = MeshBufferType::Static,
  198. DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  199. /**
  200. * @copydoc create(const MeshDataPtr&, const Vector<SubMesh>&, MeshBufferType)
  201. *
  202. * @note Internal method. Use "create" for normal use.
  203. */
  204. static MeshPtr _createPtr(const MeshDataPtr& initialMeshData, const Vector<SubMesh>& subMeshes,
  205. MeshBufferType bufferType = MeshBufferType::Static);
  206. };
  207. }