BsMeshBase.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. /** 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. MU_CPUREADABLE = 0x2000 /**< Allows the CPU to directly read the mesh data buffers from the GPU. */
  24. };
  25. /** Properties of a Mesh. Shared between sim and core thread versions of a Mesh. */
  26. class BS_CORE_EXPORT MeshProperties
  27. {
  28. public:
  29. MeshProperties();
  30. MeshProperties(UINT32 numVertices, UINT32 numIndices, DrawOperationType drawOp);
  31. MeshProperties(UINT32 numVertices, UINT32 numIndices, const Vector<SubMesh>& subMeshes);
  32. /**
  33. * Retrieves a sub-mesh containing data used for rendering a certain portion of this mesh. If no sub-meshes are
  34. * specified manually a special sub-mesh containing all indices is returned.
  35. */
  36. const SubMesh& getSubMesh(UINT32 subMeshIdx = 0) const;
  37. /** Retrieves a total number of sub-meshes in this mesh. */
  38. UINT32 getNumSubMeshes() const;
  39. /** Returns maximum number of vertices the mesh may store. */
  40. UINT32 getNumVertices() const { return mNumVertices; }
  41. /** Returns maximum number of indices the mesh may store. */
  42. UINT32 getNumIndices() const { return mNumIndices; }
  43. /** Returns bounds of the geometry contained in the vertex buffers for all sub-meshes. */
  44. const Bounds& getBounds() const { return mBounds; }
  45. protected:
  46. friend class MeshBase;
  47. friend class MeshCoreBase;
  48. friend class Mesh;
  49. friend class MeshCore;
  50. friend class TransientMesh;
  51. friend class TransientMeshCore;
  52. friend class MeshBaseRTTI;
  53. Vector<SubMesh> mSubMeshes;
  54. UINT32 mNumVertices;
  55. UINT32 mNumIndices;
  56. Bounds mBounds;
  57. };
  58. /** @} */
  59. /** @addtogroup Implementation
  60. * @{
  61. */
  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. void syncToCore(const CoreSyncData& data) override;
  105. MeshProperties mProperties;
  106. };
  107. /**
  108. * Base class all mesh implementations derive from. Meshes hold geometry information, normally in the form of one or
  109. * several index or vertex buffers. Different mesh implementations might choose to manage those buffers differently.
  110. *
  111. * @note Sim thread.
  112. */
  113. class BS_CORE_EXPORT MeshBase : public Resource
  114. {
  115. public:
  116. /**
  117. * Constructs a new mesh with no sub-meshes.
  118. *
  119. * @param[in] numVertices Number of vertices in the mesh.
  120. * @param[in] numIndices Number of indices in the mesh.
  121. * @param[in] drawOp Determines how should the provided indices be interpreted by the pipeline. Default
  122. * option is triangles, where three indices represent a single triangle.
  123. */
  124. MeshBase(UINT32 numVertices, UINT32 numIndices, DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  125. /**
  126. * Constructs a new mesh with one or multiple sub-meshes. (When using just one sub-mesh it is equivalent to using
  127. * the other overload).
  128. *
  129. * @param[in] numVertices Number of vertices in the mesh.
  130. * @param[in] numIndices Number of indices in the mesh.
  131. * @param[in] subMeshes Defines how are indices separated into sub-meshes, and how are those sub-meshes
  132. * rendered.
  133. */
  134. MeshBase(UINT32 numVertices, UINT32 numIndices, const Vector<SubMesh>& subMeshes);
  135. virtual ~MeshBase();
  136. /** Returns properties that contain information about the mesh. */
  137. const MeshProperties& getProperties() const { return mProperties; }
  138. /** Retrieves a core implementation of a mesh usable only from the core thread. */
  139. SPtr<MeshCoreBase> getCore() const;
  140. protected:
  141. /** @copydoc CoreObject::syncToCore */
  142. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  143. MeshProperties mProperties;
  144. /************************************************************************/
  145. /* SERIALIZATION */
  146. /************************************************************************/
  147. private:
  148. MeshBase() { } // Serialization only
  149. public:
  150. friend class MeshBaseRTTI;
  151. static RTTITypeBase* getRTTIStatic();
  152. RTTITypeBase* getRTTI() const override;
  153. };
  154. /** @} */
  155. }