CmCoreObject.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmAsyncOp.h"
  4. #include "boost/function.hpp"
  5. namespace CamelotEngine
  6. {
  7. /**
  8. * @brief This class provides some common functionality that all low-level GPU-related objects
  9. * need to implement.
  10. *
  11. * @note This involves initializing, keeping track of, and releasing all GPU resources.
  12. * All core GPU objects are initialized on the render thread, and destroyed on the render thread,
  13. * so majority of these methods will just schedule object initialization/destruction.
  14. */
  15. class CM_EXPORT CoreObject
  16. {
  17. protected:
  18. enum Flags
  19. {
  20. CGO_INITIALIZED = 0x01,
  21. CGO_INIT_ON_RENDER_THREAD = 0x02,
  22. CGO_SCHEDULED_FOR_INIT = 0x04,
  23. CGO_SCHEDULED_FOR_DELETE = 0x08
  24. };
  25. public:
  26. CoreObject(bool requiresGpuInit = true);
  27. virtual ~CoreObject();
  28. /**
  29. * @brief Destroys all GPU resources of this object.
  30. o *
  31. * @note Destruction is not done immediately, and is instead just scheduled on the
  32. * render thread. Unless called from render thread in which case it is executed right away.
  33. */
  34. virtual void destroy();
  35. /**
  36. * @brief Initializes all the internal resources of this object. Should be called by the
  37. * factory creation methods automatically after construction and not by user directly.
  38. *
  39. * @note Initialization is not done immediately, and is instead just scheduled on the
  40. * render thread. Unless called from render thread in which case it is executed right away.
  41. */
  42. virtual void initialize();
  43. /**
  44. * @brief Returns true if the object has been properly initialized. You are not
  45. * allowed to call any methods on the resource until you are sure resource is initialized.
  46. */
  47. bool isInitialized() const { return (mFlags & CGO_INITIALIZED) != 0; }
  48. /**
  49. * @brief Blocks the current thread until the resource is fully initialized.
  50. * If you call this without calling initialize first a deadlock will occurr.
  51. */
  52. void waitUntilInitialized();
  53. /**
  54. * @brief Sets a shared this pointer to this object. This MUST be called immediately after construction.
  55. *
  56. * @note Called automatically by the factory creation methods so user should not call this manually.
  57. */
  58. void setThisPtr(std::shared_ptr<CoreObject> ptrThis);
  59. /**
  60. * @brief Returns an unique identifier for this object.
  61. */
  62. UINT64 getInternalID() const { return mInternalID; }
  63. /**
  64. * @brief Schedules the object to be destroyed, and then deleted.
  65. *
  66. * @note You should never call this manually. It's meant for internal use only.
  67. */
  68. template<class T, class MemAlloc>
  69. static void _deleteDelayed(CoreObject* obj)
  70. {
  71. _deleteDelayedInternal(obj);
  72. if(obj->isInitialized())
  73. {
  74. std::shared_ptr<CoreObject> thisPtr(obj);
  75. obj->setThisPtr(thisPtr);
  76. obj->destroy();
  77. }
  78. else
  79. {
  80. CM_DELETE((T*)obj, T, MemAlloc);
  81. }
  82. }
  83. protected:
  84. /**
  85. * @brief Frees all of the objects dynamically allocated memory. All derived classes that have something to free
  86. * should do it here instead of their destructor. All derived classes need to call this base method when they're done.
  87. *
  88. * @note Since this is scheduled to be executed on the render thread, normally you want to destroy all GPU specific resources here.
  89. */
  90. virtual void destroy_internal();
  91. /**
  92. * @brief Initializes all the internal resources of this object. Needs to be called before doing
  93. * any operations with the object. All derived classes also need to call this base method.
  94. *
  95. * @note Since this is scheduled to be executed on the render thread, normally you want to initialize all GPU specific resources here.
  96. */
  97. virtual void initialize_internal();
  98. /**
  99. * @brief Returns a shared_ptr version of "this" pointer.
  100. */
  101. std::shared_ptr<CoreObject> getThisPtr() const { return mThis.lock(); }
  102. static void _deleteDelayedInternal(CoreObject* obj);
  103. /**
  104. * @brief Queues a command to be executed on the render thread, without a return value.
  105. *
  106. * @note Requires a shared pointer to the object this function will be executed on, in order to
  107. * make sure the object is not deleted before the command executes. Can be null if the
  108. * function is static or global.
  109. */
  110. static void queueGpuCommand(std::shared_ptr<CoreObject>& obj, boost::function<void()> func);
  111. /**
  112. * @brief Queues a command to be executed on the render thread, with a return value in the form of AsyncOp.
  113. *
  114. * @see AsyncOp
  115. *
  116. * @note Requires a shared pointer to the object this function will be executed on, in order to
  117. * make sure the object is not deleted before the command executes. Can be null if the
  118. * function is static or global.
  119. */
  120. static AsyncOp queueReturnGpuCommand(std::shared_ptr<CoreObject>& obj, boost::function<void(AsyncOp&)> func);
  121. bool isScheduledToBeInitialized() const { return (mFlags & CGO_SCHEDULED_FOR_INIT) != 0; }
  122. bool isScheduledToBeDeleted() const { return (mFlags & CGO_SCHEDULED_FOR_DELETE) != 0; }
  123. bool requiresInitOnRenderThread() const { return (mFlags & CGO_INIT_ON_RENDER_THREAD) != 0; }
  124. void setIsInitialized(bool initialized) { mFlags = initialized ? mFlags | CGO_INITIALIZED : mFlags & ~CGO_INITIALIZED; }
  125. void setScheduledToBeInitialized(bool scheduled) { mFlags = scheduled ? mFlags | CGO_SCHEDULED_FOR_INIT : mFlags & ~CGO_SCHEDULED_FOR_INIT; }
  126. void setScheduledToBeDeleted(bool scheduled) { mFlags = scheduled ? mFlags | CGO_SCHEDULED_FOR_DELETE : mFlags & ~CGO_SCHEDULED_FOR_DELETE; }
  127. private:
  128. friend class CoreGpuObjectManager;
  129. volatile UINT8 mFlags;
  130. UINT64 mInternalID; // ID == 0 is not a valid ID
  131. std::weak_ptr<CoreObject> mThis;
  132. CM_STATIC_THREAD_SYNCHRONISER(mCoreGpuObjectLoadedCondition)
  133. CM_STATIC_MUTEX(mCoreGpuObjectLoadedMutex)
  134. static void executeGpuCommand(std::shared_ptr<CoreObject>& obj, boost::function<void()> func);
  135. static void executeReturnGpuCommand(std::shared_ptr<CoreObject>& obj, boost::function<void(AsyncOp&)> func, AsyncOp& op);
  136. };
  137. }