BsMeshBase.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 "BsResource.h"
  6. #include "BsBounds.h"
  7. #include "BsDrawOps.h"
  8. #include "BsSubMesh.h"
  9. namespace BansheeEngine
  10. {
  11. /** @addtogroup Resources
  12. * @{
  13. */
  14. /**
  15. * Planned usage for the mesh. These options usually affect performance and you should specify static if you don't plan
  16. * on modifying the mesh often, otherwise specify dynamic.
  17. */
  18. enum MeshUsage
  19. {
  20. MU_STATIC, /**< Specify for a mesh that is not often updated from the CPU. */
  21. MU_DYNAMIC, /**< Specify for a mesh that is often updated from the CPU. */
  22. MU_CPUCACHED = 0x1000 /**< All mesh data will also be cached in CPU memory, making it readable with GPU reads. */
  23. };
  24. /** Properties of a Mesh. Shared between sim and core thread versions of a Mesh. */
  25. class BS_CORE_EXPORT MeshProperties
  26. {
  27. public:
  28. MeshProperties();
  29. MeshProperties(UINT32 numVertices, UINT32 numIndices, DrawOperationType drawOp);
  30. MeshProperties(UINT32 numVertices, UINT32 numIndices, const Vector<SubMesh>& subMeshes);
  31. /**
  32. * Retrieves a sub-mesh containing data used for rendering a certain portion of this mesh. If no sub-meshes are
  33. * specified manually a special sub-mesh containing all indices is returned.
  34. */
  35. const SubMesh& getSubMesh(UINT32 subMeshIdx = 0) const;
  36. /** Retrieves a total number of sub-meshes in this mesh. */
  37. UINT32 getNumSubMeshes() const;
  38. /** Returns maximum number of vertices the mesh may store. */
  39. UINT32 getNumVertices() const { return mNumVertices; }
  40. /** Returns maximum number of indices the mesh may store. */
  41. UINT32 getNumIndices() const { return mNumIndices; }
  42. /** Returns bounds of the geometry contained in the vertex buffers for all sub-meshes. */
  43. const Bounds& getBounds() const { return mBounds; }
  44. protected:
  45. friend class MeshBase;
  46. friend class MeshCoreBase;
  47. friend class Mesh;
  48. friend class MeshCore;
  49. friend class TransientMesh;
  50. friend class TransientMeshCore;
  51. friend class MeshBaseRTTI;
  52. Vector<SubMesh> mSubMeshes;
  53. UINT32 mNumVertices;
  54. UINT32 mNumIndices;
  55. Bounds mBounds;
  56. };
  57. /** @} */
  58. /** @addtogroup Implementation
  59. * @{
  60. */
  61. /** @cond INTERNAL */
  62. /**
  63. * Core version of a class used as a basis for all implemenations of meshes.
  64. *
  65. * @see MeshBase
  66. *
  67. * @note Core thread.
  68. */
  69. class BS_CORE_EXPORT MeshCoreBase : public CoreObjectCore
  70. {
  71. public:
  72. MeshCoreBase(UINT32 numVertices, UINT32 numIndices, const Vector<SubMesh>& subMeshes);
  73. virtual ~MeshCoreBase() { }
  74. /** Get vertex data used for rendering. */
  75. virtual SPtr<VertexData> getVertexData() const = 0;
  76. /** Get index data used for rendering. */
  77. virtual SPtr<IndexBufferCore> getIndexBuffer() const = 0;
  78. /**
  79. * Returns an offset into the vertex buffers that is returned by getVertexData() that signifies where this meshes
  80. * vertices begin.
  81. *
  82. * @note Used when multiple meshes share the same buffers.
  83. */
  84. virtual UINT32 getVertexOffset() const { return 0; }
  85. /**
  86. * Returns an offset into the index buffer that is returned by getIndexData() that signifies where this meshes
  87. * indices begin.
  88. *
  89. * @note Used when multiple meshes share the same buffers.
  90. */
  91. virtual UINT32 getIndexOffset() const { return 0; }
  92. /** Returns a structure that describes how are the vertices stored in the mesh's vertex buffer. */
  93. virtual SPtr<VertexDataDesc> getVertexDesc() const = 0;
  94. /**
  95. * Called whenever this mesh starts being used on the GPU.
  96. *
  97. * @note Needs to be called after all commands referencing this mesh have been sent to the GPU.
  98. */
  99. virtual void _notifyUsedOnGPU() { }
  100. /** Returns properties that contain information about the mesh. */
  101. const MeshProperties& getProperties() const { return mProperties; }
  102. protected:
  103. /** @copydoc CoreObjectCore::syncToCore */
  104. virtual void syncToCore(const CoreSyncData& data) override;
  105. MeshProperties mProperties;
  106. };
  107. /** @endcond */
  108. /**
  109. * Base class all mesh implementations derive from. Meshes hold geometry information, normally in the form of one or
  110. * several index or vertex buffers. Different mesh implementations might choose to manage those buffers differently.
  111. *
  112. * @note Sim thread.
  113. */
  114. class BS_CORE_EXPORT MeshBase : public Resource
  115. {
  116. public:
  117. /**
  118. * Constructs a new mesh with no sub-meshes.
  119. *
  120. * @param[in] numVertices Number of vertices in the mesh.
  121. * @param[in] numIndices Number of indices in the mesh.
  122. * @param[in] drawOp Determines how should the provided indices be interpreted by the pipeline. Default
  123. * option is triangles, where three indices represent a single triangle.
  124. */
  125. MeshBase(UINT32 numVertices, UINT32 numIndices, DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  126. /**
  127. * Constructs a new mesh with one or multiple sub-meshes. (When using just one sub-mesh it is equivalent to using
  128. * the other overload).
  129. *
  130. * @param[in] numVertices Number of vertices in the mesh.
  131. * @param[in] numIndices Number of indices in the mesh.
  132. * @param[in] subMeshes Defines how are indices separated into sub-meshes, and how are those sub-meshes
  133. * rendered.
  134. */
  135. MeshBase(UINT32 numVertices, UINT32 numIndices, const Vector<SubMesh>& subMeshes);
  136. virtual ~MeshBase();
  137. /** Returns properties that contain information about the mesh. */
  138. const MeshProperties& getProperties() const { return mProperties; }
  139. /** Retrieves a core implementation of a mesh usable only from the core thread. */
  140. SPtr<MeshCoreBase> getCore() const;
  141. protected:
  142. /** @copydoc CoreObject::syncToCore */
  143. virtual CoreSyncData syncToCore(FrameAlloc* allocator) override;
  144. MeshProperties mProperties;
  145. /************************************************************************/
  146. /* SERIALIZATION */
  147. /************************************************************************/
  148. private:
  149. MeshBase() { } // Serialization only
  150. public:
  151. friend class MeshBaseRTTI;
  152. static RTTITypeBase* getRTTIStatic();
  153. virtual RTTITypeBase* getRTTI() const override;
  154. };
  155. /** @} */
  156. }