CmCoreObject.h 8.2 KB

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