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