BsCoreObject.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "BsCoreObject.h"
  2. #include "BsCoreObjectCore.h"
  3. #include "BsCoreThread.h"
  4. #include "BsCoreObjectManager.h"
  5. #include "BsCoreThreadAccessor.h"
  6. #include "BsFrameAlloc.h"
  7. #include "BsDebug.h"
  8. using namespace std::placeholders;
  9. namespace BansheeEngine
  10. {
  11. CoreObject::CoreObject(bool initializeOnRenderThread)
  12. : mFlags(0), mInternalID(0), mCoreDirtyFlags(0xFFFFFFFF)
  13. {
  14. mInternalID = CoreObjectManager::instance().registerObject(this);
  15. mFlags = initializeOnRenderThread ? mFlags | CGO_INIT_ON_CORE_THREAD : mFlags;
  16. }
  17. CoreObject::~CoreObject()
  18. {
  19. if(!isDestroyed())
  20. {
  21. // Object must be released with destroy() otherwise engine can still try to use it, even if it was destructed
  22. // (e.g. if an object has one of its methods queued in a command queue, and is destructed, you will be accessing invalid memory)
  23. BS_EXCEPT(InternalErrorException, "Destructor called but object is not destroyed. This will result in nasty issues.");
  24. }
  25. #if BS_DEBUG_MODE
  26. if(!mThis.expired())
  27. {
  28. BS_EXCEPT(InternalErrorException, "Shared pointer to this object still has active references but " \
  29. "the object is being deleted? You shouldn't delete CoreObjects manually.");
  30. }
  31. #endif
  32. CoreObjectManager::instance().unregisterObject(this);
  33. }
  34. void CoreObject::destroy()
  35. {
  36. setIsDestroyed(true);
  37. if(requiresInitOnCoreThread())
  38. {
  39. assert(BS_THREAD_CURRENT_ID != CoreThread::instance().getCoreThreadId() && "Cannot destroy sim thead object from core thread.");
  40. queueDestroyGpuCommand(mCoreSpecific);
  41. }
  42. mCoreSpecific = nullptr;
  43. }
  44. void CoreObject::initialize()
  45. {
  46. mCoreSpecific = createCore();
  47. if (mCoreSpecific != nullptr)
  48. {
  49. if (requiresInitOnCoreThread())
  50. {
  51. mCoreSpecific->setScheduledToBeInitialized(true);
  52. assert(BS_THREAD_CURRENT_ID != CoreThread::instance().getCoreThreadId() && "Cannot initialize sim thread object from core thread.");
  53. queueInitializeGpuCommand(mCoreSpecific);
  54. }
  55. else
  56. {
  57. mCoreSpecific->initialize();
  58. }
  59. }
  60. }
  61. void CoreObject::blockUntilCoreInitialized()
  62. {
  63. if (mCoreSpecific != nullptr)
  64. mCoreSpecific->synchronize();
  65. }
  66. void CoreObject::syncToCore(CoreAccessor& accessor)
  67. {
  68. struct IndividualCoreSyncData
  69. {
  70. SPtr<CoreObjectCore> destination;
  71. CoreSyncData syncData;
  72. FrameAlloc* allocator;
  73. };
  74. FrameAlloc* allocator = gCoreThread().getFrameAlloc();
  75. Vector<IndividualCoreSyncData> syncData;
  76. bs_frame_mark();
  77. {
  78. FrameVector<SPtr<CoreObject>> dependencies;
  79. UINT32 stackPos = 0;
  80. dependencies.push_back(getThisPtr());
  81. while (stackPos < dependencies.size())
  82. {
  83. SPtr<CoreObject> curObj = dependencies[stackPos];
  84. stackPos++;
  85. if (curObj->isCoreDirty())
  86. {
  87. SPtr<CoreObjectCore> destObj = curObj->getCore();
  88. if (destObj == nullptr)
  89. return;
  90. IndividualCoreSyncData data;
  91. data.allocator = allocator;
  92. data.destination = destObj;
  93. data.syncData = syncToCore(data.allocator);
  94. syncData.push_back(data);
  95. curObj->markCoreClean();
  96. }
  97. // Note: I don't check for recursion. Possible infinite loop if two objects
  98. // are dependent on one another.
  99. curObj->getCoreDependencies(dependencies);
  100. }
  101. }
  102. bs_frame_clear();
  103. std::function<void(const Vector<IndividualCoreSyncData>&)> callback =
  104. [](const Vector<IndividualCoreSyncData>& data)
  105. {
  106. // Traverse in reverse to sync dependencies before dependants
  107. for (auto& riter = data.rbegin(); riter != data.rend(); ++riter)
  108. {
  109. const IndividualCoreSyncData& entry = *riter;
  110. entry.destination->syncToCore(entry.syncData);
  111. UINT8* dataPtr = entry.syncData.getBuffer();
  112. if (dataPtr != nullptr)
  113. entry.allocator->dealloc(dataPtr);
  114. }
  115. };
  116. if (syncData.size() > 0)
  117. accessor.queueCommand(std::bind(callback, syncData));
  118. }
  119. void CoreObject::_setThisPtr(std::shared_ptr<CoreObject> ptrThis)
  120. {
  121. mThis = ptrThis;
  122. }
  123. void CoreObject::queueGpuCommand(const SPtr<CoreObjectCore>& obj, std::function<void()> func)
  124. {
  125. // We call another internal method and go through an additional layer of abstraction in order to keep an active
  126. // reference to the obj (saved in the bound function).
  127. // We could have called the function directly using "this" pointer but then we couldn't have used a shared_ptr for the object,
  128. // in which case there is a possibility that the object would be released and deleted while still being in the command queue.
  129. gCoreAccessor().queueCommand(std::bind(&CoreObject::executeGpuCommand, obj, func));
  130. }
  131. AsyncOp CoreObject::queueReturnGpuCommand(const SPtr<CoreObjectCore>& obj, std::function<void(AsyncOp&)> func)
  132. {
  133. // See queueGpuCommand
  134. return gCoreAccessor().queueReturnCommand(std::bind(&CoreObject::executeReturnGpuCommand, obj, func, _1));
  135. }
  136. void CoreObject::queueInitializeGpuCommand(const SPtr<CoreObjectCore>& obj)
  137. {
  138. std::function<void()> func = std::bind(&CoreObjectCore::initialize, obj.get());
  139. CoreThread::instance().queueCommand(std::bind(&CoreObject::executeGpuCommand, obj, func));
  140. }
  141. void CoreObject::queueDestroyGpuCommand(const SPtr<CoreObjectCore>& obj)
  142. {
  143. std::function<void()> func = [&](){}; // Do nothing function. We just need the shared pointer to stay alive until it reaches the core thread
  144. gCoreAccessor().queueCommand(std::bind(&CoreObject::executeGpuCommand, obj, func));
  145. }
  146. void CoreObject::executeGpuCommand(const SPtr<CoreObjectCore>& obj, std::function<void()> func)
  147. {
  148. volatile std::shared_ptr<CoreObjectCore> objParam = obj; // Makes sure obj isn't optimized out?
  149. func();
  150. }
  151. void CoreObject::executeReturnGpuCommand(const SPtr<CoreObjectCore>& obj, std::function<void(AsyncOp&)> func, AsyncOp& op)
  152. {
  153. volatile std::shared_ptr<CoreObjectCore> objParam = obj; // Makes sure obj isn't optimized out?
  154. func(op);
  155. }
  156. }