BsMeshBase.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsGpuResource.h"
  4. #include "BsBounds.h"
  5. #include "BsDrawOps.h"
  6. #include "BsSubMesh.h"
  7. #include "BsMeshProxy.h"
  8. namespace BansheeEngine
  9. {
  10. /**
  11. * @brief Type of mesh dirty flags
  12. */
  13. enum class MeshDirtyFlag
  14. {
  15. Mesh = 0x01, /**< Internal mesh data is dirty. */
  16. Proxy = 0x02 /**< Active proxy needs to be updated. */
  17. };
  18. /**
  19. * @brief Type of buffers used by a mesh. These options usually affect performance and
  20. * you should specify static if you don't plan on modifying the mesh often,
  21. * otherwise specify dynamic.
  22. */
  23. enum class MeshBufferType
  24. {
  25. Static,
  26. Dynamic
  27. };
  28. /**
  29. * @brief Base class all mesh implementations derive from. Meshes hold geometry information,
  30. * normally in the form of one or serveral index or vertex buffers. Different mesh implementations
  31. * might choose to manage those buffers differently.
  32. *
  33. * @note Core thread only unless noted otherwise.
  34. */
  35. class BS_CORE_EXPORT MeshBase : public GpuResource
  36. {
  37. public:
  38. /**
  39. * @brief Constructs a new mesh with no sub-meshes.
  40. *
  41. * @param numVertices Number of vertices in the mesh.
  42. * @param numIndices Number of indices in the mesh.
  43. * @param drawOp Determines how should the provided indices be interpreted by the pipeline. Default option is triangles,
  44. * where three indices represent a single triangle.
  45. */
  46. MeshBase(UINT32 numVertices, UINT32 numIndices, DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  47. /**
  48. * @brief Constructs a new mesh with one or multiple sub-meshes. (When using just one sub-mesh it is equivalent
  49. * to using the other overload).
  50. *
  51. * @param numVertices Number of vertices in the mesh.
  52. * @param numIndices Number of indices in the mesh.
  53. * @param subMeshes Defines how are indices separated into sub-meshes, and how are those sub-meshes rendered.
  54. */
  55. MeshBase(UINT32 numVertices, UINT32 numIndices, const Vector<SubMesh>& subMeshes);
  56. virtual ~MeshBase();
  57. /**
  58. * @brief Retrieves a sub-mesh containing data used for rendering a
  59. * certain portion of this mesh. If no sub-meshes are specified manually
  60. * a special sub-mesh containing all indices is returned.
  61. *
  62. * @note Thread safe.
  63. */
  64. const SubMesh& getSubMesh(UINT32 subMeshIdx = 0) const;
  65. /**
  66. * @brief Retrieves a total number of sub-meshes in this mesh.
  67. *
  68. * @note Thread safe.
  69. */
  70. UINT32 getNumSubMeshes() const;
  71. /**
  72. * @brief Returns maximum number of vertices the mesh may store.
  73. *
  74. * @note Thread safe.
  75. */
  76. UINT32 getNumVertices() const { return mNumVertices; }
  77. /**
  78. * @brief Returns maximum number of indices the mesh may store.
  79. *
  80. * @note Thread safe.
  81. */
  82. UINT32 getNumIndices() const { return mNumIndices; }
  83. /**
  84. * @brief Get vertex data used for rendering.
  85. *
  86. * @note Core thread only. Internal method.
  87. */
  88. virtual std::shared_ptr<VertexData> _getVertexData() const = 0;
  89. /**
  90. * @brief Get index data used for rendering.
  91. *
  92. * @note Core thread only. Internal method.
  93. */
  94. virtual IndexBufferPtr _getIndexBuffer() const = 0;
  95. /**
  96. * @brief Returns an offset into the vertex buffers that is returned
  97. * by getVertexData that signifies where this meshes vertices
  98. * begin.
  99. *
  100. * @note Used when multiple meshes share the same buffers.
  101. *
  102. * Core thread only. Internal method.
  103. */
  104. virtual UINT32 _getVertexOffset() const { return 0; }
  105. /**
  106. * @brief Returns an offset into the index buffer that is returned
  107. * by getIndexData that signifies where this meshes indices
  108. * begin.
  109. *
  110. * @note Used when multiple meshes share the same buffers.
  111. *
  112. * Core thread only. Internal method.
  113. */
  114. virtual UINT32 _getIndexOffset() const { return 0; }
  115. /**
  116. * @brief Called whenever this mesh starts being used on the GPU.
  117. *
  118. * @note Needs to be called after all commands referencing this
  119. * mesh have been sent to the GPU.
  120. *
  121. * Core thread only. Internal method.
  122. */
  123. virtual void _notifyUsedOnGPU() { }
  124. /************************************************************************/
  125. /* CORE PROXY */
  126. /************************************************************************/
  127. /**
  128. * @brief Checks is the core dirty flag set. This is used by external systems
  129. * to know when internal data has changed and core thread potentially needs to be notified.
  130. *
  131. * @note Sim thread only.
  132. */
  133. bool _isCoreDirty(MeshDirtyFlag flag) const { return (mCoreDirtyFlags & (UINT32)flag) != 0; }
  134. /**
  135. * @brief Marks the core dirty flag as clean.
  136. *
  137. * @note Sim thread only.
  138. */
  139. void _markCoreClean(MeshDirtyFlag flag) { mCoreDirtyFlags &= ~(UINT32)flag; }
  140. /**
  141. * @brief Gets the currently active proxy of this material.
  142. */
  143. MeshProxyPtr _getActiveProxy(UINT32 i) const { return mActiveProxies[i]; }
  144. /**
  145. * @brief Sets an active proxy for this material.
  146. */
  147. void _setActiveProxy(UINT32 i, const MeshProxyPtr& proxy) { mActiveProxies[i] = proxy; }
  148. /**
  149. * @brief Creates a new core proxy from the current mesh data. Core proxy contains a snapshot of
  150. * mesh data normally managed on the sim thread (e.g. bounds).
  151. *
  152. * @note Sim thread only.
  153. * You generally need to update the core thread with a new proxy whenever core
  154. * dirty flag is set.
  155. */
  156. virtual MeshProxyPtr _createProxy(UINT32 subMeshIdx) = 0;
  157. protected:
  158. /**
  159. * @brief Marks the core data as dirty.
  160. */
  161. void markCoreDirty() { mCoreDirtyFlags = 0xFFFFFFFF; }
  162. protected:
  163. Vector<SubMesh> mSubMeshes; // Immutable
  164. UINT32 mNumVertices; // Immutable
  165. UINT32 mNumIndices; // Immutable
  166. Vector<MeshProxyPtr> mActiveProxies;
  167. UINT32 mCoreDirtyFlags;
  168. /************************************************************************/
  169. /* SERIALIZATION */
  170. /************************************************************************/
  171. private:
  172. MeshBase(); // Serialization only
  173. public:
  174. friend class MeshBaseRTTI;
  175. static RTTITypeBase* getRTTIStatic();
  176. virtual RTTITypeBase* getRTTI() const;
  177. };
  178. }