BsMeshBase.h 5.4 KB

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