CmCoreGpuObject.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 CoreGpuObject
  16. {
  17. protected:
  18. enum Flags
  19. {
  20. CGO_INITIALIZED = 0x01,
  21. CGO_SCHEDULED_FOR_INIT = 0x02,
  22. CGO_SCHEDULED_FOR_DELETE = 0x04
  23. };
  24. public:
  25. CoreGpuObject();
  26. virtual ~CoreGpuObject();
  27. /**
  28. * @brief Destroys all GPU resources of this object.
  29. o *
  30. * @note Destruction is not done immediately, and is instead just scheduled on the
  31. * render thread. Unless called from render thread in which case it is executed right away.
  32. */
  33. virtual void destroy();
  34. /**
  35. * @brief Initializes all the internal resources of this object. Should be called by the
  36. * factory creation methods automatically after construction and not by user directly.
  37. *
  38. * @note Initialization is not done immediately, and is instead just scheduled on the
  39. * render thread. Unless called from render thread in which case it is executed right away.
  40. */
  41. virtual void initialize();
  42. /**
  43. * @brief Returns true if the object has been properly initialized. You are not
  44. * allowed to call any methods on the resource until you are sure resource is initialized.
  45. */
  46. bool isInitialized() const { return (mFlags & CGO_INITIALIZED) != 0; }
  47. /**
  48. * @brief Blocks the current thread until the resource is fully initialized.
  49. * If you call this without calling initialize first a deadlock will occurr.
  50. */
  51. void waitUntilInitialized();
  52. /**
  53. * @brief Sets a shared this pointer to this object. This MUST be called immediately after construction.
  54. *
  55. * @note Called automatically by the factory creation methods so user should not call this manually.
  56. */
  57. void setThisPtr(std::shared_ptr<CoreGpuObject> ptrThis);
  58. /**
  59. * @brief Schedules the object to be destroyed, and then deleted.
  60. *
  61. * @note You should never call this manually. It's meant for internal use only.
  62. */
  63. static void _deleteDelayed(CoreGpuObject* obj);
  64. protected:
  65. /**
  66. * @brief Frees all of the objects dynamically allocated memory. All derived classes that have something to free
  67. * should do it here instead of their destructor. All derived classes need to call this base method when they're done.
  68. *
  69. * @note Since this is scheduled to be executed on the render thread, normally you want to destroy all GPU specific resources here.
  70. */
  71. virtual void destroy_internal();
  72. /**
  73. * @brief Initializes all the internal resources of this object. Needs to be called before doing
  74. * any operations with the object. All derived classes also need to call this base method.
  75. *
  76. * @note Since this is scheduled to be executed on the render thread, normally you want to initialize all GPU specific resources here.
  77. */
  78. virtual void initialize_internal();
  79. /**
  80. * @brief Returns a shared_ptr version of "this" pointer.
  81. */
  82. std::shared_ptr<CoreGpuObject> getThisPtr() const { return mThis.lock(); }
  83. /**
  84. * @brief Queues a command to be executed on the render thread, without a return value.
  85. *
  86. * @note Requires a shared pointer to the object this function will be executed on, in order to
  87. * make sure the object is not deleted before the command executes. Can be null if the
  88. * function is static or global.
  89. */
  90. static void queueGpuCommand(std::shared_ptr<CoreGpuObject>& obj, boost::function<void()> func);
  91. /**
  92. * @brief Queues a command to be executed on the render thread, with a return value in the form of AsyncOp.
  93. *
  94. * @see AsyncOp
  95. *
  96. * @note Requires a shared pointer to the object this function will be executed on, in order to
  97. * make sure the object is not deleted before the command executes. Can be null if the
  98. * function is static or global.
  99. */
  100. static AsyncOp queueReturnGpuCommand(std::shared_ptr<CoreGpuObject>& obj, boost::function<void(AsyncOp&)> func);
  101. /**
  102. * @brief Returns an unique identifier for this object.
  103. */
  104. UINT64 getInternalID() const { return mInternalID; }
  105. bool isScheduledToBeInitialized() const { return (mFlags & CGO_SCHEDULED_FOR_INIT) != 0; }
  106. bool isScheduledToBeDeleted() const { return (mFlags & CGO_SCHEDULED_FOR_DELETE) != 0; }
  107. void setIsInitialized(bool initialized) { mFlags = initialized ? mFlags | CGO_INITIALIZED : mFlags & ~CGO_INITIALIZED; }
  108. void setScheduledToBeInitialized(bool scheduled) { mFlags = scheduled ? mFlags | CGO_SCHEDULED_FOR_INIT : mFlags & ~CGO_SCHEDULED_FOR_INIT; }
  109. void setScheduledToBeDeleted(bool scheduled) { mFlags = scheduled ? mFlags | CGO_SCHEDULED_FOR_DELETE : mFlags & ~CGO_SCHEDULED_FOR_DELETE; }
  110. private:
  111. friend class CoreGpuObjectManager;
  112. UINT8 mFlags;
  113. UINT64 mInternalID; // ID == 0 is not a valid ID
  114. std::weak_ptr<CoreGpuObject> mThis;
  115. CM_STATIC_THREAD_SYNCHRONISER(mCoreGpuObjectLoadedCondition)
  116. CM_STATIC_MUTEX(mCoreGpuObjectLoadedMutex)
  117. static void executeGpuCommand(std::shared_ptr<CoreGpuObject>& obj, boost::function<void()> func);
  118. static void executeReturnGpuCommand(std::shared_ptr<CoreGpuObject>& obj, boost::function<void(AsyncOp&)> func, AsyncOp& op);
  119. };
  120. }