BsCoreObject.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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::synchronize()
  62. {
  63. if (mCoreSpecific != nullptr)
  64. mCoreSpecific->synchronize();
  65. }
  66. void CoreObject::syncToCore(CoreAccessor& accessor)
  67. {
  68. if (!isCoreDirty())
  69. return;
  70. SPtr<CoreObjectCore> destObj = getCore();
  71. if (destObj == nullptr)
  72. return;
  73. struct IndividualCoreSyncData
  74. {
  75. SPtr<CoreObjectCore> destination;
  76. CoreSyncData syncData;
  77. FrameAlloc* allocator;
  78. };
  79. IndividualCoreSyncData data;
  80. data.allocator = gCoreThread().getFrameAlloc();
  81. data.destination = destObj;
  82. data.syncData = syncToCore(data.allocator);
  83. std::function<void(const IndividualCoreSyncData&)> callback =
  84. [](const IndividualCoreSyncData& data)
  85. {
  86. data.destination->syncToCore(data.syncData);
  87. UINT8* dataPtr = data.syncData.getBuffer();
  88. if (dataPtr != nullptr)
  89. data.allocator->dealloc(dataPtr);
  90. };
  91. accessor.queueCommand(std::bind(callback, data));
  92. markCoreClean();
  93. }
  94. void CoreObject::_setThisPtr(std::shared_ptr<CoreObject> ptrThis)
  95. {
  96. mThis = ptrThis;
  97. }
  98. void CoreObject::queueGpuCommand(const SPtr<CoreObjectCore>& obj, std::function<void()> func)
  99. {
  100. // We call another internal method and go through an additional layer of abstraction in order to keep an active
  101. // reference to the obj (saved in the bound function).
  102. // We could have called the function directly using "this" pointer but then we couldn't have used a shared_ptr for the object,
  103. // in which case there is a possibility that the object would be released and deleted while still being in the command queue.
  104. gCoreAccessor().queueCommand(std::bind(&CoreObject::executeGpuCommand, obj, func));
  105. }
  106. AsyncOp CoreObject::queueReturnGpuCommand(const SPtr<CoreObjectCore>& obj, std::function<void(AsyncOp&)> func)
  107. {
  108. // See queueGpuCommand
  109. return gCoreAccessor().queueReturnCommand(std::bind(&CoreObject::executeReturnGpuCommand, obj, func, _1));
  110. }
  111. void CoreObject::queueInitializeGpuCommand(const SPtr<CoreObjectCore>& obj)
  112. {
  113. std::function<void()> func = std::bind(&CoreObjectCore::initialize, obj.get());
  114. CoreThread::instance().queueCommand(std::bind(&CoreObject::executeGpuCommand, obj, func));
  115. }
  116. void CoreObject::queueDestroyGpuCommand(const SPtr<CoreObjectCore>& obj)
  117. {
  118. std::function<void()> func = [&](){}; // Do nothing function. We just need the shared pointer to stay alive until it reaches the core thread
  119. gCoreAccessor().queueCommand(std::bind(&CoreObject::executeGpuCommand, obj, func));
  120. }
  121. void CoreObject::executeGpuCommand(const SPtr<CoreObjectCore>& obj, std::function<void()> func)
  122. {
  123. volatile std::shared_ptr<CoreObjectCore> objParam = obj; // Makes sure obj isn't optimized out?
  124. func();
  125. }
  126. void CoreObject::executeReturnGpuCommand(const SPtr<CoreObjectCore>& obj, std::function<void(AsyncOp&)> func, AsyncOp& op)
  127. {
  128. volatile std::shared_ptr<CoreObjectCore> objParam = obj; // Makes sure obj isn't optimized out?
  129. func(op);
  130. }
  131. }