CmCoreGpuObjectManager.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "CmCoreGpuObjectManager.h"
  2. #include "CmCoreGpuObject.h"
  3. #include "CmException.h"
  4. namespace CamelotEngine
  5. {
  6. CoreGpuObjectManager::CoreGpuObjectManager()
  7. :mNextAvailableID(1)
  8. {
  9. }
  10. CoreGpuObjectManager::~CoreGpuObjectManager()
  11. {
  12. CM_LOCK_MUTEX(mObjectsMutex);
  13. if(mObjects.size() > 0)
  14. {
  15. // All objects MUST be destroyed at this point, otherwise there might be memory corruption.
  16. // (Reason: This is called on application shutdown and at that point we also unload any dynamic libraries,
  17. // which will invalidate any pointers to objects created from those libraries. Therefore we require of the user to
  18. // clean up all objects manually before shutting down the application).
  19. CM_EXCEPT(InternalErrorException, "Core GPU object manager destroyed, but not all objects were released. User must release ALL " \
  20. "engine objects before application shutdown.");
  21. }
  22. if(mObjectsToDestroy.size() > 0)
  23. {
  24. // This should never happen as higher levels of the engine make sure all scheduled objects are destroyed before shutdown is initialized.
  25. CM_EXCEPT(InternalErrorException, "Objects scheduled for destruction, but shutdown initialized before destruction completed.");
  26. }
  27. }
  28. UINT64 CoreGpuObjectManager::registerObject(CoreGpuObject* object)
  29. {
  30. assert(object != nullptr);
  31. CM_LOCK_MUTEX(mObjectsMutex);
  32. mObjects[mNextAvailableID] = object;
  33. return mNextAvailableID++;
  34. }
  35. void CoreGpuObjectManager::unregisterObject(CoreGpuObject* object)
  36. {
  37. assert(object != nullptr);
  38. CM_LOCK_MUTEX(mObjectsMutex);
  39. mObjects.erase(object->getInternalID());
  40. }
  41. void CoreGpuObjectManager::registerObjectToDestroy(std::shared_ptr<CoreGpuObject> object)
  42. {
  43. CM_LOCK_MUTEX(mObjectsToDestroyMutex);
  44. mObjectsToDestroy[object->getInternalID()] = object;
  45. }
  46. void CoreGpuObjectManager::unregisterObjectToDestroy(std::shared_ptr<CoreGpuObject> object)
  47. {
  48. CM_LOCK_MUTEX(mObjectsToDestroyMutex);
  49. mObjectsToDestroy.erase(object->getInternalID());
  50. }
  51. }