BsMeshBase.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. /**
  62. * Core version of a class used as a basis for all implemenations of meshes.
  63. *
  64. * @see MeshBase
  65. *
  66. * @note Core thread.
  67. */
  68. class BS_CORE_EXPORT MeshCoreBase : public CoreObjectCore
  69. {
  70. public:
  71. MeshCoreBase(UINT32 numVertices, UINT32 numIndices, const Vector<SubMesh>& subMeshes);
  72. virtual ~MeshCoreBase() { }
  73. /** Get vertex data used for rendering. */
  74. virtual SPtr<VertexData> getVertexData() const = 0;
  75. /** Get index data used for rendering. */
  76. virtual SPtr<IndexBufferCore> getIndexBuffer() const = 0;
  77. /**
  78. * Returns an offset into the vertex buffers that is returned by getVertexData() that signifies where this meshes
  79. * vertices begin.
  80. *
  81. * @note Used when multiple meshes share the same buffers.
  82. */
  83. virtual UINT32 getVertexOffset() const { return 0; }
  84. /**
  85. * Returns an offset into the index buffer that is returned by getIndexData() that signifies where this meshes
  86. * indices begin.
  87. *
  88. * @note Used when multiple meshes share the same buffers.
  89. */
  90. virtual UINT32 getIndexOffset() const { return 0; }
  91. /** Returns a structure that describes how are the vertices stored in the mesh's vertex buffer. */
  92. virtual SPtr<VertexDataDesc> getVertexDesc() const = 0;
  93. /**
  94. * Called whenever this mesh starts being used on the GPU.
  95. *
  96. * @note Needs to be called after all commands referencing this mesh have been sent to the GPU.
  97. */
  98. virtual void _notifyUsedOnGPU() { }
  99. /** Returns properties that contain information about the mesh. */
  100. const MeshProperties& getProperties() const { return mProperties; }
  101. protected:
  102. /** @copydoc CoreObjectCore::syncToCore */
  103. void syncToCore(const CoreSyncData& data) override;
  104. MeshProperties mProperties;
  105. };
  106. /**
  107. * Base class all mesh implementations derive from. Meshes hold geometry information, normally in the form of one or
  108. * several index or vertex buffers. Different mesh implementations might choose to manage those buffers differently.
  109. *
  110. * @note Sim thread.
  111. */
  112. class BS_CORE_EXPORT MeshBase : public Resource
  113. {
  114. public:
  115. /**
  116. * Constructs a new mesh with no sub-meshes.
  117. *
  118. * @param[in] numVertices Number of vertices in the mesh.
  119. * @param[in] numIndices Number of indices in the mesh.
  120. * @param[in] drawOp Determines how should the provided indices be interpreted by the pipeline. Default
  121. * option is triangles, where three indices represent a single triangle.
  122. */
  123. MeshBase(UINT32 numVertices, UINT32 numIndices, DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  124. /**
  125. * Constructs a new mesh with one or multiple sub-meshes. (When using just one sub-mesh it is equivalent to using
  126. * the other overload).
  127. *
  128. * @param[in] numVertices Number of vertices in the mesh.
  129. * @param[in] numIndices Number of indices in the mesh.
  130. * @param[in] subMeshes Defines how are indices separated into sub-meshes, and how are those sub-meshes
  131. * rendered.
  132. */
  133. MeshBase(UINT32 numVertices, UINT32 numIndices, const Vector<SubMesh>& subMeshes);
  134. virtual ~MeshBase();
  135. /** Returns properties that contain information about the mesh. */
  136. const MeshProperties& getProperties() const { return mProperties; }
  137. /** Retrieves a core implementation of a mesh usable only from the core thread. */
  138. SPtr<MeshCoreBase> getCore() const;
  139. protected:
  140. /** @copydoc CoreObject::syncToCore */
  141. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  142. MeshProperties mProperties;
  143. /************************************************************************/
  144. /* SERIALIZATION */
  145. /************************************************************************/
  146. private:
  147. MeshBase() { } // Serialization only
  148. public:
  149. friend class MeshBaseRTTI;
  150. static RTTITypeBase* getRTTIStatic();
  151. RTTITypeBase* getRTTI() const override;
  152. };
  153. /** @} */
  154. }