CmCoreGpuObject.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. : mIsInitialized(false), mInternalID(0)
  11. {
  12. mInternalID = CoreGpuObjectManager::instance().registerObject(this);
  13. }
  14. CoreGpuObject::~CoreGpuObject()
  15. {
  16. if(mIsInitialized)
  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. CoreGpuObjectManager::instance().registerObjectToDestroy(mThis.lock());
  34. RenderSystem::instancePtr()->queueCommand(boost::bind(&CoreGpuObject::destroy_internal, this));
  35. }
  36. void CoreGpuObject::destroy_internal()
  37. {
  38. #if CM_DEBUG_MODE
  39. if(!mIsInitialized)
  40. {
  41. CoreGpuObjectManager::instance().unregisterObjectToDestroy(mThis.lock());
  42. CM_EXCEPT(InternalErrorException, "Trying to destroy an object that is already destroyed (or it never was initialized).");
  43. }
  44. #endif
  45. mIsInitialized = false;
  46. CoreGpuObjectManager::instance().unregisterObjectToDestroy(mThis.lock());
  47. }
  48. void CoreGpuObject::initialize()
  49. {
  50. #if CM_DEBUG_MODE
  51. if(mIsInitialized)
  52. CM_EXCEPT(InternalErrorException, "Trying to initialize an object that is already initialized");
  53. #endif
  54. RenderSystem::instancePtr()->queueCommand(boost::bind(&CoreGpuObject::initialize_internal, this));
  55. }
  56. void CoreGpuObject::initialize_internal()
  57. {
  58. {
  59. CM_LOCK_MUTEX(mCoreGpuObjectLoadedMutex);
  60. mIsInitialized = true;
  61. }
  62. CM_THREAD_NOTIFY_ALL(mCoreGpuObjectLoadedCondition);
  63. }
  64. void CoreGpuObject::waitUntilInitialized()
  65. {
  66. #if CM_DEBUG_MODE
  67. if(CM_THREAD_CURRENT_ID == RenderSystem::instancePtr()->getRenderThreadId())
  68. CM_EXCEPT(InternalErrorException, "You cannot call this method on the render thread. It will cause a deadlock!");
  69. #endif
  70. if(!mIsInitialized)
  71. {
  72. CM_LOCK_MUTEX_NAMED(mCoreGpuObjectLoadedMutex, lock);
  73. while(!mIsInitialized)
  74. {
  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. }