CmCoreObject.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 Schedules the object to be destroyed, and then deleted.
  61. *
  62. * @note You should never call this manually. It's meant for internal use only.
  63. */
  64. static void _deleteDelayed(CoreObject* obj);
  65. protected:
  66. /**
  67. * @brief Frees all of the objects dynamically allocated memory. All derived classes that have something to free
  68. * should do it here instead of their destructor. All derived classes need to call this base method when they're done.
  69. *
  70. * @note Since this is scheduled to be executed on the render thread, normally you want to destroy all GPU specific resources here.
  71. */
  72. virtual void destroy_internal();
  73. /**
  74. * @brief Initializes all the internal resources of this object. Needs to be called before doing
  75. * any operations with the object. All derived classes also need to call this base method.
  76. *
  77. * @note Since this is scheduled to be executed on the render thread, normally you want to initialize all GPU specific resources here.
  78. */
  79. virtual void initialize_internal();
  80. /**
  81. * @brief Returns a shared_ptr version of "this" pointer.
  82. */
  83. std::shared_ptr<CoreObject> getThisPtr() const { return mThis.lock(); }
  84. /**
  85. * @brief Queues a command to be executed on the render thread, without a return value.
  86. *
  87. * @note Requires a shared pointer to the object this function will be executed on, in order to
  88. * make sure the object is not deleted before the command executes. Can be null if the
  89. * function is static or global.
  90. */
  91. static void queueGpuCommand(std::shared_ptr<CoreObject>& obj, boost::function<void()> func);
  92. /**
  93. * @brief Queues a command to be executed on the render thread, with a return value in the form of AsyncOp.
  94. *
  95. * @see AsyncOp
  96. *
  97. * @note Requires a shared pointer to the object this function will be executed on, in order to
  98. * make sure the object is not deleted before the command executes. Can be null if the
  99. * function is static or global.
  100. */
  101. static AsyncOp queueReturnGpuCommand(std::shared_ptr<CoreObject>& obj, boost::function<void(AsyncOp&)> func);
  102. /**
  103. * @brief Returns an unique identifier for this object.
  104. */
  105. UINT64 getInternalID() const { return mInternalID; }
  106. bool isScheduledToBeInitialized() const { return (mFlags & CGO_SCHEDULED_FOR_INIT) != 0; }
  107. bool isScheduledToBeDeleted() const { return (mFlags & CGO_SCHEDULED_FOR_DELETE) != 0; }
  108. bool requiresInitOnRenderThread() const { return (mFlags & CGO_INIT_ON_RENDER_THREAD) != 0; }
  109. void setIsInitialized(bool initialized) { mFlags = initialized ? mFlags | CGO_INITIALIZED : mFlags & ~CGO_INITIALIZED; }
  110. void setScheduledToBeInitialized(bool scheduled) { mFlags = scheduled ? mFlags | CGO_SCHEDULED_FOR_INIT : mFlags & ~CGO_SCHEDULED_FOR_INIT; }
  111. void setScheduledToBeDeleted(bool scheduled) { mFlags = scheduled ? mFlags | CGO_SCHEDULED_FOR_DELETE : mFlags & ~CGO_SCHEDULED_FOR_DELETE; }
  112. private:
  113. friend class CoreGpuObjectManager;
  114. UINT8 mFlags;
  115. UINT64 mInternalID; // ID == 0 is not a valid ID
  116. std::weak_ptr<CoreObject> mThis;
  117. CM_STATIC_THREAD_SYNCHRONISER(mCoreGpuObjectLoadedCondition)
  118. CM_STATIC_MUTEX(mCoreGpuObjectLoadedMutex)
  119. static void executeGpuCommand(std::shared_ptr<CoreObject>& obj, boost::function<void()> func);
  120. static void executeReturnGpuCommand(std::shared_ptr<CoreObject>& obj, boost::function<void(AsyncOp&)> func, AsyncOp& op);
  121. };
  122. }