BsCoreObjectManager.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsCoreObjectCore.h"
  4. #include "BsModule.h"
  5. namespace BansheeEngine
  6. {
  7. // TODO Low priority - Add debug option that would remember a call stack for each resource initialization,
  8. // so when we fail to release one we know which one it is.
  9. /**
  10. * @brief Determines type of synchronization to perform when syncing
  11. * dirty data between core and sim threads.
  12. */
  13. enum CoreObjectSync
  14. {
  15. Sim, /** Syncing sim thread data to core thread. */
  16. Core /** Syncing core thread data to sim thread. */
  17. };
  18. /**
  19. * @brief Manager that keeps track of all active CoreObjects.
  20. *
  21. * @note Internal class. Thread safe unless specified otherwise.
  22. */
  23. class BS_CORE_EXPORT CoreObjectManager : public Module<CoreObjectManager>
  24. {
  25. /**
  26. * @brief Stores dirty data that is to be transferred from core
  27. * thread to sim thread part of a CoreObject, for a single object.
  28. */
  29. struct SimStoredSyncObjData
  30. {
  31. SimStoredSyncObjData()
  32. :destinationObj(nullptr)
  33. { }
  34. SimStoredSyncObjData(CoreObject* destObj, const CoreSyncData& syncData)
  35. :destinationObj(destObj), syncData(syncData)
  36. { }
  37. CoreObject* destinationObj;
  38. CoreSyncData syncData;
  39. };
  40. /**
  41. * @brief Stores dirty data that is to be transferred from sim
  42. * thread to core thread part of a CoreObject, for a single object.
  43. */
  44. struct CoreStoredSyncObjData
  45. {
  46. CoreStoredSyncObjData()
  47. :destinationObj(nullptr)
  48. { }
  49. CoreStoredSyncObjData(CoreObjectCore* destObj, const CoreSyncData& syncData)
  50. :destinationObj(destObj), syncData(syncData)
  51. { }
  52. CoreObjectCore* destinationObj;
  53. CoreSyncData syncData;
  54. };
  55. /**
  56. * @brief Stores dirty data that is to be transferred from core
  57. * thread to sim thread part of a CoreObject, for all dirty
  58. * objects in one frame.
  59. */
  60. struct SimStoredSyncData
  61. {
  62. FrameAlloc* alloc = nullptr;
  63. Map<UINT64, SimStoredSyncObjData> entries;
  64. };
  65. /**
  66. * @brief Stores dirty data that is to be transferred from sim
  67. * thread to core thread part of a CoreObject, for all dirty
  68. * objects in one frame.
  69. */
  70. struct CoreStoredSyncData
  71. {
  72. FrameAlloc* alloc = nullptr;
  73. Map<UINT64, CoreStoredSyncObjData> entries;
  74. };
  75. public:
  76. CoreObjectManager();
  77. ~CoreObjectManager();
  78. /**
  79. * @brief Registers a new CoreObject notifying the manager the object
  80. * is created.
  81. */
  82. UINT64 registerObject(CoreObject* object);
  83. /**
  84. * @brief Unregisters a CoreObject notifying the manager the object
  85. * is destroyed.
  86. */
  87. void unregisterObject(CoreObject* object);
  88. /**
  89. * @brief Stores all syncable data from dirty core objects into memory allocated
  90. * by the provided allocator. Additional meta-data is stored internally to be
  91. * used by call to syncUpload.
  92. *
  93. * @param type Determines where to copy the dirty data from. If set to Sim the data copied
  94. * will be data accessible by the sim thread, and if set to Core the data copied
  95. * will be data accessible by the core thread.
  96. * @param allocator Allocator to use for allocating memory for stored data.
  97. *
  98. * @note Not thread safe. If used with Sim type it should only be called from
  99. * sim thread, and if used with Core type it should only be called from
  100. * core thread.
  101. *
  102. * Must be followed by a call to syncUpload with the same type.
  103. */
  104. void syncDownload(CoreObjectSync type, FrameAlloc* allocator);
  105. /**
  106. * @brief Copies all the data stored by previous call to "syncDownload"
  107. * into CoreObjects or their core thread versions.
  108. *
  109. * @param type Determines where to copy the stored data to. This will be the opposite
  110. * data source than used by "syncDownload". If set to Sim the data will be
  111. * copied to data accessible by core thread, and if set to Core the data
  112. * will be copied to data accessible by the sim thread.
  113. *
  114. * @note Not thread safe. If used with Sim type it should only be called from
  115. * core thread, and if used with Core type it should only be called from
  116. * sim thread.
  117. */
  118. void syncUpload(CoreObjectSync type);
  119. private:
  120. UINT64 mNextAvailableID;
  121. Map<UINT64, CoreObject*> mObjects;
  122. Vector<SimStoredSyncData> mSimSyncData;
  123. UINT32 mSimSyncIdx;
  124. UINT32 mSimSyncCount;
  125. Vector<CoreStoredSyncData> mCoreSyncData;
  126. UINT32 mCoreSyncIdx;
  127. UINT32 mCoreSyncCount;
  128. BS_MUTEX(mObjectsMutex);
  129. };
  130. }