BsMeshBase.h 6.4 KB

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