BsCoreObject.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. }
  33. void CoreObject::destroy()
  34. {
  35. CoreObjectManager::instance().unregisterObject(this);
  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. // This will only destroy the CoreObjectCore if this was the last reference
  41. queueDestroyGpuCommand(mCoreSpecific);
  42. }
  43. mCoreSpecific = nullptr;
  44. }
  45. void CoreObject::initialize()
  46. {
  47. mCoreSpecific = createCore();
  48. if (mCoreSpecific != nullptr)
  49. {
  50. if (requiresInitOnCoreThread())
  51. {
  52. mCoreSpecific->setScheduledToBeInitialized(true);
  53. assert(BS_THREAD_CURRENT_ID != CoreThread::instance().getCoreThreadId() && "Cannot initialize sim thread object from core thread.");
  54. queueInitializeGpuCommand(mCoreSpecific);
  55. }
  56. else
  57. {
  58. mCoreSpecific->initialize();
  59. // Even though this object might not require initialization on the core thread, it will be used on it, therefore
  60. // do a memory barrier to ensure any stores are finished before continuing (When it requires init on core thread
  61. // we use the core accessor which uses a mutex, and therefore executes all stores as well, so we dont need to
  62. // do this explicitly)
  63. std::atomic_thread_fence(std::memory_order_release);
  64. }
  65. }
  66. markDependenciesDirty();
  67. }
  68. void CoreObject::blockUntilCoreInitialized()
  69. {
  70. if (mCoreSpecific != nullptr)
  71. mCoreSpecific->synchronize();
  72. }
  73. void CoreObject::syncToCore(CoreAccessor& accessor)
  74. {
  75. CoreObjectManager::instance().syncToCore(this, accessor);
  76. }
  77. void CoreObject::markCoreDirty(UINT32 flags)
  78. {
  79. bool wasDirty = isCoreDirty();
  80. mCoreDirtyFlags |= flags;
  81. if (!wasDirty && isCoreDirty())
  82. CoreObjectManager::instance().notifyCoreDirty(this);
  83. }
  84. void CoreObject::markDependenciesDirty()
  85. {
  86. CoreObjectManager::instance().notifyDependenciesDirty(this);
  87. }
  88. void CoreObject::_setThisPtr(std::shared_ptr<CoreObject> ptrThis)
  89. {
  90. mThis = ptrThis;
  91. }
  92. void CoreObject::queueGpuCommand(const SPtr<CoreObjectCore>& obj, std::function<void()> func)
  93. {
  94. // We call another internal method and go through an additional layer of abstraction in order to keep an active
  95. // reference to the obj (saved in the bound function).
  96. // We could have called the function directly using "this" pointer but then we couldn't have used a shared_ptr for the object,
  97. // in which case there is a possibility that the object would be released and deleted while still being in the command queue.
  98. gCoreAccessor().queueCommand(std::bind(&CoreObject::executeGpuCommand, obj, func));
  99. }
  100. AsyncOp CoreObject::queueReturnGpuCommand(const SPtr<CoreObjectCore>& obj, std::function<void(AsyncOp&)> func)
  101. {
  102. // See queueGpuCommand
  103. return gCoreAccessor().queueReturnCommand(std::bind(&CoreObject::executeReturnGpuCommand, obj, func, _1));
  104. }
  105. void CoreObject::queueInitializeGpuCommand(const SPtr<CoreObjectCore>& obj)
  106. {
  107. std::function<void()> func = std::bind(&CoreObjectCore::initialize, obj.get());
  108. CoreThread::instance().queueCommand(std::bind(&CoreObject::executeGpuCommand, obj, func));
  109. }
  110. void CoreObject::queueDestroyGpuCommand(const SPtr<CoreObjectCore>& obj)
  111. {
  112. std::function<void()> func = [&](){}; // Do nothing function. We just need the shared pointer to stay alive until it reaches the core thread
  113. gCoreAccessor().queueCommand(std::bind(&CoreObject::executeGpuCommand, obj, func));
  114. }
  115. void CoreObject::executeGpuCommand(const SPtr<CoreObjectCore>& obj, std::function<void()> func)
  116. {
  117. volatile std::shared_ptr<CoreObjectCore> objParam = obj; // Makes sure obj isn't optimized out?
  118. func();
  119. }
  120. void CoreObject::executeReturnGpuCommand(const SPtr<CoreObjectCore>& obj, std::function<void(AsyncOp&)> func, AsyncOp& op)
  121. {
  122. volatile std::shared_ptr<CoreObjectCore> objParam = obj; // Makes sure obj isn't optimized out?
  123. func(op);
  124. }
  125. }