BsMeshBase.h 6.5 KB

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