BsMeshHeap.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsCoreObject.h"
  4. #include "BsDrawOps.h"
  5. #include "BsIndexBuffer.h"
  6. namespace BansheeEngine
  7. {
  8. /**
  9. * @brief Core thread version of MeshHeap.
  10. *
  11. * @see MeshHeap
  12. *
  13. * @note Core thread only.
  14. */
  15. class BS_CORE_EXPORT MeshHeapCore : public CoreObjectCore
  16. {
  17. /**
  18. * @brief Signifies how is a data chunk used.
  19. */
  20. enum class UseFlags
  21. {
  22. Used, /**< Data chunk is used by both CPU and GPU. */
  23. CPUFree, /**< Data chunk was released by CPU but not GPU. */
  24. GPUFree, /**< Data chunk was released by GPU but not CPU. */
  25. Free /**< Data chunk was released by both CPU and GPU. */
  26. };
  27. /**
  28. * @brief Represents a continuous chunk of memory.
  29. */
  30. struct ChunkData
  31. {
  32. UINT32 start, size;
  33. };
  34. /**
  35. * @brief Represents an allocated piece of data representing a mesh.
  36. */
  37. struct AllocatedData
  38. {
  39. UINT32 vertChunkIdx;
  40. UINT32 idxChunkIdx;
  41. UseFlags useFlags;
  42. UINT32 eventQueryIdx;
  43. SPtr<TransientMeshCore> mesh;
  44. };
  45. /**
  46. * @brief Data about a GPU query.
  47. */
  48. struct QueryData
  49. {
  50. EventQueryPtr query;
  51. UINT32 queryId;
  52. };
  53. public:
  54. ~MeshHeapCore();
  55. private:
  56. friend class MeshHeap;
  57. friend class TransientMesh;
  58. friend class TransientMeshCore;
  59. MeshHeapCore(UINT32 numVertices, UINT32 numIndices,
  60. const VertexDataDescPtr& vertexDesc, IndexType indexType = IT_32BIT);
  61. /**
  62. * @copydoc CoreObjectCore::initialize()
  63. */
  64. virtual void initialize() override;
  65. /**
  66. * @brief Allocates a new mesh in the heap, expanding the heap if needed.
  67. *
  68. * @param meshId Mesh for which we are allocating the data.
  69. * @param meshData Data to initialize the new mesh with.
  70. */
  71. void alloc(SPtr<TransientMeshCore> mesh, const MeshDataPtr& meshData);
  72. /**
  73. * @brief Deallocates the provided mesh Freed memory
  74. * will be re-used as soon as the GPU is done with the mesh
  75. */
  76. void dealloc(SPtr<TransientMeshCore> mesh);
  77. /**
  78. * @brief Resizes the vertex buffers so they max contain the provided
  79. * number of vertices.
  80. */
  81. void growVertexBuffer(UINT32 numVertices);
  82. /**
  83. * @brief Resizes the index buffer so they max contain the provided
  84. * number of indices.
  85. */
  86. void growIndexBuffer(UINT32 numIndices);
  87. /**
  88. * @brief Creates a new event query or returns an existing one from the pool
  89. * if available. Returned value is an index into mEventQueries array.
  90. */
  91. UINT32 createEventQuery();
  92. /**
  93. * @brief Frees the event query with the specified index and returns it to the
  94. * pool so it may be reused later.
  95. */
  96. void freeEventQuery(UINT32 idx);
  97. /**
  98. * @brief Gets internal vertex data for all the meshes.
  99. */
  100. SPtr<VertexData> getVertexData() const;
  101. /**
  102. * @brief Gets internal index data for all the meshes.
  103. */
  104. SPtr<IndexBufferCore> getIndexBuffer() const;
  105. /**
  106. * @brief Returns the offset in vertices from the start of the buffer
  107. * to the first vertex of the mesh with the provided ID.
  108. */
  109. UINT32 getVertexOffset(UINT32 meshId) const;
  110. /**
  111. * @brief Returns the offset in indices from the start of the buffer
  112. * to the first index of the mesh with the provided ID.
  113. */
  114. UINT32 getIndexOffset(UINT32 meshId) const;
  115. /**
  116. * @brief Called by the render system when a mesh gets queued to the GPU.
  117. */
  118. void notifyUsedOnGPU(UINT32 meshId);
  119. /**
  120. * @brief Called by an GPU event query when GPU processes the query. Normally
  121. * signals the heap that the GPU is done with the mesh.
  122. */
  123. static void queryTriggered(SPtr<MeshHeapCore> thisPtr, UINT32 meshId, UINT32 queryId);
  124. /**
  125. * @brief Attempts to reorganize the vertex and index buffer chunks in order to
  126. * in order to make free memory contigous.
  127. *
  128. * @note This will not actually copy any data from index/vertex buffers, and will only
  129. * modify the chunk descriptors.
  130. */
  131. void mergeWithNearbyChunks(UINT32 chunkVertIdx, UINT32 chunkIdxIdx);
  132. private:
  133. UINT32 mNumVertices;
  134. UINT32 mNumIndices;
  135. Vector<UINT8*> mCPUVertexData;
  136. UINT8* mCPUIndexData;
  137. SPtr<VertexData> mVertexData;
  138. SPtr<IndexBufferCore> mIndexBuffer;
  139. Map<UINT32, AllocatedData> mMeshAllocData;
  140. VertexDataDescPtr mVertexDesc;
  141. IndexType mIndexType;
  142. Vector<ChunkData> mVertChunks;
  143. Vector<ChunkData> mIdxChunks;
  144. Stack<UINT32> mEmptyVertChunks;
  145. Stack<UINT32> mEmptyIdxChunks;
  146. List<UINT32> mFreeVertChunks;
  147. List<UINT32> mFreeIdxChunks;
  148. Vector<QueryData> mEventQueries;
  149. Stack<UINT32> mFreeEventQueries;
  150. UINT32 mNextQueryId;
  151. static const float GrowPercent;
  152. };
  153. /**
  154. * @brief Mesh heap allows you to quickly allocate and deallocate a large amounts of temporary
  155. * meshes without the large overhead of normal Mesh creation.
  156. * Only requirement is that meshes share the same vertex description and index type.
  157. *
  158. * @note This class should be considered as a replacement for a normal Mesh if you are constantly
  159. * updating the mesh (e.g. every frame) and you are not able to discard entire mesh contents
  160. * on each update. Not using discard flag on normal meshes may introduce GPU-CPU sync points
  161. * which may severely limit performance. Primary purpose of this class is to avoid
  162. * those sync points by not forcing you to discard contents.
  163. *
  164. * Downside is that this class may allocate 2-3x (or more) memory than it is actually needed
  165. * for your data.
  166. *
  167. * Sim thread only
  168. */
  169. class BS_CORE_EXPORT MeshHeap : public CoreObject
  170. {
  171. public:
  172. /**
  173. * @brief Allocates a new mesh in the heap, expanding the heap if needed. Mesh will be initialized
  174. * with the provided meshData. You may use the returned transient mesh for drawing.
  175. *
  176. * @note Offsets provided by MeshData are ignored. MeshHeap will determine
  177. * where the data will be written internally.
  178. */
  179. TransientMeshPtr alloc(const MeshDataPtr& meshData, DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  180. /**
  181. * @brief Deallocates the provided mesh and makes that room on the heap re-usable as soon as the GPU
  182. * is also done with the mesh.
  183. */
  184. void dealloc(const TransientMeshPtr& mesh);
  185. /**
  186. * @brief Retrieves a core implementation of a mesh heap usable only from the
  187. * core thread.
  188. */
  189. SPtr<MeshHeapCore> getCore() const;
  190. /**
  191. * @brief Creates a new mesh heap.
  192. *
  193. * @param numVertices Initial number of vertices the heap may store. This will grow automatically if needed.
  194. * @param numIndices Initial number of indices the heap may store. This will grow automatically if needed.
  195. * @param vertexDesc Description of the stored vertices.
  196. * @param indexType Type of the stored indices.
  197. */
  198. static MeshHeapPtr create(UINT32 numVertices, UINT32 numIndices,
  199. const VertexDataDescPtr& vertexDesc, IndexType indexType = IT_32BIT);
  200. private:
  201. /**
  202. * @copydoc create
  203. */
  204. MeshHeap(UINT32 numVertices, UINT32 numIndices,
  205. const VertexDataDescPtr& vertexDesc, IndexType indexType = IT_32BIT);
  206. /**
  207. * @copydoc CoreObject::createCore
  208. */
  209. SPtr<CoreObjectCore> createCore() const override;
  210. private:
  211. UINT32 mNumVertices;
  212. UINT32 mNumIndices;
  213. VertexDataDescPtr mVertexDesc;
  214. IndexType mIndexType;
  215. Map<UINT32, TransientMeshPtr> mMeshes;
  216. UINT32 mNextFreeId;
  217. };
  218. }