BsCoreObject.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "BsCoreObject.h"
  2. #include "BsCoreObjectCore.h"
  3. #include "BsCoreThread.h"
  4. #include "BsCoreObjectManager.h"
  5. #include "BsCoreThreadAccessor.h"
  6. using namespace std::placeholders;
  7. namespace BansheeEngine
  8. {
  9. CoreObject::CoreObject(bool initializeOnCoreThread)
  10. : mFlags(0), mInternalID(0), mCoreDirtyFlags(0xFFFFFFFF)
  11. {
  12. mInternalID = CoreObjectManager::instance().registerObject(this);
  13. mFlags = initializeOnCoreThread ? mFlags | CGO_INIT_ON_CORE_THREAD : mFlags;
  14. }
  15. CoreObject::~CoreObject()
  16. {
  17. if(!isDestroyed())
  18. {
  19. // Object must be released with destroy() otherwise engine can still try to use it, even if it was destructed
  20. // (e.g. if an object has one of its methods queued in a command queue, and is destructed, you will be accessing invalid memory)
  21. BS_EXCEPT(InternalErrorException, "Destructor called but object is not destroyed. This will result in nasty issues.");
  22. }
  23. #if BS_DEBUG_MODE
  24. if(!mThis.expired())
  25. {
  26. BS_EXCEPT(InternalErrorException, "Shared pointer to this object still has active references but " \
  27. "the object is being deleted? You shouldn't delete CoreObjects manually.");
  28. }
  29. #endif
  30. }
  31. void CoreObject::destroy()
  32. {
  33. CoreObjectManager::instance().unregisterObject(this);
  34. setIsDestroyed(true);
  35. if(requiresInitOnCoreThread())
  36. {
  37. assert(BS_THREAD_CURRENT_ID != CoreThread::instance().getCoreThreadId() && "Cannot destroy sim thead object from core thread.");
  38. // This will only destroy the CoreObjectCore if this was the last reference
  39. queueDestroyGpuCommand(mCoreSpecific);
  40. }
  41. mCoreSpecific = nullptr;
  42. }
  43. void CoreObject::initialize()
  44. {
  45. mCoreSpecific = createCore();
  46. if (mCoreSpecific != nullptr)
  47. {
  48. if (requiresInitOnCoreThread())
  49. {
  50. mCoreSpecific->setScheduledToBeInitialized(true);
  51. assert(BS_THREAD_CURRENT_ID != CoreThread::instance().getCoreThreadId() && "Cannot initialize sim thread object from core thread.");
  52. queueInitializeGpuCommand(mCoreSpecific);
  53. }
  54. else
  55. {
  56. mCoreSpecific->initialize();
  57. // Even though this object might not require initialization on the core thread, it will be used on it, therefore
  58. // do a memory barrier to ensure any stores are finished before continuing (When it requires init on core thread
  59. // we use the core accessor which uses a mutex, and therefore executes all stores as well, so we dont need to
  60. // do this explicitly)
  61. std::atomic_thread_fence(std::memory_order_release);
  62. }
  63. }
  64. markDependenciesDirty();
  65. }
  66. void CoreObject::blockUntilCoreInitialized()
  67. {
  68. if (mCoreSpecific != nullptr)
  69. mCoreSpecific->synchronize();
  70. }
  71. void CoreObject::syncToCore(CoreAccessor& accessor)
  72. {
  73. CoreObjectManager::instance().syncToCore(this, accessor);
  74. }
  75. void CoreObject::markCoreDirty(UINT32 flags)
  76. {
  77. bool wasDirty = isCoreDirty();
  78. mCoreDirtyFlags |= flags;
  79. if (!wasDirty && isCoreDirty())
  80. CoreObjectManager::instance().notifyCoreDirty(this);
  81. }
  82. void CoreObject::markDependenciesDirty()
  83. {
  84. CoreObjectManager::instance().notifyDependenciesDirty(this);
  85. }
  86. void CoreObject::_setThisPtr(std::shared_ptr<CoreObject> ptrThis)
  87. {
  88. mThis = ptrThis;
  89. }
  90. void CoreObject::queueGpuCommand(const SPtr<CoreObjectCore>& obj, std::function<void()> func)
  91. {
  92. // We call another internal method and go through an additional layer of abstraction in order to keep an active
  93. // reference to the obj (saved in the bound function).
  94. // We could have called the function directly using "this" pointer but then we couldn't have used a shared_ptr for the object,
  95. // in which case there is a possibility that the object would be released and deleted while still being in the command queue.
  96. gCoreAccessor().queueCommand(std::bind(&CoreObject::executeGpuCommand, obj, func));
  97. }
  98. AsyncOp CoreObject::queueReturnGpuCommand(const SPtr<CoreObjectCore>& obj, std::function<void(AsyncOp&)> func)
  99. {
  100. // See queueGpuCommand
  101. return gCoreAccessor().queueReturnCommand(std::bind(&CoreObject::executeReturnGpuCommand, obj, func, _1));
  102. }
  103. void CoreObject::queueInitializeGpuCommand(const SPtr<CoreObjectCore>& obj)
  104. {
  105. std::function<void()> func = std::bind(&CoreObjectCore::initialize, obj.get());
  106. CoreThread::instance().queueCommand(std::bind(&CoreObject::executeGpuCommand, obj, func));
  107. }
  108. void CoreObject::queueDestroyGpuCommand(const SPtr<CoreObjectCore>& obj)
  109. {
  110. std::function<void()> func = [&](){}; // Do nothing function. We just need the shared pointer to stay alive until it reaches the core thread
  111. gCoreAccessor().queueCommand(std::bind(&CoreObject::executeGpuCommand, obj, func));
  112. }
  113. void CoreObject::executeGpuCommand(const SPtr<CoreObjectCore>& obj, std::function<void()> func)
  114. {
  115. volatile std::shared_ptr<CoreObjectCore> objParam = obj; // Makes sure obj isn't optimized out?
  116. func();
  117. }
  118. void CoreObject::executeReturnGpuCommand(const SPtr<CoreObjectCore>& obj, std::function<void(AsyncOp&)> func, AsyncOp& op)
  119. {
  120. volatile std::shared_ptr<CoreObjectCore> objParam = obj; // Makes sure obj isn't optimized out?
  121. func(op);
  122. }
  123. }