CmCoreGpuObject.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "CmCoreGpuObject.h"
  2. #include "CmRenderSystem.h"
  3. #include "CmCoreGpuObjectManager.h"
  4. #include "CmDebug.h"
  5. namespace CamelotEngine
  6. {
  7. CM_STATIC_THREAD_SYNCHRONISER_CLASS_INSTANCE(mCoreGpuObjectLoadedCondition, CoreGpuObject)
  8. CM_STATIC_MUTEX_CLASS_INSTANCE(mCoreGpuObjectLoadedMutex, CoreGpuObject)
  9. CoreGpuObject::CoreGpuObject()
  10. : mFlags(0), mInternalID(0)
  11. {
  12. mInternalID = CoreGpuObjectManager::instance().registerObject(this);
  13. }
  14. CoreGpuObject::~CoreGpuObject()
  15. {
  16. if(isInitialized())
  17. {
  18. // Object must be released with destroy() otherwise engine can still try to use it, even if it was destructed
  19. // (e.g. if an object has one of its methods queued in a command queue, and is destructed, you will be accessing invalid memory)
  20. CM_EXCEPT(InternalErrorException, "Destructor called but object is not destroyed. This will result in nasty issues.");
  21. }
  22. #if CM_DEBUG_MODE
  23. if(!mThis.expired())
  24. {
  25. CM_EXCEPT(InternalErrorException, "Shared pointer to this object still has active references but " \
  26. "the object is being deleted? You shouldn't delete CoreGpuObjects manually.");
  27. }
  28. #endif
  29. CoreGpuObjectManager::instance().unregisterObject(this);
  30. }
  31. void CoreGpuObject::destroy()
  32. {
  33. setScheduledToBeDeleted(true);
  34. CoreGpuObjectManager::instance().registerObjectToDestroy(mThis.lock());
  35. queueGpuCommand(mThis.lock(), boost::bind(&CoreGpuObject::destroy_internal, this));
  36. }
  37. void CoreGpuObject::destroy_internal()
  38. {
  39. #if CM_DEBUG_MODE
  40. if(!isInitialized())
  41. {
  42. CoreGpuObjectManager::instance().unregisterObjectToDestroy(mThis.lock());
  43. CM_EXCEPT(InternalErrorException, "Trying to destroy an object that is already destroyed (or it never was initialized).");
  44. }
  45. #endif
  46. setIsInitialized(false);
  47. CoreGpuObjectManager::instance().unregisterObjectToDestroy(mThis.lock());
  48. }
  49. void CoreGpuObject::initialize()
  50. {
  51. #if CM_DEBUG_MODE
  52. if(isInitialized() || isScheduledToBeInitialized())
  53. CM_EXCEPT(InternalErrorException, "Trying to initialize an object that is already initialized.");
  54. #endif
  55. setScheduledToBeInitialized(true);
  56. queueGpuCommand(mThis.lock(), boost::bind(&CoreGpuObject::initialize_internal, this));
  57. }
  58. void CoreGpuObject::initialize_internal()
  59. {
  60. {
  61. CM_LOCK_MUTEX(mCoreGpuObjectLoadedMutex);
  62. setIsInitialized(true);
  63. }
  64. setScheduledToBeInitialized(false);
  65. CM_THREAD_NOTIFY_ALL(mCoreGpuObjectLoadedCondition);
  66. }
  67. void CoreGpuObject::waitUntilInitialized()
  68. {
  69. #if CM_DEBUG_MODE
  70. if(CM_THREAD_CURRENT_ID == RenderSystem::instancePtr()->getRenderThreadId())
  71. CM_EXCEPT(InternalErrorException, "You cannot call this method on the render thread. It will cause a deadlock!");
  72. #endif
  73. if(!isInitialized())
  74. {
  75. CM_LOCK_MUTEX_NAMED(mCoreGpuObjectLoadedMutex, lock);
  76. while(!isInitialized())
  77. {
  78. if(!isScheduledToBeInitialized())
  79. CM_EXCEPT(InternalErrorException, "Attempting to wait until initialization finishes but object is not scheduled to be initialized.");
  80. CM_THREAD_WAIT(mCoreGpuObjectLoadedCondition, mCoreGpuObjectLoadedMutex, lock);
  81. }
  82. }
  83. }
  84. void CoreGpuObject::setThisPtr(std::shared_ptr<CoreGpuObject> ptrThis)
  85. {
  86. mThis = ptrThis;
  87. }
  88. void CoreGpuObject::_deleteDelayed(CoreGpuObject* obj)
  89. {
  90. assert(obj != nullptr);
  91. // This method usually gets called automatically by the shared pointer when all references are released. The process:
  92. // - If the object wasn't initialized delete it right away
  93. // - Otherwise:
  94. // - We re-create the reference to the object by setting mThis pointer
  95. // - We queue the object to be destroyed so all of its GPU resources may be released on the render thread
  96. // - destroy() makes sure it keeps a reference of mThis so object isn't deleted
  97. // - Once the destroy() finishes the reference is removed and the default shared_ptr deleter is called
  98. #if CM_DEBUG_MODE
  99. if(obj->isScheduledToBeInitialized())
  100. {
  101. CM_EXCEPT(InternalErrorException, "Object scheduled to be initialized, yet it's being deleted. " \
  102. "By design objects queued in the command queue should always have a reference count >= 1, therefore never be deleted " \
  103. "while still in the queue.");
  104. }
  105. #endif
  106. if(obj->isInitialized())
  107. {
  108. std::shared_ptr<CoreGpuObject> thisPtr(obj);
  109. obj->setThisPtr(thisPtr);
  110. obj->destroy();
  111. }
  112. else
  113. {
  114. delete obj;
  115. }
  116. }
  117. void CoreGpuObject::queueGpuCommand(std::shared_ptr<CoreGpuObject>& obj, boost::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. RenderSystem::instancePtr()->queueCommand(boost::bind(&CoreGpuObject::executeGpuCommand, obj, func));
  124. }
  125. AsyncOp CoreGpuObject::queueReturnGpuCommand(std::shared_ptr<CoreGpuObject>& obj, boost::function<void(AsyncOp&)> func)
  126. {
  127. // See queueGpuCommand
  128. return RenderSystem::instancePtr()->queueReturnCommand(boost::bind(&CoreGpuObject::executeReturnGpuCommand, obj, func, _1));
  129. }
  130. void CoreGpuObject::executeGpuCommand(std::shared_ptr<CoreGpuObject>& obj, boost::function<void()> func)
  131. {
  132. volatile std::shared_ptr<CoreGpuObject> objParam = obj; // Makes sure obj isn't optimized out?
  133. func();
  134. }
  135. void CoreGpuObject::executeReturnGpuCommand(std::shared_ptr<CoreGpuObject>& obj, boost::function<void(AsyncOp&)> func, AsyncOp& op)
  136. {
  137. volatile std::shared_ptr<CoreGpuObject> objParam = obj; // Makes sure obj isn't optimized out?
  138. func(op);
  139. }
  140. }