CmCoreGpuObject.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. queueGpuCommand(mThis.lock(), boost::bind(&CoreGpuObject::destroy_internal, this));
  35. }
  36. void CoreGpuObject::destroy_internal()
  37. {
  38. #if CM_DEBUG_MODE
  39. if(!isInitialized())
  40. CM_EXCEPT(InternalErrorException, "Trying to destroy an object that is already destroyed (or it never was initialized).");
  41. #endif
  42. setIsInitialized(false);
  43. }
  44. void CoreGpuObject::initialize()
  45. {
  46. #if CM_DEBUG_MODE
  47. if(isInitialized() || isScheduledToBeInitialized())
  48. CM_EXCEPT(InternalErrorException, "Trying to initialize an object that is already initialized.");
  49. #endif
  50. setScheduledToBeInitialized(true);
  51. queueGpuCommand(mThis.lock(), boost::bind(&CoreGpuObject::initialize_internal, this));
  52. }
  53. void CoreGpuObject::initialize_internal()
  54. {
  55. {
  56. CM_LOCK_MUTEX(mCoreGpuObjectLoadedMutex);
  57. setIsInitialized(true);
  58. }
  59. setScheduledToBeInitialized(false);
  60. CM_THREAD_NOTIFY_ALL(mCoreGpuObjectLoadedCondition);
  61. }
  62. void CoreGpuObject::waitUntilInitialized()
  63. {
  64. #if CM_DEBUG_MODE
  65. if(CM_THREAD_CURRENT_ID == RenderSystem::instancePtr()->getRenderThreadId())
  66. CM_EXCEPT(InternalErrorException, "You cannot call this method on the render thread. It will cause a deadlock!");
  67. #endif
  68. if(!isInitialized())
  69. {
  70. CM_LOCK_MUTEX_NAMED(mCoreGpuObjectLoadedMutex, lock);
  71. while(!isInitialized())
  72. {
  73. if(!isScheduledToBeInitialized())
  74. CM_EXCEPT(InternalErrorException, "Attempting to wait until initialization finishes but object is not scheduled to be initialized.");
  75. CM_THREAD_WAIT(mCoreGpuObjectLoadedCondition, mCoreGpuObjectLoadedMutex, lock);
  76. }
  77. }
  78. }
  79. void CoreGpuObject::setThisPtr(std::shared_ptr<CoreGpuObject> ptrThis)
  80. {
  81. mThis = ptrThis;
  82. }
  83. void CoreGpuObject::_deleteDelayed(CoreGpuObject* obj)
  84. {
  85. assert(obj != nullptr);
  86. // This method usually gets called automatically by the shared pointer when all references are released. The process:
  87. // - If the object wasn't initialized delete it right away
  88. // - Otherwise:
  89. // - We re-create the reference to the object by setting mThis pointer
  90. // - We queue the object to be destroyed so all of its GPU resources may be released on the render thread
  91. // - destroy() makes sure it keeps a reference of mThis so object isn't deleted
  92. // - Once the destroy() finishes the reference is removed and the default shared_ptr deleter is called
  93. #if CM_DEBUG_MODE
  94. if(obj->isScheduledToBeInitialized())
  95. {
  96. CM_EXCEPT(InternalErrorException, "Object scheduled to be initialized, yet it's being deleted. " \
  97. "By design objects queued in the command queue should always have a reference count >= 1, therefore never be deleted " \
  98. "while still in the queue.");
  99. }
  100. #endif
  101. if(obj->isInitialized())
  102. {
  103. std::shared_ptr<CoreGpuObject> thisPtr(obj);
  104. obj->setThisPtr(thisPtr);
  105. obj->destroy();
  106. }
  107. else
  108. {
  109. delete obj;
  110. }
  111. }
  112. void CoreGpuObject::queueGpuCommand(std::shared_ptr<CoreGpuObject>& obj, boost::function<void()> func)
  113. {
  114. // We call another internal method and go through an additional layer of abstraction in order to keep an active
  115. // reference to the obj (saved in the bound function).
  116. // We could have called the function directly using "this" pointer but then we couldn't have used a shared_ptr for the object,
  117. // in which case there is a possibility that the object would be released and deleted while still being in the command queue.
  118. RenderSystem::instancePtr()->queueCommand(boost::bind(&CoreGpuObject::executeGpuCommand, obj, func));
  119. }
  120. AsyncOp CoreGpuObject::queueReturnGpuCommand(std::shared_ptr<CoreGpuObject>& obj, boost::function<void(AsyncOp&)> func)
  121. {
  122. // See queueGpuCommand
  123. return RenderSystem::instancePtr()->queueReturnCommand(boost::bind(&CoreGpuObject::executeReturnGpuCommand, obj, func, _1));
  124. }
  125. void CoreGpuObject::executeGpuCommand(std::shared_ptr<CoreGpuObject>& obj, boost::function<void()> func)
  126. {
  127. volatile std::shared_ptr<CoreGpuObject> objParam = obj; // Makes sure obj isn't optimized out?
  128. func();
  129. }
  130. void CoreGpuObject::executeReturnGpuCommand(std::shared_ptr<CoreGpuObject>& obj, boost::function<void(AsyncOp&)> func, AsyncOp& op)
  131. {
  132. volatile std::shared_ptr<CoreGpuObject> objParam = obj; // Makes sure obj isn't optimized out?
  133. func(op);
  134. }
  135. }