BsMeshHeap.h 7.3 KB

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