BsCoreObject.cpp 5.2 KB

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