CmCoreGpuObject.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. LOGWRN("Destructor called but object is not destroyed. Object will leak.")
  19. }
  20. CoreGpuObjectManager::instance().unregisterObject(this);
  21. }
  22. void CoreGpuObject::destroy()
  23. {
  24. #if CM_DEBUG_MODE
  25. if(!mIsInitialized)
  26. CM_EXCEPT(InternalErrorException, "Trying to destroy an object that is already destroyed (or it never was initialized).");
  27. #endif
  28. CoreGpuObjectManager::instance().registerObjectToDestroy(mThis.lock());
  29. RenderSystem::instancePtr()->queueCommand(boost::bind(&CoreGpuObject::destroy_internal, this));
  30. }
  31. void CoreGpuObject::destroy_internal()
  32. {
  33. mIsInitialized = false;
  34. CoreGpuObjectManager::instance().unregisterObjectToDestroy(mThis.lock());
  35. }
  36. void CoreGpuObject::initialize()
  37. {
  38. #if CM_DEBUG_MODE
  39. if(mIsInitialized)
  40. CM_EXCEPT(InternalErrorException, "Trying to initialize an object that is already initialized");
  41. #endif
  42. RenderSystem::instancePtr()->queueCommand(boost::bind(&CoreGpuObject::initialize_internal, this));
  43. }
  44. void CoreGpuObject::initialize_internal()
  45. {
  46. {
  47. CM_LOCK_MUTEX(mCoreGpuObjectLoadedMutex);
  48. mIsInitialized = true;
  49. }
  50. CM_THREAD_NOTIFY_ALL(mCoreGpuObjectLoadedCondition);
  51. }
  52. void CoreGpuObject::waitUntilInitialized()
  53. {
  54. #if CM_DEBUG_MODE
  55. if(CM_THREAD_CURRENT_ID == RenderSystem::instancePtr()->getRenderThreadId())
  56. CM_EXCEPT(InternalErrorException, "You cannot call this method on the render thread. It will cause a deadlock!");
  57. #endif
  58. if(!mIsInitialized)
  59. {
  60. CM_LOCK_MUTEX_NAMED(mCoreGpuObjectLoadedMutex, lock);
  61. while(!mIsInitialized)
  62. {
  63. CM_THREAD_WAIT(mCoreGpuObjectLoadedCondition, mCoreGpuObjectLoadedMutex, lock);
  64. }
  65. }
  66. }
  67. void CoreGpuObject::setThisPtr(std::shared_ptr<CoreGpuObject> ptrThis)
  68. {
  69. mThis = ptrThis;
  70. }
  71. }