BsCoreObject.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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<SPtr<CoreObject>> dependencies;
  76. Vector<IndividualCoreSyncData> syncData;
  77. UINT32 stackPos = 0;
  78. dependencies.push_back(getThisPtr());
  79. while (stackPos < dependencies.size())
  80. {
  81. SPtr<CoreObject> curObj = dependencies[stackPos];
  82. stackPos++;
  83. if (curObj->isCoreDirty())
  84. {
  85. SPtr<CoreObjectCore> destObj = curObj->getCore();
  86. if (destObj == nullptr)
  87. return;
  88. IndividualCoreSyncData data;
  89. data.allocator = allocator;
  90. data.destination = destObj;
  91. data.syncData = syncToCore(data.allocator);
  92. syncData.push_back(data);
  93. curObj->markCoreClean();
  94. }
  95. // Note: I don't check for recursion. Possible infinite loop if two objects
  96. // are dependent on one another.
  97. curObj->getCoreDependencies(dependencies);
  98. }
  99. std::function<void(const Vector<IndividualCoreSyncData>&)> callback =
  100. [](const Vector<IndividualCoreSyncData>& data)
  101. {
  102. for (auto& entry : data)
  103. {
  104. entry.destination->syncToCore(entry.syncData);
  105. UINT8* dataPtr = entry.syncData.getBuffer();
  106. if (dataPtr != nullptr)
  107. entry.allocator->dealloc(dataPtr);
  108. }
  109. };
  110. if (syncData.size() > 0)
  111. accessor.queueCommand(std::bind(callback, syncData));
  112. }
  113. void CoreObject::_setThisPtr(std::shared_ptr<CoreObject> ptrThis)
  114. {
  115. mThis = ptrThis;
  116. }
  117. void CoreObject::queueGpuCommand(const SPtr<CoreObjectCore>& obj, std::function<void()> func)
  118. {
  119. // We call another internal method and go through an additional layer of abstraction in order to keep an active
  120. // reference to the obj (saved in the bound function).
  121. // We could have called the function directly using "this" pointer but then we couldn't have used a shared_ptr for the object,
  122. // in which case there is a possibility that the object would be released and deleted while still being in the command queue.
  123. gCoreAccessor().queueCommand(std::bind(&CoreObject::executeGpuCommand, obj, func));
  124. }
  125. AsyncOp CoreObject::queueReturnGpuCommand(const SPtr<CoreObjectCore>& obj, std::function<void(AsyncOp&)> func)
  126. {
  127. // See queueGpuCommand
  128. return gCoreAccessor().queueReturnCommand(std::bind(&CoreObject::executeReturnGpuCommand, obj, func, _1));
  129. }
  130. void CoreObject::queueInitializeGpuCommand(const SPtr<CoreObjectCore>& obj)
  131. {
  132. std::function<void()> func = std::bind(&CoreObjectCore::initialize, obj.get());
  133. CoreThread::instance().queueCommand(std::bind(&CoreObject::executeGpuCommand, obj, func));
  134. }
  135. void CoreObject::queueDestroyGpuCommand(const SPtr<CoreObjectCore>& obj)
  136. {
  137. std::function<void()> func = [&](){}; // Do nothing function. We just need the shared pointer to stay alive until it reaches the core thread
  138. gCoreAccessor().queueCommand(std::bind(&CoreObject::executeGpuCommand, obj, func));
  139. }
  140. void CoreObject::executeGpuCommand(const SPtr<CoreObjectCore>& obj, std::function<void()> func)
  141. {
  142. volatile std::shared_ptr<CoreObjectCore> objParam = obj; // Makes sure obj isn't optimized out?
  143. func();
  144. }
  145. void CoreObject::executeReturnGpuCommand(const SPtr<CoreObjectCore>& obj, std::function<void(AsyncOp&)> func, AsyncOp& op)
  146. {
  147. volatile std::shared_ptr<CoreObjectCore> objParam = obj; // Makes sure obj isn't optimized out?
  148. func(op);
  149. }
  150. }