CmCoreObject.h 7.9 KB

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