BsMeshBase.h 6.3 KB

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