CmCoreObject.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmAsyncOp.h"
  4. #include <boost/preprocessor.hpp>
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief This class provides some common functionality that all low-level objects
  9. * used on the core thread need to implement.
  10. *
  11. * @note This involves initializing, keeping track of, and releasing 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. * Non-GPU core objects can normally be initialized on the caller thread.
  15. */
  16. class CM_EXPORT CoreObject
  17. {
  18. protected:
  19. /**
  20. * @brief Values that represent current state of the object
  21. */
  22. enum Flags
  23. {
  24. CGO_INITIALIZED = 0x01, /**< Object has been fully initialized and may be used. */
  25. CGO_INIT_ON_CORE_THREAD = 0x02, /**< Object requires initialization on core thread. */
  26. CGO_SCHEDULED_FOR_INIT = 0x04, /**< Object has been scheduled for initialization but core thread has not completed it yet. */
  27. CGO_SCHEDULED_FOR_DELETE = 0x08 /**< Object has been scheduled for deletion but core thread has not completed it yet. */
  28. };
  29. public:
  30. /**
  31. * @brief Constructs a new core object.
  32. *
  33. * @param requiresGpuInit (optional) If true the objects initialize_internal and destroy_internal methods
  34. * will be called from the core thread asynchronously. Otherwise they will be called
  35. * by the caller thread synchronously.
  36. */
  37. CoreObject(bool requiresGpuInit = true);
  38. virtual ~CoreObject();
  39. /**
  40. * @brief Destroys all GPU resources of this object.
  41. *
  42. * @note If is created with "CGO_INIT_ON_CORE_THREAD" flag destruction is not done immediately,
  43. * and is instead just scheduled on the core thread.
  44. * Unless called from core thread in which case it is executed immediately.
  45. * Objects without "CGO_INIT_ON_CORE_THREAD" flag are destructed immediately.
  46. */
  47. virtual void destroy();
  48. /**
  49. * @brief Initializes all the internal resources of this object. Should be called by the
  50. * factory creation methods automatically after construction and not by user directly.
  51. *
  52. * @note If is created with "CGO_INIT_ON_CORE_THREAD" flag initialization is not done immediately,
  53. * and is instead just scheduled on the core thread.
  54. * Unless called from core thread in which case it is executed immediately.
  55. * Objects without "CGO_INIT_ON_CORE_THREAD" flag are initialized immediately.
  56. */
  57. virtual void initialize();
  58. /**
  59. * @brief Returns true if the object has been properly initialized. You are not
  60. * allowed to call any methods on the resource until you are sure resource is initialized.
  61. *
  62. * @note Normally CPU objects are initialized on creation and this will never be false, and GPU
  63. * objects are initialized when the core thread processes them.
  64. */
  65. bool isInitialized() const { return (mFlags & CGO_INITIALIZED) != 0; }
  66. /**
  67. * @brief Blocks the current thread until the resource is fully initialized.
  68. *
  69. * @note If you call this without calling initialize first a deadlock will occur.
  70. * You should not call this from core thread.
  71. */
  72. void synchronize();
  73. /**
  74. * @brief Internal method. Sets a shared this pointer to this object. This MUST be called immediately after construction.
  75. *
  76. * @note Called automatically by the factory creation methods so user should not call this manually.
  77. */
  78. void _setThisPtr(std::shared_ptr<CoreObject> ptrThis);
  79. /**
  80. * @brief Returns an unique identifier for this object.
  81. */
  82. UINT64 getInternalID() const { return mInternalID; }
  83. /**
  84. * @brief Internal method. Schedules the object to be destroyed, and then deleted.
  85. */
  86. template<class T, class MemAlloc>
  87. static void _deleteDelayed(CoreObject* obj)
  88. {
  89. _deleteDelayedInternal(obj);
  90. if(obj->isInitialized())
  91. {
  92. std::shared_ptr<CoreObject> thisPtr(obj);
  93. obj->_setThisPtr(thisPtr);
  94. obj->destroy();
  95. }
  96. else
  97. {
  98. cm_delete<MemAlloc, T>((T*)obj);
  99. }
  100. }
  101. /**
  102. * @brief Returns a shared_ptr version of "this" pointer.
  103. */
  104. std::shared_ptr<CoreObject> getThisPtr() const { return mThis.lock(); }
  105. protected:
  106. /**
  107. * @brief Frees all of the objects dynamically allocated memory. All derived classes that have something to free
  108. * should do it here instead of their destructor. All derived classes need to call this base method when they're done.
  109. *
  110. * @note For objects with "CGO_INIT_ON_CORE_THREAD" flag this is scheduled to be executed on the core thread,
  111. * so normally you want to destroy all GPU specific resources here.
  112. */
  113. virtual void destroy_internal();
  114. /**
  115. * @brief Initializes all the internal resources of this object. Needs to be called before doing
  116. * any operations with the object. All derived classes also need to call this base method.
  117. *
  118. * @note For objects with "CGO_INIT_ON_CORE_THREAD" flag this is scheduled to be executed on the core thread,
  119. * so normally you want to initialize all GPU specific resources here.
  120. */
  121. virtual void initialize_internal();
  122. /**
  123. * @brief Performs some internal checks when an object is being deleted.
  124. */
  125. static void _deleteDelayedInternal(CoreObject* obj);
  126. /**
  127. * @brief Queues a command to be executed on the core thread, without a return value.
  128. *
  129. * @note Requires a shared pointer to the object this function will be executed on, in order to
  130. * make sure the object is not deleted before the command executes. Can be null if the
  131. * function is static or global.
  132. */
  133. static void queueGpuCommand(std::shared_ptr<CoreObject>& obj, std::function<void()> func);
  134. /**
  135. * @brief Queues a command to be executed on the core thread, with a return value in the form of AsyncOp.
  136. *
  137. * @see AsyncOp
  138. *
  139. * @note Requires a shared pointer to the object this function will be executed on, in order to
  140. * make sure the object is not deleted before the command executes. Can be null if the
  141. * function is static or global.
  142. */
  143. static AsyncOp queueReturnGpuCommand(std::shared_ptr<CoreObject>& obj, std::function<void(AsyncOp&)> func);
  144. bool isScheduledToBeInitialized() const { return (mFlags & CGO_SCHEDULED_FOR_INIT) != 0; }
  145. bool isScheduledToBeDeleted() const { return (mFlags & CGO_SCHEDULED_FOR_DELETE) != 0; }
  146. bool requiresInitOnCoreThread() const { return (mFlags & CGO_INIT_ON_CORE_THREAD) != 0; }
  147. void setIsInitialized(bool initialized) { mFlags = initialized ? mFlags | CGO_INITIALIZED : mFlags & ~CGO_INITIALIZED; }
  148. void setScheduledToBeInitialized(bool scheduled) { mFlags = scheduled ? mFlags | CGO_SCHEDULED_FOR_INIT : mFlags & ~CGO_SCHEDULED_FOR_INIT; }
  149. void setScheduledToBeDeleted(bool scheduled) { mFlags = scheduled ? mFlags | CGO_SCHEDULED_FOR_DELETE : mFlags & ~CGO_SCHEDULED_FOR_DELETE; }
  150. private:
  151. friend class CoreObjectManager;
  152. volatile UINT8 mFlags;
  153. UINT64 mInternalID; // ID == 0 is not a valid ID
  154. std::weak_ptr<CoreObject> mThis;
  155. CM_STATIC_THREAD_SYNCHRONISER(mCoreGpuObjectLoadedCondition)
  156. CM_STATIC_MUTEX(mCoreGpuObjectLoadedMutex)
  157. /**
  158. * @brief Queues object initialization command on the core thread. The command is added to the
  159. * primary core thread queue and will be executed as soon as the core thread is ready.
  160. */
  161. static void queueInitializeGpuCommand(std::shared_ptr<CoreObject>& obj);
  162. /**
  163. * @brief Queues object destruction command on the core thread. The command is added to the
  164. * core thread accessor of this thread and will be executed after accessor commands
  165. * are submitted and any previously queued commands are executed.
  166. *
  167. * @note It is up to the caller to ensure no other accessors attempt to use this object.
  168. */
  169. static void queueDestroyGpuCommand(std::shared_ptr<CoreObject>& obj);
  170. /**
  171. * @brief Helper wrapper method used for queuing commands with no return value on the core thread.
  172. */
  173. static void executeGpuCommand(std::shared_ptr<CoreObject>& obj, std::function<void()> func);
  174. /**
  175. * @brief Helper wrapper method used for queuing commands with a return value on the core thread.
  176. */
  177. static void executeReturnGpuCommand(std::shared_ptr<CoreObject>& obj, std::function<void(AsyncOp&)> func, AsyncOp& op);
  178. };
  179. /**
  180. * @brief Creates a new core object using the specified allocators and returns a shared pointer to it.
  181. *
  182. * @note All core thread object shared pointers must be created using this method or its overloads
  183. * and you should not create them manually.
  184. */
  185. #define MAKE_CM_NEW_CORE(z, n, unused) \
  186. template<class Type, class MainAlloc, class PtrDataAlloc BOOST_PP_ENUM_TRAILING_PARAMS(n, class T)> \
  187. std::shared_ptr<Type> cm_core_ptr(BOOST_PP_ENUM_BINARY_PARAMS(n, T, t) ) { \
  188. return std::shared_ptr<Type>(cm_new<Type, MainAlloc>(BOOST_PP_ENUM_PARAMS (n, t)), &CoreObject::_deleteDelayed<Type, MainAlloc>, StdAlloc<PtrDataAlloc>()); \
  189. }
  190. BOOST_PP_REPEAT(15, MAKE_CM_NEW_CORE, ~)
  191. #undef MAKE_CM_NEW_CORE
  192. /**
  193. * @brief Creates a new core object using the specified allocator and returns a shared pointer to it.
  194. *
  195. * @note All core thread object shared pointers must be created using this method or its overloads
  196. * and you should not create them manually.
  197. */
  198. #define MAKE_CM_NEW_CORE(z, n, unused) \
  199. template<class Type, class MainAlloc BOOST_PP_ENUM_TRAILING_PARAMS(n, class T)> \
  200. std::shared_ptr<Type> cm_core_ptr(BOOST_PP_ENUM_BINARY_PARAMS(n, T, t) ) { \
  201. return std::shared_ptr<Type>(cm_new<Type, MainAlloc>(BOOST_PP_ENUM_PARAMS (n, t)), &CoreObject::_deleteDelayed<Type, MainAlloc>, StdAlloc<GenAlloc>()); \
  202. }
  203. BOOST_PP_REPEAT(15, MAKE_CM_NEW_CORE, ~)
  204. #undef MAKE_CM_NEW_CORE
  205. /**
  206. * @brief Creates a new core object and returns a shared pointer to it.
  207. *
  208. * @note All core thread object shared pointers must be created using this method or its overloads
  209. * and you should not create them manually.
  210. */
  211. #define MAKE_CM_NEW_CORE(z, n, unused) \
  212. template<class Type BOOST_PP_ENUM_TRAILING_PARAMS(n, class T)> \
  213. std::shared_ptr<Type> cm_core_ptr(BOOST_PP_ENUM_BINARY_PARAMS(n, T, t) ) { \
  214. return std::shared_ptr<Type>(cm_new<Type, GenAlloc>(BOOST_PP_ENUM_PARAMS (n, t)), &CoreObject::_deleteDelayed<Type, GenAlloc>, StdAlloc<GenAlloc>()); \
  215. }
  216. BOOST_PP_REPEAT(15, MAKE_CM_NEW_CORE, ~)
  217. #undef MAKE_CM_NEW_CORE
  218. /**
  219. * @brief Creates a core object shared pointer using a previously constructed object.
  220. *
  221. * @note All core thread object shared pointers must be created using this method or its overloads
  222. * and you should not create them manually.
  223. */
  224. template<class Type, class MainAlloc>
  225. std::shared_ptr<Type> cm_core_ptr(Type* data)
  226. {
  227. return std::shared_ptr<Type>(data, &CoreObject::_deleteDelayed<Type, MainAlloc>, StdAlloc<GenAlloc>());
  228. }
  229. /**
  230. * @brief Creates a core object shared pointer using a previously constructed object.
  231. *
  232. * @note All core thread object shared pointers must be created using this method or its overloads
  233. * and you should not create them manually.
  234. */
  235. template<class Type, class MainAlloc, class PtrDataAlloc>
  236. std::shared_ptr<Type> cm_core_ptr(Type* data)
  237. {
  238. return std::shared_ptr<Type>(data, &CoreObject::_deleteDelayed<Type, MainAlloc>, StdAlloc<PtrDataAlloc>());
  239. }
  240. }