CmCoreObject.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmAsyncOp.h"
  4. #include <boost/preprocessor.hpp>
  5. namespace CamelotFramework
  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 core thread, and destroyed on the core 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. * core thread. Unless called from core 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. * core thread. Unless called from core 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. * @note Normally CPU objects are initialized on creation and this will never be false, and GPU
  48. * objects are initialized when the Core Thread processes them.
  49. */
  50. bool isInitialized() const { return (mFlags & CGO_INITIALIZED) != 0; }
  51. /**
  52. * @brief Blocks the current thread until the resource is fully initialized.
  53. * If you call this without calling initialize first a deadlock will occurr.
  54. */
  55. void synchronize();
  56. /**
  57. * @brief Sets a shared this pointer to this object. This MUST be called immediately after construction.
  58. *
  59. * @note Called automatically by the factory creation methods so user should not call this manually.
  60. */
  61. void setThisPtr(std::shared_ptr<CoreObject> ptrThis);
  62. /**
  63. * @brief Returns an unique identifier for this object.
  64. */
  65. UINT64 getInternalID() const { return mInternalID; }
  66. /**
  67. * @brief Schedules the object to be destroyed, and then deleted.
  68. *
  69. * @note You should never call this manually. It's meant for internal use only.
  70. */
  71. template<class T, class MemAlloc>
  72. static void _deleteDelayed(CoreObject* obj)
  73. {
  74. _deleteDelayedInternal(obj);
  75. if(obj->isInitialized())
  76. {
  77. std::shared_ptr<CoreObject> thisPtr(obj);
  78. obj->setThisPtr(thisPtr);
  79. obj->destroy();
  80. }
  81. else
  82. {
  83. cm_delete<MemAlloc, T>((T*)obj);
  84. }
  85. }
  86. /**
  87. * @brief Returns a shared_ptr version of "this" pointer.
  88. */
  89. std::shared_ptr<CoreObject> getThisPtr() const { return mThis.lock(); }
  90. protected:
  91. /**
  92. * @brief Frees all of the objects dynamically allocated memory. All derived classes that have something to free
  93. * should do it here instead of their destructor. All derived classes need to call this base method when they're done.
  94. *
  95. * @note Since this is scheduled to be executed on the core thread, normally you want to destroy all GPU specific resources here.
  96. */
  97. virtual void destroy_internal();
  98. /**
  99. * @brief Initializes all the internal resources of this object. Needs to be called before doing
  100. * any operations with the object. All derived classes also need to call this base method.
  101. *
  102. * @note Since this is scheduled to be executed on the core thread, normally you want to initialize all GPU specific resources here.
  103. */
  104. virtual void initialize_internal();
  105. static void _deleteDelayedInternal(CoreObject* obj);
  106. /**
  107. * @brief Queues a command to be executed on the core thread, without a return value.
  108. *
  109. * @note Requires a shared pointer to the object this function will be executed on, in order to
  110. * make sure the object is not deleted before the command executes. Can be null if the
  111. * function is static or global.
  112. */
  113. static void queueGpuCommand(std::shared_ptr<CoreObject>& obj, std::function<void()> func);
  114. /**
  115. * @brief Queues a command to be executed on the core thread, with a return value in the form of AsyncOp.
  116. *
  117. * @see AsyncOp
  118. *
  119. * @note Requires a shared pointer to the object this function will be executed on, in order to
  120. * make sure the object is not deleted before the command executes. Can be null if the
  121. * function is static or global.
  122. */
  123. static AsyncOp queueReturnGpuCommand(std::shared_ptr<CoreObject>& obj, std::function<void(AsyncOp&)> func);
  124. bool isScheduledToBeInitialized() const { return (mFlags & CGO_SCHEDULED_FOR_INIT) != 0; }
  125. bool isScheduledToBeDeleted() const { return (mFlags & CGO_SCHEDULED_FOR_DELETE) != 0; }
  126. bool requiresInitOnRenderThread() const { return (mFlags & CGO_INIT_ON_RENDER_THREAD) != 0; }
  127. void setIsInitialized(bool initialized) { mFlags = initialized ? mFlags | CGO_INITIALIZED : mFlags & ~CGO_INITIALIZED; }
  128. void setScheduledToBeInitialized(bool scheduled) { mFlags = scheduled ? mFlags | CGO_SCHEDULED_FOR_INIT : mFlags & ~CGO_SCHEDULED_FOR_INIT; }
  129. void setScheduledToBeDeleted(bool scheduled) { mFlags = scheduled ? mFlags | CGO_SCHEDULED_FOR_DELETE : mFlags & ~CGO_SCHEDULED_FOR_DELETE; }
  130. private:
  131. friend class CoreGpuObjectManager;
  132. volatile UINT8 mFlags;
  133. UINT64 mInternalID; // ID == 0 is not a valid ID
  134. std::weak_ptr<CoreObject> mThis;
  135. CM_STATIC_THREAD_SYNCHRONISER(mCoreGpuObjectLoadedCondition)
  136. CM_STATIC_MUTEX(mCoreGpuObjectLoadedMutex)
  137. static void queueInitializeGpuCommand(std::shared_ptr<CoreObject>& obj);
  138. static void queueDestroyGpuCommand(std::shared_ptr<CoreObject>& obj);
  139. static void executeGpuCommand(std::shared_ptr<CoreObject>& obj, std::function<void()> func);
  140. static void executeReturnGpuCommand(std::shared_ptr<CoreObject>& obj, std::function<void(AsyncOp&)> func, AsyncOp& op);
  141. };
  142. #define MAKE_CM_NEW_CORE(z, n, unused) \
  143. template<class Type, class MainAlloc, class PtrDataAlloc BOOST_PP_ENUM_TRAILING_PARAMS(n, class T)> \
  144. std::shared_ptr<Type> cm_core_ptr(BOOST_PP_ENUM_BINARY_PARAMS(n, T, t) ) { \
  145. return std::shared_ptr<Type>(cm_new<Type, MainAlloc>(BOOST_PP_ENUM_PARAMS (n, t)), &CoreObject::_deleteDelayed<Type, MainAlloc>, StdAlloc<PtrDataAlloc>()); \
  146. }
  147. BOOST_PP_REPEAT(15, MAKE_CM_NEW_CORE, ~)
  148. #undef MAKE_CM_NEW_CORE
  149. #define MAKE_CM_NEW_CORE(z, n, unused) \
  150. template<class Type, class MainAlloc BOOST_PP_ENUM_TRAILING_PARAMS(n, class T)> \
  151. std::shared_ptr<Type> cm_core_ptr(BOOST_PP_ENUM_BINARY_PARAMS(n, T, t) ) { \
  152. return std::shared_ptr<Type>(cm_new<Type, MainAlloc>(BOOST_PP_ENUM_PARAMS (n, t)), &CoreObject::_deleteDelayed<Type, MainAlloc>, StdAlloc<GenAlloc>()); \
  153. }
  154. BOOST_PP_REPEAT(15, MAKE_CM_NEW_CORE, ~)
  155. #undef MAKE_CM_NEW_CORE
  156. #define MAKE_CM_NEW_CORE(z, n, unused) \
  157. template<class Type BOOST_PP_ENUM_TRAILING_PARAMS(n, class T)> \
  158. std::shared_ptr<Type> cm_core_ptr(BOOST_PP_ENUM_BINARY_PARAMS(n, T, t) ) { \
  159. return std::shared_ptr<Type>(cm_new<Type, GenAlloc>(BOOST_PP_ENUM_PARAMS (n, t)), &CoreObject::_deleteDelayed<Type, GenAlloc>, StdAlloc<GenAlloc>()); \
  160. }
  161. BOOST_PP_REPEAT(15, MAKE_CM_NEW_CORE, ~)
  162. #undef MAKE_CM_NEW_CORE
  163. template<class Type, class MainAlloc>
  164. std::shared_ptr<Type> cm_core_ptr(Type* data)
  165. {
  166. return std::shared_ptr<Type>(data, &CoreObject::_deleteDelayed<Type, MainAlloc>, StdAlloc<GenAlloc>());
  167. }
  168. template<class Type, class MainAlloc, class PtrDataAlloc>
  169. std::shared_ptr<Type> cm_core_ptr(Type* data)
  170. {
  171. return std::shared_ptr<Type>(data, &CoreObject::_deleteDelayed<Type, MainAlloc>, StdAlloc<PtrDataAlloc>());
  172. }
  173. }